Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
122 lines (107 loc) · 2.72 KB

File metadata and controls

122 lines (107 loc) · 2.72 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Module Dependencies
*/
var async = require('async'),
Keen = require('keen.io');
/**
* Upload an image
* URL: /uploads
* Method: GET
* Auth Required: Yes
* @param {string} user_id This required param specifies the user id to filter by
* @returns {object} Returns a 201 status code with the upload object
*/
server.get('/stats/:user_id', function(req, res, next) {
// extract query params
var params = req.params || {};
var userId = params.user_id;
// async waterfall (see: https://github.com/caolan/async)
async.waterfall(
[
// keen query
function(cb) {
// configure instance of keenClient
var keenClient = Keen.configure({
projectId: config.keen.projectId,
writeKey: config.keen.writeKey,
readKey: config.keen.readKey,
masterKey: config.keen.masterKey,
});
// build query
var keenQuery = new Keen.Query('count', {
event_collection: 'views',
timeframe: 'this_30_days',
group_by: 'postId',
filters: [
{
property_name: 'postAuthorId',
operator: 'eq',
property_value: userId,
},
],
});
// run query
keenClient.run(keenQuery, function(err, res) {
if (err) {
cb(err);
}
function compareItems(a, b) {
return (a['result'] - b['result']) * -1;
}
var sortedItems = res.result;
sortedItems.sort(compareItems);
var topItems = sortedItems.slice(0, 5);
var postIds = [];
var postViewCounts = {};
// loop through top items
topItems.forEach(function(value) {
postIds.push(value['postId']);
postViewCounts[value['postId']] = value['result'];
});
cb(null, postIds, postViewCounts);
});
},
// database query
function(postIds, postViewCounts, cb) {
// run query if we have results
if (postIds.length == 0) {
var uploads = [];
cb(null, uploads);
} else {
db.query(
'SELECT * FROM uploads WHERE id IN (?)',
[postIds],
function(err, uploads) {
if (err) {
log.error(err);
return next(
new restify.InternalError(err.message),
);
}
uploads.forEach(function(upload) {
upload.viewCount = postViewCounts[upload.id];
});
function compareUploads(a, b) {
return (a['viewCount'] - b['viewCount']) * -1;
}
uploads.sort(compareUploads);
cb(null, uploads);
},
);
}
},
],
function(err, result) {
// catch all errors
if (err) {
// use global logger to log to console
log.error(err);
// return error message to client
return next(new restify.InternalError(err));
}
// send response to client
res.send(200, { mostViewed: result });
return next();
},
);
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.