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/proto"
44 "github.com/sysdb/go/sysdb"
45 )
47 func (s *Server) graph(w http.ResponseWriter, req request) {
48 if len(req.args) != 2 {
49 s.internal(w, fmt.Errorf("Missing host/metric information"))
50 }
52 host := proto.EscapeString(req.args[0])
53 metric := proto.EscapeString(req.args[1])
54 res, err := s.query(fmt.Sprintf("TIMESERIES %s.%s", host, metric))
55 if err != nil {
56 s.internal(w, fmt.Errorf("Failed to retrieve graph data: %v", err))
57 return
58 }
60 ts, ok := res.(sysdb.Timeseries)
61 if !ok {
62 s.internal(w, errors.New("TIMESERIES did not return a time-series"))
63 return
64 }
66 p, err := plot.New()
67 if err != nil {
68 s.internal(w, fmt.Errorf("Failed to create plot: %v", err))
69 return
70 }
71 p.Add(plotter.NewGrid())
72 p.X.Tick.Marker = dateTicks
74 var i int
75 for name, data := range ts.Data {
76 pts := make(plotter.XYs, len(data))
77 for i, p := range data {
78 pts[i].X = float64(time.Time(p.Timestamp).UnixNano())
79 pts[i].Y = p.Value
80 }
81 l, err := plotter.NewLine(pts)
82 if err != nil {
83 s.internal(w, fmt.Errorf("Failed to create line plotter: %v", err))
84 return
85 }
86 l.LineStyle.Color = plotutil.DarkColors[i%len(plotutil.DarkColors)]
87 p.Add(l)
88 p.Legend.Add(name, l)
89 i++
90 }
92 c := vgsvg.New(vg.Length(500), vg.Length(200))
93 p.Draw(plot.MakeDrawArea(c))
95 var buf bytes.Buffer
96 if _, err := c.WriteTo(&buf); err != nil {
97 s.internal(w, fmt.Errorf("Failed to write plot: %v", err))
98 return
99 }
100 w.Header().Set("Content-Type", "image/svg+xml")
101 w.WriteHeader(http.StatusOK)
102 io.Copy(w, &buf)
103 }
105 func dateTicks(min, max float64) []plot.Tick {
106 // TODO: this is surely not the best we can do
107 // but it'll distribute ticks evenly.
108 ticks := plot.DefaultTicks(min, max)
109 for i, t := range ticks {
110 if t.Label == "" {
111 // Skip minor ticks.
112 continue
113 }
114 ticks[i].Label = time.Unix(0, int64(t.Value)).Format(time.RFC822)
115 }
116 return ticks
117 }
119 // vim: set tw=78 sw=4 sw=4 noexpandtab :