Last active
March 2, 2021 08:51
-
-
Save davidbauer/3517e2389024b85c29503554a3d8ae6f to your computer and use it in GitHub Desktop.
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
// If you're looking at this, you probably know more about code than I do. | |
// I apologize for the messy code. I'm a journalist, not a developer. And this is an experiment, not a polished piece of software. | |
// If you'd like to make me aware of problems with the code, you can write me to [email protected]. | |
// countries missing in OWID dataset (dec 29): KIR, PRK, FSM, NRU, PLW, TON, TKM, TUV | |
var datafound = []; | |
var datamissing = []; | |
var matched = []; | |
var myObj; // where we save all imported data | |
var herdImmu = 85; // percent of population we assume for herd immunity | |
var aggregates = []; | |
var importedCountries = []; | |
// TODO: automatically get this list from https://github.com/owid/covid-19-data/blob/master/scripts/scripts/vaccinations/automations/automation_state.csv | |
// currently does weird things when we run getStartedCountries. builds the array, but matching via .includes() fails | |
var startedCountries = ['Argentina','Australia','Austria','Belgium','Brazil','Bulgaria','Canada','Cayman Islands','Chile','Czechia','Denmark','Ecuador','England','Estonia','Faeroe Islands','Finland','France','Germany','Greece','Iceland','India','Indonesia','Ireland','Isle of Man','Israel','Italy','Japan','Jersey','Latvia','Lebanon','Liechtenstein','Lithuania','Luxembourg','Maldives','Malta','Montenegro','Morocco','Netherlands','Northern Ireland','Norway','Peru','Poland','Portugal','Romania','San Marino','Saudi Arabia','Scotland','Slovakia','Slovenia','South Korea','Spain','Sweden','Switzerland','Turkey','Ukraine','United Arab Emirates','United Kingdom','United States','Wales','Albania','Algeria','Andorra','Anguilla','Azerbaijan','Bahrain','Bangladesh','Barbados','Belarus','Bermuda','Bolivia','Cambodia','China','Colombia','Costa Rica','Croatia','Cyprus','Dominican Republic','Egypt','El Salvador','Falkland Islands','Gibraltar','Greenland','Guatemala','Guernsey','Guyana','Hong Kong','Hungary','Iran','Jordan','Kazakhstan','Kuwait','Macao','Malaysia','Mauritius','Mexico','Monaco','Myanmar','Nepal','New Zealand','Northern Cyprus','Oman','Pakistan','Panama','Paraguay','Qatar','Russia','Saint Helena','Senegal','Serbia','Seychelles','Singapore','South Africa','Sri Lanka','Trinidad and Tobago','Turks and Caicos Islands','Venezuela','Zimbabwe'] | |
// IMPORT DATA FROM OWID | |
var xmlhttp = new XMLHttpRequest(); | |
xmlhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
console.log("Importing data from Our World In Data. Support their work: https://ourworldindata.org/donate."); | |
console.log("========================================="); | |
updatestamp(); | |
myObj = JSON.parse(this.responseText); | |
getStartedCountries(function() {processData(myObj)}); | |
//processData(myObj); | |
// run QA | |
qualitycheck(); | |
} | |
}; | |
xmlhttp.open("GET", "https://covid.ourworldindata.org/data/latest/owid-covid-latest.json", true); | |
//xmlhttp.open("GET", "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.json", true); // backup | |
xmlhttp.send(); | |
//FUNCTIONS | |
processData = function(d) { | |
for (country in myObj) { | |
// get the data we need | |
iso = `${country}`; | |
countryname = myObj[country].location; | |
//continent = myObj[country].continent; | |
doses_pH = myObj[country].total_vaccinations_per_hundred; | |
vaccinated_pH = myObj[country].people_vaccinated_per_hundred; | |
fullyvaccinated_pH = myObj[country].people_fully_vaccinated_per_hundred; | |
vaxratio = undefined; | |
// filter out aggregates like world, EU, continents | |
if (iso.substring(0,4) == "OWID") { | |
if (iso == "OWID_WRL") { | |
$('.dosesworldwide').text(doses_pH); | |
$('.fullyvaxworldwide').text(fullyvaccinated_pH); | |
} | |
aggregates.push(countryname + "(" + iso + "): " + doses_pH + " doses per hundred inhabitants. | " + fullyvaccinated_pH + "% fully vaccinated."); | |
} | |
// proceed with countries only | |
else { | |
// check if data is available or if country is on list of those who started vaccinating (some might not have recent data) | |
if (startedCountries.includes(countryname) || doses_pH != undefined ) { | |
matched.push(countryname + "(" + iso + ")"); | |
// check if country has optimal data (number of people vaccinated) | |
if (vaccinated_pH != null && fullyvaccinated_pH != null) { | |
vaxratio = (vaccinated_pH - fullyvaccinated_pH)/2 + fullyvaccinated_pH; //vaccinated but not fully vaccinated people count half | |
vaxratio = Math.round((vaxratio + Number.EPSILON) * 100) / 100; // round to 2 decimals max | |
saturate(iso,vaxratio); | |
datafound.push(countryname + "(" + iso + "): " + doses_pH + " doses per hundred inhabitants. | " + fullyvaccinated_pH + "% fully vaccinated."); | |
} | |
// check if country has recent data on doses administered (fallback) | |
else if (doses_pH != undefined) { | |
saturate(iso,doses_pH/2); | |
datafound.push(countryname + "(" + iso + "): " + doses_pH + " doses per hundred inhabitants."); | |
} | |
// country has started vaccinating, but has no recent data | |
else { | |
} | |
label(iso, countryname, doses_pH, fullyvaccinated_pH); | |
markStarted(iso); | |
} | |
// country has no data and hasn't started, so save it to array of missing | |
else { | |
datamissing.push(countryname + "(" + iso + ")"); | |
label(iso, countryname, doses_pH, fullyvaccinated_pH); | |
} | |
} | |
} | |
} | |
saturate = function(c,proxy) { // c = identifier for country, proxy = best available proxy for herd immunity | |
var flagobj = "." + c + ""; | |
var pct = proxy/herdImmu*100; // mapping value to 0 to 100% scale | |
var saturation = "saturate(" + pct + "%)"; | |
$(flagobj).css({'filter': saturation}); | |
} | |
label = function(c,n,v,fullyvax) { // c = identifier for country, n = country name, v = value, cont = continent, fullyvax = %fully vaccinated | |
var flagobj = "." + c + ""; | |
if(v != undefined) { | |
var alttext = n + ": " + v + " doses per 100 people."; | |
$(flagobj).addClass('started'); | |
if (fullyvax != null) {alttext = alttext + " | " + fullyvaccinated_pH + "% fully vaccinated."} | |
} | |
else { | |
var alttext = n + ": no vaccinations yet or no data available"; | |
} | |
$(flagobj).attr("alt",alttext); | |
$(flagobj).attr("title",alttext); | |
} | |
markStarted = function(c) { | |
var flagobj = "." + c + ""; | |
$(flagobj).addClass('started'); | |
} | |
updatestamp = function() { | |
$.ajax({ | |
type: "GET", | |
url: "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data-last-updated-timestamp.txt", | |
dataType: "text", | |
success: function(data) { | |
console.log("last updated: " + data); | |
d = new Date(data); | |
$('.updatetime').text(d.toLocaleDateString('en-GB')); | |
} | |
}); | |
} | |
getStartedCountries = function(callback) { | |
$.ajax({ | |
type: "GET", | |
url: "https://raw.githubusercontent.com/owid/covid-19-data/master/scripts/scripts/vaccinations/automations/automation_state.csv", | |
dataType: "text", | |
success: function(data) { | |
CSVToArray(data); | |
} | |
}); | |
callback(); | |
} | |
//QA | |
qualitycheck = function() { | |
md = matched.length; | |
console.log("Status matched for " + md + " countries"); | |
fd = datafound.length | |
console.log("Data found for " + fd + " countries"); | |
console.log(datafound); | |
nf = datamissing.length | |
console.log("No data for " + nf + " countries"); | |
console.log(datamissing); | |
} | |
//CLICK FUNCTIONS | |
showstartedones = function() { | |
$('.flag img').not('.started').addClass('hide'); | |
$('.navbtn').toggleClass('selected'); | |
} | |
showall = function() { | |
$('.hide').removeClass('hide'); | |
$('.navbtn').toggleClass('selected') | |
} | |
makeevthok = function() { | |
$('.makeok').toggleClass("isok"); | |
$('.flag img').toggleClass('evthok'); | |
$('.makeok').text(function(i, t){ | |
return t === "Make everything okay" ? "Back to reality" : "Make everything okay"; | |
}) | |
} | |
viewswitch = function() { | |
$('.view').toggleClass('hidden'); | |
$('.viewswitch').text(function(i, t){ | |
return t === "Group by continent" ? "Sort A-Z" : "Group by continent"; | |
}) | |
} | |
setHerdImmu = function (v) { | |
herdImmu = v; | |
processData(myObj); | |
console.log("Herd immunity threshold set to " + v + "%."); | |
} | |
//csv | |
function CSVToArray(input){ | |
var lines = input.split("\n"); | |
for (var i = 1; i < lines.length-1; i++) { | |
var _firstColumn = lines[i].split(",")[0]; | |
importedCountries.push(_firstColumn); | |
} | |
} | |
// it's the end of the code as we know it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment