/
expression.go
80 lines (68 loc) · 1.51 KB
/
expression.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
package db
import (
"fmt"
"github.com/open-falcon/falcon-plus/common/model"
"log"
"strings"
)
func QueryExpressions() (ret []*model.Expression, err error) {
sql := "select id, expression, func, op, right_value, max_step, priority, note, action_id from expression where action_id>0 and pause=0"
rows, err := DB.Query(sql)
if err != nil {
log.Println("ERROR:", err)
return ret, err
}
defer rows.Close()
for rows.Next() {
e := model.Expression{}
var exp string
err = rows.Scan(
&e.Id,
&exp,
&e.Func,
&e.Operator,
&e.RightValue,
&e.MaxStep,
&e.Priority,
&e.Note,
&e.ActionId,
)
if err != nil {
log.Println("WARN:", err)
continue
}
e.Metric, e.Tags, err = parseExpression(exp)
if err != nil {
log.Println("ERROR:", err)
continue
}
ret = append(ret, &e)
}
return ret, nil
}
func parseExpression(exp string) (metric string, tags map[string]string, err error) {
left := strings.Index(exp, "(")
right := strings.Index(exp, ")")
tagStrs := strings.TrimSpace(exp[left+1 : right])
arr := strings.Fields(tagStrs)
if len(arr) < 2 {
err = fmt.Errorf("tag not enough. exp: %s", exp)
return
}
tags = make(map[string]string)
for _, item := range arr {
kv := strings.Split(item, "=")
if len(kv) != 2 {
err = fmt.Errorf("parse %s fail", exp)
return
}
tags[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}
metric, exists := tags["metric"]
if !exists {
err = fmt.Errorf("no metric give of %s", exp)
return
}
delete(tags, "metric")
return
}