Skip to content

Commit

Permalink
only update devices whose lastActive timestamp is older than the INAC…
Browse files Browse the repository at this point in the history
…TIVE_THRESHOLD
  • Loading branch information
Baalmart committed Oct 10, 2024
1 parent 45b0a60 commit d77a0d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
33 changes: 27 additions & 6 deletions src/device-registry/bin/jobs/test/ut_v2-store-readings-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,28 @@ describe("new-store-readings-job", () => {
logTextStub.restore();
});

it("should mark devices as offline if not in fetched measurements", async () => {
// Mock data with one active device ID
it("should mark devices as offline if not in fetched measurements and lastActive is older than threshold", async () => {
// Mock data with one active device ID and its lastActive time
const mockData = [
{ data: [{ site_id: "123", device_id: "456", time: new Date() }] },
];

// Stubs for Event and Device Models
// Setting up a device that should be marked offline
const inactiveDeviceId = "789";
const inactiveDeviceLastActive = moment()
.subtract(INACTIVE_THRESHOLD + 1000, "milliseconds") // Older than threshold
.toDate();

// Stubbing DeviceModel to return an inactive device
const findStub = sinon.stub(DeviceModel, "airqo").returns({
find: sinon
.stub()
.resolves([
{ _id: inactiveDeviceId, lastActive: inactiveDeviceLastActive },
]),
});

// Stubbing EventModel for fetching events
const fetchStub = sinon
.stub()
.resolves({ success: true, data: mockData });
Expand All @@ -205,7 +220,6 @@ describe("new-store-readings-job", () => {

// Stubbing Device Model for offline updates
const deviceUpdateManyStub = sinon.stub().resolves({});

sandbox
.stub(DeviceModel, "airqo")
.returns({ updateMany: deviceUpdateManyStub });
Expand All @@ -215,9 +229,16 @@ describe("new-store-readings-job", () => {

await fetchAndStoreDataIntoReadingsModel();

// Expectation for devices not in fetched measurements to be marked offline
// Expectation for devices not in fetched measurements and older than threshold to be marked offline
expect(deviceUpdateManyStub).to.have.been.calledWith(
{ _id: { $nin: ["456"] } }, // Assuming '456' is the only active device ID
{
_id: { $nin: ["456"] }, // Assuming '456' is the only active device ID
lastActive: {
$lt: moment()
.subtract(INACTIVE_THRESHOLD, "milliseconds")
.toDate(),
}, // Check lastActive condition
},
{ isOnline: false }
);
});
Expand Down
8 changes: 7 additions & 1 deletion src/device-registry/bin/jobs/v2-store-readings-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,14 @@ const fetchAndStoreDataIntoReadingsModel = async () => {
}

// Update devices that are not in the activeDeviceIds set to offline
const thresholdTime = moment()

Check warning on line 209 in src/device-registry/bin/jobs/v2-store-readings-job.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/bin/jobs/v2-store-readings-job.js#L209

Added line #L209 was not covered by tests
.subtract(INACTIVE_THRESHOLD, "milliseconds")
.toDate();
await DeviceModel("airqo").updateMany(

Check warning on line 212 in src/device-registry/bin/jobs/v2-store-readings-job.js

View check run for this annotation

Codecov / codecov/patch

src/device-registry/bin/jobs/v2-store-readings-job.js#L212

Added line #L212 was not covered by tests
{ _id: { $nin: Array.from(activeDeviceIds) } },
{
_id: { $nin: Array.from(activeDeviceIds) },
lastActive: { $lt: thresholdTime },
},
{ isOnline: false }
);

Expand Down

0 comments on commit d77a0d1

Please sign in to comment.