120秒记0防停服假曲线),保留7天。 // 本地三个函数改薄壳保持旧调用点兼容。 function ensureSnapshotTable() { onlineCurveEnsureSnapshot(false, 'page'); } // ===== 记录快照(薄壳:是否真正落样本由 core 60秒节流裁决) ===== function recordSnapshot() { $r = onlineCurveEnsureSnapshot(false, 'page'); return $r['count']; } // ===== 确保有最新快照(薄壳:节流由 core 统一处理) ===== function ensureRecentSnapshot() { $r = onlineCurveEnsureSnapshot(false, 'page'); return $r['recorded'] ? $r['count'] : null; } // ===== 获取24小时聚合数据(按小时) ===== function getHourlyData() { ensureSnapshotTable(); // 每次查询数据时自动确保有最新快照(如果超过60秒未记录) ensureRecentSnapshot(); // 24小时前的时间戳 $since = time() - 86400; // 按自然小时聚合:分桶到 h0~h23,取平均值 $buckets = []; for ($i = 0; $i < 24; $i++) $buckets[$i] = ['hour' => $i, 'count' => 0, 'samples' => 0, 'max' => 0]; try { $db = getDB(); $stmt = $db->prepare("SELECT snapshot_time, online_count FROM online_snapshots WHERE snapshot_time >= :since ORDER BY snapshot_time ASC"); $stmt->bindValue(':since', $since, SQLITE3_INTEGER); $res = $stmt->execute(); while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $snapshotTime = (int)$row['snapshot_time']; // 使用本地时区获取小时(0-23) $h = (int)date('G', $snapshotTime); $buckets[$h]['count'] += (int)$row['online_count']; $buckets[$h]['samples']++; if ((int)$row['online_count'] > $buckets[$h]['max']) $buckets[$h]['max'] = (int)$row['online_count']; } } catch (\Throwable $e) { debugLog('[online_curve] 查询异常: ' . $e->getMessage()); } $result = []; foreach ($buckets as $b) { $avg = $b['samples'] > 0 ? round($b['count'] / $b['samples'], 1) : 0; $result[] = [ 'hour' => sprintf('%02d:00', $b['hour']), 'avg' => $avg, 'max' => $b['max'], 'samples' => $b['samples'], ]; } return ['buckets' => $result, 'total_snapshots' => array_sum(array_column($result, 'samples'))]; } // ===== 路由 ===== switch ($action) { case 'record': $c = recordSnapshot(); success(['recorded' => true, 'online_count' => $c]); exit; case 'data': success(getHourlyData()); exit; case 'page': default: // 页面加载时也记录一次快照 $currentOnline = recordSnapshot(); $initialState = json_encode(array_merge( ['current_online' => $currentOnline], getHourlyData() ), JSON_UNESCAPED_UNICODE); break; } ?> SDF1 - 24小时在线曲线

📈 24小时在线曲线

每60秒刷新 · Java同步自动采集
加载中...