send notification for important heartbeat

This commit is contained in:
LouisLam 2021-07-09 17:55:48 +08:00
parent 3bdf174e90
commit d24a15410e
5 changed files with 108 additions and 14 deletions

View File

@ -8,7 +8,7 @@ const axios = require("axios");
const {tcping, ping} = require("../util-server"); const {tcping, ping} = require("../util-server");
const {R} = require("redbean-node"); const {R} = require("redbean-node");
const {BeanModel} = require("redbean-node/dist/bean-model"); const {BeanModel} = require("redbean-node/dist/bean-model");
const {Notification} = require("../notification")
/** /**
* status: * status:
@ -17,7 +17,18 @@ const {BeanModel} = require("redbean-node/dist/bean-model");
*/ */
class Monitor extends BeanModel { class Monitor extends BeanModel {
toJSON() { async toJSON() {
let notificationIDList = {};
let list = await R.find("monitor_notification", " monitor_id = ? ", [
this.id
])
for (let bean of list) {
notificationIDList[bean.notification_id] = true;
}
return { return {
id: this.id, id: this.id,
name: this.name, name: this.name,
@ -29,6 +40,7 @@ class Monitor extends BeanModel {
type: this.type, type: this.type,
interval: this.interval, interval: this.interval,
keyword: this.keyword, keyword: this.keyword,
notificationIDList
}; };
} }
@ -96,6 +108,28 @@ class Monitor extends BeanModel {
// Mark as important if status changed // Mark as important if status changed
if (! previousBeat || previousBeat.status !== bean.status) { if (! previousBeat || previousBeat.status !== bean.status) {
bean.important = true; bean.important = true;
let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? `, [
this.id
])
let promiseList = [];
let text;
if (bean.status === 1) {
text = "✅ Up"
} else {
text = "🔴 Down"
}
let msg = `[${this.name}] [${text}] ${bean.msg}`;
for(let notification of notificationList) {
promiseList.push(Notification.send(JSON.parse(notification.config), msg));
}
await Promise.all(promiseList);
} else { } else {
bean.important = false; bean.important = false;
} }

View File

@ -111,12 +111,17 @@ let monitorList = {};
socket.on("add", async (monitor, callback) => { socket.on("add", async (monitor, callback) => {
try { try {
checkLogin(socket) checkLogin(socket)
let bean = R.dispense("monitor") let bean = R.dispense("monitor")
let notificationIDList = monitor.notificationIDList;
delete monitor.notificationIDList;
bean.import(monitor) bean.import(monitor)
bean.user_id = socket.userID bean.user_id = socket.userID
await R.store(bean) await R.store(bean)
await updateMonitorNotification(bean.id, notificationIDList)
await startMonitor(socket.userID, bean.id); await startMonitor(socket.userID, bean.id);
await sendMonitorList(socket); await sendMonitorList(socket);
@ -154,6 +159,8 @@ let monitorList = {};
await R.store(bean) await R.store(bean)
await updateMonitorNotification(bean.id, monitor.notificationIDList)
if (bean.active) { if (bean.active) {
await restartMonitor(socket.userID, bean.id) await restartMonitor(socket.userID, bean.id)
} }
@ -188,7 +195,7 @@ let monitorList = {};
callback({ callback({
ok: true, ok: true,
monitor: bean.toJSON(), monitor: await bean.toJSON(),
}); });
} catch (e) { } catch (e) {
@ -391,6 +398,21 @@ let monitorList = {};
})(); })();
async function updateMonitorNotification(monitorID, notificationIDList) {
R.exec("DELETE FROM monitor_notification WHERE monitor_id = ? ", [
monitorID
])
for (let notificationID in notificationIDList) {
if (notificationIDList[notificationID]) {
let relation = R.dispense("monitor_notification");
relation.monitor_id = monitorID;
relation.notification_id = notificationID;
await R.store(relation)
}
}
}
async function checkOwner(userID, monitorID) { async function checkOwner(userID, monitorID) {
let row = await R.getRow("SELECT id FROM monitor WHERE id = ? AND user_id = ? ", [ let row = await R.getRow("SELECT id FROM monitor WHERE id = ? AND user_id = ? ", [
monitorID, monitorID,
@ -445,7 +467,7 @@ async function getMonitorJSONList(userID) {
]) ])
for (let monitor of monitorList) { for (let monitor of monitorList) {
result[monitor.id] = monitor.toJSON(); result[monitor.id] = await monitor.toJSON();
} }
return result; return result;

View File

@ -41,7 +41,11 @@
</div> </div>
<div class="form-text"> <div class="form-text">
Support Direct Chat / Group / Channel's Chat ID
<p style="margin-top: 8px;">
You can get your chat id by sending message to the bot and go to this url to view the chat_id: You can get your chat id by sending message to the bot and go to this url to view the chat_id:
</p>
<p style="margin-top: 8px;"> <p style="margin-top: 8px;">
@ -59,7 +63,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-danger" @click="deleteNotification" :disabled="processing" v-if="id">Delete</button> <button type="button" class="btn btn-danger" @click="deleteConfirm" :disabled="processing" v-if="id">Delete</button>
<button type="button" class="btn btn-warning" @click="test" :disabled="processing">Test</button> <button type="button" class="btn btn-warning" @click="test" :disabled="processing">Test</button>
<button type="submit" class="btn btn-primary" :disabled="processing">Save</button> <button type="submit" class="btn btn-primary" :disabled="processing">Save</button>
</div> </div>
@ -68,6 +72,8 @@
</div> </div>
</form> </form>
<Confirm ref="confirmDelete" @yes="deleteNotification" btn-style="btn-danger">Are you sure want to delete this notification for all monitors?</Confirm>
</template> </template>
<script> <script>
@ -75,9 +81,11 @@ import { Modal } from 'bootstrap'
import { ucfirst } from "../../server/util"; import { ucfirst } from "../../server/util";
import axios from "axios"; import axios from "axios";
import { useToast } from 'vue-toastification' import { useToast } from 'vue-toastification'
import Confirm from "./Confirm.vue";
const toast = useToast() const toast = useToast()
export default { export default {
components: {Confirm},
props: { props: {
}, },
@ -104,6 +112,11 @@ export default {
}, },
methods: { methods: {
deleteConfirm() {
this.modal.hide();
this.$refs.confirmDelete.show()
},
show(notificationID) { show(notificationID) {
if (notificationID) { if (notificationID) {
this.id = notificationID; this.id = notificationID;
@ -166,11 +179,19 @@ export default {
if (res.data.result.length >= 1) { if (res.data.result.length >= 1) {
let update = res.data.result[res.data.result.length - 1] let update = res.data.result[res.data.result.length - 1]
if (update.channel_post) {
this.notification.telegramChatID = update.channel_post.chat.id;
} else if (update.message) {
this.notification.telegramChatID = update.message.chat.id; this.notification.telegramChatID = update.message.chat.id;
} else { } else {
throw new Error("Chat ID is not found, please send a message to this bot first") throw new Error("Chat ID is not found, please send a message to this bot first")
} }
} else {
throw new Error("Chat ID is not found, please send a message to this bot first")
}
} catch (error) { } catch (error) {
toast.error(error.message) toast.error(error.message)
} }

View File

@ -58,8 +58,12 @@
<p v-if="$root.notificationList.length === 0">Not available, please setup.</p> <p v-if="$root.notificationList.length === 0">Not available, please setup.</p>
<div class="form-check form-switch mb-3" v-for="notification in $root.notificationList"> <div class="form-check form-switch mb-3" v-for="notification in $root.notificationList">
<input class="form-check-input" type="checkbox" :id=" 'notification' + notification.id"> <input class="form-check-input" type="checkbox" :id=" 'notification' + notification.id" v-model="monitor.notificationIDList[notification.id]">
<label class="form-check-label" :for=" 'notification' + notification.id">{{ notification.name }} <a href="#" @click="$refs.notificationDialog.show(notification.id)">Edit</a></label>
<label class="form-check-label" :for=" 'notification' + notification.id">
{{ notification.name }}
<a href="#" @click="$refs.notificationDialog.show(notification.id)">Edit</a>
</label>
</div> </div>
<button class="btn btn-primary me-2" @click="$refs.notificationDialog.show()" type="button">Setup Notification</button> <button class="btn btn-primary me-2" @click="$refs.notificationDialog.show()" type="button">Setup Notification</button>
@ -86,7 +90,9 @@ export default {
data() { data() {
return { return {
processing: false, processing: false,
monitor: { } monitor: {
notificationIDList: {},
},
} }
}, },
computed: { computed: {
@ -109,6 +115,7 @@ export default {
name: "", name: "",
url: "https://", url: "https://",
interval: 60, interval: 60,
notificationIDList: {},
} }
} else if (this.isEdit) { } else if (this.isEdit) {
this.$root.getSocket().emit("getMonitor", this.$route.params.id, (res) => { this.$root.getSocket().emit("getMonitor", this.$route.params.id, (res) => {

View File

@ -52,20 +52,30 @@
<div class="col-md-6"> <div class="col-md-6">
<h2>Notifications</h2> <h2>Notifications</h2>
<p>Empty</p> <p v-if="$root.notificationList.length === 0">Not available, please setup.</p>
<button class="btn btn-primary" type="submit">Add Notification</button>
<ul>
<li v-for="notification in $root.notificationList">
{{ notification.name }}
<a href="#" @click="$refs.notificationDialog.show(notification.id)">Edit</a>
</li>
</ul>
<button class="btn btn-primary me-2" @click="$refs.notificationDialog.show()" type="button">Setup Notification</button>
</div> </div>
</div> </div>
</div> </div>
<NotificationDialog ref="notificationDialog" />
</template> </template>
<script> <script>
import dayjs from "dayjs"; import dayjs from "dayjs";
import utc from 'dayjs/plugin/utc' import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone' import timezone from 'dayjs/plugin/timezone'
import NotificationDialog from "../components/NotificationDialog.vue";
dayjs.extend(utc) dayjs.extend(utc)
dayjs.extend(timezone) dayjs.extend(timezone)
import {timezoneList} from "../util-frontend"; import {timezoneList} from "../util-frontend";
@ -74,7 +84,7 @@ const toast = useToast()
export default { export default {
components: { components: {
NotificationDialog
}, },
data() { data() {
return { return {