2019-03-21 21:20:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-03-30 21:20:06 +01:00
|
|
|
|
|
|
|
"github.com/zorchenhimer/MovieNight/common"
|
2019-03-21 21:20:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type streamStats struct {
|
2021-03-14 03:13:20 +01:00
|
|
|
messageIn int
|
|
|
|
messageOut int
|
|
|
|
maxUsers int
|
|
|
|
start time.Time
|
|
|
|
mutex sync.Mutex
|
2019-04-14 06:25:49 +02:00
|
|
|
streamStart time.Time
|
|
|
|
streamLive bool // True if live
|
2021-03-20 22:32:13 +01:00
|
|
|
viewers map[string]int
|
2021-03-14 03:13:20 +01:00
|
|
|
maxViewers int
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:32:13 +01:00
|
|
|
func (s *streamStats) addViewer(id string) {
|
2021-03-14 03:13:20 +01:00
|
|
|
s.mutex.Lock()
|
2021-03-20 22:32:13 +01:00
|
|
|
s.viewers[id] = len(s.viewers)
|
2021-03-16 22:43:55 +01:00
|
|
|
size := len(s.viewers)
|
|
|
|
s.updateMaxViewers(size)
|
2021-03-14 03:13:20 +01:00
|
|
|
s.mutex.Unlock()
|
|
|
|
|
2021-03-16 22:43:55 +01:00
|
|
|
common.LogDebugf("[stats] %d viewer(s) connected\n", size)
|
2021-03-14 03:13:20 +01:00
|
|
|
}
|
2021-03-20 22:32:13 +01:00
|
|
|
func (s *streamStats) removeViewer(id string) {
|
2021-03-14 03:13:20 +01:00
|
|
|
s.mutex.Lock()
|
2021-03-20 22:32:13 +01:00
|
|
|
delete(s.viewers, id)
|
2021-03-14 03:13:20 +01:00
|
|
|
s.mutex.Unlock()
|
|
|
|
|
2021-03-16 22:43:55 +01:00
|
|
|
common.LogDebugf("[stats] One viewer left the stream\n")
|
2021-03-14 03:13:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) updateMaxViewers(size int) {
|
|
|
|
if s.maxViewers < size {
|
|
|
|
s.maxViewers = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) resetViewers() {
|
2021-03-16 22:43:55 +01:00
|
|
|
s.viewers = sessionsMapNew()
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:32:13 +01:00
|
|
|
func sessionsMapNew() map[string]int {
|
|
|
|
return make(map[string]int)
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStreamStats() streamStats {
|
2021-03-16 22:43:55 +01:00
|
|
|
return streamStats{start: time.Now(), streamLive: false, viewers: sessionsMapNew()}
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) msgInInc() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.messageIn++
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
2019-04-14 06:25:49 +02:00
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
func (s *streamStats) msgOutInc() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.messageOut++
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-14 06:25:49 +02:00
|
|
|
func (s *streamStats) updateMaxUsers(count int) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
if count > s.maxUsers {
|
|
|
|
s.maxUsers = count
|
|
|
|
}
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) getMaxUsers() int {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
return s.maxUsers
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:20:50 +01:00
|
|
|
func (s *streamStats) Print() {
|
2019-04-14 06:25:49 +02:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2021-03-16 22:43:55 +01:00
|
|
|
common.LogInfof("[stats] Messages In: %d\n", s.messageIn)
|
|
|
|
common.LogInfof("[stats] Messages Out: %d\n", s.messageOut)
|
|
|
|
common.LogInfof("[stats] Max users in chat: %d\n", s.maxUsers)
|
|
|
|
common.LogInfof("[stats] Total Time: %s\n", time.Since(s.start))
|
|
|
|
common.LogInfof("[stats] Max Stream Viewer: %d\n", s.maxViewers)
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
2019-04-14 06:25:49 +02:00
|
|
|
|
|
|
|
func (s *streamStats) startStream() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
s.streamLive = true
|
|
|
|
s.streamStart = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) endStream() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
s.streamLive = false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) getStreamLength() time.Duration {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
if !s.streamLive {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return time.Since(s.streamStart)
|
|
|
|
}
|
2021-03-14 03:13:20 +01:00
|
|
|
|
|
|
|
func (s *streamStats) getViewerCount() int {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
return len(s.viewers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *streamStats) getMaxViewerCount() int {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
return s.maxViewers
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:32:13 +01:00
|
|
|
func (s *streamStats) getViewers() map[string]int {
|
2021-03-14 03:13:20 +01:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
return s.viewers
|
|
|
|
}
|