rule.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. Severity m.AlertSeverityType
  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 {
  39. multiplier := 1
  40. value, _ := strconv.Atoi(ValueFormatRegex.FindAllString(str, 1)[0])
  41. unit := UnitFormatRegex.FindAllString(str, 1)[0]
  42. if val, ok := unitMultiplier[unit]; ok {
  43. multiplier = val
  44. }
  45. return int64(value * multiplier)
  46. }
  47. func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
  48. model := &Rule{}
  49. model.Id = ruleDef.Id
  50. model.OrgId = ruleDef.OrgId
  51. model.DashboardId = ruleDef.DashboardId
  52. model.PanelId = ruleDef.PanelId
  53. model.Name = ruleDef.Name
  54. model.Message = ruleDef.Message
  55. model.Frequency = ruleDef.Frequency
  56. model.Severity = ruleDef.Severity
  57. model.State = ruleDef.State
  58. model.NoDataState = m.AlertStateType(ruleDef.Settings.Get("noDataState").MustString("unknown"))
  59. for _, v := range ruleDef.Settings.Get("notifications").MustArray() {
  60. jsonModel := simplejson.NewFromAny(v)
  61. if id, err := jsonModel.Get("id").Int64(); err != nil {
  62. return nil, ValidationError{Reason: "Invalid notification schema"}
  63. } else {
  64. model.Notifications = append(model.Notifications, id)
  65. }
  66. }
  67. for index, condition := range ruleDef.Settings.Get("conditions").MustArray() {
  68. conditionModel := simplejson.NewFromAny(condition)
  69. conditionType := conditionModel.Get("type").MustString()
  70. if factory, exist := conditionFactories[conditionType]; !exist {
  71. return nil, ValidationError{Reason: "Unknown alert condition: " + conditionType}
  72. } else {
  73. if queryCondition, err := factory(conditionModel, index); err != nil {
  74. return nil, err
  75. } else {
  76. model.Conditions = append(model.Conditions, queryCondition)
  77. }
  78. }
  79. }
  80. if len(model.Conditions) == 0 {
  81. return nil, fmt.Errorf("Alert is missing conditions")
  82. }
  83. return model, nil
  84. }
  85. type ConditionFactory func(model *simplejson.Json, index int) (Condition, error)
  86. var conditionFactories map[string]ConditionFactory = make(map[string]ConditionFactory)
  87. func RegisterCondition(typeName string, factory ConditionFactory) {
  88. conditionFactories[typeName] = factory
  89. }