yanic/runtime/stats.go

113 lines
2.8 KiB
Go
Raw Normal View History

package runtime
const (
DISABLED_AUTOUPDATER = "disabled"
2018-01-13 16:40:23 +01:00
GLOBAL_SITE = "global"
GLOBAL_DOMAIN = "global"
)
// CounterMap to manage multiple values
type CounterMap map[string]uint32
// GlobalStats struct
type GlobalStats struct {
Clients uint32
ClientsWifi uint32
ClientsWifi24 uint32
ClientsWifi5 uint32
ClientsOWE uint32
ClientsOWE24 uint32
ClientsOWE5 uint32
Gateways uint32
Nodes uint32
Firmwares CounterMap
Models CounterMap
Autoupdater CounterMap
}
//NewGlobalStats returns global statistics for InfluxDB
func NewGlobalStats(nodes *Nodes, sitesDomains map[string][]string) (result map[string]map[string]*GlobalStats) {
result = make(map[string]map[string]*GlobalStats)
result[GLOBAL_SITE] = make(map[string]*GlobalStats)
result[GLOBAL_SITE][GLOBAL_DOMAIN] = &GlobalStats{
Firmwares: make(CounterMap),
Models: make(CounterMap),
Autoupdater: make(CounterMap),
}
for site, domains := range sitesDomains {
result[site] = make(map[string]*GlobalStats)
result[site][GLOBAL_DOMAIN] = &GlobalStats{
Firmwares: make(CounterMap),
Models: make(CounterMap),
Autoupdater: make(CounterMap),
}
for _, domain := range domains {
result[site][domain] = &GlobalStats{
Firmwares: make(CounterMap),
Models: make(CounterMap),
Autoupdater: make(CounterMap),
}
}
}
nodes.RLock()
for _, node := range nodes.List {
if node.Online {
result[GLOBAL_SITE][GLOBAL_DOMAIN].Add(node)
if info := node.Nodeinfo; info != nil {
site := info.System.SiteCode
domain := info.System.DomainCode
if _, ok := result[site]; ok {
result[site][GLOBAL_DOMAIN].Add(node)
if _, ok := result[site][domain]; ok {
result[site][domain].Add(node)
}
}
}
}
}
nodes.RUnlock()
return
}
// Add values to GlobalStats
// if node is online
func (s *GlobalStats) Add(node *Node) {
s.Nodes++
if stats := node.Statistics; stats != nil {
s.Clients += stats.Clients.Total
s.ClientsWifi24 += stats.Clients.Wifi24
s.ClientsWifi5 += stats.Clients.Wifi5
s.ClientsWifi += stats.Clients.Wifi
s.ClientsOWE24 += stats.Clients.OWE24
s.ClientsOWE5 += stats.Clients.OWE5
s.ClientsOWE += stats.Clients.OWE
}
if node.IsGateway() {
s.Gateways++
}
if info := node.Nodeinfo; info != nil {
s.Models.Increment(info.Hardware.Model)
2021-02-17 23:35:12 +01:00
if info.Software.Firmware != nil {
s.Firmwares.Increment(info.Software.Firmware.Release)
}
if info.Software.Autoupdater != nil && info.Software.Autoupdater.Enabled {
s.Autoupdater.Increment(info.Software.Autoupdater.Branch)
} else {
s.Autoupdater.Increment(DISABLED_AUTOUPDATER)
}
}
}
// Increment counter in the map by one
// if the value is not empty
func (m CounterMap) Increment(key string) {
if key != "" {
2017-04-17 20:42:06 +02:00
m[key]++
}
}