Skip to content

Commit 31c5ab4

Browse files
mpfeilumut0
andauthored
Add count parameter for getLatestMeasurements (#588)
* Add count parameter for getLatestMeasurements * Add projection and restructure lastMeasurements * add tests and doc for count measurement feature Co-authored-by: Umut Tas <[email protected]>
1 parent a2d0b28 commit 31c5ab4

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ services:
1515
# - ./dumps/measurements:/exports/measurements
1616
- ./.scripts/mongodb/osem_admin.sh:/docker-entrypoint-initdb.d/osem_admin.sh
1717
# - ./.scripts/mongodb/osem_seed_boxes.sh:/docker-entrypoint-initdb.d/osem_seed_boxes.sh
18-
# - ./.scripts/mongodb/osem_seed_measurements.sh:/docker-entrypoint-initdb.d/osem_seed_measurements.sh
18+
# - ./.scripts/mongodb/osem_seed_measurements.sh:/docker-entrypoint-initdb.d/osem_seed_measurements.sh

packages/api/lib/controllers/measurementsController.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const
1919
* @apiGroup Measurements
2020
* @apiName getLatestMeasurements
2121
* @apiUse BoxIdParam
22+
* @apiParam {NumberNumber=1-100} [count] Number of measurements to be retrieved for every sensor.
2223
*/
2324
/**
2425
* @api {get} /boxes/:senseBoxId/sensors/:sensorId Get latest measurements of a sensor
@@ -35,7 +36,30 @@ const getLatestMeasurements = async function getLatestMeasurements (req, res, ne
3536
let box;
3637

3738
try {
38-
box = await Box.findBoxById(req._userParams.boxId, { onlyLastMeasurements: true });
39+
if (req._userParams.count) {
40+
box = await Box.findBoxById(req._userParams.boxId, {
41+
populate: false,
42+
onlyLastMeasurements: false,
43+
count: req._userParams.count,
44+
projection: {
45+
name: 1,
46+
lastMeasurementAt: 1,
47+
sensors: 1,
48+
grouptag: 1
49+
}
50+
});
51+
52+
const measurements = await Measurement.findLatestMeasurementsForSensorsWithCount(box, req._userParams.count);
53+
for (let index = 0; index < box.sensors.length; index++) {
54+
const sensor = box.sensors[index];
55+
const values = measurements.find(elem => elem._id.equals(sensor._id));
56+
sensor['lastMeasurements'] = values;
57+
}
58+
} else {
59+
box = await Box.findBoxById(req._userParams.boxId, {
60+
onlyLastMeasurements: true
61+
});
62+
}
3963
} catch (err) {
4064
handleError(err, next);
4165

@@ -413,6 +437,7 @@ module.exports = {
413437
retrieveParameters([
414438
{ predef: 'boxId', required: true },
415439
{ predef: 'sensorId' },
440+
{ name: 'count', dataType: 'Integer', min: 1, max: 100, required: false },
416441
{ name: 'onlyValue', required: false }
417442
]),
418443
getLatestMeasurements

packages/models/src/measurement/measurement.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,59 @@ measurementSchema.statics.findLatestMeasurementsForSensors = function findLatest
8686
.exec();
8787
};
8888

89+
measurementSchema.statics.findLatestMeasurementsForSensorsWithCount = function findLatestMeasurementsForSensorsWithCount (box, count) {
90+
const match = {
91+
$or: []
92+
};
93+
for (let index = 0; index < box.sensors.length; index++) {
94+
const sensor = box.sensors[index];
95+
match.$or.push({
96+
sensor_id: sensor._id
97+
});
98+
}
99+
100+
return this.aggregate([
101+
{
102+
$match: match,
103+
},
104+
{
105+
$sort: {
106+
createdAt: -1,
107+
},
108+
},
109+
{
110+
$limit: count * box.sensors.length,
111+
},
112+
{
113+
$group: {
114+
_id: '$sensor_id',
115+
measurements: {
116+
$push: {
117+
value: '$value',
118+
createdAt: '$createdAt',
119+
}
120+
},
121+
fromDate: {
122+
$last: '$createdAt',
123+
},
124+
toDate: {
125+
$first: '$createdAt',
126+
},
127+
},
128+
},
129+
{
130+
$project: {
131+
_id: 1,
132+
measurements: {
133+
$slice: ['$measurements', count],
134+
},
135+
fromDate: 1,
136+
toDate: 1,
137+
},
138+
},
139+
]).exec();
140+
};
141+
89142
measurementSchema.statics.getMeasurementsStream = function getMeasurementsStream ({ fromDate, toDate, sensorId }) {
90143
const queryLimit = 10000;
91144

tests/tests/007-download-data-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ describe('downloading data', function () {
194194
});
195195
});
196196

197+
it('should return the sensors of a box with 3 measurements for /boxes/:boxid/sensors?count=3 GET', function () {
198+
return chakram.get(`${BASE_URL}/boxes/${boxIds[0]}/sensors?count=3`)
199+
.then(function (response) {
200+
expect(response).to.have.status(200);
201+
expect(response).to.have.header('content-type', 'application/json; charset=utf-8');
202+
expect(response).to.have.schema(boxSensorsSchema);
203+
expect(response.body.sensors[0].lastMeasurements.measurements.length).to.be.equal(3);
204+
205+
return chakram.wait();
206+
});
207+
});
208+
197209
it('should return a single sensor of a box for /boxes/:boxid/sensors/:sensorId GET', function () {
198210
return chakram.get(`${BASE_URL}/boxes/${boxes[0]._id}/sensors/${boxes[0].sensors[0]._id}`)
199211
.then(function (response) {

0 commit comments

Comments
 (0)