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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions utils/calcRecoveryRate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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) {
return ((numRecoveries / (numDeaths + numRecoveries)) * 100).toFixed(1) + '%';
}

/**
* Calculates the recovery rate from an array of data and splices the recovery rate
* into the middle of the array for table formatting.
*
* @param {Array} one data about the element we are processing. (e.g. state, country, etc.)
* @return {Array} A spliced array almost as it was before but now with a string formatted recovery rate.
*/
function spliceRecoveryRate(one) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one is a poor name, as a reader I have no idea what that means.
Perhaps area would be better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one is the naming convention already in use in this project elsewhere for a single entry.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can make one better everywhere.

const numRecoveries = one[5];
const numDeaths = one[3];
const oneRecovery = ((numRecoveries / (numRecoveries + numDeaths)) * 100);
var oneRecoveryStr = oneRecovery.toFixed(1) + '%';
if(isNaN(oneRecovery) || oneRecovery === 0){
oneRecoveryStr = '—';
}
one.splice(6, 0, oneRecoveryStr);
return one;
}

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 for a set of objects.
*
* @param {Array} data The objects to calculate the recovery rate on.
* @return {Array} The data almost as it was before, but now has the recovery rate included.
*/
calculateMultipleRecoveryRate: function(data) {
return data.map(one => {
return spliceRecoveryRate(one)
});
},

/**
* Calculates the recovery rate for a single object. (e.g. China)
*
* @param {Array} data A single object of data for a unit.
*/
calculateSingleRecoveryRate: function(data) {
return spliceRecoveryRate(data);
}
};
2 changes: 2 additions & 0 deletions utils/getAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ 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);

Expand Down
2 changes: 2 additions & 0 deletions utils/getCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const chalk = require("chalk");
const axios = require("axios");
const logSymbols = require("log-symbols");
const comma = require("comma-number");
const { calculateSingleRecoveryRate } = require("./calcRecoveryRate");
const red = chalk.red;
const green = chalk.green;

Expand All @@ -20,6 +21,7 @@ module.exports = async (spinner, table, states, country) => {
process.exit(0);
}
let data = Object.values(api.data);
data = calculateSingleRecoveryRate(data);
data = data.map(d => comma(d));
table.push(data);
spinner.stopAndPersist();
Expand Down
2 changes: 2 additions & 0 deletions utils/getStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const axios = require("axios");
const chalk = require("chalk");
const comma = require("comma-number");
const { sortStateKeys, sortStateOrders } = require("./table.js");
const { calculateMultipleRecoveryRate } = require("./calcRecoveryRate");

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

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

Expand Down
2 changes: 2 additions & 0 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const axios = require("axios");
const comma = require("comma-number");
const { calculateWorldwideRecoveryRate } = require("./calcRecoveryRate");

module.exports = async (table, states) => {
if (!states) {
Expand All @@ -13,6 +14,7 @@ module.exports = async (table, states) => {
data[1],
`—`,
data[2],
calculateWorldwideRecoveryRate(data[1], data[2]),
`—`,
`—`,
`—`
Expand Down
10 changes: 8 additions & 2 deletions utils/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
`Deaths`,
`Deaths ${dim(`(today)`)}`,
`Recovered`,
`Recovery Rate`,
`Active`,
`Critical`,
`Per Million`
Expand All @@ -22,6 +23,7 @@ module.exports = {
`${red(`Deaths`)}`,
`${red(`Deaths (today)`)}`,
`${green(`Recovered`)}`,
`${green(`Recovery Rate`)}`,
`Active`,
`${red(`Critical`)}`,
`Per Million`
Expand All @@ -33,6 +35,7 @@ module.exports = {
`deaths`,
`deaths-today`,
`recovered`,
`recovery-rate`,
`active`,
`critical`,
`per-million`
Expand All @@ -44,6 +47,7 @@ module.exports = {
`Deaths`,
`Deaths ${dim(`(today)`)}`,
`Recovered`,
`Recovery Rate`,
`Active`
],
coloredStates: [
Expand All @@ -53,6 +57,7 @@ module.exports = {
`${red(`Deaths`)}`,
`${red(`Deaths (today)`)}`,
`${green(`Recovered`)}`,
`${green(`Recovery Rate`)}`,
`Active`
],
sortStateKeys: [
Expand All @@ -62,9 +67,10 @@ module.exports = {
`deaths`,
`deaths-today`,
`recovered`,
`recovery-rate`,
`active`
],
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1],
sortStateOrders: [1, -1, -1, -1, -1, -1, -1],
sortOrders: [1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
sortStateOrders: [1, -1, -1, -1, -1, -1, -1, -1],
style: { head: ["cyan"] }
};