Fix stddev function (#224)

* Correct stddev function usage

* Simplify code

* Fix CI error
This commit is contained in:
dongdong 2020-06-20 13:56:52 +08:00 committed by GitHub
parent 4233c36fd0
commit 018d19857d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View File

@ -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":

View File

@ -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