36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getDb } from '@/lib/db';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const db = await getDb();
|
|
|
|
// Get active/recent visitors (last 24h)
|
|
const recent = await db.all(`
|
|
SELECT lat, lon, city, country, count(*) as count
|
|
FROM visitors
|
|
WHERE timestamp > datetime('now', '-24 hours')
|
|
GROUP BY lat, lon
|
|
`);
|
|
|
|
// Get total count
|
|
const total = await db.get(`SELECT count(*) as count FROM visitors`);
|
|
|
|
return NextResponse.json({
|
|
locations: recent.map(r => ({
|
|
location: [r.lat, r.lon],
|
|
size: Math.min(0.1, 0.05 + (r.count * 0.005)), // Scale size by visitor count
|
|
city: r.city,
|
|
country: r.country
|
|
})),
|
|
totalVisitors: total.count,
|
|
active24h: recent.reduce((sum, r) => sum + r.count, 0)
|
|
});
|
|
} catch (error) {
|
|
console.error('Visitor fetch error:', error);
|
|
return NextResponse.json({ locations: [], totalVisitors: 0, active24h: 0 });
|
|
}
|
|
}
|