<& /htmlheader &>
<script type="text/javascript" src="/js/fxheader1.js"></script>
<style type="text/css">
	table.mt {border-width: 1px;border-spacing:0px ;border-style: solid;border-color: #cfcfcf;border-collapse: collapse;background-color: transparent;}
</style>

<h2>MOPS Alerts</h2>
% foreach my $rule (@$rules) {
	<%perl>
		# get type of object rule processes
		$sql = "select distinct object_type from v_alerts where rule = ?";
		my $cursor = $dbh->prepare($sql) || die dbh->errstr;
		$cursor->execute($rule);
		my $result = $cursor->fetchrow_arrayref;
		my $object_type = $result->[0];
	</%perl>
	<h3><b><a href="./rules/<% $rule %>" ><% $rule %></a></b></h3>
	<table id="<% $rule %>" class="mt" cellspacing="0" cellpadding="2" width="550px">
		<tr> 
    	<th class="darkheader">
        Alert ID
    	</th>
    	<th class="darkheader">
        <b><% $object_type eq 'D' ? 'Derived Object Id':'Tracklet Id'%></b>
    	</th>
    	<th class="darkheader">
        Database
    	</th>
    	<th class="darkheader">
        Alert Publish Date (UT)
    	</th>
		</tr>
		<%perl>
			# select all real alerts generated by the current rule.
			$sql = "select alert_id, object_id, db_name, pub_timestamp from v_alerts 
							where classification = ? and rule = ?
							order by pub_timestamp desc";
			$cursor = $dbh->prepare($sql) || die dbh->errstr;
			$cursor->execute($MOPS_EFF_NONSYNTHETIC, $rule) || die dbh->errstr;
			my $alert_page = "";
		 	while (my $row = $cursor->fetchrow_hashref) {
				if ($object_type eq 'D') {
					# get the name of the derived object
					$sql = "select object_name from $row->{db_name}.derivedobjects where derivedobject_id = ?"; 
			  		my $cursor2 = $dbh->prepare($sql) || die dbh->errstr;
					$cursor2->execute($row->{object_id});
					$result = $cursor2->fetchrow_arrayref;
					$alert_page = "/model/$row->{db_name}/search?id=$result->[0]";
				}
				elsif ($object_type eq 'T') {
					$alert_page = "/czar/$row->{db_name}/tracklet?tid=$row->{object_id}";
				}			
		</%perl>
				<tr>
					<td align=center>
%						if ($row->{pub_timestamp}) {						
							<a href="./alert.html?alertId=<% $row->{alert_id} %>&rule=<% $rule %>" ><% $row->{alert_id} %></a>
%						} else {
							<% $row->{alert_id} %>
%						}
					</td>
					<td align=center>
						<a href="<% $alert_page %>"><% $row->{object_id} %></a>
					</td>
					<td align=center>
						<% $row->{db_name} %>
					</td>
					<td align=center>
						<% $row->{pub_timestamp} %>
					</td>
				</tr>
%      }
		</table>
		<script type="text/javascript">
			fxheaderInit("<% $rule %>",300,1,0);
		</script>
		</br>
		</br>
% }
<script type="text/javascript">
	fxheader();
</script>
<& /htmlfooter &>

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

<%once>
use PS::MOPS::Constants qw(:all);
use DBI;
# intialize database variables.
my $dbserver = "mops00.ifa.hawaii.edu";
my $user = "mops";
my $passwd = "mops";
my $inst = "export";
my $connectionStr = "dbi:mysql:host=" . $dbserver . ";database=" . $inst . ";port=3306";

# connect to database
my $dbh = DBI->connect($connectionStr, $user, $passwd) ||
	die dbh->errstr;
</%once>

<%init>
# get list of rules used to generate alerts. By selecting all unique rule names
# from the alerts table.
my $sql = "select distinct rule from export.alerts order by rule";
my $rules = $dbh->selectcol_arrayref($sql);
</%init>