From 2e269b8ec43c4f155f3d532513de9ef918bd8579 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 22 Nov 2020 00:05:53 +0100 Subject: [PATCH] Improve initial load performance project 2 aufgabe 3 --- projects/project-2/aufgabe3.js | 38 ++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/projects/project-2/aufgabe3.js b/projects/project-2/aufgabe3.js index 897da88..c03f7d0 100644 --- a/projects/project-2/aufgabe3.js +++ b/projects/project-2/aufgabe3.js @@ -1,23 +1,35 @@ const aufgabe3 = async () => { + const years = [2015, 2016, 2017] + const loadData = async () => { - const loadSeries = async (seriesCode, pageSize = 20) => { - const values = (await axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", { - params: { - seriesCode: seriesCode, - pageSize: pageSize + const loadSeries = async seriesCode => { + let maxNumberOfElements = (await axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", { + params: {seriesCode} } - })).data // getting axios data object - return values.data // getting api data object (remove paging information) - .filter(elem => elem.timePeriodStart >= 2015 && elem.timePeriodStart <= 2017) - .filter(elem => elem.dimensions.Location === "ALLAREA") + )).data.totalElements + + let requests = [] + for (let year of years) { + requests.push(axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", { + params: { + seriesCode: seriesCode, + pageSize: maxNumberOfElements, + timePeriod: year + } + })) + } + + let dataArrays = (await Promise.all(requests)).map(arr => arr.data.data) + const values = [].concat(...dataArrays) + return values.filter(elem => elem.dimensions.Location === "ALLAREA") } const apiData = await Promise.all([ - loadSeries("SH_H2O_SAFE", 5000), - loadSeries("SH_SAN_SAFE", 5000) + loadSeries("SH_H2O_SAFE"), + loadSeries("SH_SAN_SAFE") ]) - return [...apiData[0], ...apiData[1]] + return [].concat(...apiData) } const levels = [0, 50, 80, 85, 90, 95, 99, 100] @@ -51,7 +63,7 @@ const aufgabe3 = async () => { minZoom: 1, maxZoom: 18 })) - let [geojsonData, apiData] = await Promise.all([ + const [geojsonData, apiData] = await Promise.all([ $.getJSON('europe.geo.json'), loadData() ])