Skip to content

Commit

Permalink
more test and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FancMa01 committed Jan 30, 2024
1 parent 9860ece commit 78c8a8b
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 143 deletions.
242 changes: 124 additions & 118 deletions server/routes/orbit/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ router.get(
return res.status(422).json({ success: false, errors: errors.array() });
const { application_id } = req.params;

if (!application_id) throw Error("Invalid app ID");
const result = await orbitMonitoring.findAll({
where: {
application_id,
Expand Down Expand Up @@ -542,10 +541,12 @@ router.delete(
//Check if this job is in bree - if so - remove
const breeJobs = jobScheduler.getAllJobs();
const expectedJobName = `Orbit Monitoring - ${id}`;
for (job of breeJobs) {
if (job.name === expectedJobName) {
jobScheduler.removeJobFromScheduler(expectedJobName);
break;
if (breeJobs?.length) {
for (job of breeJobs) {
if (job.name === expectedJobName) {
jobScheduler.removeJobFromScheduler(expectedJobName);
break;
}
}
}
} catch (err) {
Expand Down Expand Up @@ -602,22 +603,24 @@ router.get(
//connect to db

let wuList = [];
if (result?.length) {
await Promise.all(
result.map(async (build) => {
let wu = await orbitBuilds.findAll({
where: { application_id, name: build.build },
raw: true,
});

await Promise.all(
result.map(async (build) => {
let wu = await orbitBuilds.findAll({
where: { application_id, name: build.build },
raw: true,
});

wu.map((wu) => {
wuList.push(wu);
});

Promise.resolve;
})
);
if (wu.length > 0) {
wu.map((wu) => {
wuList.push(wu);
});
}

Promise.resolve;
})
);
}
//return finished list;
res.status(200).send(wuList);
} catch (err) {
Expand Down Expand Up @@ -649,113 +652,116 @@ router.post(
const sentNotifications = [];
//just grab the rows from result
let rows = result?.recordset;
//loop through rows to build notifications and import
await Promise.all(
rows.map(async (build) => {
//check if the build already exists
let orbitBuild = await orbitBuilds.findOne({
where: {
build_id: build.BuildInstanceIdKey,
application_id: application_id,
},
raw: true,
});

//if it doesn't exist, create it and send a notification
if (!orbitBuild) {
//create build
const newBuild = await orbitBuilds.create({
application_id: application_id,
build_id: build.BuildInstanceIdKey,
name: build.Name,
metaData: {
lastRun: build.DateUpdated,
status: build.Status_Code,
subStatus: build.SubStatus_Code,
workunit: build.HpccWorkUnit,
EnvironmentName: build.EnvironmentName,
Template: build.BuildTemplate_Name,
if (rows?.length) {
//loop through rows to build notifications and import
await Promise.all(
rows?.map(async (build) => {
//check if the build already exists
let orbitBuild = await orbitBuilds.findOne({
where: {
build_id: build.BuildInstanceIdKey,
application_id: application_id,
},
raw: true,
});

//if megaphone, send notification
// if (build.SubStatus_Code === "MEGAPHONE")

//build and send email notification
if (integration.metaData.notificationEmails) {
let buildDetails = {
name: newBuild.name,
status: newBuild.metaData.status,
subStatus: newBuild.metaData.subStatus,
lastRun: newBuild.metaData.lastRun,
workunit: newBuild.metaData.workunit,
};

const emailBody =
notificationTemplate.orbitBuildEmailBody(buildDetails);
const emailRecipients = integration.metaData.notificationEmails;

const notificationResponse = await notify({
to: emailRecipients,
from: process.env.EMAIL_SENDER,
subject:
"Alert: Megaphone Substatus detected on Orbit Build " +
build.Name,
text: emailBody,
html: emailBody,
//if it doesn't exist, create it and send a notification
if (!orbitBuild) {
//create build
const newBuild = await orbitBuilds.create({
application_id: application_id,
build_id: build.BuildInstanceIdKey,
name: build.Name,
metaData: {
lastRun: build.DateUpdated,
status: build.Status_Code,
subStatus: build.SubStatus_Code,
workunit: build.HpccWorkUnit,
EnvironmentName: build.EnvironmentName,
Template: build.BuildTemplate_Name,
},
});

let notification_id = uuidv4();

sentNotifications.push({
id: notification_id,
status: "notified",
notifiedTo: emailRecipients,
notification_channel: "eMail",
application_id,
notification_reason: "Megaphone Substatus",
monitoring_id: newBuild.id,
monitoring_type: "orbit",
});
//if megaphone, send notification
// if (build.SubStatus_Code === "MEGAPHONE")

//build and send email notification
if (integration.metaData.notificationEmails) {
let buildDetails = {
name: newBuild.name,
status: newBuild.metaData.status,
subStatus: newBuild.metaData.subStatus,
lastRun: newBuild.metaData.lastRun,
workunit: newBuild.metaData.workunit,
};

const emailBody =
notificationTemplate.orbitBuildEmailBody(buildDetails);
const emailRecipients = integration.metaData.notificationEmails;

const notificationResponse = await notify({
to: emailRecipients,
from: process.env.EMAIL_SENDER,
subject:
"Alert: Megaphone Substatus detected on Orbit Build " +
build.Name,
text: emailBody,
html: emailBody,
});

let notification_id = uuidv4();

sentNotifications.push({
id: notification_id,
status: "notified",
notifiedTo: emailRecipients,
notification_channel: "eMail",
application_id,
notification_reason: "Megaphone Substatus",
monitoring_id: newBuild.id,
monitoring_type: "orbit",
});
}

// //build and send Teams notification
if (integration.metaData.notificationWebhooks) {
let facts = [
{ name: newBuild.name },
{ status: newBuild.metaData.status },
{ subStatus: newBuild.metaData.subStatus },
{ lastRun: newBuild.metaData.lastRun },
{ workunit: newBuild.metaData.workunit },
];
let title = "Orbit Build Detectd With Megaphone Status";
notification_id = uuidv4();
const cardBody = notificationTemplate.orbitBuildMessageCard(
title,
facts,
notification_id
);
await axios.post(
integration.metaData.notificationWebhooks,
cardBody
);

sentNotifications.push({
id: notification_id,
status: "notified",
notifiedTo: emailRecipients,
notification_channel: "msTeams",
application_id,
notification_reason: "Megaphone Substatus",
monitoring_id: newBuild.id,
monitoring_type: "orbit",
});
}
}

// //build and send Teams notification
if (integration.metaData.notificationWebhooks) {
let facts = [
{ name: newBuild.name },
{ status: newBuild.metaData.status },
{ subStatus: newBuild.metaData.subStatus },
{ lastRun: newBuild.metaData.lastRun },
{ workunit: newBuild.metaData.workunit },
];
let title = "Orbit Build Detectd With Megaphone Status";
notification_id = uuidv4();
const cardBody = notificationTemplate.orbitBuildMessageCard(
title,
facts,
notification_id
);
await axios.post(
integration.metaData.notificationWebhooks,
cardBody
);

sentNotifications.push({
id: notification_id,
status: "notified",
notifiedTo: emailRecipients,
notification_channel: "msTeams",
application_id,
notification_reason: "Megaphone Substatus",
monitoring_id: newBuild.id,
monitoring_type: "orbit",
});
}
}

return true;
})
);
return true;
})
);
}

// Record notifications
if (sentNotifications.length > 0) {
Expand Down
54 changes: 29 additions & 25 deletions server/tests/unit/orbit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const mockSqlReturnData = require("../mock-data/sqlData.json");

//import model and spy functions that will be called
const models = require("../../models");
const jobScheduler = require("../../job-scheduler");
const orbitBuild = models.orbitBuilds;
const orbitMonitoring = models.orbitMonitoring;
const monitoring_notifications = models.monitoring_notifications;
Expand Down Expand Up @@ -40,6 +41,11 @@ orbitMonitoring.destroy = jest.fn();
//monitoring notifications mocks
monitoring_notifications.bulkCreate = jest.fn();

//job scheduler mocks
jobScheduler.createOrbitMonitoringJob = jest.fn();
jobScheduler.removeJobFromScheduler = jest.fn();
jobScheduler.getAllJobs = jest.fn();

//sql mocks
sql.connect = jest.fn();
sql.query = jest.fn(() => {
Expand Down Expand Up @@ -103,14 +109,18 @@ describe("Integration Tests", () => {
});

test("Get All Builds", async () => {
response = await request(app).get(`/api/orbit/all/${application_id}`);
expect(orbitBuild.findAll).toHaveBeenCalledTimes(1);
response = await request(app).get(
`/api/orbit/allMonitorings/${application_id}`
);
expect(orbitMonitoring.findAll).toHaveBeenCalledTimes(1);
expect(response.status).toBe(200);
});

test("Get All Builds - Bad Data", async () => {
response = await request(app).get(`/api/orbit/all/${badApplicationId}`);
expect(orbitBuild.findAll).toHaveBeenCalledTimes(0);
response = await request(app).get(
`/api/orbit/allMonitorings/${badApplicationId}`
);
expect(orbitMonitoring.findAll).toHaveBeenCalledTimes(0);
expect(response.status).toBe(422);
expect(response.body.success).toBe(false);
});
Expand Down Expand Up @@ -228,18 +238,13 @@ describe("Integration Tests", () => {
expect(response.body.success).toBe(false);
});

//i have no idea why this fails and it is incredibly annoying
// test("Get WorkUnits", async () => {
// console.log("calling");
// console.log("application ID: " + application_id);
// response = await request(app).get(
// `/api/orbit/getWorkunits/${application_id}`
// );

// expect(response.status).toBe(200);
// expect(orbitMonitoring.findAll).toHaveBeenCalledTimes(1);
// expect(orbitBuild.findAll).toHaveBeenCalledTimes(1);
// });
test("Get WorkUnits", async () => {
response = await request(app).get(
`/api/orbit/getWorkunits/${application_id}`
);
expect(orbitMonitoring.findAll).toHaveBeenCalledTimes(1);
expect(response.status).toBe(200);
});

test("Get WorkUnits - Bad ID", async () => {
response = await request(app).get(
Expand All @@ -251,17 +256,16 @@ describe("Integration Tests", () => {
expect(response.body.success).toBe(false);
});

// test("Update List", async () => {
// response = await request(app).post(
// `/api/orbit/updateList/${application_id}`
// );
test("Update List", async () => {
response = await request(app).post(
`/api/orbit/updateList/${application_id}`
);

// expect(sql.connect).toHaveBeenCalledTimes(1);
// expect(sql.query).toHaveBeenCalledTimes(1);
// expect(orbitBuild.findOne).toHaveBeenCalled();
expect(sql.connect).toHaveBeenCalledTimes(1);
expect(sql.query).toHaveBeenCalledTimes(1);

// expect(response.status).toBe(200);
// });
expect(response.status).toBe(200);
});

test("Update List - Bad ID", async () => {
response = await request(app).post(
Expand Down

0 comments on commit 78c8a8b

Please sign in to comment.