class PlcTable replaces functions plc_table_*
[plewww.git] / planetlab / events / index.php
index 0e2f245..f5c66c0 100644 (file)
@@ -1,6 +1,5 @@
 <?php
-// $Id: index.php 1166 2008-01-29 09:32:26Z amine $
-  //
+// $Id$
 
 // Require login
 require_once 'plc_login.php';
@@ -11,24 +10,63 @@ global $plc, $api;
 
 //// Print header
 require_once 'plc_drupal.php';
-//set default title
-drupal_set_title('Events');
-
 include 'plc_header.php';
 
 // Common functions
 require_once 'plc_functions.php';
-require_once 'plc_sorts.php';
+require_once 'plc_tables.php';
+require_once 'plc_minitabs.php';
   
-// find person roles
-$_person= $plc->person;
-$_roles= $_person['role_ids'];
+// needs much memory
+ini_set("memory_limit","256M");
+
+//set default title
+drupal_set_title('Events');
 
-// paginate unit
+// page size
 $page_size=30;
 
 $messages = array ();
 
+//////////////////////////////////////////////////////////// form
+
+// defaults for day ('j'), 3-letter month ('M') or year ('Y')
+function the_date ($key,$dateformat) { 
+  if ($_GET[$key]) return $_GET[$key];
+  else return date($dateformat);
+}
+
+// fill out dates from now if not specified
+$from_d = the_date('from_d','j');
+$from_m = the_date('from_m','M');
+$from_y = the_date('from_y','Y');
+$until_d = the_date('until_d','j');
+$until_m = the_date('until_m','M');
+$until_y = the_date('until_y','Y');
+
+// create the options area from a list and the selected entry
+function dropdown_options ($array,$selected) {
+  $result="";
+  foreach ($array as $item) {
+    $result.= "<option value=" . $item;
+    if ($item == $selected) $result .= ' selected=selected';
+    $result .= '>' . $item . '</option>';
+  }
+  return $result;
+}
+
+$days=range(1,31);
+$from_d_dropdown_options=dropdown_options($days,$from_d);
+$until_d_dropdown_options=dropdown_options($days,$until_d);
+$months=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
+$from_m_dropdown_options=dropdown_options($months,$from_m);
+$until_m_dropdown_options=dropdown_options($months,$until_m);
+// only propose years ranging from now + 3 full years back
+$this_year=date('Y');
+$years=range($this_year-3,$this_year);
+$from_y_dropdown_options=dropdown_options($years,$from_y);
+$until_y_dropdown_options=dropdown_options($years,$until_y);
 $event_form = <<< EOF
 <form method=get name='F' action='/db/events/index.php' >
 
@@ -58,132 +96,30 @@ $event_form = <<< EOF
 </td></tr>
 
 
-<tr><th>FROM</th> <th>UNTIL</th> </tr>
+<tr><th>FROM (inclusive)</th> <th>UNTIL (inclusive)</th> </tr>
 
 <tr>
       <td>    
-        <SELECT NAME='from_d' >
-       <OPTION>
-       <OPTION VALUE=' 1' >1
-       <OPTION VALUE=' 2' >2
-       <OPTION VALUE=' 3' >3
-       <OPTION VALUE=' 4' >4
-       <OPTION VALUE=' 5' >5
-       <OPTION VALUE=' 6' >6
-       <OPTION VALUE=' 7' >7
-       <OPTION VALUE=' 8' >8
-       <OPTION VALUE=' 9' >9
-       <OPTION VALUE=' 10' >10
-       <OPTION VALUE=' 11' >11
-       <OPTION VALUE=' 12' >12
-       <OPTION VALUE=' 13' >13
-       <OPTION VALUE=' 14' >14
-       <OPTION VALUE=' 15' >15
-       <OPTION VALUE=' 16' >16
-       <OPTION VALUE=' 17' >17
-       <OPTION VALUE=' 18' >18
-       <OPTION VALUE=' 19' >19
-       <OPTION VALUE=' 20' >20
-       <OPTION VALUE=' 21' >21
-       <OPTION VALUE=' 22' >22
-       <OPTION VALUE=' 23' >23
-       <OPTION VALUE=' 24' >24
-       <OPTION VALUE=' 25' >25
-       <OPTION VALUE=' 26' >26
-       <OPTION VALUE=' 27' >27
-       <OPTION VALUE=' 28' >28
-       <OPTION VALUE=' 29' >29
-       <OPTION VALUE=' 30' >30
-       <OPTION VALUE=' 31' >31
+        <SELECT NAME='from_d'>
+$from_d_dropdown_options                                                                
         </SELECT>
-
         <SELECT NAME='from_m' >
-       <OPTION>
-       <OPTION VALUE=' Jan' >January
-       <OPTION VALUE=' Feb' >February
-       <OPTION VALUE=' Mar' >March
-       <OPTION VALUE=' Apr' >April
-       <OPTION VALUE=' May' >May
-       <OPTION VALUE=' Jun' >June
-       <OPTION VALUE=' Jul' >July
-       <OPTION VALUE=' Aug' >August
-       <OPTION VALUE=' Sep' >September
-       <OPTION VALUE=' Oct' >October
-       <OPTION VALUE=' Nov' >November
-       <OPTION VALUE=' Dec' >December
+$from_m_dropdown_options
         </SELECT>
-
         <SELECT NAME='from_y' >
-       <OPTION>
-       <OPTION VALUE='2006' >2006
-       <OPTION VALUE='2007' >2007
-       <OPTION VALUE='2008' >2008
-       <OPTION VALUE='2009' >2009
-       <OPTION VALUE='2010' >2010
-       <OPTION VALUE='2011' >2011
+$from_y_dropdown_options
         </SELECT>
-
 </td>
 
 <TD>
    <SELECT NAME=' until_d' >
-       <OPTION>
-       <OPTION VALUE='1' >1
-       <OPTION VALUE='2' >2
-       <OPTION VALUE='3' >3
-       <OPTION VALUE='4' >4
-       <OPTION VALUE='5' >5
-       <OPTION VALUE='6' >6
-       <OPTION VALUE='7' >7
-       <OPTION VALUE='8' >8
-       <OPTION VALUE='9' >9
-       <OPTION VALUE='10' >10
-       <OPTION VALUE='11' >11
-       <OPTION VALUE='12' >12
-       <OPTION VALUE='13' >13
-       <OPTION VALUE='14' >14
-       <OPTION VALUE='15' >15
-       <OPTION VALUE='16' >16
-       <OPTION VALUE='17' >17
-       <OPTION VALUE='18' >18
-       <OPTION VALUE='19' >19
-       <OPTION VALUE='20' >20
-       <OPTION VALUE='21' >21
-       <OPTION VALUE='22' >22
-       <OPTION VALUE='23' >23
-       <OPTION VALUE='24' >24
-       <OPTION VALUE='25' >25
-       <OPTION VALUE='26' >26
-       <OPTION VALUE='27' >27
-       <OPTION VALUE='28' >28
-       <OPTION VALUE='29' >29
-       <OPTION VALUE='30' >30
-       <OPTION VALUE='31' >31
+$until_d_dropdown_options
     </SELECT>
     <SELECT NAME=' until_m' >
-       <OPTION>
-       <OPTION VALUE='Jan' >January
-       <OPTION VALUE='Feb' >February
-       <OPTION VALUE='Mar' >March
-       <OPTION VALUE='Apr' >April
-       <OPTION VALUE='May' >May
-       <OPTION VALUE='Jun' >June
-       <OPTION VALUE='Jul' >July
-       <OPTION VALUE='Aug' >August
-       <OPTION VALUE='Sep' >September
-       <OPTION VALUE='Oct' >October
-       <OPTION VALUE='Nov' >November
-       <OPTION VALUE='Dec' >December
+$until_m_dropdown_options
    </SELECT>
     <SELECT NAME=' until_y' >
-       <OPTION>
-       <OPTION VALUE='2006' >2006
-       <OPTION VALUE='2007' >2007
-       <OPTION VALUE='2008' >2008
-       <OPTION VALUE='2009' >2009
-       <OPTION VALUE='2010' >2010
-       <OPTION VALUE='2011' >2011
+$until_y_dropdown_options
     </SELECT>
 </td></tr>
 
@@ -195,6 +131,7 @@ $event_form = <<< EOF
 
 EOF;
 
+//////////////////////////////////////////////////////////// dates
 function parse_date ($day,$month,$year) {
   // if everything empty -> unspecified date, return 0
   if ( empty($day) && empty($month) && empty($year)) {
@@ -216,90 +153,95 @@ function parse_dates () {
   return array($from_date,$from_time,$until_date,$until_time);
 }
 
-function my_is_int ($x) {
-    return (is_numeric($x) ? intval($x) == $x : false);
+//////////////////////////////////////////////////////////// layout
+// outline node ids and person ids with a link
+function e_node ($node_id) {
+  if (! $node_id) return "";
+  return l_node_t($node_id,$node_id);
+}
+function e_person ($person_id) {
+  if (! $person_id) return "";
+  return l_person_t($person_id,$person_id);
+}
+// xxx broken
+function e_event ($event_id) {
+  if (! $event_id) return "";
+  return href(l_event("Event","event",$event_id),$event_id);
 }
 
-function truncate ($text,$numb,$etc = "...") {
-  if (strlen($text) > $numb) {
-    $text = substr($text, 0, $numb);
-    $text = $text.$etc;
+function e_subject ($type,$id) {
+  $mess=$type . " " . $id;
+  switch ($type) {
+  case 'Node': return l_node_t ($id,$mess);
+  case 'Site': return l_site_t ($id,$mess);
+  case 'Person': return l_person_t ($id,$mess);
+  case 'Slice': return l_slice_t ($id,$mess);
+  case 'Role': case 'Key': case 'PCU': case 'Interface': case 'NodeGroup': case "Address":
+    return "$mess";
+  default: return "Unknown $type" . "-" . $id;
   }
-  return $text;
 }
 
-// layout function to refine a row's content
-function layout ($param){
-  // format time
-  $time=$param['time'];
-  $date= date('d M Y H:i' ,$time);
-  $param['time']=$date;
-
-  // the call button
-  $message=htmlentities($param['message'], ENT_QUOTES);
-  $call=htmlentities($param['call'], ENT_QUOTES);
-  $detail_text=sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$param['runtime']);
-  $detail="<input type=button name='call' value='" . $param['call_name'] ."' onclick='alert(\"" . $detail_text . "\")'";
-  $detail=sprintf('<span title="%s">%s</span>',$call,$detail);
-  $param['call_name']=$detail;
-  unset ($param['call']);
+// synthesize links to the subject objects from types and ids
+function e_subjects ($param) {
+  $types=$param['object_types'];
+  $ids=$param['object_ids'];
+  if ( ! $types) return "";
+  return plc_vertical_table(array_map ("e_subject",$types,$ids));
+}
 
-  // the message button
-  $trunc_mess=htmlentities(truncate($param['message'],40),ENT_QUOTES);
-  $detail="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $detail_text . "\")'";
-  $detail=sprintf('<span title="%s">%s</span>',$message,$detail);
-  $param['message']=$detail;
-
-  // shrink column name : event_id -> id - paginate_id used in paginate and does not show up
-  $param['<span title="event_id">id</span>']=$param['event_id'] ; 
-  // so that event_id shows up
-  $param['paginate_id']=$param['event_id']; unset($param['event_id']);
-
-  //// shrink column names 
-  $param['<span title="fault_code">fault</span>']=$param['fault_code'] ; unset($param['fault_code']);
-  // seem empty on all rows - probably something that I screwed when importing tony's stuff
-  //  $param['<span title="object_type">oty</span>']=$param['object_type'] ; unset($param['object_type']);
-  //  $param['<span title="object_id">oid</span>']=$param['object_id'] ; unset($param['object_id']);
-  $param['<span title="object_types">otys</span>']=$param['object_types'] ; unset($param['object_types']);
-  $param['<span title="object_ids">oids</span>']=$param['object_ids'] ; unset($param['object_ids']);
-  $param['<span title="node_id">nid</span>']=plc_node_link($param['node_id']) ; unset($param['node_id']);
-  $param['<span title="person_id">pid</span>']= plc_person_link($param['person_id']) ; unset($param['person_id']);
-  if (array_key_exists('auth_type',$param)) {
-    $param['<span title="auth_type">at</span>']=$param['auth_type'] ; unset($param['auth_type']);
-  }
+function e_issuer ($param) {
+  if ($param['node_id'])       return e_subject('Node',$param['node_id']);
+  if ($param['person_id'])     return e_subject('Person',$param['person_id']);
+  return '???';
+}
 
-  // clears
-  unset($param['object_type']);
-  unset($param['object_id']);
-  unset($param['runtime']);
-  return $param;
+function e_auth ($event) {
+  if (array_key_exists('auth_type',$event)) 
+    return $event['auth_type'];
+    else
+      return "";
 }
 
-//plc_debug('GET',$_GET);
+function e_fault ($event) {
+  $f=$event['fault_code'];
+  if ($f==0) return "OK";
+  else return $f;
+}
 
-if ( !in_array ('10', $_roles)) {
-  echo "<div class='plc-warning'> You need admin role to see this page. </div>";
+////////////////////////////////////////////////////////////
+// for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
+$STEP=24*60*60;
 
- } else if (!$_GET['type']) {
+if ( ! plc_is_admin()) {
+  plc_warning("You need admin role to see this page.");
 
-  echo "<h2>What events would you like to consult :</h2>";
+ } else if (! $_GET['type']) {
+  echo "<h2>Select the events to focus on :</h2>";
   // print the selection frame
   echo $event_form;
   
  } else {
 
+  $tabs=array();
+  $tabs['Back to events form']=l_events();
+  plc_tabs($tabs);
+
   // handle dates
   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
-  if ( ($from_time != 0) && ($until_time != 0) && ($from_time > $until_time) ) {
-    $messages[] = "Warning - wrong date selection";
+  // add one day to until_time - otherwise this corresponds to 0:0
+  $until_time += $STEP;
+  if ( ($from_time != 0) && ($until_time != $STEP) && ($from_time > $until_time) ) {
+    $messages[] = "Warning - <from> is after <until>";
   }
   
   $filter=array();
+  // sort events by time is not good enough, let's use event_id
+  $filter['-SORT']='-event_id';
   if ($from_time != 0) {
     $filter[']time']=$from_time;
   }
-  if ($until_time != 0) {
+  if ($until_time != $STEP) {
     $filter['[time']=$until_time;
   }
 
@@ -318,30 +260,14 @@ if ( !in_array ('10', $_roles)) {
       $filter[']time']=0;
     }
     $events = $api->GetEvents($filter); 
-    if (empty($events)) {
-      $messages[] = "No event found - user input was [" . $user_desc . "]";
-    } else {
-      $title="Events matching " . ($user_desc ? $user_desc : "everything");
-      if ($from_time != 0) 
-       $title .= " From " . $from_date;
-      if ($until_time != 0) 
-       $title .= " Until " . $until_date;
-      drupal_set_title ($title);
-    }
-
-    // Show messages
-    if (!empty($messages)) {
-      print '<div class="messages plc-warning"><ul>';
-      foreach ($messages as $line) {
-       print "<li> $line";
-      }
-      print "</ul></div>";
-    }
-       
-    if ( ! empty ($events)) {
-      $events= array_map(layout,$events);
-      echo paginate( $events, "paginate_id", "Events", $page_size, "event_id");
-    }
+    $title="Events matching " . ($user_desc ? $user_desc : "everything");
+    if ($from_time != 0) 
+      $title .= " From " . $from_date;
+    if ($until_time != $STEP) 
+      $title .= " Until " . $until_date;
+
+    // see actual display of $title and $events below
+    
   } else {
 
     switch ($type) {
@@ -403,25 +329,71 @@ if ( !in_array ('10', $_roles)) {
        }
       }
     }
-      
-    // Show messages
-    if (!empty($messages)) {
-      print '<div class="messages plc-warning"><ul>';
-      foreach ($messages as $line) {
-       print "<li> $line";
-      }
-      print "</ul></div>";
-    }
-       
-    drupal_set_title($title);
-    $events = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type));
 
-    $events=array_map(layout,$events);
-    echo paginate( $events, "paginate_id", "--------" . $type . " EVENTS---------", $page_size, "hostname");
+    $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
+    // get set of event_ids
+    $event_ids = array_map ( create_function ('$eo','return $eo["event_id"];') , $event_objs);
+    
+    $events = $api->GetEvents (array('event_id'=>$event_ids));
+
+    // see actual display of $title and $events below
+
   }
+
+  drupal_set_title ($title);
+  // Show messages
+  if (!empty($messages)) 
+    foreach ($messages as $line) 
+      drupal_set_message($line);
+
+  $headers=array("Id"=>"int",
+                "Time"=>"EnglishDateTime",
+                "Method"=>"string",
+                "Message"=>"string",
+                "Subjects"=>"string",
+                "Issuer"=>"string",
+                "Auth"=>"string",
+                "R"=>"string",
+                "D"=>"none",
+                );
+
+  $table = new PlcTable ("events",$headers,"0r");
+  $table->set_options (array ('max_pages'=>20));
+  $table->start ();
+  foreach ($events as $event) {
+
+    // the call button
+    $message = htmlentities($event['message'], ENT_QUOTES);
+    $call = htmlentities($event['call'], ENT_QUOTES);
+    $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
+    $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
+    //    $method = sprintf('<span title="%s">%s</span>',$call,$method);
+
+  // the message button
+    $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
+    $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
+    $details="<input type=button name='message' value='X' onclick='alert(\"" . $text . "\")'";
+    //    $message=sprintf('<span title="%s">%s</span>',$message,$message);
+
+    $message=truncate($event['message'],40);
+    $table->row_start();
+    $table->cell(e_event($event['event_id']));
+    $table->cell(date('M/d/Y H:i', $event['time']));
+    $table->cell($event['call_name']);
+    $table->cell($message);
+    $table->cell(e_subjects($event));
+    $table->cell(e_issuer($event));
+    $table->cell(e_auth($event));
+    $table->cell(e_fault($event));
+    $table->cell($details);
+    $table->row_end();
+  }
+  $table->set_options(array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
+                                          "Click the button in the D(etails) column to get more details")));
+  $table->end();
+  
  }
 
-echo "<br /><p><a href='/db/events/index.php'>Back to Events</a>";
 
   // Print footer
 include 'plc_footer.php';