Code

Improved error reporting in graph(): correctly report bad requests.
[sysdb/webui.git] / server / graph.go
index 054fe8eac4a18827b82bfe6c8f84519493d1f705..941e51de6b8a994b65111c26181178e7cc2258c4 100644 (file)
@@ -33,7 +33,6 @@ import (
        "fmt"
        "io"
        "net/http"
-       "strings"
        "time"
 
        "code.google.com/p/plotinum/plot"
@@ -46,12 +45,12 @@ import (
 )
 
 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 {
+               s.badrequest(w, fmt.Errorf("Missing host/metric information"))
        }
 
        host := proto.EscapeString(req.args[0])
-       metric := proto.EscapeString(strings.Join(req.args[1:], "/"))
+       metric := proto.EscapeString(req.args[1])
        res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
        if err != nil {
                s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
@@ -70,6 +69,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 +102,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 :