2018-04-20 22:48:15 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
type StatsValues struct {
|
|
|
|
Unknown int // unregistered clients
|
|
|
|
Total int // registered clients, including invisible
|
|
|
|
Max int // high-water mark of registered clients
|
2018-04-20 22:48:15 +02:00
|
|
|
Invisible int
|
|
|
|
Operators int
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Stats tracks statistics for a running server
|
|
|
|
type Stats struct {
|
|
|
|
StatsValues
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
2018-04-20 22:48:15 +02:00
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Adds an unregistered client
|
|
|
|
func (s *Stats) Add() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.Unknown += 1
|
|
|
|
s.mutex.Unlock()
|
2018-04-20 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
// Activates a registered client, e.g., for the initial attach to a persistent client
|
|
|
|
func (s *Stats) AddRegistered(invisible, operator bool) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
if invisible {
|
|
|
|
s.Invisible += 1
|
|
|
|
}
|
|
|
|
if operator {
|
|
|
|
s.Operators += 1
|
|
|
|
}
|
|
|
|
s.Total += 1
|
|
|
|
s.setMax()
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Transition a client from unregistered to registered
|
|
|
|
func (s *Stats) Register() {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.Unknown -= 1
|
|
|
|
s.Total += 1
|
2020-02-19 01:38:42 +01:00
|
|
|
s.setMax()
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stats) setMax() {
|
2019-07-01 15:21:38 +02:00
|
|
|
if s.Max < s.Total {
|
|
|
|
s.Max = s.Total
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 22:48:15 +02:00
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Modify the Invisible count
|
|
|
|
func (s *Stats) ChangeInvisible(increment int) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.Invisible += increment
|
|
|
|
s.mutex.Unlock()
|
2018-04-20 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Modify the Operator count
|
|
|
|
func (s *Stats) ChangeOperators(increment int) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.Operators += increment
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
2018-04-20 22:48:15 +02:00
|
|
|
|
2019-07-01 15:21:38 +02:00
|
|
|
// Remove a user from the server
|
|
|
|
func (s *Stats) Remove(registered, invisible, operator bool) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
if registered {
|
|
|
|
s.Total -= 1
|
|
|
|
} else {
|
|
|
|
s.Unknown -= 1
|
|
|
|
}
|
|
|
|
if invisible {
|
|
|
|
s.Invisible -= 1
|
|
|
|
}
|
|
|
|
if operator {
|
|
|
|
s.Operators -= 1
|
|
|
|
}
|
|
|
|
s.mutex.Unlock()
|
2018-04-20 22:48:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStats retrives total, invisible and oper count
|
2019-07-01 15:21:38 +02:00
|
|
|
func (s *Stats) GetValues() (result StatsValues) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
result = s.StatsValues
|
|
|
|
s.mutex.Unlock()
|
|
|
|
return
|
2018-04-20 22:48:15 +02:00
|
|
|
}
|