Skip to content

Commit 2bd6f52

Browse files
committed
Support groups in addition to subdivisions
1 parent 368dce1 commit 2bd6f52

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

app.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ async function fetchPopulationData() {
196196
populationData = await res.json();
197197

198198
for (let element of document.getElementsByClassName("country-item")) {
199-
const population = Object.values(populationData.countries[element.dataset.code].subdivisions).reduce((a, b) => a + b, 0);
199+
const population = Object.values(regions(populationData.countries[element.dataset.code])).reduce((a, b) => a + b, 0);
200200
registerTooptip(element, `<span class="tooltip-title">${(population / 1e6).toFixed(1)} ${i18n.mioResidents}</span>\n`);
201201
}
202202

@@ -209,7 +209,6 @@ async function fetchCountryData(year, countryCode) {
209209
fetch(`${API_BASE}/PublicHolidays?countryIsoCode=${countryCode}&validFrom=${year}-01-01&validTo=${year}-12-31&languageIsoCode=${locale.toUpperCase()}`),
210210
fetch(`${API_BASE}/SchoolHolidays?countryIsoCode=${countryCode}&validFrom=${year}-01-01&validTo=${year}-12-31&languageIsoCode=${locale.toUpperCase()}`)
211211
];
212-
213212
const responses = await Promise.all(requests);
214213
if (responses.some(r => !r.ok)) {
215214
throw new Error(`Error loading data of ${countryCode} for ${year}`);
@@ -222,8 +221,17 @@ async function fetchCountryData(year, countryCode) {
222221
}
223222

224223
async function fetchRegionData(countryCode) {
225-
const RegionRes = await fetch(`${API_BASE}/Subdivisions?countryIsoCode=${countryCode}&languageIsoCode=${locale.toUpperCase()}`);
226-
cachedData.Regions[countryCode] = await RegionRes.json();
224+
const requests = [
225+
fetch(`${API_BASE}/Subdivisions?countryIsoCode=${countryCode}&languageIsoCode=${locale.toUpperCase()}`),
226+
fetch(`${API_BASE}/Groups?countryIsoCode=${countryCode}&languageIsoCode=${locale.toUpperCase()}`)
227+
];
228+
const responses = await Promise.all(requests);
229+
if (responses.some(r => !r.ok)) {
230+
throw new Error(`Error loading region data of ${countryCode}`);
231+
}
232+
const [subdivision, groups] = await Promise.all(responses.map(r => r.json()));
233+
234+
cachedData.Regions[countryCode] = [...subdivision, ...groups];
227235
}
228236

229237

@@ -258,7 +266,7 @@ function calculateDayStatistics(fromDate, toDate) {
258266
stats = {};
259267
const missingRegions = new Set();
260268
const totalPop = selectedCountries.reduce((sum, c) => {
261-
const subs = populationData.countries[c].subdivisions;
269+
const subs = regions(populationData.countries[c]);
262270
return sum + Object.values(subs).reduce((a, b) => a + b, 0);
263271
}, 0);
264272

@@ -296,7 +304,7 @@ function calculateDayStatistics(fromDate, toDate) {
296304
if (schoolHolidays.length === 0 || maxDate(schoolHolidays.map(f => f.endDate)) < d) incompleteData.add(country);
297305

298306
// Count population on holiday
299-
const countryPop = populationData.countries[country].subdivisions;
307+
const population = regions(populationData.countries[country]);
300308
let holidayRegions = new Set();
301309
let nationwideHoliday = false;
302310
let nationwideSchoolHoliday = false;
@@ -305,7 +313,7 @@ function calculateDayStatistics(fromDate, toDate) {
305313
for (const r of relevant) {
306314
const label = r.name?.[0]?.text || "Unbenannt";
307315
if (!(label in infos)) {
308-
infos[label] = {Type: r.type, Subdivisions: new Set(), All: false}
316+
infos[label] = {Type: r.type, Regions: new Set(), All: false}
309317
}
310318

311319
if (r.nationwide) {
@@ -316,25 +324,22 @@ function calculateDayStatistics(fromDate, toDate) {
316324
}
317325
infos[label].All = true;
318326

319-
} else if (r.subdivisions) {
320-
const regions = r.subdivisions.map(s => s.code.split("-").slice(0, 2).join("-"))
321-
regions.forEach(code => {
322-
infos[label].Subdivisions.add(code);
323-
if (countryPop[code]) {
324-
holidayRegions.add(code)
327+
} else if (regions(r)) {
328+
regions(r).map(s => s.code.split("-").slice(0, 2).join("-")).forEach(region => {
329+
if (population[region]) {
330+
infos[label].Regions.add(region);
331+
holidayRegions.add(region)
325332
} else {
326-
missingRegions.add(code)
333+
missingRegions.add(region)
327334
}
328335

329336
});
330-
if (regions.every(r => !countryPop[r])) incompleteData.add(country);
331337

332338
}
333339
}
334340

335-
336-
const countryPopTotal = Object.values(countryPop).reduce((a, b) => a + b, 0);
337-
const holidayPopulation = (nationwideHoliday || nationwideSchoolHoliday) ? countryPopTotal : [...holidayRegions].map(c => countryPop[c]).reduce((a, b) => a + b, 0);
341+
const countryPopTotal = Object.values(population).reduce((a, b) => a + b, 0);
342+
const holidayPopulation = (nationwideHoliday || nationwideSchoolHoliday) ? countryPopTotal : [...holidayRegions].map(c => population[c]).reduce((a, b) => a + b, 0);
338343
holidayPopulationTotal += holidayPopulation;
339344
nationwideHolidayAnyCountry |= nationwideHoliday;
340345

@@ -345,9 +350,9 @@ function calculateDayStatistics(fromDate, toDate) {
345350
tooltip.push(`\n<span class="tooltip-country">${c.name}: ${(holidayPopulation / 1e6).toFixed(1)} ${i18n.mioResidents} (${(100 * holidayPopulation / countryPopTotal).toFixed(0)}%)</span>`);
346351
}
347352
for (const [label, info] of Object.entries(infos)) {
348-
if (info.All || info.Subdivisions.size > 0) {
349-
//const divisionsText = [...info.Subdivisions].map((s) => s.split("-")[1]).toSorted().join(", ");
350-
const divisionsText = i18n.in + " " + [...info.Subdivisions].map((s) => regionNames[s] || s).toSorted().join(", ");
353+
if (info.All || info.Regions.size > 0) {
354+
//const divisionsText = [...info.Regions].map((s) => s.split("-")[1]).toSorted().join(", ");
355+
const divisionsText = i18n.in + " " + [...info.Regions].map((s) => regionNames[s] || s).toSorted().join(", ");
351356
tooltip.push(`${label} <span class="tooltip-info">(${info.Type} ${info.All ? i18n.nationwide : divisionsText})</span>`)
352357
}
353358
}
@@ -465,6 +470,15 @@ function maxDate(dates){
465470
return new Date(Math.max(...dates.map(s => new Date(s).getTime())));
466471
}
467472

473+
function regions(data){
474+
if (data.subdivisions === undefined) return data.groups
475+
if (data.groups === undefined) return data.subdivisions
476+
if (Array.isArray(data.subdivisions) && Array.isArray(data.groups)) {
477+
return [...data.subdivisions, ...data.groups];
478+
}
479+
return { ...data.subdivisions, ...data.groups };
480+
}
481+
468482
function inDateRange(date, startDate, endDate, orAdjacentWeekend = false) {
469483
const start = new Date(startDate);
470484
const end = new Date(endDate);

population.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"BE": {
100100
"name": "Belgien",
101101
"note": "Various sources",
102-
"subdivisions": {
102+
"groups": {
103103
"BE-DE": 79479,
104104
"BE-FR": 4200000,
105105
"BE-NL": 6509894
@@ -110,7 +110,7 @@
110110
"source": "City population",
111111
"url": "https://www.citypopulation.de/de/netherlands/admin/",
112112
"date": "2025-01-01",
113-
"subdivisions": {
113+
"groups": {
114114
"NL-ZU": 5170150,
115115
"NL-MI": 6309099,
116116
"NL-NO": 6416165
@@ -139,4 +139,4 @@
139139
}
140140
}
141141
}
142-
}
142+
}

0 commit comments

Comments
 (0)