This repository was archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathprocess-deliveries.js
112 lines (100 loc) · 2.89 KB
/
process-deliveries.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
const fs = require('fs');
const parseCsv = require('csv-parse/lib/sync');
const stringifyCsv = require('csv-stringify/lib/sync');
const {vaccineIdToLabel} = require('./known-vaccines.js');
const input = fs.readFileSync('./tmp/deliveries.csv', 'utf8');
const records = parseCsv(input, {
columns: true,
delimiter: '\t',
});
const formatVaccine = (id) => {
if (vaccineIdToLabel.has(id)) {
return vaccineIdToLabel.get(id);
}
throw new Error(`Unknown vaccine ID: ${id}`);
};
const stateMap = new Map([
['DE-BW', 'Baden-Württemberg'],
['DE-BY', 'Bayern'],
['DE-BE', 'Berlin'],
['DE-BB', 'Brandenburg'],
['DE-HB', 'Bremen'],
['DE-HH', 'Hamburg'],
['DE-HE', 'Hessen'],
['DE-MV', 'Mecklenburg-Vorpommern'],
['DE-NI', 'Niedersachsen'],
['DE-NW', 'Nordrhein-Westfalen'],
['DE-RP', 'Rheinland-Pfalz'],
['DE-SL', 'Saarland'],
['DE-SN', 'Sachsen'],
['DE-ST', 'Sachsen-Anhalt'],
['DE-SH', 'Schleswig-Holstein'],
['DE-TH', 'Thüringen'],
['DE-BUND', 'Bundeswehr'],
['DE-BETRIEBE', 'Companies']
]);
const formatState = (id) => {
id = id.toUpperCase();
return stateMap.get(id);
};
// state => Map<vaccineName => doses>
const startEntry = new Map();
// date => Map<state => records>
const recordsPerDate = new Map([
['2020-12-25', startEntry],
]);
for (const state of stateMap.values()) {
startEntry.set(state, new Map([
['Pfizer/BioNTech', 0],
]));
}
for (const record of records) {
const date = record.date;
const state = formatState(record.region);
const type = formatVaccine(record.impfstoff);
const doses = Number(record.dosen);
const destination = Number(record.einrichtung);
// Ensure there’s a date entry.
let entry;
if (recordsPerDate.has(date)) {
entry = recordsPerDate.get(date);
} else {
entry = new Map();
recordsPerDate.set(date, entry);
}
// Ensure there’s an entry for this state + date.
if (entry.has(state)) {
entry = entry.get(state);
} else {
const stateEntry = new Map();
entry.set(state, stateEntry);
entry = stateEntry;
}
// Ensure there’s an entry for this state + date + vaccine type.
if (entry.has(type)) {
entry.set(type, entry.get(type) + doses);
} else {
entry.set(type, doses);
}
}
const newRecords = [];
for (const [date, stateToRecords] of recordsPerDate) {
for (const [state, records] of stateToRecords) {
for (const [type, doses] of records) {
newRecords.push({
date: date,
state: state,
type: type,
doses: doses,
});
}
}
}
// Interestingly, the input CSV is not necessarily ordered by date.
newRecords.sort((a, b) => {
if (a.date > b.date) return 1;
if (a.date < b.date) return -1;
return a.state.localeCompare(b.state);
});
const csv = stringifyCsv(newRecords, { header: true });
fs.writeFileSync('./data/deliveries.csv', csv);