Skip to content

Commit d580633

Browse files
Merge branch 'main' into improv/O2B-1535/Create-a-timeRange-filter-function-for-getAll-usecases
2 parents 4bd60c6 + 55062b6 commit d580633

7 files changed

Lines changed: 323 additions & 10 deletions

File tree

lib/domain/dtos/filters/LhcFillsFilterDto.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Joi = require('joi');
1414
const { validateRange, RANGE_INVALID } = require('../../../utilities/rangeUtils');
1515
const { validateBeamTypes, BEAM_TYPE_INVALID } = require('../../../utilities/beamTypeUtils');
1616
const { validateTimeDuration } = require('../../../utilities/validateTime');
17+
const { FromToFilterDto } = require('./FromToFilterDto.js');
1718

1819
exports.LhcFillsFilterDto = Joi.object({
1920
hasStableBeams: Joi.boolean(),
@@ -23,6 +24,8 @@ exports.LhcFillsFilterDto = Joi.object({
2324
}),
2425
runDuration: validateTimeDuration,
2526
beamDuration: validateTimeDuration,
27+
stableBeamsStart: FromToFilterDto,
28+
stableBeamsEnd: FromToFilterDto,
2629
schemeName: Joi.string().trim().max(64),
2730
beamTypes: Joi.string()
2831
.trim()

lib/public/views/LhcFills/ActiveColumns/lhcFillsActiveColumns.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { fillNumberFilter } from '../../../components/Filters/LhcFillsFilter/fil
2828
import { durationFilter } from '../../../components/Filters/LhcFillsFilter/durationFilter.js';
2929
import { beamTypeFilter } from '../../../components/Filters/LhcFillsFilter/beamTypeFilter.js';
3030
import { schemeNameFilter } from '../../../components/Filters/LhcFillsFilter/schemeNameFilter.js';
31+
import { timeRangeFilter } from '../../../components/Filters/common/filters/timeRangeFilter.js';
3132

3233
/**
3334
* List of active columns for a lhc fills table
@@ -65,6 +66,14 @@ export const lhcFillsActiveColumns = {
6566
visible: true,
6667
size: 'w-8',
6768
format: (timestamp) => formatTimestamp(timestamp, false),
69+
70+
/**
71+
* Stable Beam start filter component
72+
*
73+
* @param {RunsOverviewModel} lhcFillsOverviewModel the lhcFills overview model
74+
* @return {Component} the filter component
75+
*/
76+
filter: (lhcFillsOverviewModel) => timeRangeFilter(lhcFillsOverviewModel.filteringModel.get('stableBeamsStart').timeRangeInputModel),
6877
profiles: {
6978
lhcFill: true,
7079
environment: true,
@@ -80,6 +89,14 @@ export const lhcFillsActiveColumns = {
8089
visible: true,
8190
size: 'w-8',
8291
format: (timestamp) => formatTimestamp(timestamp, false),
92+
93+
/**
94+
* Stable Beam end filter component
95+
*
96+
* @param {LhcFillsOverviewModel} lhcFillsOverviewModel the lhcFills overview model
97+
* @return {Component} the filter component
98+
*/
99+
filter: (lhcFillsOverviewModel) => timeRangeFilter(lhcFillsOverviewModel.filteringModel.get('stableBeamsEnd').timeRangeInputModel),
83100
profiles: {
84101
lhcFill: true,
85102
environment: true,

lib/public/views/LhcFills/Overview/LhcFillsOverviewModel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { OverviewPageModel } from '../../../models/OverviewModel.js';
1919
import { addStatisticsToLhcFill } from '../../../services/lhcFill/addStatisticsToLhcFill.js';
2020
import { BeamTypeFilterModel } from '../../../components/Filters/LhcFillsFilter/BeamTypeFilterModel.js';
2121
import { TextComparisonFilterModel } from '../../../components/Filters/common/filters/TextComparisonFilterModel.js';
22+
import { TimeRangeFilterModel } from '../../../components/Filters/RunsFilter/TimeRangeFilter.js';
2223

2324
/**
2425
* Model for the LHC fills overview page
@@ -39,6 +40,8 @@ export class LhcFillsOverviewModel extends OverviewPageModel {
3940
beamDuration: new TextComparisonFilterModel(),
4041
runDuration: new TextComparisonFilterModel(),
4142
hasStableBeams: new StableBeamFilterModel(),
43+
stableBeamsStart: new TimeRangeFilterModel(),
44+
stableBeamsEnd: new TimeRangeFilterModel(),
4245
beamTypes: new BeamTypeFilterModel(),
4346
schemeName: new RawTextFilterModel(),
4447
});

lib/usecases/lhcFill/GetAllLhcFillsUseCase.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,24 @@ class GetAllLhcFillsUseCase {
4747
let associatedStatisticsRequired = false;
4848

4949
if (filter) {
50-
const { hasStableBeams, fillNumbers, schemeName, beamDuration, runDuration, beamTypes } = filter;
50+
const { hasStableBeams, fillNumbers, schemeName, beamDuration, stableBeamsStart, stableBeamsEnd, runDuration, beamTypes } = filter;
5151
if (hasStableBeams) {
5252
// For now, if a stableBeamsStart is present, then a beam is stable
5353
queryBuilder.where('stableBeamsStart').not().is(null);
5454
}
5555

56+
if (stableBeamsStart) {
57+
const from = stableBeamsStart.from !== undefined ? stableBeamsStart.from : 0;
58+
const to = stableBeamsStart.to !== undefined ? stableBeamsStart.to : new Date().getTime();
59+
queryBuilder.where('stableBeamsStart').between(from, to);
60+
}
61+
62+
if (stableBeamsEnd) {
63+
const from = stableBeamsEnd.from !== undefined ? stableBeamsEnd.from : 0;
64+
const to = stableBeamsEnd.to !== undefined ? stableBeamsEnd.to : new Date().getTime();
65+
queryBuilder.where('stableBeamsEnd').between(from, to);
66+
}
67+
5668
if (fillNumbers) {
5769
const fillNumberCriteria = splitStringToStringsTrimmed(fillNumbers, SEARCH_ITEMS_SEPARATOR);
5870

test/api/lhcFills.test.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ module.exports = () => {
503503
});
504504
});
505505

506+
506507
it('should return 200 and an LHCFill array for runs duration filter, > 03:00:00', (done) => {
507508
request(server)
508509
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[runDuration][operator]=>&filter[runDuration][limit]=03:00:00')
@@ -519,6 +520,177 @@ module.exports = () => {
519520
done();
520521
});
521522
});
523+
524+
it('should return 400 when stableBeamEnd filter "from" is greater than the current time', (done) => {
525+
request(server)
526+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsEnd][from]=2647867600000')
527+
.expect(400)
528+
.end((err, res) => {
529+
if (err) {
530+
done(err);
531+
return;
532+
}
533+
534+
const { errors: [error] } = res.body;
535+
expect(error.title).to.equal('Invalid Attribute');
536+
expect(error.detail).to.equal('"query.filter.stableBeamsEnd.from" must be less than "now"');
537+
done()
538+
});
539+
});
540+
541+
it('should return 400 when stableBeamStart filter "from" is greater than the current time', (done) => {
542+
request(server)
543+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][from]=2647867600000')
544+
.expect(400)
545+
.end((err, res) => {
546+
if (err) {
547+
done(err);
548+
return;
549+
}
550+
551+
const { errors: [error] } = res.body;
552+
expect(error.title).to.equal('Invalid Attribute');
553+
expect(error.detail).to.equal('"query.filter.stableBeamsStart.from" must be less than "now"');
554+
done()
555+
});
556+
});
557+
558+
it('should return 400 when stableBeamEnd filter "from" is greater than "to"', (done) => {
559+
request(server)
560+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsEnd][from]=1647867699999&filter[stableBeamsEnd][to]=1647867600000')
561+
.expect(400)
562+
.end((err, res) => {
563+
if (err) {
564+
done(err);
565+
return;
566+
}
567+
568+
const { errors: [error] } = res.body;
569+
expect(error.title).to.equal('Invalid Attribute');
570+
expect(error.detail).to.equal('"query.filter.stableBeamsEnd.to" must be greater than "ref:from"');
571+
done()
572+
});
573+
});
574+
575+
it('should return 400 when stableBeamStart filters are strings', (done) => {
576+
request(server)
577+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][from]=bogus&filter[stableBeamsStart][to]=bogus')
578+
.expect(400)
579+
.end((err, res) => {
580+
if (err) {
581+
done(err);
582+
return;
583+
}
584+
585+
const { errors } = res.body;
586+
587+
expect(errors.map(e => e.detail)).to.have.members([
588+
'"query.filter.stableBeamsStart.from" must be a valid date',
589+
'"query.filter.stableBeamsStart.to" must be a valid date',
590+
]);
591+
592+
expect(errors.every(e => e.title === 'Invalid Attribute')).to.be.true;
593+
done()
594+
});
595+
});
596+
597+
it('should return 400 when stableBeamEnd filters are strings', (done) => {
598+
request(server)
599+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsEnd][from]=bogus&filter[stableBeamsEnd][to]=bogus')
600+
.expect(400)
601+
.end((err, res) => {
602+
if (err) {
603+
done(err);
604+
return;
605+
}
606+
607+
const { errors } = res.body;
608+
609+
expect(errors.map(e => e.detail)).to.have.members([
610+
'"query.filter.stableBeamsEnd.from" must be a valid date',
611+
'"query.filter.stableBeamsEnd.to" must be a valid date',
612+
]);
613+
614+
expect(errors.every(e => e.title === 'Invalid Attribute')).to.be.true;
615+
done()
616+
});
617+
});
618+
619+
it('should return 400 when stableBeamStart filter "from" is greater than "to"', (done) => {
620+
request(server)
621+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][from]=1647867699999&filter[stableBeamsStart][to]=1647867600000')
622+
.expect(400)
623+
.end((err, res) => {
624+
if (err) {
625+
done(err);
626+
return;
627+
}
628+
629+
const { errors: [error] } = res.body;
630+
expect(error.title).to.equal('Invalid Attribute');
631+
expect(error.detail).to.equal('"query.filter.stableBeamsStart.to" must be greater than "ref:from"');
632+
done()
633+
});
634+
});
635+
636+
it('should return 200 and a LHCFill array for only "from" filters set for stableBeamStart and end', (done) => {
637+
const fromValue = 1647867600000;
638+
639+
request(server)
640+
.get(`/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][from]=${fromValue}&filter[stableBeamsEnd][from]=${fromValue}`)
641+
.expect(200)
642+
.end((err, res) => {
643+
if (err) {
644+
done(err);
645+
return;
646+
}
647+
648+
expect(res.body.data).to.have.lengthOf(3);
649+
res.body.data.forEach(fill => {
650+
expect(fill.stableBeamsStart).to.be.at.least(fromValue);
651+
expect(fill.stableBeamsEnd).to.be.at.least(fromValue);
652+
});
653+
654+
done();
655+
});
656+
});
657+
658+
it('should return 200 and a LHCFill array for only "to" filters set for stableBeamStart and end', (done) => {
659+
const toValue = 2000000000000;
660+
661+
request(server)
662+
.get(`/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][to]=${toValue}&filter[stableBeamsEnd][to]=${toValue}`)
663+
.expect(200)
664+
.end((err, res) => {
665+
if (err) {
666+
done(err);
667+
return;
668+
}
669+
670+
expect(res.body.data).to.have.lengthOf(4);
671+
672+
res.body.data.forEach(fill => {
673+
expect(fill.stableBeamsStart).to.be.at.most(toValue);
674+
expect(fill.stableBeamsEnd).to.be.at.most(toValue);
675+
});
676+
done();
677+
});
678+
});
679+
680+
it('should return 200 and a LHCFill array for stableBeamStart and end filter set', (done) => {
681+
request(server)
682+
.get('/api/lhcFills?page[offset]=0&page[limit]=15&filter[stableBeamsStart][from]=1647867600000&filter[stableBeamsStart][to]=1647867600001&filter[stableBeamsEnd][from]=1647961200000&filter[stableBeamsEnd][to]=1647961200001')
683+
.expect(200)
684+
.end((err, res) => {
685+
if (err) {
686+
done(err);
687+
return;
688+
}
689+
690+
expect(res.body.data).to.have.lengthOf(3);
691+
done();
692+
});
693+
});
522694

523695
it('should return 200 and an LHCFill array for beam types filter, correct', (done) => {
524696
request(server)

test/lib/usecases/lhcFill/GetAllLhcFillsUseCase.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,60 @@ module.exports = () => {
317317

318318
expect(lhcFills).to.be.an('array').and.lengthOf(0)
319319
})
320+
321+
it('should return an array with only \'from\' values given', async () => {
322+
getAllLhcFillsDto.query = {
323+
filter: {
324+
stableBeamsStart: {
325+
from: 1647867600000,
326+
},
327+
stableBeamsEnd: {
328+
from: 1647867600000,
329+
},
330+
},
331+
};
332+
333+
const { lhcFills } = await new GetAllLhcFillsUseCase().execute(getAllLhcFillsDto);
334+
335+
expect(lhcFills).to.be.an('array');
336+
expect(lhcFills).to.have.lengthOf(3);
337+
});
338+
339+
it('should return an array with only \'to\' values given', async () => {
340+
getAllLhcFillsDto.query = {
341+
filter: {
342+
stableBeamsStart: {
343+
to: 2000000000000
344+
},
345+
stableBeamsEnd: {
346+
to: 2000000000000
347+
},
348+
},
349+
};
350+
351+
const { lhcFills } = await new GetAllLhcFillsUseCase().execute(getAllLhcFillsDto);
352+
353+
expect(lhcFills).to.be.an('array');
354+
expect(lhcFills).to.have.lengthOf(4);
355+
});
356+
357+
it('should return an array with fills on certain timestamps', async () => {
358+
getAllLhcFillsDto.query = {
359+
filter: {
360+
stableBeamsStart: {
361+
from: 1647867600000,
362+
to: 1647867600000,
363+
},
364+
stableBeamsEnd: {
365+
from: 1647961200000,
366+
to: 1647961200000,
367+
},
368+
},
369+
};
370+
371+
const { lhcFills } = await new GetAllLhcFillsUseCase().execute(getAllLhcFillsDto);
372+
373+
expect(lhcFills).to.be.an('array');
374+
expect(lhcFills).to.have.lengthOf(3);
375+
});
320376
};

0 commit comments

Comments
 (0)