Skip to content

Commit

Permalink
Merge pull request #837 from hpcc-systems/yadhap/domain-level-severit…
Browse files Browse the repository at this point in the history
…y-threshold-db

added severityThreshold column in asr_domains table and made necessar…
  • Loading branch information
FancMa01 authored Aug 28, 2024
2 parents e994e76 + c5396ff commit 84cc229
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ module.exports = {
unique: true,
type: DataTypes.STRING,
},
severityThreshold: {
allowNull: false,
type: DataTypes.INTEGER,
},
severityAlertRecipients: {
allowNull: false,
type: DataTypes.JSON,
},
metaData: {
allowNull: true,
type: DataTypes.JSON,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
Expand Down
12 changes: 12 additions & 0 deletions Tombolo/server/models/asr_domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ module.exports = (sequelize, DataTypes) => {
unique: true,
type: DataTypes.STRING,
},
severityThreshold: {
allowNull: false,
type: DataTypes.INTEGER,
},
severityAlertRecipients: {
allowNull: false,
type: DataTypes.JSON,
},
metaData:{
allowNull: true,
type: DataTypes.JSON,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
Expand Down
49 changes: 34 additions & 15 deletions Tombolo/server/routes/asr/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ const Products = models.asr_products;
const DomainProduct = models.asr_domain_to_products;

// Create a new domain
router.post("/domains/",[
router.post(
"/domains/",
[
body("name").notEmpty().withMessage("Domain name is required"),
body("monitoringTypeIds")
.optional()
.isArray()
.withMessage("Monitoring type is required"),
body("createdBy").notEmpty().withMessage("Created by is required"),
], async (req, res) => {
body("severityThreshold")
.isInt()
.withMessage("Severity threshold is required and must be an integer"),
body("severityAlertRecipients").isArray().withMessage("Severity alert recipients must be an array"),
],
async (req, res) => {
try {
// Validate the payload
const errors = validationResult(req);
Expand All @@ -33,10 +40,11 @@ router.post("/domains/",[

/* if monitoring type is provided,
create domain, next iterate over monitoringTypeId and make entry to asr_domain_monitoring_types*/
const { name, monitoringTypeIds, createdBy } = req.body;
const { name, severityThreshold, severityAlertRecipients, monitoringTypeIds, createdBy } =
req.body;
let domain;
if (monitoringTypeIds) {
domain = await Domains.create({ name, createdBy });
domain = await Domains.create({ name, severityThreshold, severityAlertRecipients, createdBy });

// create domain monitoring type mapping
const createPromises = monitoringTypeIds.map((monitoringId) => {
Expand All @@ -45,17 +53,16 @@ router.post("/domains/",[
monitoring_type_id: monitoringId,
createdBy,
});
}
);
});

await Promise.all(createPromises);
}

// if no monitoring type is provided, create domain without monitoring type
else {
domain = await Domains.create({ name, createdBy });
domain = await Domains.create({ name, severityThreshold,severityAlertRecipients, createdBy });
}
res.status(200).json({message: "Domain created successfully", domain});
res.status(200).json({ message: "Domain created successfully", domain });
} catch (error) {
logger.error(error);
res.status(500).json({ message: "Failed to create domain" });
Expand Down Expand Up @@ -109,6 +116,8 @@ router.patch(
[
param("id").isUUID().withMessage("ID must be a UUID"),
body("name").notEmpty().withMessage("Domain name is required"),
body("severityThreshold").isInt().withMessage("Severity threshold is required and must be an integer"),
body("severityAlertRecipients").isArray().withMessage("Severity alert recipients must be an array"),
body("monitoringTypeIds")
.optional()
.isArray()
Expand All @@ -125,12 +134,18 @@ router.patch(
}

// Update domain and delete or add relation in the junction table
const { name, monitoringTypeIds, updatedBy } = req.body;
const {
name,
severityThreshold,
severityAlertRecipients,
monitoringTypeIds,
updatedBy,
} = req.body;
let response;
if (monitoringTypeIds) {
response = await sequelize.transaction(async (t) => {
await Domains.update(
{ name, updatedBy },
{ name, severityThreshold, severityAlertRecipients, updatedBy },
{ where: { id: req.params.id }, transaction: t }
);

Expand All @@ -156,13 +171,12 @@ router.patch(
});
} else {
response = await Domains.update(
{ name, updatedBy },
{ name, severityThreshold, severityAlertRecipients, updatedBy },
{ where: { id: req.params.id } }
);
}

const message =
response[0] === 0 ? "Domain not found" : "Successfully updated domain";
const message = response[0] === 0 ? "Domain not found" : "Successfully updated domain";
res.status(200).json({ message });
} catch (err) {
logger.error(err);
Expand Down Expand Up @@ -251,7 +265,7 @@ router.get("/products/", async(req, res) => {
attributes: [], // Exclude the junction table from the result
},
as: "associatedDomains",
attributes: ["id", "name"],
attributes: ["id", "name", "severityThreshold", "severityAlertRecipients"],
},
],
order: [["createdAt", "DESC"]],
Expand Down Expand Up @@ -403,7 +417,12 @@ router.get("/domainsForSpecificMonitoring/:monitoringTypeId",
include: [
{
model: Domains,
attributes: ["id", "name"],
attributes: [
"id",
"name",
"severityThreshold",
"severityAlertRecipients",
],
},
],
raw: true,
Expand Down

0 comments on commit 84cc229

Please sign in to comment.