fix(admin): 优化数据报表

This commit is contained in:
sophie committed 2026-07-14 14:35:52 +08:00
1 parent 49c149eb3e
commit cd81911514
10 files changed
+327 -178

No files matched your search

@@ -1,14 +1,15 @@
/**
* features/admin/modules/reports/ReportsPage.vue
* 用途:管理后台报表独立页面(使用分析、应用运营、多表浏览与聊天日志批量操作)。
* 依赖:useReportsAdmin、DeleteConfirmDialog、lucide 图标。
* 逻辑:路由 /admin/reports/:table?;agents 表渲染应用运营看板而非配置 dump。
* 依赖:useReportsAdmin、DeleteConfirmDialog、PaginationBar、lucide 图标。
* 逻辑:路由 /admin/reports/:table?;分页统一复用 shared/PaginationBar。
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { Activity, AlertTriangle, BarChart2, Download, LineChart, Settings2, Trophy } from 'lucide-vue-next';
import { Activity, AlertTriangle, BarChart2, CircleHelp, Download, LineChart, Settings2, Trophy } from 'lucide-vue-next';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import PaginationBar from '@/shared/components/PaginationBar.vue';
import { formatLocalDateTime } from '@/shared/utils/dateTime';
import type { ApplicationSortKey } from '@/features/admin/types/report.types';
import { useReportsAdmin } from './composables/useReportsAdmin';
@@ -41,6 +42,22 @@ const {
usageSpikeDayItems,
usageTopUserItems,
usageTopAgentItems,
usageUserPage,
usageUserPageSize,
usageUserPageSizeOptions,
usageUserTotalPages,
goToUsageUserPage,
usageAgentPage,
usageAgentPageSize,
usageAgentPageSizeOptions,
usageAgentTotal,
usageAgentTotalPages,
goToUsageAgentPage,
usageLowAgentMaxChats,
usageLowAgentListLimit,
usageSpikeDayMinChats,
usageSpikeDayAvgMultiplier,
usageSpikeDayListLimit,
applicationRange,
applicationStatus,
applicationUsage,
@@ -52,10 +69,7 @@ const {
applicationItems,
applicationTotal,
applicationTotalPages,
applicationPageButtons,
goToApplicationPage,
prevApplicationPage,
nextApplicationPage,
onApplicationSortClick,
getApplicationSortMark,
handleApplicationSearchChange,
@@ -89,7 +103,6 @@ const {
reportTotalPages,
reportRangeStart,
reportRangeEnd,
reportPageButtons,
showDeleteConfirmDialog,
deleteConfirmMessage,
loadAll,
@@ -116,8 +129,6 @@ const {
toggleAllReportRows,
batchDeleteChatLogs,
goToReportPage,
prevReportPage,
nextReportPage,
confirmDelete,
} = useReportsAdmin();
@@ -132,24 +143,6 @@ const applicationSortableKeys: ApplicationSortKey[] = [
'avg_score',
];
/** 数值类列右对齐,避免表头与数字看起来错位 */
const applicationNumericKeys = new Set<ApplicationSortKey>([
'chat_count',
'unique_users',
'authorized_users',
'pending_requests',
'avg_score',
'last_active_at',
]);
/**
* 判断应用运营列是否按数值样式右对齐。
* @param key - 列字段
*/
function isApplicationNumericKey(key: ApplicationSortKey): boolean {
return applicationNumericKeys.has(key);
}
/**
* 点击摘要卡快速切换「有使用 / 零使用」筛选。
* @param usage - used | unused | all
@@ -158,15 +151,47 @@ function setApplicationUsageFilter(usage: 'all' | 'used' | 'unused'): void {
applicationUsage.value = usage;
}
/** 低使用应用 / 异常高峰规则提示(点击 ? 以 tooltip 形式展示) */
const showLowAgentHint = ref(false);
const showSpikeDayHint = ref(false);
/**
* 计算使用趋势柱状条高度,避免低数据量时图表完全不可见。
* 核心逻辑:按当前范围最大日对话量归一化,最小保留 12% 高度用于视觉识别。
* 切换低使用应用选取规则 tooltip 显隐;打开时关闭另一侧提示。
*/
function toggleLowAgentHint(): void {
showSpikeDayHint.value = false;
showLowAgentHint.value = !showLowAgentHint.value;
}
/**
* 切换异常高峰日期选取规则 tooltip 显隐;打开时关闭另一侧提示。
*/
function toggleSpikeDayHint(): void {
showLowAgentHint.value = false;
showSpikeDayHint.value = !showSpikeDayHint.value;
}
/**
* 点击页面其他区域时关闭运营提醒规则 tooltip。
*/
function onDocumentClickCloseInsightHints(): void {
showLowAgentHint.value = false;
showSpikeDayHint.value = false;
}
/**
* 计算使用趋势柱状条高度(像素)。
* 核心逻辑:按当前范围最大日对话量归一化;不用百分比——flex 子项无明确高度时 % 会塌成 0,导致「有日期无柱子」。
* @param count - 单日对话量
* @returns CSS height 百分比
* @returns CSS height(px)
*/
function getUsageTrendBarHeight(count: number): string {
const ratio = Math.max(0.12, count / usageTrendMax.value);
return `${Math.round(ratio * 100)}%`;
/** 图表区 h-44(176px) 减去日期标签与间距后的可绘高度 */
const plotMaxPx = 140;
const minPx = 8;
const max = Math.max(1, usageTrendMax.value);
const px = Math.max(minPx, Math.round((count / max) * plotMaxPx));
return `${px}px`;
}
/**
@@ -184,8 +209,13 @@ function formatUsageDayLabel(date: string): string {
}
onMounted(() => {
document.addEventListener('click', onDocumentClickCloseInsightHints);
void loadAll();
});
onBeforeUnmount(() => {
document.removeEventListener('click', onDocumentClickCloseInsightHints);
});
</script>
<template>
@@ -222,7 +252,7 @@ onMounted(() => {
</span>
</div>
</div>
<div class="flex flex-wrap items-center gap-2 md:gap-3">
<div v-if="!isUsageAnalysisTable" class="flex flex-wrap items-center gap-2 md:gap-3">
<button
v-if="!isApplicationOpsTable"
@click="showChatLogsColumnPanel = !showChatLogsColumnPanel"
@@ -319,11 +349,14 @@ onMounted(() => {
<div
v-for="day in usageReport.by_day"
:key="day.date"
class="flex min-w-7 flex-1 flex-col items-center justify-end gap-2"
class="flex min-w-7 flex-1 flex-col items-center justify-end gap-1.5 self-stretch"
:title="`${day.date}: ${day.count}`"
>
<div class="w-full max-w-8 rounded-t bg-brand-500/80" :style="{ height: getUsageTrendBarHeight(day.count) }" />
<span class="text-[10px] text-muted">{{ formatUsageDayLabel(day.date) }}</span>
<div
class="mt-auto w-full max-w-8 rounded-t bg-brand-500/80"
:style="{ height: getUsageTrendBarHeight(day.count) }"
/>
<span class="text-[10px] leading-none text-muted">{{ formatUsageDayLabel(day.date) }}</span>
</div>
</div>
<div v-else class="py-14 text-center text-sm text-muted">{{ t('admin.report.usageTrendEmpty') }}</div>
@@ -336,7 +369,28 @@ onMounted(() => {
</div>
<div class="space-y-3">
<div class="rounded-lg bg-surface-soft p-3">
<div class="text-xs font-semibold text-secondary">{{ t('admin.report.usageLowAgents') }}</div>
<div class="flex items-center gap-1.5">
<span class="text-xs font-semibold text-secondary">{{ t('admin.report.usageLowAgents') }}</span>
<div class="relative inline-flex">
<button
type="button"
class="inline-flex rounded p-0.5 text-muted transition-colors hover:text-brand-600"
:aria-expanded="showLowAgentHint"
:aria-label="t('admin.report.usageLowAgentsHintAria')"
@click.stop="toggleLowAgentHint"
>
<CircleHelp class="h-3.5 w-3.5" />
</button>
<div
v-if="showLowAgentHint"
role="tooltip"
class="absolute left-1/2 top-full z-20 mt-1.5 w-56 -translate-x-1/2 rounded-lg border border-default bg-surface px-3 py-2 text-xs leading-relaxed text-secondary shadow-elevated"
@click.stop
>
{{ t('admin.report.usageLowAgentsHint', { max: usageLowAgentMaxChats, limit: usageLowAgentListLimit }) }}
</div>
</div>
</div>
<div v-if="usageLowAgentItems.length" class="mt-2 space-y-2">
<div v-for="item in usageLowAgentItems" :key="item.agent" class="flex items-center justify-between gap-3 text-sm">
<span class="truncate text-secondary" :title="item.agent">{{ item.agent }}</span>
@@ -346,7 +400,32 @@ onMounted(() => {
<div v-else class="mt-2 text-xs text-muted">{{ t('admin.report.usageLowAgentsEmpty') }}</div>
</div>
<div class="rounded-lg bg-surface-soft p-3">
<div class="text-xs font-semibold text-secondary">{{ t('admin.report.usageSpikeDays') }}</div>
<div class="flex items-center gap-1.5">
<span class="text-xs font-semibold text-secondary">{{ t('admin.report.usageSpikeDays') }}</span>
<div class="relative inline-flex">
<button
type="button"
class="inline-flex rounded p-0.5 text-muted transition-colors hover:text-brand-600"
:aria-expanded="showSpikeDayHint"
:aria-label="t('admin.report.usageSpikeDaysHintAria')"
@click.stop="toggleSpikeDayHint"
>
<CircleHelp class="h-3.5 w-3.5" />
</button>
<div
v-if="showSpikeDayHint"
role="tooltip"
class="absolute left-1/2 top-full z-20 mt-1.5 w-60 -translate-x-1/2 rounded-lg border border-default bg-surface px-3 py-2 text-xs leading-relaxed text-secondary shadow-elevated"
@click.stop
>
{{ t('admin.report.usageSpikeDaysHint', {
min: usageSpikeDayMinChats,
multiplier: usageSpikeDayAvgMultiplier,
limit: usageSpikeDayListLimit,
}) }}
</div>
</div>
</div>
<div v-if="usageSpikeDayItems.length" class="mt-2 space-y-2">
<div v-for="item in usageSpikeDayItems" :key="item.date" class="flex items-center justify-between gap-3 text-sm">
<span class="text-secondary">{{ item.date }}</span>
@@ -360,24 +439,36 @@ onMounted(() => {
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
<div class="rounded-xl border border-default bg-surface p-5">
<h4 class="text-sm font-bold text-muted uppercase tracking-wider mb-4">{{ t('admin.report.byAgent') }}</h4>
<div class="space-y-3">
<div class="rounded-xl border border-default bg-surface p-5 flex flex-col">
<div class="mb-4">
<h4 class="text-sm font-bold text-muted uppercase tracking-wider">{{ t('admin.report.byAgent') }}</h4>
<p class="mt-1 text-xs text-muted">{{ t('admin.report.usageByAgentHint') }}</p>
</div>
<div class="space-y-3 flex-1 min-h-0 max-h-80 overflow-y-auto">
<div v-for="a in usageTopAgentItems" :key="a.agent" class="flex items-center justify-between p-3 bg-surface-soft rounded-lg">
<span class="font-medium text-secondary truncate" :title="a.agent">{{ a.agent }}</span>
<span class="admin-report-badge admin-report-badge--info font-bold">{{ a.count }}</span>
<span class="admin-report-badge admin-report-badge--info font-bold shrink-0">{{ a.count }}</span>
</div>
<div v-if="usageTopAgentItems.length === 0" class="text-center text-muted text-sm py-6">
{{ t('admin.report.usageByAgentEmpty') }}
</div>
</div>
<PaginationBar
class="mt-4"
:page="usageAgentPage"
v-model:page-size="usageAgentPageSize"
:total="usageAgentTotal"
:pages="usageAgentTotalPages"
:page-size-options="[...usageAgentPageSizeOptions]"
@update:page="goToUsageAgentPage"
/>
</div>
<div class="rounded-xl border border-default bg-surface p-5 flex flex-col">
<div class="mb-4">
<h4 class="text-sm font-bold text-muted uppercase tracking-wider">{{ t('admin.report.byUser') }}</h4>
<p class="mt-1 text-xs text-muted">{{ t('admin.report.usageActiveUserHint') }}</p>
<p class="mt-1 text-xs text-muted">{{ t('admin.report.usageByUserTopHint', { limit: usageUserTopLimit }) }}</p>
</div>
<div class="space-y-3 flex-1">
<div class="space-y-3 flex-1 min-h-0 max-h-80 overflow-y-auto">
<div v-for="u in usageTopUserItems" :key="u.username" class="flex items-center justify-between gap-3 p-3 bg-surface-soft rounded-lg">
<span class="font-medium text-secondary truncate" :title="formatUsageUserLabel(u)">{{ formatUsageUserLabel(u) }}</span>
<span class="admin-report-badge admin-report-badge--info font-bold shrink-0">{{ u.count }}</span>
@@ -386,6 +477,15 @@ onMounted(() => {
{{ t('admin.report.usageByUserEmpty') }}
</div>
</div>
<PaginationBar
class="mt-4"
:page="usageUserPage"
v-model:page-size="usageUserPageSize"
:total="usageUserTotal"
:pages="usageUserTotalPages"
:page-size-options="[...usageUserPageSizeOptions]"
@update:page="goToUsageUserPage"
/>
</div>
</div>
</div>
@@ -452,28 +552,38 @@ onMounted(() => {
</div>
<div class="admin-report-table-wrap">
<table class="admin-report-table admin-report-table--app-ops w-full border-collapse">
<table class="admin-report-table admin-report-table--app-ops w-full table-fixed border-collapse">
<colgroup>
<col class="w-[14%]" />
<col class="w-[7%]" />
<col class="w-[7%]" />
<col class="w-[16%]" />
<col class="w-[7%]" />
<col class="w-[7%]" />
<col class="w-[8%]" />
<col class="w-[8%]" />
<col class="w-[8%]" />
<col class="w-[18%]" />
</colgroup>
<thead>
<tr>
<th
v-for="key in applicationSortableKeys"
:key="`app-h-${key}`"
class="px-3 py-3 whitespace-nowrap"
:class="isApplicationNumericKey(key) ? 'text-right' : 'text-left'"
class="px-3 py-3 text-left align-middle"
>
<button
type="button"
class="admin-report-app-ops-sort-btn"
:class="isApplicationNumericKey(key) ? 'justify-end' : 'justify-start'"
@click="onApplicationSortClick(key)"
>
<span>{{ t(`admin.report.appOps.columns.${key}`) }}</span>
<span class="admin-report-app-ops-sort-mark" aria-hidden="true">{{ getApplicationSortMark(key) || ' ' }}</span>
<span class="truncate">{{ t(`admin.report.appOps.columns.${key}`) }}</span>
<span class="admin-report-app-ops-sort-mark" aria-hidden="true">{{ getApplicationSortMark(key) || '' }}</span>
</button>
</th>
<th class="px-3 py-3 whitespace-nowrap text-left">{{ t('admin.report.appOps.columns.category') }}</th>
<th class="px-3 py-3 whitespace-nowrap text-left">{{ t('admin.report.appOps.columns.is_active') }}</th>
<th class="px-3 py-3 whitespace-nowrap text-left">{{ t('admin.report.appOps.columns.actions') }}</th>
<th class="px-3 py-3 text-left align-middle">{{ t('admin.report.appOps.columns.category') }}</th>
<th class="px-3 py-3 text-left align-middle">{{ t('admin.report.appOps.columns.is_active') }}</th>
<th class="px-3 py-3 text-left align-middle">{{ t('admin.report.appOps.columns.actions') }}</th>
</tr>
</thead>
<tbody>
@@ -483,25 +593,25 @@ onMounted(() => {
</td>
</tr>
<tr v-for="row in applicationItems" :key="row.agent_id">
<td class="px-3 py-3 font-medium text-secondary truncate max-w-[12rem]" :title="row.name">{{ row.name }}</td>
<td class="px-3 py-3 font-mono text-right tabular-nums">{{ row.chat_count }}</td>
<td class="px-3 py-3 font-mono text-right tabular-nums">{{ row.unique_users }}</td>
<td class="px-3 py-3 font-mono text-xs text-right whitespace-nowrap">
<td class="px-3 py-3 text-left align-middle font-medium text-secondary truncate" :title="row.name">{{ row.name }}</td>
<td class="px-3 py-3 text-left align-middle font-mono tabular-nums">{{ row.chat_count }}</td>
<td class="px-3 py-3 text-left align-middle font-mono tabular-nums">{{ row.unique_users }}</td>
<td class="px-3 py-3 text-left align-middle font-mono text-xs whitespace-nowrap">
{{ row.last_active_at ? formatLocalDateTime(row.last_active_at) : '-' }}
</td>
<td class="px-3 py-3 font-mono text-right tabular-nums">{{ row.authorized_users }}</td>
<td class="px-3 py-3 font-mono text-right tabular-nums">{{ row.pending_requests }}</td>
<td class="px-3 py-3 font-mono text-right tabular-nums whitespace-nowrap">
<td class="px-3 py-3 text-left align-middle font-mono tabular-nums">{{ row.authorized_users }}</td>
<td class="px-3 py-3 text-left align-middle font-mono tabular-nums">{{ row.pending_requests }}</td>
<td class="px-3 py-3 text-left align-middle font-mono tabular-nums whitespace-nowrap">
{{ row.avg_score == null ? '-' : row.avg_score }}
<span v-if="row.feedback_count" class="text-xs text-muted">({{ row.feedback_count }})</span>
</td>
<td class="px-3 py-3 text-secondary">{{ row.category || '-' }}</td>
<td class="px-3 py-3">
<td class="px-3 py-3 text-left align-middle text-secondary truncate">{{ row.category || '-' }}</td>
<td class="px-3 py-3 text-left align-middle">
<span class="admin-report-badge" :class="row.is_active ? 'admin-report-badge--info' : ''">
{{ row.is_active ? t('admin.report.appOps.statusActive') : t('admin.report.appOps.statusInactive') }}
</span>
</td>
<td class="px-3 py-3">
<td class="px-3 py-3 text-left align-middle">
<div class="flex flex-wrap gap-1">
<button type="button" class="btn-secondary text-xs px-2 py-1" @click="openApplicationChatLogs(row)">
{{ t('admin.report.appOps.actionLogs') }}
@@ -516,46 +626,14 @@ onMounted(() => {
</table>
</div>
<div class="flex flex-col gap-3 rounded-2xl border border-default bg-surface px-4 py-3 md:flex-row md:items-center md:justify-between">
<div class="text-sm text-secondary">
{{ t('admin.report.paginationSummary', { total: applicationTotal, page: applicationPage, pages: applicationTotalPages }) }}
</div>
<div class="flex flex-wrap items-center gap-2">
<label class="flex items-center gap-2 text-xs text-secondary">
<span>{{ t('admin.report.perPage') }}</span>
<select v-model.number="applicationPageSize" class="admin-table-filter-input appearance-none pl-2 pr-7 py-1 text-xs leading-5">
<option v-for="size in applicationPageSizeOptions" :key="`app-size-${size}`" :value="size">{{ size }}</option>
</select>
<span>{{ t('admin.report.itemsUnit') }}</span>
</label>
<button type="button" class="admin-report-page-btn" :disabled="applicationPage <= 1" @click="prevApplicationPage">
{{ t('admin.report.prevPage') }}
</button>
<button
v-for="btn in applicationPageButtons"
:key="`app-page-${btn}`"
type="button"
class="admin-report-page-btn"
:disabled="btn === -1"
:class="btn === -1
? 'border-transparent text-muted cursor-default opacity-100'
: btn === applicationPage
? 'admin-report-page-btn--active'
: ''"
@click="btn !== -1 && goToApplicationPage(btn)"
>
{{ btn === -1 ? '...' : btn }}
</button>
<button
type="button"
class="admin-report-page-btn"
:disabled="applicationPage >= applicationTotalPages"
@click="nextApplicationPage"
>
{{ t('admin.report.nextPage') }}
</button>
</div>
</div>
<PaginationBar
:page="applicationPage"
v-model:page-size="applicationPageSize"
:total="applicationTotal"
:pages="applicationTotalPages"
:page-size-options="[...applicationPageSizeOptions]"
@update:page="goToApplicationPage"
/>
</div>
<div
@@ -719,56 +797,15 @@ onMounted(() => {
</table>
</div>
<div v-if="showReportTable" class="flex flex-col gap-3 rounded-2xl border border-default bg-surface px-4 py-3 md:flex-row md:items-center md:justify-between">
<div class="text-sm text-secondary">
{{ t('admin.report.paginationSummary', { total: reportTotalRows, page: reportCurrentPage, pages: reportTotalPages }) }}
</div>
<div class="flex flex-wrap items-center gap-2">
<label class="flex items-center gap-2 text-xs text-secondary">
<span>{{ t('admin.report.perPage') }}</span>
<span class="relative inline-flex items-center">
<select
v-model.number="reportPageSize"
class="admin-table-filter-input appearance-none pl-2 pr-7 py-1 text-xs leading-5"
>
<option v-for="size in reportPageSizeOptions" :key="`page-size-${size}`" :value="size">{{ size }}</option>
</select>
<svg class="pointer-events-none absolute right-2 h-3.5 w-3.5 text-muted" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M5.23 7.21a.75.75 0 0 1 1.06.02L10 11.168l3.71-3.938a.75.75 0 1 1 1.08 1.04l-4.25 4.5a.75.75 0 0 1-1.08 0l-4.25-4.5a.75.75 0 0 1 .02-1.06Z" clip-rule="evenodd" />
</svg>
</span>
<span>{{ t('admin.report.itemsUnit') }}</span>
</label>
<button
@click="prevReportPage"
:disabled="reportCurrentPage <= 1"
class="admin-report-page-btn"
>
{{ t('admin.report.prevPage') }}
</button>
<button
v-for="btn in reportPageButtons"
:key="`report-page-btn-${btn}`"
:disabled="btn === -1"
@click="btn !== -1 && goToReportPage(btn)"
class="admin-report-page-btn"
:class="btn === -1
? 'border-transparent text-muted cursor-default opacity-100'
: btn === reportCurrentPage
? 'admin-report-page-btn--active'
: ''"
>
{{ btn === -1 ? '...' : btn }}
</button>
<button
@click="nextReportPage"
:disabled="reportCurrentPage >= reportTotalPages"
class="admin-report-page-btn"
>
{{ t('admin.report.nextPage') }}
</button>
</div>
</div>
<PaginationBar
v-if="showReportTable"
v-model:page="reportCurrentPage"
v-model:page-size="reportPageSize"
:total="reportTotalRows"
:pages="reportTotalPages"
:page-size-options="[...reportPageSizeOptions]"
@update:page="goToReportPage"
/>
</div>
</section>
@@ -2,7 +2,7 @@
* features/admin/modules/reports/composables/useReportsAdmin.ts
* 用途:管理后台报表(多表浏览、筛选、排序、chat_logs 批量删除、使用分析与应用运营看板)。
* 依赖:admin.api、useReportLayoutStorage、useOffsetPagination、useMessage、dateTime、vue-router。
* 逻辑:ReportsPage 调用本 composable;agents 表走应用运营专用接口,不再浏览原始配置。
* 逻辑:ReportsPage 调用本 composable;使用分析按用户服务端分页、按应用前端分页,均复用 PaginationBar。
*/
import { computed, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
@@ -88,6 +88,11 @@ export function useReportsAdmin() {
const usageRange = ref<UsageRangeKey>('30d');
const usageRangeOptions: UsageRangeKey[] = ['7d', '30d', '90d', '180d', '365d'];
/** 按应用统计:前端分页(后端已返回范围内全部应用) */
const usageAgentPage = ref(1);
const usageAgentPageSize = ref(10);
const usageAgentPageSizeOptions = [5, 10, 20, 50];
/** 应用运营看板状态(替换原 agents 配置表浏览) */
const applicationReport = ref<ApplicationReport | null>(null);
const applicationRange = ref<UsageRangeKey>('30d');
@@ -247,23 +252,49 @@ export function useReportsAdmin() {
const usageUserTopLimit = computed(() => usageReport.value?.user_top_limit ?? 50);
const usageActiveAgentCount = computed(() => usageReport.value?.by_agent.length ?? 0);
const usageTrendMax = computed(() => Math.max(1, ...(usageReport.value?.by_day || []).map((item) => item.count)));
/** 低使用应用:范围内对话次数 ≤ 该阈值 */
const USAGE_LOW_AGENT_MAX_CHATS = 2;
/** 低使用应用提醒最多展示条数 */
const USAGE_LOW_AGENT_LIST_LIMIT = 5;
/** 异常高峰:单日对话量下限(与日均倍数取较大者) */
const USAGE_SPIKE_DAY_MIN_CHATS = 5;
/** 异常高峰:相对日均的倍数阈值 */
const USAGE_SPIKE_DAY_AVG_MULTIPLIER = 2;
/** 异常高峰日期最多展示条数 */
const USAGE_SPIKE_DAY_LIST_LIMIT = 3;
const usageLowAgentItems = computed(() =>
[...(usageReport.value?.by_agent || [])]
.filter((item) => item.count <= 2)
.filter((item) => item.count <= USAGE_LOW_AGENT_MAX_CHATS)
.sort((a, b) => a.count - b.count || a.agent.localeCompare(b.agent))
.slice(0, 5),
.slice(0, USAGE_LOW_AGENT_LIST_LIMIT),
);
const usageSpikeDayItems = computed(() => {
const days = usageReport.value?.by_day || [];
if (days.length === 0) return [];
const average = days.reduce((sum, item) => sum + item.count, 0) / days.length;
// 高峰阈值:不低于下限,且不低于日均 × 倍数
const threshold = Math.max(USAGE_SPIKE_DAY_MIN_CHATS, average * USAGE_SPIKE_DAY_AVG_MULTIPLIER);
return days
.filter((item) => item.count >= Math.max(5, average * 2))
.filter((item) => item.count >= threshold)
.sort((a, b) => b.count - a.count)
.slice(0, 3);
.slice(0, USAGE_SPIKE_DAY_LIST_LIMIT);
});
const usageTopUserItems = computed(() => usageReport.value?.by_user ?? []);
const usageAgentTotal = computed(() => usageReport.value?.by_agent.length ?? 0);
const usageAgentTotalPages = computed(() => {
const size = Math.max(1, Number(usageAgentPageSize.value) || 10);
return Math.max(1, Math.ceil(usageAgentTotal.value / size));
});
/**
* 按应用统计当前页数据(对话量降序全集上的偏移分页)。
*/
const usageTopAgentItems = computed(() => {
const list = usageReport.value?.by_agent ?? [];
const size = Math.max(1, Number(usageAgentPageSize.value) || 10);
const page = Math.min(Math.max(1, usageAgentPage.value), usageAgentTotalPages.value);
const start = (page - 1) * size;
return list.slice(start, start + size);
});
const usageTopUserItems = computed(() => usageReport.value?.by_user.slice(0, 3) ?? []);
const usageTopAgentItems = computed(() => usageReport.value?.by_agent.slice(0, 5) ?? []);
const usageUserPageButtons = computed(() =>
buildOffsetPageButtons(usageUserPage.value, usageUserTotalPages.value),
);
@@ -275,6 +306,14 @@ export function useReportsAdmin() {
const prevUsageUserPage = () => goToUsageUserPage(usageUserPage.value - 1);
const nextUsageUserPage = () => goToUsageUserPage(usageUserPage.value + 1);
/**
* 跳转按应用统计分页(纯前端切片,无需重新请求)。
* @param page - 目标页码
*/
const goToUsageAgentPage = (page: number): void => {
usageAgentPage.value = Math.min(Math.max(1, page), usageAgentTotalPages.value);
};
const loadReports = async (): Promise<void> => {
await loadUsageReport();
};
@@ -1063,9 +1102,20 @@ watch(usageUserPageSize, () => {
watch(usageRange, () => {
usageUserPage.value = 1;
usageAgentPage.value = 1;
void loadUsageReport();
});
watch(usageAgentPageSize, () => {
usageAgentPage.value = 1;
});
watch(usageAgentTotalPages, (pages) => {
if (usageAgentPage.value > pages) {
usageAgentPage.value = pages;
}
});
watch([applicationRange, applicationStatus, applicationUsage, applicationPageSize], () => {
applicationPage.value = 1;
if (isApplicationOpsTable.value) {
@@ -1112,10 +1162,21 @@ watch([reportPageSize, sortedTableData], () => {
usageUserTopLimit,
usageActiveAgentCount,
usageTrendMax,
usageLowAgentMaxChats: USAGE_LOW_AGENT_MAX_CHATS,
usageLowAgentListLimit: USAGE_LOW_AGENT_LIST_LIMIT,
usageSpikeDayMinChats: USAGE_SPIKE_DAY_MIN_CHATS,
usageSpikeDayAvgMultiplier: USAGE_SPIKE_DAY_AVG_MULTIPLIER,
usageSpikeDayListLimit: USAGE_SPIKE_DAY_LIST_LIMIT,
usageLowAgentItems,
usageSpikeDayItems,
usageTopUserItems,
usageTopAgentItems,
usageAgentPage,
usageAgentPageSize,
usageAgentPageSizeOptions,
usageAgentTotal,
usageAgentTotalPages,
goToUsageAgentPage,
usageUserPageButtons,
goToUsageUserPage,
prevUsageUserPage,