Code

Convert the code to the new client.Client interface.
[sysdb/webui.git] / server / graph.go
index 9406f760158029519ecf32edb07f7993c6dfc632..ed5c7fd8338cedb694944674e0cdfe1c440cdca0 100644 (file)
@@ -35,29 +35,54 @@ import (
        "net/http"
        "time"
 
-       "code.google.com/p/plotinum/plot"
-       "code.google.com/p/plotinum/plotter"
-       "code.google.com/p/plotinum/plotutil"
-       "code.google.com/p/plotinum/vg"
-       "code.google.com/p/plotinum/vg/vgsvg"
-       "github.com/sysdb/go/proto"
+       "github.com/gonum/plot"
+       "github.com/gonum/plot/plotter"
+       "github.com/gonum/plot/plotutil"
+       "github.com/gonum/plot/vg"
+       "github.com/sysdb/go/client"
        "github.com/sysdb/go/sysdb"
 )
 
+var urldate = "20060102150405"
+
 func (s *Server) graph(w http.ResponseWriter, req request) {
-       if len(req.args) != 2 {
-               s.internal(w, fmt.Errorf("Missing host/metric information"))
+       if len(req.args) < 2 || 4 < len(req.args) {
+               s.badrequest(w, fmt.Errorf("Missing host/metric information"))
+               return
+       }
+
+       end := time.Now()
+       start := end.Add(-24 * time.Hour)
+       var err error
+       if len(req.args) > 2 {
+               if start, err = time.Parse(urldate, req.args[2]); err != nil {
+                       s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
+                       return
+               }
+       }
+       if len(req.args) > 3 {
+               if end, err = time.Parse(urldate, req.args[3]); err != nil {
+                       s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
+                       return
+               }
+       }
+       if start.Equal(end) || start.After(end) {
+               s.badrequest(w, fmt.Errorf("START(%v) is greater than or equal to END(%v)", start, end))
+               return
        }
 
-       host := proto.EscapeString(req.args[0])
-       metric := proto.EscapeString(req.args[1])
-       res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
+       q, err := client.QueryString("TIMESERIES %s.%s START %s END %s", req.args[0], req.args[1], start, end)
+       if err != nil {
+               s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
+               return
+       }
+       res, err := s.c.Query(q)
        if err != nil {
                s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
                return
        }
 
-       ts, ok := res.(sysdb.Timeseries)
+       ts, ok := res.(*sysdb.Timeseries)
        if !ok {
                s.internal(w, errors.New("TIMESERIES did not return a time-series"))
                return
@@ -69,7 +94,7 @@ func (s *Server) graph(w http.ResponseWriter, req request) {
                return
        }
        p.Add(plotter.NewGrid())
-       p.X.Tick.Marker = dateTicks
+       p.X.Tick.Marker = dateTicks{}
 
        var i int
        for name, data := range ts.Data {
@@ -89,11 +114,14 @@ func (s *Server) graph(w http.ResponseWriter, req request) {
                i++
        }
 
-       c := vgsvg.New(vg.Length(500), vg.Length(200))
-       p.Draw(plot.MakeDrawArea(c))
+       pw, err := p.WriterTo(vg.Length(500), vg.Length(200), "svg")
+       if err != nil {
+               s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
+               return
+       }
 
        var buf bytes.Buffer
-       if _, err := c.WriteTo(&buf); err != nil {
+       if _, err := pw.WriteTo(&buf); err != nil {
                s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
                return
        }
@@ -102,10 +130,12 @@ func (s *Server) graph(w http.ResponseWriter, req request) {
        io.Copy(w, &buf)
 }
 
-func dateTicks(min, max float64) []plot.Tick {
+type dateTicks struct{}
+
+func (dateTicks) Ticks(min, max float64) []plot.Tick {
        // TODO: this is surely not the best we can do
        // but it'll distribute ticks evenly.
-       ticks := plot.DefaultTicks(min, max)
+       ticks := plot.DefaultTicks{}.Ticks(min, max)
        for i, t := range ticks {
                if t.Label == "" {
                        // Skip minor ticks.