Improve initial load performance project 2 aufgabe 3

This commit is contained in:
Marcel Schwarz 2020-11-22 00:05:53 +01:00
parent 617ab4819b
commit 2e269b8ec4

View File

@ -1,23 +1,35 @@
const aufgabe3 = async () => { const aufgabe3 = async () => {
const years = [2015, 2016, 2017]
const loadData = async () => { const loadData = async () => {
const loadSeries = async (seriesCode, pageSize = 20) => { const loadSeries = async seriesCode => {
const values = (await axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", { let maxNumberOfElements = (await axios.get("https://unstats.un.org/SDGAPI/v1/sdg/Series/Data", {
params: { params: {seriesCode}
seriesCode: seriesCode,
pageSize: pageSize
} }
})).data // getting axios data object )).data.totalElements
return values.data // getting api data object (remove paging information)
.filter(elem => elem.timePeriodStart >= 2015 && elem.timePeriodStart <= 2017) let requests = []
.filter(elem => elem.dimensions.Location === "ALLAREA") 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([ const apiData = await Promise.all([
loadSeries("SH_H2O_SAFE", 5000), loadSeries("SH_H2O_SAFE"),
loadSeries("SH_SAN_SAFE", 5000) loadSeries("SH_SAN_SAFE")
]) ])
return [...apiData[0], ...apiData[1]] return [].concat(...apiData)
} }
const levels = [0, 50, 80, 85, 90, 95, 99, 100] const levels = [0, 50, 80, 85, 90, 95, 99, 100]
@ -51,7 +63,7 @@ const aufgabe3 = async () => {
minZoom: 1, minZoom: 1,
maxZoom: 18 maxZoom: 18
})) }))
let [geojsonData, apiData] = await Promise.all([ const [geojsonData, apiData] = await Promise.all([
$.getJSON('europe.geo.json'), $.getJSON('europe.geo.json'),
loadData() loadData()
]) ])