ratio_sync.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "one-api/model"
  6. "one-api/setting/ratio_setting"
  7. "one-api/dto"
  8. "sync"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type upstreamResult struct {
  13. Name string `json:"name"`
  14. Data map[string]any `json:"data,omitempty"`
  15. Err string `json:"err,omitempty"`
  16. }
  17. type TestResult struct {
  18. Name string `json:"name"`
  19. Status string `json:"status"`
  20. Error string `json:"error,omitempty"`
  21. }
  22. type DifferenceItem struct {
  23. Current interface{} `json:"current"` // 当前本地值,可能为null
  24. Upstreams map[string]interface{} `json:"upstreams"` // 上游值:具体值/"same"/null
  25. }
  26. // SyncableChannel 可同步的渠道信息
  27. type SyncableChannel struct {
  28. ID int `json:"id"`
  29. Name string `json:"name"`
  30. BaseURL string `json:"base_url"`
  31. Status int `json:"status"`
  32. }
  33. // FetchUpstreamRatios 后端并发拉取上游倍率
  34. func FetchUpstreamRatios(c *gin.Context) {
  35. var req dto.UpstreamRequest
  36. if err := c.ShouldBindJSON(&req); err != nil {
  37. c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()})
  38. return
  39. }
  40. if req.Timeout <= 0 {
  41. req.Timeout = 10
  42. }
  43. // build upstream list from ids + custom
  44. var upstreams []dto.UpstreamDTO
  45. if len(req.ChannelIDs) > 0 {
  46. // convert []int64 -> []int for model function
  47. intIds := make([]int, 0, len(req.ChannelIDs))
  48. for _, id64 := range req.ChannelIDs {
  49. intIds = append(intIds, int(id64))
  50. }
  51. dbChannels, _ := model.GetChannelsByIds(intIds)
  52. for _, ch := range dbChannels {
  53. upstreams = append(upstreams, dto.UpstreamDTO{
  54. Name: ch.Name,
  55. BaseURL: ch.GetBaseURL(),
  56. Endpoint: "", // assume default endpoint
  57. })
  58. }
  59. }
  60. upstreams = append(upstreams, req.CustomChannels...)
  61. var wg sync.WaitGroup
  62. ch := make(chan upstreamResult, len(upstreams))
  63. for _, chn := range upstreams {
  64. wg.Add(1)
  65. go func(chItem dto.UpstreamDTO) {
  66. defer wg.Done()
  67. endpoint := chItem.Endpoint
  68. if endpoint == "" {
  69. endpoint = "/api/ratio_config"
  70. }
  71. url := chItem.BaseURL + endpoint
  72. client := http.Client{Timeout: time.Duration(req.Timeout) * time.Second}
  73. resp, err := client.Get(url)
  74. if err != nil {
  75. ch <- upstreamResult{Name: chItem.Name, Err: err.Error()}
  76. return
  77. }
  78. defer resp.Body.Close()
  79. if resp.StatusCode != http.StatusOK {
  80. ch <- upstreamResult{Name: chItem.Name, Err: resp.Status}
  81. return
  82. }
  83. var body struct {
  84. Success bool `json:"success"`
  85. Data map[string]any `json:"data"`
  86. Message string `json:"message"`
  87. }
  88. if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
  89. ch <- upstreamResult{Name: chItem.Name, Err: err.Error()}
  90. return
  91. }
  92. if !body.Success {
  93. ch <- upstreamResult{Name: chItem.Name, Err: body.Message}
  94. return
  95. }
  96. ch <- upstreamResult{Name: chItem.Name, Data: body.Data}
  97. }(chn)
  98. }
  99. wg.Wait()
  100. close(ch)
  101. // 本地倍率配置
  102. localData := ratio_setting.GetExposedData()
  103. var testResults []dto.TestResult
  104. var successfulChannels []struct {
  105. name string
  106. data map[string]any
  107. }
  108. for r := range ch {
  109. if r.Err != "" {
  110. testResults = append(testResults, dto.TestResult{
  111. Name: r.Name,
  112. Status: "error",
  113. Error: r.Err,
  114. })
  115. } else {
  116. testResults = append(testResults, dto.TestResult{
  117. Name: r.Name,
  118. Status: "success",
  119. })
  120. successfulChannels = append(successfulChannels, struct {
  121. name string
  122. data map[string]any
  123. }{name: r.Name, data: r.Data})
  124. }
  125. }
  126. // 构建差异化数据
  127. differences := buildDifferences(localData, successfulChannels)
  128. c.JSON(http.StatusOK, gin.H{
  129. "success": true,
  130. "data": gin.H{
  131. "differences": differences,
  132. "test_results": testResults,
  133. },
  134. })
  135. }
  136. // buildDifferences 构建差异化数据,只返回有意义的差异
  137. func buildDifferences(localData map[string]any, successfulChannels []struct {
  138. name string
  139. data map[string]any
  140. }) map[string]map[string]dto.DifferenceItem {
  141. differences := make(map[string]map[string]dto.DifferenceItem)
  142. ratioTypes := []string{"model_ratio", "completion_ratio", "cache_ratio", "model_price"}
  143. // 收集所有模型名称
  144. allModels := make(map[string]struct{})
  145. // 从本地数据收集模型名称
  146. for _, ratioType := range ratioTypes {
  147. if localRatioAny, ok := localData[ratioType]; ok {
  148. if localRatio, ok := localRatioAny.(map[string]float64); ok {
  149. for modelName := range localRatio {
  150. allModels[modelName] = struct{}{}
  151. }
  152. }
  153. }
  154. }
  155. // 从上游数据收集模型名称
  156. for _, channel := range successfulChannels {
  157. for _, ratioType := range ratioTypes {
  158. if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
  159. for modelName := range upstreamRatio {
  160. allModels[modelName] = struct{}{}
  161. }
  162. }
  163. }
  164. }
  165. // 对每个模型和每个比率类型进行分析
  166. for modelName := range allModels {
  167. for _, ratioType := range ratioTypes {
  168. // 获取本地值
  169. var localValue interface{} = nil
  170. if localRatioAny, ok := localData[ratioType]; ok {
  171. if localRatio, ok := localRatioAny.(map[string]float64); ok {
  172. if val, exists := localRatio[modelName]; exists {
  173. localValue = val
  174. }
  175. }
  176. }
  177. // 收集上游值
  178. upstreamValues := make(map[string]interface{})
  179. hasUpstreamValue := false
  180. hasDifference := false
  181. for _, channel := range successfulChannels {
  182. var upstreamValue interface{} = nil
  183. if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok {
  184. if val, exists := upstreamRatio[modelName]; exists {
  185. upstreamValue = val
  186. hasUpstreamValue = true
  187. // 检查是否与本地值不同
  188. if localValue != nil && localValue != val {
  189. hasDifference = true
  190. } else if localValue == val {
  191. upstreamValue = "same"
  192. }
  193. }
  194. }
  195. // 如果本地值为空但上游有值,这也是差异
  196. if localValue == nil && upstreamValue != nil && upstreamValue != "same" {
  197. hasDifference = true
  198. }
  199. upstreamValues[channel.name] = upstreamValue
  200. }
  201. // 应用过滤逻辑
  202. shouldInclude := false
  203. if localValue != nil {
  204. // 规则1: 本地值存在,至少有一个上游与本地值不同
  205. if hasDifference {
  206. shouldInclude = true
  207. }
  208. // 规则2: 本地值存在,但所有上游都未设置 - 不包含
  209. } else {
  210. // 规则3: 本地值不存在,至少有一个上游设置了值
  211. if hasUpstreamValue {
  212. shouldInclude = true
  213. }
  214. }
  215. if shouldInclude {
  216. if differences[modelName] == nil {
  217. differences[modelName] = make(map[string]dto.DifferenceItem)
  218. }
  219. differences[modelName][ratioType] = dto.DifferenceItem{
  220. Current: localValue,
  221. Upstreams: upstreamValues,
  222. }
  223. }
  224. }
  225. }
  226. return differences
  227. }
  228. // GetSyncableChannels 获取可用于倍率同步的渠道(base_url 不为空的渠道)
  229. func GetSyncableChannels(c *gin.Context) {
  230. channels, err := model.GetAllChannels(0, 0, true, false)
  231. if err != nil {
  232. c.JSON(http.StatusOK, gin.H{
  233. "success": false,
  234. "message": err.Error(),
  235. })
  236. return
  237. }
  238. var syncableChannels []dto.SyncableChannel
  239. for _, channel := range channels {
  240. // 只返回 base_url 不为空的渠道
  241. if channel.GetBaseURL() != "" {
  242. syncableChannels = append(syncableChannels, dto.SyncableChannel{
  243. ID: channel.Id,
  244. Name: channel.Name,
  245. BaseURL: channel.GetBaseURL(),
  246. Status: channel.Status,
  247. })
  248. }
  249. }
  250. c.JSON(http.StatusOK, gin.H{
  251. "success": true,
  252. "message": "",
  253. "data": syncableChannels,
  254. })
  255. }