alerting.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. type AlertTestCommand struct {
  70. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  71. PanelId int64 `json:"panelId" binding:"Required"`
  72. }
  73. type AlertTestResult struct {
  74. Firing bool `json:"firing"`
  75. State models.AlertStateType `json:"state"`
  76. ConditionEvals string `json:"conditionEvals"`
  77. TimeMs string `json:"timeMs"`
  78. Error string `json:"error,omitempty"`
  79. EvalMatches []*EvalMatch `json:"matches,omitempty"`
  80. Logs []*AlertTestResultLog `json:"logs,omitempty"`
  81. }
  82. type AlertTestResultLog struct {
  83. Message string `json:"message"`
  84. Data interface{} `json:"data"`
  85. }
  86. type EvalMatch struct {
  87. Tags map[string]string `json:"tags,omitempty"`
  88. Metric string `json:"metric"`
  89. Value null.Float `json:"value"`
  90. }
  91. type NotificationTestCommand struct {
  92. Name string `json:"name"`
  93. Type string `json:"type"`
  94. SendReminder bool `json:"sendReminder"`
  95. DisableResolveMessage bool `json:"disableResolveMessage"`
  96. Frequency string `json:"frequency"`
  97. Settings *simplejson.Json `json:"settings"`
  98. }
  99. type PauseAlertCommand struct {
  100. AlertId int64 `json:"alertId"`
  101. Paused bool `json:"paused"`
  102. }
  103. type PauseAllAlertsCommand struct {
  104. Paused bool `json:"paused"`
  105. }