Commit
+37 -3 +/-4 browse
1 | diff --git a/README.md b/README.md |
2 | index ee33b56..e79a46f 100644 |
3 | --- a/README.md |
4 | +++ b/README.md |
5 | @@ -30,8 +30,8 @@ set the query parameters as appropriate: |
6 | - **query**: required. Prometheus query to execute. |
7 | - **title**: chart title |
8 | - **stacked**: set to create an area chart instead of a line chart |
9 | - - **since**: [time.ParseDuration][1] to set distance in the past to start |
10 | - charting from |
11 | + - **since**, **until**: [time.ParseDuration][1] to set distance in the past to |
12 | + start charting from or until |
13 | - **width**, **height**: adjust chart dimensions in inches |
14 | - **step**: number of seconds between data points |
15 | - **min**, **max**: Y axis limits |
16 | diff --git a/go.mod b/go.mod |
17 | index 630e312..68f1007 100644 |
18 | --- a/go.mod |
19 | +++ b/go.mod |
20 | @@ -3,6 +3,7 @@ module git.sr.ht/~sircmpwn/chartsrv |
21 | go 1.15 |
22 | |
23 | require ( |
24 | + github.com/dustin/go-humanize v1.0.0 // indirect |
25 | github.com/go-chi/chi v4.1.2+incompatible |
26 | gonum.org/v1/plot v0.8.0 |
27 | ) |
28 | diff --git a/go.sum b/go.sum |
29 | index 817203a..be5d568 100644 |
30 | --- a/go.sum |
31 | +++ b/go.sum |
32 | @@ -5,6 +5,8 @@ github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af h1:wVe6/Ea46ZMeNkQjj |
33 | github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= |
34 | github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= |
35 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
36 | + github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= |
37 | + github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= |
38 | github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= |
39 | github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= |
40 | github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= |
41 | diff --git a/main.go b/main.go |
42 | index 887ae50..2bce2d7 100644 |
43 | --- a/main.go |
44 | +++ b/main.go |
45 | @@ -13,6 +13,7 @@ import ( |
46 | "strings" |
47 | "time" |
48 | |
49 | + "github.com/dustin/go-humanize" |
50 | "github.com/go-chi/chi" |
51 | "github.com/go-chi/chi/middleware" |
52 | "gonum.org/v1/plot" |
53 | @@ -154,6 +155,10 @@ func main() { |
54 | d, _ := time.ParseDuration(s[0]) |
55 | start = time.Now().Add(-d) |
56 | } |
57 | + if u, ok := args["until"]; ok { |
58 | + d, _ := time.ParseDuration(u[0]) |
59 | + end = time.Now().Add(-d) |
60 | + } |
61 | |
62 | width := 12*vg.Inch |
63 | height := 6*vg.Inch |
64 | @@ -166,6 +171,12 @@ func main() { |
65 | height = vg.Length(h)*vg.Inch |
66 | } |
67 | |
68 | + // Undocumented option |
69 | + var legend string |
70 | + if l, ok := args["legend"]; ok { |
71 | + legend = l[0] |
72 | + } |
73 | + |
74 | // Set step so that there's approximately 25 data points per inch |
75 | step := int(end.Sub(start).Seconds() / (25 * float64(width / vg.Inch))) |
76 | if s, ok := args["step"]; ok { |
77 | @@ -194,6 +205,8 @@ func main() { |
78 | m, _ := strconv.ParseFloat(ms[0], 64) |
79 | p.Y.Max = m |
80 | } |
81 | + |
82 | + p.Y.Tick.Marker = humanTicks{} |
83 | if ms, ok := args["min"]; ok { |
84 | m, _ := strconv.ParseFloat(ms[0], 64) |
85 | p.Y.Min = m |
86 | @@ -238,7 +251,11 @@ func main() { |
87 | nextColor = 0 |
88 | } |
89 | plotters[i] = l |
90 | - p.Legend.Add(res.Metric, l) |
91 | + if legend != "" { |
92 | + p.Legend.Add(legend, l) |
93 | + } else { |
94 | + p.Legend.Add(res.Metric, l) |
95 | + } |
96 | } |
97 | for i := len(plotters) - 1; i >= 0; i-- { |
98 | p.Add(plotters[i]) |
99 | @@ -287,3 +304,17 @@ func (dt dateTicks) Ticks(min, max float64) []plot.Tick { |
100 | } |
101 | return tks |
102 | } |
103 | + |
104 | + type humanTicks struct{} |
105 | + |
106 | + func (ht humanTicks) Ticks(min, max float64) []plot.Tick { |
107 | + tks := plot.DefaultTicks{}.Ticks(min, max) |
108 | + for i, t := range tks { |
109 | + if t.Label == "" { // Skip minor ticks, they are fine. |
110 | + continue |
111 | + } |
112 | + d, _ := strconv.ParseFloat(t.Label, 64) |
113 | + tks[i].Label = humanize.SI(d, "") |
114 | + } |
115 | + return tks |
116 | + } |