Skip to content

Commit

Permalink
Merge pull request #3533 from airqo-platform/staging
Browse files Browse the repository at this point in the history
move to production
  • Loading branch information
Baalmart authored Sep 28, 2024
2 parents 24cfe89 + a419b1c commit b2a8c26
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 154 deletions.
2 changes: 1 addition & 1 deletion k8s/device-monitor/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ images:
deviceMonitor: eu.gcr.io/airqo-250220/airqo-device-monitor-api
celeryBeat: eu.gcr.io/airqo-250220/airqo-device-monitor-celery-beat
celeryWorker: eu.gcr.io/airqo-250220/airqo-device-monitor-celery-worker
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/device-registry/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ app:
replicaCount: 3
image:
repository: eu.gcr.io/airqo-250220/airqo-device-registry-api
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-airqo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/airqo-exceedance-job
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/exceedance/values-prod-kcca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ app:
configmap: env-exceedance-production
image:
repository: eu.gcr.io/airqo-250220/kcca-exceedance-job
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
nameOverride: ''
fullnameOverride: ''
2 changes: 1 addition & 1 deletion k8s/predict/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ images:
predictJob: eu.gcr.io/airqo-250220/airqo-predict-job
trainJob: eu.gcr.io/airqo-250220/airqo-train-job
predictPlaces: eu.gcr.io/airqo-250220/airqo-predict-places-air-quality
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
api:
name: airqo-prediction-api
label: prediction-api
Expand Down
2 changes: 1 addition & 1 deletion k8s/workflows/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ images:
initContainer: eu.gcr.io/airqo-250220/airqo-workflows-xcom
redisContainer: eu.gcr.io/airqo-250220/airqo-redis
containers: eu.gcr.io/airqo-250220/airqo-workflows
tag: prod-ab3ef027-1727380390
tag: prod-24cfe896-1727423146
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
2 changes: 1 addition & 1 deletion k8s/workflows/values-stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ images:
initContainer: eu.gcr.io/airqo-250220/airqo-stage-workflows-xcom
redisContainer: eu.gcr.io/airqo-250220/airqo-stage-redis
containers: eu.gcr.io/airqo-250220/airqo-stage-workflows
tag: stage-b4ef239b-1726516795
tag: stage-2303ecd2-1727423098
nameOverride: ''
fullnameOverride: ''
podAnnotations: {}
Expand Down
4 changes: 4 additions & 0 deletions src/device-registry/.env.test
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
82 changes: 82 additions & 0 deletions src/device-registry/bin/jobs/check-network-status-job.js
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,
});
2 changes: 1 addition & 1 deletion src/device-registry/bin/jobs/kafka-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Kafka } = require("kafkajs");
const constants = require("@config/constants");
const log4js = require("log4js");
const logger = log4js.getLogger(
`${constants.ENVIRONMENT} -- bin/kafka-consumer`
`${constants.ENVIRONMENT} -- bin/jobs/kafka-consumer`
);
const { logObject } = require("@utils/log");
const createEvent = require("@utils/create-event");
Expand Down
2 changes: 1 addition & 1 deletion src/device-registry/bin/jobs/new-store-readings-job.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const constants = require("@config/constants");
const log4js = require("log4js");
const logger = log4js.getLogger(
`${constants.ENVIRONMENT} -- /bin/new-store-readings-job`
`${constants.ENVIRONMENT} -- /bin/jobs/new-store-readings-job`
);
const EventModel = require("@models/Event");
const DeviceModel = require("@models/Device");
Expand Down
2 changes: 1 addition & 1 deletion src/device-registry/bin/jobs/store-readings-job.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const constants = require("@config/constants");
const log4js = require("log4js");
const logger = log4js.getLogger(
`${constants.ENVIRONMENT} -- /bin/store-readings-job`
`${constants.ENVIRONMENT} -- /bin/jobs/store-readings-job`
);
const EventModel = require("@models/Event");
const ReadingModel = require("@models/Reading");
Expand Down
2 changes: 1 addition & 1 deletion src/device-registry/bin/jobs/store-signals-job.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const constants = require("@config/constants");
const log4js = require("log4js");
const logger = log4js.getLogger(
`${constants.ENVIRONMENT} -- /bin/store-signals-job`
`${constants.ENVIRONMENT} -- /bin/jobs/store-signals-job`
);
const EventModel = require("@models/Event");
const SignalModel = require("@models/Signal");
Expand Down
88 changes: 88 additions & 0 deletions src/device-registry/bin/jobs/test/ut_check-network-status-job.js
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;
});
});
1 change: 1 addition & 0 deletions src/device-registry/bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const { logText, logObject } = require("@utils/log");
const jsonify = require("@utils/jsonify");
require("@bin/jobs/store-signals-job");
require("@bin/jobs/new-store-readings-job");
require("@bin/jobs/check-network-status-job");

if (isEmpty(constants.SESSION_SECRET)) {
throw new Error("SESSION_SECRET environment variable not set");
Expand Down
Loading

0 comments on commit b2a8c26

Please sign in to comment.