2020-06-23 03:11:56 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
2020-07-23 08:09:11 +02:00
|
|
|
"sync"
|
2020-06-23 03:11:56 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
"github.com/owncast/owncast/core/chat"
|
2021-02-19 08:05:52 +01:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-07 08:14:33 +02:00
|
|
|
"github.com/owncast/owncast/geoip"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/models"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2021-03-04 05:44:13 +01:00
|
|
|
var l = &sync.RWMutex{}
|
2021-05-21 05:29:01 +02:00
|
|
|
var _activeViewerPurgeTimeout = time.Second * 10
|
2020-07-23 08:09:11 +02:00
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
func setupStats() error {
|
2021-02-19 08:05:52 +01:00
|
|
|
s := getSavedStats()
|
2020-06-23 03:11:56 +02:00
|
|
|
_stats = &s
|
|
|
|
|
|
|
|
statsSaveTimer := time.NewTicker(1 * time.Minute)
|
|
|
|
go func() {
|
2020-11-15 03:39:53 +01:00
|
|
|
for range statsSaveTimer.C {
|
2021-02-19 08:05:52 +01:00
|
|
|
if err := saveStats(); err != nil {
|
2020-11-15 03:39:53 +01:00
|
|
|
panic(err)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-05-21 05:29:01 +02:00
|
|
|
viewerCountPruneTimer := time.NewTicker(5 * time.Second)
|
|
|
|
go func() {
|
|
|
|
for range viewerCountPruneTimer.C {
|
|
|
|
pruneViewerCount()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// IsStreamConnected checks if the stream is connected or not.
|
2020-06-23 03:11:56 +02:00
|
|
|
func IsStreamConnected() bool {
|
|
|
|
if !_stats.StreamConnected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kind of a hack. It takes a handful of seconds between a RTMP connection and when HLS data is available.
|
2020-07-16 01:14:12 +02:00
|
|
|
// So account for that with an artificial buffer of four segments.
|
2020-07-19 00:06:54 +02:00
|
|
|
timeSinceLastConnected := time.Since(_stats.LastConnectTime.Time).Seconds()
|
2021-02-19 08:05:52 +01:00
|
|
|
waitTime := math.Max(float64(data.GetStreamLatencyLevel().SecondsPerSegment)*3.0, 7)
|
|
|
|
if timeSinceLastConnected < waitTime {
|
2020-06-23 03:11:56 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return _stats.StreamConnected
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:29:01 +02:00
|
|
|
// SetChatClientActive sets a client as active and connected.
|
|
|
|
func SetChatClientActive(client models.Client) {
|
2020-07-23 08:09:11 +02:00
|
|
|
l.Lock()
|
2021-03-04 05:44:13 +01:00
|
|
|
defer l.Unlock()
|
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
// If this clientID already exists then update it.
|
|
|
|
// Otherwise set a new one.
|
2021-05-21 05:29:01 +02:00
|
|
|
if existingClient, ok := _stats.ChatClients[client.ClientID]; ok {
|
2020-10-07 08:14:33 +02:00
|
|
|
existingClient.LastSeen = time.Now()
|
|
|
|
existingClient.Username = client.Username
|
|
|
|
existingClient.MessageCount = client.MessageCount
|
|
|
|
existingClient.Geo = geoip.GetGeoFromIP(existingClient.IPAddress)
|
2021-05-21 05:29:01 +02:00
|
|
|
_stats.ChatClients[client.ClientID] = existingClient
|
2020-10-07 08:14:33 +02:00
|
|
|
} else {
|
|
|
|
if client.Geo == nil {
|
|
|
|
geoip.FetchGeoForIP(client.IPAddress)
|
|
|
|
}
|
2021-05-21 05:29:01 +02:00
|
|
|
_stats.ChatClients[client.ClientID] = client
|
2020-09-25 05:27:47 +02:00
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 05:29:01 +02:00
|
|
|
// RemoveChatClient removes a client from the active clients record.
|
|
|
|
func RemoveChatClient(clientID string) {
|
2020-07-07 06:27:31 +02:00
|
|
|
log.Trace("Removing the client:", clientID)
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-10-14 23:07:38 +02:00
|
|
|
l.Lock()
|
2021-05-21 05:29:01 +02:00
|
|
|
delete(_stats.ChatClients, clientID)
|
2020-10-14 23:07:38 +02:00
|
|
|
l.Unlock()
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 05:29:01 +02:00
|
|
|
func GetChatClients() []models.Client {
|
2021-03-04 05:44:13 +01:00
|
|
|
l.RLock()
|
2020-10-07 08:14:33 +02:00
|
|
|
clients := make([]models.Client, 0)
|
2021-05-21 05:29:01 +02:00
|
|
|
for _, client := range _stats.ChatClients {
|
2020-10-07 08:14:33 +02:00
|
|
|
chatClient := chat.GetClient(client.ClientID)
|
|
|
|
if chatClient != nil {
|
|
|
|
clients = append(clients, chatClient.GetViewerClientFromChatClient())
|
|
|
|
} else {
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 05:44:13 +01:00
|
|
|
l.RUnlock()
|
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
return clients
|
|
|
|
}
|
|
|
|
|
2021-05-21 05:29:01 +02:00
|
|
|
// SetViewerIdActive sets a client as active and connected.
|
|
|
|
func SetViewerIdActive(id string) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
_stats.Viewers[id] = time.Now()
|
|
|
|
|
|
|
|
// Don't update viewer counts if a live stream session is not active.
|
|
|
|
if _stats.StreamConnected {
|
|
|
|
_stats.SessionMaxViewerCount = int(math.Max(float64(len(_stats.Viewers)), float64(_stats.SessionMaxViewerCount)))
|
|
|
|
_stats.OverallMaxViewerCount = int(math.Max(float64(_stats.SessionMaxViewerCount), float64(_stats.OverallMaxViewerCount)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pruneViewerCount() {
|
|
|
|
viewers := make(map[string]time.Time)
|
|
|
|
|
|
|
|
for viewerId := range _stats.Viewers {
|
|
|
|
viewerLastSeenTime := _stats.Viewers[viewerId]
|
|
|
|
if time.Since(viewerLastSeenTime) < _activeViewerPurgeTimeout {
|
|
|
|
viewers[viewerId] = viewerLastSeenTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stats.Viewers = viewers
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
func saveStats() error {
|
|
|
|
if err := data.SetPeakOverallViewerCount(_stats.OverallMaxViewerCount); err != nil {
|
|
|
|
log.Errorln("error saving viewer count", err)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2021-02-19 08:05:52 +01:00
|
|
|
if err := data.SetPeakSessionViewerCount(_stats.SessionMaxViewerCount); err != nil {
|
|
|
|
log.Errorln("error saving viewer count", err)
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
2021-06-29 20:38:13 +02:00
|
|
|
if _stats.LastDisconnectTime.Valid {
|
|
|
|
if err := data.SetLastDisconnectTime(_stats.LastConnectTime.Time); err != nil {
|
|
|
|
log.Errorln("error saving disconnect time", err)
|
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
func getSavedStats() models.Stats {
|
2021-06-28 22:59:23 +02:00
|
|
|
savedLastDisconnectTime, _ := data.GetLastDisconnectTime()
|
2021-06-20 20:30:29 +02:00
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
result := models.Stats{
|
2021-05-21 05:29:01 +02:00
|
|
|
ChatClients: make(map[string]models.Client),
|
|
|
|
Viewers: make(map[string]time.Time),
|
2021-02-19 08:05:52 +01:00
|
|
|
SessionMaxViewerCount: data.GetPeakSessionViewerCount(),
|
|
|
|
OverallMaxViewerCount: data.GetPeakOverallViewerCount(),
|
2021-06-28 22:59:23 +02:00
|
|
|
LastDisconnectTime: savedLastDisconnectTime,
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 00:09:57 +01:00
|
|
|
// 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 {
|
|
|
|
result.SessionMaxViewerCount = 0
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:05:52 +01:00
|
|
|
return result
|
2020-06-23 03:11:56 +02:00
|
|
|
}
|