From: Sebastian Harl Date: Thu, 14 May 2015 12:29:25 +0000 (+0200) Subject: server: Add support for GroupBy in graphs. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ed155b476ed90127d81de598b6d4a685ed76f2b3;p=sysdb%2Fwebui.git server: Add support for GroupBy in graphs. --- diff --git a/server/graph.go b/server/graph.go index ddc8c4d..badf28f 100644 --- a/server/graph.go +++ b/server/graph.go @@ -32,6 +32,7 @@ import ( "fmt" "io" "net/http" + "strings" "time" "github.com/gonum/plot/vg" @@ -67,11 +68,21 @@ func (s *Server) graph(w http.ResponseWriter, req request) { Start: start, End: end, } - if req.args[0] == "q" { + if req.args[0] == "q" || len(req.args[0]) > 1 && req.args[0][:2] == "q/" { if g.Metrics, err = s.queryMetrics(req.args[1]); err != nil { s.badrequest(w, fmt.Errorf("Failed to query metrics: %v", err)) return } + + if req.args[0] != "q" { + for _, arg := range strings.Split(req.args[0][2:], "/") { + if arg := strings.SplitN(arg, "=", 2); len(arg) == 2 { + if arg[0] == "g" { + g.GroupBy = strings.Split(arg[1], ",") + } + } + } + } } else { g.Metrics = []graph.Metric{{Hostname: req.args[0], Identifier: req.args[1]}} } @@ -131,7 +142,15 @@ func (s *Server) queryMetrics(q string) ([]graph.Metric, error) { var metrics []graph.Metric for _, h := range hosts { for _, m := range h.Metrics { - metrics = append(metrics, graph.Metric{Hostname: h.Name, Identifier: m.Name}) + metric := graph.Metric{ + Hostname: h.Name, + Identifier: m.Name, + Attributes: make(map[string]string), + } + for _, attr := range m.Attributes { + metric.Attributes[attr.Name] = attr.Value + } + metrics = append(metrics, metric) } } return metrics, nil diff --git a/server/query.go b/server/query.go index bde450f..22385bf 100644 --- a/server/query.go +++ b/server/query.go @@ -132,12 +132,18 @@ func fetch(req request, s *Server) (*page, error) { func graphs(req request, s *Server) (*page, error) { p := struct { Query, Metrics string + QueryOptions string + GroupBy string }{ - Query: req.r.PostForm.Get("metrics-query"), + Query: req.r.PostForm.Get("metrics-query"), + GroupBy: req.r.PostForm.Get("group-by"), } if req.r.Method == "POST" { p.Metrics = p.Query + if p.GroupBy != "" { + p.QueryOptions += "/g=" + strings.Join(strings.Fields(p.GroupBy), ",") + } } return tmpl(s.results["graphs"], &p) } diff --git a/static/style/main.css b/static/style/main.css index 1bcae86..00ec089 100644 --- a/static/style/main.css +++ b/static/style/main.css @@ -178,6 +178,7 @@ input[type=text].query { height: 25px; border: 1px solid #000; padding: 0px 3px; + margin: 3px; } button { diff --git a/templates/graphs.tmpl b/templates/graphs.tmpl index 43ec622..6a13201 100644 --- a/templates/graphs.tmpl +++ b/templates/graphs.tmpl @@ -4,9 +4,12 @@ +
+
{{if .Metrics}} - + {{end}}