forked from mayeaux/nodetube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetCache.js
211 lines (156 loc) · 5.78 KB
/
setCache.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
const Upload = require('../models/index').Upload;
const View = require('../models/index').View;
const User = require('../models/index').User;
const Subscription = require('../models/index').Subscription;
const React = require('../models/index').React;
const Comment = require('../models/index').Comment;
const SiteVisit = require('../models/index').SiteVisit;
const SearchQuery = require('../models/index').SearchQuery;
const moment = require('moment');
const redisClient = require('../config/redis');
const logCaching = process.env.LOG_CACHING;
let viewAmount, channelAmount, mediaAmount;
async function setIndexValues(){
if(logCaching == 'true'){
console.log('Setting index values');
console.log('Calculating view amounts');
}
// view amount is for the old view amount
// TODO: have to implement this as real: true only when validation is done
viewAmount = await View.estimatedDocumentCount({});
if(logCaching == 'true'){
console.log('View amount calculated, calculating channel amount');
}
channelAmount = await User.estimatedDocumentCount({});
if(logCaching == 'true'){
console.log('Channel amount calculated, calculating upload amount');
}
mediaAmount = await Upload.estimatedDocumentCount({});
if(logCaching == 'true'){
console.log('Upload amount calculated, calculating view amount');
}
// set object properly
redisClient.hmset('indexValues', {
viewAmount,
channelAmount,
mediaAmount
});
if(logCaching == 'true'){
console.log('Set index values');
}
}
// setTimeout(setIndexValues, 1000 * 60 * 2);
//
// setInterval(function(){
// setIndexValues()
// }, 1000 * 60 * 20);
/** THIS IS FOR DAILY STATS, VIEWS ALSO USED IN POPULAR **/
// TODO: refactor to do via count
async function getAmountsPerPeriods(Model, objectName){
const totalDocumentAmount = await Model.estimatedDocumentCount({});
if(logCaching == 'true'){
console.log(`Got total ${objectName} counted`);
}
// build dates
var monthAgo = moment().subtract(30, 'days').toDate();
var weekAgo = moment().subtract(7, 'days').toDate();
var dayAgo = moment().subtract(24, 'hours').toDate();
var hourAgo = moment().subtract(1, 'hours').toDate();
var minuteAgo = moment().subtract(1, 'minutes').toDate();
// find the views
const lastMonthAmount= await Model.countDocuments({ createdAt: { $gte: monthAgo } });
const lastWeekAmount = await Model.countDocuments({ createdAt: { $gte: weekAgo } });
const lastDayAmount = await Model.countDocuments({ createdAt: { $gte: dayAgo } });
const lastHourAmount = await Model.countDocuments({ createdAt: { $gte: hourAgo } });
const lastMinuteAmount = await Model.countDocuments({ createdAt: { $gte: minuteAgo } });
return{
name: objectName,
alltime: totalDocumentAmount,
month: lastMonthAmount,
week: lastWeekAmount,
day: lastDayAmount,
hour: lastHourAmount,
minute: lastMinuteAmount
};
}
// async function testThing(){
// const uploads = await Upload.find({}).select('createdAt');
// var monthAgo = moment().subtract(30, 'days').toDate();
// var weekAgo = moment().subtract(7, 'days').toDate();
// var dayAgo = moment().subtract(24, 'hours').toDate();
// var hourAgo = moment().subtract(1, 'hours').toDate();
// var minuteAgo = moment().subtract(1, 'minutes').toDate();
//
// const lastMonthUploads = _.filter(uploads, function(upload){
// return upload.createdAt > monthAgo
// })
//
// console.log(lastMonthUploads.length);
//
//
// }
//
// testThing();
async function setDailyStats(){
if(logCaching == 'true'){
console.log('Setting daily stats');
console.log('Getting uploads');
}
const uploads = await getAmountsPerPeriods(Upload, 'uploads');
await redisClient.setAsync('dailyStatsUploads', JSON.stringify(uploads));
if(logCaching == 'true'){
console.log('Uploads set, moving on');
console.log('Getting users');
}
const users = await getAmountsPerPeriods(User, 'users');
await redisClient.setAsync('dailyStatsUsers', JSON.stringify(users));
if(logCaching == 'true'){
console.log('Users set, moving on');
console.log('Getting subscriptions');
}
const subscriptions = await getAmountsPerPeriods(Subscription, 'subscriptions');
await redisClient.setAsync('dailyStatsSubscriptions', JSON.stringify(subscriptions));
if(logCaching == 'true'){
console.log('Subscriptions set, moving on');
console.log('Getting reacts');
}
const reacts = await getAmountsPerPeriods(React, 'reacts');
await redisClient.setAsync('dailyStatsReacts', JSON.stringify(reacts));
if(logCaching == 'true'){
console.log('Reacts set, moving on');
console.log('Getting searches');
}
const searches = await getAmountsPerPeriods(SearchQuery, 'searches');
await redisClient.setAsync('dailyStatsSearches', JSON.stringify(searches));
if(logCaching == 'true'){
console.log('Searches set, moving on');
console.log('Getting comments');
}
const comments = await getAmountsPerPeriods(Comment, 'comments');
await redisClient.setAsync('dailyStatsComments', JSON.stringify(comments));
if(logCaching == 'true'){
console.log('Comments set, moving on');
console.log('Getting views');
}
const views = await getAmountsPerPeriods(View, 'views');
await redisClient.setAsync('dailyStatsViews', JSON.stringify(views));
if(logCaching == 'true'){
console.log('Views set, moving on');
console.log('Getting siteVisits');
}
const siteVisits = await getAmountsPerPeriods(SiteVisit, 'siteVisits');
await redisClient.setAsync('dailyStatsSiteVisits', JSON.stringify(siteVisits));
if(logCaching == 'true'){
console.log('SiteVisit set, moving on');
console.log('Set daily stats');
}
}
module.exports = {
setDailyStats,
setIndexValues
};
// setDailyStats();
// setInterval(function(){
// setDailyStats();
// }, 1000 * 60 * 30);
//