forked from opencovid19-fr/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
132 lines (107 loc) · 2.96 KB
/
build.js
File metadata and controls
132 lines (107 loc) · 2.96 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
#!/usr/bin/env node
const {join} = require('path')
const {flatten, chain} = require('lodash')
const yaml = require('js-yaml')
const {readFile, outputJson, outputFile} = require('fs-extra')
const glob = require('glob')
const Papa = require('papaparse')
const sources = [
'agences-regionales-sante',
'sante-publique-france',
'prefectures'
]
const distPath = join(__dirname, 'dist')
async function readYamlFile(filePath) {
const content = await readFile(filePath, {encoding: 'utf8'})
return yaml.safeLoad(content, {schema: yaml.JSON_SCHEMA})
}
function ensureArray(array) {
if (array && Array.isArray(array)) {
return array
}
if (array) {
return [array]
}
return []
}
function flattenData(initialData) {
const rows = []
ensureArray(initialData.donneesRegionales).forEach(row => rows.push(row))
ensureArray(initialData.donneesDepartementales).forEach(row => rows.push(row))
if (initialData.donneesNationales) {
rows.push({
...initialData.donneesNationales,
nom: 'France',
code: 'FRA'
})
}
if (initialData.donneesMondiales) {
rows.push({
...initialData.donneesMondiales,
nom: 'Monde',
code: 'WORLD'
})
}
return rows
}
function flattenSourcesData({source, date, rows}) {
return rows.map(row => ({
date,
source,
...row
}))
}
function getGranularite(code) {
if (code === 'FRA') {
return 'pays'
}
if (code === 'WORLD') {
return 'monde'
}
if (code.startsWith('REG')) {
return 'region'
}
if (code.startsWith('DEP')) {
return 'departement'
}
if (code.startsWith('COM')) {
return 'collectivite-outremer'
}
throw new Error('Type de granularité inconnu')
}
/* eslint camelcase: off */
function jsonToCsvRow(json) {
return {
date: json.date,
granularite: getGranularite(json.code),
maille_code: json.code,
maille_nom: json.nom,
cas_confirmes: 'casConfirmes' in json ? json.casConfirmes : '',
deces: 'deces' in json ? json.deces : '',
source_nom: (json.source && json.source.nom) || '',
source_url: (json.source && json.source.url) || ''
}
}
async function outputCsv(filePath, data) {
const csvData = Papa.unparse(data)
await outputFile(filePath, csvData)
}
async function main() {
const sourcesFiles = flatten(sources.map(source => glob.sync(`${source}/**/*.yaml`)))
const sourcesData = await Promise.all(sourcesFiles.map(async sourceFile => {
const data = await readYamlFile(join(__dirname, sourceFile))
return {date: data.date, source: data.source, rows: flattenData(data)}
}))
const flattenedData = chain(sourcesData)
.map(flattenSourcesData)
.flatten()
.filter(r => 'casConfirmes' in r || 'deces' in r)
.sortBy(r => `${r.date}-${r.code}`)
.value()
await outputJson(join(distPath, 'chiffres-cles.json'), flattenedData, {spaces: 2})
await outputCsv(join(distPath, 'chiffres-cles.csv'), flattenedData.map(jsonToCsvRow))
}
main().catch(error => {
console.error(error)
process.exit(1)
})