2017-04-10 16:54:12 +00:00
|
|
|
package runtime
|
2016-12-15 10:10:09 +00:00
|
|
|
|
2017-11-14 18:33:50 +00:00
|
|
|
const DISABLED_AUTOUPDATER = "disabled"
|
|
|
|
|
2017-01-20 21:27:44 +00:00
|
|
|
// CounterMap to manage multiple values
|
2016-12-15 10:10:09 +00:00
|
|
|
type CounterMap map[string]uint32
|
|
|
|
|
2017-01-20 21:27:44 +00:00
|
|
|
// GlobalStats struct
|
2016-12-15 10:10:09 +00:00
|
|
|
type GlobalStats struct {
|
|
|
|
Clients uint32
|
|
|
|
ClientsWifi uint32
|
|
|
|
ClientsWifi24 uint32
|
|
|
|
ClientsWifi5 uint32
|
|
|
|
Gateways uint32
|
|
|
|
Nodes uint32
|
|
|
|
|
2017-11-14 18:33:50 +00:00
|
|
|
Firmwares CounterMap
|
|
|
|
Models CounterMap
|
|
|
|
Autoupdater CounterMap
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 21:27:44 +00:00
|
|
|
//NewGlobalStats returns global statistics for InfluxDB
|
2016-12-15 10:10:09 +00:00
|
|
|
func NewGlobalStats(nodes *Nodes) (result *GlobalStats) {
|
|
|
|
result = &GlobalStats{
|
2017-11-14 18:33:50 +00:00
|
|
|
Firmwares: make(CounterMap),
|
|
|
|
Models: make(CounterMap),
|
|
|
|
Autoupdater: make(CounterMap),
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
nodes.RLock()
|
2016-12-15 10:10:09 +00:00
|
|
|
for _, node := range nodes.List {
|
2017-04-10 16:54:12 +00:00
|
|
|
if node.Online {
|
2017-01-20 21:27:44 +00:00
|
|
|
result.Nodes++
|
2016-12-15 10:10:09 +00:00
|
|
|
if stats := node.Statistics; stats != nil {
|
|
|
|
result.Clients += stats.Clients.Total
|
|
|
|
result.ClientsWifi24 += stats.Clients.Wifi24
|
|
|
|
result.ClientsWifi5 += stats.Clients.Wifi5
|
|
|
|
result.ClientsWifi += stats.Clients.Wifi
|
|
|
|
}
|
2017-04-18 01:07:41 +00:00
|
|
|
if node.IsGateway() {
|
2017-01-20 21:27:44 +00:00
|
|
|
result.Gateways++
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|
|
|
|
if info := node.Nodeinfo; info != nil {
|
|
|
|
result.Models.Increment(info.Hardware.Model)
|
|
|
|
result.Firmwares.Increment(info.Software.Firmware.Release)
|
2017-11-14 18:33:50 +00:00
|
|
|
if info.Software.Autoupdater.Enabled {
|
|
|
|
result.Autoupdater.Increment(info.Software.Autoupdater.Branch)
|
|
|
|
} else {
|
|
|
|
result.Autoupdater.Increment(DISABLED_AUTOUPDATER)
|
|
|
|
}
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
nodes.RUnlock()
|
2016-12-15 10:10:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment counter in the map by one
|
|
|
|
// if the value is not empty
|
|
|
|
func (m CounterMap) Increment(key string) {
|
|
|
|
if key != "" {
|
2017-04-17 18:42:06 +00:00
|
|
|
m[key]++
|
2016-12-15 10:10:09 +00:00
|
|
|
}
|
|
|
|
}
|