-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathShiftService.js
More file actions
206 lines (184 loc) · 8.55 KB
/
ShiftService.js
File metadata and controls
206 lines (184 loc) · 8.55 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
/**
* @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.
*/
const { getShiftFromTimestamp, SHIFT_DURATION } = require('./getShiftFromTimestamp.js');
const { logAdapter, environmentAdapter, runAdapter } = require('../../../database/adapters/index.js');
const { getShiftIssues } = require('../eosReport/getShiftIssues.js');
const { ShiftTypes } = require('../../../domain/enums/ShiftTypes.js');
const { getShiftEnvironments } = require('./getShiftEnvironments.js');
const { getShiftTagsFiltersByType } = require('./getShiftTagsFiltersByType.js');
const { getShiftRuns } = require('./getShiftRuns.js');
const { getShiftIssuesTagsOccurrences } = require('../log/getShiftIssuesTagsOccurrences.js');
const { formatEosReportTitle } = require('../eosReport/formatEosReport.js');
const { getLogsByTitle } = require('../log/getLogsByTitle.js');
// Time after the end of the shift during which one the user is allowed to fill the EOS report (currently 30 minutes)
const PENDING_SHIFT_MARGIN = 30 * 60 * 1000;
/**
* @typedef Shift
* @property {number} start the start of the shift (UNIX timestamp in ms)
* @property {number} end the end of the shift (UNIX timestamp in ms)
* @property {'Morning'|'Afternoon'|'Night'} period the period of the shift
*/
/**
* Service to generate end of shift report
*/
class ShiftService {
/**
* Returns the shift of the user for which EOS can be created
*
* @return {Shift} Returns the pending shift (offset by pending shift margin)
*/
getUserPendingShiftOrFail() {
const now = Date.now();
const currentShift = getShiftFromTimestamp(now);
if (now - currentShift.start > PENDING_SHIFT_MARGIN) {
return currentShift;
} else {
return getShiftFromTimestamp(now - SHIFT_DURATION);
}
}
/**
* Return the shift right before the current user's shift
*
* @return {Shift} resolves with the shift before the current user's shift
*/
getUserPreviousShiftOrFail() {
return getShiftFromTimestamp(this.getUserPendingShiftOrFail().start - SHIFT_DURATION);
}
/**
* Return all the data for the pending shift of a specific type for a given user
*
* @param {UserIdentifier} shifterUserIdentifier the identifier of the user for which shift information must be retrieved
* @param {string} shiftType the type of the shift for which information must be retrieved
* @return {Promise<object>} the shift data
*/
async getShiftData(shifterUserIdentifier, shiftType) {
const shift = this.getUserPendingShiftOrFail();
const ret = {
shift,
issuesLogs: await this.getShiftIssues(shift.start, shiftType),
typeSpecific: null,
infoFromPreviousShifter: { value: '' },
};
ret.infoFromPreviousShifter = await this.getInfoFromPreviousShifter(shift, shiftType);
switch (shiftType) {
case ShiftTypes.ECS:
ret.typeSpecific = await this._getEcsShiftData(shift);
break;
case ShiftTypes.QC_PDP:
ret.typeSpecific = await this._getQcPdpShiftData(shift);
break;
case ShiftTypes.SL:
ret.typeSpecific = await this._getShiftLeaderShiftData(shift);
break;
}
return ret;
}
/**
* Retrieves information from the previous End of Shift (EoS) report for a given shift and shift type.
*
* This method searches for EoS reports from the previous shift that match the expected title format for the given shift type.
* It extracts the "For next shifter" section from the report if available, otherwise returns an appropriate error message.
*
* @param {Shift} shift - The current shift for which to retrieve information.
* @param {string} shiftType - The type of the shift.
* @return {Promise<object>} Resolves with an object containing either the retrieved information or an error message:
* - `value`: The extracted information for the next shifter, if available.
* - `errorMessage`: A descriptive error message if no valid information could be retrieved from the previous EoS report.
*/
async getInfoFromPreviousShifter(shift, shiftType) {
const ret = {};
// Find EoS reports from the last shift with the correct title
const pastShift = await this.getUserPreviousShiftOrFail(shifterUserIdentifier);
const expectedPreviousReportTitle = formatEosReportTitle(pastShift, shiftType);
const previousShiftReports = await getLogsByTitle(expectedPreviousReportTitle, { rootOnly: true });
if (previousShiftReports.length === 0) {
ret.errorMessage = `No ${shiftType} EoS reports found from the previous shift`;
} else if (previousShiftReports.length > 1) {
ret.errorMessage = `Multiple ${shiftType} EoS reports found from the previous shift`;
} else {
const eosReportText = previousShiftReports[0].text;
if (!eosReportText) {
ret.errorMessage = 'EoS report from the previous shift is missing the text field';
} else {
const infoFromPreviousShifterMatch = eosReportText.match(/\n\n### For next shifter\n([\S\s]*?)\n\n###/);
if (!infoFromPreviousShifterMatch) {
ret.errorMessage = 'EoS report from the previous shift is missing the information transfer field';
} else {
const [, infoFromPreviousShifter] = infoFromPreviousShifterMatch;
if (infoFromPreviousShifter === '' || infoFromPreviousShifter === '-') {
ret.errorMessage = 'Previous EoS report contains no information for next shifter';
} else {
ret.value = infoFromPreviousShifter;
}
}
}
}
return ret;
}
/**
* Return the log entries related to a given shift and shifter
*
* @param {number} shiftStart the start of the shift (UNIX timestamp in milliseconds)
* @param {string} shiftType the type of the shift for which information must be retrieved
* @return {Promise<Log[]>} resolves with the found issues
*/
async getShiftIssues(shiftStart, shiftType) {
const shift = getShiftFromTimestamp(shiftStart);
return (await getShiftIssues(shift, getShiftTagsFiltersByType(shiftType))).map(logAdapter.toEntity);
}
/**
* For a given shift, returns the ECS shift data
*
* @param {Shift} shift the shift for which data must be fetched
* @return {Promise<{environments: Environment[]}>} the ECS shift data
*/
async _getEcsShiftData(shift) {
return {
environments: (await getShiftEnvironments(shift, ShiftTypes.ECS))
.map((environment) => environmentAdapter.toEntity(environment, false)),
};
}
/**
* For a given shift, returns the QC/PDP shift data
*
* @param {Shift} shift the shift for which data must be fetched
* @return {Promise<{runs: Object<string, Run[]>}>} the QC/PDP shift data
* @private
*/
async _getQcPdpShiftData(shift) {
const runsByDefinition = {};
for (const [definition, runs] of Object.entries(await getShiftRuns(shift, ShiftTypes.QC_PDP))) {
runsByDefinition[definition] = runs.map(runAdapter.toEntity);
}
return { runs: runsByDefinition };
}
/**
* For a given shift, returns the Shift Leader shift data
*
* @param {Shift} shift the shift for which data must be fetched
* @return {Promise<{runs: Object<string, Run[]>}>} the Shift Leader shift data
* @private
*/
async _getShiftLeaderShiftData(shift) {
const runsByDefinition = {};
for (const [definition, runs] of Object.entries(await getShiftRuns(shift, ShiftTypes.SL))) {
runsByDefinition[definition] = runs.map(runAdapter.toEntity);
}
return {
runs: runsByDefinition,
tagsCounters: await getShiftIssuesTagsOccurrences(shift),
};
}
}
exports.ShiftService = ShiftService;
exports.shiftService = new ShiftService();