Skip to content

Commit 3d305c3

Browse files
authored
Merge pull request OSSPhilippines#33 from WarenGonzaga/dev
Global History Chart and Fixes
2 parents 8b25a5f + d977b98 commit 3d305c3

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,19 @@ curl -L covid19.trackercli.com/philippines
106106
curl -L covid19.trackercli.com/ph
107107
```
108108

109-
#### Country with History Chart
109+
#### Global Tracking with History Chart
110+
111+
```bash
112+
# shows global result with history chart
113+
curl -L covid19.trackercli.com/history
114+
```
115+
116+
```bash
117+
# shows by default a global history chart
118+
curl -L covid19.trackercli.com/history/all
119+
```
120+
121+
#### Country Tracking with History Chart
110122

111123
```bash
112124
# shows result with history chart
@@ -277,7 +289,7 @@ Some cups of my coffees goes to the foundation via [#OneAgainstCovid19 by PayMay
277289

278290
## Supporters and Backers
279291

280-
* [J. Archer](https://www.buymeacoffee.com/WarenGonzaga/c/151732), [Gonzalo Montes](https://www.buymeacoffee.com/WarenGonzaga/c/155002), [Keynell](https://www.buymeacoffee.com/WarenGonzaga/c/156960), [Scullum](https://www.buymeacoffee.com/WarenGonzaga), [TX_Atheist](https://www.buymeacoffee.com/WarenGonzaga/c/157561), [Jade Cole](https://www.buymeacoffee.com/WarenGonzaga/c/159563), [@crypt0r3x](https://www.buymeacoffee.com/WarenGonzaga/c/160968), [Qwitch](https://www.buymeacoffee.com/WarenGonzaga/c/161210)
292+
* [J. Archer](https://www.buymeacoffee.com/WarenGonzaga/c/151732), [Gonzalo Montes](https://www.buymeacoffee.com/WarenGonzaga/c/155002), [Keynell](https://www.buymeacoffee.com/WarenGonzaga/c/156960), [Scullum](https://www.buymeacoffee.com/WarenGonzaga), [TX_Atheist](https://www.buymeacoffee.com/WarenGonzaga/c/157561), [Jade Cole](https://www.buymeacoffee.com/WarenGonzaga/c/159563), [@crypt0r3x](https://www.buymeacoffee.com/WarenGonzaga/c/160968), [Qwitch](https://www.buymeacoffee.com/WarenGonzaga/c/161210), [Ian Vizarra](https://www.buymeacoffee.com/WarenGonzaga/c/161990)
281293

282294
Wanna see your name here? [Just buy me a coffee](https://www.buymeacoffee.com/warengonzaga)!
283295

app.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ app.get('/', async (req, res, next) => {
2323
return next();
2424
});
2525

26+
// global historical chart
27+
app.get(['/history/all/:chartType(cases|deaths)?', '/history/'], async (req, res, next) => {
28+
const userAgent = req.headers['user-agent'],
29+
api = await axios.get(`${apiBaseURL}/all`),
30+
chartType = req.params.chartType || 'cases',
31+
history = await axios.get(`${apiBaseURL}/v2/historical/all?lastdays=all`),
32+
h = history.data;
33+
data = api.data;
34+
35+
if (util.isCommandline(userAgent)) {
36+
await res.send(covid19.historyGlobalTracker(
37+
data.cases, data.deaths,
38+
data.recovered, data.updated,
39+
h, chartType
40+
));
41+
return null;
42+
}
43+
return next();
44+
});
45+
2646
// for cmd and powershell
2747
app.get(['/plain','/cmd','/basic'], async (req, res, next) => {
2848
const userAgent = req.headers['user-agent'],
@@ -53,15 +73,13 @@ app.get('/:country', async (req, res, next) => {
5373
const userAgent = req.headers['user-agent'],
5474
countryData = req.params.country,
5575
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
56-
all = await axios.get(`${apiBaseURL}/all`),
57-
u = all.data,
5876
d = api.data;
5977
if (util.isCommandline(userAgent)) {
6078
await res.send(covid19.covid19countrytracker(
6179
d.country, d.cases, d.todayCases,
6280
d.deaths, d.todayDeaths, d.recovered,
6381
d.active, d.critical, d.casesPerOneMillion,
64-
u.updated
82+
d.updated
6583
));
6684
return null;
6785
}
@@ -73,41 +91,36 @@ app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res,
7391
const userAgent = req.headers['user-agent'],
7492
countryData = req.params.country,
7593
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
76-
all = await axios.get(`${apiBaseURL}/all`),
77-
u = all.data,
7894
d = api.data;
7995
if (util.isCommandline(userAgent)) {
8096
await res.send(covid19.plaincountrytracker(
8197
d.country, d.cases, d.todayCases,
8298
d.deaths, d.todayDeaths, d.recovered,
8399
d.active, d.critical, d.casesPerOneMillion,
84-
u.updated
100+
d.updated
85101
));
86102
return null;
87103
}
88104
return next();
89105
});
90106

91-
// by historical chart by country
107+
// historical chart by country
92108
app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => {
93109
const userAgent = req.headers['user-agent'],
94110
countryData = req.params.country,
95111
chartType = req.params.chartType || 'cases',
96-
97112
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
98-
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}`),
99-
all = await axios.get(`${apiBaseURL}/all`),
113+
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
100114
s = summary.data,
101115
h = history.data;
102-
u = all.data;
103116

104117
if (util.isCommandline(userAgent)) {
105118
await res.send(
106119
covid19.historyCountryTracker(
107120
s.country, s.cases, s.todayCases,
108121
s.deaths, s.todayDeaths, s.recovered,
109122
s.active, s.critical, s.casesPerOneMillion,
110-
u.updated, h, chartType
123+
s.updated, h, chartType
111124
)
112125
);
113126
return null;

lib/cli/chart.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const chart = require('asciichart');
22

3-
// generate chart for caes or deaths
3+
// generate chart for cases or deaths
44
exports.generate = (data, type = 'cases') => {
5-
const config = {
6-
height: 7,
7-
format: (x, i) => (' ' + x.toFixed(0)).slice(-' '.length)
8-
};
9-
chartData = Object.values(data.timeline[type]).flat();
5+
const history = data[type] ? Object.values(data[type]) : Object.values(data.timeline[type]),
6+
maxLength = history.reduce((a,c) => Math.max(a, c.toFixed().length), 0),
7+
chartData = Object.values(history).flat(),
8+
config = {
9+
height: 7,
10+
format: (x, i) => x.toFixed().padStart(maxLength)
11+
};
1012
return chart.plot(chartData, config);
11-
}
13+
}

lib/cli/index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,34 @@ exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
268268
return n=='Philippines' ? visual+specialfooter : visual+defaultfooter;
269269
};
270270

271+
exports.historyGlobalTracker = (c, d, r, u, h, chartType) => {
272+
const cases = c, deaths = d, recovered = r, asof = new Date(u),
273+
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100,
274+
table = new table3({
275+
head: [{colSpan:5,content:white('COVID-19 Tracker CLI v'+pkg.version+' - Global Historical Chart')}],
276+
chars: borders
277+
}),
278+
dates = Object.keys(h[chartType]),
279+
from = dates[0],
280+
to = dates[dates.length - 1],
281+
chartData = chart.generate(h, chartType);
282+
table.push(
283+
[{colSpan:5,content:yellow('As of '+asof.toLocaleString()+' [Date:'+currentdate+']')}],
284+
[magenta('Cases'), red('Deaths'), green('Recovered'), red('Mortality %'), green('Recovered %')],
285+
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), mortalityPercentage.toFixed(2), recoveredPercentage.toFixed(2)],
286+
[{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}],
287+
[{colSpan: 5, content: chartData}],
288+
[helpInfo],[sourceInfo],[repoInfo]
289+
);
290+
const defaultfooter = footerOne+ansiBMC+footerTwo+ansiTwitter+br+br;
291+
return table.toString()+br+br+space+green(randomSay())+defaultfooter;
292+
}
293+
271294
exports.historyCountryTracker = (n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType) => {
272295
const name = n, cases = c, todayCases = tC,
273296
deaths = d, todayDeaths = tD, recovered = r,
274297
active = a, critical = cl, casesPerOneMillion = cPOM,
275-
mortalityPercentage = (d/c)*100, recoveryPercentage = (r/c)*100,
298+
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100,
276299
asof = new Date(u),
277300
dates = Object.keys(h.timeline[chartType]),
278301
from = dates[0],
@@ -282,16 +305,15 @@ exports.historyCountryTracker = (n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartTyp
282305
chars: borders,
283306
}),
284307
chartData = chart.generate(h, chartType);
285-
286308
table.push(
287309
[{colSpan: 5, content: yellow(`As of ${asof.toLocaleString()} Date: [${currentdate}]`)}],
288310
[magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active'), cyanBright('Cases/Million')],
289311
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active), formatNumber(casesPerOneMillion)],
290312
[magentaBright('Today Cases'), redBright('Today Deaths'), redBright('Critical'), red('Mortality %'), greenBright('Recovery %')],
291-
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveryPercentage.toFixed(2)],
313+
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveredPercentage.toFixed(2)],
292314
[{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}],
293315
[{colSpan: 5, content: chartData}],
294-
[sourceInfo],[repoInfo]
316+
[helpInfo],[sourceInfo],[repoInfo]
295317
);
296318

297319
const tableFooter = table.toString()+br+br+space+green(randomSay()),

0 commit comments

Comments
 (0)