зеркало из https://github.com/mozilla/mig.git
[medium] agents summary queries added to the API dashboard
This commit is contained in:
Родитель
5eaa8c2833
Коммит
b5339c9dba
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче