eval_context.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package alerting
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. type EvalContext struct {
  12. Firing bool
  13. IsTestRun bool
  14. EvalMatches []*EvalMatch
  15. Logs []*ResultLogEntry
  16. Error error
  17. Description string
  18. StartTime time.Time
  19. EndTime time.Time
  20. Rule *Rule
  21. log log.Logger
  22. dashboardSlug string
  23. ImagePublicUrl string
  24. ImageOnDiskPath string
  25. NoDataFound bool
  26. PrevAlertState m.AlertStateType
  27. Ctx context.Context
  28. }
  29. type StateDescription struct {
  30. Color string
  31. Text string
  32. Data string
  33. }
  34. func (c *EvalContext) GetStateModel() *StateDescription {
  35. switch c.Rule.State {
  36. case m.AlertStateOK:
  37. return &StateDescription{
  38. Color: "#36a64f",
  39. Text: "OK",
  40. }
  41. case m.AlertStateNoData:
  42. return &StateDescription{
  43. Color: "#888888",
  44. Text: "No Data",
  45. }
  46. case m.AlertStateExecError:
  47. return &StateDescription{
  48. Color: "#000",
  49. Text: "Execution Error",
  50. }
  51. case m.AlertStateAlerting:
  52. return &StateDescription{
  53. Color: "#D63232",
  54. Text: "Alerting",
  55. }
  56. default:
  57. panic("Unknown rule state " + c.Rule.State)
  58. }
  59. }
  60. func (c *EvalContext) ShouldUpdateAlertState() bool {
  61. return c.Rule.State != c.PrevAlertState
  62. }
  63. func (c *EvalContext) ShouldSendNotification() bool {
  64. if (c.PrevAlertState == m.AlertStatePending) && (c.Rule.State == m.AlertStateOK) {
  65. return false
  66. }
  67. alertState := c.Rule.State
  68. if c.NoDataFound {
  69. if c.Rule.NoDataState == m.NoDataKeepState {
  70. return false
  71. }
  72. alertState = c.Rule.NoDataState.ToAlertState()
  73. }
  74. if c.Error != nil {
  75. if c.Rule.ExecutionErrorState == m.NoDataKeepState {
  76. return false
  77. }
  78. alertState = c.Rule.ExecutionErrorState.ToAlertState()
  79. }
  80. return alertState != c.PrevAlertState
  81. }
  82. func (a *EvalContext) GetDurationMs() float64 {
  83. return float64(a.EndTime.Nanosecond()-a.StartTime.Nanosecond()) / float64(1000000)
  84. }
  85. func (c *EvalContext) GetNotificationTitle() string {
  86. return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
  87. }
  88. func (c *EvalContext) GetDashboardSlug() (string, error) {
  89. if c.dashboardSlug != "" {
  90. return c.dashboardSlug, nil
  91. }
  92. slugQuery := &m.GetDashboardSlugByIdQuery{Id: c.Rule.DashboardId}
  93. if err := bus.Dispatch(slugQuery); err != nil {
  94. return "", err
  95. }
  96. c.dashboardSlug = slugQuery.Result
  97. return c.dashboardSlug, nil
  98. }
  99. func (c *EvalContext) GetRuleUrl() (string, error) {
  100. if c.IsTestRun {
  101. return setting.AppUrl, nil
  102. }
  103. if slug, err := c.GetDashboardSlug(); err != nil {
  104. return "", err
  105. } else {
  106. ruleUrl := fmt.Sprintf("%sdashboard/db/%s?fullscreen&edit&tab=alert&panelId=%d", setting.AppUrl, slug, c.Rule.PanelId)
  107. return ruleUrl, nil
  108. }
  109. }
  110. func NewEvalContext(alertCtx context.Context, rule *Rule) *EvalContext {
  111. return &EvalContext{
  112. Ctx: alertCtx,
  113. StartTime: time.Now(),
  114. Rule: rule,
  115. Logs: make([]*ResultLogEntry, 0),
  116. EvalMatches: make([]*EvalMatch, 0),
  117. log: log.New("alerting.evalContext"),
  118. }
  119. }