-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (79 loc) · 2.68 KB
/
index.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
85
86
87
88
89
90
91
92
93
const covidDataURL = 'https://coronabeds.jantasamvad.org/covid-info.js'
// const covidFacilitiesURL = 'https://coronabeds.jantasamvad.org/covid-facilities.js'
const io = require('indian-ocean')
const time = require('d3-time-format')
const parse = time.timeParse('%I:%M %p, %B %e')
const format = time.timeFormat("%2021-%m-%d-%H-%M")
const parseOuter = time.timeParse('%I:%M %p, %e %B %Y')
const fetchData = require('./lib/getUrl');
const combine = require('./lib/combine');
const names = ['beds', 'icu_beds_without_ventilator', 'noncovid_icu_beds', 'ventilators', 'covid_icu_beds', 'oxygen_left_for']
const run = async() => {
const covidData = await (fetchData(covidDataURL, 'gnctd_covid_data'))
const beds = covidData.beds
const icu_beds_without_ventilator = covidData.icu_beds_without_ventilator
const noncovid_icu_beds = covidData.noncovid_icu_beds
const ventilators = covidData.ventilators
const covid_icu_beds = covidData.covid_icu_beds
const oxygen_left_for = covidData.oxygen_left_for
const mergedData = await merged([beds, icu_beds_without_ventilator, noncovid_icu_beds, ventilators, covid_icu_beds, oxygen_left_for])
writeCSV(mergedData, covidData.last_updated_at)
}
run()
const merged = async(list) => {
let mergedData = {}
list.forEach(function(array, index){
for (var key in array) {
if (array.hasOwnProperty(key)) {
if (!mergedData[key]){
mergedData[key] = {}
}
mergedData[key][names[index]] = array[key]
if (array[key].type){
mergedData[key].props = {
type: array[key].type,
last_updated_at: convertTime(array[key].last_updated_at)
}
delete mergedData[key][names[index]].type;
delete mergedData[key][names[index]].last_updated_at;
}
}
}
})
return mergedData
};
const writeCSV = async(merged, last_updated_at) => {
let data = []
for (var key in merged) {
const o = {
name: key
}
for (var miniKey in merged[key]) {
if (miniKey!='props'){
if (names.includes(miniKey)){
for (var typeInfo in merged[key][miniKey]){
o[miniKey+'_'+typeInfo] = merged[key][miniKey][typeInfo]
}
} else {
o[miniKey] = merged[key][miniKey]
}
} else {
for (var propKey in merged[key][miniKey]){
o[propKey] = merged[key][miniKey][propKey]
}
}
}
if (key=='All'){
o['last_updated_at'] = format(parseOuter(last_updated_at))
}
data.push(o)
}
io.writeDataSync(`data/beds/${convertTimeOuter(last_updated_at)}.csv`, data)
combine('./data/beds/', './data/beds-by-hospital-timeseries.csv', './data/beds-total-timeseries.csv')
}
function convertTime(time){
return format(parse(time))
}
function convertTimeOuter(time){
return format(parseOuter(time))
}