query("SELECT * FROM urls WHERE is_active = 1")->fetchAll(); $data = []; foreach ($urls as $url) { // Fetch last 60 logs for the candlestick/sparkline $stmt = $db->prepare("SELECT status, response_time, checked_at FROM logs WHERE url_id = ? ORDER BY checked_at DESC LIMIT 60"); $stmt->execute([$url['id']]); $logs = array_reverse($stmt->fetchAll()); // Calculate uptime percentage (last 24h or total) $stmt = $db->prepare("SELECT COUNT(*) as total, SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as up_count FROM logs WHERE url_id = ? AND checked_at > DATE_SUB(NOW(), INTERVAL 24 HOUR)"); $stmt->execute([$url['id']]); $stats = $stmt->fetch(); $uptime = ($stats['total'] > 0) ? round(($stats['up_count'] / $stats['total']) * 100, 2) : 100; $data[] = [ 'id' => $url['id'], 'name' => $url['name'], 'url' => $url['url'], 'status' => $url['status'], 'last_check' => $url['last_check'], 'response_time' => $url['response_time'], 'uptime' => $uptime, 'history' => $logs ]; } // Check if monitor is running $isMonitorRunning = false; $lockFile = __DIR__ . '/../monitor.lock'; if (file_exists($lockFile)) { $fp = fopen($lockFile, 'r'); if (!flock($fp, LOCK_EX | LOCK_NB)) { $isMonitorRunning = true; } fclose($fp); } echo json_encode([ 'success' => true, 'monitor_running' => $isMonitorRunning, 'data' => $data ]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }