-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh-data.js
More file actions
169 lines (152 loc) · 5.02 KB
/
refresh-data.js
File metadata and controls
169 lines (152 loc) · 5.02 KB
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
const request = require("request");
const csv = require("csvtojson");
const fs = require("fs");
csv()
// Read source CSV file
.fromStream(
request.get(
"https://raw.githubusercontent.com/carranco-sga/Mexico-COVID-19_2022/main/Mexico_COVID19_CTD.csv"
)
)
// Parse resulting JSON object
.then((jsonObj) => {
// Debug message
console.log("CSV source file obtained successfully.");
// JSON results object
var mx_timeline = [];
var mx_total_timeline = [];
var minStartDate = new Date("2020-02-28");
var EndDate = new Date("2022-08-17");
var MexicoStatesKeyMap = {
"MX-AGU": "AGU",
"MX-BCN": "BCN",
"MX-BCS": "BCS",
"MX-CAM": "CAM",
"MX-CHP": "CHP",
"MX-CHH": "CHH",
"MX-CMX": "CMX",
"MX-COA": "COA",
"MX-COL": "COL",
"MX-DUR": "DUR",
"MX-GUA": "GUA",
"MX-GRO": "GRO",
"MX-HID": "HID",
"MX-JAL": "JAL",
"MX-MIC": "MIC",
"MX-MOR": "MOR",
"MX-MEX": "MEX",
"MX-NAY": "NAY",
"MX-NLE": "NLE",
"MX-OAX": "OAX",
"MX-PUE": "PUE",
"MX-QUE": "QUE",
"MX-ROO": "ROO",
"MX-SLP": "SLP",
"MX-SIN": "SIN",
"MX-SON": "SON",
"MX-TAB": "TAB",
"MX-TAM": "TAM",
"MX-TLA": "TLA",
"MX-VER": "VER",
"MX-YUC": "YUC",
"MX-ZAC": "ZAC",
};
// Iterate source data
for (let i = 0; i < jsonObj.length; i++) {
// Current row
currentRow = jsonObj[i];
// Get current row date as date object
lastDateString = currentRow.Fecha;
dateParts = lastDateString.split("-");
// month is 0-based, that's why we need dataParts[1] - 1
currentDate = new Date(+dateParts[0], dateParts[1] - 1, +dateParts[2]);
// Only process records in the desired range
if (currentDate >= minStartDate && currentDate <= EndDate) {
// Debug message
console.log("Latest date in source file: " + currentDate);
// Create resulting row object
var resultsByDateObject = {
date: currentRow.Fecha,
list: [],
};
for (var stateKey in MexicoStatesKeyMap) {
resultsByDateObject.list.push({
id: stateKey,
confirmed: parseInt(currentRow[MexicoStatesKeyMap[stateKey]]),
// confirmed_imported: parseInt(
// currentRow[MexicoStatesKeyMap[stateKey] + "_I"]
// ),
// confirmed_local: parseInt(
// currentRow[MexicoStatesKeyMap[stateKey] + "_L"]
// ),
deaths: parseInt(currentRow[MexicoStatesKeyMap[stateKey] + "_D"]),
recovered: parseInt(
currentRow[MexicoStatesKeyMap[stateKey] + "_R"]
),
suspect: parseInt(currentRow[MexicoStatesKeyMap[stateKey] + "_S"]),
});
}
// Add to result array
mx_timeline.push(resultsByDateObject);
}
}
// Debug message
console.log("mx_timeline generated without any errors.");
// console.log(JSON.stringify(mx_timeline))
for (let i = 0; i < jsonObj.length; i++) {
// Current row
var currentRow = jsonObj[i];
// Get current row date as date object
var lastDateString = currentRow.Fecha;
var dateParts = lastDateString.split("-");
// month is 0-based, that's why we need dataParts[1] - 1
var currentDate = new Date(
+dateParts[0],
dateParts[1] - 1,
+dateParts[2]
);
// Only process records in the desired range
if (currentDate >= minStartDate && currentDate <= EndDate) {
// Debug message
console.log("Latest date in source file: " + currentDate);
// Create resulting row object
mx_total_timeline.push({
date: currentRow.Fecha,
confirmed: parseInt(currentRow.Pos),
confirmed_imported: parseInt(currentRow.Pos_I),
confirmed_local: parseInt(currentRow.Pos_L),
confirmed_official: parseInt(currentRow.Pos_rep),
deaths: parseInt(currentRow.Deceased),
recovered: parseInt(currentRow.Recovered),
suspect: parseInt(currentRow.Susp),
suspect_official: parseInt(currentRow.Susp_rep),
tested: parseInt(currentRow.Tested_tot),
negative: parseInt(currentRow.Neg_rep),
});
}
}
// Debug message
console.log("mx_total_timeline object generated without any errors.");
// console.log(mx_total_timeline);
// Write results to file system
fs.writeFile(
"public/data/mx_total_timeline.json",
JSON.stringify(mx_total_timeline),
function (err) {
if (err) return console.log(err);
else
console.log(
"OK: mx_total_timeline > public/data/mx_total_timeline.json"
);
}
);
// Write results to file system
fs.writeFile(
"public/data/mx_timeline.json",
JSON.stringify(mx_timeline),
function (err) {
if (err) return console.log(err);
else console.log("OK: mx_timeline > public/data/mx_timeline.json");
}
);
});