-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDetectorService.js
More file actions
308 lines (280 loc) · 9.68 KB
/
DetectorService.js
File metadata and controls
308 lines (280 loc) · 9.68 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
/**
* @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, BrowserStorage} from '/js/src/index.js';
import {STORAGE, PREFIX, ROLES} from './../workflow/constants.js';
import {DetectorState} from './../common/enums/DetectorState.enum.js';
import {isUserAllowedRole} from './../common/userRole.js';
/**
* Model representing API Calls regarding Detectors
*/
export default class DetectorService extends Observable {
/**
* Initialize service with model object
* @param {Observable} model
*/
constructor(model) {
super();
this.model = model;
this.authed = this.model.session.access
.filter((role) => role.startsWith(PREFIX.SSO_DET_ROLE))
.map((role) => role.replace(PREFIX.SSO_DET_ROLE, '').toUpperCase());
this.storage = new BrowserStorage(`COG-${this.model.router.location.hostname}`);
this._listRemote = RemoteData.notAsked();
this.hostsByDetectorRemote = RemoteData.notAsked();
this._selected = '';
/**
* @type {Object<String, Detector>}
*/
this._availability = {};
}
/**
* Load the selected detector from LocalStorage;
* Initialize to empty string if the user did not select any
*/
async init() {
const stored = this.storage.getLocalItem(STORAGE.DETECTOR);
this._selected = (stored && stored.SELECTED &&
(isUserAllowedRole(ROLES.Global) || this.authed.includes(stored.SELECTED))) ? stored.SELECTED : '';
this.notify();
this._listRemote = await this.getDetectorsAsRemoteData(this._listRemote, this);
this.notify();
if (this._listRemote.isSuccess()) {
this.hostsByDetectorRemote = await this.getHostsByDetectorsAsRemoteData(
this.hostsByDetectorRemote, this._listRemote.payload, this
);
for (const detector of this._listRemote.payload) {
this._availability[detector] = {
pfrAvailability: DetectorState.UNDEFINED,
sorAvailability: DetectorState.UNDEFINED,
}
}
}
this.notify();
}
/**
* Checks if the detector view is single and not global
* @returns {boolean}
*/
isSingleView() {
return this._selected && this._selected !== 'GLOBAL';
}
/**
* Checks if the user selected detector view is GLOBAL
* @returns {boolean}
*/
isGlobalView() {
return this._selected === 'GLOBAL';
}
/**
* Update selection for detector view in LocalStorage
* Format: {SELECTED: <string>}
* @param {Array<String>} detector
*/
saveSelection(detector) {
this._selected = detector;
this.storage.setLocalItem(STORAGE.DETECTOR, {SELECTED: detector});
this.notify();
}
/**
* HTTP Calls
*/
/**
* Given a list of detectors, make a request to get all the hosts
* belonging to each detector;
* Returns a RemoteData which if successful contains a map<String,JSON>
* @param {RemoteData} item
* @param {Object} that
* @returns {RemoteData}
*/
async getHostsByDetectorsAsRemoteData(item, detectors, that) {
item = RemoteData.loading();
that.notify();
const {ok, result} = await this.model.loader.get(`/api/core/hostsByDetectors`);
item = ok ?
RemoteData.success(result.hosts) : RemoteData.failure({message: 'Unable to load list of hosts by detectors'});
that.notify();
return item;
}
/**
* Fetch detectors and return it as a remoteData object
* @param {RemoteData} item
*/
async getDetectorsAsRemoteData(item, that) {
item = RemoteData.loading();
that.notify();
const {result, ok} = await this.model.loader.get(`/api/core/detectors`);
if (!ok) {
item = RemoteData.failure(result.message);
} else {
item = RemoteData.success(result.detectors);
}
that.notify();
return item;
}
/**
* Fetch detectors and return it as a remoteData object
* @param {RemoteData} item
*/
async getAndSetDetectorsAsRemoteData() {
this._listRemote = RemoteData.loading();
this.notify();
const {result, ok} = await this.model.loader.get(`/api/core/detectors`);
if (!ok) {
this._listRemote = RemoteData.failure(result.message);
} else {
this._listRemote = RemoteData.success(result.detectors);
}
this.notify();
}
/**
* Fetch detectors and return it as a remoteData object
* @param {RemoteData} item
*/
async getActiveDetectorsAsRemoteData(item) {
item = RemoteData.loading();
this.notify();
const {result, ok} = await this.model.loader.post(`/api/GetActiveDetectors`);
if (!ok) {
item = RemoteData.failure(result.message);
} else {
item = RemoteData.success(result.detectors);
}
this.notify();
return item;
}
/**
* Given a detector, it will return a RemoteData objects containing the result of query 'GetHostInventory'
* @param {String} detector
* @return {RemoteData}
*/
async getHostsForDetector(detector, item, that) {
item = RemoteData.loading();
that.notify();
const {result, ok} = await this.model.loader.post(`/api/GetHostInventory`, {detector});
if (!ok) {
item = RemoteData.failure(result.message);
} else {
item = RemoteData.success(result.hosts);
}
that.notify();
return item;
}
/**
* Getters & Setters
*/
/**
* Method to return a RemoteData object containing list of detectors fetched from AliECS
* @param {boolean} [restrictToUser = true] - if the list should be restricted to user permissions only
* @returns {RemoteData<Array<String>>}
*/
getDetectorsAsRemote(restrictToUser = true) {
if (this._listRemote.isSuccess() && restrictToUser) {
if (this.isSingleView()) {
const detectors = this._listRemote.payload.filter((detector) => detector === this._selected);
return RemoteData.success(detectors);
}
}
return this._listRemote;
}
/**
* Method to return a RemoteData object containing list of detectors fetched from AliECS and their availability
* @param {boolean} [restrictToUser = true] - if the list should be restricted to user permissions only
* @param {RemoteData} item - item in which data should be loaded and notified
* @param {typeof Model} that - model that should be notified after a change in data fetching
* @returns {RemoteData<Array<DetectorAvailability>>} - returns the state of the detectors
*/
async getDetectorsAvailabilityAsRemote(restrictToUser = true, item = RemoteData.notAsked(), that = this) {
item = RemoteData.loading();
that.notify();
let {result: {detectors}, ok: detectorsOk} = await this.model.loader.get(`/api/core/detectors`);
const {
result: {detectors: activeDetectors},
ok: detectorsActivityOk
} = await this.model.loader.post(`/api/GetActiveDetectors`);
const isLockDataOk = this.model.lock.padlockState.isSuccess();
if (detectorsOk && detectorsActivityOk && isLockDataOk) {
const padLock = this.model.lock.padlockState.payload;
if (restrictToUser && this.isSingleView()) {
detectors = detectors.filter((detector) => detector === this._selected);
}
/**
* @type {Array<DetectorAvailability>}
*/
const detectorsAvailability = detectors.map((detector) => ({
name: detector,
isActive: activeDetectors.includes(detector),
isLockedBy: padLock.lockedBy[detector],
}));
item = RemoteData.success(detectorsAvailability);
that.notify();
return item;
} else {
item = RemoteData.failure('Unable to fetch information on detectors state');
that.notify();
return item;
}
}
/**
* Method to return a RemoteData object containing list of detectors fetched from AliECS
* @deprecated as it should be using `getDetectorsAsRemote` instead
* @returns {RemoteData<Array<String>>}
*/
get listRemote() {
return this._listRemote;
}
/**
* Return selected detectors
* @return {String}
*/
get selected() {
return this._selected;
}
/**
* Set the selected detectors
* @param {String}
*/
set selected(detector) {
this._selected = detector;
}
/**
* Set the new values for DCS's properties availability which are updated via refresh page
* or websocket messages
* @param {Object<String, Object>} availability - json with detectors availability
* @return {void}
*/
set availability(availability) {
for (const detector of Object.keys(availability)) {
this._availability[detector] = {
pfrAvailability: DetectorState[availability[detector].PfrAvailability] ?? DetectorState.UNDEFINED,
sorAvailability: DetectorState[availability[detector].SorAvailability] ?? DetectorState.UNDEFINED,
}
}
}
/**
* Return an instance of the current detectors availability
*/
get availability() {
return this._availability;
}
/**
* Given a list of detectors, return if all are available for specified property (PFR/SOR)
* @param {Array<String>} detectors - list of detectors to check
* @param {String['pfrAvailability', 'sorAvailability']} property - which property to be checked
* @return {Boolean}
*/
areDetectorsAvailable(detectors, property) {
const state = property === 'pfrAvailability' ? DetectorState.PFR_AVAILABLE : DetectorState.SOR_AVAILABLE;
return detectors.every((detector) => this._availability?.[detector]?.[property] === state);
}
}