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 {
|
|
|
|
messageIn int
|
|
|
|
messageOut int
|
2019-04-14 06:25:49 +02:00
|
|
|
maxUsers int
|
2019-03-21 21:20:50 +01:00
|
|
|
start time.Time
|
|
|
|
mutex sync.Mutex
|
2019-04-14 06:25:49 +02:00
|
|
|
|
|
|
|
streamStart time.Time
|
|
|
|
streamLive bool // True if live
|
2019-03-21 21:20:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStreamStats() streamStats {
|
2019-04-14 06:25:49 +02:00
|
|
|
return streamStats{start: time.Now(), streamLive: false}
|
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()
|
|
|
|
|
2019-03-30 21:18:26 +01:00
|
|
|
common.LogInfof("Messages In: %d\n", s.messageIn)
|
|
|
|
common.LogInfof("Messages Out: %d\n", s.messageOut)
|
2019-04-14 06:25:49 +02:00
|
|
|
common.LogInfof("Max users in chat: %d\n", s.maxUsers)
|
2019-03-30 21:18:26 +01:00
|
|
|
common.LogInfof("Total Time: %s\n", time.Since(s.start))
|
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)
|
|
|
|
}
|