-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute.go
325 lines (280 loc) · 6.56 KB
/
compute.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"fmt"
"strconv"
. "github.com/andyleap/parser"
)
type Compute interface {
Run(values map[string]float64) (float64, error)
GetVars() []*ComputeVariableFactor
}
type ComputeBase struct {
Expr Compute
}
func (b ComputeBase) Run(values map[string]float64) (float64, error) {
a, err := b.Expr.Run(values)
if err != nil {
return 0, err
}
return a, nil
}
func (b ComputeBase) GetVars() []*ComputeVariableFactor {
return b.Expr.GetVars()
}
type ComputeOperand struct {
Op string
Operand Compute
}
type ComputeTerm struct {
Start Compute
Ops []ComputeOperand
}
func (t ComputeTerm) Run(values map[string]float64) (float64, error) {
v, err := t.Start.Run(values)
if err != nil {
return 0, err
}
for _, op := range t.Ops {
opv, err := op.Operand.Run(values)
if err != nil {
return 0, err
}
switch op.Op {
case "+":
v += opv
case "-":
v -= opv
case "*":
v *= opv
case "/":
v /= opv
}
}
return v, nil
}
func (t ComputeTerm) GetVars() []*ComputeVariableFactor {
vars := t.Start.GetVars()
for _, op := range t.Ops {
vars = append(vars, op.Operand.GetVars()...)
}
return vars
}
type ComputeExprFactor struct {
Expr Compute
}
func (ef ComputeExprFactor) String() string {
return fmt.Sprintf("(%d)", ef.Expr)
}
func (ef ComputeExprFactor) Run(values map[string]float64) (float64, error) {
a, err := ef.Expr.Run(values)
if err != nil {
return 0, err
}
return a, nil
}
func (ef ComputeExprFactor) GetVars() []*ComputeVariableFactor {
return ef.Expr.GetVars()
}
type ComputeNumberFactor struct {
Number float64
}
func (nf ComputeNumberFactor) String() string {
return fmt.Sprint(nf.Number)
}
func (nf ComputeNumberFactor) Run(values map[string]float64) (float64, error) {
return nf.Number, nil
}
func (nf ComputeNumberFactor) GetVars() []*ComputeVariableFactor {
return []*ComputeVariableFactor{}
}
type ComputeVariableFactor struct {
Monitor string
Variable string
}
func (vf ComputeVariableFactor) String() string {
return fmt.Sprintf("%s.%s", vf.Monitor, vf.Variable)
}
func (vf ComputeVariableFactor) Run(values map[string]float64) (float64, error) {
m, ok := values[fmt.Sprintf("%s.%s", vf.Monitor, vf.Variable)]
if !ok {
return 0, fmt.Errorf("Can't find variable %s.%s", vf.Monitor, vf.Variable)
}
return m, nil
}
func (vf ComputeVariableFactor) GetVars() []*ComputeVariableFactor {
return []*ComputeVariableFactor{&vf}
}
var (
computeGrammar *Grammar
)
func init() {
number := And(Mult(0, 1, Lit("-")), Mult(1, 0, Set("0-9")), Mult(0, 1, And(Lit("."), Mult(0, 0, Set("0-9")))))
number.Node(func(m Match) (Match, error) {
v, err := strconv.ParseFloat(String(m), 64)
if err != nil {
return nil, err
}
return ComputeNumberFactor{Number: v}, nil
})
variable := And(
Tag("Monitor", Mult(1, 0, Set("a-zA-Z"))),
Lit("."),
Tag("Variable", Mult(1, 0, Set("a-zA-Z"))))
variable.Node(func(m Match) (Match, error) {
mtag := GetTag(m, "Monitor")
vtag := GetTag(m, "Variable")
return ComputeVariableFactor{
Monitor: String(mtag.Match),
Variable: String(vtag.Match),
}, nil
})
expr := &Grammar{}
parenexpr := And(Lit("("), Tag("Expr", expr), Lit(")"))
parenexpr.Node(func(m Match) (Match, error) {
etag := GetTag(m, "Expr")
return etag.Match, nil
})
factor := Or(parenexpr, variable, number)
term := And(Tag("Start", factor), Tag("Ops", Mult(0, 0, And(Tag("Op", Set("*/")), Tag("Operand", factor)))))
term.Node(func(m Match) (Match, error) {
fmt.Println(m)
start := GetTag(m, "Start").Match.(Compute)
ops := []ComputeOperand{}
for _, op := range GetTag(m, "Ops").Match.(MatchTree) {
ops = append(ops, ComputeOperand{
Op: string(GetTag(op, "Op").Match.(MatchString)),
Operand: GetTag(op, "Operand").Match.(Compute),
})
}
if len(ops) == 0 {
return start, nil
}
return ComputeTerm{
Start: start,
Ops: ops,
}, nil
})
expr.Set(And(Tag("Start", term), Tag("Ops", Mult(0, 0, And(Tag("Op", Set("-+")), Tag("Operand", term))))))
expr.Node(func(m Match) (Match, error) {
start := GetTag(m, "Start").Match.(Compute)
ops := []ComputeOperand{}
for _, op := range GetTag(m, "Ops").Match.(MatchTree) {
ops = append(ops, ComputeOperand{
Op: string(GetTag(op, "Op").Match.(MatchString)),
Operand: GetTag(op, "Operand").Match.(Compute),
})
}
if len(ops) == 0 {
return start, nil
}
return ComputeTerm{
Start: start,
Ops: ops,
}, nil
})
computeGrammar = expr
}
func Decode(expr string) (Compute, error) {
comp, err := computeGrammar.ParseString(expr)
if err != nil {
return nil, err
}
return comp.(Compute), nil
}
type ComputedVariable struct {
compute Compute
Name string
}
func RunComputeds(cvs []ComputedVariable) map[string]float64 {
vars := make(map[string]map[string]bool)
for _, cv := range cvs {
for _, v := range cv.compute.GetVars() {
mvars, ok := vars[v.Monitor]
if !ok {
mvars = make(map[string]bool)
vars[v.Monitor] = mvars
}
mvars[v.Variable] = true
}
}
values := make(map[string]float64)
for m, v := range vars {
vs := make([]string, 0)
for vn := range v {
vs = append(vs, vn)
}
vals := Config.Monitors[m].monitor.GetValues(vs)
for k, val := range vals {
var fval float64
switch tt := val.(type) {
case float64:
fval = tt
case float32:
fval = float64(tt)
case uint32:
fval = float64(tt)
case uint64:
fval = float64(tt)
case int32:
fval = float64(tt)
case int64:
fval = float64(tt)
default:
continue
}
values[fmt.Sprintf("%s.%s", m, k)] = fval
}
}
vals := make(map[string]float64)
for _, cv := range cvs {
val, err := cv.compute.Run(values)
if err != nil {
vals[cv.Name] = val
}
}
return vals
}
func RunCompute(c Compute) (float64, error) {
vars := make(map[string]map[string]bool)
for _, v := range c.GetVars() {
mvars, ok := vars[v.Monitor]
if !ok {
mvars = make(map[string]bool)
vars[v.Monitor] = mvars
}
mvars[v.Variable] = true
}
values := make(map[string]float64)
for m, v := range vars {
vs := make([]string, 0)
for vn := range v {
vs = append(vs, vn)
}
vals := Config.Monitors[m].monitor.GetValues(vs)
for k, val := range vals {
var fval float64
switch tt := val.(type) {
case float64:
fval = tt
case float32:
fval = float64(tt)
case uint32:
fval = float64(tt)
case uint64:
fval = float64(tt)
case int32:
fval = float64(tt)
case int64:
fval = float64(tt)
default:
continue
}
values[fmt.Sprintf("%s.%s", m, k)] = fval
}
}
val, err := c.Run(values)
if err != nil {
return 0, err
}
return val, nil
}