-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathFilterService.js
More file actions
169 lines (153 loc) · 5.97 KB
/
FilterService.js
File metadata and controls
169 lines (153 loc) · 5.97 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
/**
* @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 { LogManager } from '@aliceo2/web-ui';
import { RunStatus } from '../../common/library/runStatus.enum.js';
import { wrapRunStatus } from '../dtos/BookkeepingDto.js';
const LOG_FACILITY = `${process.env.npm_config_log_label ?? 'qcg'}/filter-service`;
/**
* High level service that composes, processes and maps data from the bookkeeping service
*/
export class FilterService {
/**
* Creates an instance of FilterService to map and expose data from the bookkeeping service.
* @param {BookkeepingService} bookkeepingService - Low level data provider fetching raw data from the BKP source
* @param {object} config - Config object file that defines the refresh intervals for checking run status and runtypes
*/
constructor(bookkeepingService, config) {
this._logger = LogManager.getLogger(LOG_FACILITY);
this._bookkeepingService = bookkeepingService;
this._runTypes = [];
this._detectors = Object.freeze([]);
this._dataPasses = Object.freeze([]);
this._runTypesRefreshInterval = config?.bookkeeping?.runTypesRefreshInterval ??
(config?.bookkeeping ? 24 * 60 * 60 * 1000 : -1);// default interval is 1 day
this._dataPassesRefreshInterval = config?.bookkeeping?.dataPassesRefreshInterval ??
(config?.bookkeeping ? 6 * 60 * 60 * 1000 : -1);// default interval is 6 hour
this.initFilters().catch((error) => {
this._logger.errorMessage(`FilterService initialization failed: ${error.message || error}`);
});
}
/**
* This method is used to initialize the filter service
* @returns {Promise<void>} - resolves when the filter service is initialized
*/
async initFilters() {
await this.getRunTypes();
await this._initializeDetectors();
await this.getDataPasses();
}
/**
* This method is used to retrieve the list of run types from the bookkeeping service
* @returns {Promise<void>} - resolves when the list of run types is available
*/
async getRunTypes() {
try {
if (!this._bookkeepingService.active) {
return;
}
const rawRunTypes = await this._bookkeepingService.retrieveRunTypes();
this._runTypes = [];
for (const type of rawRunTypes) {
this._runTypes.push(type?.name);
}
this._runTypes.sort();
} catch (error) {
this._logger.errorMessage(`Error while retrieving run types: ${error.message || error}`);
this._runTypes = [];
}
}
/**
* This method is used to retrieve the list of data passes from the bookkeeping service.
* @returns {Promise<void>} Resolves when the list of data passes is available.
*/
async getDataPasses() {
try {
if (!this._bookkeepingService.active) {
return;
}
const rawDataPasses = await this._bookkeepingService.retrieveDataPasses();
this._dataPasses = Object.freeze(rawDataPasses
.filter(({ name, isFrozen }) => name && typeof isFrozen === 'boolean')
.map(({ name, isFrozen }) => Object.freeze({ name, isFrozen })));
} catch (error) {
this._logger.errorMessage(`Error while retrieving data passes: ${error.message || error}`);
}
}
/**
* Returns a list of data passes.
* @returns {Readonly<DataPass[]>} An immutable array of data passes.
*/
get dataPasses() {
return this._dataPasses;
}
/**
* This method is used to retrieve the list of detectors from the bookkeeping service
* @returns {Promise<undefined>} Resolves when the list of detectors is available
*/
async _initializeDetectors() {
if (!this._bookkeepingService?.active) {
return;
}
try {
const detectorSummaries = await this._bookkeepingService.retrieveDetectorSummaries();
this._detectors = Object.freeze(detectorSummaries
.filter(({ name, type }) => name && type)
.map(({ name, type }) => Object.freeze({ name, type })));
} catch (error) {
this._logger.errorMessage(`Failed to retrieve detectors: ${error?.message || error}`);
}
}
/**
* Returns a list of detector summaries.
* @returns {Readonly<DetectorSummary[]>} An immutable array of detector summaries.
*/
get detectors() {
return this._detectors;
}
/**
* Returns the interval in milliseconds for how often the list of run types should be refreshed.
* @returns {number} Interval in milliseconds for refreshing the list of run types.
*/
get runTypesRefreshInterval() {
return this._runTypesRefreshInterval;
}
/**
* Returns the interval in milliseconds for how often the list of data passes should be refreshed.
* @returns {number} Interval in milliseconds for refreshing the list of data passes.
*/
get dataPassesRefreshInterval() {
return this._dataPassesRefreshInterval;
}
/**
* This method is used to initialize the filter service
* @returns {string[]} - resolves when the filter service is initialized
*/
get runTypes() {
return [...this._runTypes];
}
/**
* This method is used to retrieve the run information from the bookkeeping service
* @param {number} runNumber - run number to retrieve the information for
* @returns {Promise<object>} - resolves with the run information
*/
async getRunInformation(runNumber) {
try {
return await this._bookkeepingService.retrieveRunInformation(runNumber);
} catch (error) {
const message = `Error while retrieving run status for run ${runNumber}: ${error.message || error}`;
this._logger.errorMessage(message);
return wrapRunStatus(RunStatus.UNKNOWN);
}
}
}