eval_context.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. RetryCount int
  27. Context context.Context
  28. }
  29. func (evalContext *EvalContext) Deadline() (deadline time.Time, ok bool) {
  30. return evalContext.Deadline()
  31. }
  32. func (evalContext *EvalContext) Done() <-chan struct{} {
  33. return evalContext.Context.Done()
  34. }
  35. func (evalContext *EvalContext) Err() error {
  36. return evalContext.Context.Err()
  37. }
  38. func (evalContext *EvalContext) Value(key interface{}) interface{} {
  39. return evalContext.Context.Value(key)
  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.AlertStateExecError:
  59. return &StateDescription{
  60. Color: "#000",
  61. Text: "Execution Error",
  62. }
  63. case m.AlertStateAlerting:
  64. return &StateDescription{
  65. Color: "#D63232",
  66. Text: "Alerting",
  67. }
  68. default:
  69. panic("Unknown rule state " + c.Rule.State)
  70. }
  71. }
  72. func (a *EvalContext) GetDurationMs() float64 {
  73. return float64(a.EndTime.Nanosecond()-a.StartTime.Nanosecond()) / float64(1000000)
  74. }
  75. func (c *EvalContext) GetNotificationTitle() string {
  76. return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
  77. }
  78. func (c *EvalContext) GetDashboardSlug() (string, error) {
  79. if c.dashboardSlug != "" {
  80. return c.dashboardSlug, nil
  81. }
  82. slugQuery := &m.GetDashboardSlugByIdQuery{Id: c.Rule.DashboardId}
  83. if err := bus.Dispatch(slugQuery); err != nil {
  84. return "", err
  85. }
  86. c.dashboardSlug = slugQuery.Result
  87. return c.dashboardSlug, nil
  88. }
  89. func (c *EvalContext) GetRuleUrl() (string, error) {
  90. if slug, err := c.GetDashboardSlug(); err != nil {
  91. return "", err
  92. } else {
  93. ruleUrl := fmt.Sprintf("%sdashboard/db/%s?fullscreen&edit&tab=alert&panelId=%d", setting.AppUrl, slug, c.Rule.PanelId)
  94. return ruleUrl, nil
  95. }
  96. }
  97. func NewEvalContext(alertCtx context.Context, rule *Rule) *EvalContext {
  98. return &EvalContext{
  99. Context: alertCtx,
  100. StartTime: time.Now(),
  101. Rule: rule,
  102. Logs: make([]*ResultLogEntry, 0),
  103. EvalMatches: make([]*EvalMatch, 0),
  104. log: log.New("alerting.evalContext"),
  105. RetryCount: 0,
  106. }
  107. }