'FeatureCollection', 'features' => []]); } pg_free_result($result); } catch (Exception $e) { http_response_code(500); die(json_encode(['error' => 'Query execution failed: ' . $e->getMessage()])); } } // Get current county outages if (isset($_GET['county'])) { try { $query = " WITH latest_fetch AS ( SELECT MAX(fetch_time) as max_fetch_time FROM newcountyoutages ) SELECT n.county, n.state, n.outages as outage, n.fetch_time as time, n.served, CASE WHEN n.served > 0 THEN ROUND(CAST((n.outages::FLOAT / n.served) * 100 AS NUMERIC), 2) ELSE 0 END as perout FROM newcountyoutages n, latest_fetch WHERE n.fetch_time = latest_fetch.max_fetch_time AND n.cwa = $1 "; $result = pg_query_params($dbconn, $query, ['RLX']); if ($result === false) { throw new Exception('Query failed: ' . pg_last_error()); } $results = pg_fetch_all($result) ?: []; echo json_encode($results); pg_free_result($result); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => 'Query execution failed: ' . $e->getMessage()]); } } // Note: Other endpoints from the original powerapi.php can be migrated here as needed, // such as 'states', 'max', 'countyarchive', 'archivepoint', 'svr', 'svrpolys', // 'powerids', 'poweridsgeojson', and 'polygongeojson'. // The queries would need to be updated to use the 'newpower' and 'newcountyoutages' tables // and their corresponding columns (e.g., start_time, geom, fetch_time). pg_close($dbconn); ?>