Skip to content

Commit 818e046

Browse files
author
scinorandex
committed
Add function dedicated to handling historical data
1 parent f17145f commit 818e046

File tree

3 files changed

+63
-59
lines changed

3 files changed

+63
-59
lines changed

src/utils/getInformation.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,41 @@ export const getCountryInfo: (
131131
throw new Error(`Cannot find the provided country`);
132132
}
133133
};
134+
135+
/**
136+
* Get historical info about a country / the world
137+
* @param mode - mode that the user requested
138+
* @param country - countryname that the user requested, leave blank to get world data
139+
* @returns an object containing date and chartData properties
140+
*/
141+
export const getHistorical: (
142+
mode: "cases" | "deaths" | "recovered",
143+
country?: string
144+
) => Promise<{
145+
date: string;
146+
chart: number[];
147+
}> = async (mode, country = "all") => {
148+
const { data: historicalData } = await axios.get(`/historical/${country}`);
149+
150+
const data: {
151+
[key: string]: number;
152+
} =
153+
country === "all"
154+
? historicalData[mode]
155+
: historicalData["timeline"][mode];
156+
157+
// Get first and last date
158+
const dates = Object.keys(data);
159+
160+
// Label for chart
161+
const date = `${
162+
mode.charAt(0).toUpperCase() + mode.slice(1)
163+
} from ${dates.shift()} to ${dates.pop()}`;
164+
165+
const chartData = Object.values(data);
166+
167+
return {
168+
date,
169+
chart: chartData,
170+
};
171+
};

src/utils/handlers.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import axios from "axios";
21
import { generateOutput } from "./generateOutput";
32
import { generateAsciichart } from "./generateAsciichart";
4-
import { getAllInfo, getCountryInfo } from "./getInformation";
5-
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
3+
import { getAllInfo, getCountryInfo, getHistorical } from "./getInformation";
64

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

25-
let { data: historicalData } = await axios.get(
26-
`/historical/${apiCountryname}`
27-
);
28-
29-
// get data from API request based on the mode
30-
let data = historicalData["timeline"][mode];
31-
32-
// Get first and last date of timeline
33-
const firstDate = Object.keys(data).shift();
34-
const lastDate = Object.keys(data).pop();
35-
36-
// Generate historical graph
37-
const chart = generateAsciichart(Object.values(data)).split("\n");
23+
// Fetch chart data and generate historical graph;
24+
let historicalData = await getHistorical(mode, apiCountryname);
25+
const chart = generateAsciichart(historicalData.chart).split("\n");
3826

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

4431
// Generate table
@@ -69,20 +56,11 @@ export const globalHistory: (
6956
(string[] | string)[]
7057
];
7158

72-
// Get data from API
73-
const { data: historicalData } = await axios.get("/historical/all");
74-
const data: {
75-
[key: string]: number;
76-
} = historicalData[mode];
77-
78-
const firstDate = Object.keys(data).shift();
79-
const lastDate = Object.keys(data).pop();
59+
// Fetch chart data and generate historical graph;
60+
const historicalData = await getHistorical(mode);
61+
const chart = generateAsciichart(historicalData.chart).split("\n");
8062

81-
// Generate historical graph;
82-
const chart = generateAsciichart(Object.values(data)).split("\n");
83-
84-
// prettier-ignore
85-
rows.push(`${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${firstDate} to ${lastDate}`.magenta)
63+
rows.push(historicalData.date.magenta);
8664
rows = rows.concat(chart);
8765

8866
let response = generateOutput(

src/utils/plainHandlers.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import axios from "axios";
21
import { generateAsciichart } from "./generateAsciichart";
32
import { generatePlainOutput } from "./generatePlainOutput";
4-
import { getAllInfo, getCountryInfo, PlainData } from "./getInformation";
3+
import {
4+
getAllInfo,
5+
getCountryInfo,
6+
getHistorical,
7+
PlainData,
8+
} from "./getInformation";
59

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

2024
// Get data from API
21-
const { data: historicalData } = await axios.get("/historical/all");
22-
const data: {
23-
[key: string]: number;
24-
} = historicalData[mode];
25-
26-
// Get first and last date of data
27-
const dates = Object.keys(data);
28-
const date = `${
29-
mode.charAt(0).toUpperCase() + mode.slice(1)
30-
} from ${dates.shift()} to ${dates.pop()}`;
25+
const historicalData = await getHistorical(mode);
3126

3227
// Generate historical graph
33-
const chart = generateAsciichart(Object.values(data), true, 7);
28+
const chart = generateAsciichart(historicalData.chart, true, 7);
3429

3530
return generatePlainOutput(info, `Global Historical Chart`, quiet, [
36-
date,
31+
historicalData.date,
3732
chart,
3833
]);
3934
};
@@ -49,32 +44,25 @@ export const globalHistoryPlain: (
4944

5045
export const historyPerCountryPlain: (
5146
country: string,
52-
mode: string,
47+
mode: "cases" | "deaths" | "recovered",
5348
quiet: boolean
5449
) => Promise<string> = async (country, mode, quiet) => {
5550
// Get summary info about a country
5651
const info = (await getCountryInfo(country, true)) as PlainData;
5752

58-
let { data: historicalData } = await axios.get(
59-
`/historical/${info.metainfo.countryName}`
53+
const historicalData = await getHistorical(
54+
mode,
55+
info.metainfo.countryName as string
6056
);
6157

62-
// Get data from API request based on the mode;
63-
let data = historicalData["timeline"][mode];
64-
65-
// Get first and last date of data
66-
const dates = Object.keys(data);
67-
// prettier-ignore
68-
const date = `${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${dates.shift()} to ${dates.pop()}`
69-
7058
// Generate historical graph
71-
const chart = generateAsciichart(Object.values(data), true, 7);
59+
const chart = generateAsciichart(historicalData.chart, true, 7);
7260

7361
return generatePlainOutput(
7462
info,
7563
`${info.metainfo.countryName} Chart`,
7664
quiet,
77-
[date, chart]
65+
[historicalData.date, chart]
7866
);
7967
};
8068

0 commit comments

Comments
 (0)