625ae3c95c84ce9c972f71afdf33223bd0cf7acc
[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   
20 // needs much memory
21 ini_set("memory_limit","256M");
22
23 //set default title
24 drupal_set_title('Events');
25
26 // page size
27 $page_size=30;
28
29 $messages = array ();
30
31 //////////////////////////////////////////////////////////// form
32
33 // defaults for day ('j'), 3-letter month ('M') or year ('Y')
34 function the_date ($key,$dateformat) { 
35   if ($_GET[$key]) return $_GET[$key];
36   else return date($dateformat);
37 }
38
39 // fill out dates from now if not specified
40 $from_d = the_date('from_d','j');
41 $from_m = the_date('from_m','M');
42 $from_y = the_date('from_y','Y');
43 $until_d = the_date('until_d','j');
44 $until_m = the_date('until_m','M');
45 $until_y = the_date('until_y','Y');
46
47 // create the options area from a list and the selected entry
48 function dropdown_options ($array,$selected) {
49   $result="";
50   foreach ($array as $item) {
51     $result.= "<option value=" . $item;
52     if ($item == $selected) $result .= ' selected=selected';
53     $result .= '>' . $item . '</option>';
54   }
55   return $result;
56 }
57
58 $days=range(1,31);
59 $from_d_dropdown_options=dropdown_options($days,$from_d);
60 $until_d_dropdown_options=dropdown_options($days,$until_d);
61 $months=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
62 $from_m_dropdown_options=dropdown_options($months,$from_m);
63 $until_m_dropdown_options=dropdown_options($months,$until_m);
64 // only propose years ranging from now + 3 full years back
65 $this_year=date('Y');
66 $years=range($this_year-3,$this_year);
67 $from_y_dropdown_options=dropdown_options($years,$from_y);
68 $until_y_dropdown_options=dropdown_options($years,$until_y);
69  
70 $event_form = <<< EOF
71 <form method=get name='F' action='/db/events/index.php' >
72
73 <table align='bottom'>
74 <tr><td colspan=2>
75 <table> <TR><TD>
76 <input type='radio' name='type' id='events' value='Event' checked='checked'>&nbsp;Events: 
77 </TD><TD>
78 <input type='text' onSelect="submit();" onFocus='events.checked=true' name='event' size=20>
79 </TD></TR><TR><TD>
80 <input type='radio' name='type' id='persons' value='Person'>&nbsp;Persons:
81 </TD><TD>
82 <input type='text' onSelect="submit();" onFocus='persons.checked=true' name='person' size=20>
83 </TD></TR><TR><TD>  
84 <input type='radio' name='type' id='nodes' value='Node'>&nbsp;Nodes:
85 </TD><TD>
86 <input type='text' onSelect="submit();" onFocus='nodes.checked=true' name='node' size=20>
87 </TD></TR><TR><TD>
88 <input type='radio' name='type' id='sites' value='Site'>&nbsp;Sites:
89 </TD><TD>
90 <input type='text' onSelect="submit();" onFocus='sites.checked=true' name='site' size=20>
91 </TD></TR><TR><TD>
92 <input type='radio' name='type' id='slices' value='Slice'>&nbsp;Slices:
93 </TD><TD>
94 <input type='text' onSelect="submit();" onFocus='slices.checked=true' name='slice' size=20>
95 </TD></TR></table>
96 </td></tr>
97
98
99 <tr><th>FROM (inclusive)</th> <th>UNTIL (inclusive)</th> </tr>
100
101 <tr>
102       <td>    
103         <SELECT NAME='from_d'>
104 $from_d_dropdown_options                                                                 
105         </SELECT>
106         <SELECT NAME='from_m' >
107 $from_m_dropdown_options
108         </SELECT>
109         <SELECT NAME='from_y' >
110 $from_y_dropdown_options
111         </SELECT>
112 </td>
113
114 <TD>
115    <SELECT NAME=' until_d' >
116 $until_d_dropdown_options
117     </SELECT>
118     <SELECT NAME=' until_m' >
119 $until_m_dropdown_options
120    </SELECT>
121     <SELECT NAME=' until_y' >
122 $until_y_dropdown_options
123     </SELECT>
124 </td></tr>
125
126 <TR><TD colspan=2>
127 <input type='submit' align='middle' value='Show Events'>
128 </TD></TR>
129 </table>
130 </form>
131
132 EOF;
133
134 //////////////////////////////////////////////////////////// dates
135 function parse_date ($day,$month,$year) {
136   // if everything empty -> unspecified date, return 0
137   if ( empty($day) && empty($month) && empty($year)) {
138     return array ("xxx",0);
139   } else {
140     // fill missing fields with current value
141     if (empty($day)) $day=date('d');
142     if (empty($month)) $month=date('M');
143     if (empty($year)) $year=date('Y');
144     $date=sprintf("%s %s %s",$day,$month,$year);
145     $time=strtotime($date);
146     return array($date,$time);
147   }
148 }
149
150 function parse_dates () {
151   list($from_date,$from_time) = parse_date($_GET['from_d'],$_GET['from_m'],$_GET['from_y']);
152   list($until_date,$until_time) = parse_date($_GET['until_d'],$_GET['until_m'],$_GET['until_y']);
153   return array($from_date,$from_time,$until_date,$until_time);
154 }
155
156 function my_is_int ($x) {
157     return (is_numeric($x) ? intval($x) == $x : false);
158 }
159
160 //////////////////////////////////////////////////////////// layout
161 function truncate ($text,$numb,$etc = "...") {
162   if (strlen($text) > $numb) {
163     $text = substr($text, 0, $numb);
164     $text = $text.$etc;
165   }
166   return $text;
167 }
168
169 // outline node ids and person ids with a link
170 function e_node ($node_id) {
171   if (! $node_id) return "";
172   return l_node_t($node_id,$node_id);
173 }
174 function e_person ($person_id) {
175   if (! $person_id) return "";
176   return l_person_t($person_id,$person_id);
177 }
178 // xxx broken
179 function e_event ($event_id) {
180   if (! $event_id) return "";
181   return href(l_event("Event","event",$event_id),$event_id);
182 }
183
184 function e_subject ($type,$id) {
185   $mess=$type . " " . $id;
186   switch ($type) {
187   case 'Node': return l_node_t ($id,$mess);
188   case 'Site': return l_site_t ($id,$mess);
189   case 'Person': return l_person_t ($id,$mess);
190   case 'Slice': return l_slice_t ($id,$mess);
191   case 'Role': case 'Key': case 'PCU': case 'Interface': case 'NodeGroup': case "Address":
192     return "$mess";
193   default: return "Unknown $type" . "-" . $id;
194   }
195 }
196
197 // synthesize links to the subject objects from types and ids
198 function e_subjects ($param) {
199   $types=$param['object_types'];
200   $ids=$param['object_ids'];
201   if ( ! $types) return "";
202   return plc_vertical_table(array_map ("e_subject",$types,$ids));
203 }
204
205 function e_issuer ($param) {
206   if ($param['node_id'])        return e_subject('Node',$param['node_id']);
207   if ($param['person_id'])      return e_subject('Person',$param['person_id']);
208   return '???';
209 }
210
211 function e_auth ($event) {
212   if (array_key_exists('auth_type',$event)) 
213     return $event['auth_type'];
214     else
215       return "";
216 }
217
218 function e_fault ($event) {
219   $f=$event['fault_code'];
220   if ($f==0) return "OK";
221   else return $f;
222 }
223
224 ////////////////////////////////////////////////////////////
225 // for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
226 $STEP=24*60*60;
227
228 if ( ! plc_is_admin()) {
229   plc_warning("You need admin role to see this page.");
230
231  } else if (! $_GET['type']) {
232   echo "<h2>Select the events to focus on :</h2>";
233   // print the selection frame
234   echo $event_form;
235   
236  } else {
237
238   $tabs=array();
239   $tabs['Back to events form']=l_events();
240   plc_tabs($tabs);
241
242   // handle dates
243   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
244   // add one day to until_time - otherwise this corresponds to 0:0
245   $until_time += $STEP;
246   if ( ($from_time != 0) && ($until_time != $STEP) && ($from_time > $until_time) ) {
247     $messages[] = "Warning - <from> is after <until>";
248   }
249   
250   $filter=array();
251   // sort events by time is not good enough, let's use event_id
252   $filter['-SORT']='-event_id';
253   if ($from_time != 0) {
254     $filter[']time']=$from_time;
255   }
256   if ($until_time != $STEP) {
257     $filter['[time']=$until_time;
258   }
259
260   //////////////////////////////////////// Events
261   $type=$_GET['type'];
262   if ($type == 'Event') {
263
264    // and the filter applied for fetching events using GetEvent
265     $user_desc=$_GET['event'];
266     if ( ! empty($user_desc)) {
267       // should parse stuff like 45-90,230-3000 - some other day
268       $filter['event_id']=intval($user_desc);
269     }
270     // the filter might be void here - in python we need an empty dict but that's not what we get so
271     if (empty($filter)) {
272       $filter[']time']=0;
273     }
274     $events = $api->GetEvents($filter); 
275     $title="Events matching " . ($user_desc ? $user_desc : "everything");
276     if ($from_time != 0) 
277       $title .= " From " . $from_date;
278     if ($until_time != $STEP) 
279       $title .= " Until " . $until_date;
280
281     // see actual display of $title and $events below
282     
283   } else {
284
285     switch ($type) {
286     case 'Person': 
287       $primary_key='person_id';
288       $string_key='email';
289       $user_input=$_GET['person'];
290       $method="GetPersons";
291       $object_type='Person';
292       break;
293
294     case 'Node': 
295       $primary_key='node_id';
296       $string_key='hostname';
297       $user_input=$_GET['node'];
298       $method="GetNodes";
299       $object_type='Node';
300       break;
301       
302     case 'Site': 
303       $primary_key='site_id';
304       $string_key='login_base';
305       $user_input=$_GET['site'];
306       $method="GetSites";
307       $object_type='Site';
308       break;
309
310     case 'Slice': 
311       $primary_key='slice_id';
312       $string_key='name';
313       $user_input=$_GET['slice'];
314       $method="GetSlices";
315       $object_type='Slice';
316       break;
317     }
318
319     $object_ids=array();
320     $title=sprintf('Events for type %s:',$object_type);
321     foreach ( split(",",$user_input) as $user_desc) {
322       # numeric 
323       if (my_is_int($user_desc)) {
324         $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
325         if (empty ($obj_check)) {
326           $messages[] = "No such " . $primary_key . ": " . $user_desc;
327         } else {
328           $object_ids[] = $obj_check[0][$primary_key];
329           $title .= $user_desc . ", " ;
330         }
331       } else {
332         # string
333         $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
334         if (empty($new_object_ids)) {
335           $messages[] = "No " . $string_key . " matching " . $user_desc;
336         } else {
337           foreach ($new_object_ids as $new_obj_id) {
338             $object_ids[] = $new_obj_id[$primary_key];
339             $title .= $new_obj_id[$primary_key] . ", ";
340           }
341         }
342       }
343     }
344
345     $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
346     // get set of event_ids
347     $event_ids = array_map ( create_function ('$eo','return $eo["event_id"];') , $event_objs);
348     
349     $events = $api->GetEvents (array('event_id'=>$event_ids));
350
351     // see actual display of $title and $events below
352
353   }
354
355   drupal_set_title ($title);
356   // Show messages
357   if (!empty($messages)) 
358     foreach ($messages as $line) 
359       drupal_set_message($line);
360
361   $headers=array("Id"=>"int",
362                  "Time"=>"EnglishDateTime",
363                  "Method"=>"string",
364                  "Message"=>"string",
365                  "Subjects"=>"string",
366                  "Issuer"=>"string",
367                  "Auth"=>"string",
368                  "R"=>"string",
369                  "D"=>"none",
370                  );
371
372   $table_options=array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
373                                       "Click the button in the D(etails) column to get more details",
374                                       ),
375                        'max_pages'=>20);
376   plc_table_start("events",$headers,"0r",$table_options);
377   foreach ($events as $event) {
378
379     // the call button
380     $message = htmlentities($event['message'], ENT_QUOTES);
381     $call = htmlentities($event['call'], ENT_QUOTES);
382     $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
383     $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
384     //    $method = sprintf('<span title="%s">%s</span>',$call,$method);
385
386   // the message button
387     $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
388     $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
389     $details="<input type=button name='message' value='X' onclick='alert(\"" . $text . "\")'";
390     //    $message=sprintf('<span title="%s">%s</span>',$message,$message);
391
392     $message=truncate($event['message'],40);
393     plc_table_row_start($event['event_id']);
394     plc_table_cell(e_event($event['event_id']));
395     plc_table_cell(date('M/d/Y H:i', $event['time']));
396     plc_table_cell($event['call_name']);
397     plc_table_cell($message);
398     plc_table_cell(e_subjects($event));
399     plc_table_cell(e_issuer($event));
400     plc_table_cell(e_auth($event));
401     plc_table_cell(e_fault($event));
402     plc_table_cell($details);
403     plc_table_row_end();
404   }
405   plc_table_end("events");
406  }
407
408
409   // Print footer
410 include 'plc_footer.php';
411
412 ?>
413