-
Notifications
You must be signed in to change notification settings - Fork 11.2k
🔄 feat(ratio-sync): introduce upstream ratio synchronisation feature #1220 #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Calcium-Ion
merged 6 commits into
QuantumNous:alpha
from
t0ng7u:feature/upstream-ratio-sync
Jun 20, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7975cdf
🚀 feat(ratio-sync): major refactor & UX overhaul for Upstream Ratio Sync
zeraturing fb4ff63
🗑️ chore(custom channel): Remove custom channel support from upstream…
zeraturing 67546f4
✨ chore(ui): enhance channel selector with status avatars and UI impr…
zeraturing a9f98c5
🛠️ chore(ratio-sync): improve upstream ratio comparison & output clea…
zeraturing 458472f
🔍 feat(ratio-sync): add fuzzy model search & enhance empty-state UX
zeraturing 150c506
🚀 chore(controller, dto): elevate ratio-sync feature to production re…
zeraturing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "one-api/setting/ratio_setting" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| func GetRatioConfig(c *gin.Context) { | ||
| if !ratio_setting.IsExposeRatioEnabled() { | ||
| c.JSON(http.StatusForbidden, gin.H{ | ||
| "success": false, | ||
| "message": "倍率配置接口未启用", | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "", | ||
| "data": ratio_setting.GetExposedData(), | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,322 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "one-api/common" | ||
| "one-api/dto" | ||
| "one-api/model" | ||
| "one-api/setting/ratio_setting" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultTimeoutSeconds = 10 | ||
| defaultEndpoint = "/api/ratio_config" | ||
| maxConcurrentFetches = 8 | ||
| ) | ||
|
|
||
| var ratioTypes = []string{"model_ratio", "completion_ratio", "cache_ratio", "model_price"} | ||
|
|
||
| type upstreamResult struct { | ||
| Name string `json:"name"` | ||
| Data map[string]any `json:"data,omitempty"` | ||
| Err string `json:"err,omitempty"` | ||
| } | ||
|
|
||
| func FetchUpstreamRatios(c *gin.Context) { | ||
| var req dto.UpstreamRequest | ||
| if err := c.ShouldBindJSON(&req); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": err.Error()}) | ||
| return | ||
| } | ||
|
|
||
| if req.Timeout <= 0 { | ||
| req.Timeout = defaultTimeoutSeconds | ||
| } | ||
|
|
||
| var upstreams []dto.UpstreamDTO | ||
|
|
||
| if len(req.ChannelIDs) > 0 { | ||
| intIds := make([]int, 0, len(req.ChannelIDs)) | ||
| for _, id64 := range req.ChannelIDs { | ||
| intIds = append(intIds, int(id64)) | ||
| } | ||
| dbChannels, err := model.GetChannelsByIds(intIds) | ||
| if err != nil { | ||
| common.LogError(c.Request.Context(), "failed to query channels: "+err.Error()) | ||
| c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "查询渠道失败"}) | ||
| return | ||
| } | ||
| for _, ch := range dbChannels { | ||
| if base := ch.GetBaseURL(); strings.HasPrefix(base, "http") { | ||
| upstreams = append(upstreams, dto.UpstreamDTO{ | ||
| Name: ch.Name, | ||
| BaseURL: strings.TrimRight(base, "/"), | ||
| Endpoint: "", | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(upstreams) == 0 { | ||
| c.JSON(http.StatusOK, gin.H{"success": false, "message": "无有效上游渠道"}) | ||
| return | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| ch := make(chan upstreamResult, len(upstreams)) | ||
|
|
||
| sem := make(chan struct{}, maxConcurrentFetches) | ||
|
|
||
| client := &http.Client{Transport: &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second}} | ||
|
|
||
| for _, chn := range upstreams { | ||
| wg.Add(1) | ||
| go func(chItem dto.UpstreamDTO) { | ||
| defer wg.Done() | ||
|
|
||
| sem <- struct{}{} | ||
| defer func() { <-sem }() | ||
|
|
||
| endpoint := chItem.Endpoint | ||
| if endpoint == "" { | ||
| endpoint = defaultEndpoint | ||
| } else if !strings.HasPrefix(endpoint, "/") { | ||
| endpoint = "/" + endpoint | ||
| } | ||
| fullURL := chItem.BaseURL + endpoint | ||
|
|
||
| ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(req.Timeout)*time.Second) | ||
| defer cancel() | ||
|
|
||
| httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil) | ||
| if err != nil { | ||
| common.LogWarn(c.Request.Context(), "build request failed: "+err.Error()) | ||
| ch <- upstreamResult{Name: chItem.Name, Err: err.Error()} | ||
| return | ||
| } | ||
|
|
||
| resp, err := client.Do(httpReq) | ||
| if err != nil { | ||
| common.LogWarn(c.Request.Context(), "http error on "+chItem.Name+": "+err.Error()) | ||
| ch <- upstreamResult{Name: chItem.Name, Err: err.Error()} | ||
| return | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| common.LogWarn(c.Request.Context(), "non-200 from "+chItem.Name+": "+resp.Status) | ||
| ch <- upstreamResult{Name: chItem.Name, Err: resp.Status} | ||
| return | ||
| } | ||
| var body struct { | ||
| Success bool `json:"success"` | ||
| Data map[string]any `json:"data"` | ||
| Message string `json:"message"` | ||
| } | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| common.LogWarn(c.Request.Context(), "json decode failed from "+chItem.Name+": "+err.Error()) | ||
| ch <- upstreamResult{Name: chItem.Name, Err: err.Error()} | ||
| return | ||
| } | ||
| if !body.Success { | ||
| ch <- upstreamResult{Name: chItem.Name, Err: body.Message} | ||
| return | ||
| } | ||
| ch <- upstreamResult{Name: chItem.Name, Data: body.Data} | ||
| }(chn) | ||
| } | ||
|
|
||
| wg.Wait() | ||
| close(ch) | ||
|
|
||
| localData := ratio_setting.GetExposedData() | ||
|
|
||
| var testResults []dto.TestResult | ||
| var successfulChannels []struct { | ||
| name string | ||
| data map[string]any | ||
| } | ||
|
|
||
| for r := range ch { | ||
| if r.Err != "" { | ||
| testResults = append(testResults, dto.TestResult{ | ||
| Name: r.Name, | ||
| Status: "error", | ||
| Error: r.Err, | ||
| }) | ||
| } else { | ||
| testResults = append(testResults, dto.TestResult{ | ||
| Name: r.Name, | ||
| Status: "success", | ||
| }) | ||
| successfulChannels = append(successfulChannels, struct { | ||
| name string | ||
| data map[string]any | ||
| }{name: r.Name, data: r.Data}) | ||
| } | ||
| } | ||
|
|
||
| differences := buildDifferences(localData, successfulChannels) | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "data": gin.H{ | ||
| "differences": differences, | ||
| "test_results": testResults, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func buildDifferences(localData map[string]any, successfulChannels []struct { | ||
| name string | ||
| data map[string]any | ||
| }) map[string]map[string]dto.DifferenceItem { | ||
| differences := make(map[string]map[string]dto.DifferenceItem) | ||
|
|
||
| allModels := make(map[string]struct{}) | ||
|
|
||
| for _, ratioType := range ratioTypes { | ||
| if localRatioAny, ok := localData[ratioType]; ok { | ||
| if localRatio, ok := localRatioAny.(map[string]float64); ok { | ||
| for modelName := range localRatio { | ||
| allModels[modelName] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, channel := range successfulChannels { | ||
| for _, ratioType := range ratioTypes { | ||
| if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok { | ||
| for modelName := range upstreamRatio { | ||
| allModels[modelName] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for modelName := range allModels { | ||
| for _, ratioType := range ratioTypes { | ||
| var localValue interface{} = nil | ||
| if localRatioAny, ok := localData[ratioType]; ok { | ||
| if localRatio, ok := localRatioAny.(map[string]float64); ok { | ||
| if val, exists := localRatio[modelName]; exists { | ||
| localValue = val | ||
| } | ||
| } | ||
| } | ||
|
|
||
| upstreamValues := make(map[string]interface{}) | ||
| hasUpstreamValue := false | ||
| hasDifference := false | ||
|
|
||
| for _, channel := range successfulChannels { | ||
| var upstreamValue interface{} = nil | ||
|
|
||
| if upstreamRatio, ok := channel.data[ratioType].(map[string]any); ok { | ||
| if val, exists := upstreamRatio[modelName]; exists { | ||
| upstreamValue = val | ||
| hasUpstreamValue = true | ||
|
|
||
| if localValue != nil && localValue != val { | ||
| hasDifference = true | ||
| } else if localValue == val { | ||
| upstreamValue = "same" | ||
| } | ||
| } | ||
| } | ||
| if upstreamValue == nil && localValue == nil { | ||
| upstreamValue = "same" | ||
| } | ||
|
|
||
| if localValue == nil && upstreamValue != nil && upstreamValue != "same" { | ||
| hasDifference = true | ||
| } | ||
|
|
||
| upstreamValues[channel.name] = upstreamValue | ||
| } | ||
|
|
||
| shouldInclude := false | ||
|
|
||
| if localValue != nil { | ||
| if hasDifference { | ||
| shouldInclude = true | ||
| } | ||
| } else { | ||
| if hasUpstreamValue { | ||
| shouldInclude = true | ||
| } | ||
| } | ||
|
|
||
| if shouldInclude { | ||
| if differences[modelName] == nil { | ||
| differences[modelName] = make(map[string]dto.DifferenceItem) | ||
| } | ||
| differences[modelName][ratioType] = dto.DifferenceItem{ | ||
| Current: localValue, | ||
| Upstreams: upstreamValues, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| channelHasDiff := make(map[string]bool) | ||
| for _, ratioMap := range differences { | ||
| for _, item := range ratioMap { | ||
| for chName, val := range item.Upstreams { | ||
| if val != nil && val != "same" { | ||
| channelHasDiff[chName] = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for modelName, ratioMap := range differences { | ||
| for ratioType, item := range ratioMap { | ||
| for chName := range item.Upstreams { | ||
| if !channelHasDiff[chName] { | ||
| delete(item.Upstreams, chName) | ||
| } | ||
| } | ||
| differences[modelName][ratioType] = item | ||
| } | ||
| } | ||
|
|
||
| return differences | ||
| } | ||
|
|
||
| func GetSyncableChannels(c *gin.Context) { | ||
| channels, err := model.GetAllChannels(0, 0, true, false) | ||
| if err != nil { | ||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": false, | ||
| "message": err.Error(), | ||
| }) | ||
| return | ||
| } | ||
|
|
||
| var syncableChannels []dto.SyncableChannel | ||
| for _, channel := range channels { | ||
| if channel.GetBaseURL() != "" { | ||
| syncableChannels = append(syncableChannels, dto.SyncableChannel{ | ||
| ID: channel.Id, | ||
| Name: channel.Name, | ||
| BaseURL: channel.GetBaseURL(), | ||
| Status: channel.Status, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "", | ||
| "data": syncableChannels, | ||
| }) | ||
| } | ||
|
Comment on lines
+295
to
+322
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add input validation and improve error handling. The func GetSyncableChannels(c *gin.Context) {
channels, err := model.GetAllChannels(0, 0, true, false)
if err != nil {
- c.JSON(http.StatusOK, gin.H{
+ c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": err.Error(),
})
return
}
var syncableChannels []dto.SyncableChannel
for _, channel := range channels {
- if channel.GetBaseURL() != "" {
+ baseURL := channel.GetBaseURL()
+ if baseURL != "" && isValidURL(baseURL) { // Reuse the validation function
syncableChannels = append(syncableChannels, dto.SyncableChannel{
ID: channel.Id,
Name: channel.Name,
- BaseURL: channel.GetBaseURL(),
+ BaseURL: baseURL,
Status: channel.Status,
})
}
}
c.JSON(http.StatusOK, gin.H{
"success": true,
- "message": "",
+ "message": "Successfully retrieved syncable channels",
"data": syncableChannels,
})
}🤖 Prompt for AI Agents |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Fix potential type comparison issue.
The comparison
localValue != valmay not work correctly for floating-point numbers due to precision issues, and the type assertion could fail silently.🤖 Prompt for AI Agents