| 1 | 'use strict'; |
| 2 | |
| 3 | const express = require('express'); |
| 4 | const fs = require('fs'); |
| 5 | const os = require('os'); |
| 6 | const path = require('path'); |
| 7 | |
| 8 | const router = express.Router(); |
| 9 | |
| 10 | const { getDb } = require('../db'); |
| 11 | const { loadConfig, getEffectivePaths } = require('../config'); |
| 12 | const { getImageSubtitleDebugSnapshot } = require('../image-subtitles'); |
| 13 | const { getVideoStreamDebugSnapshot } = require('../video-stream'); |
| 14 | |
| 15 | const AUTH_IDLE_TIMEOUT_MS = 60 * 60 * 1000; |
| 16 | |
| 17 | function safeResolve (value) |
| 18 | { |
| 19 | return path.resolve(String(value || '').trim()); |
| 20 | } |
| 21 | |
| 22 | function normaliseRootsForUsage (config) |
| 23 | { |
| 24 | const rawRoots = Array.isArray(config.paths) ? config.paths : []; |
| 25 | const effective = getEffectivePaths(rawRoots); |
| 26 | const roots = new Set(effective.map((root) => safeResolve(root))); |
| 27 | |
| 28 | const userPath = String(config.userPath || '').trim(); |
| 29 | if (userPath) |
| 30 | { |
| 31 | const resolvedUserPath = safeResolve(userPath); |
| 32 | const isCovered = [...roots].some((root) => |
| 33 | resolvedUserPath === root || resolvedUserPath.startsWith(`${root}${path.sep}`)); |
| 34 | if (!isCovered) roots.add(resolvedUserPath); |
| 35 | } |
| 36 | |
| 37 | return [...roots]; |
| 38 | } |
| 39 | |
| 40 | function walkDirectorySize (rootPath) |
| 41 | { |
| 42 | let bytes = 0; |
| 43 | let files = 0; |
| 44 | let directories = 0; |
| 45 | const stack = [rootPath]; |
| 46 | |
| 47 | while (stack.length > 0) |
| 48 | { |
| 49 | const current = stack.pop(); |
| 50 | let entries = []; |
| 51 | try |
| 52 | { |
| 53 | entries = fs.readdirSync(current, { withFileTypes: true }); |
| 54 | } catch |
| 55 | { |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | for (const entry of entries) |
| 60 | { |
| 61 | const fullPath = path.join(current, entry.name); |
| 62 | if (entry.isDirectory()) |
| 63 | { |
| 64 | directories += 1; |
| 65 | stack.push(fullPath); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if (!entry.isFile()) continue; |
| 70 | try |
| 71 | { |
| 72 | const stat = fs.statSync(fullPath); |
| 73 | bytes += Number.isFinite(stat.size) ? stat.size : 0; |
| 74 | files += 1; |
| 75 | } catch |
| 76 | { |
| 77 | // Best effort; skip unreadable files. |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return { bytes, files, directories }; |
| 83 | } |
| 84 | |
| 85 | function collectRootUsageStats () |
| 86 | { |
| 87 | const config = loadConfig(); |
| 88 | const roots = normaliseRootsForUsage(config); |
| 89 | |
| 90 | const perRoot = []; |
| 91 | let totalBytes = 0; |
| 92 | let totalFiles = 0; |
| 93 | |
| 94 | for (const root of roots) |
| 95 | { |
| 96 | const row = { |
| 97 | path: root, |
| 98 | exists: fs.existsSync(root), |
| 99 | bytes: 0, |
| 100 | files: 0, |
| 101 | directories: 0, |
| 102 | }; |
| 103 | |
| 104 | if (row.exists) |
| 105 | { |
| 106 | const usage = walkDirectorySize(root); |
| 107 | row.bytes = usage.bytes; |
| 108 | row.files = usage.files; |
| 109 | row.directories = usage.directories; |
| 110 | totalBytes += usage.bytes; |
| 111 | totalFiles += usage.files; |
| 112 | } |
| 113 | |
| 114 | perRoot.push(row); |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | roots: perRoot, |
| 119 | totalBytes, |
| 120 | totalFiles, |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | function getLoggedInUsersSnapshot () |
| 125 | { |
| 126 | const db = getDb(); |
| 127 | const now = Date.now(); |
| 128 | const activeSince = now - AUTH_IDLE_TIMEOUT_MS; |
| 129 | |
| 130 | const rows = db.prepare( |
| 131 | `SELECT username, MAX(last_seen_at) AS lastSeenAt |
| 132 | FROM auth_sessions |
| 133 | WHERE revoked_at IS NULL |
| 134 | AND last_seen_at >= ? |
| 135 | GROUP BY username |
| 136 | ORDER BY username COLLATE NOCASE ASC`, |
| 137 | ).all([activeSince]); |
| 138 | |
| 139 | return rows.map((row) => ({ |
| 140 | username: String(row.username || ''), |
| 141 | lastSeenAt: Number(row.lastSeenAt || 0), |
| 142 | })); |
| 143 | } |
| 144 | |
| 145 | function buildVideoDebugRows (videoSnapshot, subtitleSnapshot) |
| 146 | { |
| 147 | const perFile = new Map(); |
| 148 | |
| 149 | const activeStreams = Array.isArray(videoSnapshot.activeStreams) ? videoSnapshot.activeStreams : []; |
| 150 | for (const stream of activeStreams) |
| 151 | { |
| 152 | const key = safeResolve(stream.sourcePath); |
| 153 | perFile.set(key, { |
| 154 | filePath: key, |
| 155 | viewers: Number(stream.viewers || 0), |
| 156 | activeSessionIds: [stream.sessionId], |
| 157 | stream: stream, |
| 158 | subtitleCache: null, |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | const subtitleByFile = subtitleSnapshot && subtitleSnapshot.byFile ? subtitleSnapshot.byFile : {}; |
| 163 | for (const [filePath, subtitleRow] of Object.entries(subtitleByFile)) |
| 164 | { |
| 165 | const key = safeResolve(filePath); |
| 166 | const existing = perFile.get(key); |
| 167 | if (existing) |
| 168 | { |
| 169 | existing.subtitleCache = subtitleRow; |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | perFile.set(key, { |
| 174 | filePath: key, |
| 175 | viewers: 0, |
| 176 | activeSessionIds: [], |
| 177 | stream: null, |
| 178 | subtitleCache: subtitleRow, |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | return [...perFile.values()].sort((a, b) => a.filePath.localeCompare(b.filePath)); |
| 183 | } |
| 184 | |
| 185 | function getMemorySnapshot () |
| 186 | { |
| 187 | const totalBytes = Number(os.totalmem()) || 0; |
| 188 | const freeBytes = Number(os.freemem()) || 0; |
| 189 | const usedBytes = Math.max(0, totalBytes - freeBytes); |
| 190 | |
| 191 | return { |
| 192 | totalBytes, |
| 193 | freeBytes, |
| 194 | usedBytes, |
| 195 | usedPercent: totalBytes > 0 ? Number(((usedBytes / totalBytes) * 100).toFixed(1)) : 0, |
| 196 | processRssBytes: Number(process.memoryUsage().rss || 0), |
| 197 | processHeapUsedBytes: Number(process.memoryUsage().heapUsed || 0), |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | router.get('/debug/stream-stats', (req, res) => |
| 202 | { |
| 203 | const rootUsage = collectRootUsageStats(); |
| 204 | const loggedInUsers = getLoggedInUsersSnapshot(); |
| 205 | const videoStream = getVideoStreamDebugSnapshot(); |
| 206 | const subtitleCache = getImageSubtitleDebugSnapshot(); |
| 207 | const videoFiles = buildVideoDebugRows(videoStream, subtitleCache); |
| 208 | const memory = getMemorySnapshot(); |
| 209 | |
| 210 | res.json({ |
| 211 | generatedAt: Date.now(), |
| 212 | loggedInUsers, |
| 213 | storage: rootUsage, |
| 214 | memory, |
| 215 | videoStream, |
| 216 | subtitleCache, |
| 217 | videoFiles, |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | module.exports = router; |