-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_plot.go
More file actions
245 lines (191 loc) · 6.13 KB
/
Copy pathgraph_plot.go
File metadata and controls
245 lines (191 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"fmt"
"image/color"
"math"
"strconv"
"time"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
const (
legendPadding = 8
ops_str = "ops"
)
// Palette is a slice of Color{}s derived from Grafana's "classic" palette
var Palette = []color.Color{
color.RGBA{R: 0x73, G: 0xBF, B: 0x69, A: 255}, // rgb(115, 191, 105)
color.RGBA{R: 0xF2, G: 0xCC, B: 0x0C, A: 255}, // rgb(242, 204, 12)
color.RGBA{R: 0x8A, G: 0xB8, B: 0xFF, A: 255}, // rgb(138, 184, 255)
color.RGBA{R: 0xFF, G: 0x78, B: 0x0A, A: 255}, // rgb(255, 120, 10)
color.RGBA{R: 0xF2, G: 0x49, B: 0x5C, A: 255}, // rgb(242, 73, 92)
color.RGBA{R: 0x57, G: 0x94, B: 0xF2, A: 255}, // rgb(87, 148, 242)
color.RGBA{R: 0xB8, G: 0x77, B: 0xD9, A: 255}, // rgb(184, 119, 217)
color.RGBA{R: 0x70, G: 0x5D, B: 0xA0, A: 255}, // rgb(112, 93, 160)
color.RGBA{R: 0x37, G: 0x87, B: 0x2D, A: 255}, // rgb(55, 135, 45)
color.RGBA{R: 0xFA, G: 0xDE, B: 0x2A, A: 255}, // rgb(250, 222, 42)
}
type HourlyGrid struct {
Color color.Color
Width vg.Length
}
func (g HourlyGrid) Plot(canvas draw.Canvas, plt *plot.Plot) {
trX, _ := plt.Transforms(&canvas)
minX, maxX := plt.X.Min, plt.X.Max
t := time.Unix(int64(minX), 0).Local().Truncate(time.Hour)
if float64(t.Unix()) < minX {
t = t.Add(time.Hour)
}
for ; float64(t.Unix()) <= maxX; t = t.Add(time.Hour) {
if t.Hour()%2 == 0 {
x := trX(float64(t.Unix()))
canvas.StrokeLine2(draw.LineStyle{Color: g.Color, Width: g.Width}, x, canvas.Min.Y, x, canvas.Max.Y)
}
}
}
type HourlyTicker struct{}
func (HourlyTicker) Ticks(minVal, maxVal float64) []plot.Tick {
var ticks []plot.Tick
t := time.Unix(int64(minVal), 0).Local().Truncate(time.Hour)
if float64(t.Unix()) < minVal {
t = t.Add(time.Hour)
}
for ; float64(t.Unix()) <= maxVal; t = t.Add(time.Hour) {
if t.Hour()%2 == 0 {
// Provide a non-empty string so it's treated as a major tick
ticks = append(ticks, plot.Tick{Value: float64(t.Unix()), Label: " "})
}
}
return ticks
}
type HLine struct {
Y float64
Color color.Color
Width vg.Length
}
func (l HLine) Plot(c draw.Canvas, plt *plot.Plot) {
_, trY := plt.Transforms(&c)
y := trY(l.Y)
c.StrokeLine2(draw.LineStyle{Color: l.Color, Width: l.Width}, c.Min.X, y, c.Max.X, y)
}
type CustomYTicker struct {
Unit string
}
func (t CustomYTicker) Ticks(minVal, maxVal float64) []plot.Tick {
ticks := plot.DefaultTicks{}.Ticks(minVal, maxVal)
for i := range ticks {
if ticks[i].Label == "" {
continue // Skip minor ticks
}
val := ticks[i].Value
absVal := math.Abs(val)
var labelStr string
switch {
case absVal >= 1000000:
labelStr = fmt.Sprintf("%.1fM", val/1000000)
case absVal >= 1000:
labelStr = fmt.Sprintf("%.1fK", val/1000)
case absVal <= 1 && maxVal <= 1:
labelStr = fmt.Sprintf("%.4f", val)
default:
labelStr = fmt.Sprintf("%.0f", val)
}
if t.Unit == ops_str {
labelStr += " ops/s"
}
ticks[i].Label = labelStr
}
return ticks
}
// SeriesData represents the data for a single series
type SeriesData struct {
Points plotter.XYs
Min float64
Max float64
Sum float64
Count float64
}
// parseSeriesData parses the values from the VMResponse and returns a SeriesData struct
func parseSeriesData(values [][]interface{}) (SeriesData, error) {
seriesData := SeriesData{
Points: make(plotter.XYs, 0, len(values)),
Min: math.MaxFloat64,
Max: -math.MaxFloat64,
Sum: 0.0,
Count: 0.0,
}
for _, v := range values {
if len(v) != 2 {
return seriesData, fmt.Errorf("%w: invalid value length", ErrInvalidValueLength)
}
// Parse the timestamp and value from the VMResponse
t, ok1 := v[0].(float64)
valStr, ok2 := v[1].(string)
// If the timestamp or value is not a float64 or string, return an error
if !ok1 || !ok2 {
return seriesData, fmt.Errorf("%w: invalid value type", ErrInvalidValueType)
}
// Parse the value from the string to a float64
val, err := strconv.ParseFloat(valStr, 64)
if err != nil {
return seriesData, fmt.Errorf("%w: invalid value type", ErrInvalidValueType)
}
// Prometheus may return +Inf, -Inf, or NaN; gonum plotter cannot render these.
if math.IsInf(val, 0) || math.IsNaN(val) {
continue
}
// Update the min value for this series
if val < seriesData.Min {
seriesData.Min = val
}
// Update the max value for this series
if val > seriesData.Max {
seriesData.Max = val
}
// Update the sum, and count for this series to calculate the average
seriesData.Sum += val
seriesData.Count++
// Append the timestamp and value to the series data points
seriesData.Points = append(seriesData.Points, plotter.XY{X: t, Y: val})
}
if seriesData.Count == 0 {
return seriesData, fmt.Errorf("%w: no valid points", ErrNoValidPoints)
}
return seriesData, nil
}
// addVisualSeries creates the line and fill polygon for the series and adds them to the image.
func addVisualSeries(p *plot.Plot, seriesData SeriesData, baseColor color.Color) error {
// Create the line for the series
line, err := plotter.NewLine(seriesData.Points)
if err != nil {
return fmt.Errorf("%w: creating visual series line", err)
}
// Set the color of the line
line.Color = baseColor
// Add the line to the image
p.Add(line)
// Initialize the fill polygon for the series
polyPts := make(plotter.XYs, len(seriesData.Points)+2)
// Set the first point of the fill polygon to the first point of the series
polyPts[0] = plotter.XY{X: seriesData.Points[0].X, Y: 0}
// Set the remaining points of the fill polygon to the points of the series
for j, pt := range seriesData.Points {
polyPts[j+1] = pt
}
// Set the last point of the fill polygon to the last point of the series
polyPts[len(polyPts)-1] = plotter.XY{X: seriesData.Points[len(seriesData.Points)-1].X, Y: 0}
// Create the fill polygon for the series
poly, err := plotter.NewPolygon(polyPts)
if err != nil {
return fmt.Errorf("%w: creating visual series polygon", err)
}
// Set the color of the fill polygon
r, g, b, _ := baseColor.RGBA()
poly.Color = color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: 32} // #nosec
poly.Width = 0
// Add the fill polygon to the plot
p.Add(poly)
return nil
}