models.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package alerting
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/log"
  6. )
  7. type AlertJob struct {
  8. Offset int64
  9. Delay bool
  10. Running bool
  11. RetryCount int
  12. Rule *AlertRule
  13. }
  14. func (aj *AlertJob) Retryable() bool {
  15. return aj.RetryCount < maxAlertExecutionRetries
  16. }
  17. func (aj *AlertJob) ResetRetry() {
  18. aj.RetryCount = 0
  19. }
  20. func (aj *AlertJob) IncRetry() {
  21. aj.RetryCount++
  22. }
  23. type AlertResultContext struct {
  24. Firing bool
  25. IsTestRun bool
  26. Events []*AlertEvent
  27. Logs []*AlertResultLogEntry
  28. Error error
  29. Description string
  30. StartTime time.Time
  31. EndTime time.Time
  32. Rule *AlertRule
  33. DoneChan chan bool
  34. CancelChan chan bool
  35. log log.Logger
  36. }
  37. func (a *AlertResultContext) GetDurationMs() float64 {
  38. return float64(a.EndTime.Nanosecond()-a.StartTime.Nanosecond()) / float64(1000000)
  39. }
  40. func NewAlertResultContext(rule *AlertRule) *AlertResultContext {
  41. return &AlertResultContext{
  42. StartTime: time.Now(),
  43. Rule: rule,
  44. Logs: make([]*AlertResultLogEntry, 0),
  45. Events: make([]*AlertEvent, 0),
  46. DoneChan: make(chan bool, 1),
  47. CancelChan: make(chan bool, 1),
  48. log: log.New("alerting.engine"),
  49. }
  50. }
  51. type AlertResultLogEntry struct {
  52. Message string
  53. Data interface{}
  54. }
  55. type AlertEvent struct {
  56. Value float64
  57. Metric string
  58. State string
  59. Tags map[string]string
  60. }
  61. type Level struct {
  62. Operator string
  63. Value float64
  64. }
  65. type AlertQuery struct {
  66. Model *simplejson.Json
  67. DatasourceId int64
  68. From string
  69. To string
  70. }