false, 'message' => 'Proxmox API token not yet configured.', 'vms' => [], 'nodes' => [], ]); exit; } // Serve from cache (refreshed by stats_cache.php cron every 5 min) $cached = JarvisDB::query( 'SELECT data, UNIX_TIMESTAMP(updated_at) as updated_ts FROM api_cache WHERE cache_key=? LIMIT 1', ['proxmox'] ); if ($cached && !empty($cached[0]['data'])) { $row = $cached[0]; $data = json_decode($row['data'], true); // Add cache age to response $data['cache_age_s'] = (int)(time() - (int)$row['updated_ts']); echo json_encode($data); } else { // Cache empty — return placeholder so UI shows something useful echo json_encode([ 'configured' => true, 'node' => PROXMOX_NODE, 'node_status' => null, 'vms' => [], 'containers' => [], 'vm_count' => 0, 'ct_count' => 0, 'cached_at' => null, 'cache_age_s' => -1, 'message' => 'Cache warming up — first update in under 5 minutes.', ]); }