-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgnuplot.go
73 lines (60 loc) · 1.59 KB
/
gnuplot.go
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
package main
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func gnuplotAction(c *cli.Context) error {
if !c.Args().Present() {
return fmt.Errorf(color.RedString("need a query to run"))
}
start := c.Timestamp("start")
end := start.Add(c.Duration("duration"))
prometheus := c.String("prometheus")
// if resolution is not set, use 1s as default
resolution := c.Duration("resolution")
if resolution == 0 || resolution == time.Duration(0) {
resolution = time.Second
}
if resolution < time.Second {
return fmt.Errorf(color.RedString("resolution must be >= 1s"))
}
results, err := Query(prometheus, *start, end, resolution, c.Args().First())
if err != nil {
return err
}
header := "set grid\n" +
"set key left top\n" +
"set xdata time\n" +
"set timefmt '%s'\n" +
"set datafile separator ','\n"
buf := bytes.NewBufferString(header)
buf.WriteString("$DATA << EOD\n")
if err := csvWriter(buf, results); err != nil {
return err
}
buf.WriteString("EOD\n")
for i, result := range results {
if i == 0 {
buf.WriteString("plot ")
} else {
buf.WriteString(", ")
}
plot := fmt.Sprintf("$DATA using 1:%d with lines lw 1 title '%s'", i+2, escapeMetricName(result.Metric))
buf.WriteString(plot)
}
buf.WriteString("\n")
fmt.Printf("%s\n", buf.String())
return nil
}
func escapeMetricName(name string) string {
// Escape: { } = _
name = strings.Replace(name, `{`, `\{`, -1)
name = strings.Replace(name, `}`, `\}`, -1)
name = strings.Replace(name, `=`, `\=`, -1)
name = strings.Replace(name, `_`, `\_`, -1)
return name
}