Support assigning nil as time instead of setting time as invalid. Fix typo that led to incorrect disconnect time. Maybe addresses #1268?

This commit is contained in:
Gabe Kangas 2021-08-03 17:47:16 -07:00
parent cab963f21c
commit dcc7a7c5c3
7 changed files with 25 additions and 23 deletions

View File

@ -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"`

View File

@ -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.

View File

@ -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
}

View File

@ -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"

View File

@ -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:"-"`
}

View File

@ -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"`

View File

@ -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.