-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
72 lines (62 loc) · 1.51 KB
/
func.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
package alert
import (
"fmt"
"github.com/wxjuyun/common/model"
"math"
"strconv"
"strings"
)
type Function interface {
Compute(L *SafeLinkedList) (isTriggered bool)
}
type AllFunction struct {
Function
Limit int
Operator string
RightValue float64
}
func (this AllFunction) Compute(L *SafeLinkedList) (isTriggered bool) {
if L.Len() < this.Limit {
return false
}
isTriggered = true
for i := 0; i < this.Limit; i++ {
isTriggered = checkIsTriggered(L.Elem(i).Value.(*model.MetricValue).Value, this.Operator, this.RightValue)
if !isTriggered {
break
}
}
return
}
// @str: e.g. all(#3) sum(#3) avg(#10) diff(#10)
func ParseFuncFromString(str string, operator string, rightValue float64) (fn Function, err error) {
idx := strings.Index(str, "#")
limit, err := strconv.ParseInt(str[idx+1:len(str)-1], 10, 64)
if err != nil {
return nil, err
}
switch str[:idx-1] {
case "all":
fn = &AllFunction{Limit: int(limit), Operator: operator, RightValue: rightValue}
default:
err = fmt.Errorf("not_supported_method")
}
return
}
func checkIsTriggered(leftValue float64, operator string, rightValue float64) (isTriggered bool) {
switch operator {
case "=", "==":
isTriggered = math.Abs(leftValue-rightValue) < 0.0001
case "!=":
isTriggered = math.Abs(leftValue-rightValue) > 0.0001
case "<":
isTriggered = leftValue < rightValue
case "<=":
isTriggered = leftValue <= rightValue
case ">":
isTriggered = leftValue > rightValue
case ">=":
isTriggered = leftValue >= rightValue
}
return
}