This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.js
310 lines (286 loc) · 8.29 KB
/
updater.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
const fs = require('fs')
const path = require('path')
const https = require('https')
const PRODUCTION = process.env.production === 'true'
const COLUMN_MAPPING = {
'pruid': 'id',
'prname': 'name',
'date': 'date',
'numconf': 'confirmed',
'numprob': 'probable',
'numdeaths': 'deaths',
'numtotal': 'total',
'numtoday': 'new'
}
const AREAS = {
'1' : 'Canada',
'10': 'Newfoundland and Labrador',
'11': 'Prince Edward Island',
'12': 'Nova Scotia',
'13': 'New Brunswick',
'24': 'Québec',
'35': 'Ontario',
'46': 'Manitoba',
'47': 'Saskatchewan',
'48': 'Alberta',
'59': 'British Columbia',
'60': 'Yukon',
'61': 'Northwest Territories',
'62': 'Nunavut',
}
const IMPROVEMENT_TEXT = `<br>That's a <span class="flat">-{{yesterday-diff}}</span> improvement from yesterday and <span class="flat">-{{week-diff}}</span>
improvement from a week ago!`
// Run the show
getData(processData)
function getData(cb) {
if (PRODUCTION) {
getRemoteData(cb)
} else {
getLocalData('covid19.csv', cb)
}
}
function getTemplate(cb) {
if (PRODUCTION) {
https.get({
host: 'raw.githubusercontent.com',
port: 443,
path: '/20k-ultra/areweflatyet.ca/master/template.html'
}, (res) => {
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
cb(data)
})
}).on('error', e => {
console.error(e)
throw e
})
} else {
getLocalData('template.html', cb)
}
}
function getLocalData(file, cb) {
const CSV_PATH = path.join(__dirname, file)
fs.readFile(CSV_PATH, {
encoding: 'utf-8'
}, (err, data) => {
if (err) {
// Get remote data
getRemoteData(data => {
// Persist locally
persistFile(data, file, () => {
cb(data)
})
})
} else {
// Return local data
cb(data)
}
})
}
function getRemoteData(cb) {
https.get({
host: 'health-infobase.canada.ca',
port: 443,
path: '/src/data/covidLive/covid19.csv'
}, (res) => {
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
cb(data)
})
}).on('error', e => {
console.error(e.message)
throw e
})
}
function persistFile(data, fileName, cb) {
const FILE_PATH = path.join(__dirname, fileName)
fs.writeFile(FILE_PATH, data, (err) => {
if (err) {
throw err
}
cb()
})
}
function processData(data) {
const SORTED_DATA = parseCSV(data).sortById()
// Update template HTML
getTemplate(template => {
// Set Canada's values
template = setNationalData(template, SORTED_DATA)
// Set province values
template = setProvinceData(template, SORTED_DATA)
// Set generated timestamp
const GENERATED_TIMESTAMP = new RegExp('{{generated-timestamp}}', 'gm')
template = template.replace(GENERATED_TIMESTAMP, new Date())
// Save updated templated
persistFile(template, 'index.html', () => {
console.log('Done!')
})
})
}
function setNationalData(template, data) {
const NATIONAL_KEY = '1'
const AVERAGE_CURVE = averageCurve(data[NATIONAL_KEY])
const CURVE = normalizeCurve(AVERAGE_CURVE)
const MAIN_TEXT = new RegExp('{{main-text}}', 'gm')
// Set main text
template = template.replace(MAIN_TEXT, getTextForValue(CURVE))
// Set todays curve
template = setCurve(template, NATIONAL_KEY, CURVE)
// Set yesterday difference
const DAY_DIFF = normalizeCurve(CURVE - averageCurve(data[NATIONAL_KEY].slice(0, (data[NATIONAL_KEY].length - 1))))
template = template.replace(`{{${NATIONAL_KEY}-yesterday-diff}}`, DAY_DIFF)
// Set week difference
const WEEK_DIFF = normalizeCurve(CURVE - averageCurve(data[NATIONAL_KEY].slice(0, (data[NATIONAL_KEY].length - 7))))
return template.replace(`{{${NATIONAL_KEY}-week-diff}}`, WEEK_DIFF)
}
function setProvinceData(template, data) {
return Object.keys(data)
.filter(key => {
return Object.keys(AREAS).includes(key)
})
.reduce((modifiedTemplate, provinceKey) => {
const AVERAGE_CURVE = averageCurve(data[provinceKey])
const CURVE = normalizeCurve(AVERAGE_CURVE)
// Set todays curve
modifiedTemplate = setCurve(modifiedTemplate, provinceKey, CURVE)
// Set yesterday difference
const DAY_DIFF = normalizeCurve(CURVE - averageCurve(data[provinceKey].slice(0, (data[provinceKey].length - 1))))
modifiedTemplate = modifiedTemplate.replace(`{{${provinceKey}-yesterday-diff}}`, DAY_DIFF)
// Set week difference
const WEEK_DIFF = normalizeCurve(CURVE - averageCurve(data[provinceKey].slice(0, (data[provinceKey].length - 7))))
return modifiedTemplate.replace(`{{${provinceKey}-week-diff}}`, WEEK_DIFF)
}, template)
}
function normalizeCurve(value) {
if (isNaN(value)) return 0
return parseFloat(value.toFixed(4))
}
function getStyleClass(value) {
if (value > 15) {
return 'soon'
} else if (value <= 14.99 && value >= 10) {
return 'progress'
} else if (value <= 9.99 && value >= 5.2) {
return 'working'
} else if (value <= 5.19 && value >= 2) {
return 'almost'
} else if (value <= 1.99 && value >= 0.8) {
return 'almost'
} else {
return 'flat'
}
}
function getTextForValue(value) {
if (value > 15) {
return 'Keep going!'
} else if (value <= 14.9900 && value >= 10.0000) {
return 'Making progress!'
} else if (value <= 9.9900 && value >= 5.2000) {
return 'It\'s working!'
} else if (value <= 5.1900 && value >= 2.0000) {
return 'Getting closer!'
} else if (value <= 1.9900 && value >= 0.8000) {
return 'Almost there!'
} else {
return 'YES!'
}
}
function averageCurve(areaData) {
const SPREAD = 10
const BUFFER = 1
const DATA_LENGTH = areaData.length
// Check if area has enough data
if (areaData[areaData.length - 1].total < 100 || DATA_LENGTH < SPREAD) {
return NaN
}
return (new Array(SPREAD))
.fill(undefined)
.reduce((acc, _, index) => {
const FROM = areaData[DATA_LENGTH - (BUFFER + index)]
const NOW = areaData[DATA_LENGTH - index]
if (FROM && NOW) {
return acc + angle(0, Math.log(FROM.total), BUFFER, Math.log(NOW.total))
} else {
return acc
}
}, 0) / (SPREAD - 1)
}
function parseCSV(data) {
let rows = data.split('\n')
const HEADINGS = rows.shift().split(',')
return rows
.map(row => {
const COLUMNS = removeEscapedCommas(row).split(',')
return HEADINGS.reduce((acc, value, index) => {
if (COLUMN_MAPPING[value]) {
acc[COLUMN_MAPPING[value]] = COLUMNS[index]
}
return acc
}, {})
})
}
function removeEscapedCommas(csvLine) {
if (csvLine.split('"').length < 2) return csvLine
let firstQuotes = csvLine.indexOf('"')
if (firstQuotes !== -1) {
let nextQuotes = csvLine.substring(firstQuotes + 1).indexOf('"')
let target = csvLine.substring(firstQuotes + 1, firstQuotes + nextQuotes + 1)
csvLine = csvLine.replace(`"${target}"`, target.replace(/,/g, ''))
// Check for more quotes
if (csvLine.split('"').length >= 2) {
csvLine = removeEscapedCommas(csvLine)
}
}
return csvLine
}
function angle(cx, cy, ex, ey) {
let dy = ey - cy
let dx = ex - cx
let theta = Math.atan2(dy, dx) // range (-PI, PI]
theta *= 180 / Math.PI // rads to degs, range (-180, 180]
return theta
}
function setCurve(template, areaId, curve) {
const PATTERNS = [`{{${areaId}-name}}`, `{{${areaId}-class}}`, `{{${areaId}-curve}}`]
return PATTERNS.reduce((modifiedTemplate, pattern, index) => {
switch (index) {
case 0:
return replaceValue(modifiedTemplate, pattern, AREAS[areaId])
break
case 1:
return replaceValue(modifiedTemplate, pattern, getStyleClass(curve))
break
case 2:
return replaceValue(modifiedTemplate, pattern, curve)
break
default:
return modifiedTemplate
}
}, template)
}
function replaceValue(text, pattern, value) {
const REGEX = new RegExp(pattern, 'gm')
return text.replace(REGEX, value)
}
String.prototype.updateValue = function (regex, value) {
return this.replace(regex, value)
}
Array.prototype.sortById = function () {
return this.reduce((sorted, value) => {
if (Array.isArray(sorted[value.id])) {
sorted[value.id].push(value)
} else {
if (value.id.length > 0) {
sorted[value.id] = [value]
}
}
return sorted
}, {})
}