Code

server: Add support for GroupBy in graphs.
authorSebastian Harl <sh@tokkee.org>
Thu, 14 May 2015 12:29:25 +0000 (14:29 +0200)
committerSebastian Harl <sh@tokkee.org>
Thu, 14 May 2015 12:29:25 +0000 (14:29 +0200)
server/graph.go
server/query.go
static/style/main.css
templates/graphs.tmpl

index ddc8c4d17e5f6aec59531fbcf2a5e4f33b50c852..badf28f2be9e01758892d1f6b9f171b8c86c619a 100644 (file)
@@ -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
index bde450fef97dc74911e4aa516b859a3dba85afe7..22385bf07de13e576e03007b179ede5fd6e97000 100644 (file)
@@ -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)
 }
index 1bcae8641f89521ca6ba6c8693e29c0ca17918fc..00ec0897ff5ae02a4e9a88b0b1001d6312202e99 100644 (file)
@@ -178,6 +178,7 @@ input[type=text].query {
        height: 25px;
        border: 1px solid #000;
        padding: 0px 3px;
+       margin: 3px;
 }
 
 button {
index 43ec6226bb69868221c0f620dd0d19c3adb05023..6a1320128d0bc15a3557d39184bc034404e7631a 100644 (file)
@@ -4,9 +4,12 @@
                <input type="text" name="metrics-query" value="{{.Query}}"
                       class="query" placeholder="Search metrics" required />
                <button type="submit">GO</button>
+               <br />
+               <input type="text" name="group-by" value="{{.GroupBy}}"
+                      class="query" placeholder="Group by" />
        </form><br />
 {{if .Metrics}}
-       <img src="/graph/q/{{.Metrics}}" border="0" />
+       <img src="/graph/q{{urlquery .QueryOptions}}/{{.Metrics}}" border="0" />
 {{end}}
        <p>&nbsp;</p>
 </section>