From 72f42e67ee500535d819109eaee7f9b8e3046f17 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 24 Nov 2014 09:57:23 +0100 Subject: [PATCH] Make a metric's graph's time-range configurable. --- server/graph.go | 6 +++--- server/query.go | 41 ++++++++++++++++++++++++++++++++++++++++- static/style/main.css | 15 +++++++++++++++ templates/metric.tmpl | 15 +++++++++++---- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/server/graph.go b/server/graph.go index 47ba193..5134ed6 100644 --- a/server/graph.go +++ b/server/graph.go @@ -43,7 +43,7 @@ import ( "github.com/sysdb/go/sysdb" ) -var timeLayout = "20060102150405" +var urldate = "20060102150405" func (s *Server) graph(w http.ResponseWriter, req request) { if len(req.args) < 2 || 4 < len(req.args) { @@ -55,13 +55,13 @@ func (s *Server) graph(w http.ResponseWriter, req request) { 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 { + 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(timeLayout, req.args[3]); err != nil { + if end, err = time.Parse(urldate, req.args[3]); err != nil { s.badrequest(w, fmt.Errorf("Invalid start time: %v", err)) return } diff --git a/server/query.go b/server/query.go index 75707ff..a95dba5 100644 --- a/server/query.go +++ b/server/query.go @@ -85,6 +85,9 @@ func fetch(req request, s *Server) (*page, error) { return nil, fmt.Errorf("%s not found", strings.Title(req.cmd)) } res, err = s.query("FETCH %s %s.%s", identifier(req.cmd), req.args[0], req.args[1]) + if req.cmd == "metric" { + return metric(req, res, s) + } default: panic("Unknown request: fetch(" + req.cmd + ")") } @@ -94,6 +97,42 @@ func fetch(req request, s *Server) (*page, error) { return tmpl(s.results[req.cmd], res) } +var datetime = "2006-01-02 15:04:05" + +func metric(req request, res interface{}, s *Server) (*page, error) { + start := time.Now().Add(-24 * time.Hour) + end := time.Now() + if req.r.Method == "POST" { + var err error + // Parse the values first to verify their format. + if s := req.r.FormValue("start_date"); s != "" { + if start, err = time.Parse(datetime, s); err != nil { + return nil, fmt.Errorf("Invalid start time %q", s) + } + } + if e := req.r.FormValue("end_date"); e != "" { + if end, err = time.Parse(datetime, e); err != nil { + return nil, fmt.Errorf("Invalid end time %q", e) + } + } + } + + p := struct { + StartTime string + EndTime string + URLStart string + URLEnd string + Data interface{} + }{ + start.Format(datetime), + end.Format(datetime), + start.Format(urldate), + end.Format(urldate), + res, + } + return tmpl(s.results["metric"], &p) +} + type identifier string func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) { @@ -107,7 +146,7 @@ func (s *Server) query(cmd string, args ...interface{}) (interface{}, error) { case string: args[i] = proto.EscapeString(v) case time.Time: - args[i] = v.Format("2006-01-02 15:04:05") + args[i] = v.Format(datetime) default: panic(fmt.Sprintf("query: invalid type %T", arg)) } diff --git a/static/style/main.css b/static/style/main.css index 4ad9665..4127602 100644 --- a/static/style/main.css +++ b/static/style/main.css @@ -165,3 +165,18 @@ table.results th, table.results td { table.results td.value { word-break: break-all; } + +input[type=text].datetime { + width: 12em; + height: 25px; + border: 1px solid #000; + padding: 0px 3px; +} + +button { + background-color: #000; + color: #fff; + border: 1px solid #000; + height: 25px; + padding: 0px; +} diff --git a/templates/metric.tmpl b/templates/metric.tmpl index d855356..5dc51c1 100644 --- a/templates/metric.tmpl +++ b/templates/metric.tmpl @@ -1,10 +1,17 @@ -
{{$m := index .Metrics 0}} -

Metric {{$.Name}} — {{$m.Name}}

+
{{$m := index .Data.Metrics 0}} +

Metric {{.Data.Name}} — {{$m.Name}}

{{if $m.Timeseries}} - +
+ Time range: + + — + + +

+ {{end}} - + -- 2.30.2
Host{{$.Name}}
Host{{.Data.Name}}
Last update{{$m.LastUpdate}}
Update interval{{$m.UpdateInterval}}
Backends{{$m.Backends}}