Guest User

Untitled

a guest
Nov 26th, 2021
33
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fetch from 'node-fetch';
  2.  
  3. const ageMap = {
  4.   "10000": "all",
  5.   "41700": "0-65",
  6.   "53950": "65-80",
  7.   "21700": "80+"
  8. };
  9.  
  10. const sexMap = {
  11.   "1100": "all"
  12. };
  13.  
  14. const residents = {
  15.   "2015": (16992010 + 17072044) / 2,
  16.   "2016": (16992010 + 17072044) / 2,
  17.   "2017": (17091456 + 17172903) / 2,
  18.   "2018": (17192306 + 17271231) / 2,
  19.   "2019": (17299343 + 17399364) / 2,
  20.   "2020": (17422992 + 17469635) / 2,
  21.   "2021": (17422992 + 17469635) / 2
  22. };
  23.  
  24. function range(start, stop, step) {
  25.   var a = [start], b = start;
  26.   while (b < stop) {
  27.     a.push(b += step || 1);
  28.   }
  29.   return a;
  30. }
  31.  
  32. const selectSex = (sex = 1100) => `(Geslacht eq '${sex}')`;
  33. const selectAge = (age = 10000) => `(LeeftijdOp31December eq '${age}')`;
  34. const selectWeek = (year, week) => `(Perioden eq '${year}W${100 + week}')`;
  35. const selectYear = (year, startWeek = 2, endWeek = 51) => range(startWeek, endWeek).map((week) => selectWeek(year, week));
  36.  
  37. function buildUrl(year, startWeek = 2, endWeek = 51) {
  38.   const filterSexes = [1100].map(selectSex);
  39.   const filterAges = [10000, 41700, 53950, 21700].map(selectAge);
  40.   const filterPeriods = selectYear(year, startWeek, endWeek);
  41.   const select = `Geslacht, LeeftijdOp31December, Perioden, Overledenen_1`;
  42.   const filter = `(${filterSexes.join(' or ')}) and (${filterAges.join(' or ')}) and (${filterPeriods.join(' or ')})`;
  43.   return `https://opendata.cbs.nl/ODataApi/odata/70895ned/UntypedDataSet?$filter=${filter}&$select=${select}`
  44. }
  45.  
  46. async function main(format = "text", weeka = 29, weekb = 39) {
  47.   const data = [];
  48.  
  49.   const getYearTasks = range(2015, 2021).map((year) => ({year, get: new Promise((resolve, reject) => {
  50.     (async () => {
  51.       try {
  52.         const response = await fetch(buildUrl(year, Number(weeka), Number(weekb)));
  53.         const json = await response.json();
  54.         resolve(json);
  55.       } catch(e) {
  56.         reject(e);
  57.       }
  58.     })();
  59.   })}));
  60.  
  61.   for(const getYearTask of getYearTasks) {
  62.     const deathsWeekMap = {};
  63.     const yearData = await getYearTask.get;
  64.     const yearValues = yearData.value;
  65.  
  66.     for(const value of yearValues) {
  67.       const sex = sexMap[value['Geslacht']];
  68.       const age = ageMap[value['LeeftijdOp31December']];
  69.       const year = Number(value['Perioden'].substring(0, 4));
  70.       const week = Number(value['Perioden'].substring(6, 8));
  71.       const deaths = Number(value['Overledenen_1']);
  72.       if(!deathsWeekMap[week]) {
  73.         deathsWeekMap[week] = {};
  74.       }
  75.       deathsWeekMap[week]['Week'] = `${year} week ${week}`;
  76.       deathsWeekMap[week][age] = deaths;
  77.     }
  78.  
  79.     data.push(Object.values(deathsWeekMap));
  80.   }
  81.  
  82.   const [during_2021, during_2020, ...before_2020_rest] = data.reverse();
  83.  
  84.   const output = {};
  85.  
  86.   if(format === "json") {
  87.     output['week'] = weeka;
  88.     output['week_to'] = weekb;
  89.   }
  90.  
  91.   {
  92.     const age = "all";
  93.  
  94.     if(format === "text") {
  95.       console.log("all:");
  96.     }
  97.  
  98.     const during_2020_total = during_2020.reduce((r, week) => week['all'] + r, 0);
  99.     const during_2021_total = during_2021.reduce((r, week) => week['all'] + r, 0);
  100.     const before_2020_average = before_2020_rest.reduce((r, year) => year.reduce((r, week) => week['all'] + r, 0) + r, 0) / before_2020_rest.length;
  101.  
  102.     if(format === "json") {
  103.       output[age] = {};
  104.       output[age][`during_2020_total`] = during_2020_total;
  105.       output[age][`during_2021_total`] = during_2021_total;
  106.       output[age][`before_2020_average`] = before_2020_average;
  107.       output[age][`difference_2020`] = during_2020_total - before_2020_average;
  108.       output[age][`difference_2021`] = during_2021_total - before_2020_average;
  109.     }
  110.  
  111.     if(format === "text") {
  112.       console.log(`deaths during 2020 week ${weeka}-${weekb}:\n`, during_2020_total);
  113.       console.log(`deaths during 2021 week ${weeka}-${weekb}:\n`, during_2021_total);
  114.       console.log(`average between 2015 until 2019 week ${weeka}-${weekb}:\n`, before_2020_average);
  115.       console.log(`difference before 2020 vs during 2020 week ${weeka}-${weekb}:\n`, during_2020_total - before_2020_average);
  116.       console.log(`difference before 2020 vs during 2021 week ${weeka}-${weekb}:\n`, during_2021_total - before_2020_average);
  117.     }
  118.   }
  119.   if(format === "text") console.log();
  120.   {
  121.     const age = "0_65";
  122.  
  123.     if(format === "text") {
  124.       console.log("0-65:");
  125.     }
  126.  
  127.     const during_2020_total = during_2020.reduce((r, week) => week['0-65'] + r, 0);
  128.     const during_2021_total = during_2021.reduce((r, week) => week['0-65'] + r, 0);
  129.     const before_2020_average = before_2020_rest.reduce((r, year) => year.reduce((r, week) => week['0-65'] + r, 0) + r, 0) / before_2020_rest.length;
  130.  
  131.     if(format === "json") {
  132.       output[age] = {};
  133.       output[age][`during_2020_total`] = during_2020_total;
  134.       output[age][`during_2021_total`] = during_2021_total;
  135.       output[age][`before_2020_average`] = before_2020_average;
  136.       output[age][`difference_2020`] = during_2020_total - before_2020_average;
  137.       output[age][`difference_2021`] = during_2021_total - before_2020_average;
  138.     }
  139.  
  140.     if(format === "text") {
  141.       console.log(`deaths during 2020 week ${weeka}-${weekb}:\n`, during_2020_total);
  142.       console.log(`deaths during 2021 week ${weeka}-${weekb}:\n`, during_2021_total);
  143.       console.log(`average between 2015 until 2019 week ${weeka}-${weekb}:\n`, before_2020_average);
  144.       console.log(`difference before 2020 vs during 2020 week ${weeka}-${weekb}:\n`, during_2020_total - before_2020_average);
  145.       console.log(`difference before 2020 vs during 2021 week ${weeka}-${weekb}:\n`, during_2021_total - before_2020_average);
  146.     }
  147.   }
  148.   if(format === "text") console.log();
  149.   {
  150.     const age = "65_80";
  151.  
  152.     if(format === "text") {
  153.       console.log("65-80:");
  154.     }
  155.  
  156.     const during_2020_total = during_2020.reduce((r, week) => week['65-80'] + r, 0);
  157.     const during_2021_total = during_2021.reduce((r, week) => week['65-80'] + r, 0);
  158.     const before_2020_average = before_2020_rest.reduce((r, year) => year.reduce((r, week) => week['65-80'] + r, 0) + r, 0) / before_2020_rest.length;
  159.  
  160.     if(format === "json") {
  161.       output[age] = {};
  162.       output[age][`during_2020_total`] = during_2020_total;
  163.       output[age][`during_2021_total`] = during_2021_total;
  164.       output[age][`before_2020_average`] = before_2020_average;
  165.       output[age][`difference_2020`] = during_2020_total - before_2020_average;
  166.       output[age][`difference_2021`] = during_2021_total - before_2020_average;
  167.     }
  168.  
  169.     if(format === "text") {
  170.       console.log(`deaths during 2020 week ${weeka}-${weekb}:\n`, during_2020_total);
  171.       console.log(`deaths during 2021 week ${weeka}-${weekb}:\n`, during_2021_total);
  172.       console.log(`average between 2015 until 2019 week ${weeka}-${weekb}:\n`, before_2020_average);
  173.       console.log(`difference before 2020 vs during 2020 week ${weeka}-${weekb}:\n`, during_2020_total - before_2020_average);
  174.       console.log(`difference before 2020 vs during 2021 week ${weeka}-${weekb}:\n`, during_2021_total - before_2020_average);
  175.     }
  176.   }
  177.   if(format === "text") console.log();
  178.   {
  179.     const age = "80_plus";
  180.  
  181.     if(format === "text") {
  182.       console.log("80+:");
  183.     }
  184.  
  185.     const during_2020_total = during_2020.reduce((r, week) => week['80+'] + r, 0);
  186.     const during_2021_total = during_2021.reduce((r, week) => week['80+'] + r, 0);
  187.     const before_2020_average = before_2020_rest.reduce((r, year) => year.reduce((r, week) => week['80+'] + r, 0) + r, 0) / before_2020_rest.length;
  188.  
  189.     if(format === "json") {
  190.       output[age] = {};
  191.       output[age][`during_2020_total`] = during_2020_total;
  192.       output[age][`during_2021_total`] = during_2021_total;
  193.       output[age][`before_2020_average`] = before_2020_average;
  194.       output[age][`difference_2020`] = during_2020_total - before_2020_average;
  195.       output[age][`difference_2021`] = during_2021_total - before_2020_average;
  196.     }
  197.  
  198.     if(format === "text") {
  199.       console.log(`deaths during 2020 week ${weeka}-${weekb}:\n`, during_2020_total);
  200.       console.log(`deaths during 2021 week ${weeka}-${weekb}:\n`, during_2021_total);
  201.       console.log(`average between 2015 until 2019 week ${weeka}-${weekb}:\n`, before_2020_average);
  202.       console.log(`difference before 2020 vs during 2020 week ${weeka}-${weekb}:\n`, during_2020_total - before_2020_average);
  203.       console.log(`difference before 2020 vs during 2021 week ${weeka}-${weekb}:\n`, during_2021_total - before_2020_average);
  204.     }
  205.   }
  206.  
  207.   if(format === "json") console.log(JSON.stringify(output, null, 2), ',');
  208. }
  209.  
  210. const format = String(process.argv[2]);
  211. const weeka = Number(process.argv[3]), weekb = Number(process.argv[4]);
  212.  
  213. main(format, weeka, weekb).catch(x => console.error(x));
RAW Paste Data