Last active
January 13, 2021 12:43
-
-
Save mattsson/4b72ed82ffba9272f27c5aed87f37c78 to your computer and use it in GitHub Desktop.
Vaccination Status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constants you can change | |
const citizensInDenmark = 5837213 | |
const herdImmunityPercentage = 0.65 | |
const url = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/Denmark.csv" | |
// Set constants | |
const totalVaccinationsNeeded = citizensInDenmark * herdImmunityPercentage * 2 | |
const widget = new ListWidget() | |
widget.spacing = 4 | |
// Download CSV report | |
const request = new Request(url) | |
const csv = await request.loadString() | |
// Split document into array with one line per index | |
let lines = csv.split(/\r?\n/) | |
// Find the last line that has text | |
var latestEntryLine = "" | |
while (latestEntryLine.length == 0) { | |
latestEntryLine = lines.pop() | |
} | |
// Latest entry split into elements | |
const latestLineElements = latestEntryLine.split(",") | |
const reportUrl = latestLineElements[3] | |
const latestDateString = latestLineElements[1] | |
const latestDate = new Date(latestDateString) | |
const formatter = new DateFormatter() | |
formatter.useMediumDateStyle() | |
var formattedLatestDate = formatter.string(latestDate) | |
const now = new Date() | |
const daysToLatest = daysBetween(latestDate, now) | |
if (daysToLatest == 0) { | |
formattedLatestDate = "Today" | |
} else if (daysToLatest == 1) { | |
formattedLatestDate = "Yesterday" | |
} | |
const totalDosesGiven = latestLineElements[4] | |
const percentageGiven = (totalDosesGiven / totalVaccinationsNeeded * 100).toFixed(2) | |
// Find older entry | |
var olderEntryLine = "" | |
for (var i = 0; i < 9; i++) { | |
olderEntryLine = lines.pop() | |
} | |
// Split into elements | |
const olderLineElements = olderEntryLine.split(",") | |
const olderDateString = olderLineElements[1] | |
const olderDoses = olderLineElements[4] | |
const olderDate = new Date(olderDateString) | |
const daysSinceOlderDate = daysBetween(olderDate, latestDate) | |
const dosesGivenSinceOlderDate = totalDosesGiven - olderDoses | |
const dosesNeededForHerd = parseInt(totalVaccinationsNeeded * herdImmunityPercentage) | |
const remainingVaccinations = dosesNeededForHerd - totalDosesGiven | |
const multiplier = remainingVaccinations / dosesGivenSinceOlderDate | |
const daysToHerdImmunity = parseInt((daysSinceOlderDate * multiplier)) | |
var dateForHerdImmunity = new Date() | |
dateForHerdImmunity.setDate(latestDate.getDate() + daysToHerdImmunity) | |
const formattedDateForHerdImmunity = formatter.string(dateForHerdImmunity) | |
// Populate UI | |
const headingW = widget.addText("Vaccinations") | |
headingW.centerAlignText() | |
headingW.font = Font.boldSystemFont(15) | |
headingW.textColor = Color.lightGray() | |
const percentageW = widget.addText(percentageGiven + "%") | |
percentageW.centerAlignText() | |
percentageW.font = Font.systemFont(20) | |
const dateW = widget.addText(formattedLatestDate) | |
dateW.centerAlignText() | |
dateW.font = Font.systemFont(12) | |
dateW.textColor = Color.lightGray() | |
widget.addSpacer(4) | |
const headingTwoW = widget.addText("Herd Date") | |
headingTwoW.centerAlignText() | |
headingTwoW.font = Font.boldSystemFont(15) | |
headingTwoW.textColor = Color.lightGray() | |
const herdDateW = widget.addText(formattedDateForHerdImmunity) | |
herdDateW.centerAlignText() | |
herdDateW.font = Font.systemFont(12) | |
herdDateW.textColor = Color.lightGray() | |
if (config.runsInApp) { | |
widget.presentSmall() | |
} | |
Script.setWidget(widget) | |
Script.complete() | |
function daysBetween(date1, date2) { | |
const firstDateUTC = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate()) | |
const secondDateUTC = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate()) | |
return Math.floor((secondDateUTC - firstDateUTC) / (1000 * 60 * 60 * 24)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment