Skip to content
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)!

Expand Down
37 changes: 25 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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;
}
Expand All @@ -73,41 +91,36 @@ 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(
covid19.historyCountryTracker(
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;
Expand Down
16 changes: 9 additions & 7 deletions lib/cli/chart.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 26 additions & 4 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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()),
Expand Down