Added Debug Log and info pickup older 6month + hygiene crt

This commit is contained in:
2025-12-16 12:05:16 +01:00
parent ca81121f3d
commit f4323e20de
9 changed files with 842 additions and 20 deletions

View File

@@ -13,6 +13,7 @@ const { readNotificationSettings, writeNotificationSettings } = require('./servi
const notificationService = require('./services/notificationService');
const { readStoreWatch, writeStoreWatch, listWatcherProfiles } = require('./services/storeWatchStore');
const { readPreferences, writePreferences, sanitizeLocation } = require('./services/userPreferencesStore');
const requestLogStore = require('./services/requestLogStore');
const {
getStoreStatus: getCachedStoreStatusEntry,
setStoreStatus: setCachedStoreStatusEntry,
@@ -33,6 +34,7 @@ const STORE_STATUS_MAX_AGE_MS = 60 * 24 * 60 * 60 * 1000;
const storeLocationIndex = new Map();
let storeLocationIndexUpdatedAt = 0;
const STORE_LOCATION_INDEX_TTL_MS = 12 * 60 * 60 * 1000;
const BACKGROUND_STORE_REFRESH_INTERVAL_MS = 6 * 60 * 60 * 1000;
function toRadians(value) {
return (value * Math.PI) / 180;
@@ -63,6 +65,40 @@ app.use(cors());
app.use(express.json({ limit: '1mb' }));
app.use(express.static(path.join(__dirname, 'build')));
app.use((req, res, next) => {
const startedAt = Date.now();
let responseBodySnippet = null;
const captureBody = (body) => {
responseBodySnippet = body;
return body;
};
const originalJson = res.json.bind(res);
res.json = (body) => originalJson(captureBody(body));
const originalSend = res.send.bind(res);
res.send = (body) => originalSend(captureBody(body));
res.on('finish', () => {
try {
requestLogStore.add({
direction: 'incoming',
method: req.method,
path: req.originalUrl || req.url,
status: res.statusCode,
durationMs: Date.now() - startedAt,
sessionId: req.session?.id || null,
profileId: req.session?.profile?.id || null,
responseBody: responseBodySnippet
});
} catch (error) {
console.warn('[REQUEST-LOG] Schreiben fehlgeschlagen:', error.message);
}
});
next();
});
function isAdmin(profile) {
if (!adminEmail || !profile?.email) {
return false;
@@ -510,6 +546,32 @@ async function loadStoresForSession(session, _settings, { forceRefresh = false,
};
}
function startBackgroundStoreRefreshTicker() {
const runCheck = () => {
sessionStore.list().forEach((session) => {
if (!session?.id || !session.cookieHeader) {
return;
}
const cacheFresh = isStoreCacheFresh(session);
const job = getStoreRefreshJob(session.id);
const jobRunning = job?.status === 'running';
if (jobRunning || cacheFresh) {
return;
}
const reason = session.storesCache?.fetchedAt ? 'background-stale-cache' : 'background-no-cache';
const result = triggerStoreRefresh(session, { force: true, reason });
if (result?.started) {
console.log(`[STORE-REFRESH] Hintergrund-Refresh gestartet für Session ${session.id} (${reason})`);
}
});
};
setTimeout(() => {
runCheck();
setInterval(runCheck, BACKGROUND_STORE_REFRESH_INTERVAL_MS);
}, 60 * 1000);
}
async function restoreSessionsFromDisk() {
const saved = credentialStore.loadAll();
const entries = Object.entries(saved);
@@ -943,6 +1005,12 @@ app.post('/api/admin/settings', requireAuth, requireAdmin, (req, res) => {
res.json(updated);
});
app.get('/api/debug/requests', requireAuth, requireAdmin, (req, res) => {
const limit = req.query?.limit ? Number(req.query.limit) : undefined;
const logs = requestLogStore.list(limit);
res.json({ logs });
});
app.get('/api/health', (_req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
@@ -957,6 +1025,7 @@ async function startServer() {
} catch (error) {
console.error('[RESTORE] Fehler bei der Session-Wiederherstellung:', error.message);
}
startBackgroundStoreRefreshTicker();
app.listen(port, () => {
console.log(`Server läuft auf Port ${port}`);
});