chore(merge): 合并运营大盘 ECharts 图表

This commit is contained in:
Zhou Weidong authored and trae-solo-agent committed 2026-10-23 14:58:56 +08:00
commit ada74efc3a
2 files changed
+506 -129

No files matched your search

+30
View File
@@ -27,6 +27,28 @@ export interface TranscodeBoard {
notes: string[]
}
/** 近 7 天每日趋势(给折线图用)。 */
export interface DashboardTrend {
dates: string[]
newUsers: number[]
playEvents: number[]
newTranscodes: number[]
transcodeSuccess: number[]
}
/** 分布项(给饼图用)。 */
export interface DistributionItem {
label: string
value: number
}
export interface DashboardDistribution {
dramaStatus: Record<string, number>
transcodeStatus: Record<string, number>
episodeMedia: Record<string, number>
pendingModeration: DistributionItem[]
}
export const getDashboardOverviewApi = () => {
return request.get<DashboardOverview>({ url: '/admin/dashboard/overview' })
}
@@ -34,3 +56,11 @@ export const getDashboardOverviewApi = () => {
export const getTranscodeBoardApi = () => {
return request.get<TranscodeBoard>({ url: '/admin/dashboard/transcode' })
}
export const getDashboardTrendApi = () => {
return request.get<DashboardTrend>({ url: '/admin/dashboard/trend' })
}
export const getDashboardDistributionApi = () => {
return request.get<DashboardDistribution>({ url: '/admin/dashboard/distribution' })
}
+476 -129
View File
@@ -1,145 +1,385 @@
<!-- Author: fruit.com.cn -->
<script setup>
import { onMounted, ref } from 'vue'
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import PageHeading from '@/views/components/PageHeading.vue'
import { getDashboardOverviewApi, getTranscodeBoardApi } from '@/api/admin/dashboard'
import { dateCell } from '@/utils/format'
import echarts from '@/plugins/echarts'
import {
getDashboardDistributionApi,
getDashboardOverviewApi,
getDashboardTrendApi,
getTranscodeBoardApi
} from '@/api/admin/dashboard'
import type {
DashboardDistribution,
DashboardOverview,
DashboardTrend,
TranscodeBoard
} from '@/api/admin/dashboard'
import { dateCell, formatCount } from '@/utils/format'
/**
* 指标展示配置:key 与后端 /admin/dashboard/overview 返回的字段一一对应,
* 不在前端编字段;口径说明见后端 DashboardService 注释与 docs/api-phase1.md「2.8」。
*/
const GROUPS = [
{
key: 'user',
title: '用户',
metrics: [
{ key: 'total', label: '总用户数' },
{ key: 'newToday', label: '今日新增' },
{ key: 'new7d', label: '近 7 天新增' },
{ key: 'active7d', label: '近 7 天活跃' },
{ key: 'banned', label: '封禁' },
{ key: 'limited', label: '受限' },
{ key: 'deactivated', label: '已注销' }
]
},
{
key: 'content',
title: '内容',
metrics: [
{ key: 'dramaTotal', label: '短剧总数' },
{ key: 'dramaPublished', label: '已上线短剧' },
{ key: 'dramaReviewing', label: '待审短剧' },
{ key: 'dramaDraft', label: '草稿短剧' },
{ key: 'dramaOffline', label: '已下线短剧' },
{ key: 'episodeTotal', label: '剧集总数' },
{ key: 'episodePublished', label: '已上线剧集' },
{ key: 'episodeWithMedia', label: '有片源剧集' },
{ key: 'commentPending', label: '待审评论' },
{ key: 'reportPending', label: '待处理举报' }
]
},
{
key: 'playback',
title: '播放',
metrics: [
{ key: 'viewTotal', label: '累计观看' },
{ key: 'playTotal', label: '累计播放' },
{ key: 'watchedDramas', label: '看过的剧(去重)' },
{ key: 'finishedDramas', label: '看过且完播' },
{ key: 'finishRate', label: '完播率' },
{ key: 'playEvents7d', label: '近 7 天播放流水' },
{ key: 'playEventsTotal', label: '播放流水总量' }
]
},
{
key: 'revenue',
title: '收入',
metrics: [
{ key: 'orderTotal', label: '订单总数' },
{ key: 'orderDelivered', label: '已交付订单' },
{ key: 'orderRefunded', label: '已退款订单' },
{ key: 'paidAmount', label: '已支付金额' },
{ key: 'coinRecharged', label: '金币充值' },
{ key: 'coinConsumed', label: '金币消耗' },
{ key: 'coinBalance', label: '金币余额' },
{ key: 'entitlementActive', label: '有效权益' }
]
},
{
key: 'cost',
title: '成本',
metrics: [
{ key: 'transcodeQueued', label: '转码排队' },
{ key: 'transcodeRunning', label: '转码中' },
{ key: 'transcodeFailed', label: '转码失败' },
{ key: 'transcodeSuccess', label: '转码成功' },
{ key: 'renditions', label: '多码率产物' },
{ key: 'otpSent7d', label: '近 7 天验证码' },
{ key: 'cdnTrafficBytes', label: 'CDN 流量' }
]
}
const HIGHLIGHT_CARDS = [
{ group: 'user', key: 'total', label: '总用户数', color: '#5B8FF9' },
{ group: 'user', key: 'newToday', label: '今日新增', color: '#5AD8A6' },
{ group: 'content', key: 'dramaPublished', label: '已上线短剧', color: '#F6BD16' },
{ group: 'content', key: 'episodeWithMedia', label: '有片源剧集', color: '#E86452' },
{ group: 'playback', key: 'playTotal', label: '累计播放', color: '#6DC8EC' },
{ group: 'playback', key: 'playEvents7d', label: '近 7 天播放', color: '#945FB9' },
{ group: 'cost', key: 'transcodeRunning', label: '转码中', color: '#FF9845' },
{ group: 'cost', key: 'transcodeQueued', label: '转码排队', color: '#1E9493' }
]
const loading = ref(false)
const overview = ref(null)
const transcode = ref(null)
const overview = ref<DashboardOverview | null>(null)
const trend = ref<DashboardTrend | null>(null)
const distribution = ref<DashboardDistribution | null>(null)
const transcode = ref<TranscodeBoard | null>(null)
const lastUpdated = ref('')
/** ECharts 容器 ref 与实例,统一管理 resize / dispose。 */
const trendRef = ref<HTMLElement>()
const dramaPieRef = ref<HTMLElement>()
const transcodePieRef = ref<HTMLElement>()
const episodePieRef = ref<HTMLElement>()
const chartInstances: echarts.ECharts[] = []
const statusLabel = {
reviewing: '待审',
published: '已上线',
draft: '草稿',
offline: '已下线'
} as const
const transcodeStatusLabel = {
QUEUED: '排队中',
RUNNING: '执行中',
SUCCESS: '成功',
FAILED: '失败',
CANCELED: '已取消'
} as const
function groupValue(groupKey: string, metricKey: string) {
const g = overview.value?.[groupKey as keyof DashboardOverview] as
| Record<string, number>
| undefined
return g?.[metricKey]
}
function display(value: unknown) {
if (value === null || value === undefined) return '暂无数据源'
if (typeof value === 'number' && !Number.isInteger(value)) return value.toFixed(3)
return String(value)
}
async function load() {
loading.value = true
try {
const [board, queue] = await Promise.all([getDashboardOverviewApi(), getTranscodeBoardApi()])
const [board, t, dist, queue] = await Promise.all([
getDashboardOverviewApi(),
getDashboardTrendApi(),
getDashboardDistributionApi(),
getTranscodeBoardApi()
])
overview.value = board.data || null
trend.value = t.data || null
distribution.value = dist.data || null
transcode.value = queue.data || null
lastUpdated.value = new Date().toLocaleString()
await nextTick()
renderCharts()
} finally {
loading.value = false
}
}
/** 取不到的指标后端返回 null,这里显示「暂无数据源」而不是 0 */
function display(value) {
if (value === null || value === undefined) return '暂无数据源'
if (typeof value === 'number' && !Number.isInteger(value)) return value.toFixed(3)
return value
function renderCharts() {
disposeAll()
renderTrend()
renderDramaPie()
renderTranscodePie()
renderEpisodePie()
renderQueueBar()
}
function groupValue(groupKey, metricKey) {
return overview.value?.[groupKey]?.[metricKey]
const queueBarRef = ref<HTMLElement>()
function renderQueueBar() {
if (!queueBarRef.value || !transcode.value) return
const q = transcode.value.queue || {}
const chart = echarts.init(queueBarRef.value)
chartInstances.push(chart)
chart.setOption({
tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
grid: { left: 80, right: 24, top: 16, bottom: 24 },
xAxis: { type: 'value', splitLine: { lineStyle: { type: 'dashed' } } },
yAxis: { type: 'category', data: ['排队', '执行中', '成功', '失败'] },
series: [
{
type: 'bar',
data: [q.queued || 0, q.running || 0, q.success || 0, q.failed || 0],
itemStyle: {
borderRadius: [0, 4, 4, 0],
color: (p: { dataIndex: number }) =>
['#F6BD16', '#5B8FF9', '#5AD8A6', '#E86452'][p.dataIndex]
},
label: { show: true, position: 'right' }
}
]
})
}
onMounted(load)
async function loadAndRender() {
loading.value = true
try {
const [board, t, dist, queue] = await Promise.all([
getDashboardOverviewApi(),
getDashboardTrendApi(),
getDashboardDistributionApi(),
getTranscodeBoardApi()
])
overview.value = board.data || null
trend.value = t.data || null
distribution.value = dist.data || null
transcode.value = queue.data || null
lastUpdated.value = new Date().toLocaleString()
await nextTick()
renderCharts()
} finally {
loading.value = false
}
}
function disposeAll() {
chartInstances.forEach((c) => c.dispose())
chartInstances.length = 0
}
const basePieSeriesOption = {
type: 'pie' as const,
radius: ['40%', '70%'],
avoidLabelOverlap: true,
itemStyle: { borderRadius: 6, borderColor: '#fff', borderWidth: 2 },
label: { show: false, position: 'center' },
emphasis: {
label: { show: true, fontSize: 16, fontWeight: 'bold' }
}
}
function renderTrend() {
if (!trendRef.value || !trend.value) return
const chart = echarts.init(trendRef.value)
chartInstances.push(chart)
chart.setOption({
tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },
legend: { top: 0, textStyle: { fontSize: 12 } },
grid: { left: 48, right: 48, top: 40, bottom: 28 },
xAxis: { type: 'category', data: trend.value.dates, boundaryGap: false },
yAxis: [
{ type: 'value', name: '用户/播放', splitLine: { lineStyle: { type: 'dashed' } } },
{ type: 'value', name: '转码任务', splitLine: { show: false } }
],
series: [
{
name: '新增用户',
type: 'line',
smooth: true,
yAxisIndex: 0,
data: trend.value.newUsers,
itemStyle: { color: '#5B8FF9' }
},
{
name: '播放事件',
type: 'line',
smooth: true,
yAxisIndex: 0,
data: trend.value.playEvents,
itemStyle: { color: '#5AD8A6' }
},
{
name: '新增转码',
type: 'bar',
yAxisIndex: 1,
data: trend.value.newTranscodes,
itemStyle: { color: '#F6BD16', borderRadius: [3, 3, 0, 0] }
},
{
name: '转码成功',
type: 'bar',
yAxisIndex: 1,
data: trend.value.transcodeSuccess,
itemStyle: { color: '#945FB9', borderRadius: [3, 3, 0, 0] }
}
]
})
}
function renderDramaPie() {
if (!dramaPieRef.value || !distribution.value) return
const data = Object.entries(distribution.value.dramaStatus || {}).map(([k, v]) => ({
name: (statusLabel as Record<string, string>)[k] || k,
value: v
}))
const chart = echarts.init(dramaPieRef.value)
chartInstances.push(chart)
chart.setOption({
tooltip: { trigger: 'item', formatter: '{b}: {c} ({d}%)' },
legend: { bottom: 0, itemWidth: 12, itemHeight: 12, textStyle: { fontSize: 12 } },
color: ['#F6BD16', '#5AD8A6', '#5B8FF9', '#E86452'],
series: [{ ...basePieSeriesOption, name: '短剧状态', data }]
})
}
function renderTranscodePie() {
if (!transcodePieRef.value || !distribution.value) return
const data = Object.entries(distribution.value.transcodeStatus || {}).map(([k, v]) => ({
name: (transcodeStatusLabel as Record<string, string>)[k] || k,
value: v
}))
const chart = echarts.init(transcodePieRef.value)
chartInstances.push(chart)
chart.setOption({
tooltip: { trigger: 'item', formatter: '{b}: {c} ({d}%)' },
legend: { bottom: 0, itemWidth: 12, itemHeight: 12, textStyle: { fontSize: 12 } },
color: ['#F6BD16', '#5B8FF9', '#5AD8A6', '#E86452', '#C9C9C9'],
series: [{ ...basePieSeriesOption, name: '转码状态', data }]
})
}
function renderEpisodePie() {
if (!episodePieRef.value || !distribution.value) return
const data = Object.entries(distribution.value.episodeMedia || {}).map(([k, v]) => ({
name: k,
value: v
}))
const chart = echarts.init(episodePieRef.value)
chartInstances.push(chart)
chart.setOption({
tooltip: { trigger: 'item', formatter: '{b}: {c} ({d}%)' },
legend: { bottom: 0, itemWidth: 12, itemHeight: 12, textStyle: { fontSize: 12 } },
color: ['#5AD8A6', '#E86452'],
series: [{ ...basePieSeriesOption, name: '片源覆盖', data }]
})
}
function onResize() {
chartInstances.forEach((c) => c.resize())
}
onMounted(loadAndRender)
window.addEventListener('resize', onResize)
onBeforeUnmount(() => {
disposeAll()
window.removeEventListener('resize', onResize)
})
</script>
<template>
<PageHeading
eyebrow="数据看板"
title="运营大盘"
description="用户、内容、播放、收入与成本总览,以及转码队列。"
description="用户、内容、播放与成本总览,以及近 7 天趋势。"
>
<el-button :loading="loading" @click="load">刷新</el-button>
<el-button :loading="loading" @click="loadAndRender">刷新</el-button>
</PageHeading>
<section v-loading="loading" class="panel">
<p class="console-hint"
>数据时间:{{ lastUpdated }}(口径见每组的后端定义,缺数据源显示「暂无数据源」)</p
>
<div v-for="group in GROUPS" :key="group.key" class="board-group">
<h3 class="board-title">{{ group.title }}</h3>
<div class="board-grid">
<div v-for="metric in group.metrics" :key="metric.key" class="board-card">
<span class="board-label">{{ metric.label }}</span>
<span
class="board-value"
:class="{ 'board-value-empty': groupValue(group.key, metric.key) === null }"
>
{{ display(groupValue(group.key, metric.key)) }}
</span>
<div v-loading="loading" class="board-wrap">
<!-- 顶部关键指标卡 -->
<div class="highlight-grid">
<div
v-for="card in HIGHLIGHT_CARDS"
:key="card.group + card.key"
class="highlight-card"
:style="{ borderTopColor: card.color }"
>
<div class="highlight-label">{{ card.label }}</div>
<div class="highlight-value" :class="{ empty: groupValue(card.group, card.key) === null }">
{{ display(formatCount(groupValue(card.group, card.key))) }}
</div>
</div>
</div>
<!-- 趋势折线图(双 Y 轴 + 柱) -->
<section class="panel chart-panel">
<div class="panel-heading">
<div>
<h2>近 7 天趋势</h2>
<p>每日新增用户 / 播放事件 / 转码任务</p>
</div>
<span class="muted">数据时间:{{ lastUpdated }}</span>
</div>
<div ref="trendRef" class="chart chart-lg"></div>
</section>
<!-- 分布饼图组 -->
<section class="panel">
<div class="panel-heading">
<div>
<h2>状态分布</h2>
<p>短剧状态 / 转码状态 / 片源覆盖</p>
</div>
</div>
<div class="pie-row">
<div class="pie-cell">
<div class="pie-title">短剧状态</div>
<div ref="dramaPieRef" class="chart chart-pie"></div>
</div>
<div class="pie-cell">
<div class="pie-title">转码状态</div>
<div ref="transcodePieRef" class="chart chart-pie"></div>
</div>
<div class="pie-cell">
<div class="pie-title">片源覆盖</div>
<div ref="episodePieRef" class="chart chart-pie"></div>
</div>
</div>
</section>
<!-- 转码队列 + 失败明细 -->
<section class="panel transcode-panel">
<div class="panel-heading">
<div>
<h2>转码看板</h2>
<p>队列积压与失败任务明细</p>
</div>
<p class="muted">{{ transcode?.notes?.join(';') || '' }}</p>
</div>
<div class="transcode-grid">
<div ref="queueBarRef" class="chart chart-bar"></div>
<div class="transcode-fail">
<h3>失败任务(最近 20 条)</h3>
<el-table
:data="transcode?.failedTasks || []"
border
class="drama-el-table"
empty-text="当前没有失败的转码任务"
size="small"
>
<el-table-column prop="id" label="任务ID" width="100" />
<el-table-column prop="episodeId" label="剧集" width="100" />
<el-table-column prop="status" label="状态" width="90" />
<el-table-column prop="retryCount" label="重试" width="70" />
<el-table-column
prop="errorMsg"
label="失败原因"
min-width="220"
show-overflow-tooltip
/>
<el-table-column
prop="createdAt"
label="创建时间"
min-width="160"
show-overflow-tooltip
:formatter="dateCell"
/>
</el-table>
</div>
</div>
</section>
<!-- 口径说明 -->
<el-alert
v-if="overview?.notes?.length"
class="board-notes"
@@ -152,31 +392,138 @@ onMounted(load)
<li v-for="note in overview.notes" :key="note">{{ note }}</li>
</ul>
</el-alert>
</section>
<section class="panel board-transcode">
<h3 class="board-title">转码看板</h3>
<p class="console-hint">
{{ transcode?.notes?.join(';') || '' }}
</p>
<el-table
:data="transcode?.failedTasks || []"
border
class="drama-el-table"
empty-text="当前没有失败的转码任务"
>
<el-table-column prop="id" label="任务ID" width="90" />
<el-table-column prop="episodeId" label="剧集ID" width="90" />
<el-table-column prop="status" label="状态" width="110" />
<el-table-column prop="retryCount" label="重试次数" width="100" />
<el-table-column prop="errorMsg" label="失败原因" min-width="240" show-overflow-tooltip />
<el-table-column
prop="createdAt"
label="创建时间"
min-width="165"
show-overflow-tooltip
:formatter="dateCell"
/>
</el-table>
</section>
</div>
</template>
<style scoped lang="less">
.board-wrap {
display: flex;
flex-direction: column;
gap: 16px;
}
.panel {
background: var(--el-bg-color);
border-radius: 8px;
padding: 20px 24px;
border: 1px solid var(--el-border-color-lighter);
}
.panel-heading {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 12px;
h2 {
margin: 0;
font-size: 16px;
font-weight: 600;
}
h3 {
margin: 0 0 8px;
font-size: 14px;
font-weight: 600;
}
p {
margin: 4px 0 0;
font-size: 13px;
color: var(--el-text-color-secondary);
}
.muted {
color: var(--el-text-color-placeholder);
font-size: 12px;
}
}
.highlight-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 12px;
}
.highlight-card {
background: var(--el-bg-color);
border-radius: 8px;
padding: 14px 18px;
border: 1px solid var(--el-border-color-lighter);
border-top: 3px solid transparent;
.highlight-label {
font-size: 12px;
color: var(--el-text-color-secondary);
}
.highlight-value {
margin-top: 6px;
font-size: 24px;
font-weight: 700;
color: var(--el-text-color-primary);
font-variant-numeric: tabular-nums;
&.empty {
font-size: 14px;
color: var(--el-text-color-placeholder);
font-weight: 400;
}
}
}
.chart {
width: 100%;
}
.chart-lg {
height: 320px;
}
.chart-pie {
height: 220px;
}
.chart-bar {
height: 220px;
}
.pie-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.pie-cell {
display: flex;
flex-direction: column;
align-items: center;
.pie-title {
margin-bottom: 4px;
font-size: 13px;
color: var(--el-text-color-secondary);
}
}
.transcode-grid {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 20px;
}
.board-notes {
:deep(ul) {
margin: 4px 0 0;
padding-left: 18px;
font-size: 12px;
}
}
@media (width <= 900px) {
.pie-row,
.transcode-grid {
grid-template-columns: 1fr;
}
}
</style>