Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease calls to filter variants #429

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liwo-static",
"version": "2.52.0",
"version": "3.2.1",
"private": true,
"scripts": {
"serve": "vite",
Expand Down Expand Up @@ -58,4 +58,4 @@
"vite": "^4.4.6",
"vitest": "^0.34.6"
}
}
}
35 changes: 22 additions & 13 deletions src/lib/load-breach.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,30 @@ export async function getScenarioInfo (scenarioIds, featureInfoByScenarioId) {
return data
}

let filterVariantsPromise = null;

async function loadFilterVariants() {
if(!filterVariantsPromise) {
const services = await mapConfig.getServices()
const FILTERS_BASE_URL = services.WEBSERVICE_URL_V2
const FILTERS_API_URL = `${FILTERS_BASE_URL}/filter_variants`

filterVariantsPromise = fetch(FILTERS_API_URL, {
method: 'GET',
mode: 'cors',
credentials: 'omit',
headers
}).then(res => res.json())
}

return filterVariantsPromise
}

async function loadBreachLayer (breachId, layerName) {
// Load the dataset for a breach
const services = await mapConfig.getServices()
const BREACHES_BASE_URL = services.WEBSERVICE_URL
const FILTERS_BASE_URL = services.WEBSERVICE_URL_V2
const BREACHES_API_URL = `${BREACHES_BASE_URL}/Tools/FloodImage.asmx/GetScenariosPerBreachGeneric`
const FILTERS_API_URL = `${FILTERS_BASE_URL}/filter_variants`

const breachFetch = await fetch(BREACHES_API_URL, {
method: 'POST',
Expand All @@ -163,25 +180,17 @@ async function loadBreachLayer (breachId, layerName) {
breachid: breachId,
layername: layerName
})
})

const filterFetch = await fetch(FILTERS_API_URL, {
method: 'GET',
mode: 'cors',
credentials: 'omit',
headers
})
}).then(res => res.json())

const [breachData, filtersData] = await Promise.all([breachFetch, filterFetch])
.then(responses => responses.map(response => response.json()))
const [breachData, filtersData] = await Promise.all([breachFetch, loadFilterVariants()])

const properties = await filtersData

if (properties.length) {
store.commit('setVariantFilterProperties', { properties, breachId })
}

const data = await breachData.then(data => JSON.parse(data.d))
const data = JSON.parse(breachData.d)

// get the first layerset if available, otherwise return null
let result = null
Expand Down
12 changes: 5 additions & 7 deletions src/map.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,22 @@ const tileLayers = [
},
];

var services = null;
var servicePromise = null;

async function getServices() {
// return the content of webconfig.json
// return if we already fetched it (memoize0
if (services) {
return services;
if (servicePromise) {
return servicePromise;
}

let url = 'config/webconfig.json'
if (location.hostname.includes('netlify.app')) {
url = 'config/webconfig-netlify.json'
}

const resp = await fetch(url);
const result = await resp.json();
services = result;
return result;
servicePromise = fetch(url).then(res => res.json());
return servicePromise;
}

export default {
Expand Down
Loading