2020-06-23 04:11:56 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
2020-07-23 09:09:11 +03:00
|
|
|
"sync"
|
2020-06-23 04:11:56 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
2020-10-07 09:14:33 +03:00
|
|
|
"github.com/owncast/owncast/core/chat"
|
|
|
|
"github.com/owncast/owncast/geoip"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2020-07-23 09:09:11 +03:00
|
|
|
var l = sync.Mutex{}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
func setupStats() error {
|
|
|
|
s, err := getSavedStats()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_stats = &s
|
|
|
|
|
|
|
|
statsSaveTimer := time.NewTicker(1 * time.Minute)
|
|
|
|
go func() {
|
2020-11-15 05:39:53 +03:00
|
|
|
for range statsSaveTimer.C {
|
|
|
|
if err := saveStatsToFile(); err != nil {
|
|
|
|
panic(err)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// IsStreamConnected checks if the stream is connected or not.
|
2020-06-23 04:11:56 +03: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 02:14:12 +03:00
|
|
|
// So account for that with an artificial buffer of four segments.
|
2020-07-19 01:06:54 +03:00
|
|
|
timeSinceLastConnected := time.Since(_stats.LastConnectTime.Time).Seconds()
|
2020-11-17 08:02:48 +03:00
|
|
|
if timeSinceLastConnected < float64(config.Config.GetVideoSegmentSecondsLength())*2.3 {
|
2020-06-23 04:11:56 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return _stats.StreamConnected
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SetClientActive sets a client as active and connected.
|
2020-10-07 09:14:33 +03:00
|
|
|
func SetClientActive(client models.Client) {
|
2020-07-23 09:09:11 +03:00
|
|
|
l.Lock()
|
2020-10-07 09:14:33 +03:00
|
|
|
// If this clientID already exists then update it.
|
|
|
|
// Otherwise set a new one.
|
|
|
|
if existingClient, ok := _stats.Clients[client.ClientID]; ok {
|
|
|
|
existingClient.LastSeen = time.Now()
|
|
|
|
existingClient.Username = client.Username
|
|
|
|
existingClient.MessageCount = client.MessageCount
|
|
|
|
existingClient.Geo = geoip.GetGeoFromIP(existingClient.IPAddress)
|
|
|
|
_stats.Clients[client.ClientID] = existingClient
|
|
|
|
} else {
|
|
|
|
if client.Geo == nil {
|
|
|
|
geoip.FetchGeoForIP(client.IPAddress)
|
|
|
|
}
|
|
|
|
_stats.Clients[client.ClientID] = client
|
|
|
|
}
|
2020-07-23 09:09:11 +03:00
|
|
|
l.Unlock()
|
2020-09-25 06:27:47 +03:00
|
|
|
|
|
|
|
// Don't update viewer counts if a live stream session is not active.
|
|
|
|
if _stats.StreamConnected {
|
|
|
|
_stats.SessionMaxViewerCount = int(math.Max(float64(len(_stats.Clients)), float64(_stats.SessionMaxViewerCount)))
|
|
|
|
_stats.OverallMaxViewerCount = int(math.Max(float64(_stats.SessionMaxViewerCount), float64(_stats.OverallMaxViewerCount)))
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// RemoveClient removes a client from the active clients record.
|
2020-06-23 04:11:56 +03:00
|
|
|
func RemoveClient(clientID string) {
|
2020-07-07 07:27:31 +03:00
|
|
|
log.Trace("Removing the client:", clientID)
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-10-15 00:07:38 +03:00
|
|
|
l.Lock()
|
2020-06-23 04:11:56 +03:00
|
|
|
delete(_stats.Clients, clientID)
|
2020-10-15 00:07:38 +03:00
|
|
|
l.Unlock()
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 09:14:33 +03:00
|
|
|
func GetClients() []models.Client {
|
|
|
|
clients := make([]models.Client, 0)
|
|
|
|
for _, client := range _stats.Clients {
|
|
|
|
chatClient := chat.GetClient(client.ClientID)
|
|
|
|
if chatClient != nil {
|
|
|
|
clients = append(clients, chatClient.GetViewerClientFromChatClient())
|
|
|
|
} else {
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clients
|
|
|
|
}
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
func saveStatsToFile() error {
|
|
|
|
jsonData, err := json.Marshal(_stats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-13 09:34:49 +03:00
|
|
|
f, err := os.Create(config.StatsFile)
|
2020-06-23 04:11:56 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := f.Write(jsonData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSavedStats() (models.Stats, error) {
|
|
|
|
result := models.Stats{
|
2020-10-07 09:14:33 +03:00
|
|
|
Clients: make(map[string]models.Client),
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 09:34:49 +03:00
|
|
|
if !utils.DoesFileExists(config.StatsFile) {
|
2020-06-23 04:11:56 +03:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 09:34:49 +03:00
|
|
|
jsonFile, err := ioutil.ReadFile(config.StatsFile)
|
2020-06-23 04:11:56 +03:00
|
|
|
if err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
return result, err
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(jsonFile, &result); err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
return result, err
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-06 02:09:57 +03: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
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
return result, err
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|