-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (142 loc) · 2.94 KB
/
main.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
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
package main
import (
"bufio"
"fmt"
"github.com/go-graphite/carbonapi/expr"
"github.com/go-graphite/carbonapi/expr/functions"
"github.com/go-graphite/carbonapi/expr/rewrite"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"math"
"os"
"strconv"
ui "github.com/gizak/termui"
)
type graphRequest struct {
metric string
request string
values []float64
stepTime int64
from int64
until int64
}
func readLoop(do func(string)) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
do(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Println(err)
}
}
func main() {
userExpr := "stdin"
if len(os.Args) > 1 {
userExpr = os.Args[1]
}
parsedExpr, e, err := parser.ParseExpr(userExpr)
if err != nil || e != "" {
fmt.Errorf("error='%v', leftovers='%v'", err, e)
}
quit := make(chan bool)
defer close(quit)
ts_chan := make(chan float64)
defer close(ts_chan)
{
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
quitHandler := func(ui.Event) {
ui.StopLoop()
quit <- true
}
ui.Handle("C-c", quitHandler)
ui.Handle("q", quitHandler)
}
go func(ts_chan chan<- float64) {
readLoop(
func(s string) {
x, err := strconv.ParseFloat(s, 64)
if err == nil {
ts_chan <- x
}
},
)
}(ts_chan)
go func(ts_chan <-chan float64, quit <-chan bool) {
var ts []float64
for {
select {
case <-quit:
return
case x := <-ts_chan:
ts = append(ts, x)
if len(ts) > 60*60 {
ts = ts[len(ts)>>1:]
}
plot(apply(parsedExpr, ts))
}
}
}(ts_chan, quit)
ui.Loop()
}
func plot(ts []float64) {
chart := ui.NewLineChart()
chart.Data = map[string][]float64{"x": ts}
chart.Width = 200
chart.Height = 20
chart.X = 0
chart.Y = 0
chart.AxesColor = ui.ColorBlack
chart.LineColor["x"] = ui.ColorBlack
ui.Render(chart)
}
func apply(exp parser.Expr, ts []float64) []float64 {
metric := "stdin"
from := int64(0)
until := int64(len(ts))
metricData := types.MetricData{
FetchResponse: pb.FetchResponse{
Name: metric,
StartTime: from,
StopTime: until,
StepTime: 1,
Values: ts,
ConsolidationFunc: "average",
PathExpression: metric,
},
}
metricMap := make(map[parser.MetricRequest][]*types.MetricData)
{
request := parser.MetricRequest{
Metric: metric,
From: from,
Until: until,
}
metricMap[request] = []*types.MetricData{
&metricData,
}
}
rewrite.New(make(map[string]string))
functions.New(make(map[string]string))
out, err := expr.EvalExpr(exp, from, until, metricMap)
if err != nil {
fmt.Errorf("error='%v' expr='%v'", err, exp)
}
if len(out) > 0 {
return NaNToZero(out[0].Values)
}
return nil
}
func NaNToZero(xs []float64) []float64 {
var ys []float64
for _, x := range xs {
if math.IsNaN(x) {
x = 0
}
ys = append(ys, x)
}
return ys
}