forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyCountry.js
More file actions
128 lines (119 loc) · 3.96 KB
/
byCountry.js
File metadata and controls
128 lines (119 loc) · 3.96 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
const Table = require('cli-table3');
const _ = require('lodash');
const helpers = require('./helpers');
const api = require('./api');
const {
extraStats,
getConfirmed,
getActive,
getDeaths,
getRecovered,
getMortalityPer,
getRecoveredPer,
getEmoji,
getOneDayChange,
getOneWeekChange,
getTotalStats,
footer,
htmlTemplate,
} = require('./helpers');
function getDataByState(confirmed, deaths, recovered) {
const countryMap = {};
const lastUpdated = confirmed.last_updated;
const confirmedMap = _.keyBy(confirmed.locations, (i) => i.country + i.province);
const recoveredMap = _.keyBy(recovered.locations, (i) => i.country + i.province);
const deathsMap = _.keyBy(deaths.locations, (i) => i.country + i.province);
confirmed.locations.forEach(obj => {
const countryName = obj.country;
const provinceName = obj.province;
const mapKey = countryName + provinceName;
if (!countryMap[mapKey] && confirmedMap[mapKey].latest > 0) {
countryMap[mapKey] = {
country: countryName,
province: provinceName,
countryCode: obj.country_code,
confirmed: confirmedMap[mapKey].latest,
recovered: recoveredMap[mapKey].latest,
deaths: deathsMap[mapKey].latest,
confirmedByDay: helpers.historyObjToArr(confirmedMap[mapKey].history),
recoveredByDay: helpers.historyObjToArr(recoveredMap[mapKey].history),
deathsByDay: helpers.historyObjToArr(deathsMap[mapKey].history),
lastUpdated,
};
}
});
const countryArr = extraStats(
Object.keys(countryMap).map(key => countryMap[key])
);
return _.sortBy(countryArr, (o) => -o.confirmed);
}
exports.getJSONData = async () => {
const data = await api.getCoronaData();
const { confirmed, deaths, recovered } = data;
const countryData = getDataByState(confirmed, deaths, recovered);
const totalStats = getTotalStats(countryData);
totalStats.country = 'World';
return countryData.concat(totalStats);
};
exports.getJSONDataForCountry = async (countryCode) => {
const data = await api.getCoronaData();
const { confirmed, deaths, recovered } = data;
const countryData = getDataByState(confirmed, deaths, recovered)
.filter(obj => obj.countryCode === countryCode);
return countryData;
};
exports.getCountryTable = async ({
countryCode,
emojis = false,
isCurl = true,
minimal = false,
}) => {
const table = new Table({
head: helpers.getTableHeaders(emojis, 'State'),
chars: helpers.getTableBorders(minimal),
style: helpers.getTableStyles(minimal),
});
const data = await api.getCoronaData();
const { confirmed, deaths, recovered } = data;
const countryData = getDataByState(confirmed, deaths, recovered)
.filter(obj => obj.countryCode === countryCode);
if (countryData.length === 0) {
throw new Error(`Country code ${countryCode} does not match anything in the database.`);
}
const totalStats = getTotalStats(countryData);
table.push({
[countryData[0].country]: [
'Total',
getConfirmed(totalStats.confirmed),
getRecovered(totalStats.recovered),
getDeaths(totalStats.deaths),
getActive(totalStats.active),
getMortalityPer(totalStats.mortalityPer),
getRecoveredPer(totalStats.recoveredPer),
getOneDayChange(totalStats),
getOneWeekChange(totalStats),
]
});
if (countryData.length > 1) {
let rank = 1;
countryData.forEach(cd => {
const countryEmoji = getEmoji(cd.countryCode);
const values = [
cd.province,
getConfirmed(cd.confirmed),
getRecovered(cd.recovered),
getDeaths(cd.deaths),
getActive(cd.active),
getMortalityPer(cd.mortalityPer),
getRecoveredPer(cd.recoveredPer),
getOneDayChange(cd),
getOneWeekChange(cd),
...(emojis ? [countryEmoji] : [])
];
table.push({ [rank++]: values });
});
}
const { lastUpdated } = countryData[0];
const ret = table.toString() + footer(lastUpdated);
return isCurl ? ret : htmlTemplate(ret);
};