1 //
2 // Copyright (C) 2014-2015 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 graph handles time-series data provided by SysDB. It supports
27 // querying and post-processing of the data.
28 package graph
30 import (
31 "fmt"
32 "time"
34 "github.com/gonum/plot"
35 "github.com/gonum/plot/plotter"
36 "github.com/gonum/plot/plotutil"
37 "github.com/sysdb/go/client"
38 "github.com/sysdb/go/sysdb"
39 )
41 // A Graph represents a single graph. It may reference multiple time-series.
42 type Graph struct {
43 // Time range of the graph.
44 Start, End time.Time
46 // Metrics: {<hostname>, <identifier>}
47 Metrics [][2]string
48 }
50 type pl struct {
51 *plot.Plot
53 ts int // Index of the current time-series.
54 }
56 func (p *pl) addTimeseries(c *client.Client, metric [2]string, start, end time.Time) error {
57 q, err := client.QueryString("TIMESERIES %s.%s START %s END %s", metric[0], metric[1], start, end)
58 if err != nil {
59 return fmt.Errorf("Failed to retrieve graph data: %v", err)
60 }
61 res, err := c.Query(q)
62 if err != nil {
63 return fmt.Errorf("Failed to retrieve graph data: %v", err)
64 }
66 ts, ok := res.(*sysdb.Timeseries)
67 if !ok {
68 return fmt.Errorf("TIMESERIES did not return a time-series but %T", res)
69 }
71 for name, data := range ts.Data {
72 pts := make(plotter.XYs, len(data))
73 for i, p := range data {
74 pts[i].X = float64(time.Time(p.Timestamp).UnixNano())
75 pts[i].Y = p.Value
76 }
78 l, err := plotter.NewLine(pts)
79 if err != nil {
80 return fmt.Errorf("Failed to create line plotter: %v", err)
81 }
82 l.LineStyle.Color = plotutil.DarkColors[p.ts%len(plotutil.DarkColors)]
84 p.Add(l)
85 p.Legend.Add(name, l)
86 p.ts++
87 }
88 return nil
89 }
91 // Plot fetches a graph's time-series data using the specified client and
92 // plots it.
93 func (g *Graph) Plot(c *client.Client) (*plot.Plot, error) {
94 var err error
96 p := &pl{}
97 p.Plot, err = plot.New()
98 if err != nil {
99 return nil, fmt.Errorf("Failed to create plot: %v", err)
100 }
101 p.Add(plotter.NewGrid())
102 p.X.Tick.Marker = dateTicks{}
104 for _, m := range g.Metrics {
105 if err := p.addTimeseries(c, m, g.Start, g.End); err != nil {
106 return nil, err
107 }
108 }
109 return p.Plot, nil
110 }
112 type dateTicks struct{}
114 func (dateTicks) Ticks(min, max float64) []plot.Tick {
115 // TODO: this is surely not the best we can do
116 // but it'll distribute ticks evenly.
117 ticks := plot.DefaultTicks{}.Ticks(min, max)
118 for i, t := range ticks {
119 if t.Label == "" {
120 // Skip minor ticks.
121 continue
122 }
123 ticks[i].Label = time.Unix(0, int64(t.Value)).Format(time.RFC822)
124 }
125 return ticks
126 }
128 // vim: set tw=78 sw=4 sw=4 noexpandtab :