-
Notifications
You must be signed in to change notification settings - Fork 1
/
getCovid19Message.js
83 lines (70 loc) · 3.36 KB
/
getCovid19Message.js
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
import { setFailed } from '@actions/core';
import { StringStream } from 'scramjet';
import { get } from 'request';
const today = new Date();
export default function getCovid19Message(country, csv, url) {
try {
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
const formattedYesterdayDate = getFormattedDate(yesterday);
const dayBeforeYesterday = new Date(today);
dayBeforeYesterday.setDate(dayBeforeYesterday.getDate() - 2);
const formattedDayBeforeYesterdayDate = getFormattedDate(dayBeforeYesterday);
return generateMessage(country, csv, url, formattedYesterdayDate, formattedDayBeforeYesterdayDate, yesterday);
} catch (error) {
setFailed(error.message);
}
}
async function generateMessage(country, csv, url, formattedYesterdayDate, formattedDayBeforeYesterdayDate, todayMinusOne) {
const resultYesterday = getResult(country, csv, url, formattedYesterdayDate);
const resultDayBeforeYesterday = getResult(country, csv, url, formattedDayBeforeYesterdayDate);
let message = await Promise.all([resultYesterday, resultDayBeforeYesterday]).then((values) => {
const totalYesterdayConfirmed = countTotal(values[0], 'Confirmed');
const totalYesterdayDeaths = countTotal(values[0], 'Deaths');
const totalYesterdayRecovered = countTotal(values[0], 'Recovered');
const totalYesterdayActive = countTotal(values[0], 'Active');
const totalDayBeforeYesterdayConfirmed = countTotal(values[1], 'Confirmed');
const totalDayBeforeYesterdayDeaths = countTotal(values[1], 'Deaths');
const totalDayBeforeYesterdayRecovered = countTotal(values[1], 'Recovered');
const msg = `${country} COVID-19 Update as ${todayMinusOne.toDateString()};
New Case: ${Math.abs(parseInt(totalYesterdayConfirmed) - parseInt(totalDayBeforeYesterdayConfirmed))}.
Deaths: ${Math.abs(parseInt(totalYesterdayDeaths) - parseInt(totalDayBeforeYesterdayDeaths))}.
Recovered: ${Math.abs(parseInt(totalYesterdayRecovered) - parseInt(totalDayBeforeYesterdayRecovered))}.
Total Confirmed: ${totalYesterdayConfirmed}.
Total Deaths: ${totalYesterdayDeaths}.
Total Recovered: ${totalYesterdayRecovered}.
Total Active: ${totalYesterdayActive}.`;
return msg;
});
return message;
}
async function getResult(country, csv, url, formattedDate) {
let arr = [];
await get(url + formattedDate + csv)
.pipe(new StringStream())
.CSVParse({ delimiter: ',', skipEmptyLines: true, header: true })
.filter(data => (data.Country_Region === country))
.consume(data => arr.push(data));
return arr;
}
function getFormattedDate(dateVal) {
const monthVal = dateVal.getMonth() + 1;
let formattedDateVal = monthVal + '-' + dateVal.getDate() + '-' + dateVal.getFullYear();
if (monthVal < 10) {
formattedDateVal = '0' + formattedDateVal;
}
return formattedDateVal;
}
function countTotal(arrayVal, propertyVal) {
let sum = 0;
arrayVal.forEach(element => {
for (let property in element) {
if (property === propertyVal) {
if (element[property] !== '' && element[property] !== null && element[property] !== undefined) {
sum += parseInt(element[property]);
}
}
}
});
return sum;
}