object to map to make it faster

made code clean
make it faster
This commit is contained in:
vishalsabhaya 2024-09-04 16:24:59 +09:00
parent 96a1010b30
commit a81317fa0e

View File

@ -78,8 +78,8 @@ class Monitor extends BeanModel {
*/ */
async toJSON(preloadData = {}, includeSensitiveData = true) { async toJSON(preloadData = {}, includeSensitiveData = true) {
const tags = preloadData.tags[this.id] || []; const tags = preloadData.tags.get(this.id) || [];
const notificationIDList = preloadData.notifications[this.id] || {}; const notificationIDList = preloadData.notifications.get(this.id) || new Map();
let screenshot = null; let screenshot = null;
@ -97,15 +97,15 @@ class Monitor extends BeanModel {
path, path,
pathName, pathName,
parent: this.parent, parent: this.parent,
childrenIDs: preloadData.childrenIDs[this.id] || [], childrenIDs: preloadData.childrenIDs.get(this.id) || [],
url: this.url, url: this.url,
method: this.method, method: this.method,
hostname: this.hostname, hostname: this.hostname,
port: this.port, port: this.port,
maxretries: this.maxretries, maxretries: this.maxretries,
weight: this.weight, weight: this.weight,
active: preloadData.activeStatus[this.id], active: preloadData.activeStatus.get(this.id),
forceInactive: preloadData.forceInactive[this.id], forceInactive: preloadData.forceInactive.get(this.id),
type: this.type, type: this.type,
timeout: this.timeout, timeout: this.timeout,
interval: this.interval, interval: this.interval,
@ -127,7 +127,7 @@ class Monitor extends BeanModel {
proxyId: this.proxy_id, proxyId: this.proxy_id,
notificationIDList, notificationIDList,
tags, tags,
maintenance: preloadData.maintenanceStatus[this.id], maintenance: preloadData.maintenanceStatus.get(this.id),
mqttTopic: this.mqttTopic, mqttTopic: this.mqttTopic,
mqttSuccessMessage: this.mqttSuccessMessage, mqttSuccessMessage: this.mqttSuccessMessage,
mqttCheckType: this.mqttCheckType, mqttCheckType: this.mqttCheckType,
@ -1523,40 +1523,56 @@ class Monitor extends BeanModel {
static async preparePreloadData(monitorData) { static async preparePreloadData(monitorData) {
const monitorIDs = monitorData.map(monitor => monitor.id); const monitorIDs = monitorData.map(monitor => monitor.id);
const notifications = await Monitor.getMonitorNotification(monitorIDs); const notifications = await Monitor.getMonitorNotification(monitorIDs);
const tags = await Monitor.getMonitorTag(monitorIDs); const tags = await Monitor.getMonitorTag(monitorIDs);
const maintenanceStatuses = await Promise.all( const maintenanceStatuses = await Promise.all(monitorData.map(monitor => Monitor.isUnderMaintenance(monitor.id)));
monitorData.map(monitor => Monitor.isUnderMaintenance(monitor.id)) 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 childrenIDs = await Promise.all( const forceInactiveStatuses = await Promise.all(monitorData.map(monitor => Monitor.isParentActive(monitor.id)));
monitorData.map(monitor => Monitor.getAllChildrenIDs(monitor.id))
); const notificationsMap = new Map();
const activeStatuses = await Promise.all( notifications.forEach(row => {
monitorData.map(monitor => Monitor.isActive(monitor.id, monitor.active)) if (!notificationsMap.has(row.monitor_id)) {
); notificationsMap.set(row.monitor_id, new Map());
const forceInactiveStatuses = await Promise.all( }
monitorData.map(monitor => Monitor.isParentActive(monitor.id)) 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, []);
}
tagsMap.get(row.monitor_id).push({ name: row.name, color: row.color });
});
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]);
});
// Organize preloaded data for efficient access
return { return {
notifications: notifications.reduce((acc, row) => { notifications: notificationsMap,
acc[row.monitor_id] = acc[row.monitor_id] || {}; tags: tagsMap,
acc[row.monitor_id][row.notification_id] = true; maintenanceStatus: maintenanceStatusMap,
return acc; childrenIDs: childrenIDsMap,
}, {}), activeStatus: activeStatusMap,
tags: tags.reduce((acc, row) => { forceInactive: forceInactiveMap,
acc[row.monitor_id] = acc[row.monitor_id] || [];
acc[row.monitor_id].push({ name: row.name,
color: row.color
});
return acc;
}, {}),
maintenanceStatus: Object.fromEntries(monitorData.map((m, index) => [ m.id, maintenanceStatuses[index] ])),
childrenIDs: Object.fromEntries(monitorData.map((m, index) => [ m.id, childrenIDs[index] ])),
activeStatus: Object.fromEntries(monitorData.map((m, index) => [ m.id, activeStatuses[index] ])),
forceInactive: Object.fromEntries(monitorData.map((m, index) => [ m.id, !forceInactiveStatuses[index] ])),
}; };
} }
/** /**