fix issues
This commit is contained in:
@@ -1,9 +1,39 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/common.php';
|
require_once __DIR__ . '/common.php';
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
$dbconn = getDBConnection();
|
$dbconn = getDBConnection();
|
||||||
|
|
||||||
|
//no gets, current point outage info
|
||||||
|
if(empty(array_diff_key($_GET, array('service' => '')))) {
|
||||||
|
$result = pg_query_params($dbconn,
|
||||||
|
"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))order by start_time asc)) FROM newpower WHERE cwa = $1 and active = true AND geom IS NOT NULL",
|
||||||
|
array('RLX')) or die('Query failed: ' . pg_last_error());
|
||||||
|
$resultArray = pg_fetch_all($result);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo($resultArray[0]['json_build_object']);
|
||||||
|
}
|
||||||
|
|
||||||
|
//county/state max
|
||||||
|
if($_GET['max'] ?? null) {
|
||||||
|
if($_GET['start'] ?? null) {
|
||||||
|
$starttime = pg_escape_string($_GET['start']);
|
||||||
|
if($_GET['end'] ?? null) {
|
||||||
|
$endtime = pg_escape_string($_GET['end']);
|
||||||
|
|
||||||
|
$result = pg_query_params($dbconn,
|
||||||
|
"select distinct on (county,state) max(outages),county,state from (select distinct on (county,state,fetch_time) county,state,sum(outages) as outages, fetch_time as time, sum(served) as served from newcountyoutages where fetch_time > $2 and fetch_time < $3 and cwa = $1 group by county,state,fetch_time) as potato group by county,state",
|
||||||
|
array('RLX',$starttime,$endtime)) or die('Query failed: ' . pg_last_error());
|
||||||
|
|
||||||
|
$array = array();
|
||||||
|
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
|
||||||
|
$array[] = $line;
|
||||||
|
}
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode($array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($_GET['county'])) {
|
if (isset($_GET['county'])) {
|
||||||
try {
|
try {
|
||||||
$cwas = ['RLX','JKL','ILN','PBZ','MRX','LWX','RNK'];
|
$cwas = ['RLX','JKL','ILN','PBZ','MRX','LWX','RNK'];
|
||||||
@@ -13,16 +43,16 @@ if (isset($_GET['county'])) {
|
|||||||
WITH latest_fetch AS (
|
WITH latest_fetch AS (
|
||||||
SELECT MAX(fetch_time) as max_fetch_time FROM newcountyoutages
|
SELECT MAX(fetch_time) as max_fetch_time FROM newcountyoutages
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
n.county,
|
n.county,
|
||||||
n.state,
|
n.state,
|
||||||
n.outages as outage,
|
n.outages as outage,
|
||||||
n.fetch_time as time,
|
n.fetch_time as time,
|
||||||
n.served,
|
n.served,
|
||||||
CASE
|
CASE
|
||||||
WHEN n.served > 0 THEN ROUND(CAST((n.outages::FLOAT / n.served) * 100 AS NUMERIC), 2)
|
WHEN n.served > 0 THEN ROUND(CAST((n.outages::FLOAT / n.served) * 100 AS NUMERIC), 2)
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END as perout
|
END as perout
|
||||||
FROM newcountyoutages n, latest_fetch
|
FROM newcountyoutages n, latest_fetch
|
||||||
WHERE n.fetch_time = latest_fetch.max_fetch_time
|
WHERE n.fetch_time = latest_fetch.max_fetch_time
|
||||||
AND n.cwa IN ($placeholders)
|
AND n.cwa IN ($placeholders)
|
||||||
@@ -34,14 +64,88 @@ if (isset($_GET['county'])) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$results = pg_fetch_all($result) ?: [];
|
$results = pg_fetch_all($result) ?: [];
|
||||||
|
header('Content-Type: application/json');
|
||||||
echo json_encode($results);
|
echo json_encode($results);
|
||||||
|
|
||||||
pg_free_result($result);
|
pg_free_result($result);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
header('Content-Type: application/json');
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(['error' => 'Query execution failed: ' . $e->getMessage()]);
|
echo json_encode(['error' => 'Query execution failed: ' . $e->getMessage()]);
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pg_close($dbconn);
|
//county archive
|
||||||
|
if($_GET['countyarchive'] ?? null) {
|
||||||
|
if($_GET['start'] ?? null) {
|
||||||
|
$starttime = pg_escape_string($_GET['start']);
|
||||||
|
if($_GET['end'] ?? null) {
|
||||||
|
$endtime = pg_escape_string($_GET['end']);
|
||||||
|
|
||||||
|
$result = pg_query_params($dbconn,
|
||||||
|
"select distinct on (county,state,fetch_time) county,state,sum(outages) as outage, fetch_time as time, sum(served) as served from newcountyoutages where fetch_time > $9 and fetch_time < $10 and (cwa = $1 or cwa = $2 or cwa = $3 or cwa = $4 or cwa = $5 or cwa = $6 or cwa = $7 or cwa = $8) group by county,state,fetch_time",
|
||||||
|
array('RLX','JKL','ILN','PBZ','MRX','LWX','RNK','CTP',$starttime,$endtime)) or die('Query failed: ' . pg_last_error());
|
||||||
|
|
||||||
|
$array = array();
|
||||||
|
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
|
||||||
|
$array[] = $line;
|
||||||
|
}
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode($array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Archive point data
|
||||||
|
if($_GET['archivepoint'] ?? null) {
|
||||||
|
$starttime = pg_escape_string($_GET['start']);
|
||||||
|
$endtime = pg_escape_string($_GET['end']);
|
||||||
|
$result = pg_query_params($dbconn,
|
||||||
|
"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))order by start_time asc)) FROM newpower WHERE cwa = $1 and start_time > $2 and last_change < $3 AND geom IS NOT NULL",
|
||||||
|
array('RLX',$starttime,$endtime)) or die('Query failed: ' . pg_last_error());
|
||||||
|
$resultArray = pg_fetch_all($result);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo($resultArray[0]['json_build_object']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Svr related functionality
|
||||||
|
if($_GET['svr'] ?? null =='current') {
|
||||||
|
$result = pg_query($dbconn,
|
||||||
|
"SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry', ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue < now() and endtime > now()") or die('Query failed: ' . pg_last_error());
|
||||||
|
$resultArray = pg_fetch_all($result);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo($resultArray[0]['json_build_object']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($_GET['svr'] ?? null == 'archive') {
|
||||||
|
if($_GET['start'] ?? null) {
|
||||||
|
$starttime = pg_escape_string($_GET['start']);
|
||||||
|
if($_GET['end'] ?? null) {
|
||||||
|
$endtime = pg_escape_string($_GET['end']);
|
||||||
|
|
||||||
|
$result = pg_query_params($dbconn,
|
||||||
|
"SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry', ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue > $1 and endtime < $2"
|
||||||
|
,array($starttime,$endtime)) or die('Query failed: ' . pg_last_error());
|
||||||
|
$resultArray = pg_fetch_all($result);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo($resultArray[0]['json_build_object']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!isset($_GET['start']) && !isset($_GET['end'])) {
|
||||||
|
$result = pg_query($dbconn,
|
||||||
|
"SELECT json_build_object('type', 'FeatureCollection','features', json_agg(json_build_object('type','Feature', 'geometry', ST_AsGeoJSON(nwspoly)::json,'properties',json_build_object('issue',issue,'end',endtime,'vtec',vtec,'type',warntype)))) FROM svr where issue < now() - interval '24 hours' and endtime > now() - interval '24 hours'") or die('Query failed: ' . pg_last_error());
|
||||||
|
$resultArray = pg_fetch_all($result);
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo($resultArray[0]['json_build_object']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pg_free_result($result);
|
||||||
|
|
||||||
|
// Close database connection when needed
|
||||||
|
if (isset($dbconn)) {
|
||||||
|
pg_close($dbconn);
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
@@ -1,21 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/common.php';
|
require_once __DIR__ . '/common.php';
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a JSON error response and terminates the script.
|
|
||||||
* @param int $code HTTP status code.
|
|
||||||
* @param string $message User-facing error message.
|
|
||||||
* @param string|null $log_message Optional message for the server log.
|
|
||||||
*/
|
|
||||||
function send_error($code, $message, $log_message = null) {
|
|
||||||
http_response_code($code);
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
|
||||||
if ($log_message) {
|
|
||||||
error_log($log_message);
|
|
||||||
}
|
|
||||||
echo json_encode(['error' => $message]);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles power outage requests for the new schema.
|
* Handles power outage requests for the new schema.
|
||||||
|
|||||||
Reference in New Issue
Block a user