rule.go 2.6 KB

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