diff --git a/README.md b/README.md index 0c696b8..3e864b2 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,19 @@ curl -L covid19.trackercli.com/philippines curl -L covid19.trackercli.com/ph ``` -#### Country with History Chart +#### Global Tracking with History Chart + +```bash +# shows global result with history chart +curl -L covid19.trackercli.com/history +``` + +```bash +# shows by default a global history chart +curl -L covid19.trackercli.com/history/all +``` + +#### Country Tracking with History Chart ```bash # shows result with history chart @@ -277,7 +289,7 @@ Some cups of my coffees goes to the foundation via [#OneAgainstCovid19 by PayMay ## Supporters and Backers -* [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) +* [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) Wanna see your name here? [Just buy me a coffee](https://www.buymeacoffee.com/warengonzaga)! diff --git a/app.js b/app.js index 2eb6ebb..a43d548 100644 --- a/app.js +++ b/app.js @@ -23,6 +23,26 @@ app.get('/', async (req, res, next) => { return next(); }); +// global historical chart +app.get(['/history/all/:chartType(cases|deaths)?', '/history/'], async (req, res, next) => { + const userAgent = req.headers['user-agent'], + api = await axios.get(`${apiBaseURL}/all`), + chartType = req.params.chartType || 'cases', + history = await axios.get(`${apiBaseURL}/v2/historical/all?lastdays=all`), + h = history.data; + data = api.data; + + if (util.isCommandline(userAgent)) { + await res.send(covid19.historyGlobalTracker( + data.cases, data.deaths, + data.recovered, data.updated, + h, chartType + )); + return null; + } + return next(); +}); + // for cmd and powershell app.get(['/plain','/cmd','/basic'], async (req, res, next) => { const userAgent = req.headers['user-agent'], @@ -53,15 +73,13 @@ app.get('/:country', async (req, res, next) => { const userAgent = req.headers['user-agent'], countryData = req.params.country, api = await axios.get(`${apiBaseURL}/countries/${countryData}`), - all = await axios.get(`${apiBaseURL}/all`), - u = all.data, d = api.data; if (util.isCommandline(userAgent)) { await res.send(covid19.covid19countrytracker( d.country, d.cases, d.todayCases, d.deaths, d.todayDeaths, d.recovered, d.active, d.critical, d.casesPerOneMillion, - u.updated + d.updated )); return null; } @@ -73,33 +91,28 @@ app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, const userAgent = req.headers['user-agent'], countryData = req.params.country, api = await axios.get(`${apiBaseURL}/countries/${countryData}`), - all = await axios.get(`${apiBaseURL}/all`), - u = all.data, d = api.data; if (util.isCommandline(userAgent)) { await res.send(covid19.plaincountrytracker( d.country, d.cases, d.todayCases, d.deaths, d.todayDeaths, d.recovered, d.active, d.critical, d.casesPerOneMillion, - u.updated + d.updated )); return null; } return next(); }); -// by historical chart by country +// 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`), + history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}?lastdays=all`), s = summary.data, h = history.data; - u = all.data; if (util.isCommandline(userAgent)) { await res.send( @@ -107,7 +120,7 @@ app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => s.country, s.cases, s.todayCases, s.deaths, s.todayDeaths, s.recovered, s.active, s.critical, s.casesPerOneMillion, - u.updated, h, chartType + s.updated, h, chartType ) ); return null; diff --git a/lib/cli/chart.js b/lib/cli/chart.js index 75e8f18..d6372dc 100644 --- a/lib/cli/chart.js +++ b/lib/cli/chart.js @@ -1,11 +1,13 @@ const chart = require('asciichart'); -// generate chart for caes or deaths +// generate chart for cases or deaths exports.generate = (data, type = 'cases') => { - const config = { - height: 7, - format: (x, i) => (' ' + x.toFixed(0)).slice(-' '.length) - }; - chartData = Object.values(data.timeline[type]).flat(); + const history = data[type] ? Object.values(data[type]) : Object.values(data.timeline[type]), + maxLength = history.reduce((a,c) => Math.max(a, c.toFixed().length), 0), + chartData = Object.values(history).flat(), + config = { + height: 7, + format: (x, i) => x.toFixed().padStart(maxLength) + }; return chart.plot(chartData, config); -} \ No newline at end of file +} diff --git a/lib/cli/index.js b/lib/cli/index.js index 4846b06..ccd66da 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -268,11 +268,34 @@ exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => { return n=='Philippines' ? visual+specialfooter : visual+defaultfooter; }; +exports.historyGlobalTracker = (c, d, r, u, h, chartType) => { + const cases = c, deaths = d, recovered = r, asof = new Date(u), + mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100, + table = new table3({ + head: [{colSpan:5,content:white('COVID-19 Tracker CLI v'+pkg.version+' - Global Historical Chart')}], + chars: borders + }), + dates = Object.keys(h[chartType]), + from = dates[0], + to = dates[dates.length - 1], + chartData = chart.generate(h, chartType); + table.push( + [{colSpan:5,content:yellow('As of '+asof.toLocaleString()+' [Date:'+currentdate+']')}], + [magenta('Cases'), red('Deaths'), green('Recovered'), red('Mortality %'), green('Recovered %')], + [formatNumber(cases), formatNumber(deaths), formatNumber(recovered), mortalityPercentage.toFixed(2), recoveredPercentage.toFixed(2)], + [{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}], + [{colSpan: 5, content: chartData}], + [helpInfo],[sourceInfo],[repoInfo] + ); + const defaultfooter = footerOne+ansiBMC+footerTwo+ansiTwitter+br+br; + return table.toString()+br+br+space+green(randomSay())+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, + mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100, asof = new Date(u), dates = Object.keys(h.timeline[chartType]), from = dates[0], @@ -282,16 +305,15 @@ exports.historyCountryTracker = (n, c, tC, d, tD, r, a, cl, cPOM, u, h, chartTyp 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)], + [formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveredPercentage.toFixed(2)], [{colSpan: 5, content: magenta(`${ucfirst(chartType)} from ${from} to ${to}`)}], [{colSpan: 5, content: chartData}], - [sourceInfo],[repoInfo] + [helpInfo],[sourceInfo],[repoInfo] ); const tableFooter = table.toString()+br+br+space+green(randomSay()),