2021-07-27 17:47:13 +00:00
import { io } from "socket.io-client" ;
import { useToast } from "vue-toastification" ;
2022-04-16 17:39:49 +00:00
import jwtDecode from "jwt-decode" ;
2021-10-30 01:25:32 +00:00
import Favico from "favico.js" ;
2022-12-12 14:57:57 +00:00
import dayjs from "dayjs" ;
2023-01-17 09:25:35 +00:00
import { DOWN , MAINTENANCE , PENDING , UP } from "../util.ts" ;
2023-07-17 06:54:40 +00:00
import { getDevContainerServerHostname , isDevContainer } from "../util-frontend.js" ;
2021-09-21 13:22:35 +00:00
const toast = useToast ( ) ;
2021-06-25 13:55:49 +00:00
let socket ;
2021-09-11 11:40:03 +00:00
const noSocketIOPages = [
2022-03-15 04:00:29 +00:00
/^\/status-page$/ , // /status-page
/^\/status/ , // /status**
/^\/$/ // /
2021-09-11 08:22:30 +00:00
] ;
2021-10-30 01:25:32 +00:00
const favicon = new Favico ( {
animation : "none"
} ) ;
2021-06-25 13:55:49 +00:00
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-09-11 08:22:30 +00:00
initedSocketIO : false ,
2021-06-25 13:55:49 +00:00
} ,
2022-04-19 11:40:28 +00:00
username : null ,
2021-07-01 14:16:02 +00:00
remember : ( localStorage . remember !== "0" ) ,
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 : { } ,
2023-02-14 19:49:04 +00:00
maintenanceList : { } ,
apiKeyList : { } ,
2021-07-01 05:11:16 +00:00
heartbeatList : { } ,
importantHeartbeatList : { } ,
avgPingList : { } ,
2021-07-09 06:14:03 +00:00
uptimeList : { } ,
2021-10-01 10:44:32 +00:00
tlsInfoList : { } ,
2021-07-09 06:14:03 +00:00
notificationList : [ ] ,
2022-07-22 15:47:04 +00:00
dockerHostList : [ ] ,
2022-03-17 11:07:05 +00:00
statusPageListLoaded : false ,
2022-03-10 13:34:30 +00:00
statusPageList : [ ] ,
2021-10-30 17:37:15 +00:00
proxyList : [ ] ,
2023-06-11 20:43:33 +00:00
connectionErrorMsg : ` ${ this . $t ( "Cannot connect to the socket server." ) } ${ this . $t ( "Reconnecting..." ) } ` ,
2022-03-22 15:46:13 +00:00
showReverseProxyGuide : true ,
2022-03-29 17:49:45 +00:00
cloudflared : {
cloudflareTunnelToken : "" ,
installed : null ,
running : false ,
message : "" ,
errorMessage : "" ,
2022-03-30 03:59:49 +00:00
currentPassword : "" ,
2023-08-01 07:12:17 +00:00
} ,
faviconUpdateDebounce : null ,
2021-09-21 13:22:35 +00:00
} ;
2021-06-25 13:55:49 +00:00
} ,
created ( ) {
2021-09-11 08:22:30 +00:00
this . initSocketIO ( ) ;
} ,
2021-07-11 07:23:28 +00:00
2021-09-11 08:22:30 +00:00
methods : {
2021-09-11 06:37:05 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Initialize connection to socket server
* @ param { boolean } [ bypass = false ] Should the check for if we
* are on a status page be bypassed ?
* @ returns { ( void | null ) }
* /
2021-09-11 08:22:30 +00:00
initSocketIO ( bypass = false ) {
// No need to re-init
if ( this . socket . initedSocketIO ) {
return ;
}
2021-07-11 06:20:31 +00:00
2021-09-11 08:22:30 +00:00
// No need to connect to the socket.io for status page
2022-03-15 04:00:29 +00:00
if ( ! bypass && location . pathname ) {
for ( let page of noSocketIOPages ) {
if ( location . pathname . match ( page ) ) {
return ;
}
}
2021-09-11 08:22:30 +00:00
}
this . socket . initedSocketIO = true ;
2021-06-25 13:55:49 +00:00
2021-09-11 08:22:30 +00:00
let protocol = ( location . protocol === "https:" ) ? "wss://" : "ws://" ;
2021-07-09 06:14:03 +00:00
2021-09-11 08:22:30 +00:00
let wsHost ;
const env = process . env . NODE _ENV || "production" ;
2023-07-17 06:54:40 +00:00
if ( env === "development" && isDevContainer ( ) ) {
wsHost = protocol + getDevContainerServerHostname ( ) ;
} else if ( env === "development" || localStorage . dev === "dev" ) {
2021-09-11 08:22:30 +00:00
wsHost = protocol + location . hostname + ":3001" ;
} else {
wsHost = protocol + location . host ;
2021-06-27 08:10:55 +00:00
}
2021-09-11 08:22:30 +00:00
socket = io ( wsHost , {
2022-04-17 07:27:35 +00:00
transports : [ "websocket" ] ,
2021-09-11 08:22:30 +00:00
} ) ;
2021-06-30 13:04:58 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "info" , ( info ) => {
this . info = info ;
} ) ;
2021-06-30 13:04:58 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "setup" , ( monitorID , data ) => {
2021-09-21 13:22:35 +00:00
this . $router . push ( "/setup" ) ;
2021-09-11 08:22:30 +00:00
} ) ;
2021-06-30 13:04:58 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "autoLogin" , ( monitorID , data ) => {
this . loggedIn = true ;
this . storage ( ) . token = "autoLogin" ;
2022-04-20 14:39:07 +00:00
this . socket . token = "autoLogin" ;
2021-09-11 08:22:30 +00:00
this . allowLoginDialog = false ;
} ) ;
socket . on ( "monitorList" , ( data ) => {
// Add Helper function
2022-04-17 07:27:35 +00:00
Object . entries ( data ) . forEach ( ( [ monitorID , monitor ] ) => {
2021-09-11 08:22:30 +00:00
monitor . getUrl = ( ) => {
try {
return new URL ( monitor . url ) ;
} catch ( _ ) {
return null ;
}
} ;
} ) ;
this . monitorList = data ;
} ) ;
2022-01-23 14:22:00 +00:00
socket . on ( "maintenanceList" , ( data ) => {
this . maintenanceList = data ;
} ) ;
2023-02-14 19:49:04 +00:00
socket . on ( "apiKeyList" , ( data ) => {
this . apiKeyList = data ;
} ) ;
2021-09-11 08:22:30 +00:00
socket . on ( "notificationList" , ( data ) => {
this . notificationList = data ;
} ) ;
2022-03-10 13:34:30 +00:00
socket . on ( "statusPageList" , ( data ) => {
2022-03-17 11:07:05 +00:00
this . statusPageListLoaded = true ;
2022-03-10 13:34:30 +00:00
this . statusPageList = data ;
} ) ;
2021-10-30 17:37:15 +00:00
socket . on ( "proxyList" , ( data ) => {
this . proxyList = data . map ( item => {
item . auth = ! ! item . auth ;
item . active = ! ! item . active ;
item . default = ! ! item . default ;
return item ;
} ) ;
} ) ;
2022-07-22 15:47:04 +00:00
socket . on ( "dockerHostList" , ( data ) => {
this . dockerHostList = data ;
2022-07-22 15:57:40 +00:00
} ) ;
2022-07-22 15:47:04 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "heartbeat" , ( data ) => {
if ( ! ( data . monitorID in this . heartbeatList ) ) {
this . heartbeatList [ data . monitorID ] = [ ] ;
2021-06-30 13:04:58 +00:00
}
2021-09-16 14:48:28 +00:00
this . heartbeatList [ data . monitorID ] . push ( data ) ;
if ( this . heartbeatList [ data . monitorID ] . length >= 150 ) {
this . heartbeatList [ data . monitorID ] . shift ( ) ;
}
2021-09-11 08:22:30 +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 } ` ) ;
}
2021-06-27 08:10:55 +00:00
2021-09-11 08:22:30 +00:00
if ( ! ( data . monitorID in this . importantHeartbeatList ) ) {
this . importantHeartbeatList [ data . monitorID ] = [ ] ;
}
2021-06-29 08:06:20 +00:00
2021-09-21 13:22:35 +00:00
this . importantHeartbeatList [ data . monitorID ] . unshift ( data ) ;
2021-09-11 08:22:30 +00:00
}
} ) ;
2021-06-30 13:04:58 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "heartbeatList" , ( monitorID , data , overwrite = false ) => {
if ( ! ( monitorID in this . heartbeatList ) || overwrite ) {
this . heartbeatList [ monitorID ] = data ;
} else {
2021-09-21 13:22:35 +00:00
this . heartbeatList [ monitorID ] = data . concat ( this . heartbeatList [ monitorID ] ) ;
2021-09-11 08:22:30 +00:00
}
} ) ;
2021-07-01 05:11:16 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "avgPing" , ( monitorID , data ) => {
2021-09-21 13:22:35 +00:00
this . avgPingList [ monitorID ] = data ;
2021-09-11 08:22:30 +00:00
} ) ;
2021-07-21 04:09:09 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "uptime" , ( monitorID , type , data ) => {
2021-09-21 13:22:35 +00:00
this . uptimeList [ ` ${ monitorID } _ ${ type } ` ] = data ;
2021-09-11 08:22:30 +00:00
} ) ;
2021-06-25 13:55:49 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "certInfo" , ( monitorID , data ) => {
2021-10-01 10:44:32 +00:00
this . tlsInfoList [ monitorID ] = JSON . parse ( data ) ;
2021-09-11 08:22:30 +00:00
} ) ;
2021-07-31 15:41:24 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "importantHeartbeatList" , ( monitorID , data , overwrite ) => {
if ( ! ( monitorID in this . importantHeartbeatList ) || overwrite ) {
this . importantHeartbeatList [ monitorID ] = data ;
2021-08-03 05:07:20 +00:00
} else {
2021-09-21 13:22:35 +00:00
this . importantHeartbeatList [ monitorID ] = data . concat ( this . importantHeartbeatList [ monitorID ] ) ;
2021-09-11 08:22:30 +00:00
}
} ) ;
2021-08-03 05:07:20 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "connect_error" , ( err ) => {
console . error ( ` Failed to connect to the backend. Socket.io connect_error: ${ err . message } ` ) ;
2023-06-11 20:43:33 +00:00
this . connectionErrorMsg = ` ${ this . $t ( "Cannot connect to the socket server." ) } [ ${ err } ] ${ this . $t ( "Reconnecting..." ) } ` ;
2022-03-22 15:46:13 +00:00
this . showReverseProxyGuide = true ;
2021-09-11 08:22:30 +00:00
this . socket . connected = false ;
this . socket . firstConnect = false ;
} ) ;
2021-08-03 05:07:20 +00:00
2021-09-11 08:22:30 +00:00
socket . on ( "disconnect" , ( ) => {
2021-09-21 13:22:35 +00:00
console . log ( "disconnect" ) ;
2021-09-11 08:22:30 +00:00
this . connectionErrorMsg = "Lost connection to the socket server. Reconnecting..." ;
this . socket . connected = false ;
} ) ;
socket . on ( "connect" , ( ) => {
2021-10-05 12:27:43 +00:00
console . log ( "Connected to the socket server" ) ;
2021-09-11 08:22:30 +00:00
this . socket . connectCount ++ ;
this . socket . connected = true ;
2022-03-22 15:46:13 +00:00
this . showReverseProxyGuide = false ;
2021-09-11 08:22:30 +00:00
// Reset Heartbeat list if it is re-connect
if ( this . socket . connectCount >= 2 ) {
2021-09-21 13:22:35 +00:00
this . clearData ( ) ;
2021-07-31 15:41:24 +00:00
}
2021-06-25 13:55:49 +00:00
2021-09-11 08:22:30 +00:00
let token = this . storage ( ) . token ;
2021-06-25 13:55:49 +00:00
2021-09-11 08:22:30 +00:00
if ( token ) {
if ( token !== "autoLogin" ) {
2021-09-21 13:22:35 +00:00
this . loginByToken ( token ) ;
2021-09-11 08:22:30 +00:00
} else {
// Timeout if it is not actually auto login
setTimeout ( ( ) => {
if ( ! this . loggedIn ) {
this . allowLoginDialog = true ;
this . $root . storage ( ) . removeItem ( "token" ) ;
}
} , 5000 ) ;
}
} else {
this . allowLoginDialog = true ;
}
this . socket . firstConnect = false ;
} ) ;
2022-03-29 17:49:45 +00:00
// cloudflared
socket . on ( "cloudflared_installed" , ( res ) => this . cloudflared . installed = res ) ;
socket . on ( "cloudflared_running" , ( res ) => this . cloudflared . running = res ) ;
socket . on ( "cloudflared_message" , ( res ) => this . cloudflared . message = res ) ;
socket . on ( "cloudflared_errorMessage" , ( res ) => this . cloudflared . errorMessage = res ) ;
socket . on ( "cloudflared_token" , ( res ) => this . cloudflared . cloudflareTunnelToken = res ) ;
2022-12-12 14:57:57 +00:00
socket . on ( "initServerTimezone" , ( ) => {
socket . emit ( "initServerTimezone" , dayjs . tz . guess ( ) ) ;
} ) ;
2021-09-11 08:22:30 +00:00
} ,
2021-06-27 08:10:55 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* The storage currently in use
* @ returns { Storage }
* /
2021-07-01 14:16:02 +00:00
storage ( ) {
return ( this . remember ) ? localStorage : sessionStorage ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Get payload of JWT cookie
* @ returns { ( Object | undefined ) }
* /
2021-11-09 14:37:05 +00:00
getJWTPayload ( ) {
const jwtToken = this . $root . storage ( ) . token ;
if ( jwtToken && jwtToken !== "autoLogin" ) {
2022-04-16 17:39:49 +00:00
return jwtDecode ( jwtToken ) ;
2021-11-09 14:37:05 +00:00
}
return undefined ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Get current socket
* @ returns { Socket }
* /
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
2022-06-02 09:38:17 +00:00
/ * *
* Show success or error toast dependant on response status code
* @ param { Object } res Response object
* /
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
2022-06-02 09:38:17 +00:00
/ * *
* Show a success toast
* @ param { string } msg Message to show
* /
2022-03-17 15:38:43 +00:00
toastSuccess ( msg ) {
toast . success ( msg ) ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Show an error toast
* @ param { string } msg Message to show
* /
2022-03-17 15:38:43 +00:00
toastError ( msg ) {
toast . error ( msg ) ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Callback for login
* @ callback loginCB
* @ param { Object } res Response object
* /
/ * *
* Send request to log user in
* @ param { string } username Username to log in with
* @ param { string } password Password to log in with
* @ param { string } token User token
* @ param { loginCB } callback Callback to call with result
* /
2021-09-09 19:10:31 +00:00
login ( username , password , token , callback ) {
2021-06-25 13:55:49 +00:00
socket . emit ( "login" , {
username ,
password ,
2021-09-09 19:10:31 +00:00
token ,
2021-06-25 13:55:49 +00:00
} , ( res ) => {
2021-09-09 19:10:31 +00:00
if ( res . tokenRequired ) {
2021-09-21 13:22:35 +00:00
callback ( res ) ;
2021-09-09 19:10:31 +00:00
}
2021-06-25 13:55:49 +00:00
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 ;
2022-04-19 11:40:28 +00:00
this . username = this . getJWTPayload ( ) ? . username ;
2021-06-25 13:55:49 +00:00
// Trigger Chrome Save Password
2021-09-21 13:22:35 +00:00
history . pushState ( { } , "" ) ;
2021-06-25 13:55:49 +00:00
}
2021-09-21 13:22:35 +00:00
callback ( res ) ;
} ) ;
2021-06-25 13:55:49 +00:00
} ,
2021-06-27 08:10:55 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Log in using a token
* @ param { string } token Token to log in with
* /
2021-06-25 13:55:49 +00:00
loginByToken ( token ) {
socket . emit ( "loginByToken" , token , ( res ) => {
this . allowLoginDialog = true ;
if ( ! res . ok ) {
2021-09-21 13:22:35 +00:00
this . logout ( ) ;
2021-06-25 13:55:49 +00:00
} else {
this . loggedIn = true ;
2022-04-19 11:40:28 +00:00
this . username = this . getJWTPayload ( ) ? . username ;
2021-06-25 13:55:49 +00:00
}
2021-09-21 13:22:35 +00:00
} ) ;
2021-06-25 13:55:49 +00:00
} ,
2021-06-27 08:10:55 +00:00
2022-06-02 09:38:17 +00:00
/** Log out of the web application */
2021-06-25 13:55:49 +00:00
logout ( ) {
2021-10-30 15:33:20 +00:00
socket . emit ( "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 ;
2022-04-19 11:40:28 +00:00
this . username = null ;
2021-09-21 13:22:35 +00:00
this . clearData ( ) ;
2021-06-25 13:55:49 +00:00
} ,
2021-06-27 08:10:55 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Callback for general socket requests
* @ callback socketCB
* @ param { Object } res Result of operation
* /
/** Prepare 2FA configuration */
2021-09-09 19:10:31 +00:00
prepare2FA ( callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "prepare2FA" , callback ) ;
2021-09-09 19:10:31 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Save the current 2 FA configuration
* @ param { any } secret Unused
* @ param { socketCB } callback
* /
2021-09-09 19:10:31 +00:00
save2FA ( secret , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "save2FA" , callback ) ;
2021-09-09 19:10:31 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Disable 2 FA for this user
* @ param { socketCB } callback
* /
2021-09-09 19:10:31 +00:00
disable2FA ( callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "disable2FA" , callback ) ;
2021-09-09 19:10:31 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Verify the provided 2 FA token
* @ param { string } token Token to verify
* @ param { socketCB } callback
* /
2021-09-09 19:10:31 +00:00
verifyToken ( token , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "verifyToken" , token , callback ) ;
2021-09-09 19:10:31 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Get current 2 FA status
* @ param { socketCB } callback
* /
2021-09-09 19:10:31 +00:00
twoFAStatus ( callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "twoFAStatus" , callback ) ;
2021-09-09 19:10:31 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Get list of monitors
* @ param { socketCB } callback
* /
2021-08-26 10:55:19 +00:00
getMonitorList ( callback ) {
2021-10-01 07:47:51 +00:00
if ( ! callback ) {
callback = ( ) => { } ;
}
2021-09-21 13:22:35 +00:00
socket . emit ( "getMonitorList" , callback ) ;
2021-08-26 10:55:19 +00:00
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Get list of maintenances
* @ param { socketCB } callback
* /
2022-01-23 14:22:00 +00:00
getMaintenanceList ( callback ) {
if ( ! callback ) {
callback = ( ) => { } ;
}
socket . emit ( "getMaintenanceList" , callback ) ;
} ,
2023-02-14 19:49:04 +00:00
/ * *
* Send list of API keys
* @ param { socketCB } callback
* /
getAPIKeyList ( callback ) {
if ( ! callback ) {
callback = ( ) => { } ;
}
socket . emit ( "getAPIKeyList" , callback ) ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Add a monitor
* @ param { Object } monitor Object representing monitor to add
* @ param { socketCB } callback
* /
2021-06-25 13:55:49 +00:00
add ( monitor , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "add" , monitor , callback ) ;
2021-06-25 13:55:49 +00:00
} ,
2021-06-27 08:10:55 +00:00
2023-01-05 22:55:51 +00:00
/ * *
* Adds a maintenace
* @ param { Object } maintenance
* @ param { socketCB } callback
* /
2022-01-23 14:22:00 +00:00
addMaintenance ( maintenance , callback ) {
socket . emit ( "addMaintenance" , maintenance , callback ) ;
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Add monitors to maintenance
* @ param { number } maintenanceID
* @ param { number [ ] } monitors
* @ param { socketCB } callback
* /
2022-01-23 14:22:00 +00:00
addMonitorMaintenance ( maintenanceID , monitors , callback ) {
socket . emit ( "addMonitorMaintenance" , maintenanceID , monitors , callback ) ;
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Add status page to maintenance
* @ param { number } maintenanceID
* @ param { number } statusPages
* @ param { socketCB } callback
* /
2022-04-30 15:17:22 +00:00
addMaintenanceStatusPage ( maintenanceID , statusPages , callback ) {
socket . emit ( "addMaintenanceStatusPage" , maintenanceID , statusPages , callback ) ;
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Get monitors affected by maintenance
* @ param { number } maintenanceID
* @ param { socketCB } callback
* /
2022-01-23 14:22:00 +00:00
getMonitorMaintenance ( maintenanceID , callback ) {
socket . emit ( "getMonitorMaintenance" , maintenanceID , callback ) ;
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Get status pages where maintenance is shown
* @ param { number } maintenanceID
* @ param { socketCB } callback
* /
2022-04-30 15:17:22 +00:00
getMaintenanceStatusPage ( maintenanceID , callback ) {
socket . emit ( "getMaintenanceStatusPage" , maintenanceID , callback ) ;
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Delete monitor by ID
* @ param { number } monitorID ID of monitor to delete
* @ param { socketCB } callback
* /
2021-06-25 13:55:49 +00:00
deleteMonitor ( monitorID , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "deleteMonitor" , monitorID , callback ) ;
2021-06-25 13:55:49 +00:00
} ,
2023-01-05 22:55:51 +00:00
/ * *
* Delete specified maintenance
* @ param { number } maintenanceID
* @ param { socketCB } callback
* /
2022-01-23 14:22:00 +00:00
deleteMaintenance ( maintenanceID , callback ) {
socket . emit ( "deleteMaintenance" , maintenanceID , callback ) ;
} ,
2023-02-14 19:49:04 +00:00
/ * *
* Add an API key
* @ param { Object } key API key to add
* @ param { socketCB } callback
* /
addAPIKey ( key , callback ) {
socket . emit ( "addAPIKey" , key , callback ) ;
} ,
/ * *
* Delete specified API key
* @ param { int } keyID ID of key to delete
* @ param { socketCB } callback
* /
deleteAPIKey ( keyID , callback ) {
socket . emit ( "deleteAPIKey" , keyID , callback ) ;
} ,
2022-06-02 09:38:17 +00:00
/** Clear the hearbeat list */
2021-06-30 13:04:58 +00:00
clearData ( ) {
2021-09-21 13:22:35 +00:00
console . log ( "reset heartbeat list" ) ;
this . heartbeatList = { } ;
this . importantHeartbeatList = { } ;
2021-06-30 13:04:58 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Upload the provided backup
* @ param { string } uploadedJSON JSON to upload
* @ param { string } importHandle Type of import . If set to
* most data in database will be replaced
* @ param { socketCB } callback
* /
2021-09-11 19:53:17 +00:00
uploadBackup ( uploadedJSON , importHandle , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "uploadBackup" , uploadedJSON , importHandle , callback ) ;
2021-09-01 15:09:32 +00:00
} ,
2021-09-07 12:12:53 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Clear events for a specified monitor
* @ param { number } monitorID ID of monitor to clear
* @ param { socketCB } callback
* /
2021-08-29 16:47:01 +00:00
clearEvents ( monitorID , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "clearEvents" , monitorID , callback ) ;
2021-08-29 16:47:01 +00:00
} ,
2022-06-02 09:38:17 +00:00
/ * *
* Clear the heartbeats of a specified monitor
* @ param { number } monitorID Id of monitor to clear
* @ param { socketCB } callback
* /
2021-08-29 16:47:01 +00:00
clearHeartbeats ( monitorID , callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "clearHeartbeats" , monitorID , callback ) ;
2021-08-29 16:47:01 +00:00
} ,
2021-08-31 22:36:24 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Clear all statistics
* @ param { socketCB } callback
* /
2021-08-31 22:36:24 +00:00
clearStatistics ( callback ) {
2021-09-21 13:22:35 +00:00
socket . emit ( "clearStatistics" , callback ) ;
2021-08-31 22:36:24 +00:00
} ,
2021-10-18 11:00:39 +00:00
2022-06-02 09:38:17 +00:00
/ * *
* Get monitor beats for a specific monitor in a time range
* @ param { number } monitorID ID of monitor to fetch
* @ param { number } period Time in hours from now
* @ param { socketCB } callback
* /
2021-10-18 11:00:39 +00:00
getMonitorBeats ( monitorID , period , callback ) {
socket . emit ( "getMonitorBeats" , monitorID , period , callback ) ;
}
2021-06-25 13:55:49 +00:00
} ,
computed : {
2021-07-01 13:47:14 +00:00
2022-04-19 11:40:28 +00:00
usernameFirstChar ( ) {
if ( typeof this . username == "string" && this . username . length >= 1 ) {
return this . username . charAt ( 0 ) . toUpperCase ( ) ;
} else {
return "🐻" ;
}
} ,
2021-06-27 08:10:55 +00:00
lastHeartbeatList ( ) {
2021-09-21 13:22:35 +00:00
let result = { } ;
2021-06-27 08:10:55 +00:00
for ( let monitorID in this . heartbeatList ) {
let index = this . heartbeatList [ monitorID ] . length - 1 ;
result [ monitorID ] = this . heartbeatList [ monitorID ] [ index ] ;
}
return result ;
} ,
statusList ( ) {
2021-09-21 13:22:35 +00:00
let result = { } ;
2021-06-25 13:55:49 +00:00
2021-06-27 08:10:55 +00:00
let unknown = {
2021-10-18 16:19:26 +00:00
text : this . $t ( "Unknown" ) ,
2021-07-27 17:47:13 +00:00
color : "secondary" ,
2021-09-21 13:22:35 +00:00
} ;
2021-06-27 08:10:55 +00:00
for ( let monitorID in this . lastHeartbeatList ) {
2021-09-21 13:22:35 +00:00
let lastHeartBeat = this . lastHeartbeatList [ monitorID ] ;
2021-06-27 08:10:55 +00:00
2023-01-17 09:34:47 +00:00
if ( ! lastHeartBeat ) {
2021-06-27 08:10:55 +00:00
result [ monitorID ] = unknown ;
2023-01-17 09:34:47 +00:00
} else if ( lastHeartBeat . status === UP ) {
2021-06-27 08:10:55 +00:00
result [ monitorID ] = {
2021-10-18 16:19:26 +00:00
text : this . $t ( "Up" ) ,
2021-07-27 17:47:13 +00:00
color : "primary" ,
2021-06-27 08:10:55 +00:00
} ;
2023-01-17 09:34:47 +00:00
} else if ( lastHeartBeat . status === DOWN ) {
2021-06-27 08:10:55 +00:00
result [ monitorID ] = {
2021-10-18 16:19:26 +00:00
text : this . $t ( "Down" ) ,
2021-07-27 17:47:13 +00:00
color : "danger" ,
2021-06-27 08:10:55 +00:00
} ;
2023-01-17 09:34:47 +00:00
} else if ( lastHeartBeat . status === PENDING ) {
2021-07-19 16:23:06 +00:00
result [ monitorID ] = {
2021-10-18 16:19:26 +00:00
text : this . $t ( "Pending" ) ,
2021-07-27 17:47:13 +00:00
color : "warning" ,
2021-07-19 16:23:06 +00:00
} ;
2023-01-17 09:34:47 +00:00
} else if ( lastHeartBeat . status === MAINTENANCE ) {
result [ monitorID ] = {
text : this . $t ( "statusMaintenance" ) ,
color : "maintenance" ,
} ;
2021-06-27 08:10:55 +00:00
} else {
result [ monitorID ] = unknown ;
}
}
return result ;
2021-07-27 17:47:13 +00:00
} ,
2022-03-12 07:10:45 +00:00
stats ( ) {
let result = {
2023-06-26 05:23:06 +00:00
active : 0 ,
2022-03-12 07:10:45 +00:00
up : 0 ,
down : 0 ,
2022-04-30 12:33:54 +00:00
maintenance : 0 ,
2023-06-26 05:23:06 +00:00
pending : 0 ,
2022-03-12 07:10:45 +00:00
unknown : 0 ,
pause : 0 ,
} ;
for ( let monitorID in this . $root . monitorList ) {
let beat = this . $root . lastHeartbeatList [ monitorID ] ;
let monitor = this . $root . monitorList [ monitorID ] ;
2023-01-17 09:25:35 +00:00
if ( monitor && ! monitor . active ) {
2022-03-12 07:10:45 +00:00
result . pause ++ ;
} else if ( beat ) {
2023-06-26 05:23:06 +00:00
result . active ++ ;
2023-01-17 09:25:35 +00:00
if ( beat . status === UP ) {
2022-03-12 07:10:45 +00:00
result . up ++ ;
2023-01-17 09:25:35 +00:00
} else if ( beat . status === DOWN ) {
2022-03-12 07:10:45 +00:00
result . down ++ ;
2023-01-17 09:25:35 +00:00
} else if ( beat . status === PENDING ) {
2023-06-26 05:23:06 +00:00
result . pending ++ ;
2023-01-17 09:25:35 +00:00
} else if ( beat . status === MAINTENANCE ) {
result . maintenance ++ ;
2022-03-12 07:10:45 +00:00
} else {
result . unknown ++ ;
}
} else {
result . unknown ++ ;
}
}
return result ;
} ,
2022-06-28 13:55:05 +00:00
/ * *
* Frontend Version
* It should be compiled to a static value while building the frontend .
* Please see . / config / vite . config . js , it is defined via vite . js
* @ returns { string }
* /
frontendVersion ( ) {
// eslint-disable-next-line no-undef
return FRONTEND _VERSION ;
} ,
/ * *
* Are both frontend and backend in the same version ?
* @ returns { boolean }
* /
isFrontendBackendVersionMatched ( ) {
if ( ! this . info . version ) {
return true ;
}
return this . info . version === this . frontendVersion ;
}
2021-07-01 14:16:02 +00:00
} ,
watch : {
2022-03-12 07:10:45 +00:00
// Update Badge
"stats.down" ( to , from ) {
if ( to !== from ) {
2023-08-01 07:12:17 +00:00
if ( this . faviconUpdateDebounce != null ) {
clearTimeout ( this . faviconUpdateDebounce ) ;
}
this . faviconUpdateDebounce = setTimeout ( ( ) => {
favicon . badge ( to ) ;
} , 1000 ) ;
2022-03-12 07:10:45 +00:00
}
} ,
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 ) {
2021-09-21 13:22:35 +00:00
window . location . reload ( ) ;
2021-07-27 17:47:13 +00:00
}
2021-07-13 10:08:12 +00:00
} ,
2021-07-01 14:16:02 +00:00
remember ( ) {
2021-09-21 13:22:35 +00:00
localStorage . remember = ( this . remember ) ? "1" : "0" ;
2021-07-27 17:47:13 +00:00
} ,
2021-07-01 14:16:02 +00:00
2021-09-11 08:22:30 +00:00
// Reconnect the socket io, if status-page to dashboard
"$route.fullPath" ( newValue , oldValue ) {
2022-03-15 04:00:29 +00:00
if ( newValue ) {
for ( let page of noSocketIOPages ) {
if ( newValue . match ( page ) ) {
return ;
}
}
2021-09-11 08:22:30 +00:00
}
2022-03-15 04:00:29 +00:00
2021-09-11 08:22:30 +00:00
this . initSocketIO ( ) ;
} ,
2021-07-27 17:47:13 +00:00
} ,
2021-06-25 13:55:49 +00:00
2021-09-21 13:22:35 +00:00
} ;