1
- import { Frequency_api , VectorDescription_api } from "@api" ;
1
+ import { Frequency_api , SummaryVectorObservations_api , VectorDescription_api } from "@api" ;
2
2
import { VectorHistoricalData_api , VectorRealizationData_api , VectorStatisticData_api } from "@api" ;
3
3
import { apiService } from "@framework/ApiService" ;
4
4
import { EnsembleIdent } from "@framework/EnsembleIdent" ;
@@ -12,7 +12,6 @@ const CACHE_TIME = 60 * 1000;
12
12
export function useVectorListQueries (
13
13
caseUuidsAndEnsembleNames : EnsembleIdent [ ] | null
14
14
) : UseQueryResult < VectorDescription_api [ ] > [ ] {
15
- // Note: how to cancel queryFn if key is updated?
16
15
return useQueries ( {
17
16
queries : ( caseUuidsAndEnsembleNames ?? [ ] ) . map ( ( item ) => {
18
17
return {
@@ -33,7 +32,6 @@ export function useVectorDataQueries(
33
32
realizationsToInclude : number [ ] | null ,
34
33
allowEnable : boolean
35
34
) : UseQueryResult < VectorRealizationData_api [ ] > [ ] {
36
- // Note: how to cancel queryFn if key is updated?
37
35
return useQueries ( {
38
36
queries : ( vectorSpecifications ?? [ ] ) . map ( ( item ) => {
39
37
return {
@@ -141,3 +139,90 @@ export function useHistoricalVectorDataQueries(
141
139
} ) ,
142
140
} ) ;
143
141
}
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