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,9 +1,11 @@
/**
* features/admin/components/AdminNav.vue
* 管理后台左侧导航,使用子路由跳转。
* 依赖:admin-nav.store、modules 常量、vue-router。
* 逻辑:报表/企微子菜单默认折叠;当前路由落在对应模块时自动展开以便看到激活项。
*/
<script setup lang="ts">
import { computed } from 'vue';
import { computed, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import {
@@ -76,6 +78,25 @@ const navigateModule = (routeName: string): void => {
const navigateReport = (table: string): void => {
router.push({ name: 'AdminReports', params: { table } });
};
/**
* 进入报表/企微模块时自动展开子菜单,避免当前页在折叠状态下不可见。
*/
watch(
isReportsActive,
(active) => {
if (active) navStore.reportsExpanded = true;
},
{ immediate: true },
);
watch(
isWecomActive,
(active) => {
if (active) navStore.wecomExpanded = true;
},
{ immediate: true },
);
</script>
<template>
@@ -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,
@@ -1,9 +1,10 @@
/**
* features/admin/stores/admin-nav.store.ts
* 管理后台导航 UI 状态(侧栏折叠、报表展开、角标)。
* 管理后台导航 UI 状态(侧栏折叠、报表/企微子菜单展开、角标)。
*
* 角标数据来自 GET /api/admin/dashboard/badges,在 store 内做 TTL 缓存与 in-flight 去重,
* 避免切换 admin 子路由时重复请求(AdminLayout 挂载一次即可)。
* 报表与企业微信子菜单默认折叠,进入对应模块时由 AdminNav 展开。
*/
import { defineStore } from 'pinia';
import { ref } from 'vue';
@@ -15,8 +16,10 @@ const BADGE_CACHE_TTL_MS = 60_000;
export const useAdminNavStore = defineStore('adminNav', () => {
const sidebarCollapsed = ref(false);
const reportsExpanded = ref(true);
const wecomExpanded = ref(true);
/** 数据报表子菜单:默认折叠 */
const reportsExpanded = ref(false);
/** 企业微信子菜单:默认折叠 */
const wecomExpanded = ref(false);
/** 「用户审批」导航角标:pending 注册用户数 */
const pendingApprovalsCount = ref(0);
/** 「权限申请」导航角标:pending 权限申请数 */
@@ -1,16 +1,23 @@
/**
* features/kb/utils/kb-display.utils.ts
* 文档标题、时间、文件大小与状态徽标等纯展示工具。
* 用途:文档标题、时间、文件大小与状态徽标等纯展示工具。
* 依赖:kb.api 类型、kb-i18n.types、shared/utils/dateTime(UTC 时间戳本地化)。
* 逻辑:KbWorkspace / KbAdminPanel 等调用;时间统一走 formatLocalDateTime,避免无时区后缀被当成本地时间导致差 8 小时。
*/
import { formatLocalDateTime } from '@/shared/utils/dateTime';
import type { KbDocumentItem } from '../api/kb.api';
import type { KbTranslateFn } from './kb-i18n.types';
/**
* 格式化知识库时间戳为本地时区 `YYYY-MM-DD HH:mm:ss`。
* 核心逻辑:委托 formatLocalDateTime;后端 isoformat() 常无 Z,按 UTC 解析后再转浏览器本地时区。
* @param value - API 返回的时间字符串
* @returns 本地展示文案;空值返回 `--`
*/
export const formatDateTime = (value?: string | null) => {
if (!value) return '--';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
const pad = (n: number) => String(n).padStart(2, '0');
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
const formatted = formatLocalDateTime(value);
return formatted === '-' ? '--' : formatted;
};
export const formatFileSize = (value?: number | null) => {
+6 -1
View File
@@ -908,11 +908,16 @@
"usageTrendEmpty": "No trend data yet",
"usageInsights": "Operational alerts",
"usageLowAgents": "Low-usage apps",
"usageLowAgentsHint": "Apps with ≤ {max} chats in the selected range, sorted ascending, showing up to {limit}.",
"usageLowAgentsHintAria": "Show low-usage app selection rule",
"usageLowAgentsEmpty": "No low-usage apps in this range",
"usageSpikeDays": "Spike days",
"usageSpikeDaysHint": "A day is a spike when chats ≥ max({min}, daily average × {multiplier}); sorted descending, showing up to {limit} days.",
"usageSpikeDaysHintAria": "Show spike day selection rule",
"usageSpikeDaysEmpty": "No clear spikes in this range",
"usageByAgentEmpty": "No app chat activity yet",
"usageActiveUserHint": "Showing the top 3 active users on this page",
"usageByAgentHint": "All apps with chats in range, with pagination",
"usageActiveUserHint": "Top 50 users by chat volume, with pagination",
"appOps": {
"title": "App Operations",
"intro": "Per-app usage, authorization, pending requests, and ratings — with drill-down to chat logs and permission requests.",
+6 -1
View File
@@ -892,11 +892,16 @@
"usageTrendEmpty": "ยังไม่มีข้อมูลแนวโน้ม",
"usageInsights": "การแจ้งเตือนการดำเนินงาน",
"usageLowAgents": "แอปที่ใช้น้อย",
"usageLowAgentsHint": "แอปที่มีแชท ≤ {max} ครั้งในช่วงที่เลือก เรียงจากน้อยไปมาก แสดงสูงสุด {limit} รายการ",
"usageLowAgentsHintAria": "ดูเกณฑ์การคัดเลือกแอปที่ใช้น้อย",
"usageLowAgentsEmpty": "ไม่มีแอปที่ใช้น้อยในช่วงนี้",
"usageSpikeDays": "วันที่มีการใช้งานพุ่งสูง",
"usageSpikeDaysHint": "ถือว่าเป็นพีคเมื่อแชทต่อวัน ≥ max({min}, ค่าเฉลี่ยรายวัน × {multiplier}) เรียงจากมากไปน้อย แสดงสูงสุด {limit} วัน",
"usageSpikeDaysHintAria": "ดูเกณฑ์การคัดเลือกวันพีค",
"usageSpikeDaysEmpty": "ไม่พบช่วงพีคที่ชัดเจนในช่วงนี้",
"usageByAgentEmpty": "ยังไม่มีประวัติแชทของแอป",
"usageActiveUserHint": "แสดงผู้ใช้ที่ใช้งานสูงสุด 3 อันดับในหน้านี้",
"usageByAgentHint": "แสดงแอปทั้งหมดที่มีแชทในช่วงนี้ พร้อมแบ่งหน้า",
"usageActiveUserHint": "ผู้ใช้ 50 อันดับแรกตามปริมาณแชท พร้อมแบ่งหน้า",
"appOps": {
"title": "แอปปฏิบัติการ",
"intro": "ดูปริมาณการใช้ สิทธิ์ คำขอที่รอ และคะแนนรายแอป พร้อมเจาะลงบันทึกแชทและคำขอสิทธิ์",
+6 -1
View File
@@ -888,11 +888,16 @@
"usageTrendEmpty": "暂无趋势数据",
"usageInsights": "运营提醒",
"usageLowAgents": "低使用应用",
"usageLowAgentsHint": "统计范围内对话次数 ≤ {max} 的应用,按对话量升序最多展示 {limit} 个。",
"usageLowAgentsHintAria": "查看低使用应用选取规则",
"usageLowAgentsEmpty": "当前范围内暂无低使用应用",
"usageSpikeDays": "异常高峰日期",
"usageSpikeDaysHint": "单日对话量 ≥ max({min}, 日均 × {multiplier}) 视为高峰,按对话量降序最多展示 {limit} 天。",
"usageSpikeDaysHintAria": "查看异常高峰日期选取规则",
"usageSpikeDaysEmpty": "当前范围内未发现明显高峰",
"usageByAgentEmpty": "暂无应用对话记录",
"usageActiveUserHint": "展示当前页活跃用户前三名",
"usageByAgentHint": "展示范围内全部有对话的应用,支持翻页",
"usageActiveUserHint": "对话量前 50 名用户,支持翻页",
"appOps": {
"title": "应用运营",
"intro": "按应用查看使用量、授权、待审与评分,支持下钻聊天日志与权限申请。",
+6 -1
View File
@@ -892,11 +892,16 @@
"usageTrendEmpty": "暫無趨勢資料",
"usageInsights": "營運提醒",
"usageLowAgents": "低使用應用",
"usageLowAgentsHint": "統計範圍內對話次數 ≤ {max} 的應用,按對話量升序最多展示 {limit} 個。",
"usageLowAgentsHintAria": "查看低使用應用選取規則",
"usageLowAgentsEmpty": "目前範圍內暫無低使用應用",
"usageSpikeDays": "異常高峰日期",
"usageSpikeDaysHint": "單日對話量 ≥ max({min}, 日均 × {multiplier}) 視為高峰,按對話量降序最多展示 {limit} 天。",
"usageSpikeDaysHintAria": "查看異常高峰日期選取規則",
"usageSpikeDaysEmpty": "目前範圍內未發現明顯高峰",
"usageByAgentEmpty": "暫無應用對話記錄",
"usageActiveUserHint": "顯示目前頁活躍使用者前三名",
"usageByAgentHint": "展示範圍內全部有對話的應用,支援翻頁",
"usageActiveUserHint": "對話量前 50 名使用者,支援翻頁",
"appOps": {
"title": "應用運營",
"intro": "按應用查看使用量、授權、待審與評分,支援下鑽聊天日誌與權限申請。",
+4 -4
View File
@@ -467,22 +467,22 @@
@apply text-left transition-colors text-secondary hover:text-brand-600;
}
/* 应用运营表:取消 uppercase/tracking,避免中文表头被拉开导致与数据列视觉错位 */
/* 应用运营表:取消 uppercase/tracking;表头与单元格统一左对齐,排序箭头不挤占文案起点 */
.admin-report-table--app-ops thead {
@apply normal-case tracking-normal;
}
.admin-report-table--app-ops thead th,
.admin-report-table--app-ops tbody td {
@apply align-middle;
@apply align-middle text-left;
}
.admin-report-app-ops-sort-btn {
@apply inline-flex w-full items-center gap-1 text-xs font-semibold text-secondary transition-colors hover:text-brand-600;
@apply inline-flex max-w-full items-center justify-start gap-0.5 text-left text-xs font-semibold text-secondary transition-colors hover:text-brand-600;
}
.admin-report-app-ops-sort-mark {
@apply inline-block w-2.5 shrink-0 text-[10px] text-brand-500;
@apply inline-block w-2.5 shrink-0 text-left text-[10px] leading-none text-brand-500;
}
.admin-report-page-btn {