channel_monitor_user_handler.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package handler
  2. import (
  3. "time"
  4. "github.com/Wei-Shaw/sub2api/internal/domain"
  5. "github.com/Wei-Shaw/sub2api/internal/handler/admin"
  6. "github.com/Wei-Shaw/sub2api/internal/handler/dto"
  7. "github.com/Wei-Shaw/sub2api/internal/pkg/response"
  8. "github.com/Wei-Shaw/sub2api/internal/service"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // ChannelMonitorUserHandler 渠道监控用户只读 handler。
  12. type ChannelMonitorUserHandler struct {
  13. monitorService *service.ChannelMonitorService
  14. settingService *service.SettingService
  15. }
  16. // NewChannelMonitorUserHandler 创建 handler。
  17. // settingService 用于每次请求前读取功能开关;关闭时 List/GetStatus 直接返回空/404。
  18. func NewChannelMonitorUserHandler(
  19. monitorService *service.ChannelMonitorService,
  20. settingService *service.SettingService,
  21. ) *ChannelMonitorUserHandler {
  22. return &ChannelMonitorUserHandler{
  23. monitorService: monitorService,
  24. settingService: settingService,
  25. }
  26. }
  27. // featureEnabled 返回当前渠道监控功能是否开启。
  28. // settingService 为 nil(测试场景)视为启用。
  29. func (h *ChannelMonitorUserHandler) featureEnabled(c *gin.Context) bool {
  30. if h.settingService == nil {
  31. return true
  32. }
  33. runtime := h.settingService.GetChannelMonitorRuntime(c.Request.Context())
  34. return runtime.Enabled && runtime.Mode == service.ChannelMonitorModeV1
  35. }
  36. // quotaVisible 返回用户端是否展示配额/余额快照(channel_monitor_show_quota,
  37. // fail-closed:未配置/非 "true" 一律视为关闭)。settingService 为 nil 时 fail-closed。
  38. func (h *ChannelMonitorUserHandler) quotaVisible(c *gin.Context) bool {
  39. if h.settingService == nil {
  40. return false
  41. }
  42. return h.settingService.GetChannelMonitorRuntime(c.Request.Context()).ShowQuota
  43. }
  44. // --- Response ---
  45. type channelMonitorUserListItem struct {
  46. ID int64 `json:"id"`
  47. Name string `json:"name"`
  48. Provider string `json:"provider"`
  49. GroupName string `json:"group_name"`
  50. PrimaryModel string `json:"primary_model"`
  51. PrimaryStatus string `json:"primary_status"`
  52. PrimaryLatencyMs *int `json:"primary_latency_ms"`
  53. PrimaryPingLatencyMs *int `json:"primary_ping_latency_ms"`
  54. Availability7d float64 `json:"availability_7d"`
  55. ExtraModels []dto.ChannelMonitorExtraModelStatus `json:"extra_models"`
  56. Timeline []channelMonitorUserTimelinePoint `json:"timeline"`
  57. // LatestQuota 主模型最近配额快照;channel_monitor_show_quota=false 时
  58. // 由 userMonitorViewToItem 的调用方传入 false 剥离(服务端脱敏,非仅前端隐藏)。
  59. LatestQuota *domain.MonitorQuotaSnapshot `json:"latest_quota,omitempty"`
  60. }
  61. // channelMonitorUserTimelinePoint 主模型最近一次检测的 timeline 点。
  62. // 仅用于用户视图 list 响应,admin 视图不使用。
  63. type channelMonitorUserTimelinePoint struct {
  64. Status string `json:"status"`
  65. LatencyMs *int `json:"latency_ms"`
  66. PingLatencyMs *int `json:"ping_latency_ms"`
  67. CheckedAt string `json:"checked_at"`
  68. }
  69. type channelMonitorUserDetailResponse struct {
  70. ID int64 `json:"id"`
  71. Name string `json:"name"`
  72. Provider string `json:"provider"`
  73. GroupName string `json:"group_name"`
  74. Models []channelMonitorUserModelStat `json:"models"`
  75. }
  76. type channelMonitorUserModelStat struct {
  77. Model string `json:"model"`
  78. LatestStatus string `json:"latest_status"`
  79. LatestLatencyMs *int `json:"latest_latency_ms"`
  80. Availability7d float64 `json:"availability_7d"`
  81. Availability15d float64 `json:"availability_15d"`
  82. Availability30d float64 `json:"availability_30d"`
  83. AvgLatency7dMs *int `json:"avg_latency_7d_ms"`
  84. }
  85. func userMonitorViewToItem(v *service.UserMonitorView, includeQuota bool) channelMonitorUserListItem {
  86. extras := make([]dto.ChannelMonitorExtraModelStatus, 0, len(v.ExtraModels))
  87. for _, e := range v.ExtraModels {
  88. extras = append(extras, dto.ChannelMonitorExtraModelStatus{
  89. Model: e.Model,
  90. Status: e.Status,
  91. LatencyMs: e.LatencyMs,
  92. })
  93. }
  94. timeline := make([]channelMonitorUserTimelinePoint, 0, len(v.Timeline))
  95. for _, p := range v.Timeline {
  96. timeline = append(timeline, channelMonitorUserTimelinePoint{
  97. Status: p.Status,
  98. LatencyMs: p.LatencyMs,
  99. PingLatencyMs: p.PingLatencyMs,
  100. CheckedAt: p.CheckedAt.UTC().Format(time.RFC3339),
  101. })
  102. }
  103. item := channelMonitorUserListItem{
  104. ID: v.ID,
  105. Name: v.Name,
  106. Provider: v.Provider,
  107. GroupName: v.GroupName,
  108. PrimaryModel: v.PrimaryModel,
  109. PrimaryStatus: v.PrimaryStatus,
  110. PrimaryLatencyMs: v.PrimaryLatencyMs,
  111. PrimaryPingLatencyMs: v.PrimaryPingLatencyMs,
  112. Availability7d: v.Availability7d,
  113. ExtraModels: extras,
  114. Timeline: timeline,
  115. }
  116. if includeQuota {
  117. item.LatestQuota = v.LatestQuota
  118. }
  119. return item
  120. }
  121. func userMonitorDetailToResponse(d *service.UserMonitorDetail) *channelMonitorUserDetailResponse {
  122. models := make([]channelMonitorUserModelStat, 0, len(d.Models))
  123. for _, m := range d.Models {
  124. models = append(models, channelMonitorUserModelStat{
  125. Model: m.Model,
  126. LatestStatus: m.LatestStatus,
  127. LatestLatencyMs: m.LatestLatencyMs,
  128. Availability7d: m.Availability7d,
  129. Availability15d: m.Availability15d,
  130. Availability30d: m.Availability30d,
  131. AvgLatency7dMs: m.AvgLatency7dMs,
  132. })
  133. }
  134. return &channelMonitorUserDetailResponse{
  135. ID: d.ID,
  136. Name: d.Name,
  137. Provider: d.Provider,
  138. GroupName: d.GroupName,
  139. Models: models,
  140. }
  141. }
  142. // --- Handlers ---
  143. // List GET /api/v1/channel-monitors
  144. func (h *ChannelMonitorUserHandler) List(c *gin.Context) {
  145. if !h.featureEnabled(c) {
  146. response.Success(c, gin.H{"items": []channelMonitorUserListItem{}})
  147. return
  148. }
  149. views, err := h.monitorService.ListUserView(c.Request.Context())
  150. if err != nil {
  151. response.ErrorFrom(c, err)
  152. return
  153. }
  154. includeQuota := h.quotaVisible(c)
  155. items := make([]channelMonitorUserListItem, 0, len(views))
  156. for _, v := range views {
  157. items = append(items, userMonitorViewToItem(v, includeQuota))
  158. }
  159. response.Success(c, gin.H{"items": items})
  160. }
  161. // GetStatus GET /api/v1/channel-monitors/:id/status
  162. func (h *ChannelMonitorUserHandler) GetStatus(c *gin.Context) {
  163. if !h.featureEnabled(c) {
  164. response.ErrorFrom(c, service.ErrChannelMonitorNotFound)
  165. return
  166. }
  167. // 复用 admin.ParseChannelMonitorID 保持错误码与日志一致。
  168. id, ok := admin.ParseChannelMonitorID(c)
  169. if !ok {
  170. return
  171. }
  172. detail, err := h.monitorService.GetUserDetail(c.Request.Context(), id)
  173. if err != nil {
  174. response.ErrorFrom(c, err)
  175. return
  176. }
  177. response.Success(c, userMonitorDetailToResponse(detail))
  178. }