2021-06-25 13:55:49 +00:00
import { io } from "socket.io-client" ;
import { useToast } from 'vue-toastification'
const toast = useToast ( )
let storage = localStorage ;
let socket ;
export default {
data ( ) {
return {
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
} ,
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 ,
monitorList : [
] ,
2021-06-27 08:10:55 +00:00
heartbeatList : {
} ,
2021-06-25 13:55:49 +00:00
}
} ,
created ( ) {
socket = io ( "http://localhost:3001" , {
transports : [ 'websocket' ]
} ) ;
socket . on ( 'monitorList' , ( data ) => {
this . monitorList = data ;
} ) ;
2021-06-27 08:10:55 +00:00
socket . on ( 'heartbeat' , ( data ) => {
if ( ! ( data . monitorID in this . heartbeatList ) ) {
this . heartbeatList [ data . monitorID ] = [ ] ;
}
this . heartbeatList [ data . monitorID ] . push ( data )
} ) ;
2021-06-29 08:06:20 +00:00
socket . on ( 'heartbeatList' , ( monitorID , data ) => {
if ( ! ( monitorID in this . heartbeatList ) ) {
this . heartbeatList [ monitorID ] = data ;
} else {
this . heartbeatList [ monitorID ] = data . concat ( this . heartbeatList [ monitorID ] )
}
} ) ;
2021-06-27 08:10:55 +00:00
2021-06-25 13:55:49 +00:00
socket . on ( 'disconnect' , ( ) => {
2021-06-29 08:06:20 +00:00
console . log ( "disconnect" )
2021-06-25 13:55:49 +00:00
this . socket . connected = false ;
} ) ;
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 ) {
console . log ( "reset heartbeat list" )
this . heartbeatList = { }
}
2021-06-25 13:55:49 +00:00
if ( storage . token ) {
this . loginByToken ( storage . token )
} 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-06-25 13:55:49 +00:00
getSocket ( ) {
return socket ;
} ,
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 ) {
storage . token = res . token ;
this . socket . token = res . token ;
this . loggedIn = true ;
// Trigger Chrome Save Password
history . pushState ( { } , '' )
}
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 ( ) {
storage . removeItem ( "token" ) ;
this . socket . token = null ;
socket . emit ( "logout" , ( ) => {
2021-06-29 08:06:20 +00:00
window . location . reload ( )
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 )
} ,
} ,
computed : {
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 ;
} ,
2021-06-29 08:06:20 +00:00
// TODO: handle history + real time
importantHeartbeatList ( ) {
let result = { }
for ( let monitorID in this . heartbeatList ) {
result [ monitorID ] = [ ] ;
let index = this . heartbeatList [ monitorID ] . length - 1 ;
let list = this . heartbeatList [ monitorID ] ;
for ( let heartbeat of list ) {
if ( heartbeat . important ) {
result [ monitorID ] . push ( heartbeat )
}
}
}
return result ;
} ,
2021-06-27 08:10:55 +00:00
statusList ( ) {
let result = { }
2021-06-25 13:55:49 +00:00
2021-06-27 08:10:55 +00:00
let unknown = {
text : "Unknown" ,
color : "secondary"
}
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" ,
color : "primary"
} ;
} else if ( lastHeartBeat . status === 0 ) {
result [ monitorID ] = {
text : "Down" ,
color : "danger"
} ;
} else {
result [ monitorID ] = unknown ;
}
}
return result ;
}
2021-06-25 13:55:49 +00:00
}
}