From ce3336e3f4604431716bef29a013048d985241bf Mon Sep 17 00:00:00 2001 From: IanShaw027 <131567472+IanShaw027@users.noreply.github.com> Date: Sun, 11 Jan 2026 23:40:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=92=8C=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 ops_ws_handler.go 中的代码格式和返回值 - 移除 ops_repo_latency_histogram_buckets.go 中不必要的错误检查 - 修复 api_contract_test.go 缩进并添加运维监控配置项测试 - 移除 ops_cleanup_service.go 中未使用的变量 - 添加 ops_retry.go 中缺失的 status 字段 --- .../internal/handler/admin/ops_ws_handler.go | 5 ++- .../ops_repo_latency_histogram_buckets.go | 6 +-- backend/internal/server/api_contract_test.go | 39 ++++++++++++------- .../internal/service/ops_cleanup_service.go | 27 +++++++------ backend/internal/service/ops_retry.go | 1 + 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/backend/internal/handler/admin/ops_ws_handler.go b/backend/internal/handler/admin/ops_ws_handler.go index 54a271c1..34081e8d 100644 --- a/backend/internal/handler/admin/ops_ws_handler.go +++ b/backend/internal/handler/admin/ops_ws_handler.go @@ -391,7 +391,10 @@ func tryAcquireOpsWSIPSlot(clientIP string, limit int32) bool { } v, _ := wsConnCountByIP.LoadOrStore(clientIP, &atomic.Int32{}) - counter, ok := v.(*atomic.Int32); if !ok { return } + counter, ok := v.(*atomic.Int32) + if !ok { + return false + } for { current := counter.Load() diff --git a/backend/internal/repository/ops_repo_latency_histogram_buckets.go b/backend/internal/repository/ops_repo_latency_histogram_buckets.go index 618957e9..fc085fc6 100644 --- a/backend/internal/repository/ops_repo_latency_histogram_buckets.go +++ b/backend/internal/repository/ops_repo_latency_histogram_buckets.go @@ -29,18 +29,18 @@ var latencyHistogramOrderedRanges = func() []string { func latencyHistogramRangeCaseExpr(column string) string { var sb strings.Builder - _ = sb.WriteString("CASE\n") + sb.WriteString("CASE\n") for _, b := range latencyHistogramBuckets { if b.upperMs <= 0 { continue } - _ = sb.WriteString(fmt.Sprintf("\tWHEN %s < %d THEN '%s'\n", column, b.upperMs, b.label)) + sb.WriteString(fmt.Sprintf("\tWHEN %s < %d THEN '%s'\n", column, b.upperMs, b.label)) } // Default bucket. last := latencyHistogramBuckets[len(latencyHistogramBuckets)-1] - _ = sb.WriteString(fmt.Sprintf("\tELSE '%s'\n", last.label)) + sb.WriteString(fmt.Sprintf("\tELSE '%s'\n", last.label)) sb.WriteString("END") return sb.String() } diff --git a/backend/internal/server/api_contract_test.go b/backend/internal/server/api_contract_test.go index 6d8f67e3..d96732bd 100644 --- a/backend/internal/server/api_contract_test.go +++ b/backend/internal/server/api_contract_test.go @@ -262,11 +262,11 @@ func TestAPIContracts(t *testing.T) { name: "GET /api/v1/admin/settings", setup: func(t *testing.T, deps *contractDeps) { t.Helper() - deps.settingRepo.SetAll(map[string]string{ - service.SettingKeyRegistrationEnabled: "true", - service.SettingKeyEmailVerifyEnabled: "false", + deps.settingRepo.SetAll(map[string]string{ + service.SettingKeyRegistrationEnabled: "true", + service.SettingKeyEmailVerifyEnabled: "false", - service.SettingKeySMTPHost: "smtp.example.com", + service.SettingKeySMTPHost: "smtp.example.com", service.SettingKeySMTPPort: "587", service.SettingKeySMTPUsername: "user", service.SettingKeySMTPPassword: "secret", @@ -285,10 +285,15 @@ func TestAPIContracts(t *testing.T) { service.SettingKeyContactInfo: "support", service.SettingKeyDocURL: "https://docs.example.com", - service.SettingKeyDefaultConcurrency: "5", - service.SettingKeyDefaultBalance: "1.25", - }) - }, + service.SettingKeyDefaultConcurrency: "5", + service.SettingKeyDefaultBalance: "1.25", + + service.SettingKeyOpsMonitoringEnabled: "false", + service.SettingKeyOpsRealtimeMonitoringEnabled: "true", + service.SettingKeyOpsQueryModeDefault: "auto", + service.SettingKeyOpsMetricsIntervalSeconds: "60", + }) + }, method: http.MethodGet, path: "/api/v1/admin/settings", wantStatus: http.StatusOK, @@ -309,13 +314,17 @@ func TestAPIContracts(t *testing.T) { "turnstile_site_key": "site-key", "turnstile_secret_key_configured": true, "linuxdo_connect_enabled": false, - "linuxdo_connect_client_id": "", - "linuxdo_connect_client_secret_configured": false, - "linuxdo_connect_redirect_url": "", - "site_name": "Sub2API", - "site_logo": "", - "site_subtitle": "Subtitle", - "api_base_url": "https://api.example.com", + "linuxdo_connect_client_id": "", + "linuxdo_connect_client_secret_configured": false, + "linuxdo_connect_redirect_url": "", + "ops_monitoring_enabled": false, + "ops_realtime_monitoring_enabled": true, + "ops_query_mode_default": "auto", + "ops_metrics_interval_seconds": 60, + "site_name": "Sub2API", + "site_logo": "", + "site_subtitle": "Subtitle", + "api_base_url": "https://api.example.com", "contact_info": "support", "doc_url": "https://docs.example.com", "default_concurrency": 5, diff --git a/backend/internal/service/ops_cleanup_service.go b/backend/internal/service/ops_cleanup_service.go index 2e770f7d..c46ceaa9 100644 --- a/backend/internal/service/ops_cleanup_service.go +++ b/backend/internal/service/ops_cleanup_service.go @@ -94,20 +94,19 @@ func (s *OpsCleanupService) Start() { if parsed, err := time.LoadLocation(strings.TrimSpace(s.cfg.Timezone)); err == nil && parsed != nil { loc = parsed } - } - - c := cron.New(cron.WithParser(opsCleanupCronParser), cron.WithLocation(loc)) - id, err := c.AddFunc(schedule, func() { s.runScheduled() }) - if err != nil { - log.Printf("[OpsCleanup] not started (invalid schedule=%q): %v", schedule, err) - return - } - s.cron = c - s.entryID = id - s.cron.Start() - log.Printf("[OpsCleanup] started (schedule=%q tz=%s)", schedule, loc.String()) - }) -} + } + + c := cron.New(cron.WithParser(opsCleanupCronParser), cron.WithLocation(loc)) + _, err := c.AddFunc(schedule, func() { s.runScheduled() }) + if err != nil { + log.Printf("[OpsCleanup] not started (invalid schedule=%q): %v", schedule, err) + return + } + s.cron = c + s.cron.Start() + log.Printf("[OpsCleanup] started (schedule=%q tz=%s)", schedule, loc.String()) + }) + } func (s *OpsCleanupService) Stop() { if s == nil { diff --git a/backend/internal/service/ops_retry.go b/backend/internal/service/ops_retry.go index 5d0ed34e..bf6213ae 100644 --- a/backend/internal/service/ops_retry.go +++ b/backend/internal/service/ops_retry.go @@ -52,6 +52,7 @@ const ( type limitedResponseWriter struct { header http.Header wroteHeader bool + status int limit int totalWritten int64