forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetInformation.ts
More file actions
171 lines (150 loc) · 5.85 KB
/
getInformation.ts
File metadata and controls
171 lines (150 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import axios from "axios";
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
let countryCodes: { [key: string]: string } = {};
(async () => {
countryCodes = (await axios.get("http://country.io/names.json")).data;
})();
export interface PlainData {
data: {
[key: string]: string;
};
metainfo: {
[key: string]: number | string;
};
}
/**
* @param isPlain Set to true to recieve an object containing the responses instead of the rows
* @returns an object containing the data and metainfo **if isPlain is set to true**
* @returns an array in the format of [timestamp, rows] **if isPlain is set to false**
*/
export const getAllInfo: (
isPlain?: boolean
) => Promise<[number, (string[] | string)[]] | PlainData> = async (
isPlain = false
) => {
let { data: globalData } = await axios.get("/all");
let { cases, deaths, recovered, updated } = globalData;
let mortalityPercentage = ((deaths / cases) * 100).toFixed(2) + "%";
let recoveredPercentage = ((recovered / cases) * 100).toFixed(2) + "%";
[cases, deaths, recovered] = [cases, deaths, recovered].map((num: number) =>
num.toLocaleString("en-US", { maximumFractionDigits: 0 })
);
// Return object containing information if isPlain is set to true
if (isPlain) {
return {
data: {
Cases: cases,
Deaths: deaths,
"Mortality %": mortalityPercentage,
Recovered: recovered,
"Recovered %": recoveredPercentage,
},
metainfo: {
updated,
},
};
}
// Return rows if isPlain is set to false
// prettier-ignore
return [updated, [
["Cases".magenta, "Deaths".red,"Recovered".green, "Mortality %".red,"Recovered %".green],
[cases, deaths, recovered, mortalityPercentage, recoveredPercentage]]]
};
/**
* @param country the country code or string that the user provides from req.params or CLI
* @param isPlain Set to true to recieve an object containing the responses instead of the rows
* @returns an object containing the data and metainfo **if isPlain is set to true**
* @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false**
*/
export const getCountryInfo: (
country: string,
isPlain?: boolean
) => Promise<
[number, string, string, (string[] | string)[]] | PlainData
> = async (country, isPlain) => {
// Wait 1 second for countryCodes to initialize, needed for CLI
if (Object.keys(countryCodes).length === 0) {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
}
country =
country.length < 3 ? countryCodes[country.toUpperCase()] : country; // Convert country code to country name
if (country === undefined || typeof country === "undefined")
throw new Error(`Cannot find provided country`);
try {
let { data: countryData } = await axios.get(`/countries/${country}`);
// prettier-ignore
let { country: countryName, updated, cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical} = countryData;
let mortalityPercentage = ((deaths / cases) * 100).toFixed(2) + "%";
let recoveredPercentage = ((recovered / cases) * 100).toFixed(2) + "%";
// prettier-ignore
[ cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical ] =
[ cases, deaths, recovered, active, casesPerOneMillion, todayCases, todayDeaths, critical,
].map((num: number) =>
num.toLocaleString("en-US", { maximumFractionDigits: 0 })
);
// Return object containing information if isPlain is set to true
if (isPlain) {
return {
data: {
Cases: cases,
"Today Cases": todayCases,
Active: active,
Recovered: recovered,
Deaths: deaths,
"Today Deaths": todayDeaths,
Critical: critical,
"Mortality %": mortalityPercentage,
"Recovery %": recoveredPercentage,
"Cases/Million": casesPerOneMillion,
},
metainfo: {
updated,
countryName,
},
};
}
//prettier-ignore
return [updated, country, countryName, [
[ "Cases".magenta, "Deaths".red, "Recovered".green, "Active".blue, "Cases/Million".blue,],
[ cases, deaths, recovered, active, casesPerOneMillion,],
[ "Today Cases".magenta, "Today Deaths".red, "Critical".red, "Mortaility %".red, "Recovery %".green],
[ todayCases, todayDeaths, critical, mortalityPercentage, recoveredPercentage]]
]
} catch {
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,
};
};