-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCOVID-19 Dashboard.js
767 lines (660 loc) · 32.9 KB
/
COVID-19 Dashboard.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: syringe;
////////////////////////////////////////////////
// Debug ///////////////////////////////////////
////////////////////////////////////////////////
let debug = false
// Fine tune Debug Mode by modifying specific variables below
var logCache = true
var logCacheUpdateStatus = true
var logURLs = true
var temporaryLogging = true // if (temporaryLogging) { console.log("") }
////////////////////////////////////////////////
// Configuration ///////////////////////////////
////////////////////////////////////////////////
let cacheInvalidationInMinutes = 60
let smallWidgetWidth = 121
let padding = 14
let barWidth = smallWidgetWidth - 2 * padding
let barHeight = 3
let showFirstAndSecondVaccinationOnProgressBar = true
// Global Configuration ////////////////////////
let country = {
germany: "DEU",
canada: "CAN",
usa: "USA"
}
let flag = {
"DEU": "🇩🇪",
"CAN": "🇨🇦",
"USA": "🇺🇸"
}
// Local Configuration /////////////////////////
let location = {
kissing: "FDB",
augsburg: "A",
munich: "M",
freilassing: "BGL"
}
let coordinates = {
"FDB": "48.294,10.969",
"A": "48.366,10.898",
"M": "48.135,11.613",
"BGL": "47.835,12.970"
}
let name = {
"FDB": "Kissing",
"A": "Augsburg",
"M": "München",
"BGL": "Freilassing"
}
////////////////////////////////////////////////
// Disable Debug Logs in Production ////////////
////////////////////////////////////////////////
if (!debug) {
logCache = false
logCacheUpdateStatus = false
logURLs = false
temporaryLogging = false
}
////////////////////////////////////////////////
// Data ////////////////////////////////////////
////////////////////////////////////////////////
let today = new Date()
let formatter = new DateFormatter()
formatter.locale = "en"
formatter.dateFormat = "MMM d"
// Vaccination Data ////////////////////////////
let vaccinationResponseMemoryCache
let vaccinationData = {}
await loadVaccinationData(country.germany)
await loadVaccinationData(country.canada)
await loadVaccinationData(country.usa)
// Global Case Data ////////////////////////////
let globalCaseData = {}
await loadGlobalCaseData(country.germany)
await loadGlobalCaseData(country.canada)
await loadGlobalCaseData(country.usa)
// Local Case Data /////////////////////////////
let localCaseData = {}
let localHistoryData = {}
await loadLocalCaseData(location.kissing)
await loadLocalCaseData(location.augsburg)
await loadLocalCaseData(location.munich)
await loadLocalCaseData(location.freilassing)
await loadLocalHistoryData(location.kissing)
await loadLocalHistoryData(location.augsburg)
await loadLocalHistoryData(location.munich)
await loadLocalHistoryData(location.freilassing)
////////////////////////////////////////////////
// Debug Execution - DO NOT MODIFY /////////////
////////////////////////////////////////////////
printCache()
////////////////////////////////////////////////
// Widget //////////////////////////////////////
////////////////////////////////////////////////
let widget = new ListWidget()
widget.setPadding(padding, padding, padding, padding)
await createWidget()
////////////////////////////////////////////////
// Script //////////////////////////////////////
////////////////////////////////////////////////
Script.setWidget(widget)
Script.complete()
if (config.runsInApp) {
widget.presentMedium()
}
////////////////////////////////////////////////
// Widget Creation /////////////////////////////
////////////////////////////////////////////////
async function createWidget() {
let canvas = widget.addStack()
canvas.layoutVertically()
displayTitle(canvas)
canvas.addSpacer()
displayContent(canvas)
canvas.addSpacer()
displayFooter(canvas)
}
// Title ///////////////////////////////////////
function displayTitle(canvas) {
let title = canvas.addText("COVID-19 Dashboard".toUpperCase())
title.font = Font.semiboldRoundedSystemFont(13)
title.textColor = Color.dynamic(Color.darkGray(), Color.lightGray())
}
// Content /////////////////////////////////////
function displayContent(canvas) {
let contentContainer = canvas.addStack()
contentContainer.layoutHorizontally()
let localContainer = contentContainer.addStack()
localContainer.layoutVertically()
displayLocalData(localContainer)
contentContainer.addSpacer(padding)
let globalContainer = contentContainer.addStack()
globalContainer.size = new Size(smallWidgetWidth, -1)
globalContainer.layoutVertically()
displayGlobalData(globalContainer)
}
// Local Data //////////////////////////////////
function displayLocalData(canvas) {
displayPrimaryRegion(canvas, location.kissing)
canvas.addSpacer(2)
displaySecondaryRegionContainer(canvas, location.augsburg, location.munich, location.freilassing)
}
// Primary Region //////////////////////////////
function displayPrimaryRegion(canvas, location) {
let incidenceValue = localCaseData[location].cases7_per_100k.toFixed(1)
let primaryLocationContainer = canvas.addStack()
primaryLocationContainer.layoutHorizontally()
let nameContainer = primaryLocationContainer.addStack()
nameContainer.layoutVertically()
let locationLabel = nameContainer.addText(name[location])
locationLabel.font = Font.mediumRoundedSystemFont(13)
let tendencyContainer = nameContainer.addStack()
tendencyContainer.layoutHorizontally()
tendencyContainer.addSpacer(10)
let tendencyLabel = tendencyContainer.addText(getLocalTendency(location))
tendencyLabel.font = Font.boldRoundedSystemFont(30)
tendencyLabel.textColor = incidenceColor(incidenceValue)
tendencyContainer.addSpacer(10)
primaryLocationContainer.addSpacer()
let incidenceContainer = primaryLocationContainer.addStack()
incidenceContainer.layoutVertically()
incidenceContainer.addSpacer()
let incidenceLabel = incidenceContainer.addText(incidenceValue)
incidenceLabel.font = Font.mediumRoundedSystemFont(40)
incidenceLabel.textColor = incidenceColor(incidenceValue)
}
// Secondary Region Container //////////////////
function displaySecondaryRegionContainer(canvas, location1, location2, location3) {
let container = canvas.addStack()
displaySecondaryRegion(container, location1)
container.addSpacer()
displaySecondaryRegion(container, location2)
container.addSpacer()
displaySecondaryRegion(container, location3)
}
// Secondary Region ////////////////////////////
function displaySecondaryRegion(canvas, location) {
let container = canvas.addStack()
container.layoutVertically()
let locationLabel = container.addText(name[location])
locationLabel.font = Font.mediumRoundedSystemFont(10)
locationLabel.textColor = Color.dynamic(Color.darkGray(), Color.lightGray())
container.addSpacer(2)
let incidenceValue = localCaseData[location].cases7_per_100k.toFixed(1)
let incidenceLabel = container.addText(incidenceValue + " " + getLocalTendency(location))
incidenceLabel.font = Font.semiboldRoundedSystemFont(10)
incidenceLabel.textColor = incidenceColor(incidenceValue)
}
// Global Data /////////////////////////////////
function displayGlobalData(canvas) {
displayCountry(canvas, country.germany)
canvas.addSpacer()
displayCountry(canvas, country.canada)
canvas.addSpacer()
displayCountry(canvas, country.usa)
canvas.addSpacer(2)
}
// Content Row /////////////////////////////////
function displayCountry(canvas, country) {
displayInformation(canvas, country)
canvas.addSpacer(2)
displayProgressBar(canvas, country)
}
// Country Data ////////////////////////////////
function displayInformation(canvas, country) {
let informationContainer = canvas.addStack()
informationContainer.layoutHorizontally()
displayFlag(informationContainer, country)
informationContainer.addSpacer()
displayIncidence(informationContainer, country)
displayPercentage(informationContainer, country)
}
// Flag ////////////////////////////////////////
function displayFlag(canvas, country) {
let flagLabel = canvas.addText(flag[country])
flagLabel.font = Font.regularRoundedSystemFont(13)
}
// 7-Day Incidence /////////////////////////////
function displayIncidence(canvas, country) {
let smallLabelContainer = canvas.addStack()
smallLabelContainer.layoutVertically()
smallLabelContainer.addSpacer(2)
let incidenceValue = get7DayIncidence(country).toFixed(1)
let incidenceLabel = smallLabelContainer.addText(incidenceValue + " " + getTendency(country))
incidenceLabel.font = Font.semiboldRoundedSystemFont(10)
incidenceLabel.textColor = incidenceColor(incidenceValue)
}
// Total Vaccination Percentage ////////////////
function displayPercentage(canvas, country) {
let percentageContainer = canvas.addStack()
percentageContainer.size = new Size(50, 0)
percentageContainer.layoutHorizontally()
percentageContainer.addSpacer()
let vaccinationPercentage = vaccinationData[country].people_fully_vaccinated_per_hundred
let percentageLabel = percentageContainer.addText(vaccinationPercentage.toFixed(1) + "%")
percentageLabel.font = Font.mediumRoundedSystemFont(13)
percentageLabel.minimumScaleFactor = 0.8
percentageLabel.lineLimit = 1
}
// Vaccination Progress Bar ////////////////////
function displayProgressBar(canvas, country) {
let firstVaccinationPercentage = vaccinationData[country].people_vaccinated_per_hundred
let vaccinationPercentage = vaccinationData[country].people_fully_vaccinated_per_hundred
let progressBar = canvas.addImage(drawProgressBar(firstVaccinationPercentage, vaccinationPercentage))
progressBar.cornerRadius = barHeight / 2
}
// Progress Bar Creation ///////////////////////
function drawProgressBar(firstVaccinationPercentage, fullVaccinationPercentage) {
// Total Vaccination Target in Percent
let target = {
good: 60,
perfect: 70
}
// Drawing Canvas
let canvas = new DrawContext()
canvas.size = new Size(barWidth, barHeight)
canvas.opaque = false
canvas.respectScreenScale = true
// Bar Container
canvas.setFillColor(Color.dynamic(Color.darkGray(), Color.lightGray()))
let bar = new Path()
let backgroundRect = new Rect(0, 0, barWidth, barHeight)
bar.addRect(backgroundRect)
canvas.addPath(bar)
canvas.fillPath()
if (showFirstAndSecondVaccinationOnProgressBar) {
// Progress Bar Color for first vaccination
let firstVaccinationColor = Color.dynamic(Color.lightGray(), Color.darkGray())
// First Vaccination Progress Bar
canvas.setFillColor(firstVaccinationColor)
let firstVaccinationProgress = new Path()
let firstVaccinationQuotient = firstVaccinationPercentage / 100
let firstVaccinationProgressWidth = Math.min(barWidth, barWidth * firstVaccinationQuotient) // Makes breaking the scale impossible although barWidth * quotient should suffice
firstVaccinationProgress.addRect(new Rect(0, 0, firstVaccinationProgressWidth, barHeight))
canvas.addPath(firstVaccinationProgress)
canvas.fillPath()
}
// Progress Bar Color depending on vaccination status
let color
if (fullVaccinationPercentage >= target.perfect) {
color = Color.green()
} else if (fullVaccinationPercentage >= target.good) {
color = Color.orange()
} else {
color = Color.red()
}
// Progress Bar
canvas.setFillColor(color)
let progress = new Path()
let quotient = fullVaccinationPercentage / 100
let progressWidth = Math.min(barWidth, barWidth * quotient) // Makes breaking the scale impossible although barWidth * quotient should suffice
progress.addRect(new Rect(0, 0, progressWidth, barHeight))
canvas.addPath(progress)
canvas.fillPath()
return canvas.getImage()
}
// Footer //////////////////////////////////////
function displayFooter(canvas) {
let updateDictionary = getUpdateDictionary()
let sortedUpdates = Object.keys(updateDictionary).sort().reverse() // newest first
let updateInfoArray = sortedUpdates.map(k => relativeTimestamp(new Date(k)) + " (" + updateDictionary[k].join(', ') + ")")
let updateInfoText = updateInfoArray.join(', ')
let lastUpdateLabel = canvas.addText("Last Update: " + updateInfoText)
lastUpdateLabel.font = Font.mediumRoundedSystemFont(10)
lastUpdateLabel.textColor = Color.dynamic(Color.lightGray(), Color.darkGray())
}
function getUpdateDictionary() {
let updateFormatter = new DateFormatter()
updateFormatter.locale = "en"
updateFormatter.dateFormat = "yyyy-MM-dd"
let updateDict = {}
let rkiUpdates = [getLastRKIUpdate(location.kissing), getLastRKIUpdate(location.augsburg), getLastRKIUpdate(location.munich), getLastRKIUpdate(location.freilassing)]
let oldestLocalCasesUpdate = rkiUpdates.sort().reverse()[0]
if (!updateDict[updateFormatter.string(oldestLocalCasesUpdate)]) {
updateDict[updateFormatter.string(oldestLocalCasesUpdate)] = []
}
updateDict[updateFormatter.string(oldestLocalCasesUpdate)].push("RKI")
let jhuUpdates = [getLastJHUUpdate(country.germany), getLastJHUUpdate(country.canada), getLastJHUUpdate(country.usa)]
let oldestGlobalCasesUpdate = jhuUpdates.sort().reverse()[0]
if (!updateDict[updateFormatter.string(oldestGlobalCasesUpdate)]) {
updateDict[updateFormatter.string(oldestGlobalCasesUpdate)] = []
}
updateDict[updateFormatter.string(oldestGlobalCasesUpdate)].push("JHU")
let owidUpdates = [getLastOWIDUpdate(country.germany), getLastOWIDUpdate(country.canada), getLastOWIDUpdate(country.usa)]
let oldestGlobalVaccinationsUpdate = owidUpdates.sort().reverse()[0]
if (!updateDict[updateFormatter.string(oldestGlobalVaccinationsUpdate)]) {
updateDict[updateFormatter.string(oldestGlobalVaccinationsUpdate)] = []
}
updateDict[updateFormatter.string(oldestGlobalVaccinationsUpdate)].push("OWID")
return updateDict
}
////////////////////////////////////////////////
// Calculations ////////////////////////////////
////////////////////////////////////////////////
function incidenceColor(incidenceValue) {
let color
if (incidenceValue < 35) {
color = Color.green()
} else if (incidenceValue < 50) {
color = Color.yellow()
} else if (incidenceValue < 100) {
color = Color.dynamic(new Color("e74300"), new Color("e64400"))
} else {
color = Color.dynamic(new Color("9e000a"), new Color("b61116")) // #ce2222
}
return color
}
function get7DayIncidence(country, requestedDate) {
// Start Index = Date Difference to Today (defaults to today)
let startIndex = requestedDate ? daysBetween(requestedDate, today) : 0
// Sum up daily new cases for the 7 days from the requested date (or today if none specified)
let newWeeklyCases = globalCaseData[country].cases.slice(startIndex, startIndex + 7).reduce(sum, 0)
let population = vaccinationData[country].population
return 100_000 * (newWeeklyCases / population)
}
function getTendency(country, accuracy, longTimeAccuracy) {
let tendencyIndicator = {
falling: "↘",
steady: "→",
rising: "↗"
}
let yesterday = new Date()
yesterday.setDate(today.getDate() - 1)
let lastWeek = new Date()
lastWeek.setDate(today.getDate() - 7)
let incidenceToday = get7DayIncidence(country, today)
let incidenceYesterday = get7DayIncidence(country, yesterday)
let incidenceLastWeek = get7DayIncidence(country, lastWeek)
let incidenceDifference = incidenceToday - incidenceYesterday
let longTermIncidenceDifference = incidenceToday - incidenceLastWeek
// The short term tendency is deemed steady if it differs less than the requested accuracy (default: 5)
let steadyRange = accuracy ?? 5
// The long term tendency is deemed steady if it differs less than the requested long term accuracy (default: 10)
let longTermSteadyRange = longTimeAccuracy ?? 10
// The short term tendency is the primary return value. If short term is steady, the long term tendency will be returned, if it is similar to the short term tendency.
let tendency
if (incidenceDifference < -steadyRange) {
tendency = tendencyIndicator.falling
} else if (incidenceDifference > steadyRange) {
tendency = tendencyIndicator.rising
} else if (incidenceDifference <= 0 && longTermIncidenceDifference < -longTermSteadyRange) {
tendency = tendencyIndicator.falling
} else if (incidenceDifference >= 0 && longTermIncidenceDifference > longTermSteadyRange) {
tendency = tendencyIndicator.rising
} else {
tendency = tendencyIndicator.steady
}
return tendency
}
function getLocal7DayIncidence(location, requestedDate) {
// Start Index = Date Difference to Today (defaults to today)
let startIndex = requestedDate ? daysBetween(requestedDate, today) : 0
// Sum up daily new cases for the 7 days from the requested date (or today if none specified)
let newWeeklyCasesArray = localHistoryData[location].cases.slice(startIndex, startIndex + 7)
let newWeeklyCases = localHistoryData[location].cases.slice(startIndex, startIndex + 7).reduce(sum, 0)
let population = localCaseData[location].EWZ
return 100_000 * (newWeeklyCases / population)
}
function getLocalTendency(location, accuracy, longTimeAccuracy) {
let tendencyIndicator = {
falling: "↘",
steady: "→",
rising: "↗"
}
let yesterday = new Date()
yesterday.setDate(today.getDate() - 1)
let lastWeek = new Date()
lastWeek.setDate(today.getDate() - 7)
let incidenceToday = getLocal7DayIncidence(location, today)
let incidenceYesterday = getLocal7DayIncidence(location, yesterday)
let incidenceLastWeek = getLocal7DayIncidence(location, lastWeek)
let incidenceDifference = incidenceToday - incidenceYesterday
let longTermIncidenceDifference = incidenceToday - incidenceLastWeek
// The short term tendency is deemed steady if it differs less than the requested accuracy (default: 5)
let steadyRange = accuracy ?? 5
// The long term tendency is deemed steady if it differs less than the requested long term accuracy (default: 10)
let longTermSteadyRange = longTimeAccuracy ?? 10
// The short term tendency is the primary return value. If short term is steady, the long term tendency will be returned, if it is similar to the short term tendency.
let tendency
if (incidenceDifference < -steadyRange) {
tendency = tendencyIndicator.falling
} else if (incidenceDifference > steadyRange) {
tendency = tendencyIndicator.rising
} else if (incidenceDifference <= 0 && longTermIncidenceDifference < -longTermSteadyRange) {
tendency = tendencyIndicator.falling
} else if (incidenceDifference >= 0 && longTermIncidenceDifference > longTermSteadyRange) {
tendency = tendencyIndicator.rising
} else {
tendency = tendencyIndicator.steady
}
return tendency
}
function getCoordinates(location) {
let coordinatesString = coordinates[location]
let splitCoordinates = coordinatesString.split(",").map(parseFloat)
return { latitude: splitCoordinates[0], longitude: splitCoordinates[1] }
}
function getRKIDateString(addDays) {
addDays = addDays || 0
return new Date(Date.now() + addDays * 24 * 60 * 60 * 1000).toISOString().substring(0, 10)
}
function getLastRKIUpdate(location) {
let lastUpdate = new Date(localHistoryData[location].last_updated_date)
// Since incidence is always determined by looking at cases from the previous day, we add 1 day here.
lastUpdate.setDate(lastUpdate.getDate() + 1)
// If data gets reported before midnight, the last update should still be today instead of tomorrow.
return lastUpdate.getTime() > today.getTime() ? today : lastUpdate
}
function getLastJHUUpdate(country) {
let lastUpdate = new Date(globalCaseData[country].last_updated_date)
// Since incidence is always determined by looking at cases from the previous day, we add 1 day here.
lastUpdate.setDate(lastUpdate.getDate() + 1)
// If data gets reported before midnight in our time zone, the last update should still show today instead of tomorrow.
return lastUpdate.getTime() > today.getTime() ? today : lastUpdate
}
function getLastOWIDUpdate(country) {
let lastUpdate = new Date(vaccinationData[country].last_updated_date)
// Since vaccinations are always reported at the end of the day, we add 1 day here (data from yesterday = last update today)
lastUpdate.setDate(lastUpdate.getDate() + 1)
// If data gets reported before midnight, the last update should still be today instead of tomorrow.
return lastUpdate.getTime() > today.getTime() ? today : lastUpdate
}
function relativeTimestamp(date) {
let yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
switch (formatter.string(date)) {
case formatter.string(today):
return "Today"
case formatter.string(yesterday):
return "Yesterday"
default:
return formatter.string(date)
}
}
function sum(a, b) {
return a + b
}
////////////////////////////////////////////////
// Networking //////////////////////////////////
////////////////////////////////////////////////
async function loadVaccinationData(country) {
let files = FileManager.local()
let cacheName = debug ? ("debug-api-cache-ourworldindata-latest-" + country) : ("api-cache-ourworldindata-latest-" + country)
let cachePath = files.joinPath(files.cacheDirectory(), cacheName)
let cacheExists = files.fileExists(cachePath)
let cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
try {
// Use Cache if available and last updated within specified `cacheInvalidationInMinutes
if (!debug && cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheInvalidationInMinutes * 60 * 1000)) {
if (logCacheUpdateStatus) { console.log(country + " Vaccination Data: Using cached Data") }
vaccinationData[country] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(country + " Vaccination Data: Updating cached Data") }
if (logURLs) { console.log("\nURL: Vaccination " + country) }
if (logURLs) { console.log('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.json') }
if (vaccinationResponseMemoryCache) {
vaccinationData[country] = vaccinationResponseMemoryCache[country]
} else {
let response = await new Request('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.json').loadJSON()
vaccinationData[country] = response[country]
}
files.writeString(cachePath, JSON.stringify(vaccinationData[country]))
}
} catch (error) {
console.error(error)
if (cacheExists) {
if (logCacheUpdateStatus) { console.log(country + " Vaccination Data: Loading new Data failed, using cached as fallback") }
vaccinationData[country] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(country + " Vaccination Data: Loading new Data failed and no Cache found") }
}
}
}
async function loadGlobalCaseData(country) {
let files = FileManager.local()
let cacheName = debug ? ("debug-api-cache-global-cases-" + country) : ("api-cache-global-cases-" + country)
let cachePath = files.joinPath(files.cacheDirectory(), cacheName)
let cacheExists = files.fileExists(cachePath)
let cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
try {
// Use Cache if available and last updated within specified `cacheInvalidationInMinutes
if (!debug && cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheInvalidationInMinutes * 60 * 1000)) {
if (logCacheUpdateStatus) { console.log(country + " Case Data: Using cached Data") }
globalCaseData[country] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(country + " Case Data: Updating cached Data") }
if (logURLs) { console.log("\nURL: Cases " + country) }
if (logURLs) { console.log('https://corona.lmao.ninja/v2/historical/' + country + '?lastdays=40') }
let response = await new Request('https://corona.lmao.ninja/v2/historical/' + country + '?lastdays=40').loadJSON()
let activeCases = {}
let dates = []
for (var entry in response.timeline.cases) {
let date = new Date(entry)
dates.push(date.getTime())
activeCases[date.getTime()] = response.timeline.cases[entry]
}
let sortedKeys = dates.sort().reverse()
globalCaseData[country] = {}
globalCaseData[country]["cases"] = sortedKeys.map(date => activeCases[date] - activeCases[date - 24 * 60 * 60 * 1000]).slice(0,-1)
// Add Last Update of JHU Data to Dictionary
let lastJHUDataUpdate = treatAsUTC(new Date(parseInt(sortedKeys[0]))).toISOString()
globalCaseData[country]["last_updated_date"] = lastJHUDataUpdate
files.writeString(cachePath, JSON.stringify(globalCaseData[country]))
}
} catch (error) {
console.error(error)
if (cacheExists) {
if (logCacheUpdateStatus) { console.log(country + " Case Data: Loading new Data failed, using cached as fallback") }
globalCaseData[country] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(country + " Case Data: Loading new Data failed and no Cache found") }
}
}
}
async function loadLocalCaseData(location) {
let files = FileManager.local()
let cacheName = debug ? ("debug-api-cache-local-cases-" + location) : ("api-cache-local-cases-" + location)
let cachePath = files.joinPath(files.cacheDirectory(), cacheName)
let cacheExists = files.fileExists(cachePath)
let cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
try {
// Use Cache if available and last updated within specified `cacheInvalidationInMinutes
if (!debug && cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheInvalidationInMinutes * 60 * 1000)) {
if (logCacheUpdateStatus) { console.log(location + " Case Data: Using cached Data") }
localCaseData[location] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(location + " Case Data: Updating cached Data") }
let coordinates = getCoordinates(location)
if (logURLs) { console.log("\nURL: Cases " + name[location]) }
if (logURLs) { console.log('https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=RS,GEN,cases7_per_100k,EWZ&geometry=' + coordinates.longitude.toFixed(3) + '%2C' + coordinates.latitude.toFixed(3) + '&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json') }
let response = await new Request('https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=RS,GEN,cases7_per_100k,EWZ&geometry=' + coordinates.longitude.toFixed(3) + '%2C' + coordinates.latitude.toFixed(3) + '&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json').loadJSON()
localCaseData[location] = response.features[0].attributes
files.writeString(cachePath, JSON.stringify(localCaseData[location]))
}
} catch (error) {
console.error(error)
if (cacheExists) {
if (logCacheUpdateStatus) { console.log(location + " Case Data: Loading new Data failed, using cached as fallback") }
localCaseData[location] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(location + " Case Data: Loading new Data failed and no Cache found") }
}
}
}
async function loadLocalHistoryData(location) {
let files = FileManager.local()
let cacheName = debug ? ("debug-api-cache-local-history-" + location) : ("api-cache-local-history-" + location)
let cachePath = files.joinPath(files.cacheDirectory(), cacheName)
let cacheExists = files.fileExists(cachePath)
let cacheDate = cacheExists ? files.modificationDate(cachePath) : 0
try {
// Use Cache if available and last updated within specified `cacheInvalidationInMinutes
if (!debug && cacheExists && (today.getTime() - cacheDate.getTime()) < (cacheInvalidationInMinutes * 60 * 1000)) {
if (logCacheUpdateStatus) { console.log(location + " History Data: Using cached Data") }
localHistoryData[location] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(location + " History Data: Updating cached Data") }
if (logURLs) { console.log("\nURL: History " + name[location]) }
if (logURLs) { console.log('https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?where=IdLandkreis%20%3D%20%27' + localCaseData[location].RS + '%27%20AND%20Meldedatum%20%3E%3D%20TIMESTAMP%20%27' + getRKIDateString(-15) + '%2000%3A00%3A00%27%20AND%20Meldedatum%20%3C%3D%20TIMESTAMP%20%27' + getRKIDateString(1) + '%2000%3A00%3A00%27&outFields=Landkreis,Meldedatum,AnzahlFall&outSR=4326&f=json') }
let response = await new Request('https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_COVID19/FeatureServer/0/query?where=IdLandkreis%20%3D%20%27' + localCaseData[location].RS + '%27%20AND%20Meldedatum%20%3E%3D%20TIMESTAMP%20%27' + getRKIDateString(-15) + '%2000%3A00%3A00%27%20AND%20Meldedatum%20%3C%3D%20TIMESTAMP%20%27' + getRKIDateString(1) + '%2000%3A00%3A00%27&outFields=Landkreis,Meldedatum,AnzahlFall&outSR=4326&f=json').loadJSON()
// The response contains multiple entries per day. This sums them up and creates a new dictionary with each days new cases as values and the corresponding UNIX timestamp as keys.
let aggregate = response.features.map(f => f.attributes).reduce((dict, feature) => {
dict[feature["Meldedatum"]] = (dict[feature["Meldedatum"]]|0) + feature["AnzahlFall"]
return dict
}, {})
let sortedKeys = Object.keys(aggregate).sort().reverse()
// Local History Data is now being sorted by keys (Timestamps) and put into a sorted array (newest day first).
localHistoryData[location] = {}
localHistoryData[location]["cases"] = sortedKeys.map(k => aggregate[k])
// Add Last Update of RKI Data to Dictionary
let lastRKIDataUpdate = new Date(parseInt(sortedKeys[0])).toISOString()
localHistoryData[location]["last_updated_date"] = lastRKIDataUpdate
files.writeString(cachePath, JSON.stringify(localHistoryData[location]))
}
} catch (error) {
console.error(error)
if (cacheExists) {
if (logCacheUpdateStatus) { console.log(location + " History Data: Loading new Data failed, using cached as fallback") }
localHistoryData[location] = JSON.parse(files.readString(cachePath))
} else {
if (logCacheUpdateStatus) { console.log(location + " History Data: Loading new Data failed and no Cache found") }
}
}
}
////////////////////////////////////////////////
// Date Calculation ////////////////////////////
////////////////////////////////////////////////
// --> see stackoverflow.com/a/11252167/6333824
function treatAsUTC(date) {
var result = new Date(date)
result.setMinutes(result.getMinutes() - result.getTimezoneOffset())
return result
}
function daysBetween(startDate, endDate) {
var millisecondsPerDay = 24 * 60 * 60 * 1000
return Math.round((treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay)
}
////////////////////////////////////////////////
// Debug ///////////////////////////////////////
////////////////////////////////////////////////
function printCache() {
if (logCache) {
console.log("\n\n**Global Vaccination Data**\n")
console.log(JSON.stringify(vaccinationData, null, 2))
console.log("\n\n**Global Cases Data**\n")
console.log(JSON.stringify(globalCaseData, null, 2))
console.log("\n\n**Local Cases Data**\n")
console.log(JSON.stringify(localCaseData, null, 2))
console.log("\n\n**Local History Data**\n")
console.log(JSON.stringify(localHistoryData, null, 2))
}
}
////////////////////////////////////////////////
// Author: Benno Kress /////////////////////////
// Original: Benno Kress ///////////////////////
// github.com/bennokress/Scriptable-Scripts ////
// Please copy every line! /////////////////////
////////////////////////////////////////////////