-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3533 from airqo-platform/staging
move to production
- Loading branch information
Showing
21 changed files
with
333 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SESSION_SECRET=test_secret | ||
REDIS_SERVER=127.0.0.1 | ||
REDIS_SERVER_DEV=127.0.0.1 | ||
REDIS_PORT=4674 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const constants = require("@config/constants"); | ||
const log4js = require("log4js"); | ||
const logger = log4js.getLogger( | ||
`${constants.ENVIRONMENT} -- /bin/jobs/check-network-status-job` | ||
); | ||
const DeviceModel = require("@models/Device"); | ||
const cron = require("node-cron"); | ||
const { logText } = require("@utils/log"); | ||
|
||
const BATCH_SIZE = 1000; // Define the size of each batch | ||
|
||
const checkNetworkStatus = async () => { | ||
try { | ||
let totalDevices = 0; | ||
let offlineDevicesCount = 0; | ||
let pageNumber = 0; | ||
|
||
while (true) { | ||
// Fetch a batch of devices | ||
const devices = await DeviceModel("airqo") | ||
.find({}, "isOnline _id") | ||
.lean() | ||
.limit(BATCH_SIZE) | ||
.skip(pageNumber * BATCH_SIZE); | ||
|
||
if (devices.length === 0) break; // Exit loop if no more devices | ||
|
||
totalDevices += devices.length; | ||
|
||
// Count offline devices in the current batch | ||
offlineDevicesCount += devices.filter( | ||
(device) => device.isOnline === false | ||
).length; | ||
|
||
pageNumber++; // Move to the next batch | ||
} | ||
|
||
if (totalDevices === 0) { | ||
logText("No devices found"); | ||
logger.info("No devices found."); | ||
return; | ||
} | ||
|
||
const offlinePercentage = (offlineDevicesCount / totalDevices) * 100; | ||
|
||
if (offlinePercentage > 60) { | ||
logText( | ||
`⚠️💔😥 More than 60% of devices are offline: ${offlinePercentage.toFixed( | ||
2 | ||
)}%` | ||
); | ||
|
||
logger.warn( | ||
`⚠️💔😥 More than 60% of devices are offline: ${offlinePercentage.toFixed( | ||
2 | ||
)}%` | ||
); | ||
} else { | ||
// logText( | ||
// `✅ Network status is acceptable: ${offlinePercentage.toFixed( | ||
// 2 | ||
// )}% offline` | ||
// ); | ||
// logger.info( | ||
// `✅ Network status is acceptable: ${offlinePercentage.toFixed( | ||
// 2 | ||
// )}% offline` | ||
// ); | ||
} | ||
} catch (error) { | ||
logText(`Error checking network status: ${error.message}`); | ||
logger.error(`Error checking network status: ${error.message}`); | ||
logger.error(`Stack trace: ${error.stack}`); | ||
} | ||
}; | ||
|
||
logText("Network status job is now running....."); | ||
const schedule = "0 */2 * * *"; | ||
cron.schedule(schedule, checkNetworkStatus, { | ||
scheduled: true, | ||
timezone: constants.TIMEZONE, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/device-registry/bin/jobs/test/ut_check-network-status-job.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require("module-alias/register"); | ||
const sinon = require("sinon"); | ||
const { expect } = require("chai"); | ||
const log4js = require("log4js"); | ||
const DeviceModel = require("@models/Device"); | ||
const { logText } = require("@utils/log"); | ||
const checkNetworkStatus = require("@bin/jobs/check-network-status-job"); | ||
|
||
describe("checkNetworkStatus", () => { | ||
let loggerStub; | ||
let findStub; | ||
|
||
beforeEach(() => { | ||
// Stub the logger methods | ||
loggerStub = sinon.stub(log4js.getLogger(), "info"); | ||
sinon.stub(log4js.getLogger(), "warn"); | ||
sinon.stub(log4js.getLogger(), "error"); | ||
|
||
// Stub the DeviceModel find method | ||
findStub = sinon.stub(DeviceModel.prototype, "find").returns({ | ||
lean: () => ({ | ||
limit: () => ({ | ||
skip: () => Promise.resolve([]), // Default to an empty array | ||
}), | ||
}), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore all stubs | ||
sinon.restore(); | ||
}); | ||
|
||
it("should log 'No devices found' when there are no devices", async () => { | ||
await checkNetworkStatus(); | ||
|
||
expect(loggerStub.calledWith("No devices found.")).to.be.true; | ||
expect(logText.calledWith("No devices found")).to.be.true; | ||
}); | ||
|
||
it("should calculate offline percentage correctly and log acceptable status", async () => { | ||
findStub.returns({ | ||
lean: () => ({ | ||
limit: () => ({ | ||
skip: () => | ||
Promise.resolve([{ isOnline: true }, { isOnline: false }]), // Mock devices | ||
}), | ||
}), | ||
}); | ||
|
||
await checkNetworkStatus(); | ||
|
||
expect( | ||
loggerStub.calledWith("✅ Network status is acceptable: 50.00% offline") | ||
).to.be.true; | ||
}); | ||
|
||
it("should calculate offline percentage correctly and log warning if more than 60% are offline", async () => { | ||
findStub.returns({ | ||
lean: () => ({ | ||
limit: () => ({ | ||
skip: () => | ||
Promise.resolve([ | ||
{ isOnline: false }, | ||
{ isOnline: false }, | ||
{ isOnline: true }, | ||
]), // Mock devices | ||
}), | ||
}), | ||
}); | ||
|
||
await checkNetworkStatus(); | ||
|
||
expect( | ||
loggerStub.calledWith("⚠️ More than 60% of devices are offline: 66.67%") | ||
).to.be.true; | ||
}); | ||
|
||
it("should handle errors gracefully", async () => { | ||
findStub.throws(new Error("Database error")); | ||
|
||
await checkNetworkStatus(); | ||
|
||
expect( | ||
loggerStub.calledWith("Error checking network status: Database error") | ||
).to.be.true; | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.