notification_settings.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package model
  2. import (
  3. "time"
  4. "github.com/quagmt/udecimal"
  5. "github.com/uptrace/bun"
  6. )
  7. // NotificationSettings 通知设置模型
  8. type NotificationSettings struct {
  9. bun.BaseModel `bun:"table:notification_settings,alias:ns"`
  10. ID int `bun:"id,pk,autoincrement" json:"id"`
  11. // 全局开关
  12. Enabled bool `bun:"enabled,notnull,default:false" json:"enabled"`
  13. // 兼容旧配置:默认使用 legacy 字段(单 URL / 自动识别),创建新目标后会切到新模式
  14. UseLegacyMode bool `bun:"use_legacy_mode,notnull,default:false" json:"useLegacyMode"`
  15. // 熔断器告警配置
  16. CircuitBreakerEnabled bool `bun:"circuit_breaker_enabled,notnull,default:false" json:"circuitBreakerEnabled"`
  17. CircuitBreakerWebhook *string `bun:"circuit_breaker_webhook" json:"circuitBreakerWebhook"`
  18. // 每日用户消费排行榜配置
  19. DailyLeaderboardEnabled bool `bun:"daily_leaderboard_enabled,notnull,default:false" json:"dailyLeaderboardEnabled"`
  20. DailyLeaderboardWebhook *string `bun:"daily_leaderboard_webhook" json:"dailyLeaderboardWebhook"`
  21. DailyLeaderboardTime string `bun:"daily_leaderboard_time,default:'09:00'" json:"dailyLeaderboardTime"` // HH:mm 格式
  22. DailyLeaderboardTopN *int `bun:"daily_leaderboard_top_n,default:5" json:"dailyLeaderboardTopN"` // 显示前 N 名
  23. // 成本预警配置
  24. CostAlertEnabled bool `bun:"cost_alert_enabled,notnull,default:false" json:"costAlertEnabled"`
  25. CostAlertWebhook *string `bun:"cost_alert_webhook" json:"costAlertWebhook"`
  26. CostAlertThreshold udecimal.Decimal `bun:"cost_alert_threshold,type:numeric(5,2),default:0.80" json:"costAlertThreshold"` // 阈值 0-1 (80% = 0.80)
  27. CostAlertCheckInterval *int `bun:"cost_alert_check_interval,default:60" json:"costAlertCheckInterval"` // 检查间隔(分钟)
  28. CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp" json:"createdAt"`
  29. UpdatedAt time.Time `bun:"updated_at,notnull,default:current_timestamp" json:"updatedAt"`
  30. }
  31. // GetDailyLeaderboardTopN 获取排行榜显示数量
  32. func (n *NotificationSettings) GetDailyLeaderboardTopN() int {
  33. if n.DailyLeaderboardTopN == nil {
  34. return 5
  35. }
  36. return *n.DailyLeaderboardTopN
  37. }
  38. // GetCostAlertCheckInterval 获取成本预警检查间隔(分钟)
  39. func (n *NotificationSettings) GetCostAlertCheckInterval() int {
  40. if n.CostAlertCheckInterval == nil {
  41. return 60
  42. }
  43. return *n.CostAlertCheckInterval
  44. }