-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRunPatch.js
More file actions
397 lines (348 loc) · 12.2 KB
/
Copy pathRunPatch.js
File metadata and controls
397 lines (348 loc) · 12.2 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
import { Observable } from '/js/src/index.js';
import { RUN_CALIBRATION_STATUS, RunCalibrationStatus } from '../../../domain/enums/RunCalibrationStatus.js';
import { RunQualities } from '../../../domain/enums/RunQualities.js';
/**
* @typedef EorReasonPatch
* @property {number} [id]
* @property {number} reasonTypeId
* @property {string} category
* @property {string} title
* @property {string} description
* @property {string|null} [lastEditedName]
*/
/**
* Represents a patch that may be applied to an existing run
*/
export class RunPatch extends Observable {
/**
* Constructor
* @param {Run} [run] the run on which the patch will be applied
*/
constructor(run) {
super();
/**
* Store the run to reset patch
*
* @type {Run|undefined}
* @private
*/
this._run = run;
this.reset();
}
/**
* States if the current patch is valid
*
* @return {boolean} true if the patch is valid
*/
isValid() {
// If we change the run quality, we require a reason
const runQualityChangeReasonIsValid = !this.requireRunQualityChangeReason()
|| this._runQualityChangeReason?.trim();
// If we change the calibration status, we require a reason
const calibrationStatusChangeReasonIsValid = !this.requireCalibrationStatusChangeReason
|| this._calibrationStatusChangeReason?.trim();
// If we change the detectors qualities, we require a reason
const detectorQualityChangeReasonIsValid = !this.hasAnyDetectorsQualityChange()
|| this._detectorsQualitiesChangeReason?.trim();
return calibrationStatusChangeReasonIsValid && detectorQualityChangeReasonIsValid && runQualityChangeReasonIsValid;
}
/**
* Returns the patch in the form of a simple object that can be displayed as JSON
*
* @return {object} the JSON version of the patch
*/
toPojo() {
const ret = {};
if (this._tags.length !== this._run.tags.length || this._run.tags.some(({ text }) => !this._tags.includes(text))) {
ret.tags = this._tags;
}
if (this.hasAnyDetectorsQualityChange()) {
ret.detectorsQualities = [];
for (const [detectorId, quality] of this._detectorsQualitiesPatches.entries()) {
ret.detectorsQualities.push({ detectorId, quality });
}
ret.detectorsQualitiesChangeReason = this._detectorsQualitiesChangeReason;
}
if (this._eorReasons.length !== this._run.eorReasons.length || this._eorReasons.some(({ id }) => id === undefined)) {
// Strip lastEditedName — the server's EorReasonDto only accepts id, reasonTypeId, and description
ret.eorReasons = this._eorReasons.map(({ id, reasonTypeId, description }) => ({ id, reasonTypeId, description }));
}
if (this._hasRunQualityChange()) {
ret.runQuality = this._runQuality;
ret.runQualityChangeReason = this._runQualityChangeReason;
}
if (this._run.calibrationStatus !== this._calibrationStatus) {
ret.calibrationStatus = this._calibrationStatus;
}
if (this.requireCalibrationStatusChangeReason) {
ret.calibrationStatusChangeReason = this._calibrationStatusChangeReason;
}
if (this.formData.inelasticInteractionRateAvg !== this._run.inelasticInteractionRateAvg) {
ret.inelasticInteractionRateAvg = this.formData.inelasticInteractionRateAvg;
}
if (this.formData.inelasticInteractionRateAtStart !== this._run.inelasticInteractionRateAtStart) {
ret.inelasticInteractionRateAtStart = this.formData.inelasticInteractionRateAtStart;
}
if (this.formData.inelasticInteractionRateAtMid !== this._run.inelasticInteractionRateAtMid) {
ret.inelasticInteractionRateAtMid = this.formData.inelasticInteractionRateAtMid;
}
if (this.formData.inelasticInteractionRateAtEnd !== this._run.inelasticInteractionRateAtEnd) {
ret.inelasticInteractionRateAtEnd = this.formData.inelasticInteractionRateAtEnd;
}
return ret;
}
/**
* Reset the patch to no modification
*
* @return {void}
*/
reset() {
const {
calibrationStatus,
eorReasons = [],
tags = [],
detectorsQualities = [],
runQuality,
inelasticInteractionRateAvg,
inelasticInteractionRateAtStart,
inelasticInteractionRateAtMid,
inelasticInteractionRateAtEnd,
} = this._run || {};
this._runQuality = runQuality;
this._eorReasons = eorReasons.map(({ id, description, reasonTypeId, lastEditedName }) => ({
id,
description,
reasonTypeId,
lastEditedName,
}));
this._tags = tags.map(({ text }) => text);
this.formData = {
inelasticInteractionRateAvg,
inelasticInteractionRateAtStart,
inelasticInteractionRateAtMid,
inelasticInteractionRateAtEnd,
};
/**
* Stores, for each detector, its current quality
* @type {Map<number, string>}
* @private
*/
this._detectorsQualities = new Map();
for (const { id, quality } of detectorsQualities) {
this._detectorsQualities.set(id, quality);
}
/**
* Stores, for each detector the new quality if it is different from the current one
* @type {Map<number, string>}
* @private
*/
this._detectorsQualitiesPatches = new Map();
this._calibrationStatus = calibrationStatus;
this.notify();
}
/**
* Apply a patch on current form data
*
* @param {Partial<Run>} patch the patch to apply
* @return {void}
*/
patchFormData(patch) {
this.formData = { ...this.formData, ...patch };
this.notify();
}
/**
* Returns the patched run quality
*
* @return {string} the quality
*/
get runQuality() {
return this._runQuality;
}
/**
* Set the runQuality
* @param {string|undefined} runQuality the patched run quality
* @return {void}
*/
setRunQuality(runQuality) {
this._runQuality = runQuality;
this.notify();
}
/**
* States if the current run quality change require justification
* @return {boolean} true if the quality change require justification
*/
requireRunQualityChangeReason() {
return this._hasRunQualityChange()
&& !(this._run.runQuality === RunQualities.NONE
&& (this._runQuality === RunQualities.GOOD || this._runQuality === RunQualities.TEST));
}
/**
* States if the run quality has been changed
*
* @return {boolean} true if the run quality has been changed
*/
_hasRunQualityChange() {
return this._run.runQuality !== this._runQuality;
}
/**
* Return the reason of the run quality change
*
* @return {string} the quality change reason
*/
get runQualityChangeReason() {
return this._runQualityChangeReason;
}
/**
* Set the reason of the run quality change
*
* @param {string} reason the reason
*/
set runQualityChangeReason(reason) {
this._runQualityChangeReason = reason;
this.notify();
}
/**
* Define the patch's tag list
*
* @param {string[]} tags the tags list
* @return {void}
*/
setTags(tags) {
this._tags = tags;
this.notify();
}
/**
* Add a new EOR reason to the patch
* @param {EorReasonPatch} newEorReason the EOR reason to add
* @return {void}
*/
addEorReason({ reasonTypeId, description }) {
const isNew = this._eorReasons.every((eorReason) => eorReason.reasonTypeId !== reasonTypeId || eorReason.description !== description);
if (isNew) {
this._eorReasons.push({ description, reasonTypeId });
}
this.notify();
}
/**
* Delete an EOR reason patch from the run patch
*
* @param {EorReasonPatch} eorReasonPatch the EOR reason to delete from the patch
* @return {void}
*/
removeEorReason(eorReasonPatch) {
const eorIndex = this._eorReasons.findIndex((reference) => compareEorReasonsPatch(reference, eorReasonPatch));
if (eorIndex >= 0) {
this._eorReasons.splice(eorIndex, 1);
}
this.notify();
}
/**
* Returns the patched list of EOR reasons for the patched run
* @return {EorReasonPatch[]} the list of EOR reasons patch in the run patch
*/
get eorReasons() {
return this._eorReasons;
}
/**
* Returns the quality assigned for a given detector
*
* @param {number} detectorId the id of the detector
* @return {string|undefined} the quality
*/
getDetectorQuality(detectorId) {
return this._detectorsQualitiesPatches.get(detectorId);
}
/**
* Defines the quality of a given detector
*
* @param {number} detectorId the id of the detector
* @param {string} quality the quality of the detector
* @return {void}
*/
setDetectorQuality(detectorId, quality) {
if (this._detectorsQualities.get(detectorId) === quality) {
this._detectorsQualitiesPatches.delete(detectorId);
} else {
this._detectorsQualitiesPatches.set(detectorId, quality);
}
this.notify();
}
/**
* States if any of the detector quality is being patched
*
* @return {boolean} true if any detector quality is changing
*/
hasAnyDetectorsQualityChange() {
return this._detectorsQualitiesPatches.size > 0;
}
/**
* Returns the reason of the detectors' qualities change, if it applies
*
* @return {string} the change reason
*/
get detectorsQualityChangeReason() {
return this._detectorsQualitiesChangeReason || '';
}
/**
* Set the reason of the detectors' qualities change, if it applies
* @param {string} reason the change reason
*/
set detectorsQualityChangeReason(reason) {
this._detectorsQualitiesChangeReason = reason;
this.notify();
}
/**
* Returns the current calibration status
* @return {string} the current calibration status
*/
get calibrationStatus() {
return this._calibrationStatus;
}
/**
* Set the current calibration status
* @param {string} calibrationStatus the new calibration status
* @return {void}
*/
set calibrationStatus(calibrationStatus) {
if (!RUN_CALIBRATION_STATUS.includes(calibrationStatus)) {
throw new Error('Invalid calibration status');
}
this._calibrationStatus = calibrationStatus;
this.notify();
}
/**
* States if the current run and patch require a calibration status change reason
*
* @return {boolean} true if a reason is required
*/
get requireCalibrationStatusChangeReason() {
return Boolean(this._run.calibrationStatus === RunCalibrationStatus.FAILED
^ this._calibrationStatus === RunCalibrationStatus.FAILED);
}
/**
* Returns the current reason for calibration status change, if it applies
* @return {string} the reason
*/
get calibrationStatusChangeReason() {
return this._calibrationStatusChangeReason;
}
/**
* If it applies, set the current reason for calibration status change
* @param {string} reason the new reason
*/
set calibrationStatusChangeReason(reason) {
if (!this.requireCalibrationStatusChangeReason) {
throw new Error('This run do not require calibration status change reason');
}
this._calibrationStatusChangeReason = reason;
this.notify();
}
}
/**
* Compare two EOR reasons patch and states if they are the same
*
* @param {EorReasonPatch} a the first element to compare
* @param {EorReasonPatch} b the second element to compare
* @return {boolean} true if the two EOR reasons patches are the same
*/
const compareEorReasonsPatch = (a, b) => a.id === b.id && a.reasonTypeId === b.reasonTypeId && a.description === b.description;