From a32331b98941a44d5a59bf73ecc898ccd7068c37 Mon Sep 17 00:00:00 2001 From: Thomas Mauran Date: Thu, 26 Dec 2024 17:07:57 +0100 Subject: [PATCH 1/2] fix: lastMeasuredAt --- lib/modules/measure/MeasureService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/measure/MeasureService.ts b/lib/modules/measure/MeasureService.ts index eaf1c041..321cc548 100644 --- a/lib/modules/measure/MeasureService.ts +++ b/lib/modules/measure/MeasureService.ts @@ -513,7 +513,7 @@ export class MeasureService extends BaseService { device._source.measures = {}; } - let lastMeasuredAt = 0; + let lastMeasuredAt = device._source.lastMeasuredAt ?? 0; for (const measurement of measurements) { if (measurement.origin.type !== "device") { From be75e4cc0b379b624ddb2b096041d24d382d4e02 Mon Sep 17 00:00:00 2001 From: Quentin Date: Tue, 31 Dec 2024 11:24:41 +0100 Subject: [PATCH 2/2] chore(tests): add tests and apply fix to assets --- lib/modules/measure/MeasureService.ts | 2 +- .../pipeline-process-before.test.ts | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/modules/measure/MeasureService.ts b/lib/modules/measure/MeasureService.ts index 321cc548..fe4a1949 100644 --- a/lib/modules/measure/MeasureService.ts +++ b/lib/modules/measure/MeasureService.ts @@ -564,7 +564,7 @@ export class MeasureService extends BaseService { asset._source.measures = {}; } - let lastMeasuredAt = 0; + let lastMeasuredAt = asset._source.lastMeasuredAt ?? 0; for (const measurement of measurements) { if (measurement.origin.type === "computed") { diff --git a/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts b/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts index c9968aca..4ef58c0d 100644 --- a/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts +++ b/tests/scenario/modules/ingestion-pipeline/pipeline-process-before.test.ts @@ -78,4 +78,69 @@ describe("Ingestion Pipeline: process before", () => { expect(asset._source.lastMeasuredAt).toBe(1680096420000); }); + + it("should not update lastMeasuredAt if measures are older than current lastMeasure", async () => { + const now = Date.now(); + await sendDummyTempPositionPayloads(sdk, [ + { + deviceEUI: "linked2", + temperature: { + value: 21, + measuredAt: now, + }, + location: { + value: { + lat: 21, + lon: 21, + }, + measuredAt: now, + }, + }, + ]); + + let device = await sdk.document.get( + "engine-ayse", + "devices", + "DummyTempPosition-linked2", + ); + expect(device._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + let asset = await sdk.document.get( + "engine-ayse", + "assets", + "Container-linked2", + ); + expect(asset._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + + await sendDummyTempPositionPayloads(sdk, [ + { + deviceEUI: "linked2", + temperature: { + value: 21, + measuredAt: now - 100000, + }, + location: { + value: { + lat: 21, + lon: 21, + }, + measuredAt: now - 100000, + }, + }, + ]); + device = await sdk.document.get( + "engine-ayse", + "devices", + "DummyTempPosition-linked2", + ); + + expect(device._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + + asset = await sdk.document.get( + "engine-ayse", + "assets", + "Container-linked2", + ); + + expect(asset._source.lastMeasuredAt).toBeGreaterThanOrEqual(now); + }); });