fix(center): 数据看板与概览补齐「已取消」运行统计

- 壳子上报 start + 终态(done/error/cancelled),统计只认 start 作总数、done 作成功、error 作失败,cancelled 三类都不进,导致 成功+失败 ≠ 总数

- /api/admin/stats:新增 runs.cancelled / runs.triggered,total 改为终态记录数,failed 纳入 running(未收尾中断),trend 与 topPlugins 补 cancelled;成功+失败+取消严格等于总数

- 数据看板:hero 加取消徽标与口径说明,运行构成环图三段,趋势加取消线,插件排行显示取消数;概览运行卡片扩为 5 张(含已取消)

- 成功率口径不变:success/(success+failed),取消不进分母
This commit is contained in:
2026-11-06 15:40:48 +08:00
parent 8738620065
commit 619ac1fe81
3 changed files with 87 additions and 30 deletions
@@ -38,7 +38,8 @@ const trendData = computed(() => {
return [
{ name: '触发', data: trend.map((t: any) => t.runs), showAreaColor: true },
{ name: '成功', data: trend.map((t: any) => t.success) },
{ name: '失败', data: trend.map((t: any) => t.failed) }
{ name: '失败', data: trend.map((t: any) => t.failed) },
{ name: '取消', data: trend.map((t: any) => t.cancelled) }
]
})
@@ -101,24 +102,25 @@ onMounted(load)
</el-col>
</el-row>
<!-- 运行统计 -->
<el-row :gutter="14" class="mb-3.5">
<el-col :xs="12" :sm="12" :md="6" v-for="(c, i) in [
{ title: '运行总次数', count: stats.runs?.total || 0, icon: 'ri:play-circle-line', iconStyle: 'bg-[#377dff]', desc: '全部触发' },
{ title: '成功', count: stats.runs?.success || 0, icon: 'ri:checkbox-circle-line', iconStyle: 'bg-[#00b42a]', desc: 'done / success' },
{ title: '失败', count: stats.runs?.failed || 0, icon: 'ri:close-circle-line', iconStyle: 'bg-[#f53f3f]', desc: 'error / failed' },
{ title: '活跃用户', count: stats.runs?.activeUsers || 0, icon: 'ri:user-heart-line', iconStyle: 'bg-[#ff7d00]', desc: '有触发记录' }
]" :key="i">
<ArtStatsCard
:title="c.title"
:count="c.count"
:icon="c.icon"
:icon-style="c.iconStyle"
:description="c.desc"
class="rounded-xl mb-3.5"
/>
</el-col>
</el-row>
<!-- 运行统计(总次数 = 成功 + 失败 + 取消,口径已闭合) -->
<div class="mb-3.5 grid grid-cols-2 gap-3.5 md:grid-cols-3 xl:grid-cols-5">
<ArtStatsCard
v-for="(c, i) in [
{ title: '运行总次数', count: stats.runs?.total || 0, icon: 'ri:play-circle-line', iconStyle: 'bg-[#377dff]', desc: '成功+失败+取消' },
{ title: '成功', count: stats.runs?.success || 0, icon: 'ri:checkbox-circle-line', iconStyle: 'bg-[#00b42a]', desc: 'done / success' },
{ title: '失败', count: stats.runs?.failed || 0, icon: 'ri:close-circle-line', iconStyle: 'bg-[#f53f3f]', desc: 'error / failed' },
{ title: '已取消', count: stats.runs?.cancelled || 0, icon: 'ri:stop-circle-line', iconStyle: 'bg-[#8a8f99]', desc: 'cancelled(用户停止/排队取消)' },
{ title: '活跃用户', count: stats.runs?.activeUsers || 0, icon: 'ri:user-heart-line', iconStyle: 'bg-[#ff7d00]', desc: '有触发记录' }
]"
:key="i"
:title="c.title"
:count="c.count"
:icon="c.icon"
:icon-style="c.iconStyle"
:description="c.desc"
class="rounded-xl"
/>
</div>
<!-- 图表区 -->
<el-row :gutter="14" class="mb-3.5">
@@ -16,14 +16,20 @@
return [
{ name: '触发', data: trend.map((t: any) => t.runs), showAreaColor: true },
{ name: '成功', data: trend.map((t: any) => t.success) },
{ name: '失败', data: trend.map((t: any) => t.failed) }
{ name: '失败', data: trend.map((t: any) => t.failed) },
{ name: '取消', data: trend.map((t: any) => t.cancelled) }
]
})
const trendX = computed(() => (data.value.trend || []).map((t: any) => t.day))
/** 运行总次数 = 终态记录数(成功+失败+取消),三者相加严格等于它 */
const totalRuns = computed(() => data.value.runs?.total || 0)
const triggeredRuns = computed(() => data.value.runs?.triggered || 0)
const successCount = computed(() => data.value.runs?.success || 0)
const failedCount = computed(() => data.value.runs?.failed || 0)
/** 用户取消(强制停止 / 排队取消 / 排队超时 / 客户端退出)不算失败 */
const cancelledCount = computed(() => data.value.runs?.cancelled || 0)
/** 成功率分母:只算成功+失败,取消不进分母(壳子上报口径) */
const finishedCount = computed(() => successCount.value + failedCount.value)
const successRate = computed(() => {
@@ -38,7 +44,8 @@
const ringData = computed(() => [
{ name: '成功', value: successCount.value },
{ name: '失败', value: failedCount.value }
{ name: '失败', value: failedCount.value },
{ name: '取消', value: cancelledCount.value }
])
const avgDuration = computed(() => {
@@ -156,10 +163,16 @@
<span class="hero-chip">
<i class="dot bg-[#ffb3b3]"></i>失败 {{ fmtNum(failedCount) }}
</span>
<span class="hero-chip">
<i class="dot bg-[#ffd591]"></i>取消 {{ fmtNum(cancelledCount) }}
</span>
<span class="hero-chip">
<i class="dot bg-white/60"></i>平均耗时 {{ avgDuration }}
</span>
</div>
<div class="mt-2 text-[11px] text-white/60">
总次数 = 成功 + 失败 + 取消;触发上报 {{ fmtNum(triggeredRuns) }} 次(含排队取消、仍在运行)
</div>
</div>
<div class="shrink-0">
@@ -213,7 +226,7 @@
<div>
<span class="font-semibold">近 14 天运行趋势</span>
<span class="ml-2 text-xs text-g-400"
>触发 = start · 成功 = done/success · 失败 = error/failed</span
>触发 = start · 成功 = done/success · 失败 = error/failed · 取消 = cancelled</span
>
</div>
<span class="text-xs text-g-400">近 14 天</span>
@@ -236,7 +249,7 @@
<div class="relative">
<ArtRingChart
:data="ringData"
:colors="['#00b42a', '#f53f3f']"
:colors="['#00b42a', '#f53f3f', '#8a8f99']"
:radius="['56%', '80%']"
:border-radius="8"
height="180px"
@@ -254,7 +267,7 @@
<span class="font-medium text-g-900">
{{ fmtNum(successCount) }}
<span class="ml-1 text-xs font-normal text-g-400">
{{ finishedCount ? Math.round((successCount / finishedCount) * 100) : 0 }}%
{{ totalRuns ? Math.round((successCount / totalRuns) * 100) : 0 }}%
</span>
</span>
</div>
@@ -263,7 +276,16 @@
<span class="font-medium text-g-900">
{{ fmtNum(failedCount) }}
<span class="ml-1 text-xs font-normal text-g-400">
{{ finishedCount ? Math.round((failedCount / finishedCount) * 100) : 0 }}%
{{ totalRuns ? Math.round((failedCount / totalRuns) * 100) : 0 }}%
</span>
</span>
</div>
<div class="flex-cb text-sm">
<span class="flex-c gap-2 text-g-500"> <i class="dot bg-[#8a8f99]"></i>取消 </span>
<span class="font-medium text-g-900">
{{ fmtNum(cancelledCount) }}
<span class="ml-1 text-xs font-normal text-g-400">
{{ totalRuns ? Math.round((cancelledCount / totalRuns) * 100) : 0 }}%
</span>
</span>
</div>
@@ -312,6 +334,10 @@
<b :class="p.failed ? 'text-[var(--el-color-danger)]' : 'text-g-400'">
{{ fmtNum(p.failed) }}
</b>
· 取消
<b :class="p.cancelled ? 'text-[#8a8f99]' : 'text-g-400'">
{{ fmtNum(p.cancelled) }}
</b>
</span>
</div>
<div class="mt-1.5 h-1.5 overflow-hidden rounded-full bg-[var(--el-fill-color)]">
@@ -105,16 +105,21 @@ export class AdminOpsController extends BaseController {
(SELECT COUNT(*) FROM plugins WHERE status='revoked') AS revoked`))[0];
const runs: any = (await m.query(
`SELECT (SELECT COUNT(*) FROM runtime_reports WHERE status='start') AS started,
(SELECT COUNT(*) FROM runtime_reports WHERE status <> 'start') AS finished,
(SELECT COUNT(*) FROM runtime_reports WHERE status IN ('done','success')) AS success,
(SELECT COUNT(*) FROM runtime_reports WHERE status IN ('error','failed')) AS failed,
(SELECT COUNT(*) FROM runtime_reports
WHERE status IN ('error','failed','running')) AS failed,
(SELECT COUNT(*) FROM runtime_reports WHERE status IN ('cancel','cancelled','canceled')) AS cancelled,
(SELECT COALESCE(SUM(duration_sec),0) FROM runtime_reports) AS duration_sec,
(SELECT COUNT(DISTINCT account) FROM runtime_reports WHERE account <> '') AS active_users,
(SELECT COUNT(DISTINCT plugin_id) FROM runtime_reports WHERE plugin_id <> '') AS active_plugins`))[0];
// 取消(用户强制停止 / 排队取消 / 排队超时 / 客户端退出)也落终态,必须计入构成,否则 总数 ≠ 成功+失败
const trendRows: any[] = await m.query(
`SELECT LEFT(at,10) AS day,
SUM(CASE WHEN status='start' THEN 1 ELSE 0 END) AS runs,
SUM(CASE WHEN status IN ('done','success') THEN 1 ELSE 0 END) AS success,
SUM(CASE WHEN status IN ('error','failed') THEN 1 ELSE 0 END) AS failed
SUM(CASE WHEN status IN ('error','failed','running') THEN 1 ELSE 0 END) AS failed,
SUM(CASE WHEN status IN ('cancel','cancelled','canceled') THEN 1 ELSE 0 END) AS cancelled
FROM runtime_reports WHERE at >= CURDATE() - INTERVAL 13 DAY GROUP BY day ORDER BY day`);
const trendMap: Record<string, any> = {};
for (const r of trendRows) trendMap[r.day] = r;
@@ -124,13 +129,20 @@ export class AdminOpsController extends BaseController {
const day = new Date(d.getTime() - i * 86400000);
const key = `${day.getFullYear()}-${String(day.getMonth() + 1).padStart(2, '0')}-${String(day.getDate()).padStart(2, '0')}`;
const r = trendMap[key];
trend.push({ day: key.slice(5), runs: Number(r?.runs || 0), success: Number(r?.success || 0), failed: Number(r?.failed || 0) });
trend.push({
day: key.slice(5),
runs: Number(r?.runs || 0),
success: Number(r?.success || 0),
failed: Number(r?.failed || 0),
cancelled: Number(r?.cancelled || 0),
});
}
const topPlugins: any[] = await m.query(
`SELECT plugin_id AS pluginId, COUNT(*) AS runs,
SUM(CASE WHEN status='start' THEN 1 ELSE 0 END) AS triggered,
SUM(CASE WHEN status IN ('done','success') THEN 1 ELSE 0 END) AS success,
SUM(CASE WHEN status IN ('error','failed') THEN 1 ELSE 0 END) AS failed,
SUM(CASE WHEN status IN ('cancel','cancelled','canceled') THEN 1 ELSE 0 END) AS cancelled,
COALESCE(SUM(duration_sec),0) AS durationSec
FROM runtime_reports GROUP BY plugin_id ORDER BY triggered DESC LIMIT 8`);
const pnames: Record<string, string> = {};
@@ -143,9 +155,26 @@ export class AdminOpsController extends BaseController {
return {
users: { total: Number(users.total || 0), admins: Number(users.admins || 0), authorized: Number(users.authorized || 0) },
plugins: { total: Number(plugins.total || 0), published: Number(plugins.published || 0), testing: Number(plugins.testing || 0), disabled: Number(plugins.disabled || 0), revoked: Number(plugins.revoked || 0) },
runs: { total: Number(runs.started || 0), success: Number(runs.success || 0), failed: Number(runs.failed || 0), durationSec: Number(runs.duration_sec || 0), activeUsers: Number(runs.active_users || 0), activePlugins: Number(runs.active_plugins || 0) },
runs: {
// total = 终态记录数(成功+失败+取消),保证三者相加严格等于总数;triggered = 开始上报数(含正在跑/排队取消没开始的)
total: Number(runs.finished || 0),
triggered: Number(runs.started || 0),
success: Number(runs.success || 0),
failed: Number(runs.failed || 0),
cancelled: Number(runs.cancelled || 0),
durationSec: Number(runs.duration_sec || 0),
activeUsers: Number(runs.active_users || 0),
activePlugins: Number(runs.active_plugins || 0),
},
trend,
topPlugins: topPlugins.map((tp: any) => ({ ...tp, name: pnames[tp.pluginId] || tp.pluginId, runs: Number(tp.triggered || 0) })),
topPlugins: topPlugins.map((tp: any) => ({
...tp,
name: pnames[tp.pluginId] || tp.pluginId,
runs: Number(tp.triggered || 0),
success: Number(tp.success || 0),
failed: Number(tp.failed || 0),
cancelled: Number(tp.cancelled || 0),
})),
topUsers: topUsers.map((tu: any) => ({ account: tu.account, runs: Number(tu.runs || 0), nickname: nick[tu.account] || '' })),
today: d.toISOString().slice(0, 10),
};