-
Notifications
You must be signed in to change notification settings - Fork 9
/
analytics.js
73 lines (67 loc) · 2.47 KB
/
analytics.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
const mongo = require('./utils/functions/mongo.js');
// Record daily and weekly user growth
async function collectData() {
let currentUsers = await mongo.db.collection('users').countDocuments({});
let currentQuestions =
(await mongo.db.collection('questions').countDocuments({})) +
(await mongo.db.collection('usaboQuestions').countDocuments({}));
let sitedata = await mongo.SiteData.findOne({ tag: 'QUESTIONS' }).exec();
let currentAttempts = 0;
Object.keys(sitedata.data.attempts).forEach(
(element) => (currentAttempts += sitedata.data.attempts[element])
);
let currentSolves = 0;
Object.keys(sitedata.data.solves).forEach(
(element) => (currentSolves += sitedata.data.solves[element])
);
return { currentUsers, currentQuestions, currentAttempts, currentSolves };
}
async function collectDailyData() {
let collectedData = await collectData();
let history = await mongo.SiteData.findOne({ tag: 'HISTORY_MONTH' }).exec();
history.data.userbase.push(collectedData.currentUsers);
history.data.questioncount.push(collectedData.currentQuestions);
history.data.attempts.push(collectedData.currentAttempts);
history.data.solves.push(collectedData.currentSolves);
Object.keys(history.data).forEach((element) => {
if (history.data[element].length > 30) {
history.data[element].shift();
}
});
await mongo.SiteData.updateOne(
{ tag: 'HISTORY_MONTH' },
{ $set: { data: history.data } }
).exec();
triggerAnalytics();
}
async function collectWeeklyData() {
let collectedData = await collectData();
let history = await mongo.SiteData.findOne({ tag: 'HISTORY_YEAR' }).exec();
history.data.userbase.push(collectedData.currentUsers);
history.data.questioncount.push(collectedData.currentQuestions);
history.data.attempts.push(collectedData.currentAttempts);
history.data.solves.push(collectedData.currentSolves);
Object.keys(history.data).forEach((element) => {
if (history.data[element].length > 52) {
history.data[element].shift();
}
});
await mongo.SiteData.updateOne(
{ tag: 'HISTORY_YEAR' },
{ $set: { data: history.data } }
).exec();
initializeAnalytics();
}
function initializeAnalytics() {
let time = Date.now(),
day = 24 * 60 * 60 * 1000,
week = 7 * 24 * 60 * 60 * 1000;
if (week - (time % week) == day - (time % day)) {
setTimeout(collectWeeklyData, week - (time % week));
} else {
setTimeout(collectDailyData, day - (time % day));
}
}
module.exports = {
initializeAnalytics,
};