-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
stats.go
132 lines (105 loc) · 2.46 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"sync"
"time"
"github.com/zorchenhimer/MovieNight/common"
)
type streamStats struct {
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, viewers: sessionsMapNew()}
}
func (s *streamStats) msgInInc() {
s.mutex.Lock()
s.messageIn++
s.mutex.Unlock()
}
func (s *streamStats) msgOutInc() {
s.mutex.Lock()
s.messageOut++
s.mutex.Unlock()
}
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
}
func (s *streamStats) Print() {
s.mutex.Lock()
defer s.mutex.Unlock()
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() {
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)
}
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
}