fix(providers): correct endpoint health aggregation and display

This commit is contained in:
elky
2026-09-06 18:24:27 +08:00
parent dba5e6e9e9
commit d1b5eb08ee
9 changed files with 425 additions and 38 deletions
@@ -1,6 +1,63 @@
import { describe, expect, it } from 'vitest'
import { getEndpointTooltip } from '../useEndpointStatus'
import type { EndpointHealthDetail } from '@/api/endpoints'
import {
getEndpointDotColor,
getEndpointHealthLabel,
getEndpointHealthBarWidth,
getEndpointTooltip,
} from '../useEndpointStatus'
const availableEndpoint: EndpointHealthDetail = {
api_format: 'openai:chat',
health_score: 1,
is_active: true,
total_keys: 1,
active_keys: 1,
}
describe('endpoint health display', () => {
it.each([null, undefined, Number.NaN, Number.POSITIVE_INFINITY])(
'renders missing or invalid health %s as unknown rather than a percentage',
(healthScore) => {
const endpoint = { ...availableEndpoint, health_score: healthScore } as EndpointHealthDetail
expect(getEndpointHealthLabel(endpoint)).toBe('-')
expect(getEndpointHealthBarWidth(endpoint)).toBe('100%')
expect(getEndpointDotColor(endpoint)).toBe('bg-muted-foreground/40')
expect(getEndpointTooltip(endpoint, 'zh-CN')).toContain('暂无健康数据')
},
)
it.each([
{ is_active: false },
{ active_keys: 0 },
{ active_keys: 0, total_keys: 0 },
])('does not show a health percentage for unavailable endpoint %s', (overrides) => {
const endpoint = { ...availableEndpoint, ...overrides }
expect(getEndpointHealthLabel(endpoint)).toBe('-')
expect(getEndpointHealthBarWidth(endpoint)).toBe('100%')
expect(getEndpointDotColor(endpoint)).toBe('bg-muted-foreground/40')
})
it.each([
[0, '0%', '5%', 'bg-red-500'],
[0.2, '20%', '20%', 'bg-red-500'],
[0.5, '50%', '50%', 'bg-amber-500'],
[0.8, '80%', '80%', 'bg-green-500'],
[1, '100%', '100%', 'bg-green-500'],
[-0.2, '0%', '5%', 'bg-red-500'],
[1.2, '100%', '100%', 'bg-green-500'],
] as const)('renders score %s consistently across label, bar and tooltip', (score, label, width, color) => {
const endpoint = { ...availableEndpoint, health_score: score }
expect(getEndpointHealthLabel(endpoint)).toBe(label)
expect(getEndpointHealthBarWidth(endpoint)).toBe(width)
expect(getEndpointDotColor(endpoint)).toBe(color)
expect(getEndpointTooltip(endpoint, 'zh-CN')).toContain(`健康度 ${label}`)
})
})
describe('getEndpointTooltip', () => {
it('formats the internal Codex Live id at the user-visible tooltip boundary', () => {
@@ -32,11 +32,29 @@ export function isEndpointAvailable(endpoint: EndpointHealthDetail): boolean {
return getEndpointStatus(endpoint) === 'available'
}
function getEndpointHealthScore(endpoint: EndpointHealthDetail): number | null {
const score = endpoint.health_score
if (!isEndpointAvailable(endpoint) || typeof score !== 'number' || !Number.isFinite(score)) {
return null
}
return Math.min(1, Math.max(0, score))
}
export function getEndpointHealthLabel(endpoint: EndpointHealthDetail): string {
const score = getEndpointHealthScore(endpoint)
return score === null ? '-' : `${(score * 100).toFixed(0)}%`
}
export function getEndpointHealthBarWidth(endpoint: EndpointHealthDetail): string {
const score = getEndpointHealthScore(endpoint)
return score === null ? '100%' : `${Math.max(score * 100, 5)}%`
}
/**
* 根据健康分数获取颜色
*/
export function getHealthScoreColor(score: number | undefined | null): string {
if (score === undefined || score === null) {
if (score === undefined || score === null || !Number.isFinite(score)) {
return 'bg-muted-foreground/40'
}
if (score >= 0.8) return 'bg-green-500'
@@ -48,10 +66,7 @@ export function getHealthScoreColor(score: number | undefined | null): string {
* 端点不可用时进度条颜色
*/
export function getEndpointDotColor(endpoint: EndpointHealthDetail): string {
if (!isEndpointAvailable(endpoint)) {
return 'bg-muted-foreground/40'
}
return getHealthScoreColor(endpoint.health_score)
return getHealthScoreColor(getEndpointHealthScore(endpoint))
}
/**
@@ -70,8 +85,8 @@ export function getEndpointTooltip(endpoint: EndpointHealthDetail, locale: Local
case 'keys_disabled':
return `${format}: ${t('无可用密钥')}`
case 'available': {
const score = endpoint.health_score
if (score === undefined || score === null) {
const score = getEndpointHealthScore(endpoint)
if (score === null) {
return `${format}: ${t('暂无健康数据')}`
}
return `${format}: ${t('健康度')} ${(score * 100).toFixed(0)}%`