1- import { Frequency_api , VectorDescription_api } from "@api" ;
1+ import { Frequency_api , SummaryVectorObservations_api , VectorDescription_api } from "@api" ;
22import { VectorHistoricalData_api , VectorRealizationData_api , VectorStatisticData_api } from "@api" ;
33import { apiService } from "@framework/ApiService" ;
44import { EnsembleIdent } from "@framework/EnsembleIdent" ;
@@ -12,7 +12,6 @@ const CACHE_TIME = 60 * 1000;
1212export function useVectorListQueries (
1313 caseUuidsAndEnsembleNames : EnsembleIdent [ ] | null
1414) : UseQueryResult < VectorDescription_api [ ] > [ ] {
15- // Note: how to cancel queryFn if key is updated?
1615 return useQueries ( {
1716 queries : ( caseUuidsAndEnsembleNames ?? [ ] ) . map ( ( item ) => {
1817 return {
@@ -33,7 +32,6 @@ export function useVectorDataQueries(
3332 realizationsToInclude : number [ ] | null ,
3433 allowEnable : boolean
3534) : UseQueryResult < VectorRealizationData_api [ ] > [ ] {
36- // Note: how to cancel queryFn if key is updated?
3735 return useQueries ( {
3836 queries : ( vectorSpecifications ?? [ ] ) . map ( ( item ) => {
3937 return {
@@ -141,3 +139,90 @@ export function useHistoricalVectorDataQueries(
141139 } ) ,
142140 } ) ;
143141}
142+
143+ /**
144+ * Definition of ensemble vector observation data
145+ *
146+ * hasSummaryObservations: true if the ensemble has observations, i.e the summary observations array is not empty
147+ * vectorsObservationData: array of vector observation data for requested vector specifications
148+ */
149+ export type EnsembleVectorObservationData = {
150+ hasSummaryObservations : boolean ;
151+ vectorsObservationData : { vectorSpecification : VectorSpec ; data : SummaryVectorObservations_api } [ ] ;
152+ } ;
153+
154+ /**
155+ * Definition of map of ensemble ident and ensemble vector observation data
156+ */
157+ export type EnsembleVectorObservationDataMap = Map < EnsembleIdent , EnsembleVectorObservationData > ;
158+
159+ /**
160+ * Definition of vector observations queries result for combined queries
161+ */
162+ export type VectorObservationsQueriesResult = {
163+ isFetching : boolean ;
164+ isError : boolean ;
165+ ensembleVectorObservationDataMap : EnsembleVectorObservationDataMap ;
166+ } ;
167+
168+ /**
169+ * This function takes vectorSpecifications and returns a map of ensembleIdent and the respective observations data for
170+ * the selected vectors.
171+ *
172+ * If the returned summary array from back-end is empty array, the ensemble does not have observations.
173+ * If the selected vectors are not among the returned summary array, the vector does not have observations.
174+ */
175+ export function useVectorObservationsQueries (
176+ vectorSpecifications : VectorSpec [ ] | null ,
177+ allowEnable : boolean
178+ ) : VectorObservationsQueriesResult {
179+ const uniqueEnsembleIdents = [ ...new Set ( vectorSpecifications ?. map ( ( item ) => item . ensembleIdent ) ?? [ ] ) ] ;
180+ return useQueries ( {
181+ queries : ( uniqueEnsembleIdents ?? [ ] ) . map ( ( item ) => {
182+ return {
183+ queryKey : [ "getObservations" , item . getCaseUuid ( ) , item . getEnsembleName ( ) ] ,
184+ queryFn : ( ) =>
185+ apiService . observations . getObservations ( item . getCaseUuid ( ) ?? "" , item . getEnsembleName ( ) ?? "" ) ,
186+ staleTime : STALE_TIME ,
187+ cacheTime : CACHE_TIME ,
188+ enabled : ! ! ( allowEnable && item . getCaseUuid ( ) && item . getEnsembleName ( ) ) ,
189+ } ;
190+ } ) ,
191+ combine : ( results ) => {
192+ const combinedResult : EnsembleVectorObservationDataMap = new Map ( ) ;
193+ if ( ! vectorSpecifications )
194+ return { isFetching : false , isError : false , ensembleVectorObservationDataMap : combinedResult } ;
195+
196+ results . forEach ( ( result , index ) => {
197+ const ensembleIdent = uniqueEnsembleIdents . at ( index ) ;
198+ if ( ! ensembleIdent ) return ;
199+
200+ const ensembleVectorSpecifications = vectorSpecifications . filter (
201+ ( item ) => item . ensembleIdent === ensembleIdent
202+ ) ;
203+
204+ const ensembleHasObservations = result . data ?. summary . length !== 0 ;
205+ combinedResult . set ( ensembleIdent , {
206+ hasSummaryObservations : ensembleHasObservations ,
207+ vectorsObservationData : [ ] ,
208+ } ) ;
209+ for ( const vectorSpec of ensembleVectorSpecifications ) {
210+ const vectorObservationsData =
211+ result . data ?. summary . find ( ( elm ) => elm . vector_name === vectorSpec . vectorName ) ?? null ;
212+ if ( ! vectorObservationsData ) continue ;
213+
214+ combinedResult . get ( ensembleIdent ) ?. vectorsObservationData . push ( {
215+ vectorSpecification : vectorSpec ,
216+ data : vectorObservationsData ,
217+ } ) ;
218+ }
219+ } ) ;
220+
221+ return {
222+ isFetching : results . some ( ( result ) => result . isFetching ) ,
223+ isError : results . some ( ( result ) => result . isError ) ,
224+ ensembleVectorObservationDataMap : combinedResult ,
225+ } ;
226+ } ,
227+ } ) ;
228+ }
0 commit comments