Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show which children are down for group notifications #5192

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions db/knex_migrations/2024-10-17-0000-notifications-path-as-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.up = function (knex) {
return knex.schema
.alterTable("notification", function (table) {
table.boolean("use_path_as_name").notNullable().defaultTo(true);
});
};

exports.down = function (knex) {
return knex.schema.alterTable("notification", function (table) {
table.dropColumn("use_path_as_name");
});
};
8 changes: 8 additions & 0 deletions server/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ async function sendNotificationList(socket) {
for (let bean of list) {
let notificationObject = bean.export();
notificationObject.isDefault = (notificationObject.isDefault === 1);
notificationObject.usePathAsName = (notificationObject.usePathAsName === 1);
notificationObject.active = (notificationObject.active === 1);

const configObject = JSON.parse(notificationObject.config);
if ( !("usePathAsName" in configObject)) {
configObject.usePathAsName = notificationObject.usePathAsName;
notificationObject.config = JSON.stringify(configObject);
}

result.push(notificationObject);
}

Expand Down
39 changes: 34 additions & 5 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ class Monitor extends BeanModel {

if (children.length > 0) {
bean.status = UP;
bean.msg = "All children up and running";
bean.msg = "";
let errorChildNames = [];
for (const child of children) {
if (!child.active) {
// Ignore inactive childs
Expand All @@ -399,10 +400,22 @@ class Monitor extends BeanModel {
} else if (bean.status === PENDING && lastBeat.status === DOWN) {
bean.status = lastBeat.status;
}

if (lastBeat && (lastBeat.status === PENDING || lastBeat.status === DOWN)) {
const childMonitor = await Monitor.getMonitor(lastBeat.monitor_id);
if (errorChildNames.length > 0) {
bean.msg += "\r\n";
}

bean.msg += "- " + childMonitor.name + ":\r\n" + lastBeat.msg.trim().replace(/^/gm, " ");
errorChildNames.push(childMonitor.name);
}
}

if (bean.status !== UP) {
bean.msg = "Child inaccessible";
if (bean.status === UP) {
bean.msg = "All children up and running";
} else if (bean.status === PENDING || bean.status === DOWN) {
bean.msg = "Some Children are having problems (" + errorChildNames.join(", ") + ")\r\n" + bean.msg;
}
} else {
// Set status pending if group is empty
Expand Down Expand Up @@ -1323,8 +1336,10 @@ class Monitor extends BeanModel {
for (let notification of notificationList) {
try {
const heartbeatJSON = bean.toJSON();
const monitorData = [{ id: monitor.id,
active: monitor.active
const monitorData = [{
id: monitor.id,
name: monitor.name,
active: monitor.active,
}];
const preloadData = await Monitor.preparePreloadData(monitorData);
// Prevent if the msg is undefined, notifications such as Discord cannot send out.
Expand Down Expand Up @@ -1604,6 +1619,20 @@ class Monitor extends BeanModel {
};
}

/**
* Gets Monitor with specific ID
* @param {number} monitorID ID of monitor to get
* @returns {Promise<LooseObject<any>>} Children
*/
static async getMonitor(monitorID) {
return await R.getRow(`
SELECT * FROM monitor
WHERE id = ?
`, [
monitorID,
]);
}

/**
* Gets Parent of the monitor
* @param {number} monitorID ID of monitor to get
Expand Down
5 changes: 5 additions & 0 deletions server/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class Notification {
* @throws Error with fail msg
*/
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
if (notification.usePathAsName && monitorJSON) {
monitorJSON["name"] = monitorJSON["pathName"];
}
if (this.providerList[notification.type]) {
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
} else {
Expand Down Expand Up @@ -211,6 +214,8 @@ class Notification {
bean.user_id = userID;
bean.config = JSON.stringify(notification);
bean.is_default = notification.isDefault || false;
bean.use_path_as_name = notification.usePathAsName;

await R.store(bean);

if (notification.applyExisting) {
Expand Down
12 changes: 12 additions & 0 deletions src/components/NotificationDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@

<br>

<div class="form-check form-switch">
<input v-model="notification.usePathAsName" class="form-check-input" type="checkbox">
<label class="form-check-label">Use Monitor Path as Name</label>
</div>
<div class="form-text">
If your monitor is inside a group, the name will show the full path of the monitor. For example "Group A / Monitor 1".
</div>

<br>

<div class="form-check form-switch">
<input v-model="notification.applyExisting" class="form-check-input" type="checkbox">
<label class="form-check-label">{{ $t("Apply on all existing monitors") }}</label>
Expand Down Expand Up @@ -95,6 +105,7 @@ export default {
/** @type { null | keyof NotificationFormList } */
type: null,
isDefault: false,
usePathAsName: false,
// Do not set default value here, please scroll to show()
}
};
Expand Down Expand Up @@ -263,6 +274,7 @@ export default {
name: "",
type: "telegram",
isDefault: false,
usePathAsName: true,
};
}

Expand Down
Loading