From b5339c9dbaa753984a944740870ef3985f2bcdd6 Mon Sep 17 00:00:00 2001 From: Julien Vehent Date: Tue, 17 Jun 2014 23:38:43 -0400 Subject: [PATCH] [medium] agents summary queries added to the API dashboard --- src/mig/api/api.go | 13 ++++++++++++ src/mig/api/convert.go | 24 +++++++++++++++++++++ src/mig/database/queries.go | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) diff --git a/src/mig/api/api.go b/src/mig/api/api.go index a759cb87..4bee8cb9 100644 --- a/src/mig/api/api.go +++ b/src/mig/api/api.go @@ -555,6 +555,19 @@ func getDashboard(respWriter http.ResponseWriter, request *http.Request) { } ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving getDashboard()"}.Debug() }() + + // get summary of agents active in the last 5 minutes + sum, err := ctx.DB.SumAgentsByVersion(time.Now().Add(-5 * time.Minute)) + if err != nil { + panic(err) + } + count, err := ctx.DB.CountNewAgents(time.Now().Add(-24 * time.Hour)) + if err != nil { + panic(err) + } + sumItem, err := agentsSummaryToItem(sum, count, ctx) + resource.AddItem(sumItem) + // add the last 10 actions actions, err := ctx.DB.Last10Actions() if err != nil { diff --git a/src/mig/api/convert.go b/src/mig/api/convert.go index 014007f5..76d6ab05 100644 --- a/src/mig/api/convert.go +++ b/src/mig/api/convert.go @@ -38,6 +38,7 @@ package main import ( "fmt" "mig" + migdb "mig/database" "github.com/jvehent/cljs" ) @@ -81,3 +82,26 @@ func commandToItem(cmd mig.Command) (item cljs.Item, err error) { } return } + +// agentsSumToItem receives an AgentsSum and returns an Item +// in the Collection+JSON format +func agentsSummaryToItem(sum []migdb.AgentsSum, count float64, ctx Context) (item cljs.Item, err error) { + item.Href = fmt.Sprintf("%s/dashboard", ctx.Server.BaseURL) + var total float64 = 0 + for _, asum := range sum { + total += asum.Count + } + item.Data = []cljs.Data{ + {Name: "active agents", Value: total}, + {Name: "agents versions count", Value: sum}, + {Name: "agents started in the last 24 hours", Value: count}, + } + links := make([]cljs.Link, 0) + link := cljs.Link{ + Rel: "agents dashboard", + Href: fmt.Sprintf("%s/agents/dashboard", ctx.Server.BaseURL), + } + links = append(links, link) + item.Links = links + return +} diff --git a/src/mig/database/queries.go b/src/mig/database/queries.go index 87aa4b43..288149a1 100644 --- a/src/mig/database/queries.go +++ b/src/mig/database/queries.go @@ -768,3 +768,45 @@ func (db *DB) MarkAgentDestroyed(agent mig.Agent) (err error) { } return } + +type AgentsSum struct { + Version string `json:"version"` + Count float64 `json:"count"` +} + +// SumAgentsByVersion retrieves a sum of agents grouped by version +func (db *DB) SumAgentsByVersion(pointInTime time.Time) (sum []AgentsSum, err error) { + rows, err := db.c.Query(`SELECT COUNT(*), version FROM agents + WHERE agents.heartbeattime >= $1 AND agents.heartbeattime <= NOW() + GROUP BY version`, pointInTime) + if err != nil { + err = fmt.Errorf("Error while counting agents: '%v'", err) + return + } + for rows.Next() { + var asum AgentsSum + err = rows.Scan(&asum.Count, &asum.Version) + if err != nil { + rows.Close() + err = fmt.Errorf("Failed to retrieve summary data: '%v'", err) + return + } + sum = append(sum, asum) + } + rows.Close() + return +} + +// NewAgents retrieves a count of agents that started after `pointInTime` +func (db *DB) CountNewAgents(pointInTime time.Time) (sum float64, err error) { + err = db.c.QueryRow(`SELECT COUNT(name) FROM agents + WHERE starttime >= $1 AND starttime <= NOW()`, pointInTime).Scan(&sum) + if err != nil { + err = fmt.Errorf("Error while counting agents: '%v'", err) + return + } + if err == sql.ErrNoRows { + return + } + return +}