This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
refresh-icmr.js
84 lines (76 loc) · 3.15 KB
/
refresh-icmr.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
84
import { errorResponse, rawResponse } from './api';
import { Store } from './store';
import { STORE_KEYS } from './constants';
/**
* Fetches data from @SOURCE_URL and caches it
*/
export async function refreshTestingData(request) {
const isDebugMode = request.url.includes("debug");
const response = await fetch(SOURCE_URL);
if (response.status === 200) {
const testingData = (await response.json())["tested"];
if (!testingData || !Array.isArray(testingData) || testingData.length === 0) {
return await errorResponse({"error": "response not an array"});
}
const rawRecords = testingData.map(row => ({
timestamp: getLastUpdated(row["updatetimestamp"]),
totalSamplesTested: parseInt(row["totalsamplestested"]),
totalIndividualsTested: parseInt(row["totalindividualstested"]),
totalPositiveCases: parseInt(row["totalpositivecases"]),
source: row["source"]
}));
const latestData = rawRecords[rawRecords.length-1];
const lastRefreshed = new Date().toISOString();
// per day history
await Promise.all(isDebugMode ? [] : [
// store raw history
Store.put(STORE_KEYS.CACHED_TESTING_HISTORY_RAW, JSON.stringify({
success: true,
data: rawRecords,
lastRefreshed,
lastOriginUpdate: latestData.timestamp
})),
// store per day history
Store.put(STORE_KEYS.CACHED_TESTING_HISTORY, JSON.stringify({
success: true,
data: getPerDayRecords(rawRecords),
lastRefreshed,
lastOriginUpdate: latestData.timestamp
}))
]);
return rawResponse(rawRecords);
}
else {
const error = {code: response.status, status: response.statusText, body: await response.text()};
return await errorResponse(error);
}
}
function getPerDayRecords(rawRecords) {
const perDayDict = {};
for (let i=0; i<rawRecords.length; i++) {
const day = rawRecords[i].timestamp.substring(0,10);
perDayDict[day] = rawRecords[i];
}
return Object.values(perDayDict).map(x => ({
day: x.timestamp.substring(0,10),
totalSamplesTested: x.totalSamplesTested,
totalIndividualsTested: x.totalIndividualsTested,
totalPositiveCases: x.totalPositiveCases,
source: x.source
})).sort((x, y) => x.day.localeCompare(y.day));
}
function getLastUpdated(content) {
const r = RegExp("(\\d+)/(\\d+)/(\\d+) (\\d{1,2})[:.](\\d{1,2})([:.]\\d{1,2})?");
const m = content.toLowerCase().match(r);
if (!m || m.length !== 7) {
console.log(`invalid timestamp in ICMR data: ${content}`);
throw `invalid timestamp in ICMR data: ${content}`;
}
const day = parseInt(m[1]);
const month = parseInt(m[2])-1;
const year = parseInt(m[3]);
const hour = parseInt(m[4]) + (content.endsWith("pm") ? 12 : 0);
const min = parseInt(m[5]);
return new Date(Date.UTC(year, month, day, hour, min, 0, 0) - 330*60*1000).toISOString();
}
const SOURCE_URL = "https://api.covid19india.org/data.json";