uptime-kuma/src/mixins/socket.js

330 lines
9.3 KiB
JavaScript
Raw Normal View History

2021-07-01 13:47:14 +00:00
import dayjs from "dayjs";
2021-07-27 17:47:13 +00:00
import { io } from "socket.io-client";
import { useToast } from "vue-toastification";
2021-06-25 13:55:49 +00:00
const toast = useToast()
let socket;
export default {
data() {
return {
2021-07-13 10:08:12 +00:00
info: { },
2021-06-25 13:55:49 +00:00
socket: {
token: null,
firstConnect: true,
connected: false,
2021-06-29 08:06:20 +00:00
connectCount: 0,
2021-06-25 13:55:49 +00:00
},
2021-07-01 14:16:02 +00:00
remember: (localStorage.remember !== "0"),
2021-07-01 13:47:14 +00:00
userTimezone: localStorage.timezone || "auto",
2021-06-25 13:55:49 +00:00
allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed.
loggedIn: false,
2021-07-01 05:11:16 +00:00
monitorList: { },
heartbeatList: { },
importantHeartbeatList: { },
avgPingList: { },
2021-07-09 06:14:03 +00:00
uptimeList: { },
certInfoList: {},
2021-07-09 06:14:03 +00:00
notificationList: [],
2021-07-11 07:23:28 +00:00
windowWidth: window.innerWidth,
showListMobile: false,
2021-07-31 13:13:32 +00:00
connectionErrorMsg: "Cannot connect to the socket server. Reconnecting..."
2021-06-25 13:55:49 +00:00
}
},
created() {
2021-07-27 17:47:13 +00:00
window.addEventListener("resize", this.onResize);
2021-07-11 07:23:28 +00:00
2021-07-11 06:20:31 +00:00
let wsHost;
2021-07-19 15:06:42 +00:00
const env = process.env.NODE_ENV || "production";
if (env === "development" || localStorage.dev === "dev") {
2021-07-11 06:20:31 +00:00
wsHost = ":3001"
} else {
wsHost = ""
}
socket = io(wsHost, {
2021-07-27 17:47:13 +00:00
transports: ["websocket"],
2021-06-25 13:55:49 +00:00
});
2021-07-27 17:47:13 +00:00
socket.on("info", (info) => {
2021-07-13 10:08:12 +00:00
this.info = info;
});
2021-07-27 17:47:13 +00:00
socket.on("setup", (monitorID, data) => {
2021-07-11 05:47:57 +00:00
this.$router.push("/setup")
});
socket.on("monitorList", (data) => {
// Add Helper function
Object.entries(data).forEach(([monitorID, monitor]) => {
monitor.getUrl = () => {
try {
return new URL(monitor.url);
} catch (_) {
return null;
}
};
});
2021-06-25 13:55:49 +00:00
this.monitorList = data;
});
2021-07-27 17:47:13 +00:00
socket.on("notificationList", (data) => {
2021-07-09 06:14:03 +00:00
this.notificationList = data;
});
2021-07-27 17:47:13 +00:00
socket.on("heartbeat", (data) => {
2021-06-27 08:10:55 +00:00
if (! (data.monitorID in this.heartbeatList)) {
this.heartbeatList[data.monitorID] = [];
}
this.heartbeatList[data.monitorID].push(data)
2021-06-30 13:04:58 +00:00
// Add to important list if it is important
// Also toast
if (data.important) {
if (data.status === 0) {
toast.error(`[${this.monitorList[data.monitorID].name}] [DOWN] ${data.msg}`, {
timeout: false,
});
} else if (data.status === 1) {
toast.success(`[${this.monitorList[data.monitorID].name}] [Up] ${data.msg}`, {
timeout: 20000,
});
} else {
toast(`[${this.monitorList[data.monitorID].name}] ${data.msg}`);
}
if (! (data.monitorID in this.importantHeartbeatList)) {
this.importantHeartbeatList[data.monitorID] = [];
}
this.importantHeartbeatList[data.monitorID].unshift(data)
}
2021-06-27 08:10:55 +00:00
});
2021-07-27 17:47:13 +00:00
socket.on("heartbeatList", (monitorID, data) => {
2021-06-29 08:06:20 +00:00
if (! (monitorID in this.heartbeatList)) {
this.heartbeatList[monitorID] = data;
} else {
this.heartbeatList[monitorID] = data.concat(this.heartbeatList[monitorID])
}
});
2021-07-27 17:47:13 +00:00
socket.on("avgPing", (monitorID, data) => {
2021-06-30 13:04:58 +00:00
this.avgPingList[monitorID] = data
});
2021-07-27 17:47:13 +00:00
socket.on("uptime", (monitorID, type, data) => {
2021-07-01 05:11:16 +00:00
this.uptimeList[`${monitorID}_${type}`] = data
});
2021-07-27 17:47:13 +00:00
socket.on("certInfo", (monitorID, data) => {
this.certInfoList[monitorID] = JSON.parse(data)
});
2021-07-27 17:47:13 +00:00
socket.on("importantHeartbeatList", (monitorID, data) => {
2021-06-30 13:04:58 +00:00
if (! (monitorID in this.importantHeartbeatList)) {
this.importantHeartbeatList[monitorID] = data;
} else {
this.importantHeartbeatList[monitorID] = data.concat(this.importantHeartbeatList[monitorID])
}
});
2021-06-27 08:10:55 +00:00
2021-07-31 13:13:32 +00:00
socket.on("connect_error", (err) => {
console.error(`Failed to connect to the backend. Socket.io connect_error: ${err.message}`);
this.connectionErrorMsg = `Cannot connect to the socket server. [${err}] Reconnecting...`;
this.socket.connected = false;
this.socket.firstConnect = false;
});
2021-07-27 17:47:13 +00:00
socket.on("disconnect", () => {
2021-06-29 08:06:20 +00:00
console.log("disconnect")
2021-07-31 13:13:32 +00:00
this.connectionErrorMsg = "Lost connection to the socket server. Reconnecting...";
2021-06-25 13:55:49 +00:00
this.socket.connected = false;
});
2021-07-27 17:47:13 +00:00
socket.on("connect", () => {
2021-06-29 08:06:20 +00:00
console.log("connect")
this.socket.connectCount++;
2021-06-25 13:55:49 +00:00
this.socket.connected = true;
2021-06-29 08:06:20 +00:00
// Reset Heartbeat list if it is re-connect
if (this.socket.connectCount >= 2) {
2021-06-30 13:04:58 +00:00
this.clearData()
2021-06-29 08:06:20 +00:00
}
2021-06-25 13:55:49 +00:00
2021-07-01 14:16:02 +00:00
if (this.storage().token) {
this.loginByToken(this.storage().token)
2021-06-25 13:55:49 +00:00
} else {
this.allowLoginDialog = true;
}
2021-06-29 08:06:20 +00:00
this.socket.firstConnect = false;
2021-06-25 13:55:49 +00:00
});
},
methods: {
2021-06-27 08:10:55 +00:00
2021-07-11 07:23:28 +00:00
cancelActiveList() {
this.$root.showListMobile = false;
},
onResize() {
this.windowWidth = window.innerWidth;
},
2021-07-01 14:16:02 +00:00
storage() {
return (this.remember) ? localStorage : sessionStorage;
},
2021-06-25 13:55:49 +00:00
getSocket() {
2021-07-18 18:46:45 +00:00
return socket;
2021-06-25 13:55:49 +00:00
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
toastRes(res) {
if (res.ok) {
toast.success(res.msg);
} else {
toast.error(res.msg);
}
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
login(username, password, callback) {
socket.emit("login", {
username,
password,
}, (res) => {
if (res.ok) {
2021-07-01 14:16:02 +00:00
this.storage().token = res.token;
2021-06-25 13:55:49 +00:00
this.socket.token = res.token;
this.loggedIn = true;
// Trigger Chrome Save Password
2021-07-27 17:47:13 +00:00
history.pushState({}, "")
2021-06-25 13:55:49 +00:00
}
callback(res)
})
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
loginByToken(token) {
socket.emit("loginByToken", token, (res) => {
this.allowLoginDialog = true;
if (! res.ok) {
this.logout()
} else {
this.loggedIn = true;
}
})
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
logout() {
2021-07-01 14:16:02 +00:00
this.storage().removeItem("token");
2021-06-25 13:55:49 +00:00
this.socket.token = null;
2021-06-30 13:04:58 +00:00
this.loggedIn = false;
2021-06-25 13:55:49 +00:00
2021-06-30 13:04:58 +00:00
this.clearData()
2021-06-25 13:55:49 +00:00
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
add(monitor, callback) {
socket.emit("add", monitor, callback)
},
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
deleteMonitor(monitorID, callback) {
socket.emit("deleteMonitor", monitorID, callback)
},
2021-06-30 13:04:58 +00:00
clearData() {
console.log("reset heartbeat list")
this.heartbeatList = {}
this.importantHeartbeatList = {}
},
2021-06-25 13:55:49 +00:00
},
computed: {
2021-07-01 13:47:14 +00:00
2021-07-11 07:23:28 +00:00
isMobile() {
return this.windowWidth <= 767.98;
},
2021-07-01 13:47:14 +00:00
timezone() {
if (this.userTimezone === "auto") {
return dayjs.tz.guess()
}
2021-07-27 17:53:59 +00:00
return this.userTimezone
2021-07-01 13:47:14 +00:00
},
2021-06-27 08:10:55 +00:00
lastHeartbeatList() {
let result = {}
for (let monitorID in this.heartbeatList) {
let index = this.heartbeatList[monitorID].length - 1;
result[monitorID] = this.heartbeatList[monitorID][index];
}
return result;
},
statusList() {
let result = {}
2021-06-25 13:55:49 +00:00
2021-06-27 08:10:55 +00:00
let unknown = {
text: "Unknown",
2021-07-27 17:47:13 +00:00
color: "secondary",
2021-06-27 08:10:55 +00:00
}
for (let monitorID in this.lastHeartbeatList) {
let lastHeartBeat = this.lastHeartbeatList[monitorID]
if (! lastHeartBeat) {
result[monitorID] = unknown;
} else if (lastHeartBeat.status === 1) {
result[monitorID] = {
text: "Up",
2021-07-27 17:47:13 +00:00
color: "primary",
2021-06-27 08:10:55 +00:00
};
} else if (lastHeartBeat.status === 0) {
result[monitorID] = {
text: "Down",
2021-07-27 17:47:13 +00:00
color: "danger",
2021-06-27 08:10:55 +00:00
};
} else if (lastHeartBeat.status === 2) {
result[monitorID] = {
text: "Pending",
2021-07-27 17:47:13 +00:00
color: "warning",
};
2021-06-27 08:10:55 +00:00
} else {
result[monitorID] = unknown;
}
}
return result;
2021-07-27 17:47:13 +00:00
},
2021-07-01 14:16:02 +00:00
},
watch: {
2021-07-13 10:08:12 +00:00
// Reload the SPA if the server version is changed.
"info.version"(to, from) {
2021-07-27 17:47:13 +00:00
if (from && from !== to) {
window.location.reload()
}
2021-07-13 10:08:12 +00:00
},
2021-07-01 14:16:02 +00:00
remember() {
localStorage.remember = (this.remember) ? "1" : "0"
2021-07-27 17:47:13 +00:00
},
2021-07-01 14:16:02 +00:00
2021-07-27 17:47:13 +00:00
},
2021-06-25 13:55:49 +00:00
}