diff --git a/models/nodes.go b/models/nodes.go index 8a01ff7..c98c875 100644 --- a/models/nodes.go +++ b/models/nodes.go @@ -171,6 +171,36 @@ func (nodes *Nodes) worker() { } } +func (nodes *Nodes) GetStats() map[string]interface{} { + var nodesCount uint32 + var clientsCount uint32 + var clientsWifiCount uint32 + var clientsWifi24Count uint32 + var clientsWifi5Count uint32 + + nodes.Lock() + for _, node := range nodes.List { + if node.Flags.Online { + nodesCount += 1 + if stats := node.Statistics; stats != nil { + clientsCount += stats.Clients.Total + clientsWifi24Count += stats.Clients.Wifi24 + clientsWifi5Count += stats.Clients.Wifi5 + clientsWifiCount += stats.Clients.Wifi + } + } + } + nodes.Unlock() + + return map[string]interface{}{ + "nodes": nodesCount, + "clients.total": clientsCount, + "clients.wifi": clientsWifiCount, + "clients.wifi24": clientsWifi24Count, + "clients.wifi5": clientsWifi5Count, + } +} + func (nodes *Nodes) load() { path := nodes.config.Nodes.NodesPath log.Println("loading", path) diff --git a/stats_db.go b/stats_db.go index b9dcab5..662c35e 100644 --- a/stats_db.go +++ b/stats_db.go @@ -18,6 +18,7 @@ const ( type StatsDb struct { points chan *client.Point wg sync.WaitGroup + nodes *models.Nodes client client.Client } @@ -36,6 +37,7 @@ func NewStatsDb() *StatsDb { db := &StatsDb{ client: c, points: make(chan *client.Point, 500), + nodes: nodes, } // start worker @@ -143,11 +145,21 @@ func (c *StatsDb) worker() { var err error var writeNow, closed bool timer := time.NewTimer(batchDuration) + globalDuration := time.Second * time.Duration(config.Nodes.SaveInterval) + globalTimer := time.NewTimer(globalDuration) for !closed { // wait for new points select { + case <-globalTimer.C: + point, err := client.NewPoint("global", nil, nodes.GetStats(), time.Now()) + if err != nil { + panic(err) + } + c.points <- point + globalTimer.Reset(globalDuration) + log.Print("saving global point") case point, ok := <-c.points: if ok { if bp == nil { @@ -180,7 +192,7 @@ func (c *StatsDb) worker() { bp = nil } } - + globalTimer.Stop() timer.Stop() c.wg.Done() }