deprecated
[plewww.git] / planetlab / events / index.php
1 <?php
2 // $Id$
3
4 // Require login
5 require_once 'plc_login.php';
6
7 // Get session and API handles
8 require_once 'plc_session.php';
9 global $plc, $api;
10
11 //// Print header
12 require_once 'plc_drupal.php';
13 include 'plc_header.php';
14
15 // Common functions
16 require_once 'plc_functions.php';
17 require_once 'plc_tables.php';
18 require_once 'plc_minitabs.php';
19 require_once 'plc_datepicker.php';
20   
21 // needs much memory
22 ini_set("memory_limit","256M");
23
24 //set default title
25 drupal_set_title('Events');
26
27 // page size
28 $page_size=30;
29
30 $messages = array ();
31
32 //////////////////////////////////////////////////////////// form
33
34 // defaults for day ('j'), 3-letter month ('M') or year ('Y')
35 function the_date ($key,$dateformat) { 
36   if ($_GET[$key]) return $_GET[$key];
37   else return date($dateformat);
38 }
39
40 // fill out dates from now if not specified
41 $from_picker = new PlcDatepicker ('from_date','From (inclusive)');
42 $from_picker->today();
43 $from_html=$from_picker->html();
44 $until_picker = new PlcDatepicker ('until_date','Until (inclusive)');
45 $until_picker->today();
46 $until_html=$until_picker->html();
47
48 $action=l_events();
49
50 $event_form = <<< EOF
51 <form method=get name='events' action='$action' >
52
53 <table class='plc_details'>
54 <tr><td colspan=2>
55 <table> <TR><TD>
56 <input type='radio' name='type' id='events' value='Event' checked='checked'>&nbsp;Events: 
57 </TD><TD>
58 <input type='text' onSelect="submit();" onFocus='events.checked=true' name='event' size=20>
59 </TD></TR><TR><TD>
60 <input type='radio' name='type' id='persons' value='Person'>&nbsp;Persons:
61 </TD><TD>
62 <input type='text' onSelect="submit();" onFocus='persons.checked=true' name='person' size=20>
63 </TD></TR><TR><TD>  
64 <input type='radio' name='type' id='nodes' value='Node'>&nbsp;Nodes:
65 </TD><TD>
66 <input type='text' onSelect="submit();" onFocus='nodes.checked=true' name='node' size=20>
67 </TD></TR><TR><TD>
68 <input type='radio' name='type' id='sites' value='Site'>&nbsp;Sites:
69 </TD><TD>
70 <input type='text' onSelect="submit();" onFocus='sites.checked=true' name='site' size=20>
71 </TD></TR><TR><TD>
72 <input type='radio' name='type' id='slices' value='Slice'>&nbsp;Slices:
73 </TD><TD>
74 <input type='text' onSelect="submit();" onFocus='slices.checked=true' name='slice' size=20>
75 </TD></TR></table>
76 </td></tr>
77
78 <tr><td> $from_html </td><td>$until_html</td></tr>
79 <tr><td colspan=2>&nbsp </td></tr>
80 <TR><TD colspan=2 style='text-align:center'>
81 <input type='submit' align='middle' value='Show Events'>
82 </TD></TR>
83 </table>
84 </form>
85
86 EOF;
87
88 //////////////////////////////////////////////////////////// dates
89 // our format is yyyy/MMM/dd
90 function parse_date ($datestring) {
91   if (empty ($datestring)) {
92     $year="2008"; $month="Jan"; $day="01";
93   } else {
94     list ($year,$month,$day) = split ('[/.-]',$datestring);
95   }
96   $date=sprintf("%s %s %s",$day,$month,$year);
97   $time=strtotime($date);
98   return array($date,$time);
99 }
100
101 function parse_dates () {
102   list($from_date,$from_time) = parse_date($_GET['from_date']);
103   list($until_date,$until_time) = parse_date($_GET['until_date']);
104   return array($from_date,$from_time,$until_date,$until_time);
105 }
106
107 //////////////////////////////////////////////////////////// layout
108 // outline node ids and person ids with a link
109 function e_node ($node_id) {
110   if (! $node_id) return "";
111   return l_node_t($node_id,$node_id);
112 }
113 function e_person ($person_id) {
114   if (! $person_id) return "";
115   return l_person_t($person_id,$person_id);
116 }
117 // xxx broken
118 function e_event ($event_id) {
119   if (! $event_id) return "";
120   return href(l_event("Event","event",$event_id),$event_id);
121 }
122
123 function e_subject ($type,$id) {
124   $mess=$type . " " . $id;
125   switch ($type) {
126   case 'Node': return l_node_t ($id,$mess);
127   case 'Site': return l_site_t ($id,$mess);
128   case 'Person': return l_person_t ($id,$mess);
129   case 'Slice': return l_slice_t ($id,$mess);
130   case 'Role': case 'Key': case 'PCU': case 'Interface': case 'NodeGroup': case "Address":
131     return "$mess";
132   default: return "Unknown $type" . "-" . $id;
133   }
134 }
135
136 // synthesize links to the subject objects from types and ids
137 function e_subjects ($param) {
138   $types=$param['object_types'];
139   $ids=$param['object_ids'];
140   if ( ! $types) return "";
141   return plc_vertical_table(array_map ("e_subject",$types,$ids));
142 }
143
144 function e_issuer ($param) {
145   if ($param['node_id'])        return e_subject('Node',$param['node_id']);
146   if ($param['person_id'])      return e_subject('Person',$param['person_id']);
147   return '???';
148 }
149
150 function e_auth ($event) {
151   if (array_key_exists('auth_type',$event)) 
152     return $event['auth_type'];
153     else
154       return "";
155 }
156
157 function e_fault ($event) {
158   $f=$event['fault_code'];
159   if ($f==0) return "OK";
160   else return $f;
161 }
162
163 ////////////////////////////////////////////////////////////
164 // for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
165 $STEP=24*60*60;
166
167 if ( ! plc_is_admin()) {
168   plc_warning("You need admin role to see this page.");
169
170  } else if (! $_GET['type']) {
171   echo "<h2>Select the events to focus on :</h2>";
172   // print the selection frame
173   echo $event_form;
174   
175  } else {
176
177   $tabs=array();
178   $tabs['Back to events form']=l_events();
179   plc_tabs($tabs);
180
181   // handle dates
182   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
183   // add one day to until_time - otherwise this corresponds to 0:0
184   $until_time += $STEP;
185   if ( ($from_time != 0) && ($until_time != $STEP) && ($from_time > $until_time) ) {
186     $messages[] = "Warning - <from> is after <until>";
187   }
188   
189   $filter=array();
190   // sort events by time is not good enough, let's use event_id
191   $filter['-SORT']='-event_id';
192   if ($from_time != 0) {
193     $filter[']time']=$from_time;
194   }
195   if ($until_time != $STEP) {
196     $filter['[time']=$until_time;
197   }
198
199   //////////////////////////////////////// Events
200   $type=$_GET['type'];
201   if ($type == 'Event') {
202
203    // and the filter applied for fetching events using GetEvent
204     $user_desc=$_GET['event'];
205     if ( ! empty($user_desc)) {
206       // should parse stuff like 45-90,230-3000 - some other day
207       $filter['event_id']=intval($user_desc);
208     }
209     // the filter might be void here - in python we need an empty dict but that's not what we get so
210     if (empty($filter)) {
211       $filter[']time']=0;
212     }
213     $events = $api->GetEvents($filter); 
214     $title="Events matching " . ($user_desc ? $user_desc : "everything");
215     if ($from_time != 0) 
216       $title .= " From " . $from_date;
217     if ($until_time != $STEP) 
218       $title .= " Until " . $until_date;
219
220     // see actual display of $title and $events below
221     
222   } else {
223
224     switch ($type) {
225     case 'Person': 
226       $primary_key='person_id';
227       $string_key='email';
228       $user_input=$_GET['person'];
229       $method="GetPersons";
230       $object_type='Person';
231       break;
232
233     case 'Node': 
234       $primary_key='node_id';
235       $string_key='hostname';
236       $user_input=$_GET['node'];
237       $method="GetNodes";
238       $object_type='Node';
239       break;
240       
241     case 'Site': 
242       $primary_key='site_id';
243       $string_key='login_base';
244       $user_input=$_GET['site'];
245       $method="GetSites";
246       $object_type='Site';
247       break;
248
249     case 'Slice': 
250       $primary_key='slice_id';
251       $string_key='name';
252       $user_input=$_GET['slice'];
253       $method="GetSlices";
254       $object_type='Slice';
255       break;
256     }
257
258     $object_ids=array();
259     $title=sprintf('Events for type %s:',$object_type);
260     foreach ( split(",",$user_input) as $user_desc) {
261       # numeric 
262       if (my_is_int($user_desc)) {
263         $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
264         if (empty ($obj_check)) {
265           $messages[] = "No such " . $primary_key . ": " . $user_desc;
266         } else {
267           $object_ids[] = $obj_check[0][$primary_key];
268           $title .= $user_desc . ", " ;
269         }
270       } else {
271         # string
272         $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
273         if (empty($new_object_ids)) {
274           $messages[] = "No " . $string_key . " matching " . $user_desc;
275         } else {
276           foreach ($new_object_ids as $new_obj_id) {
277             $object_ids[] = $new_obj_id[$primary_key];
278             $title .= $new_obj_id[$primary_key] . ", ";
279           }
280         }
281       }
282     }
283
284     $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
285     // get set of event_ids
286     $event_ids = array_map ( create_function ('$eo','return $eo["event_id"];') , $event_objs);
287     
288     $events = $api->GetEvents (array('event_id'=>$event_ids));
289
290     // see actual display of $title and $events below
291
292   }
293
294   drupal_set_title ($title);
295   // Show messages
296   if (!empty($messages)) 
297     foreach ($messages as $line) 
298       drupal_set_message($line);
299
300   $headers=array("Id"=>"int",
301                  "Time"=>"EnglishDateTime",
302                  "Method"=>"string",
303                  "Message"=>"string",
304                  "Subjects"=>"string",
305                  "Issuer"=>"string",
306                  "Auth"=>"string",
307                  "R"=>"string",
308                  "D"=>"none",
309                  );
310
311   $table = new PlcTable ("events",$headers,"0r");
312   $table->set_options (array ('max_pages'=>20));
313   $table->start ();
314   foreach ($events as $event) {
315
316     // the call button
317     $message = htmlentities($event['message'], ENT_QUOTES);
318     $call = htmlentities($event['call'], ENT_QUOTES);
319     $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
320     $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
321     //    $method = sprintf('<span title="%s">%s</span>',$call,$method);
322
323   // the message button
324     $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
325     $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
326     $details="<input type=button name='message' value='X' onclick='alert(\"" . $text . "\")'";
327     //    $message=sprintf('<span title="%s">%s</span>',$message,$message);
328
329     $message=truncate($event['message'],40);
330     $table->row_start();
331     $table->cell(e_event($event['event_id']));
332     $table->cell(date('M/d/Y H:i', $event['time']));
333     $table->cell($event['call_name']);
334     $table->cell($message);
335     $table->cell(e_subjects($event));
336     $table->cell(e_issuer($event));
337     $table->cell(e_auth($event));
338     $table->cell(e_fault($event));
339     $table->cell($details);
340     $table->row_end();
341   }
342   $table->set_options(array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
343                                            "Click the button in the D(etails) column to get more details")));
344   $table->end();
345   
346  }
347
348
349   // Print footer
350 include 'plc_footer.php';
351
352 ?>
353