-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatePopularities.js
349 lines (323 loc) · 12 KB
/
updatePopularities.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const MONGO_URI = process.env.MONGODB_URI;
const MongoClient = require('mongodb').MongoClient;
const sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);
const wtimeout = require('./server_code/utils').wtimeout;
// TODO indexes
// TODO move most of the code out of this file into one combined/encapsulated file with the rest of the
// related logic that is currently found in updateSummaries.js
function handleError(err) {
if (err !== null) {
console.error(err);
console.trace("Caught from:");
process.exit(1);
}
}
function updateViews(db, viewsTimeInterval, articleId) {
let timeBucketCollName;
let sumFromDate;
if (viewsTimeInterval === 'daily_views') {
timeBucketCollName = 'time_buckets_for_daily_views';
sumFromDate = new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/));
} else if (viewsTimeInterval === 'weekly_views') {
timeBucketCollName = 'time_buckets_for_weekly_views';
sumFromDate = new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 7 /*week*/));
} else if (viewsTimeInterval === 'monthly_views') {
timeBucketCollName = 'time_buckets_for_monthly_views';
sumFromDate = new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 30 /*month*/));
} else if (viewsTimeInterval === 'yearly_views') {
timeBucketCollName = 'time_buckets_for_yearly_views';
sumFromDate = new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 365 /*year*/));
}
db.collection(timeBucketCollName, (err, tbColl) => {
if (err !== null) {
throw err;
} else {
tbColl.aggregate([
{ $match: {
articleId: articleId,
timeBucket: {$gt: sumFromDate}
}
},
{ $group: {
_id: null,
totalViews: { $sum: '$count' } }
}
],
function(err, result) {
if (err !== null) {
handleError(err);
}
db.collection('article', (err, articleColl) => {
if (err !== null) {
handleError(err);
} else {
const hasViews = result.length !== 0;
let views;
if (hasViews) {
views = result[0].totalViews;
} else {
views = 0;
}
const setVal = {};
setVal[viewsTimeInterval + '.lastUpdated'] = new Date();
setVal[viewsTimeInterval + '.views'] = views;
articleColl.updateOne(
{
_id: articleId,
},
{
$set: setVal
}
).catch(function(err) {
handleError(err);
});
}
});
}
);
}
});
}
const howOftenToUpdateDaily = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/; // one hour
const howOftenToUpdateWeekly = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 7; // seven hours
const howOftenToUpdateMonthly = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/; // one day
const howOftenToUpdateYearly = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 7 /*week*/; // one week
/*
* Finds an article whose viewsTimeInterval (of the form 'daily_views', 'weekly_views', ...) hasn't been updated
* in a while. Then it locks this article for a period of time by setting a timestamp so that any other processes
* won't process this same article.
*
* Resolves with article id if there is an article that is ready to be updated, else resolves with null.
*/
function processOneInterval(db, timeInterval, attributeName) {
const prom = new Promise(function(resolve, reject) {
// NOTE: if you change these values, you may also need to change the acceptable amount of time
// that an entry can go without being updated (look in monitorSummariesHealth() )
let howOftenToUpdate;
if (process.env.NODE_ENV === 'development') {
howOftenToUpdate = 1000 /*sec*/ * 10; // 10 seconds
} else {
if (timeInterval === 'daily') {
howOftenToUpdate = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/; // one hour
} else if (timeInterval === 'weekly') {
howOftenToUpdate = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 7; // seven hours
} else if (timeInterval === 'monthly') {
howOftenToUpdate = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/; // one day
} else if (timeInterval === 'yearly') {
howOftenToUpdate = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 7 /*week*/; // one week
}
}
const threshold = new Date(Date.now() - howOftenToUpdate);
const howLongToLock = 1000 /*sec*/ * 30; // 30 seconds
db.collection('article', (err, articleColl) => {
if (err !== null) {
handleError(err);
} else {
const filter = {};
filter[attributeName + '.lastUpdated'] = {$lt: threshold};
filter[attributeName + '.lockedUntil'] = {$lt: new Date()}; // lock so that multiple workers can divide up work
// filter will look something like this
//{
// daily_views.lastUpdated': {$lt: threshold},
// daily_views.lockedUntil': {$lt: new Date()}
//}
const setVal = {};
setVal[attributeName + '.lockedUntil'] = new Date(Date.now() + howLongToLock);
articleColl.findOneAndUpdate(
filter,
{
$set: setVal
}
).then(function(result) {
if (result.value !== null) {
resolve(result.value._id);
} else {
resolve(null);
}
}
).catch(function(err){handleError(err)});
}
});
});
return prom;
}
/*
TODO when we insert an article, do we need to initialize these values?
*/
function monitorHealth(db) {
if (process.env.NODE_ENV === 'development') {
return;
}
const acceptableAmountOfTimeSinceLastUpdate = {
'daily': howOftenToUpdateDaily + (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/),
'weekly': howOftenToUpdateWeekly + (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 2 /*2 hours*/),
'monthly': howOftenToUpdateMonthly + (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 7 /*7 hours*/),
'yearly': howOftenToUpdateYearly + (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*1 day*/)
}
db.collection('random_stuff', (err, randColl) => {
if (err !== null) {
throw err;
} else {
randColl.findOne(
{
_id: 'summariesLastMonitored',
},
{
}
).then(function(result) {
const howOftenToMonitor = 1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 7; /*7 hours*/
if (result == null || result.lastMonitoredAt < new Date(Date.now() - howOftenToMonitor)) {
db.collection('article', (err, articleColl) => {
if (err !== null) {
throw err;
} else {
articleColl.find(
{
$or: [
{'daily_views.lastUpdated':{$lt: acceptableAmountOfTimeSinceLastUpdate.daily}},
{'weekly_views.lastUpdated':{$lt: acceptableAmountOfTimeSinceLastUpdate.weekly}},
{'monthly_views.lastUpdated':{$lt: acceptableAmountOfTimeSinceLastUpdate.monthly}},
{'yearly_views.lastUpdated':{$lt: acceptableAmountOfTimeSinceLastUpdate.yearly}},
]
}
).count().then(function(count) {
const prom = new Promise(function(resolve, reject) {
if (count) {
sendgrid.send({
to: process.env.ALERT_EMAIL_ADDRESS,
from: 'noreply@' + process.env.DOMAIN_NAME,
subject: process.env.DOMAIN_NAME + ' ALERT - most viewed summary out of date',
text: 'Health check failed.'
}, function (err, json) {
if (err) {
reject(err);
} else {
resolve("Health check failed. Alerted via email.");
}
}
);
} else {
resolve("Everything looks good.")
}
});
return prom;
}, function(err) {
handleError(err);
}
).then(function() {
randColl.updateOne(
{
_id: 'summariesLastMonitored',
},
{
$set: {'lastMonitoredAt': new Date()}
},
{
upsert: true
}
)
}, function(err) {
handleError(err);
}
).then(function(r){}, function(err){handleError(err)});
}
});
}
}, function(err) {
handleError(err);
}
);
}
});
}
function deleteOldTimeBucketEntries(db) {
const tbCollectionNames = [
'time_buckets_for_daily',
'time_buckets_for_weekly',
'time_buckets_for_monthly',
'time_buckets_for_yearly'
];
const whenToDelete = {
'time_buckets_for_daily': new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 40)),
'time_buckets_for_weekly': new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 10)),
'time_buckets_for_monthly': new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 40)),
'time_buckets_for_yearly': new Date(Date.now() - (1000 /*sec*/ * 60 /*min*/ * 60 /*hour*/ * 24 /*day*/ * 400))
};
for (let i = 0; i < tbCollectionNames.length; i++) {
const collectionName = tbCollectionNames[i]
db.collection(collectionName, (err, tbColl) => {
if (err !== null) {
throw err;
} else {
tbColl.deleteMany({
timeBucket: {$lt: whenToDelete[collectionName]}
}).then(function(r){}, function(err) {
handleError(err);
});
}
});
}
}
function processAll(db) {
// play with these ratios if one summary needs more processing than another.
const numProcessesForDaily = 1;
const numProcessesForWeekly = 1;
const numProcessesForMonthly = 1;
const numProcessesForYearly = 1;
monitorHealth(db); //TODO monitor health should happen outside of this function so that it runs for sure.
for (let i = 0; i < numProcessesForDaily; i++) {
processOneInterval(db, 'daily', 'daily_views').then(function(articleId) {
if (articleId !== null) {
updateViews(db, 'daily_views', articleId);
}
}).catch(function(err) {
handleError(err);
});
}
for (let i = 0; i < numProcessesForWeekly; i++) {
processOneInterval(db, 'weekly', 'weekly_views').then(function(articleId) {
if (articleId !== null) {
updateViews(db, 'weekly_views', articleId);
}
}).catch(function(err) {
handleError(err);
});
}
for (let i = 0; i < numProcessesForMonthly; i++) {
processOneInterval(db, 'monthly', 'monthly_views').then(function(articleId) {
if (articleId !== null) {
updateViews(db, 'monthly_views', articleId);
}
}).catch(function(err) {
handleError(err);
});
}
for (let i = 0; i < numProcessesForYearly; i++) {
processOneInterval(db, 'yearly', 'yearly_views').then(function(articleId) {
if (articleId !== null) {
updateViews(db, 'yearly_views', articleId);
}
}).catch(function(err) {
handleError(err);
});
}
//deleteOldTimeBucketEntries(db);
}
MongoClient.connect(MONGO_URI,
{
db: {
wtimeout: wtimeout
}
},
(err, db) => {
if (err !== null) {
handleError(err);
} else {
setInterval(function() {
for (let i = 0; i < 10; i++) {
processAll(db);
}
}, 10000
);
}
});