From a7a49be850be15f23ac597c9a21efad68cf65b8f Mon Sep 17 00:00:00 2001 From: IanShaw027 <131567472+IanShaw027@users.noreply.github.com> Date: Wed, 14 Jan 2026 23:47:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ops):=20=E4=BD=BF=E7=94=A8TTFT?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3Duration=E4=BD=9C=E4=B8=BA=E5=81=A5=E5=BA=B7?= =?UTF-8?q?=E5=88=86=E6=95=B0=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 业务健康分数:错误率 50% + TTFT 50% - TTFT 阈值:1s → 100分,3s → 0分 - TTFT 对 AI 服务的用户体验更有意义 - 更新所有相关测试用例期望值 --- backend/internal/service/ops_health_score.go | 22 +++++++++---------- .../internal/service/ops_health_score_test.go | 16 +++++++------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/backend/internal/service/ops_health_score.go b/backend/internal/service/ops_health_score.go index 0ff1d8cb..5efae870 100644 --- a/backend/internal/service/ops_health_score.go +++ b/backend/internal/service/ops_health_score.go @@ -32,7 +32,7 @@ func computeDashboardHealthScore(now time.Time, overview *OpsDashboardOverview) } // computeBusinessHealth calculates business health score (0-100) -// Components: Error Rate (50%) + Latency (50%) +// Components: Error Rate (50%) + TTFT (50%) func computeBusinessHealth(overview *OpsDashboardOverview) float64 { // Error rate score: 1% → 100, 10% → 0 (linear) // Combines request errors and upstream errors @@ -48,22 +48,22 @@ func computeBusinessHealth(overview *OpsDashboardOverview) float64 { } } - // Latency score: 1s → 100, 2s → 0 (linear) - // Uses P99 of duration - latencyScore := 100.0 - if overview.Duration.P99 != nil { - p99 := float64(*overview.Duration.P99) + // TTFT score: 1s → 100, 3s → 0 (linear) + // Time to first token is critical for user experience + ttftScore := 100.0 + if overview.TTFT.P99 != nil { + p99 := float64(*overview.TTFT.P99) if p99 > 1000 { - if p99 <= 2000 { - latencyScore = (2000 - p99) / 1000 * 100 + if p99 <= 3000 { + ttftScore = (3000 - p99) / 2000 * 100 } else { - latencyScore = 0 + ttftScore = 0 } } } - // Weighted combination: 50% error rate + 50% latency - return errorScore*0.5 + latencyScore*0.5 + // Weighted combination: 50% error rate + 50% TTFT + return errorScore*0.5 + ttftScore*0.5 } // computeInfraHealth calculates infrastructure health score (0-100) diff --git a/backend/internal/service/ops_health_score_test.go b/backend/internal/service/ops_health_score_test.go index 791238ac..25bfb43d 100644 --- a/backend/internal/service/ops_health_score_test.go +++ b/backend/internal/service/ops_health_score_test.go @@ -127,8 +127,8 @@ func TestComputeDashboardHealthScore_Comprehensive(t *testing.T) { MemoryUsagePercent: float64Ptr(75), }, }, - wantMin: 61, - wantMax: 62, + wantMin: 96, + wantMax: 97, }, { name: "DB failure", @@ -203,8 +203,8 @@ func TestComputeDashboardHealthScore_Comprehensive(t *testing.T) { MemoryUsagePercent: float64Ptr(30), }, }, - wantMin: 25, - wantMax: 50, + wantMin: 84, + wantMax: 85, }, { name: "combined failures - business healthy + infra degraded", @@ -303,15 +303,15 @@ func TestComputeBusinessHealth(t *testing.T) { wantMax: 78, }, { - name: "latency boundary 2s", + name: "TTFT boundary 2s", overview: &OpsDashboardOverview{ SLA: 0.99, ErrorRate: 0, UpstreamErrorRate: 0, - Duration: OpsPercentiles{P99: intPtr(2000)}, + TTFT: OpsPercentiles{P99: intPtr(2000)}, }, - wantMin: 50, - wantMax: 50, + wantMin: 75, + wantMax: 75, }, { name: "upstream error dominates",