47ba193c1d052ceff42c6e23930f61a83e7acb40
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 "code.google.com/p/plotinum/plot"
39 "code.google.com/p/plotinum/plotter"
40 "code.google.com/p/plotinum/plotutil"
41 "code.google.com/p/plotinum/vg"
42 "code.google.com/p/plotinum/vg/vgsvg"
43 "github.com/sysdb/go/sysdb"
44 )
46 var timeLayout = "20060102150405"
48 func (s *Server) graph(w http.ResponseWriter, req request) {
49 if len(req.args) < 2 || 4 < len(req.args) {
50 s.badrequest(w, fmt.Errorf("Missing host/metric information"))
51 return
52 }
54 end := time.Now()
55 start := end.Add(-24 * time.Hour)
56 var err error
57 if len(req.args) > 2 {
58 if start, err = time.Parse(timeLayout, req.args[2]); err != nil {
59 s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
60 return
61 }
62 }
63 if len(req.args) > 3 {
64 if end, err = time.Parse(timeLayout, req.args[3]); err != nil {
65 s.badrequest(w, fmt.Errorf("Invalid start time: %v", err))
66 return
67 }
68 }
69 if start.Equal(end) || start.After(end) {
70 s.badrequest(w, fmt.Errorf("START(%v) is greater than or equal to END(%v)", start, end))
71 return
72 }
74 res, err := s.query("TIMESERIES %s.%s START %s END %s", req.args[0], req.args[1], start, end)
75 if err != nil {
76 s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
77 return
78 }
80 ts, ok := res.(sysdb.Timeseries)
81 if !ok {
82 s.internal(w, errors.New("TIMESERIES did not return a time-series"))
83 return
84 }
86 p, err := plot.New()
87 if err != nil {
88 s.internal(w, fmt.Errorf("Failed to create plot: %v", err))
89 return
90 }
91 p.Add(plotter.NewGrid())
92 p.X.Tick.Marker = dateTicks
94 var i int
95 for name, data := range ts.Data {
96 pts := make(plotter.XYs, len(data))
97 for i, p := range data {
98 pts[i].X = float64(time.Time(p.Timestamp).UnixNano())
99 pts[i].Y = p.Value
100 }
101 l, err := plotter.NewLine(pts)
102 if err != nil {
103 s.internal(w, fmt.Errorf("Failed to create line plotter: %v", err))
104 return
105 }
106 l.LineStyle.Color = plotutil.DarkColors[i%len(plotutil.DarkColors)]
107 p.Add(l)
108 p.Legend.Add(name, l)
109 i++
110 }
112 c := vgsvg.New(vg.Length(500), vg.Length(200))
113 p.Draw(plot.MakeDrawArea(c))
115 var buf bytes.Buffer
116 if _, err := c.WriteTo(&buf); err != nil {
117 s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
118 return
119 }
120 w.Header().Set("Content-Type", "image/svg+xml")
121 w.WriteHeader(http.StatusOK)
122 io.Copy(w, &buf)
123 }
125 func dateTicks(min, max float64) []plot.Tick {
126 // TODO: this is surely not the best we can do
127 // but it'll distribute ticks evenly.
128 ticks := plot.DefaultTicks(min, max)
129 for i, t := range ticks {
130 if t.Label == "" {
131 // Skip minor ticks.
132 continue
133 }
134 ticks[i].Label = time.Unix(0, int64(t.Value)).Format(time.RFC822)
135 }
136 return ticks
137 }
139 // vim: set tw=78 sw=4 sw=4 noexpandtab :