remove async from tojson method

move path to preload function
This commit is contained in:
vishalsabhaya 2024-09-04 20:42:39 +09:00
parent b0281e44b8
commit 043f1a2ed9
3 changed files with 63 additions and 48 deletions

View File

@ -76,7 +76,7 @@ class Monitor extends BeanModel {
* JSON
* @returns {Promise<object>} Object ready to parse
*/
async toJSON(preloadData = {}, includeSensitiveData = true) {
toJSON(preloadData = {}, includeSensitiveData = true) {
const tags = preloadData.tags.get(this.id) || [];
const notificationIDList = preloadData.notifications.get(this.id) || new Map();
@ -87,7 +87,7 @@ class Monitor extends BeanModel {
screenshot = "/screenshots/" + jwt.sign(this.id, UptimeKumaServer.getInstance().jwtSecret) + ".png";
}
const path = await this.getPath();
const path = preloadData.paths.get(this.id) || [];
const pathName = path.join(" / ");
let data = {
@ -1322,7 +1322,7 @@ class Monitor extends BeanModel {
heartbeatJSON["timezoneOffset"] = UptimeKumaServer.getInstance().getTimezoneOffset();
heartbeatJSON["localDateTime"] = dayjs.utc(heartbeatJSON["time"]).tz(heartbeatJSON["timezone"]).format(SQL_DATETIME_FORMAT);
await Notification.send(JSON.parse(notification.config), msg, await monitor.toJSON(preloadData, false), heartbeatJSON);
await Notification.send(JSON.parse(notification.config), msg, monitor.toJSON(preloadData, false), heartbeatJSON);
} catch (e) {
log.error("monitor", "Cannot send notification to " + notification.name);
log.error("monitor", e);
@ -1521,6 +1521,16 @@ class Monitor extends BeanModel {
* @returns {Promise<LooseObject<any>>} object
*/
static async preparePreloadData(monitorData) {
const notificationsMap = new Map();
const tagsMap = new Map();
const maintenanceStatusMap = new Map();
const childrenIDsMap = new Map();
const activeStatusMap = new Map();
const forceInactiveMap = new Map();
const pathsMap = new Map();
if (monitorData.length > 0) {
const monitorIDs = monitorData.map(monitor => monitor.id);
const notifications = await Monitor.getMonitorNotification(monitorIDs);
const tags = await Monitor.getMonitorTag(monitorIDs);
@ -1528,8 +1538,8 @@ class Monitor extends BeanModel {
const childrenIDs = await Promise.all(monitorData.map(monitor => Monitor.getAllChildrenIDs(monitor.id)));
const activeStatuses = await Promise.all(monitorData.map(monitor => Monitor.isActive(monitor.id, monitor.active)));
const forceInactiveStatuses = await Promise.all(monitorData.map(monitor => Monitor.isParentActive(monitor.id)));
const paths = await Promise.all(monitorData.map(monitor => Monitor.getAllPath(monitor.id, monitor.name)));
const notificationsMap = new Map();
notifications.forEach(row => {
if (!notificationsMap.has(row.monitor_id)) {
notificationsMap.set(row.monitor_id, new Map());
@ -1537,7 +1547,6 @@ class Monitor extends BeanModel {
notificationsMap.get(row.monitor_id).set(row.notification_id, true);
});
const tagsMap = new Map();
tags.forEach(row => {
if (!tagsMap.has(row.monitor_id)) {
tagsMap.set(row.monitor_id, []);
@ -1548,26 +1557,27 @@ class Monitor extends BeanModel {
});
});
const maintenanceStatusMap = new Map();
monitorData.forEach((monitor, index) => {
maintenanceStatusMap.set(monitor.id, maintenanceStatuses[index]);
});
const childrenIDsMap = new Map();
monitorData.forEach((monitor, index) => {
childrenIDsMap.set(monitor.id, childrenIDs[index]);
});
const activeStatusMap = new Map();
monitorData.forEach((monitor, index) => {
activeStatusMap.set(monitor.id, activeStatuses[index]);
});
const forceInactiveMap = new Map();
monitorData.forEach((monitor, index) => {
forceInactiveMap.set(monitor.id, !forceInactiveStatuses[index]);
});
monitorData.forEach((monitor, index) => {
pathsMap.set(monitor.id, paths[index]);
});
}
return {
notifications: notificationsMap,
tags: tagsMap,
@ -1575,6 +1585,7 @@ class Monitor extends BeanModel {
childrenIDs: childrenIDsMap,
activeStatus: activeStatusMap,
forceInactive: forceInactiveMap,
paths: pathsMap,
};
}
@ -1610,16 +1621,18 @@ class Monitor extends BeanModel {
/**
* Gets the full path
* @param {number} monitorID ID of the monitor to get
* @param {string} name of the monitor to get
* @returns {Promise<string[]>} Full path (includes groups and the name) of the monitor
*/
async getPath() {
const path = [ this.name ];
static async getAllPath(monitorID, name) {
const path = [ name ];
if (this.parent === null) {
return path;
}
let parent = await Monitor.getParent(this.id);
let parent = await Monitor.getParent(monitorID);
while (parent !== null) {
path.unshift(parent.name);
parent = await Monitor.getParent(parent.id);

View File

@ -900,7 +900,7 @@ let needSetup = false;
const preloadData = await Monitor.preparePreloadData(monitorData);
callback({
ok: true,
monitor: await monitor.toJSON(preloadData),
monitor: monitor.toJSON(preloadData),
});
} catch (e) {

View File

@ -240,14 +240,16 @@ class UptimeKumaServer {
const monitorData = monitorList.map(monitor => ({
id: monitor.id,
active: monitor.active,
name: monitor.name,
}));
const preloadData = await Monitor.preparePreloadData(monitorData);
const monitorPromises = monitorList.map(monitor => monitor.toJSON(preloadData).then(json => {
const monitorPromises = monitorList.map(monitor => {
const json = monitor.toJSON(preloadData);
return { id: monitor.id,
json
};
}));
});
const monitors = await Promise.all(monitorPromises);
monitors.forEach(monitor => {