2021-09-19 04:04:51 -07:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
|
|
|
|
class Group extends BeanModel {
|
|
|
|
|
2022-04-16 13:11:45 -07:00
|
|
|
/**
|
|
|
|
* Return a object that ready to parse to JSON for public
|
|
|
|
* Only show necessary data to public
|
2022-04-21 05:01:22 -07:00
|
|
|
* @param {boolean} [showTags=false] Should the JSON include monitor tags
|
2022-04-16 13:11:45 -07:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2022-03-18 02:56:46 -07:00
|
|
|
async toPublicJSON(showTags = false) {
|
2021-09-19 08:24:51 -07:00
|
|
|
let monitorBeanList = await this.getMonitorList();
|
2021-09-19 04:04:51 -07:00
|
|
|
let monitorList = [];
|
|
|
|
|
|
|
|
for (let bean of monitorBeanList) {
|
2022-03-18 02:56:46 -07:00
|
|
|
monitorList.push(await bean.toPublicJSON(showTags));
|
2021-09-19 04:04:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
weight: this.weight,
|
|
|
|
monitorList,
|
|
|
|
};
|
|
|
|
}
|
2021-09-19 08:24:51 -07:00
|
|
|
|
2022-04-16 13:11:45 -07:00
|
|
|
/**
|
|
|
|
* Get all monitors
|
|
|
|
* @returns {Array<Bean>}
|
|
|
|
*/
|
2021-09-19 08:24:51 -07:00
|
|
|
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
|
2021-09-19 08:24:51 -07:00
|
|
|
`, [
|
|
|
|
this.id,
|
|
|
|
]));
|
|
|
|
}
|
2021-09-19 04:04:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Group;
|