Files
CardAuth/frontend/src/views/OnlineStats.vue
T

242 lines
9.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="online-stats-page">
<!-- 统计卡片 -->
<el-row :gutter="20" class="stats-row">
<el-col :xs="12" :sm="8" :md="4" :lg="4">
<div class="stat-card stat-current">
<div class="stat-icon"><el-icon :size="32"><Odometer /></el-icon></div>
<div class="stat-info">
<div class="stat-value">{{ stats.current_online }}</div>
<div class="stat-label">当前在线(5分钟内)</div>
</div>
</div>
</el-col>
<el-col :xs="12" :sm="8" :md="4" :lg="4">
<div class="stat-card stat-today">
<div class="stat-icon"><el-icon :size="32"><Sunny /></el-icon></div>
<div class="stat-info">
<div class="stat-value">{{ stats.today_online }}</div>
<div class="stat-label">今日在线</div>
</div>
</div>
</el-col>
<el-col :xs="12" :sm="8" :md="4" :lg="4">
<div class="stat-card stat-week">
<div class="stat-icon"><el-icon :size="32"><Calendar /></el-icon></div>
<div class="stat-info">
<div class="stat-value">{{ stats.week_online }}</div>
<div class="stat-label">本周在线(周一起)</div>
</div>
</div>
</el-col>
<el-col :xs="12" :sm="8" :md="4" :lg="4">
<div class="stat-card stat-month">
<div class="stat-icon"><el-icon :size="32"><TrendCharts /></el-icon></div>
<div class="stat-info">
<div class="stat-value">{{ stats.month_online }}</div>
<div class="stat-label">本月在线</div>
</div>
</div>
</el-col>
<el-col :xs="12" :sm="8" :md="4" :lg="4">
<div class="stat-card stat-year">
<div class="stat-icon"><el-icon :size="32"><Histogram /></el-icon></div>
<div class="stat-info">
<div class="stat-value">{{ stats.year_online }}</div>
<div class="stat-label">本年在线</div>
</div>
</div>
</el-col>
</el-row>
<!-- 趋势图 -->
<el-card shadow="never" class="main-card">
<div class="chart-toolbar">
<div class="section-title">在线用户数趋势</div>
<div class="chart-controls">
<el-select v-model="projectId" placeholder="全部项目" clearable style="width: 160px" @change="fetchData">
<el-option v-for="p in projects" :key="p.id" :label="p.name" :value="p.id" />
</el-select>
<el-radio-group v-model="period" @change="fetchData">
<el-radio-button value="day">按日(30天)</el-radio-button>
<el-radio-button value="week">按周(12周)</el-radio-button>
<el-radio-button value="month">按月(12月)</el-radio-button>
<el-radio-button value="year">按年(12年)</el-radio-button>
</el-radio-group>
<el-button :icon="Refresh" circle @click="fetchData" />
</div>
</div>
<div v-loading="loading" ref="chartRef" class="chart-box"></div>
</el-card>
<!-- 分项目统计 -->
<el-card shadow="never" class="main-card">
<div class="section-title">按项目在线统计(在线用户 = 客户端机器指纹去重)</div>
<el-table :data="stats.projects" stripe border class="data-table" v-loading="loading">
<el-table-column prop="project_name" label="项目" min-width="160" show-overflow-tooltip>
<template #default="{ row }">{{ row.project_name || row.project_id }}</template>
</el-table-column>
<el-table-column prop="current_online" label="当前在线" width="110" align="center">
<template #default="{ row }">
<el-tag type="success" size="small">{{ row.current_online }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="today_online" label="今日在线" width="110" align="center" />
<el-table-column prop="week_online" label="本周在线" width="110" align="center" />
<el-table-column prop="month_online" label="本月在线" width="110" align="center" />
<el-table-column prop="year_online" label="本年在线" width="110" align="center" />
</el-table>
<div v-if="!stats.projects?.length && !loading" class="empty-tip">暂无在线记录(自上线后随客户端心跳累积)</div>
</el-card>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { Refresh, Odometer, Sunny, Calendar, TrendCharts, Histogram } from '@element-plus/icons-vue'
import * as echarts from 'echarts'
import request from '@/api'
import installationApi from '@/api/installations'
const loading = ref(false)
const period = ref('day')
const projectId = ref('')
const projects = ref([])
const chartRef = ref(null)
let chartInstance = null
const stats = ref({
current_online: 0,
today_online: 0,
week_online: 0,
month_online: 0,
year_online: 0,
trend: [],
projects: []
})
function renderChart() {
const el = chartRef.value
if (!el) return
if (!chartInstance || chartInstance.isDisposed()) {
chartInstance = echarts.init(el)
}
const trend = stats.value.trend || []
chartInstance.setOption({
tooltip: {
trigger: 'axis',
backgroundColor: 'rgba(255,255,255,0.96)',
borderColor: '#e2e8f0', borderWidth: 1,
textStyle: { color: '#333', fontSize: 12 }
},
grid: { left: '3%', right: '4%', top: 16, bottom: 30, containLabel: true },
xAxis: {
type: 'category',
data: trend.map(t => t.label),
axisLine: { lineStyle: { color: '#e2e8f0' } },
axisTick: { show: false },
axisLabel: { fontSize: 10, color: '#94a3b8', interval: 'auto' }
},
yAxis: {
type: 'value', minInterval: 1,
axisLabel: { fontSize: 10, color: '#94a3b8' },
splitLine: { lineStyle: { color: '#f1f5f9', type: 'dashed' } },
axisLine: { show: false }, axisTick: { show: false }
},
series: [{
name: '在线用户数',
type: 'bar',
data: trend.map(t => t.value),
barMaxWidth: 22,
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#34d399' },
{ offset: 1, color: '#a7f3d0' }
]),
borderRadius: [6, 6, 0, 0]
}
}]
}, true)
}
async function fetchData() {
loading.value = true
try {
const params = { period: period.value }
if (projectId.value) params.project_id = projectId.value
const res = await installationApi.onlineStats(params)
stats.value = { ...stats.value, ...(res.data || {}) }
await nextTick()
renderChart()
} catch (e) {
console.error(e)
} finally {
loading.value = false
}
}
async function fetchProjects() {
try {
const res = await request.get('/projects/all')
projects.value = res.data.list || res.data || []
if (!Array.isArray(projects.value)) projects.value = []
} catch {
projects.value = []
}
}
function handleResize() {
chartInstance?.resize()
}
onMounted(() => {
fetchProjects()
fetchData()
window.addEventListener('resize', handleResize)
})
onUnmounted(() => {
window.removeEventListener('resize', handleResize)
chartInstance?.dispose()
})
</script>
<style scoped>
.online-stats-page { max-width: 1600px; margin: 0 auto; }
.stats-row { margin-bottom: 20px; }
.stat-card {
display: flex; align-items: center; gap: 12px;
padding: 14px 18px; border-radius: 12px; background: #fff;
border: 1px solid #f0f0f4; transition: all .25s;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(0,0,0,.05); }
.stat-icon {
width: 40px; height: 40px; border-radius: 10px;
display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.stat-icon :deep(.el-icon) { font-size: 20px !important; }
.stat-current .stat-icon { background: #ecfdf5; color: #10b981; }
.stat-today .stat-icon { background: #fffbeb; color: #f59e0b; }
.stat-week .stat-icon { background: #eff6ff; color: #3b82f6; }
.stat-month .stat-icon { background: #f5f3ff; color: #8b5cf6; }
.stat-year .stat-icon { background: #fdf2f8; color: #ec4899; }
.stat-info { display: flex; flex-direction: column; min-width: 0; }
.stat-value { font-size: 22px; font-weight: 700; color: #1d1d1f; line-height: 1.1; letter-spacing: -.3px; }
.stat-label { font-size: 12px; color: #9ca3af; margin-top: 2px; font-weight: 500; }
.main-card { margin-bottom: 20px; border-radius: 16px; border: 1px solid #f0f0f4; }
.main-card:deep(.el-card__body) { padding: 24px; }
.chart-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-wrap: wrap; gap: 12px; }
.chart-controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.section-title { font-weight: 600; color: #374151; }
.chart-box { height: 320px; }
.data-table { border-radius: 12px; overflow: hidden; }
.data-table:deep(.el-table__header th) { background: #f8f9fb; font-weight: 600; color: #374151; font-size: 13px; border-color: #f0f0f4; }
.data-table:deep(.el-table__body td) { font-size: 13px; color: #374151; border-color: #f5f5f7; }
.empty-tip { text-align: center; color: #9ca3af; font-size: 13px; padding: 24px 0 8px; }
@media (max-width: 768px) {
.chart-toolbar { flex-direction: column; align-items: stretch; }
.main-card:deep(.el-card__body) { padding: 14px; }
.chart-box { height: 240px; }
}
</style>