1 //
2 // Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 package server
28 // Helper functions for handling and plotting graphs.
30 import (
31 "bytes"
32 "errors"
33 "fmt"
34 "io"
35 "net/http"
36 "time"
38 "github.com/gonum/plot"
39 "github.com/gonum/plot/plotter"
40 "github.com/gonum/plot/plotutil"
41 "github.com/gonum/plot/vg"
42 "github.com/sysdb/go/sysdb"
43 )
45 var urldate = "20060102150405"
47 func (s *Server) graph(w http.ResponseWriter, req request) {
48 if len(req.args) < 2 || 4 < len(req.args) {
49 s.badrequest(w, fmt.Errorf("Missing host/metric information"))
50 return
51 }
53 end := time.Now()
54 start := end.Add(-24 * time.Hour)
55 var err error
56 if len(req.args) > 2 {
57 if start, err = time.Parse(urldate, req.args[2]); err != nil {
58 s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
59 return
60 }
61 }
62 if len(req.args) > 3 {
63 if end, err = time.Parse(urldate, req.args[3]); err != nil {
64 s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
65 return
66 }
67 }
68 if start.Equal(end) || start.After(end) {
69 s.badrequest(w, fmt.Errorf("START(%v) is greater than or equal to END(%v)", start, end))
70 return
71 }
73 res, err := s.query("TIMESERIES %s.%s START %s END %s", req.args[0], req.args[1], start, end)
74 if err != nil {
75 s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
76 return
77 }
79 ts, ok := res.(sysdb.Timeseries)
80 if !ok {
81 s.internal(w, errors.New("TIMESERIES did not return a time-series"))
82 return
83 }
85 p, err := plot.New()
86 if err != nil {
87 s.internal(w, fmt.Errorf("Failed to create plot: %v", err))
88 return
89 }
90 p.Add(plotter.NewGrid())
91 p.X.Tick.Marker = dateTicks{}
93 var i int
94 for name, data := range ts.Data {
95 pts := make(plotter.XYs, len(data))
96 for i, p := range data {
97 pts[i].X = float64(time.Time(p.Timestamp).UnixNano())
98 pts[i].Y = p.Value
99 }
100 l, err := plotter.NewLine(pts)
101 if err != nil {
102 s.internal(w, fmt.Errorf("Failed to create line plotter: %v", err))
103 return
104 }
105 l.LineStyle.Color = plotutil.DarkColors[i%len(plotutil.DarkColors)]
106 p.Add(l)
107 p.Legend.Add(name, l)
108 i++
109 }
111 pw, err := p.WriterTo(vg.Length(500), vg.Length(200), "svg")
112 if err != nil {
113 s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
114 return
115 }
117 var buf bytes.Buffer
118 if _, err := pw.WriteTo(&buf); err != nil {
119 s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
120 return
121 }
122 w.Header().Set("Content-Type", "image/svg+xml")
123 w.WriteHeader(http.StatusOK)
124 io.Copy(w, &buf)
125 }
127 type dateTicks struct{}
129 func (dateTicks) Ticks(min, max float64) []plot.Tick {
130 // TODO: this is surely not the best we can do
131 // but it'll distribute ticks evenly.
132 ticks := plot.DefaultTicks{}.Ticks(min, max)
133 for i, t := range ticks {
134 if t.Label == "" {
135 // Skip minor ticks.
136 continue
137 }
138 ticks[i].Label = time.Unix(0, int64(t.Value)).Format(time.RFC822)
139 }
140 return ticks
141 }
143 // vim: set tw=78 sw=4 sw=4 noexpandtab :