From f5dc5d61488a56296c30ee63c89b79dba9cddca9 Mon Sep 17 00:00:00 2001 From: Clemens Zagler Date: Mon, 27 Mar 2023 11:10:17 +0200 Subject: [PATCH] issue noi-techpark/webcomp-mobility-echarging-dashboard/#63: Also consider stations that don't have any measurement data. WIP --- docker-compose.yml | 2 +- src/api/mobility.js | 41 +++++++++++++++++++++++++---------- src/components/details_box.js | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index f7a0e89..8e9c0a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: context: . dockerfile: infrastructure/docker/node.dockerfile volumes: - - ./:/code:Z + - ./:/code working_dir: /code command: sh -c "test -d node_modules || npm install; npm run start" ports: diff --git a/src/api/mobility.js b/src/api/mobility.js index 3bd2726..08b2367 100644 --- a/src/api/mobility.js +++ b/src/api/mobility.js @@ -5,7 +5,7 @@ const NINJA_BASE_PATH = 'https://mobility.api.opendatahub.bz.it/v2'; export async function request__access_types() { const request = await fetch( - `${NINJA_BASE_PATH}/flat/EChargingStation/*/latest?limit=-1&offset=0&select=smetadata.accessType&where=sactive.eq.true&shownull=false&distinct=true&origin=${fetch_origin}` + `${NINJA_BASE_PATH}/flat/EChargingStation?limit=-1&offset=0&select=smetadata.accessType&where=sactive.eq.true&shownull=false&distinct=true&origin=${fetch_origin}` ); const response = await request.json(); this.access_types = response.data.map((o, i) => [ @@ -33,30 +33,47 @@ export async function request__plug_types() { } } +// merge lists of stations by scode +function merge_by_scode(...lists_to_merge) { + const ret = {}; + lists_to_merge.flat().forEach(e => ret[e.scode] = {...ret[e.scode], ...e}); + return Object.values(ret); +} + export async function request__stations_plugs(station_id) { try { - const request = await fetch( - `${NINJA_BASE_PATH}/flat/EChargingPlug/*/latest?limit=-1&offset=0&where=sactive.eq.true,pcode.eq.${station_id}&shownull=false&origin=${fetch_origin}` - ); - const response = await request.json(); - return response.data; + // some stations don't have data and therefore don't show up in the level 4 ninja API call + // So we have to do two separate requests and merge the data + const all_plugs = await (await fetch( + `${NINJA_BASE_PATH}/flat/EChargingPlug?limit=-1&offset=0&where=sactive.eq.true,pcode.eq.${station_id}&shownull=false&origin=${fetch_origin}` + )).json(); + const plugs_with_data = await (await fetch( + `${NINJA_BASE_PATH}/flat/EChargingPlug/echarging-plug-status/latest?select=mvalue,scode&limit=-1&offset=0&where=sactive.eq.true,pcode.eq.${station_id}&shownull=false&origin=${fetch_origin}` + )).json(); + + return merge_by_scode(all_plugs.data, plugs_with_data.data); } catch (e) { return undefined; } } - /** * STATIONS */ export async function request__get_stations_details() { this.is_loading = true; - const request = await fetch( - `${NINJA_BASE_PATH}/flat/EChargingStation/*/latest?limit=-1&offset=0&where=sactive.eq.true&shownull=false&distinct=true&origin=${fetch_origin}`, + // some stations don't have data and therefore don't show up in the level 4 ninja API call + // So we have to do two separate requests and merge the data + const all_stations = await(await fetch( + `${NINJA_BASE_PATH}/flat/EChargingStation?limit=-1&offset=0&where=sactive.eq.true&shownull=false&distinct=true&origin=${fetch_origin}`, fetch_options - ); - const response = await request.json(); - this.all_stations_details = response.data; + )).json(); + const stations_with_data = await(await fetch( + `${NINJA_BASE_PATH}/flat/EChargingStation/number-available/latest?select=mvalue,scode&limit=-1&offset=0&where=sactive.eq.true&shownull=false&distinct=true&origin=${fetch_origin}`, + fetch_options + )).json(); + + this.all_stations_details = merge_by_scode(all_stations.data, stations_with_data.data); this.is_loading = false; } diff --git a/src/components/details_box.js b/src/components/details_box.js index d21f5ad..9bc9f2e 100644 --- a/src/components/details_box.js +++ b/src/components/details_box.js @@ -27,7 +27,7 @@ export function render__details_box() { const { sorigin } = this.current_station; // converts unicode characters to strings like \u00df for ß in "Straße" - const address = smetadata != undefined ? smetadata.address.replace(/\\u[\dA-F]{4}/gi, + const address = smetadata != undefined && smetadata.address != undefined ? smetadata.address.replace(/\\u[\dA-F]{4}/gi, function (match) { return String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)); }) : null;