155 lines
11 KiB
Vue
155 lines
11 KiB
Vue
<script setup lang="ts">
|
||
import type { ModelUsageLog, ModelUsageStats } from '#/api/console';
|
||
|
||
import { computed, ref, watch } from 'vue';
|
||
|
||
import { Alert, Button, Card, Col, Empty, Progress, Row, Segmented, Statistic, Table, Tag } from 'ant-design-vue';
|
||
|
||
import { fetchModelUsage, fetchModelUsageLog } from '#/api/console';
|
||
|
||
import { usageNumber, usagePurpose, usageTokens, usageUtcTime } from './model-usage-format';
|
||
import { useUsageRequest } from './use-usage-request';
|
||
|
||
const props = defineProps<{ accountId: number; days: number }>();
|
||
const purpose = ref('');
|
||
const page = ref(1);
|
||
const pageSize = ref(20);
|
||
const { data: stats, loading, error, run: runStats } = useUsageRequest<ModelUsageStats>();
|
||
const { data: log, loading: logLoading, error: logError, run: runLog } = useUsageRequest<ModelUsageLog>();
|
||
const summary = computed(() => stats.value?.summary);
|
||
const coverage = computed(() => summary.value?.request_count
|
||
? Math.round((summary.value.known_count / summary.value.request_count) * 1000) / 10
|
||
: 0);
|
||
const maxDailyTokens = computed(() => Math.max(1, ...(stats.value?.daily ?? []).map((row) => row.total_tokens ?? 0)));
|
||
const models = computed(() => (stats.value?.by_model ?? []).map((row) => ({
|
||
...row, key: `${row.provider_id}:${row.model}`,
|
||
})));
|
||
|
||
function scope() {
|
||
return { accountId: props.accountId, days: props.days, purpose: purpose.value };
|
||
}
|
||
|
||
function loadLog() {
|
||
const query = { ...scope(), limit: pageSize.value, offset: (page.value - 1) * pageSize.value };
|
||
return runLog((signal) => fetchModelUsageLog(query, signal));
|
||
}
|
||
|
||
function reload() {
|
||
const query = scope();
|
||
void runStats((signal) => fetchModelUsage(query, signal));
|
||
void loadLog();
|
||
}
|
||
|
||
watch(() => [props.accountId, props.days, purpose.value], () => {
|
||
page.value = 1;
|
||
reload();
|
||
}, { immediate: true });
|
||
|
||
function changePage(pagination: { current?: number; pageSize?: number }) {
|
||
const nextSize = pagination.pageSize ?? pageSize.value;
|
||
page.value = nextSize === pageSize.value ? (pagination.current ?? 1) : 1;
|
||
pageSize.value = nextSize;
|
||
void loadLog();
|
||
}
|
||
|
||
const tokenColumns = [
|
||
{ title: '请求数', dataIndex: 'request_count', key: 'request_count', width: 85 },
|
||
{ title: '已记录输入', key: 'input_tokens', width: 115 },
|
||
{ title: '已记录输出', key: 'output_tokens', width: 115 },
|
||
{ title: '已记录总 Token', key: 'total_tokens', width: 135 },
|
||
{ title: '用量覆盖', key: 'coverage', width: 120 },
|
||
];
|
||
</script>
|
||
|
||
<template>
|
||
<Card title="模型 Token 用量" class="mb-5" size="small">
|
||
<template #extra><Button :loading="loading || logLoading" @click="reload">刷新用量</Button></template>
|
||
<p class="mb-3 text-sm text-gray-500">
|
||
每次实际上游请求单独计数,包含所有回答候选、裁判、重试、界面检测及知识整理。Token 仅汇总上游已返回的用量;未返回和历史未记录的部分不作估算。
|
||
</p>
|
||
<Segmented v-model:value="purpose" class="mb-4" :options="[
|
||
{ value: '', label: '全部用途' }, { value: 'chat', label: '客服回答与裁判' },
|
||
{ value: 'guard', label: '界面检测' }, { value: 'knowledge', label: '知识整理' },
|
||
]" />
|
||
<Alert v-if="error" class="mb-4" type="error" show-icon :message="`用量统计${error}`" />
|
||
<Row :gutter="[12, 12]" class="mb-4">
|
||
<Col :xs="24" :sm="12" :xl="6"><Card size="small" :loading="loading"><Statistic title="实际上游请求" :value="summary ? usageNumber(summary.request_count) : '—'" /></Card></Col>
|
||
<Col :xs="24" :sm="12" :xl="6"><Card size="small" :loading="loading"><Statistic title="已记录输入 Token" :value="usageTokens(summary, 'input_tokens')" /></Card></Col>
|
||
<Col :xs="24" :sm="12" :xl="6"><Card size="small" :loading="loading"><Statistic title="已记录输出 Token" :value="usageTokens(summary, 'output_tokens')" /></Card></Col>
|
||
<Col :xs="24" :sm="12" :xl="6"><Card size="small" :loading="loading"><Statistic title="已记录总 Token" :value="usageTokens(summary, 'total_tokens')" /></Card></Col>
|
||
</Row>
|
||
<template v-if="stats && summary">
|
||
<div class="mb-4 rounded border border-gray-200 p-3 dark:border-gray-700">
|
||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||
<span>总 Token 用量覆盖:{{ usageNumber(summary.known_count) }} / {{ usageNumber(summary.request_count) }} 次请求</span>
|
||
<Tag :color="summary.missing_count ? 'orange' : 'green'">{{ usageNumber(summary.missing_count) }} 次未返回总用量</Tag>
|
||
</div>
|
||
<Progress :percent="coverage" size="small" :status="summary.missing_count ? 'normal' : undefined" />
|
||
<p v-if="summary.input_known_count != null && summary.output_known_count != null" class="mb-1 text-xs text-gray-500">
|
||
输入用量已记录 {{ summary.input_known_count }} / {{ summary.request_count }} 次;输出用量已记录 {{ summary.output_known_count }} / {{ summary.request_count }} 次。部分请求缺少用量时,合计仅包含已知部分。
|
||
</p>
|
||
<p class="mb-1 text-xs text-gray-500">
|
||
已记录缓存输入 {{ usageTokens(summary, 'cached_input_tokens') }};已记录推理 {{ usageTokens(summary, 'reasoning_tokens') }}。
|
||
缓存与推理属于明细项,不额外叠加至总 Token。单项未返回时显示“未返回”。
|
||
</p>
|
||
<p class="m-0 text-xs text-gray-500">统计起点 {{ usageUtcTime(stats.since) }} UTC;日期趋势按 UTC 自然日汇总。</p>
|
||
</div>
|
||
<Alert v-if="stats.legacy_unmetered_calls" class="mb-4" type="warning" show-icon
|
||
:message="`另有 ${usageNumber(stats.legacy_unmetered_calls)} 次历史业务调用未记录 Token`"
|
||
description="这些历史调用没有逐次上游用量记录,不计入以上请求数和已记录 Token 合计。" />
|
||
<Empty v-if="!summary.request_count" class="my-5" description="当前账号与时间范围内暂无上游请求记录" />
|
||
<template v-else>
|
||
<h3 class="mb-2 font-medium">按模型统计</h3>
|
||
<Table :data-source="models" row-key="key" size="small" :pagination="false" :scroll="{ x: 850 }" class="mb-4"
|
||
:columns="[{ title: '模型 / 服务商', key: 'model', width: 230 }, ...tokenColumns]">
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'model'"><div>{{ record.model || '未标记模型' }}</div><div class="text-xs text-gray-500">{{ record.provider_name || record.provider_id }}</div></template>
|
||
<template v-else-if="column.key === 'coverage'">{{ record.known_count }} / {{ record.request_count }}<div v-if="record.missing_count" class="text-xs text-orange-500">{{ record.missing_count }} 次未返回</div></template>
|
||
<template v-else-if="String(column.key).endsWith('_tokens')">{{ usageTokens(record, column.key) }}</template>
|
||
</template>
|
||
</Table>
|
||
<h3 class="mb-2 font-medium">按用途统计</h3>
|
||
<Table :data-source="stats.by_purpose" row-key="purpose" size="small" :pagination="false" :scroll="{ x: 800 }" class="mb-4"
|
||
:columns="[{ title: '用途', key: 'purpose', width: 180 }, ...tokenColumns]">
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'purpose'">{{ usagePurpose(record.purpose) }}</template>
|
||
<template v-else-if="column.key === 'coverage'">{{ record.known_count }} / {{ record.request_count }}</template>
|
||
<template v-else-if="String(column.key).endsWith('_tokens')">{{ usageTokens(record, column.key) }}</template>
|
||
</template>
|
||
</Table>
|
||
<h3 class="mb-2 font-medium">每日趋势(UTC)</h3>
|
||
<Table :data-source="stats.daily" row-key="date" size="small" :pagination="false" :scroll="{ x: 950 }" class="mb-4"
|
||
:columns="[{ title: '日期(UTC)', dataIndex: 'date', key: 'date', width: 125 }, ...tokenColumns, { title: '已记录总量趋势', key: 'trend', width: 200 }]">
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'trend'"><Progress v-if="record.total_tokens != null" :percent="Math.round((record.total_tokens / maxDailyTokens) * 100)" :show-info="false" size="small" /><span v-else>未返回</span></template>
|
||
<template v-else-if="column.key === 'coverage'">{{ record.known_count }} / {{ record.request_count }}</template>
|
||
<template v-else-if="String(column.key).endsWith('_tokens')">{{ usageTokens(record, column.key) }}</template>
|
||
</template>
|
||
</Table>
|
||
</template>
|
||
</template>
|
||
<h3 class="mb-2 font-medium">实际请求明细</h3>
|
||
<Alert v-if="logError" class="mb-3" type="error" show-icon :message="`请求明细${logError}`"><template #action><Button size="small" @click="loadLog">重试</Button></template></Alert>
|
||
<Table :data-source="log?.items ?? []" row-key="event_id" size="small" :loading="logLoading" :scroll="{ x: 1600 }"
|
||
:pagination="{ current: page, pageSize, total: log?.total ?? 0, showSizeChanger: true, pageSizeOptions: ['20', '50', '100'], showTotal: (total: number) => `共 ${total} 次上游请求` }"
|
||
:columns="[
|
||
{ title: '时间(UTC)', key: 'created_at', width: 170 }, { title: '模型 / 服务商', key: 'model', width: 230 },
|
||
{ title: '用途 / 角色', key: 'purpose', width: 150 }, { title: '状态', key: 'status', width: 80 },
|
||
{ title: '输入 Token', key: 'input_tokens', width: 110 }, { title: '输出 Token', key: 'output_tokens', width: 110 },
|
||
{ title: '总 Token', key: 'total_tokens', width: 110 }, { title: '缓存输入', key: 'cached_input_tokens', width: 110 },
|
||
{ title: '推理 Token', key: 'reasoning_tokens', width: 110 }, { title: '耗时', key: 'latency_ms', width: 90 },
|
||
{ title: '尝试次数', dataIndex: 'attempt', key: 'attempt', width: 90 }, { title: '请求编号', dataIndex: 'request_id', key: 'request_id', width: 210, ellipsis: true },
|
||
]" @change="changePage">
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'created_at'">{{ usageUtcTime(record.created_at) }}</template>
|
||
<template v-else-if="column.key === 'model'"><div>{{ record.model || '未标记模型' }}</div><div class="text-xs text-gray-500">{{ record.provider_name || record.provider_id }}</div></template>
|
||
<template v-else-if="column.key === 'purpose'"><div>{{ usagePurpose(record.purpose) }}</div><Tag>{{ record.role === 'judge' ? '裁判' : '回答 / 生成' }}</Tag></template>
|
||
<template v-else-if="column.key === 'status'"><Tag :color="record.status === 'success' ? 'green' : 'red'">{{ record.status === 'success' ? '成功' : '失败' }}</Tag></template>
|
||
<template v-else-if="String(column.key).endsWith('_tokens')">{{ usageNumber(record[String(column.key)]) }}</template>
|
||
<template v-else-if="column.key === 'latency_ms'">{{ usageNumber(record.latency_ms, '—') }} ms</template>
|
||
</template>
|
||
<template #emptyText>{{ logError ? '请求明细加载失败,请重试' : '暂无实际上游请求' }}</template>
|
||
</Table>
|
||
</Card>
|
||
</template>
|