Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 Add Recovery Rate metric #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions utils/calcRecoveryRate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Calculates the recovery rate and puts it in a nice looking string format.
*
* @param {int} numDeaths The number of deaths for a unit
* @param {int} numRecoveries The number of recoveries for a unit
* @return {String} A parsed string percentage string of recoveries rounded to one decimal place.
*/
function calcRecoveryRate(numDeaths, numRecoveries) {
const factor = numRecoveries / (numDeaths + numRecoveries);
const percentage = (factor * 100).toFixed(1) + '%';
if(isNaN(factor) || factor === 0) {
return '-';
}else {
return percentage;
}
}

module.exports = {
/**
*
* @param {String} deaths A comma delineated number for number of deaths.
* @param {String} recoveries A comma delineated number for number of recoveries.
* @return {String} A parsed string percentage string of recoveries rounded to one decimal place.
*/
calculateWorldwideRecoveryRate: function(deaths, recoveries) {
const numDeaths = parseInt(deaths.replace(/,/g,''));
const numRecoveries = parseInt(recoveries.replace(/,/g,''));
return calcRecoveryRate(numDeaths, numRecoveries);
},

/**
* Calculates the recovery rate and puts it in a nice looking string format.
*
* @param {int} numDeaths The number of deaths for a unit
* @param {int} numRecoveries The number of recoveries for a unit
* @return {String} A parsed string percentage string of recoveries rounded to one decimal place.
*/
calculateRecoveryRate: function(numDeaths, numRecoveries) {
return calcRecoveryRate(numDeaths, numRecoveries);
}
};
28 changes: 28 additions & 0 deletions utils/getAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const axios = require("axios");
const chalk = require("chalk");
const comma = require("comma-number");
const { sortKeys, sortOrders } = require("./table.js");
const { calculateMultipleRecoveryRate } = require("./calcRecoveryRate");

module.exports = async (spinner, table, states, country, options) => {
if (!country && !states) {
const api = await axios.get(`https://corona.lmao.ninja/countries`);
let all = api.data.map(one => Object.values(one));
all = calculateMultipleRecoveryRate(all);

const sortIndex = sortKeys.indexOf(options.sort);

if (sortIndex != -1) {
const dir = sortOrders[sortIndex];
all = all.sort((a, b) => (a[sortIndex] > b[sortIndex] ? dir : -dir));
}

all.map(one => {
one = one.map(d => comma(d));
return table.push(one);
});
spinner.stopAndPersist();
spinner.info(`${chalk.cyan(`Sorted by:`)} ${options.sort}`);
console.log(table.toString());
}
};
2 changes: 2 additions & 0 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const cyan = chalk.cyan;
const dim = chalk.dim;
const comma = require('comma-number');
const { sortingKeys } = require('./table.js');
const { calculateRecoveryRate } = require("./calcRecoveryRate");
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const orderBy = require('lodash.orderby');
Expand Down Expand Up @@ -43,6 +44,7 @@ module.exports = async (
comma(oneCountry.deaths),
comma(oneCountry.todayDeaths),
comma(oneCountry.recovered),
calculateRecoveryRate(oneCountry.deaths, oneCountry.recovered),
comma(oneCountry.active),
comma(oneCountry.critical),
comma(oneCountry.casesPerOneMillion)
Expand Down
13 changes: 10 additions & 3 deletions utils/getCountry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const chalk = require('chalk');
const axios = require('axios');
const chalk = require("chalk");
const axios = require("axios");
const logSymbols = require("log-symbols");
const comma = require("comma-number");
const { calculateRecoveryRate } = require("./calcRecoveryRate");
const sym = require('log-symbols');
const comma = require('comma-number');
const red = chalk.red;
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
Expand All @@ -23,6 +25,10 @@ module.exports = async (spinner, table, states, countryName) => {
);
process.exit(0);
}
// let data = Object.values(api.data);
// data = calculateSingleRecoveryRate(data);
// data = data.map(d => comma(d));
// table.push(data);

table.push([
`—`,
Expand All @@ -32,6 +38,7 @@ module.exports = async (spinner, table, states, countryName) => {
comma(thisCountry.deaths),
comma(thisCountry.todayDeaths),
comma(thisCountry.recovered),
calculateRecoveryRate(thisCountry.deaths, thisCountry.recovered),
comma(thisCountry.active),
comma(thisCountry.critical),
comma(thisCountry.casesPerOneMillion)
Expand Down
2 changes: 2 additions & 0 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios');
const comma = require('comma-number');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const { calculateWorldwideRecoveryRate } = require('./calcRecoveryRate');

module.exports = async (table, states) => {
const [err, all] = await to(axios.get(`https://corona.lmao.ninja/all`));
Expand All @@ -18,6 +19,7 @@ module.exports = async (table, states) => {
data[1],
`—`,
data[2],
calculateWorldwideRecoveryRate(data[1], data[2]),
`—`,
`—`,
`—`
Expand Down
2 changes: 2 additions & 0 deletions utils/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
`Deaths`,
`Deaths ${dim(`(today)`)}`,
`Recovered`,
`Recovery Rate`,
`Active`,
`Critical`,
`Per Million`
Expand All @@ -25,6 +26,7 @@ module.exports = {
`${red(`Deaths`)}`,
`${red(`Deaths (today)`)}`,
`${green(`Recovered`)}`,
`${green(`Recovery Rate`)}`,
`${yellow(`Active`)}`,
`${red(`Critical`)}`,
`Per Million`
Expand Down