fix(map): read all relevant selection sources
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { settleWithConcurrency } from './useMapThemeSelectionInsights'
|
||||
|
||||
describe('settleWithConcurrency', () => {
|
||||
it('keeps result order and never exceeds the acquisition limit', async () => {
|
||||
let active = 0
|
||||
let maximumActive = 0
|
||||
|
||||
const results = await settleWithConcurrency([0, 1, 2, 3, 4, 5], 3, async (value) => {
|
||||
active += 1
|
||||
maximumActive = Math.max(maximumActive, active)
|
||||
await new Promise((resolve) => setTimeout(resolve, (5 - value) * 2))
|
||||
active -= 1
|
||||
return value * 10
|
||||
})
|
||||
|
||||
expect(maximumActive).toBe(3)
|
||||
expect(results).toEqual([
|
||||
{ status: 'fulfilled', value: 0 },
|
||||
{ status: 'fulfilled', value: 10 },
|
||||
{ status: 'fulfilled', value: 20 },
|
||||
{ status: 'fulfilled', value: 30 },
|
||||
{ status: 'fulfilled', value: 40 },
|
||||
{ status: 'fulfilled', value: 50 },
|
||||
])
|
||||
})
|
||||
|
||||
it('retains individual acquisition failures without stopping the queue', async () => {
|
||||
const results = await settleWithConcurrency(['ok', 'fail', 'later'], 2, async (value) => {
|
||||
if (value === 'fail') {
|
||||
throw new Error('provider unavailable')
|
||||
}
|
||||
return value.toUpperCase()
|
||||
})
|
||||
|
||||
expect(results[0]).toEqual({ status: 'fulfilled', value: 'OK' })
|
||||
expect(results[1].status).toBe('rejected')
|
||||
expect(results[2]).toEqual({ status: 'fulfilled', value: 'LATER' })
|
||||
})
|
||||
})
|
||||
@@ -7,7 +7,13 @@ import { floodHazardSelectionToMapSelection } from '../lib/floodHazardSelection'
|
||||
import { thematicRasterSelectionToMapSelection } from '../lib/thematicRaster'
|
||||
import { bathymetryRasterSelectionToMapSelection } from '../lib/bathymetryRaster'
|
||||
|
||||
export type MapThemeAcquisitionKind = 'thematic_raster' | 'dhmv' | 'flood_hazard' | 'grb' | 'official_vector'
|
||||
export type MapThemeAcquisitionKind =
|
||||
| 'thematic_raster'
|
||||
| 'dhmv'
|
||||
| 'flood_hazard'
|
||||
| 'grb'
|
||||
| 'official_vector'
|
||||
| 'bathymetry_profiles'
|
||||
|
||||
export interface MapThemeAcquisition {
|
||||
kind: MapThemeAcquisitionKind
|
||||
@@ -30,6 +36,31 @@ export interface MapThemeInsight<TThemeId extends string> {
|
||||
result: VectorSelectionResponse
|
||||
}
|
||||
|
||||
export async function settleWithConcurrency<T, TResult>(
|
||||
items: T[],
|
||||
concurrency: number,
|
||||
task: (item: T, index: number) => Promise<TResult>,
|
||||
): Promise<Array<PromiseSettledResult<TResult>>> {
|
||||
const results = new Array<PromiseSettledResult<TResult>>(items.length)
|
||||
const workerCount = Math.min(items.length, Math.max(1, Math.floor(concurrency)))
|
||||
let nextIndex = 0
|
||||
|
||||
const runWorker = async () => {
|
||||
while (nextIndex < items.length) {
|
||||
const index = nextIndex
|
||||
nextIndex += 1
|
||||
try {
|
||||
results[index] = { status: 'fulfilled', value: await task(items[index], index) }
|
||||
} catch (reason) {
|
||||
results[index] = { status: 'rejected', reason }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(Array.from({ length: workerCount }, () => runWorker()))
|
||||
return results
|
||||
}
|
||||
|
||||
export function useMapThemeSelectionInsights<TThemeId extends string>(
|
||||
selectedProjectId: string | null,
|
||||
onDatasetsChanged?: () => Promise<unknown>,
|
||||
@@ -69,8 +100,10 @@ export function useMapThemeSelectionInsights<TThemeId extends string>(
|
||||
setThemeInsightsLoading(true)
|
||||
setThemeInsightsError(null)
|
||||
try {
|
||||
const settled = await Promise.allSettled(
|
||||
queries.map(async ({ themeId, dataset: existingDataset, partitioned, acquisition }) => {
|
||||
const settled = await settleWithConcurrency(
|
||||
queries,
|
||||
3,
|
||||
async ({ themeId, dataset: existingDataset, partitioned, acquisition }) => {
|
||||
let dataset = existingDataset
|
||||
if (acquisition) {
|
||||
const commonPayload = {
|
||||
@@ -98,10 +131,12 @@ export function useMapThemeSelectionInsights<TThemeId extends string>(
|
||||
...commonPayload,
|
||||
product_key: acquisition.productKey as 'buildings' | 'roads' | 'water' | 'parcels',
|
||||
})
|
||||
: await datasetsApi.acquireOfficialVector(selectedProjectId, {
|
||||
...commonPayload,
|
||||
product_key: acquisition.productKey,
|
||||
})
|
||||
: acquisition.kind === 'bathymetry_profiles'
|
||||
? await datasetsApi.acquireBathymetryProfiles(selectedProjectId, commonPayload)
|
||||
: await datasetsApi.acquireOfficialVector(selectedProjectId, {
|
||||
...commonPayload,
|
||||
product_key: acquisition.productKey,
|
||||
})
|
||||
if (acquisitionJob.status !== 'success' || !acquisitionJob.output_dataset_id) {
|
||||
throw new Error(
|
||||
acquisitionJob.error_message
|
||||
@@ -166,7 +201,7 @@ export function useMapThemeSelectionInsights<TThemeId extends string>(
|
||||
limit: 1000,
|
||||
}),
|
||||
}
|
||||
}),
|
||||
},
|
||||
)
|
||||
const successful = settled.flatMap((item) => (item.status === 'fulfilled' ? [item.value] : []))
|
||||
const failures = settled.flatMap((item, index) => (
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react'
|
||||
import { datasetsApi, externalApi } from '../services/api'
|
||||
import { formatError } from '../lib/formatError'
|
||||
import type {
|
||||
BathymetrySourceRead,
|
||||
DhmvProductRead,
|
||||
FloodHazardProductRead,
|
||||
GrbProductRead,
|
||||
@@ -15,6 +16,7 @@ export interface OfficialMapProducts {
|
||||
floodHazard: FloodHazardProductRead[]
|
||||
grb: GrbProductRead[]
|
||||
officialVector: OfficialVectorProductRead[]
|
||||
bathymetry: BathymetrySourceRead[]
|
||||
}
|
||||
|
||||
const EMPTY_PRODUCTS: OfficialMapProducts = {
|
||||
@@ -23,6 +25,7 @@ const EMPTY_PRODUCTS: OfficialMapProducts = {
|
||||
floodHazard: [],
|
||||
grb: [],
|
||||
officialVector: [],
|
||||
bathymetry: [],
|
||||
}
|
||||
|
||||
export function useOfficialMapProducts(selectedProjectId: string | null) {
|
||||
@@ -49,8 +52,9 @@ export function useOfficialMapProducts(selectedProjectId: string | null) {
|
||||
datasetsApi.listFloodHazardProducts(selectedProjectId),
|
||||
datasetsApi.listGrbProducts(selectedProjectId),
|
||||
datasetsApi.listOfficialVectorProducts(selectedProjectId),
|
||||
datasetsApi.listBathymetrySources(selectedProjectId),
|
||||
])
|
||||
.then(([thematic, dhmv, floodHazard, grb, officialVector]) => {
|
||||
.then(([thematic, dhmv, floodHazard, grb, officialVector, bathymetry]) => {
|
||||
if (!cancelled) {
|
||||
setProducts({
|
||||
thematic: thematic.items,
|
||||
@@ -58,6 +62,7 @@ export function useOfficialMapProducts(selectedProjectId: string | null) {
|
||||
floodHazard: floodHazard.items,
|
||||
grb: grb.items,
|
||||
officialVector: officialVector.items,
|
||||
bathymetry: bathymetry.items,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user