Skip to content

Commit

Permalink
Fix stddev function (ccfos#224)
Browse files Browse the repository at this point in the history
* Correct stddev function usage

* Simplify code

* Fix CI error
  • Loading branch information
chenjiandongx authored Jun 20, 2020
1 parent 4233c36 commit 018d198
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/modules/judge/judge/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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":
Expand Down
6 changes: 3 additions & 3 deletions src/modules/judge/judge/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,21 @@ 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
}

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
Expand Down

0 comments on commit 018d198

Please sign in to comment.