未验证 提交 018d1985 编写于 作者: D dongdong 提交者: GitHub

Fix stddev function (#224)

* Correct stddev function usage

* Simplify code

* Fix CI error
上级 4233c36f
......@@ -129,9 +129,8 @@ func (f AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonF
type StddevFunction struct {
Function
Limit int
Operator string
RightValue float64
Num int
Limit int
}
func (f StddevFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) {
......@@ -150,8 +149,12 @@ func (f StddevFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.Js
num += math.Pow(float64(vs[i].Value)-mean, 2)
}
leftValue = dataobj.JsonFloat(math.Sqrt(num / float64(f.Limit)))
isTriggered = checkIsTriggered(leftValue, f.Operator, f.RightValue)
std := math.Sqrt(num / float64(f.Limit))
upperBound := mean + std*float64(f.Num)
lowerBound := mean - std*float64(f.Num)
leftValue = vs[0].Value
isTriggered = checkIsTriggered(leftValue, "<", lowerBound) || checkIsTriggered(leftValue, ">", upperBound)
return
}
......@@ -365,7 +368,7 @@ func ParseFuncFromString(str string, span []interface{}, operator string, rightV
case "avg":
fn = &AvgFunction{Limit: limit, Operator: operator, RightValue: rightValue}
case "stddev":
fn = &StddevFunction{Limit: limit, Operator: operator, RightValue: rightValue}
fn = &StddevFunction{Limit: limit, Num: span[1].(int)}
case "diff":
fn = &DiffFunction{Limit: limit, Operator: operator, RightValue: rightValue}
case "pdiff":
......
......@@ -182,13 +182,13 @@ func Judge(stra *model.Stra, exps []model.Exp, historyData []*dataobj.HistoryDat
func judgeItemWithStrategy(stra *model.Stra, historyData []*dataobj.HistoryData, exp model.Exp, firstItem *dataobj.JudgeItem, now int64) (leftValue dataobj.JsonFloat, isTriggered bool) {
straFunc := exp.Func
straParam := []interface{}{}
var straParam []interface{}
if firstItem.Step == 0 {
logger.Errorf("wrong step:%+v", firstItem)
return
}
limit := stra.AlertDur / int(firstItem.Step)
limit := stra.AlertDur / firstItem.Step
if limit <= 0 {
limit = 1
}
......@@ -196,7 +196,7 @@ func judgeItemWithStrategy(stra *model.Stra, historyData []*dataobj.HistoryData,
straParam = append(straParam, limit)
switch straFunc {
case "happen":
case "happen", "stddev":
if len(exp.Params) < 1 {
logger.Errorf("stra:%d exp:%+v stra param is null", stra.Id, exp)
return
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册