Code

Make a metric's graph's time-range configurable.
[sysdb/webui.git] / server / graph.go
index 11b55b84fef483d16ff545bf50894444fdd58576..5134ed6659f2ed5bd8539efe9d1580def3c4aec0 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 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(strings.Join(req.args[1:], "/"))
-       res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
+       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