This commit is contained in:
2025-12-10 02:18:45 +00:00
parent 4997c3d85b
commit 4aba2c4273
7 changed files with 688 additions and 0 deletions

98
php/newpowerapi.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
require_once __DIR__ . '/common.php';
header('Content-Type: application/json');
$dbconn = getDBConnection();
// Default endpoint: Get current point outages
if (empty($_GET)) {
try {
$query = "
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(geom)::json,
'properties', json_build_object(
'time', start_time,
'county', county,
'state', state,
'outage', outagen,
'lastchange', last_change,
'cause', cause,
'area_geometry', ST_AsGeoJSON(COALESCE(realareageom, geom))::json
)
)
ORDER BY start_time ASC
)
) as geojson
FROM newpower
WHERE cwa = $1 AND active = true
";
$result = pg_query_params($dbconn, $query, array('RLX'));
if ($result === false) {
throw new Exception('Query failed: ' . pg_last_error());
}
$resultArray = pg_fetch_assoc($result);
if ($resultArray && $resultArray['geojson']) {
echo $resultArray['geojson'];
} else {
echo json_encode(['type' => '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);
?>