admin panel
This commit is contained in:
72
pages/api/admin/dashboard.ts
Normal file
72
pages/api/admin/dashboard.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import sqlite3 from 'sqlite3'
|
||||
import path from 'path'
|
||||
|
||||
interface DashboardStats {
|
||||
total_sources: number
|
||||
pending_sources: number
|
||||
pending_reports: number
|
||||
high_risk_sources: number
|
||||
sources_added_week: number
|
||||
reports_today: number
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<DashboardStats | { error: string }>
|
||||
) {
|
||||
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 {
|
||||
const stats = await new Promise<DashboardStats>((resolve, reject) => {
|
||||
const queries = [
|
||||
"SELECT COUNT(*) as total_sources FROM sources WHERE status = 'verified'",
|
||||
"SELECT COUNT(*) as pending_sources FROM sources WHERE status = 'pending'",
|
||||
"SELECT COUNT(*) as pending_reports FROM reports WHERE status = 'pending'",
|
||||
"SELECT COUNT(*) as high_risk_sources FROM sources WHERE status = 'verified' AND risk_level >= 4",
|
||||
"SELECT COUNT(*) as sources_added_week FROM sources WHERE created_at > datetime('now', '-7 days')",
|
||||
"SELECT COUNT(*) as reports_today FROM reports WHERE created_at > datetime('now', '-1 day')"
|
||||
]
|
||||
|
||||
const results: any = {}
|
||||
let completed = 0
|
||||
|
||||
queries.forEach((query, index) => {
|
||||
db.get(query, (err, row: any) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
const key = Object.keys(row)[0]
|
||||
results[key] = row[key]
|
||||
completed++
|
||||
|
||||
if (completed === queries.length) {
|
||||
resolve({
|
||||
total_sources: results.total_sources || 0,
|
||||
pending_sources: results.pending_sources || 0,
|
||||
pending_reports: results.pending_reports || 0,
|
||||
high_risk_sources: results.high_risk_sources || 0,
|
||||
sources_added_week: results.sources_added_week || 0,
|
||||
reports_today: results.reports_today || 0
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return res.status(200).json(stats)
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database error:', error)
|
||||
return res.status(500).json({ error: 'Internal server error' })
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user