Code

Make a graph's start and end times configurable.
authorSebastian Harl <sh@tokkee.org>
Sun, 23 Nov 2014 16:29:35 +0000 (17:29 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 23 Nov 2014 16:29:35 +0000 (17:29 +0100)
server/graph.go
server/query.go

index 941e51de6b8a994b65111c26181178e7cc2258c4..47ba193c1d052ceff42c6e23930f61a83e7acb40 100644 (file)
@@ -40,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 {
+       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(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
        }
 
-       host := proto.EscapeString(req.args[0])
-       metric := proto.EscapeString(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
index edeae731baa5f9519618e25e2e9321dae2c7591b..75707ffbdd6540edeb1a4e00330ba7e53f35bd3b 100644 (file)
@@ -32,6 +32,7 @@ import (
        "fmt"
        "log"
        "strings"
+       "time"
 
        "github.com/sysdb/go/proto"
        "github.com/sysdb/go/sysdb"
@@ -105,6 +106,8 @@ func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) {
                        // Nothing to do.
                case string:
                        args[i] = proto.EscapeString(v)
+               case time.Time:
+                       args[i] = v.Format("2006-01-02 15:04:05")
                default:
                        panic(fmt.Sprintf("query: invalid type %T", arg))
                }