-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-spreadsheet.js
48 lines (42 loc) · 1.39 KB
/
update-spreadsheet.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
const fs = require("fs")
const moment = require("moment")
const neatCsv = require("neat-csv")
const ObjectsToCsv = require("objects-to-csv")
const readFile = path =>
new Promise((resolve, reject) => {
fs.readFile(path, async (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
const updateData = async metrics => {
const hospitalizationsFileName = "COVID-19-hospitalizations.csv"
const casesByDateData = await readFile("src/data/state-cases-by-date.csv")
const casesByDate = await neatCsv(casesByDateData)
const caseByDate = casesByDate.find(item => {
return moment(item["date"], "YYYY-MM-DD").isSame(
moment(metrics["Date"], "MM/DD/YYYY")
)
})
if (caseByDate) {
metrics["Deaths"] = caseByDate.newDeaths
metrics["New Cases"] = caseByDate.newCases
const hospitalizationsData = await readFile(
`src/data/${hospitalizationsFileName}`
)
const hospitalizations = await neatCsv(hospitalizationsData)
const index = hospitalizations.findIndex(item => {
return moment(item["Date"], "MM/DD/YYYY").isSame(
moment(metrics["Date"], "MM/DD/YYYY")
)
})
if (index == -1) {
hospitalizations.push(metrics)
} else {
hospitalizations[index] = metrics
}
const csv = new ObjectsToCsv(hospitalizations)
await csv.toDisk(`src/data/${hospitalizationsFileName}`)
}
}
module.exports = updateData