forked from pomber/covid19
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.js
91 lines (80 loc) · 3.07 KB
/
action.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
const fs = require("fs");
const path = require("path");
const parse = require("csv-parse/lib/sync");
const WORKSPACE = process.env.GITHUB_WORKSPACE;
const DATA_REPO = "data"; // from main.yml checkout action path
const MAIN_REPO = "main"; // from main.yml checkout action path
const FILENAME_CONFIRMED = "time_series_19-covid-Confirmed.csv";
const FILENAME_DEATHS = "time_series_19-covid-Deaths.csv";
const FILENAME_RECOVERED = "time_series_19-covid-Recovered.csv";
const dataPath = path.join(
WORKSPACE,
DATA_REPO,
"csse_covid_19_data",
"csse_covid_19_time_series"
);
const countryOutputPath = path.join(WORKSPACE, MAIN_REPO, "docs", "timeseries.json");
function extract(filename) {
const csv = fs.readFileSync(path.resolve(dataPath, filename));
const [headers, ...rows] = parse(csv);
const [province, country, lat, long, ...dates] = headers;
const countryCounts = {};
rows.forEach(([province, country, lat, long, ...counts]) => {
// if country hasn't been added to countryCounts, add empty obj
countryCounts[country] = countryCounts[country] || {mainData: {} };
// if province is truthy, add province structure now now
if (province) {
countryCounts[country].Provinces = countryCounts[country]["Provinces"] || {}
countryCounts[country]["Provinces"][province] = {}
}
dates.forEach((date, i) => {
countryCounts[country].mainData[date] = countryCounts[country].mainData[date] || 0;
countryCounts[country].mainData[date] += +counts[i];
if (province) {
countryCounts[country]["Provinces"][province][date] = counts[i];
}
});
});
return [countryCounts, dates];
}
function buildSeries (dates, confirmed, deaths, recovered) {
//console.log(confirmed['3/16/20'])
//console.log(dates)
return dates.map(date => {
const [month, day] = date.split("/");
return {
date: `2020-${month}-${day}`,
confirmed: confirmed[date],
deaths: deaths[date],
recovered: recovered[date]
};
});
}
const [confirmed, dates] = extract(FILENAME_CONFIRMED);
const [deaths] = extract(FILENAME_DEATHS);
const [recovered] = extract(FILENAME_RECOVERED);
const countries = Object.keys(confirmed);
const mainResults = {};
const provSeries = {};
countries.forEach(country => {
let c = confirmed[country].mainData,
d = deaths[country].mainData,
r = recovered[country].mainData;
mainResults[country] = buildSeries(dates, c, d, r);
if (confirmed[country].Provinces) {
provSeries[country] = {}
let provinces = Object.keys(confirmed[country].Provinces)
for (p of provinces) {
let dp = deaths[country].Provinces[p],
cp = confirmed[country].Provinces[p],
rp = recovered[country].Provinces[p]
provSeries[country][p] = buildSeries(dates, cp, dp, rp);
}
}
});
fs.writeFileSync(countryOutputPath, JSON.stringify(mainResults, null, 2));
let byProvince = Object.keys(provSeries);
byProvince.forEach (ctry => {
let thisPath = path.join(WORKSPACE, MAIN_REPO, "docs", `provinces-${ctry}.json`);
fs.writeFileSync(thisPath, JSON.stringify(provSeries[ctry], null, 2))
})