forked from covid19india/api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsheet-to-json.js
55 lines (50 loc) · 1.96 KB
/
sheet-to-json.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
const moment = require("moment");
const rawData = require('./raw_data');
const { fetchData, writeData } = require("./lib");
const { SHEET, SHEET_STATEWISE_TAB, SHEET_CASES_TIME_SERIES_TAB, SHEET_KEY_VALUES_TAB, SHEET_Tested_Numbers_ICMR_Data, FILE_DATA } = require("./lib/constants");
const tabs = {
statewise: SHEET_STATEWISE_TAB,
cases_time_series: SHEET_CASES_TIME_SERIES_TAB,
key_values: SHEET_KEY_VALUES_TAB,
tested:SHEET_Tested_Numbers_ICMR_Data,
};
function getDelta(state) {
return rawData.raw_data.reduce((stat, row) => {
let stateName = row.detectedstate;
let isToday = moment().utcOffset(330).isSame(moment(row.dateannounced, "DD-MM-YYYY"), "day");
if (stateName && (stateName === state || state === "Total") && isToday) {
let currentStatus = row.currentstatus;
if (currentStatus) {
stat.confirmed += 1;
switch (currentStatus) {
case "Hospitalized":
stat.active += 1;
break;
case "Recovered":
stat.recovered += 1;
break;
case "Deceased":
stat.deaths += 1;
break;
}
} else {
console.error("Current status is empty in sheet for patient:", row.patientnumber);
}
}
return stat;
}, {active: 0, confirmed: 0, deaths: 0, recovered: 0});
}
async function task() {
console.log(`Fetching data from sheets: ${SHEET}...`);
let data = await fetchData({ sheet: SHEET, tabs });
data.statewise = data.statewise.map(data => Object.assign(data, {delta: getDelta(data.state)}));
console.log(`Writing data to json file: ${FILE_DATA}...`);
await writeData({file: FILE_DATA, data});
console.log("Operation completed!");
}
(async function main() {
console.log("Running task on start...");
await task();
console.log("Created Json File With Updated Contents");
})();
// source https://github.com/reustle/covid19japan/blob/master/scripts/cache-spreadsheet-data/cache-sheet.js , and made the changes accordingly