2017-04-10 18:54:12 +02:00
|
|
|
package runtime
|
2016-12-15 11:10:09 +01:00
|
|
|
|
2017-11-21 15:12:06 +01:00
|
|
|
const (
|
|
|
|
DISABLED_AUTOUPDATER = "disabled"
|
2018-01-13 16:40:23 +01:00
|
|
|
GLOBAL_SITE = "global"
|
2018-01-17 20:20:35 +01:00
|
|
|
GLOBAL_DOMAIN = "global"
|
2017-11-21 15:12:06 +01:00
|
|
|
)
|
2017-11-14 19:33:50 +01:00
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// CounterMap to manage multiple values
|
2016-12-15 11:10:09 +01:00
|
|
|
type CounterMap map[string]uint32
|
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
// GlobalStats struct
|
2016-12-15 11:10:09 +01:00
|
|
|
type GlobalStats struct {
|
2019-01-19 18:45:59 +01:00
|
|
|
Clients uint32
|
|
|
|
ClientsWifi uint32
|
|
|
|
ClientsWifi24 uint32
|
|
|
|
ClientsWifi5 uint32
|
|
|
|
Gateways uint32
|
|
|
|
Nodes uint32
|
|
|
|
NodesNoRespondd uint32
|
2016-12-15 11:10:09 +01:00
|
|
|
|
2017-11-14 19:33:50 +01:00
|
|
|
Firmwares CounterMap
|
|
|
|
Models CounterMap
|
|
|
|
Autoupdater CounterMap
|
2016-12-15 11:10:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
//NewGlobalStats returns global statistics for InfluxDB
|
2018-01-17 20:20:35 +01:00
|
|
|
func NewGlobalStats(nodes *Nodes, sitesDomains map[string][]string) (result map[string]map[string]*GlobalStats) {
|
|
|
|
result = make(map[string]map[string]*GlobalStats)
|
2017-11-21 15:12:06 +01:00
|
|
|
|
2018-01-17 20:20:35 +01:00
|
|
|
result[GLOBAL_SITE] = make(map[string]*GlobalStats)
|
|
|
|
result[GLOBAL_SITE][GLOBAL_DOMAIN] = &GlobalStats{
|
2017-11-14 19:33:50 +01:00
|
|
|
Firmwares: make(CounterMap),
|
|
|
|
Models: make(CounterMap),
|
|
|
|
Autoupdater: make(CounterMap),
|
2016-12-15 11:10:09 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 20:20:35 +01:00
|
|
|
for site, domains := range sitesDomains {
|
|
|
|
result[site] = make(map[string]*GlobalStats)
|
|
|
|
result[site][GLOBAL_DOMAIN] = &GlobalStats{
|
2017-11-21 15:12:06 +01:00
|
|
|
Firmwares: make(CounterMap),
|
|
|
|
Models: make(CounterMap),
|
|
|
|
Autoupdater: make(CounterMap),
|
|
|
|
}
|
2018-01-17 20:20:35 +01:00
|
|
|
for _, domain := range domains {
|
|
|
|
result[site][domain] = &GlobalStats{
|
|
|
|
Firmwares: make(CounterMap),
|
|
|
|
Models: make(CounterMap),
|
|
|
|
Autoupdater: make(CounterMap),
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 15:12:06 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 14:46:29 +02:00
|
|
|
nodes.RLock()
|
2016-12-15 11:10:09 +01:00
|
|
|
for _, node := range nodes.List {
|
2017-04-10 18:54:12 +02:00
|
|
|
if node.Online {
|
2018-01-17 20:20:35 +01:00
|
|
|
result[GLOBAL_SITE][GLOBAL_DOMAIN].Add(node)
|
2017-11-21 15:12:06 +01:00
|
|
|
|
2016-12-15 11:10:09 +01:00
|
|
|
if info := node.Nodeinfo; info != nil {
|
2017-11-21 15:12:06 +01:00
|
|
|
site := info.System.SiteCode
|
2018-01-17 20:20:35 +01:00
|
|
|
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)
|
|
|
|
}
|
2017-11-14 19:33:50 +01:00
|
|
|
}
|
2016-12-15 11:10:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 14:46:29 +02:00
|
|
|
nodes.RUnlock()
|
2016-12-15 11:10:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:12:06 +01:00
|
|
|
// 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
|
|
|
|
}
|
2019-01-19 18:45:59 +01:00
|
|
|
if node.NoRespondd {
|
|
|
|
s.NodesNoRespondd++
|
|
|
|
}
|
2017-11-21 15:12:06 +01:00
|
|
|
if node.IsGateway() {
|
|
|
|
s.Gateways++
|
|
|
|
}
|
|
|
|
if info := node.Nodeinfo; info != nil {
|
|
|
|
s.Models.Increment(info.Hardware.Model)
|
|
|
|
s.Firmwares.Increment(info.Software.Firmware.Release)
|
|
|
|
if info.Software.Autoupdater.Enabled {
|
|
|
|
s.Autoupdater.Increment(info.Software.Autoupdater.Branch)
|
|
|
|
} else {
|
|
|
|
s.Autoupdater.Increment(DISABLED_AUTOUPDATER)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 11:10:09 +01:00
|
|
|
// 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]++
|
2016-12-15 11:10:09 +01:00
|
|
|
}
|
|
|
|
}
|