rule.go 2.8 KB

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