rule.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package alerting
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. )
  9. type Rule struct {
  10. Id int64
  11. OrgId int64
  12. DashboardId int64
  13. PanelId int64
  14. Frequency int64
  15. Name string
  16. Message string
  17. NoDataState m.NoDataOption
  18. ExecutionErrorState m.ExecutionErrorOption
  19. State m.AlertStateType
  20. Conditions []Condition
  21. Notifications []int64
  22. }
  23. type ValidationError struct {
  24. Reason string
  25. }
  26. func (e ValidationError) Error() string {
  27. return e.Reason
  28. }
  29. var (
  30. ValueFormatRegex = regexp.MustCompile("^\\d+")
  31. UnitFormatRegex = regexp.MustCompile("\\w{1}$")
  32. )
  33. var unitMultiplier = map[string]int{
  34. "s": 1,
  35. "m": 60,
  36. "h": 3600,
  37. }
  38. func getTimeDurationStringToSeconds(str string) (int64, error) {
  39. multiplier := 1
  40. matches := ValueFormatRegex.FindAllString(str, 1)
  41. if len(matches) <= 0 {
  42. return 0, fmt.Errorf("Frequency could not be parsed")
  43. }
  44. value, err := strconv.Atoi(matches[0])
  45. if err != nil {
  46. return 0, err
  47. }
  48. unit := UnitFormatRegex.FindAllString(str, 1)[0]
  49. if val, ok := unitMultiplier[unit]; ok {
  50. multiplier = val
  51. }
  52. return int64(value * multiplier), nil
  53. }
  54. func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
  55. model := &Rule{}
  56. model.Id = ruleDef.Id
  57. model.OrgId = ruleDef.OrgId
  58. model.DashboardId = ruleDef.DashboardId
  59. model.PanelId = ruleDef.PanelId
  60. model.Name = ruleDef.Name
  61. model.Message = ruleDef.Message
  62. model.Frequency = ruleDef.Frequency
  63. model.State = ruleDef.State
  64. model.NoDataState = m.NoDataOption(ruleDef.Settings.Get("noDataState").MustString("no_data"))
  65. model.ExecutionErrorState = m.ExecutionErrorOption(ruleDef.Settings.Get("executionErrorState").MustString("alerting"))
  66. for _, v := range ruleDef.Settings.Get("notifications").MustArray() {
  67. jsonModel := simplejson.NewFromAny(v)
  68. if id, err := jsonModel.Get("id").Int64(); err != nil {
  69. return nil, ValidationError{Reason: "Invalid notification schema"}
  70. } else {
  71. model.Notifications = append(model.Notifications, id)
  72. }
  73. }
  74. for index, condition := range ruleDef.Settings.Get("conditions").MustArray() {
  75. conditionModel := simplejson.NewFromAny(condition)
  76. conditionType := conditionModel.Get("type").MustString()
  77. if factory, exist := conditionFactories[conditionType]; !exist {
  78. return nil, ValidationError{Reason: "Unknown alert condition: " + conditionType}
  79. } else {
  80. if queryCondition, err := factory(conditionModel, index); err != nil {
  81. return nil, err
  82. } else {
  83. model.Conditions = append(model.Conditions, queryCondition)
  84. }
  85. }
  86. }
  87. if len(model.Conditions) == 0 {
  88. return nil, fmt.Errorf("Alert is missing conditions")
  89. }
  90. return model, nil
  91. }
  92. type ConditionFactory func(model *simplejson.Json, index int) (Condition, error)
  93. var conditionFactories map[string]ConditionFactory = make(map[string]ConditionFactory)
  94. func RegisterCondition(typeName string, factory ConditionFactory) {
  95. conditionFactories[typeName] = factory
  96. }