#! /usr/local/bin/perl
# Set our timezone
$ENV{TZ} = "CST6CDT";
$meccadir    = "/edata1/mecca/";
$infodir      = $meccadir."info/";
%states = ("c" => "In cleanup",
           "w" => "Waiting On-deck",
           "i" => "Staging Input Files",
           "o" => "Staging Output Files",
           "r" => "On-deck (Input Staged)",
           "s" => "Submitted (in queue)",
           "a" => "Analysis Phase",
           "d" => "Finished",
           "x" => "Cancelled",
           "h" => "On Hold"
          );
%state_times = ("c" => "CLEAN-STIME",
                "w" => "STARTUP-STIME",
                "i" => "ISR-TIME",
                "o" => "CLEAN-STIME",
                "r" => "ISD-TIME",
                "s" => "SUB-TIME" ,
                "a" => "ANAL-STIME",
                "d" => "DONE-TIME",
                "x" => "CANCEL-TIME",
                "h" => "HOLD-TIME"
               );
%logic = (1 => "Yes",0 => "No");
%stage_logic = (1 => "Yes",0 => "No",2 => "Input Only",3 => "Output Only");
@times = (
["SUB-TIME"     ,"Submitted"],
["STARTUP-STIME","Went On-deck"],
["ISR-TIME"     ,"Requested Input Disk"],
["IMR-TIME"     ,"Requested Input Tape"],
["IMD-TIME"     ,"Input Tape Mounted"],
["ISD-TIME"     ,"Finished Input Staging"],
["STARTUP-ETIME","Finished On-deck"],
["ANAL-STIME"   ,"Began Analysis"],
["OSR-TIME"     ,"Requested Output Disk"],
["ANAL-ETIME"   ,"Finished Analysis"],
["ISF-TIME"     ,"Deallocated Input Disk"],
["CLEAN-STIME"  ,"Began Rundown"],
["OMR-TIME"     ,"Requested Output Tape"],
["OMD-TIME"     ,"Output Tape Mounted"],
["OSF-TIME"     ,"Deallocated Output Disk"],
["OSD-TIME"     ,"Finished Output Staging"],
["DONE-TIME"    ,"Finished Job"],
["CANCEL-TIME"  ,"Cancelled"],
["HOLD-TIME"    ,"Placed on Hold"]
);
@dtimes = (
["STARTUP-STIME","ANAL-STIME",   "Time spent On-deck"],
["IMR-TIME",     "IMD-TIME"     ,"Time for Input Mount"],
["ISR-TIME",     "ISD-TIME"     ,"Time spent Staging Input"],
["ANAL-STIME"   ,"ANAL-ETIME"   ,"Time spent Analyzing"],
["CLEAN-STIME"  ,"DONE-TIME"    ,"Time spent in Rundown"],
["OMR-TIME",     "OMD-TIME"     ,"Time for Output Mount"],
["CLEAN-STIME"  ,"OSD-TIME"     ,"Time spent Staging Output"],
);
# HTML definitions
$color = "bgcolor=949cce";
$elem_color = "bgcolor=949cce";
$head_color = "bgcolor=ff6060";
$query_string = $ENV{QUERY_STRING};
@pairs = split (/&/, $query_string);
foreach $pair ( @pairs ) {
  ($key, $value ) = split ( /=/, $pair );
  $form_key{$key} = $value;
}
print "Content-type: text/html \n";
print "\n";             #End of HTTP header, now start the page
$system = $form_key{system};
$job    = $form_key{job};
# Open info file, store all lines, put into %info
$info_file = $infodir.$system."_".$job.".info";
open INFO,$info_file;
@infolines = <INFO>;
close INFO;
%info      = ();
%info_field      = ();
foreach $line (@infolines) {
  $line =~ s/\s+//;
  ($key,$value) = split /:/,$line;
  $info{$key} = $value;
  $info_field{$key} = 1;
}
$pwd         = $info{PWD};
$platform    = $info{PLATFORM};
$system      = $info{SYSTEM};
$job_num     = $info{JOB};
$run         = $info{ASKRUN};
$askrun      = $info{ASKRUN};
$didrun      = $info{DIDRUN};
$foundrun    = $info{FOUNDRUN};
$state       = $info{STATE};
$staging     = $info{STAGE};
$frtape      = $info{FRTAPE};
$fdtape      = $info{FDTAPE};
$tapenum     = $info{TAPENUM};
$host        = $info{HOST};
$isr_time  = $info{"ISR-TIME"};
$isdisk    = $info{"ISDISK"};
$isd_time  = $info{"ISD-TIME"};
$is_status = $info{"IS-STATUS"};
$isf_time  = $info{"ISF-TIME"};
$istrys    = $info{"ISTRYS"};
$isnfiles  = $info{"ISNFILES"};
$itaped    = $info{"ITAPED"};
$ilabel    = $info{"ILABEL"};
$osr_time  = $info{"OSR-TIME"};
$osdisk    = $info{"OSDISK"};
$os_status = $info{"OS-STATUS"};
$osf_time  = $info{"OSF-TIME"};
$osd_time  = $info{"OSD-TIME"};
$ostrys    = $info{"OSTRYS"};
$osnfiles  = $info{"OSNFILES"};
$otaped    = $info{"OTAPED"};
$iw_events = $info{"IW-EVENTS"};
$is_events = $info{"IS-EVENTS"};
$ia_events = $info{"IA-EVENTS"};
$os_events = $info{"OS-EVENTS"};
$oa_events = $info{"OA-EVENTS"};
if (!$staging) {$staging = 0}
# Document preamble
$next_job = $job+1;
$prev_job = $job-1;
print "<title>Job Info for $system $job_num</title>\n";
print "<center><table border=0 cellpadding=10><tr align=center>\n";
print "<td rowspan=2><a href=http://www-focus.fnal.gov/cgi-bin/home/www/mecca/ShowInfo?system=$system&job=$prev_job>";
print "<img src=http://www-focus.fnal.gov/mecca/back.gif vspace=5 border=0></a>";
print "<br><b>Prev. Job</td>\n";
print "<td><font size=+3><b>Job Info for $system $job_num</b></font></td>\n";
print "<td rowspan=2><a href=http://www-focus.fnal.gov/cgi-bin/home/www/mecca/ShowInfo?system=$system&job=$next_job>";
print "<img src=http://www-focus.fnal.gov/mecca/forward.gif vspace=5 border=0></a>\n";
print "<br><b>Next Job</td>\n";
print "<tr align=center><td>\n";
print "<a href=http://www-focus.fnal.gov/mecca/tablehelp.html#ji>\n";
print "<img border=0 src=http://www-focus.fnal.gov/img/button_help.gif></a>\n";
print "</table></center><p> \n";
### Make nice tables
print "<center><TABLE CELLPADDING=10 BORDER=0> \n";
# Current Status Table
print "<TR><TD VALIGN=TOP>";
print "<table border=1 cellpadding=5> \n";
 print "<tr><th colspan=2 align=center $head_color>Current Job Status</th></tr>\n";
 print "<tr><td $color>Production System</td><td $color>$system</td></tr>\n";
 print "<tr><td $color>Job Number</td><td $color>$job_num</td></tr>\n";
 if ($pwd) {
   @path_parts = split /\//,$pwd;
   $area = pop @path_parts;
   $queue = pop @path_parts;
   print "<tr><td $color>Batch Area</td><td $color>$queue\/$area</td></tr>\n";
 }
 print "<tr><td $color>Staging Requested</td><td $color>$stage_logic{$staging}</td></tr>\n";
 print "<tr><td $color>Job Progress</td><td $color>$states{$state}</td></tr>\n";
 $time_state = time() - $info{$state_times{$state}};
 if ($time_state < 0) {
   $hours = 0;
   $minutes = 0;
   $seconds = 0;
 } else {
   $hours   = int (($time_state)/3600.0);
   $minutes = int (($time_state-3600*$hours)/60.0);
   $seconds = int ($time_state-3600*$hours - 60*$minutes);
 }
 if ($hours < 72  && $state ne "x" && $state ne "d") {
   $time_string = sprintf "%3d:%2.2d:%2.2d",($hours,$minutes,$seconds);
   print "<tr><td $color>Time in State</td><td $color>$time_string</td></tr>\n";
 }
# Run information table
print "<tr><th colspan=2 align=center $head_color>Run/Tape Info</th></tr>\n";
if ($frtape && $fdtape) {
  print "<tr><td $color>Input Tape </td><td $color>$fdtape</td></tr> \n";
  print "<tr><td $color>Output Tape</td><td $color>$frtape</td></tr> \n";
} else {
  print "<tr><td $color>Tape Number</td><td $color>$tapenum</td></tr> \n";
}
if ($ilabel) {TableElement("Internal Label",$ilabel)}
if ($itaped) {print "<tr><td $color>Input Tape Drive</td><td $color>$itaped</td></tr>\n"}
if ($otaped) {print "<tr><td $color>Output Tape Drive</td><td $color>$otaped</td></tr>\n"}
if ($askrun) {
  print "<tr><td $color>Requested Run</td><td $color>$askrun</td></tr> \n";
}
if ($didrun) {
  print "<tr><td $color>Analyzed or Staged Run</td><td $color>$didrun</td></tr> \n";
}
if ($foundrun) {
  @found_lines = grep /FOUNDRUN/,@infolines;
  foreach $line (@found_lines) {
    $line =~ s/\n//;
    ($junk,$frun) = split /:/,$line;
    push @allruns,$frun;
  }
  print "<tr><td $color>Runs found on tape</td><td $color>@allruns</td></tr> \n";
}
if ($staging) {
  $sisr_time = MeccaTime($isr_time);
  $sosr_time = MeccaTime($osr_time);
  $sisd_time = MeccaTime($isd_time);
  $sosd_time = MeccaTime($osd_time);
  $sisf_time = MeccaTime($isf_time);
  $sosf_time = MeccaTime($osf_time);
  print "<tr><th colspan=2 align=center $head_color>Staging Info</th></tr>\n";
  if ($isr_time) {TableElement("Input Disk Requested",$sisr_time)}
  if ($isdisk)   {TableElement("Input Staging Disk","$isdisk on $host")}
  if ($isd_time) {TableElement("Input Staging Completed",$sisd_time)}
  if ($isf_time) {TableElement("Input Disk Deallocated",$sisf_time)}
  if ($istrys)   {TableElement("# of Input attempts",$istrys)}
  if ($isnfiles) {TableElement("# of files input",$isnfiles)}
                  TableElement("Input Staging Successful","$logic{$is_status} &nbsp");
  if ($osr_time) {TableElement("Output Disk Requested",$sosr_time)}
  if ($osdisk)   {TableElement("Output Staging Disk","$osdisk on $host")}
  if ($osd_time) {TableElement("Output Staging Completed",$sosd_time)}
  if ($osf_time) {TableElement("Output Disk Deallocated",$sosf_time)}
  if ($ostrys)   {TableElement("# of Output stage attempts",$ostrys)}
  if ($osnfiles) {TableElement("# of files output",$osnfiles)}
                  TableElement("Output Staging Successful","$logic{$os_status} &nbsp");
}
print "</table> \n";
print "</TD>\n";
print "<TD VALIGN=TOP>\n";
# Times Table
print "<table border=1 cellpadding=5> \n";
if ($iw_events || $is_events || $ia_events || $oa_events || $os_events) {
  print "<tr><th colspan=2 align=center $head_color>Event Counts</th></tr>\n";
}
if ($iw_events) {TableElement("Input Events  (Whyme)",    $iw_events)}
if ($is_events) {TableElement("Input Events  (Staged)",   $is_events)}
if ($ia_events) {TableElement("Input Events  (Analyzed)", $ia_events)}
if ($oa_events) {TableElement("Output Events (Analyzed)", $oa_events)}
if ($os_events) {TableElement("Output Events (Staged)",   $os_events)}
print "<tr><th colspan=2 align=center $head_color>Action Times</th></tr>\n";
for $i (0..$#times) {
  $description = $times[$i][1];
  $ntime       = $info{$times[$i][0]};
  if ($ntime) {
    $time_string = MeccaTime($ntime);
    TableElement($description,$time_string);
  }
}
for $i (0..$#dtimes) {
  $description  = $dtimes[$i][2];
  $ntime1       = $info{$dtimes[$i][0]};
  $ntime2       = $info{$dtimes[$i][1]};
  if ($ntime2 < $ntime1) {
    $hours = 0;
    $minutes = 0;
    $seconds = 0;
  } else {
    $hours   = int (($ntime2 - $ntime1)/3600.0);
    $minutes = int ((($ntime2 - $ntime1-3600*$hours)/60.0));
    $seconds = int ($ntime2 - $ntime1-3600*$hours - 60*$minutes);
  }
  if ($ntime1 && $ntime2) {
    $time_string = sprintf "%3d:%2.2d:%2.2d",($hours,$minutes,$seconds);
    TableElement($description,$time_string);
  }
}
print "</table> \n";
print "</TD></TR>\n";
# Staging info Table
# End Master Table
print "</TABLE></center>\n";
print "<p> \n";
# Print out full file
print "<p><hr size=5>";
print "<center><font size=+2>Raw Info File</font></center>";
print "<hr width=80%><p> \n";
print "<pre> \n";
foreach $line (@infolines) {
  print "$line \n";
}
print "</pre> \n";
sub MeccaTime ($) {
  $numerical_time = $_[0];
  $time_string = localtime($numerical_time);
  ($wday,$month,$day,$time,$year) = split /\s+/,$time_string;
  $time_string = "$time $day-$month-$year";
  return $time_string;
}
sub TableElement ($$) {
  $label = $_[0];
  $value = $_[1];
  print "<tr><td $elem_color>$label</td><td $elem_color>$value</td></tr>\n";
  return;
}