47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/common.php';
|
|
|
|
header('Content-Type: application/json');
|
|
$dbconn = getDBConnection();
|
|
|
|
if (isset($_GET['county'])) {
|
|
try {
|
|
$cwas = ['RLX','JKL','ILN','PBZ','MRX','LWX','RNK'];
|
|
$placeholders = implode(',', array_map(function($i) { return '$' . $i; }, range(1, count($cwas))));
|
|
|
|
$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 IN ($placeholders)
|
|
";
|
|
|
|
$result = pg_query_params($dbconn, $query, $cwas);
|
|
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()]);
|
|
}
|
|
}
|
|
|
|
pg_close($dbconn);
|
|
?>
|