From 786286df0a094806c5b5134cd38185f091f106f7 Mon Sep 17 00:00:00 2001 From: Lukas Davidovic Date: Tue, 24 Dec 2024 15:19:33 +0100 Subject: [PATCH] analytics and performance monitoring --- pages/api/analytics/performance.ts | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 pages/api/analytics/performance.ts diff --git a/pages/api/analytics/performance.ts b/pages/api/analytics/performance.ts new file mode 100644 index 0000000..4c32e7a --- /dev/null +++ b/pages/api/analytics/performance.ts @@ -0,0 +1,46 @@ +import type { NextApiRequest, NextApiResponse } from "next" +import sqlite3 from "sqlite3" +import path from "path" + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") return res.status(405).json({ error: "Method not allowed" }) + + const dbPath = path.join(process.cwd(), "database", "antihoax.db") + const db = new sqlite3.Database(dbPath) + + try { + // Get performance metrics + const stats = await new Promise((resolve, reject) => { + db.get(` + SELECT + COUNT(*) as total_sources, + COUNT(CASE WHEN status = 'verified' THEN 1 END) as verified_sources, + COUNT(CASE WHEN risk_level >= 4 THEN 1 END) as high_risk_sources, + COUNT(CASE WHEN created_at >= date('now', '-7 days') THEN 1 END) as sources_last_week + FROM sources + `, (err, row) => { + if (err) reject(err) + else resolve(row) + }) + }) + + // Get API usage simulation + const apiUsage = { + daily_requests: Math.floor(Math.random() * 10000) + 5000, + avg_response_time: Math.floor(Math.random() * 150) + 50, + error_rate: Math.random() * 0.05 + } + + res.json({ + database_stats: stats, + api_performance: apiUsage, + last_updated: new Date().toISOString() + }) + + } catch (error) { + console.error('Analytics error:', error) + res.status(500).json({ error: "Failed to fetch analytics" }) + } finally { + db.close() + } +} \ No newline at end of file