<& /htmlheader &>

<h2>MOPS Alerts</h2>

<b>Displaying <% scalar(@alerts) %> out of <% $n %> alerts</b>
(starting at position <% $start %>)
<br /><br />

<!-- <a href="alerttimeline.html">Alert Timeline</a><br /> -->
<table border=1 cellspacing=2>
<tr> 
    <td>
        <b>Channel</b>
    </td>
    <td>
        <b>Alert ID</b>
    </td>
    <td>
        <b>Publisher</b>
    </td>
    <td>
        <b>Alert Publish Date (UT)</b>
    </td>
</tr>
% foreach my $aref (@alerts) {
<tr> 
    <td>
% # Fix node name.
% my $nodeID = $aref->{nodeID};
% $nodeID =~ s/$NODE_ROOT//;
        <a href="channel.html?nodeID=<% $aref->{nodeID} %>"><% $nodeID %></a>
    </td>
    <td>
        <a href="alert.xml?serviceID=<% $aref->{serviceID} %>&nodeID=<% $aref->{nodeID} %>&id=<% $aref->{id} %>"><% $aref->{id} %></a>
    </td>
    <td>
% # Fix publisher name
% my $jid = substr($aref->{jid}, 0, index($aref->{jid}, "@" . $ALERT_SERVER));
        <% $jid %>
    </td>
    <td>
% # Fix date: we have here the number of millisecs since 1970.
% my $sec;
% my $min;
% my $hour;
% my $mday;
% my $mon;
% my $year;
% my $wday;
% my $yday;
% my $isdst;
% my $t = $aref->{creationDate} / 1000.0;
% ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($t);
% my $creationDate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
        <% $creationDate %>
    </td>
</tr>
% }
</table>

<br />
Pages: 
% my $i;
% my $k;
% for($i=1; $i<=$npages; $i++) {
% $k = $MAX_ROWS * ($i - 1);
% if($k eq $start) {
<% $i %>
% } else {
<a href="index.html?instance=<% $instance %>&start=<% $k %>"><% $i %></a>
% } # if
% } # for
% if($npages eq 0) {
1
% }

<& /htmlfooter &>

<%args>
$start => 0
$instance => ""
</%args>

<%init>
use DBI;
use POSIX qw(ceil);


my $DBSERVER = "mops00.ifa.hawaii.edu";
my $ALERT_SERVER = "mopshq2.ifa.hawaii.edu";
my $USER = "mops";
my $PASSWD = "mops";
my $DB = "openfire";
my $NODE_ROOT = "home/" . $ALERT_SERVER . "/";
my $MAX_ROWS = 50;
my $npages = 0;
my $aref;
my @alerts;
my $n;

my $connectionStr = "dbi:mysql:host=" . $DBSERVER . ";database=" . $DB;
my $dbh = DBI->connect($connectionStr, $USER, $PASSWD);

# Retrieve the number of alerts. We could have $n passed on the URL, but we
# cannot be sure that it would still be valid.
$aref = $dbh->selectrow_hashref("select count(*) as n from ofPubsubItem where nodeID like '" . $NODE_ROOT . "%' and id like '". $instance ."%'", undef);
$n = $aref->{n};

# How many pages to we need to display?
$npages = ceil($n / $MAX_ROWS);

# Now retrieve the requested number of rows.
my $cur = $dbh->prepare("select serviceID, nodeID, id, jid, creationDate from ofPubsubItem where nodeID like '" . $NODE_ROOT . "%' and id like '". $instance ."%' order by creationDate desc limit " . $start ."," . $MAX_ROWS);
$cur->{mysql_use_result} = 1;
$cur->execute;

while($aref = $cur->fetchrow_hashref) {
    push @alerts, $aref;
}
</%init>
