|
|
@ -8,18 +8,50 @@ import ( |
|
|
|
) |
|
|
|
|
|
|
|
type streamStats struct { |
|
|
|
messageIn int |
|
|
|
messageOut int |
|
|
|
maxUsers int |
|
|
|
start time.Time |
|
|
|
mutex sync.Mutex |
|
|
|
|
|
|
|
messageIn int |
|
|
|
messageOut int |
|
|
|
maxUsers int |
|
|
|
start time.Time |
|
|
|
mutex sync.Mutex |
|
|
|
streamStart time.Time |
|
|
|
streamLive bool // True if live
|
|
|
|
viewers map[string]int |
|
|
|
maxViewers int |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) addViewer(id string) { |
|
|
|
s.mutex.Lock() |
|
|
|
s.viewers[id] = len(s.viewers) |
|
|
|
size := len(s.viewers) |
|
|
|
s.updateMaxViewers(size) |
|
|
|
s.mutex.Unlock() |
|
|
|
|
|
|
|
common.LogDebugf("[stats] %d viewer(s) connected\n", size) |
|
|
|
} |
|
|
|
func (s *streamStats) removeViewer(id string) { |
|
|
|
s.mutex.Lock() |
|
|
|
delete(s.viewers, id) |
|
|
|
s.mutex.Unlock() |
|
|
|
|
|
|
|
common.LogDebugf("[stats] One viewer left the stream\n") |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) updateMaxViewers(size int) { |
|
|
|
if s.maxViewers < size { |
|
|
|
s.maxViewers = size |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) resetViewers() { |
|
|
|
s.viewers = sessionsMapNew() |
|
|
|
} |
|
|
|
|
|
|
|
func sessionsMapNew() map[string]int { |
|
|
|
return make(map[string]int) |
|
|
|
} |
|
|
|
|
|
|
|
func newStreamStats() streamStats { |
|
|
|
return streamStats{start: time.Now(), streamLive: false} |
|
|
|
return streamStats{start: time.Now(), streamLive: false, viewers: sessionsMapNew()} |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) msgInInc() { |
|
|
@ -53,10 +85,11 @@ func (s *streamStats) Print() { |
|
|
|
s.mutex.Lock() |
|
|
|
defer s.mutex.Unlock() |
|
|
|
|
|
|
|
common.LogInfof("Messages In: %d\n", s.messageIn) |
|
|
|
common.LogInfof("Messages Out: %d\n", s.messageOut) |
|
|
|
common.LogInfof("Max users in chat: %d\n", s.maxUsers) |
|
|
|
common.LogInfof("Total Time: %s\n", time.Since(s.start)) |
|
|
|
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) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) startStream() { |
|
|
@ -83,3 +116,24 @@ func (s *streamStats) getStreamLength() time.Duration { |
|
|
|
} |
|
|
|
return time.Since(s.streamStart) |
|
|
|
} |
|
|
|
|
|
|
|
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 |
|
|
|
} |
|
|
|
|
|
|
|
func (s *streamStats) getViewers() map[string]int { |
|
|
|
s.mutex.Lock() |
|
|
|
defer s.mutex.Unlock() |
|
|
|
|
|
|
|
return s.viewers |
|
|
|
} |
|
|
|