Skip to content

Commit d977b98

Browse files
authored
Merge pull request OSSPhilippines#32 from ianvizarra/feature/historical-global
- Global Historical Chart - Updated Readme - Updated History Chart API
2 parents 1952cf6 + c6a4189 commit d977b98

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
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: 22 additions & 2 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'],
@@ -84,13 +104,13 @@ app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res,
84104
return next();
85105
});
86106

87-
// by historical chart by country
107+
// historical chart by country
88108
app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => {
89109
const userAgent = req.headers['user-agent'],
90110
countryData = req.params.country,
91111
chartType = req.params.chartType || 'cases',
92112
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
93-
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}`),
113+
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`),
94114
s = summary.data,
95115
h = history.data;
96116

lib/cli/chart.js

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

33
// generate chart for cases or deaths
44
exports.generate = (data, type = 'cases') => {
5-
const maxLength = Object.values(data.timeline[type])
6-
.reduce((a,c) => Math.max(a, c.toFixed().length), 0)
7-
const config = {
8-
height: 7,
9-
format: (x, i) => x.toFixed().padStart(maxLength)
10-
};
11-
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+
};
1212
return chart.plot(chartData, config);
1313
}

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)