alert.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { del, get, patch, post } from './base'
  2. // Alert Rule Types
  3. export type MetricType = 'error_rate' | 'avg_response_time' | 'response_time_p95' | 'response_time_p99'
  4. export type OperatorType = '>' | '<' | '>=' | '<=' | '='
  5. export type AlertRule = {
  6. id: string
  7. app_id: string
  8. name: string
  9. description: string
  10. is_enabled: boolean
  11. metric_type: MetricType
  12. operator: OperatorType
  13. threshold_value: number
  14. check_interval_minutes: number
  15. consecutive_triggers: number
  16. notification_channels: {
  17. email?: string[]
  18. dingtalk?: string[]
  19. wecom?: string[]
  20. }
  21. created_by: string
  22. created_at: string
  23. updated_at: string
  24. }
  25. export type AlertLog = {
  26. id: string
  27. rule_id: string
  28. triggered_at: string
  29. metric_value: number
  30. threshold_value: number
  31. notification_status: 'pending' | 'sent' | 'failed' | 'partial'
  32. notification_details: string
  33. }
  34. export type NotificationConfig = {
  35. email: {
  36. enabled?: boolean
  37. smtp_server?: string
  38. smtp_port?: number
  39. smtp_user?: string
  40. smtp_password?: string
  41. smtp_from_email?: string
  42. smtp_from_name?: string
  43. recipients?: string[]
  44. }
  45. dingtalk: {
  46. enabled?: boolean
  47. webhooks?: string[]
  48. }
  49. wecom: {
  50. enabled?: boolean
  51. webhooks?: string[]
  52. }
  53. }
  54. // Get all alert rules for an app
  55. export const fetchAlertRules = (appId: string) => {
  56. return get<{ data: AlertRule[] }>(`/apps/${appId}/alert-rules`)
  57. }
  58. // Get a specific alert rule
  59. export const fetchAlertRule = (appId: string, ruleId: string) => {
  60. return get<AlertRule>(`/apps/${appId}/alert-rules/${ruleId}`)
  61. }
  62. // Create a new alert rule
  63. export const createAlertRule = (appId: string, data: Partial<AlertRule>) => {
  64. return post<AlertRule>(`/apps/${appId}/alert-rules`, { body: data })
  65. }
  66. // Update an alert rule
  67. export const updateAlertRule = (appId: string, ruleId: string, data: Partial<AlertRule>) => {
  68. return patch<AlertRule>(`/apps/${appId}/alert-rules/${ruleId}`, { body: data })
  69. }
  70. // Delete an alert rule
  71. export const deleteAlertRule = (appId: string, ruleId: string) => {
  72. return del<{ result: string }>(`/apps/${appId}/alert-rules/${ruleId}`)
  73. }
  74. // Get alert logs for a rule
  75. export const fetchAlertLogs = (appId: string, ruleId: string, limit = 100) => {
  76. return get<{ data: AlertLog[] }>(`/apps/${appId}/alert-rules/${ruleId}/logs`, { params: { limit } })
  77. }
  78. // Get notification channel configuration
  79. export const fetchNotificationConfig = (appId: string) => {
  80. return get<NotificationConfig>(`/apps/${appId}/alert-notification-config`)
  81. }
  82. // Save notification channel configuration
  83. export const saveNotificationConfig = (appId: string, config: NotificationConfig) => {
  84. return post<NotificationConfig>(`/apps/${appId}/alert-notification-config`, { body: config })
  85. }