-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
analytics.js
148 lines (135 loc) · 4.09 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
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
'use strict'
const fs = require('fs')
const { URL } = require('url')
// We can either use a process-wide object regularly saved to a JSON file,
// or a Redis equivalent (for multi-process / when the filesystem is unreliable.
let redis
let useRedis = false
if (process.env.REDISTOGO_URL) {
const { port, hostname, password } = new URL(process.env.REDISTOGO_URL)
redis = require('redis').createClient(port, hostname)
redis.auth(password)
useRedis = true
}
let analytics = {}
let autosaveIntervalId
const analyticsPath = process.env.SHIELDS_ANALYTICS_FILE || './analytics.json'
function performAutosave() {
const contents = JSON.stringify(analytics)
if (useRedis) {
redis.set(analyticsPath, contents)
} else {
fs.writeFileSync(analyticsPath, contents)
}
}
function scheduleAutosaving() {
const analyticsAutoSavePeriod = 10000
autosaveIntervalId = setInterval(performAutosave, analyticsAutoSavePeriod)
}
// For a clean shutdown.
function cancelAutosaving() {
if (autosaveIntervalId) {
clearInterval(autosaveIntervalId)
autosaveIntervalId = null
}
performAutosave()
}
function defaultAnalytics() {
const analytics = Object.create(null)
// In case something happens on the 36th.
analytics.vendorMonthly = new Array(36)
resetMonthlyAnalytics(analytics.vendorMonthly)
analytics.rawMonthly = new Array(36)
resetMonthlyAnalytics(analytics.rawMonthly)
analytics.vendorFlatMonthly = new Array(36)
resetMonthlyAnalytics(analytics.vendorFlatMonthly)
analytics.rawFlatMonthly = new Array(36)
resetMonthlyAnalytics(analytics.rawFlatMonthly)
analytics.vendorFlatSquareMonthly = new Array(36)
resetMonthlyAnalytics(analytics.vendorFlatSquareMonthly)
analytics.rawFlatSquareMonthly = new Array(36)
resetMonthlyAnalytics(analytics.rawFlatSquareMonthly)
return analytics
}
function load() {
const defaultAnalyticsObject = defaultAnalytics()
if (useRedis) {
redis.get(analyticsPath, (err, value) => {
if (err == null && value != null) {
// if/try/return trick:
// if error, then the rest of the function is run.
try {
analytics = JSON.parse(value)
// Extend analytics with a new value.
for (const key in defaultAnalyticsObject) {
if (!(key in analytics)) {
analytics[key] = defaultAnalyticsObject[key]
}
}
return
} catch (e) {
console.error('Invalid Redis analytics, resetting.')
console.error(e)
}
}
analytics = defaultAnalyticsObject
})
} else {
// Not using Redis.
try {
analytics = JSON.parse(fs.readFileSync(analyticsPath))
// Extend analytics with a new value.
for (const key in defaultAnalyticsObject) {
if (!(key in analytics)) {
analytics[key] = defaultAnalyticsObject[key]
}
}
} catch (e) {
if (e.code !== 'ENOENT') {
console.error('Invalid JSON file for analytics, resetting.')
console.error(e)
}
analytics = defaultAnalyticsObject
}
}
}
let lastDay = new Date().getDate()
function resetMonthlyAnalytics(monthlyAnalytics) {
for (let i = 0; i < monthlyAnalytics.length; i++) {
monthlyAnalytics[i] = 0
}
}
function incrMonthlyAnalytics(monthlyAnalytics) {
try {
const currentDay = new Date().getDate()
// If we changed month, reset empty days.
while (lastDay !== currentDay) {
// Assumption: at least a hit a month.
lastDay = (lastDay + 1) % monthlyAnalytics.length
monthlyAnalytics[lastDay] = 0
}
monthlyAnalytics[currentDay]++
} catch (e) {
console.error(e.stack)
}
}
function noteRequest(queryParams, match) {
incrMonthlyAnalytics(analytics.vendorMonthly)
if (queryParams.style === 'flat') {
incrMonthlyAnalytics(analytics.vendorFlatMonthly)
} else if (queryParams.style === 'flat-square') {
incrMonthlyAnalytics(analytics.vendorFlatSquareMonthly)
}
}
function setRoutes(server) {
server.ajax.on('analytics/v1', (json, end) => {
end(analytics)
})
}
module.exports = {
load,
scheduleAutosaving,
cancelAutosaving,
noteRequest,
setRoutes,
}