From 48f304f3cf87e402601b6e4f394108eb833f415e Mon Sep 17 00:00:00 2001 From: ianvizarra Date: Sun, 29 Mar 2020 19:52:29 +0800 Subject: [PATCH 1/2] Historical Graph of Cases and Deaths --- app.js | 27 +++++++++++++++++++++++++++ lib/cli/chart.js | 11 +++++++++++ lib/cli/index.js | 39 ++++++++++++++++++++++++++++++++++++++- package-lock.json | 7 ++++++- package.json | 1 + 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 lib/cli/chart.js diff --git a/app.js b/app.js index de36202..d489378 100644 --- a/app.js +++ b/app.js @@ -95,6 +95,33 @@ app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, return next(); }); +// by historical chart by country +app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => { + const userAgent = req.headers['user-agent'], + countryData = req.params.country, + chartType = req.params.chartType || 'cases', + + summary = await axios.get(`${apiBaseURL}/countries/${countryData}`), + history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}`), + all = await axios.get(`${apiBaseURL}/all`), + s = summary.data, + h = history.data; + u = all.data; + + if (util.isCommandline(userAgent)) { + await res.send( + covid19.historyCountryTracker( + s.country, s.cases, s.todayCases, + s.deaths, s.todayDeaths, s.recovered, + s.active, s.critical, s.casesPerOneMillion, + u.updated, h, chartType + ) + ); + return null; + } + return next(); +}); + app.get('*', (req, res) => res.send(` Welcome to COVID-19 Tracker CLI by Waren Gonzaga Please visit: https://warengonza.ga/covid19-tracker-cli diff --git a/lib/cli/chart.js b/lib/cli/chart.js new file mode 100644 index 0000000..fd571e1 --- /dev/null +++ b/lib/cli/chart.js @@ -0,0 +1,11 @@ +const chart = require('asciichart'); + +// generate chart for caes or deaths +exports.generate = (data, type = 'cases') => { + const config = { + height: 12, + format: (x, i) => (' ' + x.toFixed(0)).slice(-' '.length) + }; + chartData = Object.values(data.timeline[type]).flat(); + return chart.plot(chartData, config); +} \ No newline at end of file diff --git a/lib/cli/index.js b/lib/cli/index.js index bf96f56..0818798 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -2,7 +2,8 @@ const color = require('ansi-styles'), fs = require('fs'), - table3 = require('cli-table3'); + table3 = require('cli-table3'), + chart = require('./chart'); // ansi colors const @@ -250,5 +251,41 @@ exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => { return n=='Philippines' ? visual+specialfooter : visual+defaultfooter; }; +exports.historyCountryTracker = (n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartType) => { + const name = n, cases = c, todayCases = tC, + deaths = d, todayDeaths = tD, recovered = r, + active = a, critical = cl, casesPerOneMillion = cPOM, + mortalityPercentage = (d/c)*100, recoveryPercentage = (r/c)*100, + asof = new Date(u), + dates = Object.keys(h.timeline[chartType]), + from = dates[0], + to = dates[dates.length - 1], + table = new table3({ + head: [{colSpan: 5, content: white(`COVID-19 Tracker CLI v ${pkg.version} - ${name} Historical Chart`)}], + chars: borders, + }), + chartData = chart.generate(h, chartType); + + table.push( + [{colSpan: 5, content: yellow(`As of ${asof.toLocaleString()} Date: [${currentdate}]`)}], + [magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active'), cyanBright('Cases/Million')], + [formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active), formatNumber(casesPerOneMillion)], + [magentaBright('Today Cases'), redBright('Today Deaths'), redBright('Critical'), red('Mortality %'), greenBright('Recovery %')], + [formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveryPercentage.toFixed(2)], + [{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}], + [{colSpan: 5, content: chartData}], + [sourceInfo],[repoInfo] + ); + + const tableFooter = table.toString()+br+br+space+'"'+green(randomSay())+'"', + defaultfooter = footerOne+ansiBMC+footerTwo+ansiTwitter+br+br, + specialfooter = footerOne+ansiGCash+br+' '+ansiBMC+footerTwo+ansiTwitter+br+br; + + return n == 'Philippines' ? tableFooter+specialfooter : tableFooter+defaultfooter; +} + const footerOne = br+br+' '+line+br+' '+bmcline+br+' ', footerTwo = br+' '+line+br+' '+twitterline+br+' '; + +// capitalize first letter +const ucfirst = (string) => string.charAt(0).toUpperCase() + string.slice(1); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 21e442d..69114b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "covid19-tracker-cli", - "version": "2.1.0", + "version": "2.8.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -62,6 +62,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "asciichart": { + "version": "1.5.15", + "resolved": "https://registry.npmjs.org/asciichart/-/asciichart-1.5.15.tgz", + "integrity": "sha512-0XHe9H/NB1A717Ji8IhNK7NIvdqxQfEVfTVqFqkQAK06N3qqnIldsU1o4O8KuYhHoCADOiC/Xeg6ZmbJyFtrTA==" + }, "axios": { "version": "0.19.2", "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", diff --git a/package.json b/package.json index 9cf9e8c..a122f50 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "ansi-styles": "^4.2.1", + "asciichart": "^1.5.15", "axios": "^0.19.2", "cli-table3": "^0.5.1", "express": "^4.17.1" From 562d7f9c4289e0d9763b2bea92ab41f49184a473 Mon Sep 17 00:00:00 2001 From: ianvizarra Date: Sun, 29 Mar 2020 20:05:09 +0800 Subject: [PATCH 2/2] Added Collaborator --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ba3f25..c0d1d3d 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ COVID-19 Tracker CLI is licensed under MIT - * **Twitter:**