@@ -295,6 +295,93 @@ func (this PDiffFunction) Compute(L *SafeLinkedList) (vs []*model.HistoryData, l
295
295
return
296
296
}
297
297
298
+
299
+ // 3
300
+ // ____
301
+ // 3 /
302
+ // ____/
303
+ //
304
+ //
305
+ //kpdiff(#3,3) 告警上面类似的数据模型,解决某个指标长期稳定,当指标变化并且是持续的情况下告警。原来的diff 跟pdiff是只要一个点变化就告警。
306
+ //kpdiff(#3,3) 左边的3是指稳定数据走势的数据点,右边的3是指持续变化的数据点
307
+
308
+ type KPDiffFunction struct {
309
+ Function
310
+ Num int
311
+ Limit int
312
+ Operator string
313
+ RightValue float64
314
+ }
315
+
316
+ func (this KPDiffFunction ) Compute (L * SafeLinkedList ) (vs []* model.HistoryData , leftValue float64 , isTriggered bool , isEnough bool ) {
317
+ vs , isEnough = L .HistoryData (this .Limit + this .Num )
318
+ if ! isEnough {
319
+ return
320
+ }
321
+
322
+ if len (vs ) == 0 {
323
+ isEnough = false
324
+ return
325
+ }
326
+ for i := 0 ; i < this .Num ; i ++ {
327
+ isTriggered = false
328
+ if vs [i ].Value == 0 {
329
+ break
330
+ }
331
+
332
+ //kpdiff(#3,3) 全部右边的点都对全部左边的点相减,得到3*3个差,再将3*3个差值分别除以对应左边的点,得到3*3个商值,全部商值满足阈值则报警
333
+ for j := 0 ; j < this .Limit ; j ++ {
334
+ leftValue = (vs [j ].Value - vs [this .Limit + this .Num - 1 - i ].Value ) / vs [j ].Value * 100.0
335
+ isTriggered = checkIsTriggered (leftValue , this .Operator , this .RightValue )
336
+ if isTriggered == false {
337
+ break
338
+ }
339
+ }
340
+ if isTriggered == false {
341
+ return
342
+ }
343
+ }
344
+ return
345
+ }
346
+
347
+ type KDiffFunction struct {
348
+ Function
349
+ Num int
350
+ Limit int
351
+ Operator string
352
+ RightValue float64
353
+ }
354
+
355
+ func (this KDiffFunction ) Compute (L * SafeLinkedList ) (vs []* model.HistoryData , leftValue float64 , isTriggered bool , isEnough bool ) {
356
+ vs , isEnough = L .HistoryData (this .Limit + this .Num )
357
+ if ! isEnough {
358
+ return
359
+ }
360
+
361
+ if len (vs ) == 0 {
362
+ isEnough = false
363
+ return
364
+ }
365
+ for i := 0 ; i < this .Num ; i ++ {
366
+ isTriggered = false
367
+ if vs [i ].Value == 0 {
368
+ break
369
+ }
370
+
371
+ for j := 0 ; j < this .Limit ; j ++ {
372
+ leftValue = vs [j ].Value - vs [this .Limit + this .Num - 1 - i ].Value
373
+ isTriggered = checkIsTriggered (leftValue , this .Operator , this .RightValue )
374
+ if isTriggered == false {
375
+ break
376
+ }
377
+ }
378
+ if isTriggered == false {
379
+ return
380
+ }
381
+ }
382
+ return
383
+ }
384
+
298
385
func atois (s string ) (ret []int , err error ) {
299
386
a := strings .Split (s , "," )
300
387
ret = make ([]int , len (a ))
@@ -337,6 +424,10 @@ func ParseFuncFromString(str string, operator string, rightValue float64) (fn Fu
337
424
fn = & LookupFunction {Num : args [0 ], Limit : args [1 ], Operator : operator , RightValue : rightValue }
338
425
case "stddev" :
339
426
fn = & StdDeviationFunction {Limit : args [0 ], Operator : operator , RightValue : rightValue }
427
+ case "kdiff" :
428
+ fn = & KDiffFunction {Num : args [0 ], Limit : args [1 ], Operator : operator , RightValue : rightValue }
429
+ case "kpdiff" :
430
+ fn = & KPDiffFunction {Num : args [0 ], Limit : args [1 ], Operator : operator , RightValue : rightValue }
340
431
default :
341
432
err = fmt .Errorf ("not_supported_method" )
342
433
}
0 commit comments