From b45a968a9ac8921a8b8d721e0ba5f43de6baf464 Mon Sep 17 00:00:00 2001 From: dongdong Date: Tue, 16 Jun 2020 14:26:00 +0800 Subject: [PATCH] Add stddev funtion for judge (#214) --- src/modules/judge/judge/func.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/modules/judge/judge/func.go b/src/modules/judge/judge/func.go index 51d4cc33..749cd041 100644 --- a/src/modules/judge/judge/func.go +++ b/src/modules/judge/judge/func.go @@ -127,6 +127,34 @@ func (f AvgFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonF return } +type StddevFunction struct { + Function + Limit int + Operator string + RightValue float64 +} + +func (f StddevFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj.JsonFloat, isTriggered bool) { + if len(vs) < f.Limit { + return + } + + var sum float64 + for i := 0; i < f.Limit; i++ { + sum += float64(vs[i].Value) + } + mean := sum / float64(f.Limit) + + var num float64 + for i := 0; i < f.Limit; i++ { + 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) + return +} + type DiffFunction struct { Function Limit int @@ -321,7 +349,7 @@ func (f CAvgRateFunction) Compute(vs []*dataobj.HistoryData) (leftValue dataobj. func ParseFuncFromString(str string, span []interface{}, operator string, rightValue float64) (fn Function, err error) { if str == "" { - return nil, fmt.Errorf("func can not be null!") + return nil, fmt.Errorf("func can not be null") } limit := span[0].(int) @@ -336,6 +364,8 @@ func ParseFuncFromString(str string, span []interface{}, operator string, rightV fn = &SumFunction{Limit: limit, Operator: operator, RightValue: rightValue} case "avg": fn = &AvgFunction{Limit: limit, Operator: operator, RightValue: rightValue} + case "stddev": + fn = &StddevFunction{Limit: limit, Operator: operator, RightValue: rightValue} case "diff": fn = &DiffFunction{Limit: limit, Operator: operator, RightValue: rightValue} case "pdiff":