feat: 在线统计增加按年趋势与本年在线

This commit is contained in:
root committed 2026-10-16 16:23:55 +08:00
1 parent 70fda65889
commit 3ff9c41d18
3 files changed
+50 -13

No files matched your search

@@ -90,7 +90,7 @@ class InstallationController extends Controller
$user = $this->getCurrentUser() ?: [];
$projectId = !empty($_GET['project_id']) ? (int) $_GET['project_id'] : null;
$period = $_GET['period'] ?? 'day';
if (!in_array($period, ['day', 'week', 'month'], true)) {
if (!in_array($period, ['day', 'week', 'month', 'year'], true)) {
$period = 'day';
}
+31 -7
View File
@@ -257,7 +257,7 @@ class InstallationService
* 在线统计:当前在线(5分钟内心跳)+ 按天去重的日/周/月在线用户数 + 趋势 + 分项目
* 统计口径:「在线用户」= 有心跳的机器指纹(machine_id)
*
* @param string $period day|week|month 趋势粒度
* @param string $period day|week|month|year 趋势粒度
*/
public static function onlineStats(array $user, ?int $projectId = null, string $period = 'day'): array
{
@@ -282,21 +282,22 @@ class InstallationService
array_merge($params, $projParams)
);
$logWhere = " AND i.online_date >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 32 DAY), '%Y-%m-%d')" . $projWhere;
$logWhere = " AND i.online_date >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 11 YEAR), '%Y-01-01')" . $projWhere;
$logParams = $params;
// 今日 / 本周(周一起)/ 本月 去重在线机器数
// 今日 / 本周(周一起)/ 本月 / 本年 去重在线机器数
$counts = $db->fetch(
"SELECT
COUNT(DISTINCT CASE WHEN i.online_date = CURDATE() THEN i.machine_id END) AS today_online,
COUNT(DISTINCT CASE WHEN YEARWEEK(i.online_date, 1) = YEARWEEK(CURDATE(), 1) THEN i.machine_id END) AS week_online,
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01') THEN i.machine_id END) AS month_online
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01') THEN i.machine_id END) AS month_online,
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-01-01') THEN i.machine_id END) AS year_online
FROM {$db->table('online_logs')} i
WHERE 1=1 {$logWhere}",
array_merge($logParams, $projParams)
);
// 分项目:当前在线(来自 installations)+ 日/周/月(来自 online_logs)
// 分项目:当前在线(来自 installations)+ 日/周/月/年(来自 online_logs)
$curRows = $db->fetchAll(
"SELECT i.project_id, COUNT(*) AS current_online
FROM {$db->table('installations')} i
@@ -310,7 +311,8 @@ class InstallationService
"SELECT i.project_id, p.name AS project_name,
COUNT(DISTINCT CASE WHEN i.online_date = CURDATE() THEN i.machine_id END) AS today_online,
COUNT(DISTINCT CASE WHEN YEARWEEK(i.online_date, 1) = YEARWEEK(CURDATE(), 1) THEN i.machine_id END) AS week_online,
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01') THEN i.machine_id END) AS month_online
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01') THEN i.machine_id END) AS month_online,
COUNT(DISTINCT CASE WHEN i.online_date >= DATE_FORMAT(CURDATE(), '%Y-01-01') THEN i.machine_id END) AS year_online
FROM {$db->table('online_logs')} i
LEFT JOIN {$db->table('projects')} p ON p.id = i.project_id
WHERE 1=1 {$logWhere}
@@ -328,6 +330,7 @@ class InstallationService
'today_online' => (int) $row['today_online'],
'week_online' => (int) $row['week_online'],
'month_online' => (int) $row['month_online'],
'year_online' => (int) $row['year_online'],
];
}
foreach ($curRows as $row) {
@@ -341,12 +344,13 @@ class InstallationService
'today_online' => 0,
'week_online' => 0,
'month_online' => 0,
'year_online' => 0,
];
}
$projects[$pid]['current_online'] = (int) $row['current_online'];
}
$projects = array_values($projects);
usort($projects, static fn (array $a, array $b) => $b['month_online'] <=> $a['month_online']);
usort($projects, static fn (array $a, array $b) => $b['year_online'] <=> $a['year_online']);
// 趋势
$trend = [];
@@ -388,6 +392,25 @@ class InstallationService
$ym = $d->format('Y-m');
$trend[] = ['label' => $ym, 'value' => $map[$ym] ?? 0];
}
} elseif ($period === 'year') {
$rows = $db->fetchAll(
"SELECT DATE_FORMAT(i.online_date, '%Y') AS y, COUNT(DISTINCT i.machine_id) AS cnt
FROM {$db->table('online_logs')} i
WHERE i.online_date >= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 11 YEAR), '%Y-01-01') {$projWhere}
GROUP BY y",
$projParams
);
$map = [];
foreach ($rows as $r) {
$map[$r['y']] = (int) $r['cnt'];
}
$now = new \DateTime('today');
for ($i = 11; $i >= 0; $i--) {
$d = clone $now;
$d->modify("-{$i} years");
$y = $d->format('Y');
$trend[] = ['label' => $y, 'value' => $map[$y] ?? 0];
}
} else {
$rows = $db->fetchAll(
"SELECT DATE(i.online_date) AS d, COUNT(DISTINCT i.machine_id) AS cnt
@@ -414,6 +437,7 @@ class InstallationService
'today_online' => (int) ($counts['today_online'] ?? 0),
'week_online' => (int) ($counts['week_online'] ?? 0),
'month_online' => (int) ($counts['month_online'] ?? 0),
'year_online' => (int) ($counts['year_online'] ?? 0),
'trend' => $trend,
'projects' => $projects,
];
+18 -5
View File
@@ -2,7 +2,7 @@
<div class="online-stats-page">
<!-- 统计卡片 -->
<el-row :gutter="20" class="stats-row">
<el-col :xs="12" :sm="6" :md="6" :lg="6">
<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">
@@ -11,7 +11,7 @@
</div>
</div>
</el-col>
<el-col :xs="12" :sm="6" :md="6" :lg="6">
<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">
@@ -20,7 +20,7 @@
</div>
</div>
</el-col>
<el-col :xs="12" :sm="6" :md="6" :lg="6">
<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">
@@ -29,7 +29,7 @@
</div>
</div>
</el-col>
<el-col :xs="12" :sm="6" :md="6" :lg="6">
<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">
@@ -38,6 +38,15 @@
</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>
<!-- 趋势图 -->
@@ -52,6 +61,7 @@
<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>
@@ -74,6 +84,7 @@
<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>
@@ -82,7 +93,7 @@
<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import { Refresh, Odometer, Sunny, Calendar, TrendCharts } from '@element-plus/icons-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'
@@ -99,6 +110,7 @@ const stats = ref({
today_online: 0,
week_online: 0,
month_online: 0,
year_online: 0,
trend: [],
projects: []
})
@@ -207,6 +219,7 @@ onUnmounted(() => {
.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; }