Skip to content

Commit 8db009c

Browse files
committed
Add endpoint for fetching all beam modes
1 parent 0a110ef commit 8db009c

8 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
/**
15+
* @typedef SequelizeBeamMode
16+
*
17+
* BeamMode is currently a string column in the runs table, but we define this typedef as if it were a full model for future use.
18+
* BeamMode should become independent entity in the future. [O2B-1499]
19+
*
20+
* @property {string} name
21+
*/

lib/server/controllers/runs.controller.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const { dtoValidator } = require('../utilities');
3737
const { ApiConfig } = require('../../config/index.js');
3838
const { DtoFactory } = require('../../domain/dtos/DtoFactory.js');
3939
const { runService } = require('../services/run/RunService.js');
40+
const { getAllBeamModes } = require('../services/beam/getAllBeamModes.js');
4041
const { updateExpressResponseFromNativeError } = require('../express/updateExpressResponseFromNativeError.js');
4142
const { runToHttpView } = require('./runsToHttpView.js');
4243

@@ -297,6 +298,31 @@ const listReasonTypes = async (_request, response, _next) => {
297298
}
298299
};
299300

301+
/**
302+
* Retrieve a list of unique beam modes
303+
*
304+
* @param {Object} _request The *request* object represents the HTTP request and has properties for the request query
305+
* string, parameters, body, HTTP headers, and so on.
306+
* @param {Object} response The *response* object represents the HTTP response that an Express app sends when it gets
307+
* an
308+
* HTTP request.
309+
* @param {Function} _next The *next* object represents the next middleware function which is used to pass control to
310+
* the next middleware function.
311+
* @returns {undefined}
312+
*/
313+
const listBeamModes = async (_request, response, _next) => {
314+
try {
315+
const beamModes = await getAllBeamModes();
316+
if (beamModes && beamModes.length > 0) {
317+
response.status(200).json({ data: beamModes });
318+
} else {
319+
response.status(204).json({ data: [] });
320+
}
321+
} catch {
322+
response.status(502).json({ errors: ['Unable to retrieve list of beam modes'] });
323+
}
324+
};
325+
300326
// eslint-disable-next-line jsdoc/require-param
301327
/**
302328
* Retrieve distinct combination of levels of alice L3 and dipole current rounded to kilo amperes
@@ -323,6 +349,7 @@ module.exports = {
323349
getLogsByRunNumberHandler,
324350
getFlpsByRunNumberHandler,
325351
listReasonTypes,
352+
listBeamModes,
326353
listRuns,
327354
startRun,
328355
updateRunByRunNumber,

lib/server/routers/runs.router.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module.exports = {
2323
path: 'reasonTypes',
2424
controller: RunsController.listReasonTypes,
2525
},
26+
{
27+
method: 'get',
28+
path: 'beamModes',
29+
controller: RunsController.listBeamModes,
30+
},
2631
{
2732
method: 'get',
2833
controller: RunsController.listRuns,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
const { repositories: { RunRepository }, sequelize } = require('../../../database');
15+
const { Op } = require('sequelize');
16+
17+
/**
18+
* Return the list of all the available detectors
19+
*
20+
* @returns {Promise<SequelizeBeamMode[]>} Promise resolving with the list of beam modes
21+
*/
22+
exports.getAllBeamModes = async () => {
23+
const t = await RunRepository.findAll({
24+
where: {
25+
lhc_beam_mode: {
26+
[Op.ne]: null,
27+
},
28+
},
29+
attributes: [[sequelize.fn('DISTINCT', sequelize.col('lhc_beam_mode')), 'name']],
30+
});
31+
return t;
32+
};

test/api/runs.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,19 @@ module.exports = () => {
642642
});
643643
});
644644

645+
646+
describe('GET /api/runs/beamModes', () => {
647+
it('should successfully return status 200 and list of beam modes', async () => {
648+
const { body } = await request(server)
649+
.get('/api/runs/beamModes')
650+
.expect(200);
651+
652+
expect(body.data).to.be.an('array');
653+
expect(body.data).to.have.lengthOf(4);
654+
655+
expect(body.data[0].name).to.equal('STABLE BEAMS');
656+
});
657+
});
645658
describe('GET /api/runs/reasonTypes', () => {
646659
it('should successfully return status 200 and list of reason types', async () => {
647660
const { body } = await request(server)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
const { expect } = require('chai');
15+
const { getAllBeamModes } = require('../../../../../lib/server/services/beam/getAllBeamModes.js');
16+
17+
module.exports = () => {
18+
it('should successfully return the full list of not null beam modes from runs table', async () => {
19+
const beamModes = await getAllBeamModes();
20+
expect(beamModes.map(({ dataValues: { name } }) => ({ name }))).to.deep.eq([
21+
{ name: 'STABLE BEAMS' },
22+
{ name: 'STABLE' },
23+
{ name: 'NO BEAM' },
24+
{ name: 'UNSTABLE BEAMS' },
25+
]);
26+
});
27+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @license
3+
* Copyright CERN and copyright holders of ALICE O2. This software is
4+
* distributed under the terms of the GNU General Public License v3 (GPL
5+
* Version 3), copied verbatim in the file "COPYING".
6+
*
7+
* See http://alice-o2.web.cern.ch/license for full licensing information.
8+
*
9+
* In applying this license CERN does not waive the privileges and immunities
10+
* granted to it by virtue of its status as an Intergovernmental Organization
11+
* or submit itself to any jurisdiction.
12+
*/
13+
14+
const getAllBeamModes = require('./getAllBeamModes.test.js');
15+
16+
module.exports = () => {
17+
describe('getAllBeamModes', getAllBeamModes);
18+
};

test/lib/server/services/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
const { resetDatabaseContent } = require('../../../utilities/resetDatabaseContent.js');
1515

16+
const BeamModeSuite = require('./beamMode/index.js');
1617
const DetectorSuite = require('./detector/index.js');
1718
const DplSuite = require('./dpl/index.js');
1819
const Environment = require('./environment/index.js');
@@ -38,6 +39,7 @@ module.exports = () => {
3839

3940
after(resetDatabaseContent);
4041

42+
describe('BeamMode', BeamModeSuite);
4143
describe('Detector', DetectorSuite);
4244
describe('DPL', DplSuite);
4345
describe('Environment', Environment);

0 commit comments

Comments
 (0)