From dcc7a7c5c3a9ef8dfa7e39714f642490d712b6ed Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Tue, 3 Aug 2021 17:47:16 -0700 Subject: [PATCH] Support assigning nil as time instead of setting time as invalid. Fix typo that led to incorrect disconnect time. Maybe addresses #1268? --- controllers/status.go | 4 ++-- core/data/config.go | 13 ++++++------- core/stats.go | 8 ++++---- core/streamState.go | 9 ++++++--- models/stats.go | 8 ++++---- models/status.go | 4 ++-- yp/api.go | 2 +- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/controllers/status.go b/controllers/status.go index 70d48c243..830cce693 100644 --- a/controllers/status.go +++ b/controllers/status.go @@ -33,8 +33,8 @@ type webStatusResponse struct { Online bool `json:"online"` ViewerCount int `json:"viewerCount"` - LastConnectTime utils.NullTime `json:"lastConnectTime"` - LastDisconnectTime utils.NullTime `json:"lastDisconnectTime"` + LastConnectTime *utils.NullTime `json:"lastConnectTime"` + LastDisconnectTime *utils.NullTime `json:"lastDisconnectTime"` VersionNumber string `json:"versionNumber"` StreamTitle string `json:"streamTitle"` diff --git a/core/data/config.go b/core/data/config.go index 0fd01cccc..64b1304ac 100644 --- a/core/data/config.go +++ b/core/data/config.go @@ -327,24 +327,23 @@ func SetPeakOverallViewerCount(count int) error { } // GetLastDisconnectTime will return the time the last stream ended. -func GetLastDisconnectTime() (utils.NullTime, error) { - invalidTime := utils.NullTime{Time: time.Now(), Valid: false} +func GetLastDisconnectTime() (*utils.NullTime, error) { var disconnectTime utils.NullTime configEntry, err := _datastore.Get(lastDisconnectTimeKey) if err != nil { - return invalidTime, err + return nil, err } if err := configEntry.getObject(&disconnectTime); err != nil { - return invalidTime, err + return nil, err } - if !disconnectTime.Valid { - return invalidTime, err + if !disconnectTime.Valid || disconnectTime.Time.IsZero() { + return nil, err } - return disconnectTime, nil + return &disconnectTime, nil } // SetLastDisconnectTime will set the time the last stream ended. diff --git a/core/stats.go b/core/stats.go index b2a3fbda1..d51cf21dc 100644 --- a/core/stats.go +++ b/core/stats.go @@ -102,7 +102,7 @@ func pruneViewerCount() { l.Lock() defer l.Unlock() - + for viewerId := range _stats.Viewers { viewerLastSeenTime := _stats.Viewers[viewerId] if time.Since(viewerLastSeenTime) < _activeViewerPurgeTimeout { @@ -120,8 +120,8 @@ func saveStats() { if err := data.SetPeakSessionViewerCount(_stats.SessionMaxViewerCount); err != nil { log.Errorln("error saving viewer count", err) } - if _stats.LastDisconnectTime.Valid { - if err := data.SetLastDisconnectTime(_stats.LastConnectTime.Time); err != nil { + if _stats.LastDisconnectTime != nil && _stats.LastDisconnectTime.Valid { + if err := data.SetLastDisconnectTime(_stats.LastDisconnectTime.Time); err != nil { log.Errorln("error saving disconnect time", err) } } @@ -140,7 +140,7 @@ func getSavedStats() models.Stats { // If the stats were saved > 5min ago then ignore the // peak session count value, since the session is over. - if !result.LastDisconnectTime.Valid || time.Since(result.LastDisconnectTime.Time).Minutes() > 5 { + if result.LastDisconnectTime == nil || !result.LastDisconnectTime.Valid || time.Since(result.LastDisconnectTime.Time).Minutes() > 5 { result.SessionMaxViewerCount = 0 } diff --git a/core/streamState.go b/core/streamState.go index 556998d20..21b4872e0 100644 --- a/core/streamState.go +++ b/core/streamState.go @@ -32,9 +32,10 @@ var _currentBroadcast *models.CurrentBroadcast // setStreamAsConnected sets the stream as connected. func setStreamAsConnected(rtmpOut *io.PipeReader) { + now := utils.NullTime{Time: time.Now(), Valid: true} _stats.StreamConnected = true - _stats.LastConnectTime = utils.NullTime{Time: time.Now(), Valid: true} - _stats.LastDisconnectTime = utils.NullTime{Time: time.Now(), Valid: false} + _stats.LastDisconnectTime = nil + _stats.LastConnectTime = &now _stats.SessionMaxViewerCount = 0 _currentBroadcast = &models.CurrentBroadcast{ @@ -82,8 +83,10 @@ func setStreamAsConnected(rtmpOut *io.PipeReader) { func SetStreamAsDisconnected() { _ = chat.SendSystemAction("The stream is ending.", true) + now := utils.NullTime{Time: time.Now(), Valid: true} _stats.StreamConnected = false - _stats.LastDisconnectTime = utils.NullTime{Time: time.Now(), Valid: true} + _stats.LastDisconnectTime = &now + _stats.LastConnectTime = nil _broadcaster = nil offlineFilename := "offline.ts" diff --git a/models/stats.go b/models/stats.go index 9ef3f74e2..bfba4a2f4 100644 --- a/models/stats.go +++ b/models/stats.go @@ -8,12 +8,12 @@ import ( // Stats holds the stats for the system. type Stats struct { - SessionMaxViewerCount int `json:"sessionMaxViewerCount"` - OverallMaxViewerCount int `json:"overallMaxViewerCount"` - LastDisconnectTime utils.NullTime `json:"lastDisconnectTime"` + SessionMaxViewerCount int `json:"sessionMaxViewerCount"` + OverallMaxViewerCount int `json:"overallMaxViewerCount"` + LastDisconnectTime *utils.NullTime `json:"lastDisconnectTime"` StreamConnected bool `json:"-"` - LastConnectTime utils.NullTime `json:"-"` + LastConnectTime *utils.NullTime `json:"-"` ChatClients map[string]Client `json:"-"` Viewers map[string]time.Time `json:"-"` } diff --git a/models/status.go b/models/status.go index 96b26dd27..59e7f4fa3 100644 --- a/models/status.go +++ b/models/status.go @@ -9,8 +9,8 @@ type Status struct { OverallMaxViewerCount int `json:"overallMaxViewerCount"` SessionMaxViewerCount int `json:"sessionMaxViewerCount"` - LastConnectTime utils.NullTime `json:"lastConnectTime"` - LastDisconnectTime utils.NullTime `json:"lastDisconnectTime"` + LastConnectTime *utils.NullTime `json:"lastConnectTime"` + LastDisconnectTime *utils.NullTime `json:"lastDisconnectTime"` VersionNumber string `json:"versionNumber"` StreamTitle string `json:"streamTitle"` diff --git a/yp/api.go b/yp/api.go index c8b302694..054497648 100644 --- a/yp/api.go +++ b/yp/api.go @@ -23,7 +23,7 @@ type ypDetailsResponse struct { SessionMaxViewerCount int `json:"sessionMaxViewerCount"` Social []models.SocialHandle `json:"social"` - LastConnectTime utils.NullTime `json:"lastConnectTime"` + LastConnectTime *utils.NullTime `json:"lastConnectTime"` } // GetYPResponse gets the status of the server for YP purposes.