uptime-kuma/server/model/group.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

const { BeanModel } = require("redbean-node/dist/bean-model");
const { R } = require("redbean-node");
class Group extends BeanModel {
/**
* Return a object that ready to parse to JSON for public
* Only show necessary data to public
* @param {boolean} [showTags=false] Should the JSON include monitor tags
* @returns {Object}
*/
2022-03-18 02:56:46 -07:00
async toPublicJSON(showTags = false) {
let monitorBeanList = await this.getMonitorList();
let monitorList = [];
for (let bean of monitorBeanList) {
2022-03-18 02:56:46 -07:00
monitorList.push(await bean.toPublicJSON(showTags));
}
return {
id: this.id,
name: this.name,
weight: this.weight,
monitorList,
};
}
/**
* Get all monitors
* @returns {Array<Bean>}
*/
async getMonitorList() {
return R.convertToBeans("monitor", await R.getAll(`
SELECT monitor.* FROM monitor, monitor_group
WHERE monitor.id = monitor_group.monitor_id
AND group_id = ?
2021-09-22 00:23:58 -07:00
ORDER BY monitor_group.weight
`, [
this.id,
]));
}
}
module.exports = Group;