-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFlpSelection.js
More file actions
323 lines (296 loc) · 11.4 KB
/
FlpSelection.js
File metadata and controls
323 lines (296 loc) · 11.4 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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* 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 {Observable, RemoteData} from '/js/src/index.js';
/**
* Model representing Workflow
*/
export default class FlpSelection extends Observable {
/**
* Initialize FLPs Selection Component
* @param {Object} workflow
* @param {Lock} lockModel - model of the lock state which will be used to automatically unselect detectors that are no longer locked by the current user
*/
constructor(workflow, lockModel) {
super();
this.loader = workflow.model.loader;
this.workflow = workflow;
this.list = RemoteData.notAsked();
this.firstFlpSelection = -1;
this.hostsByDetectors = {};
this.detectors = RemoteData.notAsked();
this.activeDetectors = RemoteData.notAsked();
this.selectedDetectors = [];
this.unavailableDetectors = []; // detectors which are loaded from configuration but active in AliECS
this.missingHosts = [];
this.detectorViewConfigurationError = false;
// Observe lock changes to automatically unselect detectors when locks are released
lockModel.observe(() => this._handleLockChanges());
}
/**
* Initialize detectors and hosts panels with empty selection
*/
async init() {
this.selectedDetectors = [];
this.list = RemoteData.notAsked();
this.hostsByDetectors = {};
this.workflow.form.setHosts([]);
this.unavailableDetectors = [];
this.missingHosts = [];
this.detectorViewConfigurationError = false;
this.notify();
await this.getAndSetDetectors();
}
/**
* Method to request a list of detectors from AliECS and initialized the user form accordingly
* @return {Promise<void>}
*/
async getAndSetDetectors() {
this.detectors = this.workflow.model.detectors.listRemote;
await this.getActiveDetectors();
}
/**
* Method to retrieve the detectors that are active as per AliECS
* @return {Promise<void>}
*/
async getActiveDetectors() {
this.activeDetectors = RemoteData.loading();
this.notify();
const {result, ok} = await this.workflow.model.loader.post('/api/GetActiveDetectors', {});
this.activeDetectors = ok ? RemoteData.success(result) : RemoteData.failure(result.message);
this.notify();
}
/**
* Given a list of detectors and hosts:
* * update the data from AliECS with available detectors
* * make the selection of the detectors based on passed values
* * make the selection of the hosts based on passed values
* * inform the user if some are unavailable anymore
* @param {Array<String>} detectors
* @param {Array<String>} hosts
*/
async setDetectorsAndHosts(detectors, hosts) {
this.init();
await this.getAndSetDetectors(); // get the latest information on detectors
detectors.map((detector) => {
if (this.activeDetectors.isSuccess() && !this.activeDetectors.payload.detectors.includes(detector)) {
this.selectedDetectors.push(detector);
this.setHostsForDetector(detector);
hosts.forEach((host) => {
if (this.hostsByDetectors[detector].includes(host)) {
this.workflow.form.addHost(host);
}
});
}
if (this.activeDetectors.isSuccess() && this.activeDetectors.payload.detectors.includes(detector)) {
this.unavailableDetectors.push(detector);
}
});
hosts.filter((host) => !this.workflow.form.hosts.includes(host))
.forEach((host) => this.missingHosts.push(host));
this.notify();
}
/**
* Toggle selection of a detector. A detector can have one of the states taken/released:
* This toggle will also notify the observer by default unless specified otherwise via shouldNotify
* @param {String} name
* @param {boolean} shouldNotify - specify if the method should trigger a notification at the end of the process.
*/
toggleDetectorSelection(name, shouldNotify = true) {
const indexUnavailable = this.unavailableDetectors.indexOf(name)
if (indexUnavailable >= 0) {
this.unavailableDetectors.splice(indexUnavailable, 1);
} else {
const index = this.selectedDetectors.indexOf(name);
if (index >= 0) {
this.selectedDetectors.splice(index, 1);
this.removeHostsByDetector(name);
} else if (!this.isDetectorActive(name)) {
this.selectedDetectors.push(name);
this.setHostsForDetector(name, true);
}
}
if (shouldNotify) {
this.notify();
}
}
/**
* Method which checks if a detector is available and current user has the lock for it.
* If so, it will select the detector and all its associated FLPs
* @param {Array<String>} detectorsToSelect - list of detectors to select
* @returns {Promise<void>}
*/
selectAllAvailableDetectors(detectorsToSelect) {
this.selectedDetectors = [];
const activeDetectors = this.activeDetectors.isSuccess() ? this.activeDetectors.payload.detectors : [];
detectorsToSelect.filter((detector) =>
!activeDetectors.includes(detector) && this.workflow.model.lock.isLockedByCurrentUser(detector)
)
.forEach((detector) => {
this.selectedDetectors.push(detector);
this.setHostsForDetector(detector, true);
});
this.notify();
}
/**
* Given a name of a detector, it checks if it is part of the active list
* @param {String} name
* @returns {boolean}
*/
isDetectorActive(name) {
return this.activeDetectors.isSuccess() && this.activeDetectors.payload.detectors.includes(name)
}
/**
* Toggle the selection of an FLP from the form host
* The user can also use SHIFT key to select between 2 FLP machines, thus
* selecting all machines between the 2 selected
* @param {string} name
* @param {Event} e
*/
toggleFLPSelection(name, e) {
if (e.shiftKey && this.list.isSuccess()) {
if (this.firstFlpSelection === -1) {
this.firstFlpSelection = this.list.payload.indexOf(name);
} else {
let secondFlpSelection = this.list.payload.indexOf(name);
if (this.firstFlpSelection > secondFlpSelection) {
[this.firstFlpSelection, secondFlpSelection] = [secondFlpSelection, this.firstFlpSelection];
}
for (let flpIndex = this.firstFlpSelection; flpIndex <= secondFlpSelection; ++flpIndex) {
const flpName = this.list.payload[flpIndex];
const hostFormIndex = this.workflow.form.getHosts().indexOf(flpName);
if (hostFormIndex < 0) {
this.workflow.form.addHost(flpName);
}
}
this.firstFlpSelection = -1;
}
} else {
this.firstFlpSelection = -1;
const index = this.workflow.form.hosts.indexOf(name);
if (index < 0) {
this.workflow.form.addHost(name);
} else {
this.workflow.form.removeHostByIndex(index);
}
}
this.notify();
}
/**
* If all FLPs are selected than deselect them
* Else select all FLPs
*/
toggleAllFLPSelection() {
this.workflow.form.hosts = this.areAllFLPsSelected() ? [] : JSON.parse(JSON.stringify(this.list.payload));
this.notify();
}
/**
* Check if all FLPs are selected
* @return {boolean}
*/
areAllFLPsSelected() {
return this.list.isSuccess() && this.workflow.form.hosts.length === this.list.payload.length;
}
/**
* Remove a detector and its corresponding hosts
* @param {String} detector
*/
removeHostsByDetector(detector) {
if (this.hostsByDetectors[detector]) {
this.hostsByDetectors[detector].forEach((host) => {
// Delete hosts of the detector from form selection
const index = this.workflow.form.hosts.indexOf(host);
if (index >= 0) {
this.workflow.form.hosts.splice(index, 1);
}
})
delete this.hostsByDetectors[detector];
let temp = []
// update list of displayed hosts with remaining ones
Object.keys(this.hostsByDetectors).forEach((detector) => temp = temp.concat(this.hostsByDetectors[detector]))
this.list = RemoteData.success(temp);
this.notify();
}
}
/**
* Method to return the name of the detector to which a given host name belongs to
* @param {String} host
*/
getDetectorForHost(host) {
let detectorForHost = '';
Object.keys(this.hostsByDetectors).forEach((detector) => {
const hosts = this.hostsByDetectors[detector];
if (hosts.includes(host)) {
detectorForHost = detector;
}
});
return detectorForHost;
}
/**
* Given a detector name, build a string containing the name and number of selected hosts
* and available hosts for that detector
* @param {String} detector
* @returns {String}
*/
getDetectorWithIndexes(detector) {
const hostsByDetectorRemote = this.workflow.model.detectors.hostsByDetectorRemote;
if (hostsByDetectorRemote.isSuccess() && hostsByDetectorRemote.payload[detector]) {
const hosts = hostsByDetectorRemote.payload[detector];
const selectedHosts = this.workflow.form.hosts;
const totalSelected = selectedHosts.filter((host) => hosts.includes(host)).length;
return detector + ' (' + totalSelected + '/' + hosts.length + ')'
}
return detector;
}
/**
* Given a detector name, if hosts were successfully loaded on page load,
* update the list of hosts by adding the ones for the given detector
* If specified via shouldSelect, it will also add the hosts to the form so that they appeared
* as selected for the user
* @param {String} detector
*/
setHostsForDetector(detector, shouldSelect = false) {
const hostsByDetectorRemote = this.workflow.model.detectors.hostsByDetectorRemote;
if (!hostsByDetectorRemote.isSuccess()) {
this.list = RemoteData.failure(hostsByDetectorRemote.message);
} else {
this.hostsByDetectors[detector] = hostsByDetectorRemote.payload[detector];
let temp = [];
Object.keys(this.hostsByDetectors).forEach((detector) => temp = temp.concat(this.hostsByDetectors[detector]));
this.list = RemoteData.success(temp);
if (shouldSelect) {
this.hostsByDetectors[detector].forEach((hostname) => this.workflow.form.addHost(hostname));
}
}
this.notify();
}
/**
* Handler for lock state changes so that it automatically unselects detectors that are no longer locked by the current user.
* This method will be called either by:
* * changes triggered by the user on the lock button of a detector panel/locks page
* * changes triggered by a different user and received via websocket subscription to lock changes
* In all cases, if a detector is no longer locked by the current user, it will be unselected from the workflow and its hosts will be removed from the form selection.
* @private
* @returns {void}
*/
_handleLockChanges() {
this.selectedDetectors
.filter((detector) => !this.workflow.model.lock.isLockedByCurrentUser(detector))
.forEach((detector) => {
if (this.selectedDetectors.includes(detector)) {
this.toggleDetectorSelection(detector, false);
}
});
this.notify();
}
}