Add some more stats
Added a few more stats to keep track of: - Max users in chat - Stream start Also added a `/stats` command to list the user count (current and max), as well as uptime (sever and stream, separately). Resolves #85
This commit is contained in:
parent
83477dc0eb
commit
607e61234c
|
@ -249,6 +249,23 @@ var commands = &CommandControl{
|
||||||
return "", nil
|
return "", nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
common.CNStats.String(): Command{
|
||||||
|
HelpText: "Show some stats for stream.",
|
||||||
|
Function: func(cl *Client, args []string) (string, error) {
|
||||||
|
cl.belongsTo.clientsMtx.Lock()
|
||||||
|
users := len(cl.belongsTo.clients)
|
||||||
|
cl.belongsTo.clientsMtx.Unlock()
|
||||||
|
|
||||||
|
// Just print max users and time alive here
|
||||||
|
return fmt.Sprintf("Current users in chat: <b>%d</b><br />Max users in chat: <b>%d</b><br />Server uptime: <b>%s</b><br />Stream uptime: <b>%s</b>",
|
||||||
|
users,
|
||||||
|
stats.getMaxUsers(),
|
||||||
|
time.Since(stats.start),
|
||||||
|
stats.getStreamLength(),
|
||||||
|
), nil
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
mod: map[string]Command{
|
mod: map[string]Command{
|
||||||
|
|
|
@ -109,6 +109,8 @@ func (cr *ChatRoom) Join(conn *chatConnection, data common.JoinData) (*Client, e
|
||||||
sendHiddenMessage(common.CdJoin, nil)
|
sendHiddenMessage(common.CdJoin, nil)
|
||||||
sendHiddenMessage(common.CdEmote, common.Emotes)
|
sendHiddenMessage(common.CdEmote, common.Emotes)
|
||||||
|
|
||||||
|
stats.updateMaxUsers(len(cr.clients))
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ var (
|
||||||
CNAuth ChatCommandNames = []string{"auth"}
|
CNAuth ChatCommandNames = []string{"auth"}
|
||||||
CNUsers ChatCommandNames = []string{"users"}
|
CNUsers ChatCommandNames = []string{"users"}
|
||||||
CNNick ChatCommandNames = []string{"nick", "name"}
|
CNNick ChatCommandNames = []string{"nick", "name"}
|
||||||
|
CNStats ChatCommandNames = []string{"stats"}
|
||||||
// Mod Commands
|
// Mod Commands
|
||||||
CNSv ChatCommandNames = []string{"sv"}
|
CNSv ChatCommandNames = []string{"sv"}
|
||||||
CNPlaying ChatCommandNames = []string{"playing"}
|
CNPlaying ChatCommandNames = []string{"playing"}
|
||||||
|
@ -50,6 +51,7 @@ var ChatCommands = []ChatCommandNames{
|
||||||
CNAuth,
|
CNAuth,
|
||||||
CNUsers,
|
CNUsers,
|
||||||
CNNick,
|
CNNick,
|
||||||
|
CNStats,
|
||||||
|
|
||||||
// Mod
|
// Mod
|
||||||
CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin,
|
CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin,
|
||||||
|
|
|
@ -373,10 +373,14 @@ func handlePublish(conn *rtmp.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stats.startStream()
|
||||||
|
|
||||||
common.LogInfoln("Stream started")
|
common.LogInfoln("Stream started")
|
||||||
avutil.CopyPackets(ch.que, conn)
|
avutil.CopyPackets(ch.que, conn)
|
||||||
common.LogInfoln("Stream finished")
|
common.LogInfoln("Stream finished")
|
||||||
|
|
||||||
|
stats.endStream()
|
||||||
|
|
||||||
l.Lock()
|
l.Lock()
|
||||||
delete(channels, streamPath)
|
delete(channels, streamPath)
|
||||||
l.Unlock()
|
l.Unlock()
|
||||||
|
|
51
stats.go
51
stats.go
|
@ -10,12 +10,16 @@ import (
|
||||||
type streamStats struct {
|
type streamStats struct {
|
||||||
messageIn int
|
messageIn int
|
||||||
messageOut int
|
messageOut int
|
||||||
|
maxUsers int
|
||||||
start time.Time
|
start time.Time
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
|
||||||
|
streamStart time.Time
|
||||||
|
streamLive bool // True if live
|
||||||
}
|
}
|
||||||
|
|
||||||
func newStreamStats() streamStats {
|
func newStreamStats() streamStats {
|
||||||
return streamStats{start: time.Now()}
|
return streamStats{start: time.Now(), streamLive: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamStats) msgInInc() {
|
func (s *streamStats) msgInInc() {
|
||||||
|
@ -23,14 +27,59 @@ func (s *streamStats) msgInInc() {
|
||||||
s.messageIn++
|
s.messageIn++
|
||||||
s.mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamStats) msgOutInc() {
|
func (s *streamStats) msgOutInc() {
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
s.messageOut++
|
s.messageOut++
|
||||||
s.mutex.Unlock()
|
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() {
|
func (s *streamStats) Print() {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
common.LogInfof("Messages In: %d\n", s.messageIn)
|
common.LogInfof("Messages In: %d\n", s.messageIn)
|
||||||
common.LogInfof("Messages Out: %d\n", s.messageOut)
|
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("Total Time: %s\n", time.Since(s.start))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue