-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRunsPerDataPassOverviewModel.js
More file actions
432 lines (387 loc) · 15.1 KB
/
Copy pathRunsPerDataPassOverviewModel.js
File metadata and controls
432 lines (387 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* @license
* Copyright CERN and copyright holders of ALICE O2. This software is
* distributed under the terms of the GNU General Public License v3 (GPL
* Version 3), copied verbatim in the file "COPYING".
*
* See http://alice-o2.web.cern.ch/license for full licensing information.
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { buildUrl, RemoteData } from '/js/src/index.js';
import { ObservableData } from '../../../utilities/ObservableData.js';
import { getRemoteDataSlice } from '../../../utilities/fetch/getRemoteDataSlice.js';
import { getRemoteData } from '../../../utilities/fetch/getRemoteData.js';
import { rctDetectorsProvider } from '../../../services/detectors/detectorsProvider.js';
import { FixedPdpBeamTypeRunsOverviewModel } from '../Overview/FixedPdpBeamTypeRunsOverviewModel.js';
import { jsonPatch } from '../../../utilities/fetch/jsonPatch.js';
import { jsonPut } from '../../../utilities/fetch/jsonPut.js';
import { SkimmingStage } from '../../../domain/enums/SkimmingStage.js';
import { NumericalComparisonFilterModel } from '../../../components/Filters/common/filters/NumericalComparisonFilterModel.js';
import { jsonFetch } from '../../../utilities/fetch/jsonFetch.js';
import { mergeRemoteData } from '../../../utilities/mergeRemoteData.js';
import { RemoteDataSource } from '../../../utilities/fetch/RemoteDataSource.js';
import { DetectorType } from '../../../domain/enums/DetectorTypes.js';
const ALL_CPASS_PRODUCTIONS_REGEX = /cpass\d+/;
const DETECTOR_NAMES_NOT_IN_CPASSES = ['EVS'];
/**
* Runs Per Data Pass overview model
*/
export class RunsPerDataPassOverviewModel extends FixedPdpBeamTypeRunsOverviewModel {
/**
* Constructor
* @param {Model} model global model
*/
constructor(model) {
super(model);
this._dataPass$ = new ObservableData(RemoteData.notAsked());
this._dataPass$.bubbleTo(this);
this._detectors$ = ObservableData
.builder()
.sources([rctDetectorsProvider.qc$, this._dataPass$])
.apply((remoteDataList) => mergeRemoteData(remoteDataList)
.apply({ Success: ([detectors, dataPass]) => ALL_CPASS_PRODUCTIONS_REGEX.test(dataPass.name)
? detectors.filter(({ name, type }) => type !== DetectorType.AOT_GLO || DETECTOR_NAMES_NOT_IN_CPASSES.includes(name))
: detectors,
}))
.build();
this._detectors$.bubbleTo(this);
this.registerDetectorsNotBadFractionFilterModels(this._detectors$);
this.registerDetectorsForQcFlagsDataExport(this._detectors$);
this.registerObervablesQcSummaryDependesOn([this._detectors$]);
this._markAsSkimmableRequestResult$ = new ObservableData(RemoteData.notAsked());
this._markAsSkimmableRequestResult$.bubbleTo(this);
this._gaqSummary$ = new ObservableData({});
this._gaqSummary$.bubbleTo(this);
this._gaqSummarySources = {};
this._gaqSequenceAbortController = null;
this._skimmableRuns$ = new ObservableData(RemoteData.notAsked());
this._skimmableRuns$.bubbleTo(this);
this._filteringModel.put('gaq[notBadFraction]', new NumericalComparisonFilterModel({
scale: 0.01,
integer: false,
}));
this._freezeOrUnfreezeActionState$ = new ObservableData(RemoteData.notAsked());
this._freezeOrUnfreezeActionState$.bubbleTo(this);
this._discardAllQcFlagsActionState$ = new ObservableData(RemoteData.notAsked());
this._discardAllQcFlagsActionState$.bubbleTo(this);
this._item$.observe(() => this._fetchGaqSummaryForCurrentRuns());
}
/**
* Change ready for skimming flag for given run
*
* @param {{ runNumber: number, readyForSkimming: boolean }} skimmableRun run number with ready for skimming flag
* @return {Promise<void>} resolves once request is handled
*/
async changeReadyForSkimmingFlagForRun(skimmableRun) {
this._skimmableRuns$.getCurrent().match({
Success: async (currentSkimmableRuns) => {
this._skimmableRuns$.setCurrent(RemoteData.loading());
const { runNumber, readyForSkimming } = skimmableRun;
try {
const { data: skimmableRuns } = await jsonPut(
buildUrl('/api/dataPasses/skimming/runs', { dataPassId: this._dataPassId }),
{ data: [{ runNumber, readyForSkimming }] },
);
const newRunToReadyForSkimmingFlag = Object.fromEntries(skimmableRuns
.map(({ runNumber, readyForSkimming }) => [runNumber, readyForSkimming]));
this._skimmableRuns$.setCurrent(RemoteData.success({ ...currentSkimmableRuns, ...newRunToReadyForSkimmingFlag }));
} catch (error) {
this._skimmableRuns$.setCurrent(RemoteData.failure(error));
}
},
Other: () => {
this._skimmableRuns$.setCurrent(RemoteData.failure([
{
title: 'Missing data',
detail: 'Need to successfully fetch skimmable runs in order to update them',
},
]));
},
});
}
/**
* @inheritdoc
*/
async load() {
if (!this._dataPassId) {
return;
}
await this._fetchDataPass().then(() =>
this._dataPass$.getCurrent().match({
Success: ({ skimmingStage, pdpBeamTypes }) => {
if (skimmingStage === SkimmingStage.SKIMMABLE) {
this._fetchSkimmableRuns();
}
this.setPdpBeamTypes(pdpBeamTypes);
},
Other: () => null,
}));
super.load();
}
/**
* @inheritdoc
*/
getRootEndpoint() {
const gaqNotBadFilter = this._filteringModel.get('gaq[notBadFraction]');
const filter = { dataPassIds: [this._dataPassId] };
if (!gaqNotBadFilter.isEmpty) {
filter.gaq = {
mcReproducibleAsNotBad: this._mcReproducibleAsNotBad,
};
}
return buildUrl(super.getRootEndpoint(), { filter });
}
/**
* @inheritdoc
*/
resetFiltering(fetch = true) {
super.resetFiltering(fetch);
}
/**
* Freeze the current data pass
*
* @return {Promise<void>} Resolves once the data pass is frozen
*/
async freezeDataPass() {
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.loading());
try {
await jsonPatch(
buildUrl('/api/dataPasses/freeze', { dataPassId: this._dataPassId }),
{},
);
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.success(null));
await this.load();
} catch (error) {
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.failure(error));
}
}
/**
* Unfreeze the current data pass
*
* @return {Promise<void>} Resolves once the data pass is unfrozen
*/
async unfreezeDataPass() {
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.loading());
try {
await jsonPatch(
buildUrl('/api/dataPasses/unfreeze', { dataPassId: this._dataPassId }),
{},
);
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.success(null));
await this.load();
} catch (error) {
this._freezeOrUnfreezeActionState$.setCurrent(RemoteData.failure(error));
}
}
/**
* Return the state of the freeze/unfreeze action
*
* @return {RemoteData<null, ApiError>} the freeze/unfreeze action state
*/
get freezeOrUnfreezeActionState() {
return this._freezeOrUnfreezeActionState$.getCurrent();
}
/**
* Discard all the QC for the data-pass
*
* @return {Promise<void>} resolves once flags has been deleted
*/
async discardAllQcFlags() {
this._discardAllQcFlagsActionState$.setCurrent(RemoteData.loading());
try {
await jsonFetch(
buildUrl('/api/qcFlags/perDataPass', { dataPassId: this._dataPassId }),
{ method: 'DELETE' },
);
this._discardAllQcFlagsActionState$.setCurrent(RemoteData.success(null));
await this.load();
} catch (error) {
this._discardAllQcFlagsActionState$.setCurrent(RemoteData.failure(error));
}
}
/**
* Return the state of the discard all QC flags action
*
* @return {RemoteData<null, ApiError>} the QC flags action state
*/
get discardAllQcFlagsActionState() {
return this._discardAllQcFlagsActionState$.getCurrent();
}
/**
* Send request to mark current data pass as skimmable
* @return {Promise<void>} resolved once request is handled
*/
async markDataPassAsSkimmable() {
this._markAsSkimmableRequestResult$.setCurrent(RemoteData.loading());
try {
await jsonPatch(buildUrl('/api/dataPasses/skimming/markSkimmable', { dataPassId: this._dataPassId }));
this._markAsSkimmableRequestResult$.setCurrent(RemoteData.success());
await this._fetchDataPass();
await this._fetchSkimmableRuns();
} catch (error) {
this._markAsSkimmableRequestResult$.setCurrent(RemoteData.failure(error));
}
}
/**
* Result of request for marking current data pass as skimmable
* @return {RemoteData} remote data
*/
get markAsSkimmableRequestResult() {
return this._markAsSkimmableRequestResult$.getCurrent();
}
/**
* Get skimmable runs
*/
get skimmableRuns() {
return this._skimmableRuns$.getCurrent();
}
/**
* @inheritdoc
*/
reset(fetch = true) {
super.reset(fetch);
this._markAsSkimmableRequestResult$?.setCurrent(RemoteData.NotAsked());
}
/**
* Set id of data pass which runs are to be fetched
* @param {number} dataPassId id of Data Pass
*/
set dataPassId(dataPassId) {
if (dataPassId !== this._dataPassId) {
this.reset(false);
}
this._dataPassId = dataPassId;
}
/**
* Get current data pass which runs are fetched
*/
get dataPass() {
return this._dataPass$.getCurrent();
}
/**
* Get all detectors
* @return {RemoteData<DplDetector[]>} detectors
*/
get detectors() {
return this._detectors$.getCurrent();
}
/**
* GAQ summary getter
* @return {RemoteData<GaqSummary>} GAQ summary
*/
get gaqSummary() {
return this._gaqSummary$.getCurrent();
}
/**
* Data pass id getter
* @return {number} current data pass id
*/
get dataPassId() {
return this._dataPassId;
}
/**
* Fetch data pass data which runs are fetched
* @return {Promise<void>} promise
*/
async _fetchDataPass() {
this._dataPass$.setCurrent(RemoteData.loading());
try {
const { items: [dataPass] = [] } = await getRemoteDataSlice(`/api/dataPasses?filter[ids][]=${this._dataPassId}`);
this._dataPass$.setCurrent(RemoteData.success(dataPass));
} catch (error) {
this._dataPass$.setCurrent(RemoteData.failure(error));
}
}
/**
* Cancel all ongoing and future GAQ summary fetches
* @return {void} promise
*/
_abortGaqFetches() {
// Aborts the overall sequence fetch, i.e. stops further individual run fetches
this._gaqSequenceAbortController?.abort();
// Aborts individual run fetches in-flight
for (const runNumber in this._gaqSummarySources) {
this._gaqSummarySources[runNumber]?._abortController?.abort();
}
}
/**
* Fetch GAQ summary for given data pass and run
* @param {number} [runNumber] run number to filter by
* @return {Promise<void>} resolves once data has been fetched
*/
async _fetchGaqSummary(runNumber) {
this._gaqSummarySources[runNumber] = new RemoteDataSource();
// Pipe the result into the correct slot in the gaqSummary$ observable
this._gaqSummarySources[runNumber].pipe({
setCurrent: (remoteData) => {
const current = this._gaqSummary$.getCurrent();
this._gaqSummary$.setCurrent({
...current,
[runNumber]: remoteData.apply({ Success: (response) => response.data }),
});
},
});
const url = buildUrl('/api/qcFlags/summary/gaq', {
dataPassId: this._dataPassId,
mcReproducibleAsNotBad: this._mcReproducibleAsNotBad,
runNumber: runNumber,
});
await this._gaqSummarySources[runNumber].fetch(url);
}
/**
* Fetch GAQ summary for currently displayed (paginated) runs
* @return {void}
*/
_fetchGaqSummaryForCurrentRuns() {
// Stop any previous fetch (quickly changing filters, pagination, etc)
this._abortGaqFetches();
// Reset abort controller
this._gaqSequenceAbortController = new AbortController();
const { signal } = this._gaqSequenceAbortController;
this._item$.getCurrent().match({
Success: (runs) => {
const runNumbers = runs.map((run) => run.runNumber);
// Prepare GAQ summary object with NotAsked RemoteData state for all runs
let gaqSummary = {};
for (const runNumber of runNumbers) {
gaqSummary = { ...gaqSummary, [runNumber]: RemoteData.notAsked() };
}
this._gaqSummary$.setCurrent(gaqSummary);
// Trigger GAQ summary fetch for each run
for (const runNumber of runNumbers) {
if (signal.aborted) {
return;
}
this._fetchGaqSummary(runNumber);
}
},
Other: () => {
// Don't fetch if runs haven't loaded successfully yet
},
});
}
/**
* Fetch skimmable runs for given data pass
* @return {Promise<void>} resolves once data are fetched
*/
async _fetchSkimmableRuns() {
this._skimmableRuns$.setCurrent(RemoteData.loading());
try {
const { data: skimmableRuns } = await getRemoteData(buildUrl('/api/dataPasses/skimming/runs', { dataPassId: this._dataPassId }));
const runToReadyForSkimmingFlag = Object.fromEntries(skimmableRuns
.map(({ runNumber, readyForSkimming }) => [runNumber, readyForSkimming]));
this._skimmableRuns$.setCurrent(RemoteData.success(runToReadyForSkimmingFlag));
} catch (error) {
this._skimmableRuns$.setCurrent(RemoteData.failure(error));
}
}
/**
* @inheritdoc
*/
get qcSummaryScope() {
return { dataPassId: this.dataPassId };
}
}