-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
212 lines (170 loc) · 5.87 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
const csv = require('csv')
const fs = require('fs');
const { promisify } = require("util");
const fetch = require('node-fetch');
const stats = require("stats-lite")
const wcc = require('world-countries-capitals');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
var NodeGeocoder = require('node-geocoder');
const redis = require("redis");
const cache = redis.createClient();
const getCacheAsync = promisify(cache.get).bind(cache);
const setCacheAsync = promisify(cache.set).bind(cache);
const fahrenheitToCelsius = require('fahrenheit-to-celsius');
const overrideCountryCity = Object.freeze({
China: 'Wuhan',
Philippines: 'Manila',
"Czech Republic": "Prague"
})
const getValue = async (key) => {
const data = await getCacheAsync(key);
if (!data) {
return null;
}
return JSON.parse(data);
}
const setValue = async (key, value) => {
return await setCacheAsync(key, JSON.stringify(value));
}
const fetchWithCache = async (uri) => {
try {
let data = await getValue(uri);
if (!data || data.error) {
console.log("fetching: ", uri);
data = await fetch(uri);
data = await data.json();
if (data.error) {
throw new Error(data.error);
}
await setValue(uri, data);
}
return data;
} catch (error) {
console.log({ error });
return null;
}
}
var geocoderOptions = {
provider: 'google',
// Optional depending on the providers
httpAdapter: 'https', // Default
apiKey: process.env.API_KEY, // for Mapquest, OpenCage, Google Premier
formatter: null // 'gpx', 'string', ...
};
var geocoder = NodeGeocoder(geocoderOptions);
const calculateMedianGrowthRate = (country, data) => {
const countryData = data.filter(row => row[1] === country);
const totalCases = countryData[countryData.length - 1][4];
if (totalCases < 75) {
throw new Error("not enough data");
}
const growthRates = countryData.reduce((acc, row, idx) => {
if (idx === 0) {
return acc;
}
return [...acc, (row[4] - countryData[idx - 1][4]) / countryData[idx - 1][4]]
}, []);
const median = stats.median(growthRates);
const mean = stats.mean(growthRates);
return { median, mean };
}
const getAllTimesFebruar = () => {
return Array.from({ length: 29 }, (_, idx) => `2020-02-${idx < 9 ? "0" : ""}${idx + 1}T12:00:00`);
}
const getCapitalAverageTemperatureFebrurary = async ({ longitude, latitude, capital }) => {
const times = getAllTimesFebruar();
const uris = times.map(t => `https://api.darksky.net/forecast/${process.env.DARK_SKY_KEY}/${latitude},${longitude},${t}?exclude=daily,flags,minutely,daily,alerts`)
const temps = []
const humids = []
for (const uri of uris) {
try {
const { currently: { temperature, humidity } } = await fetchWithCache(uri);
temps.push(fahrenheitToCelsius(temperature));
humids.push(humidity);
} catch (error) {
console.log(error)
}
}
const averageTemp = stats.mean(temps);
const averageHumid = stats.mean(humids);
return [averageTemp, averageHumid];
}
const processCountry = async ({ country, totalCases }, data) => {
try {
let capital = overrideCountryCity[country];
if (!capital) {
const countryData = wcc.getCountryDetailsByName(country);
capital = countryData[0].capital;
}
// Get the coordinates
const [{ latitude, longitude, countryCode }] = await geocoder.geocode(`${capital}, ${country}`);
const { median, mean } = calculateMedianGrowthRate(country, data);
const [temp, humid] = await getCapitalAverageTemperatureFebrurary({ latitude, longitude });
return { country, capital, mean, median, temp, humid, totalCases };
} catch (error) {
console.log("error for: " + country);
}
return null;
}
const parseData = () => {
return new Promise((r, e) => {
const data = [];
const countries = {};
let idx = 0;
fs.createReadStream('full_data.csv')
.pipe(csv.parse())
.on('data', (row) => {
if (idx++ === 0) {
return;
}
// exlude if less than 5 cases
if (row[4] > 4) {
data.push(row);
countries[row[1]] = {
country: row[1],
city: row[6],
totalCases: row[4]
}
}
})
.on('end', () => {
return r({
data, countries: Object.values(countries)
});
});
});
}
const saveData = async (data) => {
const csvWriter = createCsvWriter({
path: 'result.csv',
header: [
{ id: 'country', title: 'Country' },
{ id: 'capital', title: 'Capital' },
{ id: 'median', title: 'Median growth' },
{ id: 'mean', title: 'Mean growth' },
{ id: 'temp', title: 'Average Temp' },
{ id: 'humid', title: 'Average humidity' },
{ id: 'totalCases', title: 'Total cases' },
]
});
await csvWriter.writeRecords(data);
}
const processAll = async () => {
const { data, countries } = await parseData();
const cc = [...countries];
let result = [];
let idx = 0;
for (country of countries) {
try {
const d = await processCountry(country, data);
// console.log(idx++ + " out of: " + cc.length);
result.push(d);
} catch (error) {
console.log(error);
}
}
result = result.filter(Boolean).filter(row => !isNaN(row.median));
console.log(result.length + ' countries')
await saveData(result);
}
processAll();