Fixed and changes

This commit is contained in:
Akshay Kolli
2026-02-08 03:03:53 -05:00
parent 3f72118348
commit dd1a2ea200
13 changed files with 1948 additions and 125 deletions

View File

@@ -1,53 +1,55 @@
import { NextResponse } from 'next/server';
// Configuration - Update these URLs to match your actual services
const SERVICES = [
{ name: 'Website', url: 'https://www.akkolli.net' },
{ name: 'Gitea', url: 'https://code.akkolli.net' },
{ name: 'Nextcloud', url: 'http://localhost:6060' },
];
import { getDb } from '@/lib/db';
export const dynamic = 'force-dynamic';
export async function GET() {
const results = await Promise.all(
SERVICES.map(async (service) => {
const start = performance.now();
try {
// Set a short timeout (e.g., 5 seconds)
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
try {
const db = await getDb();
const response = await fetch(service.url, {
method: 'HEAD',
signal: controller.signal,
cache: 'no-store',
});
// Get live status (most recent log per service)
const live = await db.all(`
SELECT service_name as name, url, status, latency
FROM uptime_logs
WHERE id IN (SELECT MAX(id) FROM uptime_logs GROUP BY service_name)
`);
clearTimeout(timeoutId);
// Get 24h stats
const stats24h = await db.all(`
SELECT service_name,
count(*) as total,
sum(case when status = 'up' then 1 else 0 end) as up_count
FROM uptime_logs
WHERE timestamp > datetime('now', '-24 hours')
GROUP BY service_name
`);
const end = performance.now();
const latency = Math.round(end - start);
// Get 7d stats
const stats7d = await db.all(`
SELECT service_name,
count(*) as total,
sum(case when status = 'up' then 1 else 0 end) as up_count
FROM uptime_logs
WHERE timestamp > datetime('now', '-7 days')
GROUP BY service_name
`);
return {
name: service.name,
url: service.url,
status: response.ok ? 'up' : 'down',
latency: latency,
timestamp: new Date().toISOString(),
};
} catch (error) {
return {
name: service.name,
url: service.url,
status: 'down',
latency: 0,
timestamp: new Date().toISOString(),
error: 'Unreachable'
};
}
})
);
// Merge data
const results = live.map(l => {
const s24 = stats24h.find(s => s.service_name === l.name);
const s7d = stats7d.find(s => s.service_name === l.name);
return NextResponse.json(results);
return {
...l,
uptime24h: s24 ? Math.round((s24.up_count / s24.total) * 100) : 100,
uptime7d: s7d ? Math.round((s7d.up_count / s7d.total) * 100) : 100,
};
});
return NextResponse.json(results);
} catch (error) {
console.error('Uptime stats error:', error);
// Fallback to simple check if DB fails
return NextResponse.json([]);
}
}