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
127 lines (124 loc) · 3.84 KB
/
byCountry.js
File metadata and controls
127 lines (124 loc) · 3.84 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
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 getDataByState(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[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(recoveredMap[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.getCountryTable = async (country) => {
const head = [
'',
'State',
'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 = getDataByState(confirmed, deaths, recovered, country)
.filter(obj => obj.country === country);
const totalStats = getTotalStats(countryData);
table.push({
[country]: [
'Total',
getConfirmed(totalStats.confirmed),
getRecovered(totalStats.recovered),
getDeaths(totalStats.deaths),
getActive(totalStats.active),
getMortalityPer(totalStats.mortalityPer),
getRecoveredPer(totalStats.recoveredPer),
getOneDayChange(totalStats),
getOneWeekChange(totalStats),
// '',
// getEmoji(countryData[0].countryCode),
]
})
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),
// getRateOfGrowth(cd),
// getEmoji(cd.countryCode),
]
table.push({ [rank++]: values })
});
}
return table.toString() + footer;
}