evaluator.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conditions
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/pkg/utils"
  19. "yunion.io/x/onecloud/pkg/apis/monitor"
  20. "yunion.io/x/onecloud/pkg/monitor/validators"
  21. )
  22. // AlertEvaluator evaluates the reduced value of a timeserie.
  23. // Returning true if a timeseries is violating the condition
  24. // ex: ThresholdEvaluator, NoValueEvaluator, RangeEvaluator
  25. type AlertEvaluator interface {
  26. Eval(reducedValue *float64) bool
  27. String() string
  28. }
  29. type noValueEvaluator struct{}
  30. func (e *noValueEvaluator) Eval(reducedValue *float64) bool {
  31. return reducedValue == nil
  32. }
  33. func (e *noValueEvaluator) String() string {
  34. return "no_data"
  35. }
  36. type thresholdEvaluator struct {
  37. Type string
  38. Threshold float64
  39. }
  40. func newThresholdEvaluator(cond *monitor.Condition) (*thresholdEvaluator, error) {
  41. defaultEval := &thresholdEvaluator{
  42. Type: cond.Type,
  43. Threshold: cond.Params[0],
  44. }
  45. return defaultEval, nil
  46. }
  47. func (e *thresholdEvaluator) Eval(reducedValue *float64) bool {
  48. if reducedValue == nil {
  49. return false
  50. }
  51. val := *reducedValue
  52. switch e.Type {
  53. case "gt":
  54. return val > e.Threshold
  55. case "eq":
  56. return val == e.Threshold
  57. case "lt":
  58. return val < e.Threshold
  59. }
  60. return false
  61. }
  62. func (e *thresholdEvaluator) String() string {
  63. var op string
  64. switch e.Type {
  65. case "gt":
  66. op = ">"
  67. case "eq":
  68. op = "="
  69. case "lt":
  70. op = "<"
  71. }
  72. return fmt.Sprintf("%s %.2f", op, e.Threshold)
  73. }
  74. type rangedEvaluator struct {
  75. Type string
  76. Lower float64
  77. Upper float64
  78. }
  79. func newRangedEvaluator(cond *monitor.Condition) (*rangedEvaluator, error) {
  80. if len(cond.Params) == 0 {
  81. return nil, errors.Wrap(validators.ErrMissingParameterThreshold, "RangedEvaluator parameter is empty")
  82. }
  83. if len(cond.Params) == 1 {
  84. return nil, errors.Wrap(validators.ErrMissingParameterThreshold, "RangedEvaluator parameter second parameter is missing")
  85. }
  86. rangedEval := &rangedEvaluator{
  87. Type: cond.Type,
  88. Lower: cond.Params[0],
  89. Upper: cond.Params[1],
  90. }
  91. return rangedEval, nil
  92. }
  93. func (e *rangedEvaluator) Eval(reducedValue *float64) bool {
  94. if reducedValue == nil {
  95. return false
  96. }
  97. val := *reducedValue
  98. switch e.Type {
  99. case "within_range":
  100. return (e.Lower < val && e.Upper > val) || (e.Upper < val && e.Lower > val)
  101. case "outside_range":
  102. return (e.Upper < val && e.Lower < val) || (e.Upper > val && e.Lower > val)
  103. }
  104. return false
  105. }
  106. func (e *rangedEvaluator) String() string {
  107. return fmt.Sprintf("%s [%.2f, %.2f]", e.Type, e.Lower, e.Upper)
  108. }
  109. // NewAlertEvaluator is a factory function for returning
  110. // an `AlertEvaluator` depending on the input condition.
  111. func NewAlertEvaluator(cond *monitor.Condition) (AlertEvaluator, error) {
  112. typ := cond.Type
  113. if typ == "" {
  114. return nil, validators.ErrMissingParameterType
  115. }
  116. if utils.IsInStringArray(string(typ), validators.EvaluatorDefaultTypes) {
  117. return newThresholdEvaluator(cond)
  118. }
  119. if utils.IsInStringArray(string(typ), validators.EvaluatorRangedTypes) {
  120. return newRangedEvaluator(cond)
  121. }
  122. if typ == "no_value" {
  123. return &noValueEvaluator{}, nil
  124. }
  125. return nil, errors.Wrapf(validators.ErrInvalidEvaluatorType, "type: %s", typ)
  126. }