Code

Make a graph's start and end times configurable.
[sysdb/webui.git] / server / graph.go
index 054fe8eac4a18827b82bfe6c8f84519493d1f705..47ba193c1d052ceff42c6e23930f61a83e7acb40 100644 (file)
@@ -33,7 +33,6 @@ import (
        "fmt"
        "io"
        "net/http"
-       "strings"
        "time"
 
        "code.google.com/p/plotinum/plot"
@@ -41,18 +40,38 @@ import (
        "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/sysdb/go/sysdb"
 )
 
+var timeLayout = "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
        }
 
-       host := proto.EscapeString(req.args[0])
-       metric := proto.EscapeString(strings.Join(req.args[1:], "/"))
-       res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
+       end := time.Now()
+       start := end.Add(-24 * time.Hour)
+       var err error
+       if len(req.args) > 2 {
+               if start, err = time.Parse(timeLayout, 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(timeLayout, 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
+       }
+
+       res, err := s.query("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
@@ -70,6 +89,7 @@ func (s *Server) graph(w http.ResponseWriter, req request) {
                return
        }
        p.Add(plotter.NewGrid())
+       p.X.Tick.Marker = dateTicks
 
        var i int
        for name, data := range ts.Data {
@@ -102,4 +122,18 @@ func (s *Server) graph(w http.ResponseWriter, req request) {
        io.Copy(w, &buf)
 }
 
+func dateTicks(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)
+       for i, t := range ticks {
+               if t.Label == "" {
+                       // Skip minor ticks.
+                       continue
+               }
+               ticks[i].Label = time.Unix(0, int64(t.Value)).Format(time.RFC822)
+       }
+       return ticks
+}
+
 // vim: set tw=78 sw=4 sw=4 noexpandtab :