This repository has been 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
/
build-chart.js
928 lines (878 loc) · 29.6 KB
/
build-chart.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
const fs = require('fs');
const minifyHtml = require('html-minifier-terser').minify;
const prettier = require('prettier');
const template = require('lodash.template');
const {addDays, readCsvFile, fillGaps} = require('./utils.js');
const {POPULATION_GERMANY, POPULATION_PER_STATE} = require('./population.js');
const getCumulativeDeliveries = require('./cumulative-deliveries.js');
const {latestNationalData, pluckFromNationalCumulativeData} = require('./read-national-csv.js');
const listFormatter = new Intl.ListFormat('en');
const formatList = (items) => {
return listFormatter.format(items);
};
const BUNDESWEHR = 'Bundeswehr';
const ANOMALIES_PER_STATE = new Map();
const MARKDOWN_TABLE_LINES = [];
const checkState = (state, data) => {
for (const {name, values} of data.datasets) {
let tmp = 0;
for (const [index, value] of values.entries()) {
if (value < tmp) {
const date = data.labels[index];
MARKDOWN_TABLE_LINES.push(`${date}|${state}|${name.toLowerCase()}|${intFormatter.format(tmp)}|${intFormatter.format(value)
}|-${intFormatter.format(tmp - value)}`);
if (ANOMALIES_PER_STATE.has(state)) {
ANOMALIES_PER_STATE.get(state).add(date);
} else {
ANOMALIES_PER_STATE.set(state, new Set([date]));
}
}
tmp = value;
}
}
};
const dataAnomalyWarning = (state) => {
if (ANOMALIES_PER_STATE.has(state)) {
const dates = [...ANOMALIES_PER_STATE.get(state)].sort();
const plural = dates.length > 1;
return `<p><strong>Note:</strong> The drop${plural ? 's' : ''} on ${formatList(dates.map(date => `<time>${date}</time>`))} ${plural ? 'are' : 'is'} not ${plural ? 'errors' : 'an error'} in our chart — <a href="https://github.com/mathiasbynens/covid-19-vaccinations-germany#anomalies-in-the-data">it matches the data reported by the RKI</a>, which either underreported these numbers, or overreported the numbers for the preceding days. \u{1F937}</p>`;
}
return '';
};
const records = readCsvFile('./data/data.csv');
const states = new Set();
const map = new Map();
let maxCount = 0;
let oldestDate = '9001-12-31';
let latestDate = '1970-01-01';
let latestPubDate = '1970-01-01';
for (const {date, pubDate, state, onlyPartiallyVaccinatedCumulative, onlyPartiallyVaccinatedPercent, atLeastPartiallyVaccinatedCumulative, atLeastPartiallyVaccinatedPercent, fullyVaccinatedCumulative, fullyVaccinatedPercent, totalDosesCumulative, initialDosesCumulative, finalDosesCumulative, initialDosesPercent, finalDosesPercent, initialDosesCumulativeBioNTech, finalDosesCumulativeBioNTech, initialDosesCumulativeModerna, finalDosesCumulativeModerna, initialDosesCumulativeAstraZeneca, finalDosesCumulativeAstraZeneca, finalDosesCumulativeJohnsonAndJohnson, boosterDosesCumulative, boosterDosesCumulativeBioNTech, boosterDosesCumulativeModerna, boosterDosesCumulativeJohnsonAndJohnson, onlyPartiallyVaccinatedCumulativeBioNTech, onlyPartiallyVaccinatedCumulativeModerna, onlyPartiallyVaccinatedCumulativeAstraZeneca, fullyVaccinatedCumulativeBioNTech, fullyVaccinatedCumulativeModerna, fullyVaccinatedCumulativeAstraZeneca, fullyVaccinatedCumulativeJohnsonAndJohnson} of records) {
if (state !== BUNDESWEHR) states.add(state);
const countInitialDoses = Number(initialDosesCumulative);
const countFinalDoses = Number(finalDosesCumulative);
const countBoosterDoses = Number(boosterDosesCumulative);
const countTotal = Number(totalDosesCumulative);
const countBioNTech = Number(initialDosesCumulativeBioNTech) + Number(finalDosesCumulativeBioNTech) + Number(boosterDosesCumulativeBioNTech);
const countModerna = Number(initialDosesCumulativeModerna) + Number(finalDosesCumulativeModerna) + Number(boosterDosesCumulativeModerna);
const countAstraZeneca = Number(initialDosesCumulativeAstraZeneca) + Number(finalDosesCumulativeAstraZeneca);
const countJohnsonAndJohnson = Number(finalDosesCumulativeJohnsonAndJohnson) + + Number(boosterDosesCumulativeJohnsonAndJohnson);
if (countTotal > maxCount) {
maxCount = countTotal;
}
if (date > latestDate) {
latestDate = date;
}
if (date < oldestDate) {
oldestDate = date;
}
if (pubDate > latestPubDate) {
latestPubDate = pubDate;
}
const percentInitialDose = Number(atLeastPartiallyVaccinatedPercent);
const percentFinalDose = Number(fullyVaccinatedPercent);
if (!map.has(date)) {
map.set(date, new Map());
}
map.get(date).set(state, {
cumulativeTotal: countTotal,
cumulativeInitial: countInitialDoses,
cumulativeFinal: countFinalDoses,
cumulativeBooster: countBoosterDoses,
cumulativeTotalBioNTech: countBioNTech,
cumulativeTotalModerna: countModerna,
cumulativeTotalAstraZeneca: countAstraZeneca,
cumulativeTotalJohnsonAndJohnson: countJohnsonAndJohnson,
onlyPartiallyVaccinatedCumulative: Number(onlyPartiallyVaccinatedCumulative),
onlyPartiallyVaccinatedPercent: Number(onlyPartiallyVaccinatedPercent),
atLeastPartiallyVaccinatedCumulative: Number(atLeastPartiallyVaccinatedCumulative),
atLeastPartiallyVaccinatedPercent: Number(atLeastPartiallyVaccinatedPercent),
fullyVaccinatedCumulative: Number(fullyVaccinatedCumulative),
fullyVaccinatedPercent: Number(fullyVaccinatedPercent),
percentInitialDose: percentInitialDose,
percentFinalDose: percentFinalDose,
onlyPartiallyVaccinatedCumulativeBioNTech: Number(onlyPartiallyVaccinatedCumulativeBioNTech),
onlyPartiallyVaccinatedCumulativeModerna: Number(onlyPartiallyVaccinatedCumulativeModerna),
onlyPartiallyVaccinatedCumulativeAstraZeneca: Number(onlyPartiallyVaccinatedCumulativeAstraZeneca),
fullyVaccinatedCumulativeBioNTech: Number(fullyVaccinatedCumulativeBioNTech),
fullyVaccinatedCumulativeModerna: Number(fullyVaccinatedCumulativeModerna),
fullyVaccinatedCumulativeAstraZeneca: Number(fullyVaccinatedCumulativeAstraZeneca),
fullyVaccinatedCumulativeJohnsonAndJohnson: Number(fullyVaccinatedCumulativeJohnsonAndJohnson),
});
}
// Fill the gaps in the data. (Missing days, usually over the weekend.)
const sortedMap = fillGaps(map, oldestDate, latestDate);
const dosesPerDayRecords = readCsvFile('./data/doses-per-day.csv');
const {
cumulativeDeliveryMap,
latestDeliveryDate,
cumulativeNationalDosesDelivered,
cumulativeNationalDosesDeliveredPerVaccine,
} = getCumulativeDeliveries({ startDate: oldestDate, endDate: latestDate });
const cumulativeDosesDeliveredVsAdministered = {
BioNTech: { delivered: [], administered: [] },
Moderna: { delivered: [], administered: [] },
AstraZeneca: { delivered: [], administered: [] },
JohnsonAndJohnson: { delivered: [], administered: [] },
};
for (const [date, cumulativeDeliveredDosesPerVaccine] of cumulativeNationalDosesDeliveredPerVaccine) {
if (date < oldestDate) continue;
// Cumulative delivered doses per vaccine.
cumulativeDosesDeliveredVsAdministered.BioNTech.delivered.push(cumulativeDeliveredDosesPerVaccine.get('Pfizer/BioNTech'));
cumulativeDosesDeliveredVsAdministered.Moderna.delivered.push(cumulativeDeliveredDosesPerVaccine.get('Moderna'));
cumulativeDosesDeliveredVsAdministered.AstraZeneca.delivered.push(cumulativeDeliveredDosesPerVaccine.get('Oxford/AstraZeneca'));
cumulativeDosesDeliveredVsAdministered.JohnsonAndJohnson.delivered.push(cumulativeDeliveredDosesPerVaccine.get('Johnson & Johnson'));
// Cumulative administered doses per vaccine.
let cumulativeAdministeredBioNTech = 0;
let cumulativeAdministeredModerna = 0;
let cumulativeAdministeredAstraZeneca = 0;
let cumulativeAdministeredJohnsonAndJohnson = 0;
const entries = sortedMap.get(date);
for (const [state, metrics] of entries) {
cumulativeAdministeredBioNTech += metrics.cumulativeTotalBioNTech;
cumulativeAdministeredModerna += metrics.cumulativeTotalModerna;
cumulativeAdministeredAstraZeneca += metrics.cumulativeTotalAstraZeneca;
cumulativeAdministeredJohnsonAndJohnson += metrics.cumulativeTotalJohnsonAndJohnson;
}
cumulativeDosesDeliveredVsAdministered.BioNTech.administered.push(cumulativeAdministeredBioNTech);
cumulativeDosesDeliveredVsAdministered.Moderna.administered.push(cumulativeAdministeredModerna);
cumulativeDosesDeliveredVsAdministered.AstraZeneca.administered.push(cumulativeAdministeredAstraZeneca);
cumulativeDosesDeliveredVsAdministered.JohnsonAndJohnson.administered.push(cumulativeAdministeredJohnsonAndJohnson);
}
const percentFormatter = new Intl.NumberFormat('en', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
function getMetric(metric, state) {
const current = state ?
map.get(latestDate).get(state)[metric] :
latestNationalData[metric];
return intFormatter.format(current);
}
function getPercentage(metric, state) {
if (state) {
const latestEntries = sortedMap.get(latestDate);
const latestStateEntries = latestEntries.get(state);
const percent = latestStateEntries[metric];
return percentFormatter.format(percent);
}
const percent = latestNationalData[metric];
return percentFormatter.format(percent);
}
const intFormatter = new Intl.NumberFormat('en', {
minimumFractionDigits: 0,
maximumFractionDigits: 0,
});
function population(state) {
const number = state ?
POPULATION_PER_STATE.get(state) :
POPULATION_GERMANY;
return intFormatter.format(number);
}
function currentDoses(state) {
const current = state ?
map.get(latestDate).get(state).cumulativeTotal :
latestNationalData.totalDosesCumulative;
return intFormatter.format(current);
}
function currentDosesPerVaccine(vaccineId, metric) {
// metric = 'administered' | 'delivered'
const current = cumulativeDosesDeliveredVsAdministered[vaccineId][metric].slice(-1);
return intFormatter.format(current);
}
function vaccinatedWithVaccineId(vaccineId, metric) {
// metric = fullyVaccinated | onlyPartiallyVaccinated
const current = nationalCumulativePerVaccine[metric][vaccineId];
return intFormatter.format(current);
}
function percentVaccinatedWithVaccineId(vaccineId, metric) {
// metric = fullyVaccinated | onlyPartiallyVaccinated
const current = nationalCumulativePerVaccine[metric][vaccineId];
const total = metric === 'fullyVaccinated' ?
latestNationalData.fullyVaccinatedCumulative :
latestNationalData.onlyPartiallyVaccinatedCumulative;
const percent = current / total * 100;
return percentFormatter.format(percent);
}
function currentDosesPerTotalDosesDeliveredPerVaccine(vaccineId) {
const metrics = cumulativeDosesDeliveredVsAdministered[vaccineId];
const percent = metrics.administered.slice(-1) / metrics.delivered.slice(-1) * 100;
return percentFormatter.format(percent);
}
function percentAdministeredPerVaccine(vaccineId) {
const current = cumulativeDosesDeliveredVsAdministered[vaccineId].administered.slice(-1);
const percent = current / latestNationalData.totalDosesCumulative * 100;
return percentFormatter.format(percent);
}
function percentDeliveredPerVaccine(vaccineId) {
const current = cumulativeDosesDeliveredVsAdministered[vaccineId].delivered.slice(-1);
const percent = current / cumulativeNationalDosesDelivered * 100;
return percentFormatter.format(percent);
}
function currentDosesPerTotalDosesDelivered(state) {
const percent = (() => {
if (state) {
const cumulativeTotal = map.get(latestDate).get(state).cumulativeTotal;
const cumulativeDosesDelivered = cumulativeDeliveryMap.get(latestDate).get(state);
return cumulativeTotal / cumulativeDosesDelivered * 100;
}
return latestNationalData.totalDosesCumulative / cumulativeNationalDosesDelivered * 100;
})();
return percentFormatter.format(percent);
}
function totalDosesDelivered(state) {
const number = state ?
cumulativeDeliveryMap.get(latestDate).get(state) :
cumulativeNationalDosesDelivered;
return intFormatter.format(number);
}
const lastWeek = addDays(latestDate, -7);
const nationalSevenDayAverage = (() => {
let total = 0;
for (const day of dosesPerDayRecords.slice(-7)) {
total += Number(day.totalDoses);
}
return total / 7;
})();
function sevenDayAverageDoses(state) {
const average = (() => {
if (state) {
const old = map.get(lastWeek).get(state).cumulativeTotal;
const current = map.get(latestDate).get(state).cumulativeTotal;
return (current - old) / 7;
}
return nationalSevenDayAverage;
})();
return intFormatter.format(average);
}
function sevenDayAverageDosesAsPercentage(state) {
const population = state ?
POPULATION_PER_STATE.get(state) :
POPULATION_GERMANY;
const average = (() => {
if (state) {
const old = map.get(lastWeek).get(state).cumulativeTotal;
const current = map.get(latestDate).get(state).cumulativeTotal;
return (current - old) / 7;
}
return nationalSevenDayAverage;
})();
return percentFormatter.format(average / population * 100);
}
let nationalCumulativePerVaccine = {
onlyPartiallyVaccinated: {},
fullyVaccinated: {},
};
function generateNationalData() {
const labels = [
// '2021-01-05',
// '2021-01-06',
// '2021-01-07',
...sortedMap.keys(),
];
const datasets = [
// {
// name: 'Initial dose',
// type: 'line',
// values: [77876, 82749, 84349],
// },
];
const countsAvailable = [];
for (const [date, entry] of sortedMap) {
let onlyPartiallyVaccinated = 0;
let onlyPartiallyVaccinatedBioNTech = 0;
let onlyPartiallyVaccinatedModerna = 0;
let onlyPartiallyVaccinatedAstraZeneca = 0;
let atLeastPartiallyVaccinated = 0;
let fullyVaccinated = 0;
let fullyVaccinatedBioNTech = 0;
let fullyVaccinatedModerna = 0;
let fullyVaccinatedAstraZeneca = 0;
let fullyVaccinatedJohnsonAndJohnson = 0;
let availableDoses = 0;
for (const data of entry.values()) {
onlyPartiallyVaccinated += data.onlyPartiallyVaccinatedCumulative;
onlyPartiallyVaccinatedBioNTech += data.onlyPartiallyVaccinatedCumulativeBioNTech;
onlyPartiallyVaccinatedModerna += data.onlyPartiallyVaccinatedCumulativeModerna;
onlyPartiallyVaccinatedAstraZeneca += data.onlyPartiallyVaccinatedCumulativeAstraZeneca;
atLeastPartiallyVaccinated += data.atLeastPartiallyVaccinatedCumulative;
fullyVaccinated += data.fullyVaccinatedCumulative;
fullyVaccinatedBioNTech += data.fullyVaccinatedCumulativeBioNTech;
fullyVaccinatedModerna += data.fullyVaccinatedCumulativeModerna;
fullyVaccinatedAstraZeneca += data.fullyVaccinatedCumulativeAstraZeneca;
fullyVaccinatedJohnsonAndJohnson += data.fullyVaccinatedCumulativeJohnsonAndJohnson;
availableDoses = cumulativeDeliveryMap.get(date).get('Total');
}
if (date === latestDate) {
nationalCumulativePerVaccine.onlyPartiallyVaccinated.BioNTech = onlyPartiallyVaccinatedBioNTech;
nationalCumulativePerVaccine.onlyPartiallyVaccinated.Moderna = onlyPartiallyVaccinatedModerna;
nationalCumulativePerVaccine.onlyPartiallyVaccinated.AstraZeneca = onlyPartiallyVaccinatedAstraZeneca;
nationalCumulativePerVaccine.fullyVaccinated.BioNTech = fullyVaccinatedBioNTech;
nationalCumulativePerVaccine.fullyVaccinated.Moderna = fullyVaccinatedModerna;
nationalCumulativePerVaccine.fullyVaccinated.AstraZeneca = fullyVaccinatedAstraZeneca;
nationalCumulativePerVaccine.fullyVaccinated.JohnsonAndJohnson = fullyVaccinatedJohnsonAndJohnson;
}
countsAvailable.push(availableDoses);
}
datasets.push(
{
name: 'Available doses',
chartType: 'bar',
values: countsAvailable,
},
{
name: 'Total doses',
chartType: 'line',
values: pluckFromNationalCumulativeData('totalDosesCumulative'),
},
{
name: 'Initial doses',
chartType: 'line',
values: pluckFromNationalCumulativeData('initialDosesCumulative'),
},
{
name: 'Final doses',
chartType: 'line',
values: pluckFromNationalCumulativeData('finalDosesCumulative'),
},
{
name: 'Booster doses',
chartType: 'line',
values: pluckFromNationalCumulativeData('boosterDosesCumulative'),
},
);
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
// {
// label: '',
// value: Math.round(maxCount * 1.05),
// type: 'solid'
// },
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/national-data.json`, `${stringified}\n`);
return stringified;
}
function average(numbers) {
let total = 0;
for (const number of numbers) {
total += number;
}
const result = total / numbers.length;
return Number(result.toFixed(0));
}
function generateNationalDosesPerDayData() {
const labels = [];
const countsTotal = [];
const countsInitialDose = [];
const countsFinalDose = [];
const countsBoosterDose = [];
for (const {date, initialDoses, finalDoses, firstBoosterDoses, totalDoses} of dosesPerDayRecords) {
labels.push(date);
countsTotal.push(Number(totalDoses));
countsInitialDose.push(Number(initialDoses));
countsFinalDose.push(Number(finalDoses));
countsBoosterDose.push(Number(firstBoosterDoses));
}
const sevenDayAverage = [];
for (const index of countsTotal.keys()) {
const slice = countsTotal.slice(Math.max(0, index - 6), index + 1);
sevenDayAverage.push(average(slice));
}
const datasets = [
{
name: 'Total doses',
chartType: 'bar',
values: countsTotal,
},
{
name: 'Initial doses',
chartType: 'bar',
values: countsInitialDose,
},
{
name: 'Final doses',
chartType: 'bar',
values: countsFinalDose,
},
{
name: 'Booster doses',
chartType: 'bar',
values: countsBoosterDose,
},
{
name: '7-day average of total doses',
chartType: 'line',
values: sevenDayAverage,
},
];
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
// {
// label: '',
// value: Math.round(maxCount * 1.05),
// type: 'solid'
// },
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/national-data-per-day.json`, `${stringified}\n`);
return stringified;
}
function generateNationalDosesPerWeekData() {
const labels = [];
const countsTotal = [];
const countsInitialDose = [];
const countsFinalDose = [];
const countsBoosterDose = [];
let weeklyTotal = 0;
let weeklyInitialDose = 0;
let weeklyFinalDose = 0;
let weeklyBoosterDose = 0;
let monday = '2020-12-21';
let nextSunday = '2020-12-27';
for (const {date, initialDoses, finalDoses, firstBoosterDoses, totalDoses} of dosesPerDayRecords) {
weeklyTotal += Number(totalDoses);
weeklyInitialDose += Number(initialDoses);
weeklyFinalDose += Number(finalDoses);
weeklyBoosterDose += Number(firstBoosterDoses);
if (date === nextSunday || date === latestDate) {
// `Week from ${monday} to ${nextSunday}`
labels.push(monday);
countsTotal.push(weeklyTotal);
countsInitialDose.push(weeklyInitialDose);
countsFinalDose.push(weeklyFinalDose);
countsBoosterDose.push(weeklyBoosterDose);
weeklyTotal = 0;
weeklyInitialDose = 0;
weeklyFinalDose = 0;
weeklyBoosterDose = 0;
monday = addDays(date, 1);
nextSunday = addDays(date, 7);
}
}
const datasets = [
{
name: 'Total doses',
chartType: 'bar',
values: countsTotal,
},
{
name: 'Initial doses',
chartType: 'bar',
values: countsInitialDose,
},
{
name: 'Final doses',
chartType: 'bar',
values: countsFinalDose,
},
{
name: 'Booster doses',
chartType: 'bar',
values: countsBoosterDose,
},
];
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
// {
// label: '',
// value: Math.round(maxCount * 1.05),
// type: 'solid'
// },
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/national-data-per-week.json`, `${stringified}\n`);
return stringified;
}
function generatePartialVsFullVaccinationsData() {
const labels = [
// '2021-01-05',
// '2021-01-06',
// '2021-01-07',
...sortedMap.keys(),
];
const datasets = [
{
name: 'People who received at least one dose',
type: 'line',
values: pluckFromNationalCumulativeData('atLeastPartiallyVaccinatedCumulative'),
},
{
name: 'People waiting for second dose',
type: 'line',
values: pluckFromNationalCumulativeData('onlyPartiallyVaccinatedCumulative'),
},
{
name: 'People who are fully vaccinated',
type: 'line',
values: pluckFromNationalCumulativeData('fullyVaccinatedCumulative'),
},
];
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
// {
// label: '',
// value: Math.round(maxCount * 1.05),
// type: 'solid'
// },
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/waiting-for-second-dose-data.json`, `${stringified}\n`);
return stringified;
}
function generatePercentData() {
const labels = [
// '2021-01-05',
// '2021-01-06',
// '2021-01-07',
...sortedMap.keys(),
];
const datasets = [
// {
// name: 'Bavaria',
// type: 'line',
// values: [2.9,5.93,8.28],
// },
];
for (const state of states) { // Guarantee consistent ordering.
const counts = [];
for (const entry of sortedMap.values()) {
const tmp = entry.get(state);
const percent = tmp.percentInitialDose + tmp.percentFinalDose;
const count = Number(percent.toFixed(2));
counts.push(count);
}
datasets.push({
name: state,
type: 'line',
values: counts,
});
}
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY=0.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid',
},
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/percent-data.json`, `${stringified}\n`);
return stringified;
}
function formatVaccineId(vaccineId) {
// vaccineId = BioNTech | Moderna | AstraZeneca | JohnsonAndJohnson
// Note: keep return values in sync with the known-vaccines.js labels.
switch (vaccineId) {
case 'BioNTech':
return 'Pfizer/BioNTech';
case 'Moderna':
return 'Moderna';
case 'AstraZeneca':
return 'Oxford/AstraZeneca';
case 'JohnsonAndJohnson':
return 'Johnson & Johnson';
}
}
const vaccineIds = [
'BioNTech',
'Moderna',
'AstraZeneca',
'JohnsonAndJohnson',
];
function generatePerVaccineData(vaccineId) {
// vaccineId = BioNTech | Moderna | AstraZeneca | JohnsonAndJohnson
const labels = [
...sortedMap.keys(),
];
const datasets = [
{
name: 'Available doses',
chartType: 'bar',
values: cumulativeDosesDeliveredVsAdministered[vaccineId].delivered,
},
{
name: 'Administered doses',
chartType: 'line',
values: cumulativeDosesDeliveredVsAdministered[vaccineId].administered,
},
];
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/per-vaccine-data-${vaccineId}.json`, `${stringified}\n`);
return stringified;
}
let isDeliveryDataDefinitelyOutdated = false;
function generateRolloutData() {
const labels = [
// '2021-01-05',
// '2021-01-06',
// '2021-01-07',
...sortedMap.keys(),
];
const datasets = [
// {
// name: 'Bavaria',
// type: 'line',
// values: [2.9,5.93,8.28],
// },
];
for (const state of states) { // Guarantee consistent ordering.
if (state === BUNDESWEHR) continue;
const counts = [];
for (const [date, entry] of sortedMap) {
const administered = entry.get(state).cumulativeTotal;
const delivered = cumulativeDeliveryMap.get(date).get(state);
const count = Number((administered / delivered * 100).toFixed(2));
if (count > 100) {
isDeliveryDataDefinitelyOutdated = true;
}
counts.push(count);
}
datasets.push({
name: state,
type: 'line',
values: counts,
});
}
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY=0.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid',
},
],
};
const stringified = JSON.stringify(data, null, 2);
fs.writeFileSync(`./tmp/rollout-data.json`, `${stringified}\n`);
return stringified;
}
const rolloutData = generateRolloutData();
const STATE_DATA_CACHE = new Map();
function generateStateData(desiredState) {
if (STATE_DATA_CACHE.has(desiredState)) {
return STATE_DATA_CACHE.get(desiredState);
}
const labels = [
// '2021-01-05',
// '2021-01-06',
// '2021-01-07',
...sortedMap.keys(),
];
const datasets = [
// {
// name: 'Initial dose',
// type: 'line',
// values: [77876, 82749, 84349],
// },
];
const countsTotal = [];
const countsInitialDose = [];
const countsFinalDose = [];
const countsBoosterDose = [];
const countsAvailable = [];
for (const [date, entry] of sortedMap) {
const data = entry.get(desiredState);
countsTotal.push(data.cumulativeTotal);
countsInitialDose.push(data.cumulativeInitial);
countsFinalDose.push(data.cumulativeFinal);
countsBoosterDose.push(data.cumulativeBooster);
const availableDoses = cumulativeDeliveryMap.get(date).get(desiredState);
countsAvailable.push(availableDoses);
}
datasets.push(
{
name: 'Available doses',
chartType: 'bar',
values: countsAvailable,
},
{
name: 'Total doses',
chartType: 'line',
values: countsTotal,
},
{
name: 'Initial doses',
chartType: 'line',
values: countsInitialDose,
},
{
name: 'Final doses',
chartType: 'line',
values: countsFinalDose,
},
{
name: 'Booster doses',
chartType: 'line',
values: countsBoosterDose,
},
);
const data = {
labels,
datasets,
// This is a workaround that effectively sets minY and maxY.
// https://github.com/frappe/charts/issues/86
yMarkers: [
{
label: '',
value: 0,
type: 'solid'
},
// {
// label: '',
// value: Math.round(maxCount * 1.05),
// type: 'solid'
// },
],
};
checkState(desiredState, data);
const stringified = JSON.stringify(data, null, 2);
STATE_DATA_CACHE.set(desiredState, stringified);
fs.writeFileSync(`./tmp/state-data-${desiredState}.json`, `${stringified}\n`);
return stringified;
}
for (const state of states) {
generateStateData(state);
}
{
const formatMarkdown = (text) => {
return prettier.format(text, {
parser: 'markdown'
});
};
const fixReadme = (markdown) => {
const file = './README.md';
const readme = fs.readFileSync(file, 'utf8').toString();
const updated = readme.replace(
/(?<=<!-- START AUTO-UPDATED ANOMALIES SECTION -->)([^<]+)(?=<!-- END AUTO-UPDATED ANOMALIES SECTION -->)/,
`\n${markdown}\n`
);
fs.writeFileSync(file, updated);
};
const markdown = formatMarkdown(
'|`date`|state|metric|previous value|current value|delta|\n|--|--|--|-:|-:|-:|\n' +
MARKDOWN_TABLE_LINES.sort().join('\n')
).trimEnd();
fixReadme(markdown);
}
const HTML_TEMPLATE = fs.readFileSync('./templates/chart.template', 'utf8');
const createHtml = template(HTML_TEMPLATE, {
interpolate: /<%=([\s\S]+?)%>/g,
imports: {
latestPubDate,
dataAnomalyWarning,
isDeliveryDataDefinitelyOutdated,
population,
getMetric,
getPercentage,
sevenDayAverageDoses,
sevenDayAverageDosesAsPercentage,
currentDoses,
currentDosesPerTotalDosesDelivered,
totalDosesDelivered,
latestDeliveryDate,
nationalData: generateNationalData(),
nationalDataPerDay: generateNationalDosesPerDayData(),
nationalDataPerWeek: generateNationalDosesPerWeekData(),
generatePerVaccineData,
formatVaccineId,
vaccineIds,
currentDosesPerVaccine,
currentDosesPerTotalDosesDeliveredPerVaccine,
percentAdministeredPerVaccine,
percentDeliveredPerVaccine,
vaccinatedWithVaccineId,
percentVaccinatedWithVaccineId,
generatePercentData,
rolloutData,
generateStateData,
generatePartialVsFullVaccinationsData,
},
});
const html = createHtml({
states: states,
});
const minified = minifyHtml(html, {
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: false,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
preserveLineBreaks: false,
preventAttributesEscaping: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeTagWhitespace: false,
sortAttributes: true,
sortClassName: true,
});
fs.writeFileSync('./index.html', minified);