alerting.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dtos
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/grafana/grafana/pkg/components/null"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/models"
  8. )
  9. type AlertRule struct {
  10. Id int64 `json:"id"`
  11. DashboardId int64 `json:"dashboardId"`
  12. PanelId int64 `json:"panelId"`
  13. Name string `json:"name"`
  14. Message string `json:"message"`
  15. State models.AlertStateType `json:"state"`
  16. NewStateDate time.Time `json:"newStateDate"`
  17. EvalDate time.Time `json:"evalDate"`
  18. EvalData *simplejson.Json `json:"evalData"`
  19. ExecutionError string `json:"executionError"`
  20. Url string `json:"url"`
  21. CanEdit bool `json:"canEdit"`
  22. }
  23. func formatShort(interval time.Duration) string {
  24. var result string
  25. hours := interval / time.Hour
  26. if hours > 0 {
  27. result += fmt.Sprintf("%dh", hours)
  28. }
  29. remaining := interval - (hours * time.Hour)
  30. mins := remaining / time.Minute
  31. if mins > 0 {
  32. result += fmt.Sprintf("%dm", mins)
  33. }
  34. remaining = remaining - (mins * time.Minute)
  35. seconds := remaining / time.Second
  36. if seconds > 0 {
  37. result += fmt.Sprintf("%ds", seconds)
  38. }
  39. return result
  40. }
  41. func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
  42. return &AlertNotification{
  43. Id: notification.Id,
  44. Name: notification.Name,
  45. Type: notification.Type,
  46. IsDefault: notification.IsDefault,
  47. Created: notification.Created,
  48. Updated: notification.Updated,
  49. Frequency: formatShort(notification.Frequency),
  50. SendReminder: notification.SendReminder,
  51. DisableResolveMessage: notification.DisableResolveMessage,
  52. Settings: notification.Settings,
  53. }
  54. }
  55. type AlertNotification struct {
  56. Id int64 `json:"id"`
  57. Name string `json:"name"`
  58. Type string `json:"type"`
  59. IsDefault bool `json:"isDefault"`
  60. SendReminder bool `json:"sendReminder"`
  61. DisableResolveMessage bool `json:"disableResolveMessage"`
  62. Frequency string `json:"frequency"`
  63. Created time.Time `json:"created"`
  64. Updated time.Time `json:"updated"`
  65. Settings *simplejson.Json `json:"settings"`
  66. }
  67. type AlertTestCommand struct {
  68. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  69. PanelId int64 `json:"panelId" binding:"Required"`
  70. }
  71. type AlertTestResult struct {
  72. Firing bool `json:"firing"`
  73. State models.AlertStateType `json:"state"`
  74. ConditionEvals string `json:"conditionEvals"`
  75. TimeMs string `json:"timeMs"`
  76. Error string `json:"error,omitempty"`
  77. EvalMatches []*EvalMatch `json:"matches,omitempty"`
  78. Logs []*AlertTestResultLog `json:"logs,omitempty"`
  79. }
  80. type AlertTestResultLog struct {
  81. Message string `json:"message"`
  82. Data interface{} `json:"data"`
  83. }
  84. type EvalMatch struct {
  85. Tags map[string]string `json:"tags,omitempty"`
  86. Metric string `json:"metric"`
  87. Value null.Float `json:"value"`
  88. }
  89. type NotificationTestCommand struct {
  90. Name string `json:"name"`
  91. Type string `json:"type"`
  92. SendReminder bool `json:"sendReminder"`
  93. DisableResolveMessage bool `json:"disableResolveMessage"`
  94. Frequency string `json:"frequency"`
  95. Settings *simplejson.Json `json:"settings"`
  96. }
  97. type PauseAlertCommand struct {
  98. AlertId int64 `json:"alertId"`
  99. Paused bool `json:"paused"`
  100. }
  101. type PauseAllAlertsCommand struct {
  102. Paused bool `json:"paused"`
  103. }