Skip to content

Commit

Permalink
add global stats to influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
genofire committed Jul 27, 2016
1 parent e1b84dc commit 959521b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
30 changes: 30 additions & 0 deletions models/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion stats_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
type StatsDb struct {
points chan *client.Point
wg sync.WaitGroup
nodes *models.Nodes
client client.Client
}

Expand All @@ -36,6 +37,7 @@ func NewStatsDb() *StatsDb {
db := &StatsDb{
client: c,
points: make(chan *client.Point, 500),
nodes: nodes,
}

// start worker
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -180,7 +192,7 @@ func (c *StatsDb) worker() {
bp = nil
}
}

globalTimer.Stop()
timer.Stop()
c.wg.Done()
}

0 comments on commit 959521b

Please sign in to comment.