-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdataPasses.controller.js
More file actions
190 lines (181 loc) · 6.81 KB
/
dataPasses.controller.js
File metadata and controls
190 lines (181 loc) · 6.81 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
/**
* @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.
*/
/* eslint-disable jsdoc/require-param */
const Joi = require('joi');
const { ApiConfig } = require('../../config/index.js');
const { DtoFactory, tokenSchema } = require('../../domain/dtos/DtoFactory');
const { dataPassService } = require('../services/dataPasses/DataPassService.js');
const { dtoValidator } = require('../utilities/dtoValidator.js');
const { countedItemsToHttpView } = require('../utilities/countedItemsToHttpView.js');
const { updateExpressResponseFromNativeError } = require('../express/updateExpressResponseFromNativeError');
const PaginationDto = require('../../domain/dtos/PaginationDto.js');
const { NON_PHYSICS_PRODUCTIONS_NAMES_WORDS } = require('../../domain/enums/NonPhysicsProductionsNamesWords.js');
/**
* List All DataPasses with statistics
*/
const listDataPassesHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.queryOnly({
filter: {
simulationPassIds: Joi.array().items(Joi.number()),
lhcPeriodIds: Joi.array().items(Joi.number()),
ids: Joi.array().items(Joi.number()),
names: Joi.array().items(Joi.string()),
// 'debug,test' or the reverse have a length of 10
permittedNonPhysicsNames: Joi.string().max(10).custom((value, helper) => {
const nameTokens = value.split(',');
const allTokensCorrect = nameTokens.every((token) => NON_PHYSICS_PRODUCTIONS_NAMES_WORDS.includes(token));
if (!allTokensCorrect) {
return helper.error(`All permittedNonPhysicsNames must comma delimited list of ${NON_PHYSICS_PRODUCTIONS_NAMES_WORDS}`);
}
return nameTokens;
}),
},
page: PaginationDto,
sort: DtoFactory.order(['id', 'name']),
}),
req,
res,
);
if (validatedDTO) {
try {
const { filter, page: { limit = ApiConfig.pagination.limit, offset } = {}, sort = { name: 'DESC' } } = validatedDTO.query;
const { count, rows: items } = await dataPassService.getAll({
filter,
limit,
offset,
sort,
});
res.json(countedItemsToHttpView({ count, items }, limit));
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
/**
* Freeze the given data pass
*/
const freezeHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.queryOnly({ dataPassId: Joi.number().required() }),
req,
res,
);
if (validatedDTO) {
try {
await dataPassService.setFrozenState({ id: validatedDTO.query.dataPassId }, true);
res.sendStatus(204);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
/**
* Un-freeze the given data pass
* @param {import('express').Request} req the request object
* @param {import('express').Response} res the response object
* @returns {Promise<void>} resolves once the operation is done
*/
const unfreezeHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.queryOnly({ dataPassId: Joi.number().required() }),
req,
res,
);
if (validatedDTO) {
try {
await dataPassService.setFrozenState({ id: validatedDTO.query.dataPassId }, false);
res.sendStatus(204);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
/**
* Set given data pass (for PROTON_PROTON runs) as skimmable
* @param {import('express').Request} req the request object
* @param {import('express').Response} res the response object
* @returns {Promise<void>} resolves once the operation is done
*/
const markAsSkimmableHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.queryOnly({ dataPassId: Joi.number().required() }),
req,
res,
);
if (validatedDTO) {
try {
await dataPassService.markAsSkimmable({ id: validatedDTO.query.dataPassId });
res.sendStatus(204);
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
/**
* Fetch skimmable runs list with information whether they are ready for skimming
* @param {import('express').Request} req the request object
* @param {import('express').Response} res the response object
* @returns {Promise<void>} resolves once the operation is done
*/
const fetchSkimmableRunsHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
DtoFactory.queryOnly({ dataPassId: Joi.number().required() }),
req,
res,
);
if (validatedDTO) {
try {
const data = await dataPassService.getSkimmableRuns({ id: validatedDTO.query.dataPassId });
res.json({ data });
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
/**
* Update ready_for_skimming status of given runs
* @param {import('express').Request} req the request object
* @param {import('express').Response} res the response object
* @returns {Promise<void>} resolves once the operation is done
*/
const updateReadyForSkimmingRunsHandler = async (req, res) => {
const validatedDTO = await dtoValidator(
Joi.object({
query: tokenSchema.concat(Joi.object({ dataPassId: Joi.number().required() })),
body: Joi.object({ data: Joi.array().items(Joi.object({
runNumber: Joi.number().integer().positive().required(),
readyForSkimming: Joi.boolean().required().allow(null),
})).required() }).required(),
params: Joi.object({}),
}),
req,
res,
);
if (validatedDTO) {
try {
const data = await dataPassService.updateReadyForSkimmingRuns({ id: validatedDTO.query.dataPassId }, validatedDTO.body.data);
res.json({ data });
} catch (error) {
updateExpressResponseFromNativeError(res, error);
}
}
};
exports.DataPassesController = {
listDataPassesHandler,
freezeHandler,
unfreezeHandler,
markAsSkimmableHandler,
fetchSkimmableRunsHandler,
updateReadyForSkimmingRunsHandler,
};