eval_context.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. FiringEval string
  19. StartTime time.Time
  20. EndTime time.Time
  21. Rule *Rule
  22. log log.Logger
  23. dashboardSlug string
  24. ImagePublicUrl string
  25. ImageOnDiskPath string
  26. NoDataFound bool
  27. PrevAlertState m.AlertStateType
  28. Ctx context.Context
  29. }
  30. func NewEvalContext(alertCtx context.Context, rule *Rule) *EvalContext {
  31. return &EvalContext{
  32. Ctx: alertCtx,
  33. StartTime: time.Now(),
  34. Rule: rule,
  35. Logs: make([]*ResultLogEntry, 0),
  36. EvalMatches: make([]*EvalMatch, 0),
  37. log: log.New("alerting.evalContext"),
  38. PrevAlertState: rule.State,
  39. }
  40. }
  41. type StateDescription struct {
  42. Color string
  43. Text string
  44. Data string
  45. }
  46. func (c *EvalContext) GetStateModel() *StateDescription {
  47. switch c.Rule.State {
  48. case m.AlertStateOK:
  49. return &StateDescription{
  50. Color: "#36a64f",
  51. Text: "OK",
  52. }
  53. case m.AlertStateNoData:
  54. return &StateDescription{
  55. Color: "#888888",
  56. Text: "No Data",
  57. }
  58. case m.AlertStateAlerting:
  59. return &StateDescription{
  60. Color: "#D63232",
  61. Text: "Alerting",
  62. }
  63. default:
  64. panic("Unknown rule state " + c.Rule.State)
  65. }
  66. }
  67. func (c *EvalContext) ShouldUpdateAlertState() bool {
  68. return c.Rule.State != c.PrevAlertState
  69. }
  70. func (c *EvalContext) ShouldSendNotification() bool {
  71. if (c.PrevAlertState == m.AlertStatePending) && (c.Rule.State == m.AlertStateOK) {
  72. return false
  73. }
  74. return true
  75. }
  76. func (a *EvalContext) GetDurationMs() float64 {
  77. return float64(a.EndTime.Nanosecond()-a.StartTime.Nanosecond()) / float64(1000000)
  78. }
  79. func (c *EvalContext) GetNotificationTitle() string {
  80. return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
  81. }
  82. func (c *EvalContext) GetDashboardSlug() (string, error) {
  83. if c.dashboardSlug != "" {
  84. return c.dashboardSlug, nil
  85. }
  86. slugQuery := &m.GetDashboardSlugByIdQuery{Id: c.Rule.DashboardId}
  87. if err := bus.Dispatch(slugQuery); err != nil {
  88. return "", err
  89. }
  90. c.dashboardSlug = slugQuery.Result
  91. return c.dashboardSlug, nil
  92. }
  93. func (c *EvalContext) GetRuleUrl() (string, error) {
  94. if c.IsTestRun {
  95. return setting.AppUrl, nil
  96. }
  97. if slug, err := c.GetDashboardSlug(); err != nil {
  98. return "", err
  99. } else {
  100. ruleUrl := fmt.Sprintf("%sdashboard/db/%s?fullscreen&edit&tab=alert&panelId=%d", setting.AppUrl, slug, c.Rule.PanelId)
  101. return ruleUrl, nil
  102. }
  103. }