mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
refactor(frontend): consolidate chart, form, icon and proxy display rules
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -23,27 +23,12 @@ const providerIconComponent = computed(() => normalizedProvider.value
|
||||
: undefined)
|
||||
const authenticationIconComponent = computed(() => authenticationIcon(props.authenticationKind))
|
||||
const showAuthenticationKind = computed(() => props.authenticationKind !== undefined)
|
||||
const groupGapClass = computed(() => {
|
||||
if (props.size === 'xs')
|
||||
return 'gap-0.75'
|
||||
return props.size === 'sm'
|
||||
? 'gap-1'
|
||||
: 'gap-2'
|
||||
})
|
||||
const iconContainerClass = computed(() => {
|
||||
if (props.size === 'xs')
|
||||
return 'size-4.5 rounded-md'
|
||||
return props.size === 'sm'
|
||||
? 'size-5 rounded-md'
|
||||
: 'size-7 rounded-lg'
|
||||
})
|
||||
const iconClass = computed(() => {
|
||||
if (props.size === 'xs')
|
||||
return 'size-2.5'
|
||||
return props.size === 'sm'
|
||||
? 'size-3'
|
||||
: 'size-4'
|
||||
})
|
||||
const sizeClasses = {
|
||||
xs: { gap: 'gap-0.75', container: 'size-4.5 rounded-md', icon: 'size-2.5' },
|
||||
sm: { gap: 'gap-1', container: 'size-5 rounded-md', icon: 'size-3' },
|
||||
md: { gap: 'gap-2', container: 'size-7 rounded-lg', icon: 'size-4' },
|
||||
}
|
||||
const classes = computed(() => sizeClasses[props.size])
|
||||
|
||||
const providerLabel = computed(() => formatProviderLabel(props.provider, t('common.providers.unknown')))
|
||||
|
||||
@@ -51,23 +36,23 @@ const authenticationLabel = computed(() => formatAuthenticationLabel(props.authe
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span class="inline-flex items-center whitespace-nowrap" :class="groupGapClass">
|
||||
<span class="inline-flex items-center whitespace-nowrap" :class="classes.gap">
|
||||
<span
|
||||
class="inline-flex shrink-0 items-center justify-center bg-z-fill-secondary/80 text-z-text"
|
||||
:class="iconContainerClass"
|
||||
:class="classes.container"
|
||||
:title="providerLabel"
|
||||
>
|
||||
<component :is="providerIconComponent" v-if="providerIconComponent" :class="iconClass" />
|
||||
<component :is="providerIconComponent" v-if="providerIconComponent" :class="classes.icon" />
|
||||
<span v-else class="text-[10px] font-heavy text-z-text-quaternary">?</span>
|
||||
</span>
|
||||
|
||||
<span
|
||||
v-if="showAuthenticationKind"
|
||||
class="inline-flex shrink-0 items-center justify-center bg-z-fill-secondary/80 text-z-text"
|
||||
:class="iconContainerClass"
|
||||
:class="classes.container"
|
||||
:title="authenticationLabel"
|
||||
>
|
||||
<component :is="authenticationIconComponent" v-if="authenticationIconComponent" :class="iconClass" />
|
||||
<component :is="authenticationIconComponent" v-if="authenticationIconComponent" :class="classes.icon" />
|
||||
<span v-else class="text-[10px] font-heavy text-z-text-quaternary">?</span>
|
||||
</span>
|
||||
|
||||
|
||||
@@ -24,20 +24,24 @@ const parsed = computed(() => parseJsonObject(text.value, props.maximumBytes))
|
||||
const error = computed(() => parsed.value.error)
|
||||
const value = computed(() => parsed.value.value)
|
||||
const fields = computed(() => inputFields(props.schema))
|
||||
const hasExtra = computed(() => Object.keys(value.value ?? {}).some(key => !fields.value?.some(field => field.key === key)))
|
||||
const incompatibleValue = computed(() => fields.value?.some((field) => {
|
||||
const item = inputValue(field.key)
|
||||
if (item === undefined)
|
||||
const fieldsByKey = computed(() => new Map(fields.value?.map(field => [field.key, field])))
|
||||
const canUseForm = computed(() => {
|
||||
if (fields.value === null || error.value)
|
||||
return false
|
||||
if (field.values)
|
||||
return !field.values.some(option => option === item)
|
||||
if (field.type === 'number' || field.type === 'integer')
|
||||
return typeof item !== 'number' && typeof item !== 'string'
|
||||
return field.type === 'boolean'
|
||||
? typeof item !== 'boolean'
|
||||
: typeof item !== 'string'
|
||||
}) ?? false)
|
||||
const showJson = computed(() => jsonMode.value || fields.value === null || hasExtra.value || incompatibleValue.value || Boolean(error.value))
|
||||
return Object.entries(value.value ?? {}).every(([key, item]) => {
|
||||
const field = fieldsByKey.value.get(key)
|
||||
if (!field)
|
||||
return false
|
||||
if (field.values)
|
||||
return field.values.some(option => option === item)
|
||||
if (field.type === 'number' || field.type === 'integer')
|
||||
return typeof item === 'number' || typeof item === 'string'
|
||||
return field.type === 'boolean'
|
||||
? typeof item === 'boolean'
|
||||
: typeof item === 'string'
|
||||
})
|
||||
})
|
||||
const showJson = computed(() => jsonMode.value || !canUseForm.value)
|
||||
const empty = computed(() => fields.value?.length === 0 && props.schema.additionalProperties === false && !showJson.value)
|
||||
|
||||
function fieldValue(field: InputField) {
|
||||
@@ -105,8 +109,8 @@ function update(field: InputField, input: string) {
|
||||
}
|
||||
|
||||
async function focusField(key?: string) {
|
||||
const editableField = key && fields.value?.some(field => field.key === key)
|
||||
if (editableField && jsonMode.value && !error.value && !hasExtra.value && !incompatibleValue.value)
|
||||
const editableField = key && fieldsByKey.value.has(key)
|
||||
if (editableField && canUseForm.value)
|
||||
jsonMode.value = false
|
||||
await nextTick()
|
||||
|
||||
@@ -128,7 +132,7 @@ defineExpose({ focusField })
|
||||
<ZForm v-if="showJson">
|
||||
<ZFormItem :label="t('common.form.inputPlaceholder')" :error="error">
|
||||
<template #extra>
|
||||
<ZIconButton v-if="fields !== null" :aria-label="t('common.form.useForm')" class="size-4!" variant="link" :disabled="disabled || Boolean(error) || hasExtra || incompatibleValue" @click="jsonMode = false">
|
||||
<ZIconButton v-if="fields !== null" :aria-label="t('common.form.useForm')" class="size-4!" variant="link" :disabled="disabled || !canUseForm" @click="jsonMode = false">
|
||||
<List class="size-3.5" />
|
||||
</ZIconButton>
|
||||
</template>
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { ProxyBatchItem } from '../utils/batch'
|
||||
import { ZScrollbar } from '@codex-proxy/ui'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { proxyBatchStates } from '../utils/batch'
|
||||
import { proxyBatchStates, summarizeProxyBatch, summarizeProxyTests } from '../utils/batch'
|
||||
import ProxyRegion from './ProxyRegion.vue'
|
||||
|
||||
const props = defineProps<{ items: ProxyBatchItem[], tests?: ProxyBatchItem[] }>()
|
||||
@@ -12,51 +12,61 @@ const props = defineProps<{ items: ProxyBatchItem[], tests?: ProxyBatchItem[] }>
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
|
||||
const testItems = computed(() => props.tests ?? [])
|
||||
|
||||
function resultMessage(item: ProxyBatchItem, test?: ProxyBatchItem) {
|
||||
if (test && test.status !== 'succeeded')
|
||||
return test.message
|
||||
const detection = test?.record?.lastTest?.location
|
||||
if (detection?.status === 'failed')
|
||||
return detection.message
|
||||
return item.status === 'succeeded'
|
||||
? ''
|
||||
: item.message
|
||||
}
|
||||
|
||||
const rows = computed(() => {
|
||||
const tests = new Map(testItems.value.map(item => [item.key, item]))
|
||||
return props.items.map((item) => {
|
||||
const test = item.record
|
||||
? tests.get(item.record.id)
|
||||
: undefined
|
||||
const detection = test?.record?.lastTest?.location
|
||||
const status = (test ?? item).status
|
||||
const state = proxyBatchStates.value[status]
|
||||
let label = state.label
|
||||
if (status === 'succeeded') {
|
||||
label = test
|
||||
? t('proxies.batchTest.connectionIsNormal')
|
||||
: t('proxies.batchTest.addedStatus')
|
||||
}
|
||||
return {
|
||||
item,
|
||||
test,
|
||||
result: test ?? item,
|
||||
message: test && test.status !== 'succeeded'
|
||||
? test.message
|
||||
: detection?.status === 'failed'
|
||||
? detection.message
|
||||
: item.status !== 'succeeded'
|
||||
? item.message
|
||||
: '',
|
||||
label,
|
||||
tone: state.tone,
|
||||
message: resultMessage(item, test),
|
||||
messageTone: status === 'succeeded'
|
||||
? 'text-z-warning-text'
|
||||
: state.tone,
|
||||
}
|
||||
})
|
||||
})
|
||||
const completed = computed(() => props.items.filter(item => !['pending', 'running', 'skipped'].includes(item.status)).length)
|
||||
const created = computed(() => props.items.filter(item => item.status === 'succeeded').length)
|
||||
const invalid = computed(() => props.items.filter(item => item.status === 'failed').length)
|
||||
const succeeded = computed(() => testItems.value.filter(item => item.status === 'succeeded').length)
|
||||
const failed = computed(() => testItems.value.filter(item => item.status === 'failed').length)
|
||||
const allOperations = computed(() => [
|
||||
...props.items,
|
||||
...testItems.value,
|
||||
])
|
||||
const attention = computed(() => allOperations.value.filter(item => ['error', 'conflict', 'busy', 'unknown'].includes(item.status)).length)
|
||||
const skipped = computed(() => allOperations.value.filter(item => item.status === 'skipped').length)
|
||||
const locationPending = computed(() => testItems.value.filter(item => item.status === 'succeeded' && item.record?.lastTest?.location.status !== 'detected').length)
|
||||
|
||||
const additions = computed(() => summarizeProxyBatch(props.items))
|
||||
const connections = computed(() => summarizeProxyTests(testItems.value))
|
||||
const attention = computed(() => additions.value.attention + connections.value.attention)
|
||||
const skipped = computed(() => additions.value.skipped + connections.value.skipped)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="min-w-0 space-y-3" :aria-label="t('proxies.batchTest.resultsTitle')">
|
||||
<div class="flex flex-wrap items-center gap-x-4 gap-y-1 text-z-xs text-z-text-secondary" role="status" aria-live="polite">
|
||||
<span>{{ t('proxies.batchTest.processed') }} <strong class="font-mono font-medium text-z-text">{{ completed }} / {{ items.length }}</strong></span>
|
||||
<span>{{ t('proxies.batchTest.added', { created }) }}</span>
|
||||
<span v-if="invalid">{{ t('proxies.batchTest.invalidFormat', { invalid }) }}</span>
|
||||
<span>{{ t('proxies.batchTest.processed') }} <strong class="font-mono font-medium text-z-text">{{ additions.completed }} / {{ items.length }}</strong></span>
|
||||
<span>{{ t('proxies.batchTest.added', { created: additions.succeeded }) }}</span>
|
||||
<span v-if="additions.failed">{{ t('proxies.batchTest.invalidFormat', { invalid: additions.failed }) }}</span>
|
||||
<template v-if="testItems.length">
|
||||
<span>{{ t('proxies.batchTest.connected', { succeeded }) }}</span>
|
||||
<span>{{ t('proxies.batchTest.connectionFailed', { failed }) }}</span>
|
||||
<span v-if="locationPending">{{ t('proxies.batchTest.locationPending', { locationPending }) }}</span>
|
||||
<span>{{ t('proxies.batchTest.connected', { succeeded: connections.succeeded }) }}</span>
|
||||
<span>{{ t('proxies.batchTest.connectionFailed', { failed: connections.failed }) }}</span>
|
||||
<span v-if="connections.locationPending">{{ t('proxies.batchTest.locationPending', { locationPending: connections.locationPending }) }}</span>
|
||||
</template>
|
||||
<span v-if="attention">{{ t('proxies.batchTest.needsAttention', { attention }) }}</span>
|
||||
<span v-if="skipped">{{ t('proxies.batchTest.skipped', { skipped }) }}</span>
|
||||
@@ -65,9 +75,9 @@ const locationPending = computed(() => testItems.value.filter(item => item.statu
|
||||
<ol class="m-0 list-none space-y-1 p-0">
|
||||
<li v-for="row in rows" :key="row.item.key" class="grid grid-cols-[minmax(0,1fr)_auto] gap-x-3 gap-y-1 rounded-z-sm px-3 py-2.5 odd:bg-z-fill-quaternary">
|
||||
<span class="min-w-0 truncate font-mono text-z-xs text-z-text" :title="row.item.endpoint">{{ row.item.endpoint || t('proxies.batchTest.lineNumber', { key: row.item.key }) }}</span>
|
||||
<span class="flex flex-wrap justify-end gap-x-2 text-z-xs" :class="proxyBatchStates[row.result.status].tone">
|
||||
<span class="flex flex-wrap justify-end gap-x-2 text-z-xs" :class="row.tone">
|
||||
<span v-if="row.test" class="text-z-text-secondary">{{ t('proxies.batchTest.addedStatus') }}</span>
|
||||
<span>{{ row.result.status === 'succeeded' ? row.test ? t('proxies.batchTest.connectionIsNormal') : t('proxies.batchTest.addedStatus') : proxyBatchStates[row.result.status].label }}</span>
|
||||
<span>{{ row.label }}</span>
|
||||
</span>
|
||||
<div class="col-span-2 flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1 text-z-xs text-z-text-secondary">
|
||||
<span class="max-w-full truncate" :title="row.item.name">{{ row.item.name }}</span>
|
||||
@@ -76,7 +86,7 @@ const locationPending = computed(() => testItems.value.filter(item => item.statu
|
||||
<span class="whitespace-nowrap">{{ row.test.record.lastTest.latencyMs }} ms</span><span v-if="row.test.record.lastTest.exitIpv4"> · IPv4 {{ row.test.record.lastTest.exitIpv4 }}</span><span v-if="row.test.record.lastTest.exitIpv6"> · IPv6 {{ row.test.record.lastTest.exitIpv6 }}</span>
|
||||
</span>
|
||||
</div>
|
||||
<span v-if="row.message" class="col-span-2 text-z-xs leading-relaxed wrap-anywhere" :class="row.result.status === 'succeeded' ? 'text-z-warning-text' : proxyBatchStates[row.result.status].tone">{{ row.message }}</span>
|
||||
<span v-if="row.message" class="col-span-2 text-z-xs leading-relaxed wrap-anywhere" :class="row.messageTone">{{ row.message }}</span>
|
||||
</li>
|
||||
</ol>
|
||||
</ZScrollbar>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { getProxies } from '@/api'
|
||||
import { useRequestState } from '@/composables/useRequestState'
|
||||
import { t } from '@/i18n'
|
||||
import { notification as ZNotification } from '@/utils/notification'
|
||||
import { summarizeProxyTests } from '../utils/batch'
|
||||
import { useProxyBatchTest } from './useProxyBatchTest'
|
||||
|
||||
export function useProxyBatchTesting(options: { test: ProxyTestExecutor, reload: () => Promise<unknown> }) {
|
||||
@@ -48,23 +49,22 @@ export function useProxyBatchTesting(options: { test: ProxyTestExecutor, reload:
|
||||
await options.reload()
|
||||
if (!request.isCurrent(requestId))
|
||||
return
|
||||
const succeeded = tests.items.value.filter(item => item.status === 'succeeded').length
|
||||
const incomplete = tests.items.value.filter(item => item.status !== 'succeeded')
|
||||
const locationPending = tests.items.value.filter(item => item.status === 'succeeded' && item.record?.lastTest?.location.status !== 'detected').length
|
||||
const { succeeded, locationPending } = summarizeProxyTests(tests.items.value)
|
||||
const incomplete = tests.items.value.length - succeeded
|
||||
const missing = wanted.size - collected.size
|
||||
const summary = [
|
||||
t('proxies.batchTest.batchTestCompletedConnected', { succeeded }),
|
||||
]
|
||||
if (incomplete.length)
|
||||
summary.push(t('proxies.batchTest.unsuccessful', { count: incomplete.length }))
|
||||
if (incomplete)
|
||||
summary.push(t('proxies.batchTest.unsuccessful', { count: incomplete }))
|
||||
if (locationPending)
|
||||
summary.push(t('proxies.batchTest.locationsPending', { locationPending }))
|
||||
if (missing)
|
||||
summary.push(t('proxies.batchTest.proxiesNoLongerAvailable', { missing }))
|
||||
const firstFailure = incomplete.find(item => item.status !== 'skipped')
|
||||
const firstFailure = tests.items.value.find(item => item.status !== 'succeeded' && item.status !== 'skipped')
|
||||
if (firstFailure)
|
||||
summary.push(`${firstFailure.name}:${firstFailure.message}`)
|
||||
const notify = incomplete.length || locationPending || missing
|
||||
const notify = incomplete || locationPending || missing
|
||||
? ZNotification.warning
|
||||
: ZNotification.success
|
||||
notify({
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { OutboundProxyRecord, ProxyBatchResult } from '@/api'
|
||||
import { countBy } from 'es-toolkit'
|
||||
import { computed } from 'vue'
|
||||
import { ApiError } from '@/api/request'
|
||||
import { t } from '@/i18n'
|
||||
@@ -23,6 +24,25 @@ export interface ProxyBatchItem {
|
||||
record?: OutboundProxyRecord
|
||||
}
|
||||
|
||||
export function summarizeProxyBatch(items: ProxyBatchItem[]) {
|
||||
const counts = countBy(items, item => item.status)
|
||||
const skipped = counts.skipped ?? 0
|
||||
return {
|
||||
completed: items.length - (counts.pending ?? 0) - (counts.running ?? 0) - skipped,
|
||||
succeeded: counts.succeeded ?? 0,
|
||||
failed: counts.failed ?? 0,
|
||||
attention: (counts.error ?? 0) + (counts.conflict ?? 0) + (counts.busy ?? 0) + (counts.unknown ?? 0),
|
||||
skipped,
|
||||
}
|
||||
}
|
||||
|
||||
export function summarizeProxyTests(items: ProxyBatchItem[]) {
|
||||
return {
|
||||
...summarizeProxyBatch(items),
|
||||
locationPending: items.filter(item => item.status === 'succeeded' && item.record?.lastTest?.location.status !== 'detected').length,
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProxyBatchInput {
|
||||
line: number
|
||||
raw: string
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import type { EChartsOption, LineSeriesOption } from 'echarts'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import type { UsageCostMetric, UsageMetricResponse } from '@/api'
|
||||
import { ZCard, ZEmpty, ZSegmented } from '@codex-proxy/ui'
|
||||
@@ -7,12 +7,10 @@ import { ZCard, ZEmpty, ZSegmented } from '@codex-proxy/ui'
|
||||
import { computed } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import BaseChart from '@/components/charts/BaseChart.vue'
|
||||
import {
|
||||
zeroInactiveValues,
|
||||
} from '@/components/charts/timeSeriesGap'
|
||||
import { zeroInactiveValues } from '@/components/charts/timeSeriesGap'
|
||||
import { chartTooltipContent, tooltipIndex, tooltipRows } from '@/components/charts/tooltip'
|
||||
import { useChartPalette } from '@/composables/useChartPalette'
|
||||
import { decimalDisplayNumber, formatLocalizedCompactNumber as formatCompactNumber, formatPercent, formatUsd } from '@/utils/format'
|
||||
import { decimalDisplayNumber, formatPercent, formatUsd } from '@/utils/format'
|
||||
|
||||
import {
|
||||
formatUsdAxis,
|
||||
@@ -37,23 +35,19 @@ const props = withDefaults(
|
||||
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
|
||||
type UsageChartPalette = ReturnType<typeof useChartPalette>['palette']['value']
|
||||
interface CostSeries {
|
||||
label: string
|
||||
color: string
|
||||
values: (number | null)[]
|
||||
area?: 'strong'
|
||||
tooltipValue: (index: number) => string | null
|
||||
}
|
||||
|
||||
const activeView = defineModel<UsageCostMetric>({ required: true })
|
||||
const { palette } = useChartPalette()
|
||||
const cost = computed(() => props.result?.data.summary ?? null)
|
||||
const points = computed(() => props.result?.data.points ?? [])
|
||||
const costPoints = computed(() => props.result?.metric === 'cost'
|
||||
? props.result.data.points
|
||||
: [])
|
||||
const savingsPoints = computed(() => props.result?.metric === 'savings'
|
||||
? props.result.data.points
|
||||
: [])
|
||||
const cachePoints = computed(() => props.result?.metric === 'cache'
|
||||
? props.result.data.points
|
||||
: [])
|
||||
const requestActivity = computed(() => points.value.map(point => point.requestCount > 0))
|
||||
const hasNoCacheCost = computed(() => costPoints.value.some(point => point.value.noCacheCost != null))
|
||||
const coverageRate = computed(() => {
|
||||
if (!cost.value)
|
||||
return null
|
||||
@@ -79,21 +73,79 @@ const viewOptions = computed(() => ([
|
||||
},
|
||||
]))
|
||||
|
||||
const hasData = computed(() => {
|
||||
if (props.loading || props.result?.metric !== activeView.value)
|
||||
return false
|
||||
if (activeView.value === 'cost')
|
||||
return costPoints.value.some(point => point.value.estimatedCost != null || point.value.noCacheCost != null)
|
||||
if (activeView.value === 'savings')
|
||||
return savingsPoints.value.some(point => point.value != null)
|
||||
return cachePoints.value.some(({ value }) => value.inputTokens > 0 || value.cachedTokenRate > 0 || (value.cacheHitRequestRate ?? 0) > 0)
|
||||
function amountValues(amounts: (string | null)[]) {
|
||||
return zeroInactiveValues(amounts.map(amount => decimalDisplayNumber(amount)), requestActivity.value)
|
||||
}
|
||||
|
||||
function primaryAmountSeries(label: string, amounts: (string | null)[]): CostSeries {
|
||||
return {
|
||||
label,
|
||||
color: palette.value.success,
|
||||
area: 'strong',
|
||||
values: amountValues(amounts),
|
||||
tooltipValue: index => formatUsd(amounts[index] ?? (requestActivity.value[index] === false
|
||||
? '0'
|
||||
: null)),
|
||||
}
|
||||
}
|
||||
|
||||
function rateSeries(label: string, color: string, values: (number | null)[]): CostSeries {
|
||||
return {
|
||||
label,
|
||||
color,
|
||||
values,
|
||||
tooltipValue: index => formatPercent(values[index] ?? null),
|
||||
}
|
||||
}
|
||||
|
||||
const chart = computed<{ hasData: boolean, series: CostSeries[] }>(() => {
|
||||
const response = props.result
|
||||
if (props.loading || !response || response.metric !== activeView.value)
|
||||
return { hasData: false, series: [] }
|
||||
|
||||
if (response.metric === 'cost') {
|
||||
const actual = response.data.points.map(({ value }) => value.estimatedCost)
|
||||
const comparison = response.data.points.map(({ value }) => value.noCacheCost)
|
||||
const hasComparison = comparison.some(amount => amount != null)
|
||||
const series = [
|
||||
primaryAmountSeries(t('usage.cost.actualCost'), actual),
|
||||
]
|
||||
if (hasComparison) {
|
||||
series.push({
|
||||
label: t('usage.cost.withoutCacheEstimate'),
|
||||
color: palette.value.textMuted,
|
||||
values: amountValues(comparison),
|
||||
// 对比曲线允许补零,提示只展示该时间桶实际提供的估算
|
||||
tooltipValue: index => comparison[index] == null
|
||||
? null
|
||||
: formatUsd(comparison[index]),
|
||||
})
|
||||
}
|
||||
return { hasData: hasComparison || actual.some(amount => amount != null), series }
|
||||
}
|
||||
if (response.metric === 'savings') {
|
||||
const savings = response.data.points.map(point => point.value)
|
||||
return {
|
||||
hasData: savings.some(amount => amount != null),
|
||||
series: [
|
||||
primaryAmountSeries(t('usage.cost.cacheSavings'), savings),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
const { points } = response.data
|
||||
return {
|
||||
hasData: points.some(({ value }) => value.inputTokens > 0 || value.cachedTokenRate > 0 || (value.cacheHitRequestRate ?? 0) > 0),
|
||||
series: [
|
||||
rateSeries(t('usage.cost.cacheTokenProportion'), palette.value.normal, points.map(({ value }) => value.cachedTokenRate)),
|
||||
rateSeries(t('usage.cost.hitRequestRate'), palette.value.success, points.map(({ value }) => value.cacheHitRequestRate)),
|
||||
],
|
||||
}
|
||||
})
|
||||
|
||||
const chartOption = computed<EChartsOption>(() => {
|
||||
const theme = palette.value
|
||||
const legend = legendNames()
|
||||
const series = chartSeries(theme)
|
||||
|
||||
const series = chart.value.series
|
||||
return {
|
||||
animationDuration: 240,
|
||||
grid: {
|
||||
@@ -104,157 +156,34 @@ const chartOption = computed<EChartsOption>(() => {
|
||||
outerBoundsMode: 'same',
|
||||
outerBoundsContain: 'axisLabel',
|
||||
},
|
||||
legend: usageLegend(theme, legend),
|
||||
legend: usageLegend(theme, series.map(item => item.label)),
|
||||
tooltip: usageTooltip(theme, formatTooltip),
|
||||
xAxis: usageCategoryAxis(
|
||||
points.value.map(point => point.label),
|
||||
theme,
|
||||
),
|
||||
yAxis: usageValueAxis(theme, axisFormatter(), {
|
||||
min: activeView.value === 'cache'
|
||||
? 0
|
||||
: undefined,
|
||||
max: activeView.value === 'cache'
|
||||
? 1
|
||||
: undefined,
|
||||
}),
|
||||
series,
|
||||
yAxis: activeView.value === 'cache'
|
||||
? usageValueAxis(theme, formatPercent, { min: 0, max: 1 })
|
||||
: usageValueAxis(theme, formatUsdAxis),
|
||||
series: series.flatMap(item => usageGapAwareLineSeries(item.label, item.values, item.color, { area: item.area })),
|
||||
}
|
||||
})
|
||||
|
||||
function legendNames() {
|
||||
if (activeView.value === 'savings') {
|
||||
return [
|
||||
t('usage.cost.cacheSavings'),
|
||||
]
|
||||
}
|
||||
if (activeView.value === 'cache') {
|
||||
return [
|
||||
t('usage.cost.cacheTokenProportion'),
|
||||
t('usage.cost.hitRequestRate'),
|
||||
]
|
||||
}
|
||||
return hasNoCacheCost.value
|
||||
? [
|
||||
t('usage.cost.actualCost'),
|
||||
t('usage.cost.withoutCacheEstimate'),
|
||||
]
|
||||
: [
|
||||
t('usage.cost.actualCost'),
|
||||
]
|
||||
}
|
||||
|
||||
function axisFormatter() {
|
||||
if (activeView.value === 'cost' || activeView.value === 'savings')
|
||||
return formatUsdAxis
|
||||
if (activeView.value === 'cache')
|
||||
return (value: number) => formatPercent(value)
|
||||
return (value: number) => formatCompactNumber(value)
|
||||
}
|
||||
|
||||
function chartSeries(theme: UsageChartPalette): LineSeriesOption[] {
|
||||
if (activeView.value === 'savings') {
|
||||
return [
|
||||
...usageGapAwareLineSeries(
|
||||
t('usage.cost.cacheSavings'),
|
||||
zeroInactiveValues(
|
||||
savingsPoints.value.map(point => decimalDisplayNumber(point.value)),
|
||||
requestActivity.value,
|
||||
),
|
||||
theme.success,
|
||||
{ area: 'strong' },
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
if (activeView.value === 'cache') {
|
||||
return [
|
||||
...usageGapAwareLineSeries(
|
||||
t('usage.cost.cacheTokenProportion'),
|
||||
cachePoints.value.map(point => point.value.cachedTokenRate),
|
||||
theme.normal,
|
||||
),
|
||||
...usageGapAwareLineSeries(
|
||||
t('usage.cost.hitRequestRate'),
|
||||
cachePoints.value.map(point => point.value.cacheHitRequestRate),
|
||||
theme.success,
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
const series = [
|
||||
...usageGapAwareLineSeries(
|
||||
t('usage.cost.actualCost'),
|
||||
zeroInactiveValues(
|
||||
costPoints.value.map(point => decimalDisplayNumber(point.value.estimatedCost)),
|
||||
requestActivity.value,
|
||||
),
|
||||
theme.success,
|
||||
{ area: 'strong' },
|
||||
),
|
||||
]
|
||||
if (hasNoCacheCost.value) {
|
||||
series.push(
|
||||
...usageGapAwareLineSeries(
|
||||
t('usage.cost.withoutCacheEstimate'),
|
||||
zeroInactiveValues(
|
||||
costPoints.value.map(point => decimalDisplayNumber(point.value.noCacheCost)),
|
||||
requestActivity.value,
|
||||
),
|
||||
theme.textMuted,
|
||||
),
|
||||
)
|
||||
}
|
||||
return series
|
||||
}
|
||||
|
||||
function formatTooltip(params: unknown) {
|
||||
const rows = tooltipRows(params)
|
||||
const pointIndex = tooltipIndex(rows[0])
|
||||
const point = points.value[pointIndex]
|
||||
const index = tooltipIndex(tooltipRows(params)[0])
|
||||
const point = points.value[index]
|
||||
if (!point)
|
||||
return ''
|
||||
const theme = palette.value
|
||||
|
||||
if (activeView.value === 'savings') {
|
||||
return chartTooltipContent(point.label, [
|
||||
usageTooltipItem(
|
||||
t('usage.cost.cacheSavings'),
|
||||
formatUsd(costAmount(savingsPoints.value[pointIndex]?.value ?? null, pointIndex)),
|
||||
theme.success,
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
if (activeView.value === 'cache') {
|
||||
return chartTooltipContent(point.label, [
|
||||
usageTooltipItem(t('usage.cost.cacheTokenProportion'), formatPercent(cachePoints.value[pointIndex]?.value.cachedTokenRate ?? null), theme.normal),
|
||||
usageTooltipItem(t('usage.cost.hitRequestRate'), formatPercent(cachePoints.value[pointIndex]?.value.cacheHitRequestRate ?? null), theme.success),
|
||||
])
|
||||
}
|
||||
|
||||
const lines = [
|
||||
usageTooltipItem(
|
||||
t('usage.cost.actualCost'),
|
||||
formatUsd(costAmount(costPoints.value[pointIndex]?.value.estimatedCost ?? null, pointIndex)),
|
||||
theme.success,
|
||||
),
|
||||
]
|
||||
if (costPoints.value[pointIndex]?.value.noCacheCost != null) {
|
||||
lines.push(usageTooltipItem(
|
||||
t('usage.cost.withoutCacheEstimate'),
|
||||
formatUsd(costAmount(costPoints.value[pointIndex]?.value.noCacheCost ?? null, pointIndex)),
|
||||
theme.textMuted,
|
||||
))
|
||||
}
|
||||
const lines = chart.value.series.flatMap((series) => {
|
||||
const value = series.tooltipValue(index)
|
||||
return value == null
|
||||
? []
|
||||
: [
|
||||
usageTooltipItem(series.label, value, series.color),
|
||||
]
|
||||
})
|
||||
return chartTooltipContent(point.label, lines)
|
||||
}
|
||||
|
||||
function costAmount(value: string | null, pointIndex: number) {
|
||||
return value ?? (requestActivity.value[pointIndex] === false
|
||||
? '0'
|
||||
: null)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -271,8 +200,8 @@ function costAmount(value: string | null, pointIndex: number) {
|
||||
</template>
|
||||
|
||||
<template #body>
|
||||
<div class="grid min-h-66" :class="hasData ? 'gap-3' : 'flex-1'">
|
||||
<div v-if="hasData && cost" class="grid grid-cols-2 gap-x-1 gap-y-3 rounded-xl bg-z-fill-quaternary/45 p-2 @min-[24rem]/cost:grid-cols-3 @min-[40rem]/cost:grid-cols-5">
|
||||
<div class="grid min-h-66" :class="chart.hasData ? 'gap-3' : 'flex-1'">
|
||||
<div v-if="chart.hasData && cost" class="grid grid-cols-2 gap-x-1 gap-y-3 rounded-xl bg-z-fill-quaternary/45 p-2 @min-[24rem]/cost:grid-cols-3 @min-[40rem]/cost:grid-cols-5">
|
||||
<div class="grid min-w-0 gap-1 px-1.5">
|
||||
<span class="text-[10px] font-bold text-z-text-quaternary" :title="t('usage.billing.totalCost')">{{ t('usage.cost.actualCost') }}</span>
|
||||
<strong class="wrap-anywhere font-mono text-z-sm font-heavy tabular-nums text-z-text">
|
||||
@@ -304,7 +233,7 @@ function costAmount(value: string | null, pointIndex: number) {
|
||||
</strong>
|
||||
</div>
|
||||
</div>
|
||||
<BaseChart v-if="hasData" :option="chartOption" :height="210" />
|
||||
<BaseChart v-if="chart.hasData" :option="chartOption" :height="210" />
|
||||
<ZEmpty
|
||||
v-else
|
||||
size="small"
|
||||
|
||||
Reference in New Issue
Block a user