From 848556adbd929e09b4a4cffadde5d7ddf4315dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 15 May 2023 00:28:45 +0200 Subject: [PATCH] add time stats to state. --- internal/session/session.go | 20 ++++++++++++++++++++ pkg/types/session.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/session/session.go b/internal/session/session.go index f5118407..716f1115 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -98,7 +98,13 @@ func (session *SessionCtx) ConnectWebSocketPeer(websocketPeer types.WebSocketPee } session.logger.Info().Msg("set websocket connected") + + // update state + now := time.Now() session.state.IsConnected = true + session.state.ConnectedSince = &now + session.state.NotConnectedSince = nil + session.manager.emmiter.Emit("connected", session) // 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") + + now := time.Now() session.state.IsConnected = false + session.state.ConnectedSince = nil + session.state.NotConnectedSince = &now + session.manager.emmiter.Emit("disconnected", session) session.websocketMu.Lock() @@ -235,7 +246,16 @@ func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, conne Bool("connected", connected). Msg("set webrtc connected") + // update state 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) if connected { diff --git a/pkg/types/session.go b/pkg/types/session.go index a872587e..e901ad7a 100644 --- a/pkg/types/session.go +++ b/pkg/types/session.go @@ -3,6 +3,7 @@ package types import ( "errors" "net/http" + "time" ) var ( @@ -25,7 +26,16 @@ type SessionProfile struct { type SessionState struct { IsConnected bool `json:"is_connected"` - IsWatching bool `json:"is_watching"` + // 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"` + // 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 {