forked from kring/nsw-covid-to-terria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (44 loc) · 2.44 KB
/
index.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
const papaparse = require("papaparse");
const fs = require("fs");
const uniq = require("lodash/uniq");
const sortedUniq = require("lodash/sortedUniq");
const moment = require("moment");
const csv = fs.readFileSync("covid-19-cases-by-notification-date-and-postcode-local-health-district-and-local-government-area.csv", "utf8");
const parsed = papaparse.parse(csv, {
header: true
});
const postcodes = uniq(parsed.data.map(row => row.postcode).filter(postcode => postcode && postcode.trim().length > 0 && postcode.trim() !== "0"));
parsed.data.forEach(row => {
row.moment = moment(row.notification_date, "YYYY-MM-DD");
row.jsdate = row.moment.valueOf();
});
let startDate = new Date('2021-05-31');
const dates = sortedUniq(parsed.data.map(row => row.jsdate).filter(date => !Number.isNaN(date) && date >= startDate).sort());
//const dates = sortedUniq(parsed.data.map(row => row.jsdate).filter(date => !Number.isNaN(date)).sort());
const last3Days = [];
const last7Days = [];
const total = [];
let result = "date,postcode,New Cases This Day,New Cases in Last 3 Days,New Cases in Last 7 Days,Total Cases\n";
for (let i = 0; i < postcodes.length; ++i) {
last3Days[postcodes[i]] = [];
last7Days[postcodes[i]] = [];
total[postcodes[i]] = [];
}
for (let i = 0; i < dates.length; ++i) {
const thisDate = dates[i];
const thisMoment = moment(thisDate);
const onThisDate = parsed.data.filter(row => moment(row.notification_date, "YYYY-MM-DD").valueOf() === thisDate);
for (let j = 0; j < postcodes.length; ++j) {
const thisPostcode = postcodes[j];
const inThisPostcode = onThisDate.filter(row => row.postcode === thisPostcode);
total[thisPostcode].push(...inThisPostcode);
const lastThreeDays = thisMoment.clone().subtract(2, "days");
last3Days[thisPostcode] = last3Days[thisPostcode].filter(row => !row.moment.isBefore(lastThreeDays));
last3Days[thisPostcode].push(...inThisPostcode);
const lastSevenDays = thisMoment.clone().subtract(6, "days");
last7Days[thisPostcode] = last7Days[thisPostcode].filter(row => !row.moment.isBefore(lastSevenDays));
last7Days[thisPostcode].push(...inThisPostcode);
result += `${thisMoment.toISOString(true)},${thisPostcode},${inThisPostcode.length},${last3Days[thisPostcode].length},${last7Days[thisPostcode].length},${total[thisPostcode].length}\n`;
}
}
fs.writeFileSync("covid-19-cases-by-notification-date-and-postcode-local-health-district-and-local-government-area-for-terria.csv", result, "utf8");