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