From 845b748ffe1fa5fe32183afe8136e924b45212ff Mon Sep 17 00:00:00 2001 From: "Apple\\Apple" Date: Mon, 9 Jun 2025 19:03:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=A1=20feat:=20Add=20speed=20test=20fu?= =?UTF-8?q?nctionality=20to=20API=20info=20display?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add speed test tag with gauge icon for each API route - Integrate tcptest.cn service for API endpoint performance testing - Implement handleSpeedTest callback to open speed test in new tab - Add Tag component import from @douyinfe/semi-ui - Use Gauge icon with white circular tag styling - Position speed test tag before API route for better visibility - URL encoding handles special characters for proper test URL generation - Remove unused IconTestScoreStroked import and clean up comments The speed test feature allows users to quickly test API endpoint performance by clicking a small circular tag that opens the tcptest.cn speed testing service with the encoded API URL. --- web/src/i18n/locales/en.json | 3 ++- web/src/pages/Detail/index.js | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 16a10e3c..2c3c6330 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1585,5 +1585,6 @@ "请输入说明": "Please enter the description", "如:香港线路": "e.g. Hong Kong line", "请联系管理员在系统设置中配置API信息": "Please contact the administrator to configure API information in the system settings.", - "确定要删除此API信息吗?": "Are you sure you want to delete this API information?" + "确定要删除此API信息吗?": "Are you sure you want to delete this API information?", + "测速": "Speed Test" } \ No newline at end of file diff --git a/web/src/pages/Detail/index.js b/web/src/pages/Detail/index.js index b19201b9..e65b7f67 100644 --- a/web/src/pages/Detail/index.js +++ b/web/src/pages/Detail/index.js @@ -13,6 +13,7 @@ import { Tabs, TabPane, Empty, + Tag } from '@douyinfe/semi-ui'; import { IconRefresh, @@ -25,7 +26,7 @@ import { IconPulse, IconStopwatchStroked, IconTypograph, - IconPieChart2Stroked, + IconPieChart2Stroked } from '@douyinfe/semi-icons'; import { IllustrationConstruction, IllustrationConstructionDark } from '@douyinfe/semi-illustrations'; import { VChart } from '@visactor/react-vchart'; @@ -495,6 +496,12 @@ const Detail = (props) => { } }, [t]); + const handleSpeedTest = useCallback((apiUrl) => { + const encodedUrl = encodeURIComponent(apiUrl); + const speedTestUrl = `https://www.tcptest.cn/http/${encodedUrl}`; + window.open(speedTestUrl, '_blank'); + }, []); + const handleInputChange = useCallback((value, name) => { if (name === 'data_export_default_time') { setDataExportDefaultTime(value); @@ -698,22 +705,17 @@ const Detail = (props) => { }, [dataExportDefaultTime, getTimeInterval]); const updateChartData = useCallback((data) => { - // 处理原始数据 const processedData = processRawData(data); const { totalQuota, totalTimes, totalTokens, uniqueModels, timePoints, timeQuotaMap, timeTokensMap, timeCountMap } = processedData; - // 计算趋势数据 const trendDataResult = calculateTrendData(timePoints, timeQuotaMap, timeTokensMap, timeCountMap); setTrendData(trendDataResult); - // 生成模型颜色映射 const newModelColors = generateModelColors(uniqueModels); setModelColors(newModelColors); - // 聚合数据 const aggregatedData = aggregateDataByTimeAndModel(data); - // 生成饼图数据 const modelTotals = new Map(); for (let [_, value] of aggregatedData) { updateMapValue(modelTotals, value.model, value.count); @@ -724,7 +726,6 @@ const Detail = (props) => { value: count, })).sort((a, b) => b.value - a.value); - // 生成线图数据 const chartTimePoints = generateChartTimePoints(aggregatedData, data); let newLineData = []; @@ -748,7 +749,6 @@ const Detail = (props) => { newLineData.sort((a, b) => a.Time.localeCompare(b.Time)); - // 更新图表配置 updateChartSpec( setSpecPie, newPieData, @@ -765,7 +765,6 @@ const Detail = (props) => { 'barData' ); - // 更新状态 setPieData(newPieData); setLineData(newLineData); setConsumeQuota(totalQuota); @@ -994,7 +993,17 @@ const Detail = (props) => {
-
+
+ } + size="small" + color="white" + shape='circle' + onClick={() => handleSpeedTest(api.url)} + className="cursor-pointer hover:opacity-80 text-xs" + > + {t('测速')} + {api.route}
Date: Mon, 9 Jun 2025 19:14:34 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(setting):=20m?= =?UTF-8?q?ove=20API=20info=20functions=20to=20dedicated=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move validateApiInfo and getApiInfo functions from controller layer to setting/api_info.go to improve code organization and separation of concerns. Changes: - Create setting/api_info.go with ValidateApiInfo() and GetApiInfo() functions - Remove validateApiInfo function from controller/option.go - Remove getApiInfo function from controller/misc.go - Update function calls to use setting package - Clean up unused imports (net/url, regexp, fmt) in controller/option.go This refactoring aligns the API info configuration management with the existing pattern used by other setting modules (chat.go, group_ratio.go, rate_limit.go, etc.) and improves code reusability and maintainability. --- controller/misc.go | 23 +------ controller/option.go | 96 +------------------------- setting/api_info.go | 124 ++++++++++++++++++++++++++++++++++ web/src/pages/Detail/index.js | 2 +- 4 files changed, 127 insertions(+), 118 deletions(-) create mode 100644 setting/api_info.go diff --git a/controller/misc.go b/controller/misc.go index 622796f1..be76cab5 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -74,33 +74,12 @@ func GetStatus(c *gin.Context) { "oidc_client_id": system_setting.GetOIDCSettings().ClientId, "oidc_authorization_endpoint": system_setting.GetOIDCSettings().AuthorizationEndpoint, "setup": constant.Setup, - "api_info": getApiInfo(), + "api_info": setting.GetApiInfo(), }, }) return } -func getApiInfo() []map[string]interface{} { - // 从OptionMap中获取API信息,如果不存在则返回空数组 - common.OptionMapRWMutex.RLock() - apiInfoStr, exists := common.OptionMap["ApiInfo"] - common.OptionMapRWMutex.RUnlock() - - if !exists || apiInfoStr == "" { - // 如果没有配置,返回空数组 - return []map[string]interface{}{} - } - - // 解析存储的API信息 - var apiInfo []map[string]interface{} - if err := json.Unmarshal([]byte(apiInfoStr), &apiInfo); err != nil { - // 如果解析失败,返回空数组 - return []map[string]interface{}{} - } - - return apiInfo -} - func GetNotice(c *gin.Context) { common.OptionMapRWMutex.RLock() defer common.OptionMapRWMutex.RUnlock() diff --git a/controller/option.go b/controller/option.go index fac1fd86..f33b877a 100644 --- a/controller/option.go +++ b/controller/option.go @@ -2,110 +2,16 @@ package controller import ( "encoding/json" - "fmt" "net/http" - "net/url" "one-api/common" "one-api/model" "one-api/setting" "one-api/setting/system_setting" - "regexp" "strings" "github.com/gin-gonic/gin" ) -func validateApiInfo(apiInfoStr string) error { - if apiInfoStr == "" { - return nil // 空字符串是合法的 - } - - var apiInfoList []map[string]interface{} - if err := json.Unmarshal([]byte(apiInfoStr), &apiInfoList); err != nil { - return fmt.Errorf("API信息格式错误:%s", err.Error()) - } - - // 验证数组长度 - if len(apiInfoList) > 50 { - return fmt.Errorf("API信息数量不能超过50个") - } - - // 允许的颜色值 - validColors := map[string]bool{ - "blue": true, "green": true, "cyan": true, "purple": true, "pink": true, - "red": true, "orange": true, "amber": true, "yellow": true, "lime": true, - "light-green": true, "teal": true, "light-blue": true, "indigo": true, - "violet": true, "grey": true, - } - - // URL正则表达式 - urlRegex := regexp.MustCompile(`^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/.*)?$`) - - for i, apiInfo := range apiInfoList { - // 检查必填字段 - urlStr, ok := apiInfo["url"].(string) - if !ok || urlStr == "" { - return fmt.Errorf("第%d个API信息缺少URL字段", i+1) - } - - route, ok := apiInfo["route"].(string) - if !ok || route == "" { - return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1) - } - - description, ok := apiInfo["description"].(string) - if !ok || description == "" { - return fmt.Errorf("第%d个API信息缺少说明字段", i+1) - } - - color, ok := apiInfo["color"].(string) - if !ok || color == "" { - return fmt.Errorf("第%d个API信息缺少颜色字段", i+1) - } - - // 验证URL格式 - if !urlRegex.MatchString(urlStr) { - return fmt.Errorf("第%d个API信息的URL格式不正确", i+1) - } - - // 验证URL可解析性 - if _, err := url.Parse(urlStr); err != nil { - return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error()) - } - - // 验证字段长度 - if len(urlStr) > 500 { - return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1) - } - - if len(route) > 100 { - return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1) - } - - if len(description) > 200 { - return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1) - } - - // 验证颜色值 - if !validColors[color] { - return fmt.Errorf("第%d个API信息的颜色值不合法", i+1) - } - - // 检查并过滤危险字符(防止XSS) - dangerousChars := []string{" 50 { + return fmt.Errorf("API信息数量不能超过50个") + } + + // 允许的颜色值 + validColors := map[string]bool{ + "blue": true, "green": true, "cyan": true, "purple": true, "pink": true, + "red": true, "orange": true, "amber": true, "yellow": true, "lime": true, + "light-green": true, "teal": true, "light-blue": true, "indigo": true, + "violet": true, "grey": true, + } + + // URL正则表达式 + urlRegex := regexp.MustCompile(`^https?://[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*(/.*)?$`) + + for i, apiInfo := range apiInfoList { + // 检查必填字段 + urlStr, ok := apiInfo["url"].(string) + if !ok || urlStr == "" { + return fmt.Errorf("第%d个API信息缺少URL字段", i+1) + } + + route, ok := apiInfo["route"].(string) + if !ok || route == "" { + return fmt.Errorf("第%d个API信息缺少线路描述字段", i+1) + } + + description, ok := apiInfo["description"].(string) + if !ok || description == "" { + return fmt.Errorf("第%d个API信息缺少说明字段", i+1) + } + + color, ok := apiInfo["color"].(string) + if !ok || color == "" { + return fmt.Errorf("第%d个API信息缺少颜色字段", i+1) + } + + // 验证URL格式 + if !urlRegex.MatchString(urlStr) { + return fmt.Errorf("第%d个API信息的URL格式不正确", i+1) + } + + // 验证URL可解析性 + if _, err := url.Parse(urlStr); err != nil { + return fmt.Errorf("第%d个API信息的URL无法解析:%s", i+1, err.Error()) + } + + // 验证字段长度 + if len(urlStr) > 500 { + return fmt.Errorf("第%d个API信息的URL长度不能超过500字符", i+1) + } + + if len(route) > 100 { + return fmt.Errorf("第%d个API信息的线路描述长度不能超过100字符", i+1) + } + + if len(description) > 200 { + return fmt.Errorf("第%d个API信息的说明长度不能超过200字符", i+1) + } + + // 验证颜色值 + if !validColors[color] { + return fmt.Errorf("第%d个API信息的颜色值不合法", i+1) + } + + // 检查并过滤危险字符(防止XSS) + dangerousChars := []string{" {
{api.route.substring(0, 2)}