2021-09-19 07:04:51 -04:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
|
|
|
|
class Group extends BeanModel {
|
|
|
|
|
2022-04-16 16:11:45 -04:00
|
|
|
/**
|
2022-04-22 14:10:13 -04:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-04-16 16:11:45 -04:00
|
|
|
* Only show necessary data to public
|
2022-04-21 08:01:22 -04:00
|
|
|
* @param {boolean} [showTags=false] Should the JSON include monitor tags
|
2022-04-16 16:11:45 -04:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
2023-07-04 19:37:45 -04:00
|
|
|
async toPublicJSON(showTags = false, certExpiry = false) {
|
2021-09-19 11:24:51 -04:00
|
|
|
let monitorBeanList = await this.getMonitorList();
|
2021-09-19 07:04:51 -04:00
|
|
|
let monitorList = [];
|
|
|
|
|
|
|
|
for (let bean of monitorBeanList) {
|
2023-07-04 19:37:45 -04:00
|
|
|
monitorList.push(await bean.toPublicJSON(showTags, certExpiry));
|
2021-09-19 07:04:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
weight: this.weight,
|
|
|
|
monitorList,
|
|
|
|
};
|
|
|
|
}
|
2021-09-19 11:24:51 -04:00
|
|
|
|
2022-04-16 16:11:45 -04:00
|
|
|
/**
|
|
|
|
* Get all monitors
|
2022-04-21 15:02:18 -04:00
|
|
|
* @returns {Bean[]}
|
2022-04-16 16:11:45 -04:00
|
|
|
*/
|
2021-09-19 11:24:51 -04:00
|
|
|
async getMonitorList() {
|
|
|
|
return R.convertToBeans("monitor", await R.getAll(`
|
2022-06-11 12:23:12 -04:00
|
|
|
SELECT monitor.*, monitor_group.send_url FROM monitor, monitor_group
|
2021-09-19 11:24:51 -04:00
|
|
|
WHERE monitor.id = monitor_group.monitor_id
|
|
|
|
AND group_id = ?
|
2021-09-22 03:23:58 -04:00
|
|
|
ORDER BY monitor_group.weight
|
2021-09-19 11:24:51 -04:00
|
|
|
`, [
|
|
|
|
this.id,
|
|
|
|
]));
|
|
|
|
}
|
2021-09-19 07:04:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Group;
|