-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcleanup.js
348 lines (298 loc) · 10.4 KB
/
cleanup.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
const SQL = require('@nearform/sql')
const AWS = require('aws-sdk')
const { zonedTimeToUtc, utcToZonedTime } = require('date-fns-tz')
const fetch = require('node-fetch')
const {
withDatabase,
getExpiryConfig,
getTimeZone,
getENXLogoEnabled,
getAPHLServerDetails,
getCheckinSummaryEnabled,
runIfDev
} = require('./utils')
async function summariseCheckins(client) {
const enabled = await getCheckinSummaryEnabled()
if (!enabled) {
console.log('Checkin summay not enabled')
return
}
const timeZone = await getTimeZone()
const query = SQL`INSERT INTO checkin_summary (event_date, age, sex, county, town, checkins)
SELECT created_at, coalesce(demographics->>'ageRange', 'u') as agedata, coalesce(demographics->>'sex', 'u') as sexdata, coalesce(trim(split_part(demographics->>'locality', ',', 1)), 'u') as county
, coalesce(trim(split_part(demographics->>'locality', ',', 2)), 'u') as town, COUNT(*) as checkins FROM check_ins
WHERE created_at >= (now() AT TIME ZONE ${timeZone})::DATE - 1
GROUP BY created_at, agedata, sexdata, county, town
ON CONFLICT ON CONSTRAINT checkin_summary_pkey
DO UPDATE SET checkins = EXCLUDED.checkins
WHERE checkin_summary.event_date = EXCLUDED.event_date AND checkin_summary.age = EXCLUDED.age AND checkin_summary.sex = EXCLUDED.sex
AND checkin_summary.county = EXCLUDED.county AND checkin_summary.town = EXCLUDED.town`
// UPSERT into summary table
const { rows } = await client.query(query)
console.log('Summary checkin counts created', rows.length)
}
async function createAPHLVerificationServerMetrics(client) {
const details = await getAPHLServerDetails()
if (details.server === '') {
console.log('No APHL server configured, ignoring stats')
return
}
const response = await fetch(`${details.server}/api/stats/realm.json`, {
method: 'GET',
headers: {
'X-API-Key': details.key,
'Content-Type': 'application/json'
}
})
if (response.status === 200) {
const data = await response.json()
const runDate = new Date()
runDate.setHours(0, 0, 0, 0)
runDate.setDate(runDate.getDate() - 1)
const dataSet = data.statistics.filter(
(s) => new Date(s.date.substring(0, 10)).getTime() >= runDate.getTime()
)
for (let i = 0; i < dataSet.length; i++) {
const metricsDate = dataSet[i].date
const metrics = dataSet[i].data
const sql = SQL`
INSERT INTO metrics (date, event, os, version, value)
VALUES (${metricsDate}, 'APHL_CODES_ISSUES', '', '', ${metrics.codes_issued}),
(${metricsDate}, 'APHL_CODES_CLAIMED', '', '', ${metrics.codes_claimed}),
(${metricsDate}, 'APHL_CODES_INVALID', '', '', ${metrics.codes_invalid}),
(${metricsDate}, 'APHL_CLAIM_MEAN_AGE_SECONDS', '', '', ${metrics.code_claim_mean_age_seconds})
ON CONFLICT ON CONSTRAINT metrics_pkey
DO UPDATE SET value = EXCLUDED.value
WHERE metrics.date = EXCLUDED.date AND metrics.event = EXCLUDED.event `
await client.query(sql)
}
}
}
async function createRegistrationMetrics(client) {
const timeZone = await getTimeZone()
const sql = SQL`
INSERT INTO metrics (date, event, os, version, value)
SELECT
(created_at AT TIME ZONE ${timeZone})::DATE as groupDate,
'REGISTER',
'',
'',
COUNT(id)
FROM registrations
WHERE
(nonce != '123456' OR nonce IS NULL)
AND
(
((created_at AT TIME ZONE ${timeZone})::DATE =
(CURRENT_TIMESTAMP AT TIME ZONE ${timeZone})::DATE) OR
((created_at AT TIME ZONE ${timeZone})::DATE =
(CURRENT_TIMESTAMP AT TIME ZONE ${timeZone})::DATE - 1)
)
GROUP BY groupDate
ON CONFLICT ON CONSTRAINT metrics_pkey
DO UPDATE SET value = EXCLUDED.value
RETURNING value`
const { rows } = await client.query(sql)
console.log(`updated register metric for last 2 days ${rows.length}`)
}
async function storeENXHourlyData(client, metrics) {
const dbData = {}
metrics.forEach((metric) => {
if (!dbData[metric.date]) {
dbData[metric.date] = {}
}
dbData[metric.date][metric.metric] = metric.value || 0
})
const sql = SQL`
INSERT INTO enx_onboarding_requests (event_date, all_counts, success_counts, settings_counts, enbuddy_counts, healthenbuddy_counts)
VALUES `
Object.keys(dbData).forEach((key, index) => {
const metric = dbData[key]
sql.append(
SQL`(${new Date(key)}, ${metric.ENX_LOGO_REQUESTS_ALL || 0}, ${
metric.ENX_LOGO_REQUESTS_200 || 0
}, ${metric.ENX_LOGO_REQUESTS_SETTINGS || 0}, ${
metric.ENX_LOGO_REQUESTS_ENBUDDY || 0
}, ${metric.ENX_LOGO_REQUESTS_HEALTHENBUDDY || 0})`
)
if (index < Object.keys(dbData).length - 1) {
sql.append(SQL`,`)
}
})
sql.append(SQL`
ON CONFLICT ON CONSTRAINT enx_onboarding_requests_event_date_key
DO UPDATE SET all_counts = EXCLUDED.all_counts, success_counts = EXCLUDED.success_counts, settings_counts = EXCLUDED.settings_counts,
enbuddy_counts = EXCLUDED.enbuddy_counts, healthenbuddy_counts = EXCLUDED.healthenbuddy_counts
WHERE enx_onboarding_requests.event_date = EXCLUDED.event_date
`)
if (Object.keys(dbData).length > 0) {
await client.query(sql)
}
}
async function storeENXLogoRequests(client, metrics) {
// include zero metrics also for now
const nonZeroMetrics = metrics // .filter(m => m.value > 0)
const sql = SQL`
INSERT INTO metrics (date, event, os, version, value)
VALUES `
nonZeroMetrics.forEach((metric, index) => {
sql.append(SQL`(${metric.date}, ${metric.metric}, '', '', ${metric.value})`)
if (index < nonZeroMetrics.length - 1) {
sql.append(SQL`,`)
}
})
sql.append(SQL`
ON CONFLICT ON CONSTRAINT metrics_pkey
DO UPDATE SET value = EXCLUDED.value
WHERE metrics.date = EXCLUDED.date AND metrics.event = EXCLUDED.event
`)
if (nonZeroMetrics.length > 0) {
await client.query(sql)
}
}
function buildMetricsQuery(period) {
const metrics = [
{ metric: 'enxlogoall', label: 'ENX_LOGO_REQUESTS_ALL' },
{ metric: 'enxlogo200', label: 'ENX_LOGO_REQUESTS_200' },
{ metric: 'enxlogosettings', label: 'ENX_LOGO_REQUESTS_SETTINGS' },
{ metric: 'enxlogoenbuddy', label: 'ENX_LOGO_REQUESTS_ENBUDDY' },
{
metric: 'enxlogohealthenbuddy',
label: 'ENX_LOGO_REQUESTS_HEALTHENBUDDY'
}
]
const metricsData = []
metrics.forEach((m) => {
metricsData.push({
Id: `en_${m.metric}`,
MetricStat: {
Metric: {
Namespace: 'ApiGateway',
MetricName: m.metric
},
Period: period,
Stat: 'Sum'
},
Label: `${m.label}`,
ReturnData: true
})
})
return metricsData
}
async function createENXLogoMetrics(client, event, hourlyBreakdown) {
const timeZone = await getTimeZone()
const enxLogoEnabled = await getENXLogoEnabled()
if (!enxLogoEnabled) {
console.log('Skipping enx logo checks, not enabled')
return
}
const cw = new AWS.CloudWatch()
let startDate = new Date()
startDate.setHours(0, 0, 0, 0)
if (event && event.startDate) {
startDate = new Date(event.startDate)
}
startDate.setDate(startDate.getDate() - 1)
const endDate = new Date(startDate)
endDate.setHours(23, 59, 59, 999)
endDate.setDate(endDate.getDate() + 1)
const params = {
MetricDataQueries: buildMetricsQuery(hourlyBreakdown ? 3600 : 86400),
StartTime: zonedTimeToUtc(startDate, timeZone),
EndTime: zonedTimeToUtc(endDate, timeZone)
}
const logData = await new Promise((resolve, reject) => {
cw.getMetricData(params, function (err, data) {
if (err) {
console.log(err) // an error occurred
reject(err)
} else {
resolve(data)
}
})
})
const results = logData.MetricDataResults
const dbMetrics = []
if (!hourlyBreakdown) {
results.forEach((response) => {
dbMetrics.push({
date: endDate,
metric: response.Label,
value:
response.Values && response.Values.length > 0 ? response.Values[0] : 0
})
dbMetrics.push({
date: startDate,
metric: response.Label,
value:
response.Values && response.Values.length > 1 ? response.Values[1] : 0
})
})
await storeENXLogoRequests(client, dbMetrics)
} else {
results.forEach((response) => {
response.Timestamps.forEach((t, index) => {
dbMetrics.push({
date: utcToZonedTime(t, timeZone),
metric: response.Label,
value:
response.Values && response.Values.length >= index
? response.Values[index]
: 0
})
})
})
await storeENXHourlyData(client, dbMetrics)
}
console.log(
'updated enx logo requests metrics',
startDate,
endDate,
hourlyBreakdown
)
}
async function removeExpiredCodes(client, codeLifetime) {
const sql = SQL`
DELETE FROM verifications
WHERE created_at < CURRENT_TIMESTAMP - ${`${codeLifetime} mins`}::INTERVAL
`
const { rowCount } = await client.query(sql)
console.log(`deleted ${rowCount} codes older than ${codeLifetime} minutes`)
}
async function removeExpiredTokens(client, tokenLifetime) {
const sql = SQL`
DELETE FROM upload_tokens
WHERE created_at < CURRENT_TIMESTAMP - ${`${tokenLifetime} mins`}::INTERVAL
`
const { rowCount } = await client.query(sql)
console.log(`deleted ${rowCount} tokens older than ${tokenLifetime} minutes`)
}
async function removeOldNoticesKeys(client, noticeLifetime) {
const sql = SQL`
DELETE FROM notices
WHERE created_at < CURRENT_TIMESTAMP - ${`${noticeLifetime} mins`}::INTERVAL
`
const { rowCount } = await client.query(sql)
console.log(
`deleted ${rowCount} notices keys older than ${noticeLifetime} minutes`
)
}
exports.handler = async function (event) {
const {
codeLifetime,
tokenLifetime,
noticeLifetime
} = await getExpiryConfig()
await withDatabase(async (client) => {
await createRegistrationMetrics(client)
await removeExpiredCodes(client, codeLifetime)
await removeExpiredTokens(client, tokenLifetime)
await removeOldNoticesKeys(client, noticeLifetime)
await createENXLogoMetrics(client, event, false)
await createENXLogoMetrics(client, event, true)
await createAPHLVerificationServerMetrics(client)
await summariseCheckins(client)
})
return true
}
runIfDev(exports.handler)