{
'events' : [
% foreach my $thing (@stuff) {
    {
        'start' : '<% $thing->{start} %>' ,
        'title' : '<% $thing->{title} %>',
        'icon' : '<% $thing->{icon} %>',
        'link' : '<% $thing->{link} %>', 
        'image' : '<% $thing->{image} %>',
        'description' : '<% $thing->{title} %> <% $thing->{description} %>'
    },
% }
]
}


<%init>

my %monfs = qw(
    1 Jan
    2 Feb
    3 Mar
    4 Apr
    5 May
    6 Jun
    7 Jul
    8 Aug
    9 Sep
    10 Oct
    11 Nov
    12 Dec
);

my $inst = $m->notes('INST');
my $model = $m->notes('MODEL');

use Astro::Time;
use PS::MOPS::Constants qw(:all);
use DBI;

my @stuff;


my $SERVER = "mops64.lsstcorp.org";
my $USER = "mops";
my $PASSWD = "mops";
my $DB = "openfire";
my $NODE_ROOT = "home/" . $SERVER . "/";
my $MAX_ROWS = 50;
my $npages = 0;
my $aref;
my @alerts;
my $n;
my $sec;
my $min;
my $hour;
my $mday;
my $mon;
my $year;
my $wday;
my $yday;
my $isdst;
my $t;
my $creationDate;
my $nodeID;

my $connectionStr = "dbi:mysql:host=" . $SERVER . ";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 pubsubItem where nodeID like '" . $NODE_ROOT . "%'", 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 pubsubItem where nodeID like '" . $NODE_ROOT . "%' order by nodeID asc, creationDate desc");
$cur->{mysql_use_result} = 1;
$cur->execute;

while($aref = $cur->fetchrow_hashref) {
    # Fix date: we have here the number of millisecs since 1970.
    $t = $aref->{creationDate} / 1000.0;
    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($t);
    $creationDate = sprintf("%s %02d %4d %02d:%02d:%02d GMT", $monfs{$mon+1}, $mday, $year+1900, $hour, $min, $sec);
    
    # Fix node name.
    $nodeID = $aref->{nodeID};
    $nodeID =~ s/$NODE_ROOT//;

    push @stuff, {
        start => $creationDate,
        icon => '/images/blue-circle.png',
        title => 'MOPS Alert',
        link => "/model/alerts/alert.xml?serviceID=" . $aref->{serviceID} . "&nodeID=" . $aref->{nodeID} . "&id=" . $aref->{id},
        image => "",
        description => "\"" . $aref->{id} . "\" published on " . $creationDate . " to channel \"" . $nodeID . "\"",
    };
}

$r->content_type('text/javascript');
</%init>
