-
Notifications
You must be signed in to change notification settings - Fork 5
/
spf-donnees-hospitalieres-jobfile.js
113 lines (109 loc) · 3.84 KB
/
spf-donnees-hospitalieres-jobfile.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require('path')
const fs = require('fs-extra')
const _ = require('lodash')
const moment = require('moment')
const program = require('commander')
// Build departements list for each region to compute region statistics
const departements = fs.readJsonSync(path.join(__dirname, 'codes-departements-france.json'), { encoding: 'utf8' })
let regions = {}
departements.forEach(departement => {
const region = departement.reg.toString()
if (!regions[region]) regions[region] = []
regions[region].push(departement.dep.toString())
})
// By default try to grap latest data
program
.option('-d, --date [date]', 'Change the date of the data to be generated (defaults to yesterday)', moment().subtract(1, 'day').format('YYYY-MM-DD'))
.parse(process.argv)
const date = moment(program.date)
if (!date.isValid()) {
console.error('Invalid date, using yesterday as default')
date = moment().subtract(1, 'day')
}
module.exports = {
id: 'spf-donnees-hospitalieres',
store: 'memory',
tasks: [{
id: `spf-donnees-hospitalieres-${date.format('MM-DD-YYYY')}`,
type: 'http',
options: {
url: `https://www.data.gouv.fr/fr/datasets/r/63352e38-d353-4b54-bfd1-f1b3ee1cabd7`
}
}],
hooks: {
tasks: {
after: {
readCSV: {
headers: true,
delimiter: ';'
},
transformJson: {
filter: { jour: `${date.format('YYYY-MM-DD')}`, sexe: '0' }, // Select total not men/women data
mapping: {
dep: 'code',
hosp: 'hospitalises',
rea: 'reanimation',
rad: 'gueris',
dc: 'deces'
},
unitMapping: {
hospitalises: { asNumber: true, empty: 0 },
reanimation: { asNumber: true, empty: 0 },
gueris: { asNumber: true, empty: 0 },
deces: { asNumber: true, empty: 0 }
},
pick: ['code', 'hospitalises', 'reanimation', 'gueris', 'deces'],
outputPath: 'donneesDepartementales'
},
donneesRegionales: {
hook: 'apply',
dataPath: 'result.data',
function: (data) => {
const donneesDepartementales = data.donneesDepartementales
data.donneesRegionales = []
_.forOwn(regions, (value, key) => {
// Find matches
const entries = _.filter(donneesDepartementales, element => value.includes(element.code))
if (entries.length > 1) data.donneesRegionales.push(_.reduce(entries, (total, entry) => {
return {
code: key,
hospitalises: total.hospitalises + entry.hospitalises,
reanimation: total.reanimation + entry.reanimation,
gueris: total.gueris + entry.gueris,
deces: total.deces + entry.deces
}
}, entries.shift()))
else if (entries.length > 0) data.donneesRegionales.push(Object.assign({ code: key }, _.omit(entries[0], ['code'])))
})
data.donneesNationales = {}
if (data.donneesRegionales.length > 0) data.donneesNationales = _.reduce(_.tail(data.donneesRegionales), (total, entry) => {
return {
hospitalises: total.hospitalises + entry.hospitalises,
reanimation: total.reanimation + entry.reanimation,
gueris: total.gueris + entry.gueris,
deces: total.deces + entry.deces
}
}, _.head(data.donneesRegionales))
}
},
writeYAML: {
store: 'fs'
}
}
},
jobs: {
before: {
createStores: [{
id: 'memory'
}, {
id: 'fs',
options: { path: path.join(__dirname, 'spf-donnees-hospitalieres') }
}]
},
after: {
clearOutputs: {},
removeStores: ['memory', 'fs']
}
}
}
}