Skip to content

Commit 48f304f

Browse files
committed
Historical Graph of Cases and Deaths
1 parent d63c050 commit 48f304f

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res,
9595
return next();
9696
});
9797

98+
// by historical chart by country
99+
app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => {
100+
const userAgent = req.headers['user-agent'],
101+
countryData = req.params.country,
102+
chartType = req.params.chartType || 'cases',
103+
104+
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
105+
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}`),
106+
all = await axios.get(`${apiBaseURL}/all`),
107+
s = summary.data,
108+
h = history.data;
109+
u = all.data;
110+
111+
if (util.isCommandline(userAgent)) {
112+
await res.send(
113+
covid19.historyCountryTracker(
114+
s.country, s.cases, s.todayCases,
115+
s.deaths, s.todayDeaths, s.recovered,
116+
s.active, s.critical, s.casesPerOneMillion,
117+
u.updated, h, chartType
118+
)
119+
);
120+
return null;
121+
}
122+
return next();
123+
});
124+
98125
app.get('*', (req, res) => res.send(`
99126
Welcome to COVID-19 Tracker CLI by Waren Gonzaga
100127
Please visit: https://warengonza.ga/covid19-tracker-cli

lib/cli/chart.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const chart = require('asciichart');
2+
3+
// generate chart for caes or deaths
4+
exports.generate = (data, type = 'cases') => {
5+
const config = {
6+
height: 12,
7+
format: (x, i) => (' ' + x.toFixed(0)).slice(-' '.length)
8+
};
9+
chartData = Object.values(data.timeline[type]).flat();
10+
return chart.plot(chartData, config);
11+
}

lib/cli/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const color = require('ansi-styles'),
44
fs = require('fs'),
5-
table3 = require('cli-table3');
5+
table3 = require('cli-table3'),
6+
chart = require('./chart');
67

78
// ansi colors
89
const
@@ -250,5 +251,41 @@ exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
250251
return n=='Philippines' ? visual+specialfooter : visual+defaultfooter;
251252
};
252253

254+
exports.historyCountryTracker = (n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType) => {
255+
const name = n, cases = c, todayCases = tC,
256+
deaths = d, todayDeaths = tD, recovered = r,
257+
active = a, critical = cl, casesPerOneMillion = cPOM,
258+
mortalityPercentage = (d/c)*100, recoveryPercentage = (r/c)*100,
259+
asof = new Date(u),
260+
dates = Object.keys(h.timeline[chartType]),
261+
from = dates[0],
262+
to = dates[dates.length - 1],
263+
table = new table3({
264+
head: [{colSpan: 5, content: white(`COVID-19 Tracker CLI v ${pkg.version} - ${name} Historical Chart`)}],
265+
chars: borders,
266+
}),
267+
chartData = chart.generate(h, chartType);
268+
269+
table.push(
270+
[{colSpan: 5, content: yellow(`As of ${asof.toLocaleString()} Date: [${currentdate}]`)}],
271+
[magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active'), cyanBright('Cases/Million')],
272+
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active), formatNumber(casesPerOneMillion)],
273+
[magentaBright('Today Cases'), redBright('Today Deaths'), redBright('Critical'), red('Mortality %'), greenBright('Recovery %')],
274+
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveryPercentage.toFixed(2)],
275+
[{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}],
276+
[{colSpan: 5, content: chartData}],
277+
[sourceInfo],[repoInfo]
278+
);
279+
280+
const tableFooter = table.toString()+br+br+space+'"'+green(randomSay())+'"',
281+
defaultfooter = footerOne+ansiBMC+footerTwo+ansiTwitter+br+br,
282+
specialfooter = footerOne+ansiGCash+br+' '+ansiBMC+footerTwo+ansiTwitter+br+br;
283+
284+
return n == 'Philippines' ? tableFooter+specialfooter : tableFooter+defaultfooter;
285+
}
286+
253287
const footerOne = br+br+' '+line+br+' '+bmcline+br+' ',
254288
footerTwo = br+' '+line+br+' '+twitterline+br+' ';
289+
290+
// capitalize first letter
291+
const ucfirst = (string) => string.charAt(0).toUpperCase() + string.slice(1);

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "MIT",
1111
"dependencies": {
1212
"ansi-styles": "^4.2.1",
13+
"asciichart": "^1.5.15",
1314
"axios": "^0.19.2",
1415
"cli-table3": "^0.5.1",
1516
"express": "^4.17.1"

0 commit comments

Comments
 (0)