Skip to content

Commit

Permalink
fix: avoid fetching data multiple times in time series function
Browse files Browse the repository at this point in the history
  • Loading branch information
claustres committed Sep 5, 2024
1 parent 3f11c0a commit 887abe9
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions map/client/utils/utils.time-series.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function fetchDataForSeries({
// Build timeseries to be used in charts for target feature and associated layer definition or probe location
export function getTimeSeries({
feature, location, layer, layers, startTime, endTime, runTime,
level, forecastModel, forecastLevel, probeFunction, weacastApi
level, forecastModel, forecastLevel, probeFunction, weacastApi, fetchDelay
}) {
// A feature comes from a single layer so target variables from it
let variables = _.get(layer, 'variables', [])
Expand All @@ -67,14 +67,19 @@ export function getTimeSeries({
variables = _.uniqBy(variables, 'name')
if (variables.length === 0) return []
const properties = _.get(feature, 'properties', {})
// Fetch data function
const fetch = () => fetchDataForSeries({
// Create promise to fetch data as it will be shared by all series,
// indeed a measure stores all aggregated variables
const data = fetchDataForSeries({
feature, location, layer, startTime, endTime,
level, forecastModel, forecastLevel, probeFunction, weacastApi
})
// Create promise to fetch data as it will be shared by all series,
// indeed a measure stores all aggregated variables
const data = fetch()
// Fetch data function to request data update,
// we use debounce as a measure stores all aggregated variables
// so that when all series are updated at once a single query will be send.
const fetch = _.debounce(() => fetchDataForSeries({
feature, location, layer, startTime, endTime,
level, forecastModel, forecastLevel, probeFunction, weacastApi
}), fetchDelay || 250, { leading: true, trailing: false })

const series = variables.map(variable => {
// Base unit could be either directly the unit or the property of the measure storing the unit
Expand Down

0 comments on commit 887abe9

Please sign in to comment.