Skip to content
Prev Previous commit
Next Next commit
Add function dedicated to handling historical data
  • Loading branch information
scinorandex committed Mar 31, 2021
commit 818e046b03a0516f384c1af71077b622d54a0fc0
38 changes: 38 additions & 0 deletions src/utils/getInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,41 @@ export const getCountryInfo: (
throw new Error(`Cannot find the provided country`);
}
};

/**
* Get historical info about a country / the world
* @param mode - mode that the user requested
* @param country - countryname that the user requested, leave blank to get world data
* @returns an object containing date and chartData properties
*/
export const getHistorical: (
mode: "cases" | "deaths" | "recovered",
country?: string
) => Promise<{
date: string;
chart: number[];
}> = async (mode, country = "all") => {
const { data: historicalData } = await axios.get(`/historical/${country}`);

const data: {
[key: string]: number;
} =
country === "all"
? historicalData[mode]
: historicalData["timeline"][mode];

// Get first and last date
const dates = Object.keys(data);

// Label for chart
const date = `${
mode.charAt(0).toUpperCase() + mode.slice(1)
} from ${dates.shift()} to ${dates.pop()}`;

const chartData = Object.values(data);

return {
date,
chart: chartData,
};
};
40 changes: 9 additions & 31 deletions src/utils/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import axios from "axios";
import { generateOutput } from "./generateOutput";
import { generateAsciichart } from "./generateAsciichart";
import { getAllInfo, getCountryInfo } from "./getInformation";
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
import { getAllInfo, getCountryInfo, getHistorical } from "./getInformation";

/**
* historyPerCountry shows a tablechart of the <mode> of a country
Expand All @@ -22,23 +20,12 @@ export const historyPerCountry: (
country
)) as [number, string, string, (string[] | string)[]];

let { data: historicalData } = await axios.get(
`/historical/${apiCountryname}`
);

// get data from API request based on the mode
let data = historicalData["timeline"][mode];

// Get first and last date of timeline
const firstDate = Object.keys(data).shift();
const lastDate = Object.keys(data).pop();

// Generate historical graph
const chart = generateAsciichart(Object.values(data)).split("\n");
// Fetch chart data and generate historical graph;
let historicalData = await getHistorical(mode, apiCountryname);
const chart = generateAsciichart(historicalData.chart).split("\n");

// add chart label and chart
// prettier-ignore
rows.push(`${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${firstDate} to ${lastDate}`.magenta);
rows.push(historicalData.date.magenta);
rows = rows.concat(chart);

// Generate table
Expand Down Expand Up @@ -69,20 +56,11 @@ export const globalHistory: (
(string[] | string)[]
];

// Get data from API
const { data: historicalData } = await axios.get("/historical/all");
const data: {
[key: string]: number;
} = historicalData[mode];

const firstDate = Object.keys(data).shift();
const lastDate = Object.keys(data).pop();
// Fetch chart data and generate historical graph;
const historicalData = await getHistorical(mode);
const chart = generateAsciichart(historicalData.chart).split("\n");

// Generate historical graph;
const chart = generateAsciichart(Object.values(data)).split("\n");

// prettier-ignore
rows.push(`${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${firstDate} to ${lastDate}`.magenta)
rows.push(historicalData.date.magenta);
rows = rows.concat(chart);

let response = generateOutput(
Expand Down
44 changes: 16 additions & 28 deletions src/utils/plainHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import axios from "axios";
import { generateAsciichart } from "./generateAsciichart";
import { generatePlainOutput } from "./generatePlainOutput";
import { getAllInfo, getCountryInfo, PlainData } from "./getInformation";
import {
getAllInfo,
getCountryInfo,
getHistorical,
PlainData,
} from "./getInformation";

/**
* globalHistory shows a tablechart of the cases of all the countries
Expand All @@ -11,29 +15,20 @@ import { getAllInfo, getCountryInfo, PlainData } from "./getInformation";
* @param quiet tells the response to be in quiet mode or not
*/
export const globalHistoryPlain: (
mode: string,
mode: "cases" | "deaths" | "recovered",
quiet: boolean
) => Promise<string> = async (mode, quiet) => {
// Get summary info
const info = (await getAllInfo(true)) as PlainData;

// Get data from API
const { data: historicalData } = await axios.get("/historical/all");
const data: {
[key: string]: number;
} = historicalData[mode];

// Get first and last date of data
const dates = Object.keys(data);
const date = `${
mode.charAt(0).toUpperCase() + mode.slice(1)
} from ${dates.shift()} to ${dates.pop()}`;
const historicalData = await getHistorical(mode);

// Generate historical graph
const chart = generateAsciichart(Object.values(data), true, 7);
const chart = generateAsciichart(historicalData.chart, true, 7);

return generatePlainOutput(info, `Global Historical Chart`, quiet, [
date,
historicalData.date,
chart,
]);
};
Expand All @@ -49,32 +44,25 @@ export const globalHistoryPlain: (

export const historyPerCountryPlain: (
country: string,
mode: string,
mode: "cases" | "deaths" | "recovered",
quiet: boolean
) => Promise<string> = async (country, mode, quiet) => {
// Get summary info about a country
const info = (await getCountryInfo(country, true)) as PlainData;

let { data: historicalData } = await axios.get(
`/historical/${info.metainfo.countryName}`
const historicalData = await getHistorical(
mode,
info.metainfo.countryName as string
);

// Get data from API request based on the mode;
let data = historicalData["timeline"][mode];

// Get first and last date of data
const dates = Object.keys(data);
// prettier-ignore
const date = `${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${dates.shift()} to ${dates.pop()}`

// Generate historical graph
const chart = generateAsciichart(Object.values(data), true, 7);
const chart = generateAsciichart(historicalData.chart, true, 7);

return generatePlainOutput(
info,
`${info.metainfo.countryName} Chart`,
quiet,
[date, chart]
[historicalData.date, chart]
);
};

Expand Down