2017-04-10 18:54:12 +02:00
|
|
|
package runtime
|
2016-12-15 11:10:09 +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 {
|
|
|
|
Clients uint32
|
|
|
|
ClientsWifi uint32
|
|
|
|
ClientsWifi24 uint32
|
|
|
|
ClientsWifi5 uint32
|
|
|
|
Gateways uint32
|
|
|
|
Nodes uint32
|
|
|
|
|
|
|
|
Firmwares CounterMap
|
|
|
|
Models CounterMap
|
|
|
|
}
|
|
|
|
|
2017-01-20 22:27:44 +01:00
|
|
|
//NewGlobalStats returns global statistics for InfluxDB
|
2017-02-19 00:18:54 +01:00
|
|
|
func NewGlobalStats(nodes *Nodes, site string) (result *GlobalStats) {
|
2016-12-15 11:10:09 +01:00
|
|
|
result = &GlobalStats{
|
|
|
|
Firmwares: make(CounterMap),
|
|
|
|
Models: make(CounterMap),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes.List {
|
2017-04-10 18:54:12 +02:00
|
|
|
if node.Online {
|
2016-12-15 11:10:09 +01:00
|
|
|
if info := node.Nodeinfo; info != nil {
|
2017-02-19 00:18:54 +01:00
|
|
|
if len(site) == 0 || info.System.SiteCode == site {
|
|
|
|
result.Nodes++
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if node.Gateway {
|
|
|
|
result.Gateways++
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Models.Increment(info.Hardware.Model)
|
|
|
|
result.Firmwares.Increment(info.Software.Firmware.Release)
|
|
|
|
}
|
2016-12-15 11:10:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment counter in the map by one
|
|
|
|
// if the value is not empty
|
|
|
|
func (m CounterMap) Increment(key string) {
|
|
|
|
if key != "" {
|
|
|
|
val := m[key]
|
|
|
|
m[key] = val + 1
|
|
|
|
}
|
|
|
|
}
|