alerting.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Uid: notification.Uid,
  45. Name: notification.Name,
  46. Type: notification.Type,
  47. IsDefault: notification.IsDefault,
  48. Created: notification.Created,
  49. Updated: notification.Updated,
  50. Frequency: formatShort(notification.Frequency),
  51. SendReminder: notification.SendReminder,
  52. DisableResolveMessage: notification.DisableResolveMessage,
  53. Settings: notification.Settings,
  54. }
  55. }
  56. type AlertNotification struct {
  57. Id int64 `json:"id"`
  58. Uid string `json:"uid"`
  59. Name string `json:"name"`
  60. Type string `json:"type"`
  61. IsDefault bool `json:"isDefault"`
  62. SendReminder bool `json:"sendReminder"`
  63. DisableResolveMessage bool `json:"disableResolveMessage"`
  64. Frequency string `json:"frequency"`
  65. Created time.Time `json:"created"`
  66. Updated time.Time `json:"updated"`
  67. Settings *simplejson.Json `json:"settings"`
  68. }
  69. func NewAlertNotificationLookup(notification *models.AlertNotification) *AlertNotificationLookup {
  70. return &AlertNotificationLookup{
  71. Id: notification.Id,
  72. Uid: notification.Uid,
  73. Name: notification.Name,
  74. Type: notification.Type,
  75. IsDefault: notification.IsDefault,
  76. }
  77. }
  78. type AlertNotificationLookup struct {
  79. Id int64 `json:"id"`
  80. Uid string `json:"uid"`
  81. Name string `json:"name"`
  82. Type string `json:"type"`
  83. IsDefault bool `json:"isDefault"`
  84. }
  85. type AlertTestCommand struct {
  86. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  87. PanelId int64 `json:"panelId" binding:"Required"`
  88. }
  89. type AlertTestResult struct {
  90. Firing bool `json:"firing"`
  91. State models.AlertStateType `json:"state"`
  92. ConditionEvals string `json:"conditionEvals"`
  93. TimeMs string `json:"timeMs"`
  94. Error string `json:"error,omitempty"`
  95. EvalMatches []*EvalMatch `json:"matches,omitempty"`
  96. Logs []*AlertTestResultLog `json:"logs,omitempty"`
  97. }
  98. type AlertTestResultLog struct {
  99. Message string `json:"message"`
  100. Data interface{} `json:"data"`
  101. }
  102. type EvalMatch struct {
  103. Tags map[string]string `json:"tags,omitempty"`
  104. Metric string `json:"metric"`
  105. Value null.Float `json:"value"`
  106. }
  107. type NotificationTestCommand struct {
  108. Name string `json:"name"`
  109. Type string `json:"type"`
  110. SendReminder bool `json:"sendReminder"`
  111. DisableResolveMessage bool `json:"disableResolveMessage"`
  112. Frequency string `json:"frequency"`
  113. Settings *simplejson.Json `json:"settings"`
  114. }
  115. type PauseAlertCommand struct {
  116. AlertId int64 `json:"alertId"`
  117. Paused bool `json:"paused"`
  118. }
  119. type PauseAllAlertsCommand struct {
  120. Paused bool `json:"paused"`
  121. }