forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorona.js
More file actions
141 lines (136 loc) · 4.38 KB
/
corona.js
File metadata and controls
141 lines (136 loc) · 4.38 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
const Table = require('cli-table3');
const axios = require('axios');
const _ = require('lodash');
const chalk = require('chalk');
const helpers = require('./helpers');
const {
getCountry,
getConfirmed,
getActive,
getDeaths,
getRecovered,
getMortalityPer,
getRecoveredPer,
getEmoji,
calActive,
calMortalityPer,
calRecoveredPer,
getOneDayChange,
getOneWeekChange,
getRateOfGrowth,
getTotalStats,
footer,
} = require('./helpers');
function getDataByCountry(confirmed, deaths, recovered) {
const countryMap = {};
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[countryName]) {
countryMap[countryName] = {
country: countryName,
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(recoveredMap[mapKey].history),
};
} else {
countryMap[countryName].confirmed += confirmedMap[mapKey].latest;
countryMap[countryName].recovered += recoveredMap[mapKey].latest;
countryMap[countryName].deaths += deathsMap[mapKey].latest;
countryMap[countryName].confirmedByDay = helpers.addArr(
countryMap[countryName].confirmedByDay,
helpers.historyObjToArr(confirmedMap[mapKey].history)
);
countryMap[countryName].recoveredByDay = helpers.addArr(
countryMap[countryName].recoveredByDay,
helpers.historyObjToArr(recoveredMap[mapKey].history)
);
countryMap[countryName].deathsByDay = helpers.addArr(
countryMap[countryName].deathsByDay,
helpers.historyObjToArr(deathsMap[mapKey].history)
);
}
});
const countryArr = extraStats(
Object.keys(countryMap).map(key => countryMap[key])
);
return _.sortBy(countryArr, (o) => -o.confirmed)
}
function extraStats(dataArr) {
return dataArr.map(obj => Object.assign({}, obj,
{
active: calActive(obj),
mortalityPer: calMortalityPer(obj),
recoveredPer: calRecoveredPer(obj),
})
);
}
exports.getCompleteTable = async () => {
const head = [
'',
'Country',
'Confirmed ✅',
'Recovered 😃',
'Deaths 😞',
'Active 😷',
'Mortality %',
'Recovered %',
'1 Day 🔺',
'1 Week 🔺',
// 'RoG',
// '🏳',
];
const table = new Table({
head,
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
});
const result = await axios('https://coronavirus-tracker-api.herokuapp.com/all');
const { latest, confirmed, deaths, recovered } = result.data;
const countryData = getDataByCountry(confirmed, deaths, recovered)
const worldStats = getTotalStats(countryData);
table.push({
'': [
'World',
getConfirmed(worldStats.confirmed),
getRecovered(worldStats.recovered),
getDeaths(worldStats.deaths),
getActive(worldStats.active),
getMortalityPer(worldStats.mortalityPer),
getRecoveredPer(worldStats.recoveredPer),
getOneDayChange(worldStats),
getOneWeekChange(worldStats),
// '',
// '🌎'
]
})
let rank = 1;
countryData.forEach(cd => {
const countryEmoji = getEmoji(cd.countryCode);
const values = [
getCountry(cd.country),
getConfirmed(cd.confirmed),
getRecovered(cd.recovered),
getDeaths(cd.deaths),
getActive(cd.active),
getMortalityPer(cd.mortalityPer),
getRecoveredPer(cd.recoveredPer),
getOneDayChange(cd),
getOneWeekChange(cd),
// getRateOfGrowth(cd),
// getEmoji(cd.countryCode),
]
table.push({ [rank++]: values })
});
return table.toString() + footer;
}