alerting.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Settings: notification.Settings,
  52. }
  53. }
  54. type AlertNotification struct {
  55. Id int64 `json:"id"`
  56. Name string `json:"name"`
  57. Type string `json:"type"`
  58. IsDefault bool `json:"isDefault"`
  59. SendReminder bool `json:"sendReminder"`
  60. Frequency string `json:"frequency"`
  61. Created time.Time `json:"created"`
  62. Updated time.Time `json:"updated"`
  63. Settings *simplejson.Json `json:"settings"`
  64. }
  65. type AlertTestCommand struct {
  66. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  67. PanelId int64 `json:"panelId" binding:"Required"`
  68. }
  69. type AlertTestResult struct {
  70. Firing bool `json:"firing"`
  71. State models.AlertStateType `json:"state"`
  72. ConditionEvals string `json:"conditionEvals"`
  73. TimeMs string `json:"timeMs"`
  74. Error string `json:"error,omitempty"`
  75. EvalMatches []*EvalMatch `json:"matches,omitempty"`
  76. Logs []*AlertTestResultLog `json:"logs,omitempty"`
  77. }
  78. type AlertTestResultLog struct {
  79. Message string `json:"message"`
  80. Data interface{} `json:"data"`
  81. }
  82. type EvalMatch struct {
  83. Tags map[string]string `json:"tags,omitempty"`
  84. Metric string `json:"metric"`
  85. Value null.Float `json:"value"`
  86. }
  87. type NotificationTestCommand struct {
  88. Name string `json:"name"`
  89. Type string `json:"type"`
  90. SendReminder bool `json:"sendReminder"`
  91. Frequency string `json:"frequency"`
  92. Settings *simplejson.Json `json:"settings"`
  93. }
  94. type PauseAlertCommand struct {
  95. AlertId int64 `json:"alertId"`
  96. Paused bool `json:"paused"`
  97. }
  98. type PauseAllAlertsCommand struct {
  99. Paused bool `json:"paused"`
  100. }