Skip to content

Instantly share code, notes, and snippets.

@jhavard
Created January 4, 2016 18:28
Show Gist options
  • Save jhavard/a02a181420ceec142d10 to your computer and use it in GitHub Desktop.
Save jhavard/a02a181420ceec142d10 to your computer and use it in GitHub Desktop.
Stupid simple web interface to AWIPS fxatext database
# snippet from httpd.conf, borrowed from wordpress .htaccess
# because I didn't feel like thinking about rewrite rules
<Location "/text">
RewriteEngine On
RewriteBase /
RewriteRule ^text\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /text.php [L]
</Location>
<!DOCTYPE html>
<head>
<title>Text Products</title>
<style>
thead {
background-color: #666699;
color: white;
font-weight: bold;
}
tr:nth-child(even) {
background-color: #b7b7b7;
}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
$db = pg_connect('dbname=fxatext host=dx1 user=awips');
# returns array(nnnid,xxxid)
function pil_split($pil) {
$nnnid = str_pad(substr($pil, 0, 3), 3);
$xxxid = str_pad(substr($pil,3,3), 3);
return array($nnnid,$xxxid);
}
function site_product_listing($site) {
$res = pg_query_params("select cccid,nnnid || xxxid as pil,max(inserttime) from stdtextproducts where cccid = $1 group by cccid,nnnid,xxxid order by pil", array($site));
print "<table><thead><tr><td>cccid</td><td>pil</td><td>Last Seen</td></tr></thead>\n";
while ($row = pg_fetch_array($res)) {
printf("<tr><td>%s</td><td><a href=\"/text/%s/%s\">%s</a></td><td>%s</td></tr>\n", $row['cccid'], $row['cccid'], $row['pil'], $row['pil'], $row['max']);
}
print "</table>\n";
}
function get_all_pil($pil) {
print "<pre>\n";
$res = pg_query_params("SELECT * FROM STDTEXTPRODUCTS WHERE NNNID = $1 AND XXXID = $2 ORDER BY INSERTTIME DESC", pil_split($pil));
while ($row = pg_fetch_array($res)) {
print $row['product'];
print "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
}
print "* * * * * END OF LISTING * * * * *\n";
print "</pre>\n";
}
function top_level_listing() {
$res = pg_query("select distinct cccid, max(inserttime) as max from stdtextproducts group by cccid order by cccid");
print "<table><thead><tr><td>CCCID</td><td>Last Seen</td></tr></thead>\n";
while ($row = pg_fetch_array($res)) {
$cccid = $row['cccid'];
$seen = $row['max'];
print "<tr><td><a href=\"/text/$cccid\">$cccid</a></td><td>$seen</td></tr>\n";
}
print "</table>\n";
}
if (array_key_exists('REDIRECT_URL', $_SERVER)) {
$pi = preg_split("/\//", strtoupper($_SERVER['REDIRECT_URL']));
} else {
$pi = array('/');
}
$l = count($pi);
if ($l == 1) {
top_level_listing();
} elseif ($l == 3) {
$site = $pi[2];
if ($site == "") {
top_level_listing();
} else {
site_product_listing($site);
}
} elseif ($l == 4) {
$site = $pi[2];
$prod = $pi[3];
if ($prod == "") {
site_product_listing($site);
} else {
get_all_pil($prod);
}
} elseif ($l == 5) {
$site = $pi[2];
$prod = $pi[3];
$item = $pi[4];
print "Well, if you want to get specific, you can have $item for $prod from $site\n";
} else {
print "Count of $l exceptional...\n";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment