add time stats to state.

This commit is contained in:
Miroslav Šedivý 2023-05-15 00:28:45 +02:00
parent 82401391c8
commit 848556adbd
2 changed files with 31 additions and 1 deletions

View File

@ -98,7 +98,13 @@ func (session *SessionCtx) ConnectWebSocketPeer(websocketPeer types.WebSocketPee
} }
session.logger.Info().Msg("set websocket connected") session.logger.Info().Msg("set websocket connected")
// update state
now := time.Now()
session.state.IsConnected = true session.state.IsConnected = true
session.state.ConnectedSince = &now
session.state.NotConnectedSince = nil
session.manager.emmiter.Emit("connected", session) session.manager.emmiter.Emit("connected", session)
// if there is a previous peer, destroy it // if there is a previous peer, destroy it
@ -154,7 +160,12 @@ func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocket
// //
session.logger.Info().Msg("set websocket disconnected") session.logger.Info().Msg("set websocket disconnected")
now := time.Now()
session.state.IsConnected = false session.state.IsConnected = false
session.state.ConnectedSince = nil
session.state.NotConnectedSince = &now
session.manager.emmiter.Emit("disconnected", session) session.manager.emmiter.Emit("disconnected", session)
session.websocketMu.Lock() session.websocketMu.Lock()
@ -235,7 +246,16 @@ func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, conne
Bool("connected", connected). Bool("connected", connected).
Msg("set webrtc connected") Msg("set webrtc connected")
// update state
session.state.IsWatching = connected session.state.IsWatching = connected
if now := time.Now(); connected {
session.state.WatchingSince = &now
session.state.NotWatchingSince = nil
} else {
session.state.WatchingSince = nil
session.state.NotWatchingSince = &now
}
session.manager.emmiter.Emit("state_changed", session) session.manager.emmiter.Emit("state_changed", session)
if connected { if connected {

View File

@ -3,6 +3,7 @@ package types
import ( import (
"errors" "errors"
"net/http" "net/http"
"time"
) )
var ( var (
@ -25,7 +26,16 @@ type SessionProfile struct {
type SessionState struct { type SessionState struct {
IsConnected bool `json:"is_connected"` IsConnected bool `json:"is_connected"`
// when the session was last connected
ConnectedSince *time.Time `json:"connected_since,omitempty"`
// when the session was last not connected
NotConnectedSince *time.Time `json:"not_connected_since,omitempty"`
IsWatching bool `json:"is_watching"` IsWatching bool `json:"is_watching"`
// when the session was last watching
WatchingSince *time.Time `json:"watching_since,omitempty"`
// when the session was last not watching
NotWatchingSince *time.Time `json:"not_watching_since,omitempty"`
} }
type Settings struct { type Settings struct {