398530f7218d1af2b0362a8b7bd46ed2f6d85e90
[plewww.git] / planetlab / events / index.php
1 <?php
2 // $Id: index.php 1166 2008-01-29 09:32:26Z amine $
3   //
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 //// Print header
13 require_once 'plc_drupal.php';
14 //set default title
15 drupal_set_title('Events');
16
17 include 'plc_header.php';
18
19 // Common functions
20 require_once 'plc_functions.php';
21 require_once 'plc_sorts.php';
22   
23 // find person roles
24 $_person= $plc->person;
25 $_roles= $_person['role_ids'];
26
27 // paginate unit
28 $page_size=30;
29
30 $messages = array ();
31
32 $event_form = <<< EOF
33 <form method=get name='F' action='/db/events/index.php' >
34
35 <table align='bottom'>
36 <tr><td colspan=2>
37 <table> <TR><TD>
38 <input type='radio' name='type' id='events' value='Event' checked='checked'>&nbsp;Events: 
39 </TD><TD>
40 <input type='text' onSelect="submit();" onFocus='events.checked=true' name='event' size=20>
41 </TD></TR><TR><TD>
42 <input type='radio' name='type' id='persons' value='Person'>&nbsp;Persons:
43 </TD><TD>
44 <input type='text' onSelect="submit();" onFocus='persons.checked=true' name='person' size=20>
45 </TD></TR><TR><TD>  
46 <input type='radio' name='type' id='nodes' value='Node'>&nbsp;Nodes:
47 </TD><TD>
48 <input type='text' onSelect="submit();" onFocus='nodes.checked=true' name='node' size=20>
49 </TD></TR><TR><TD>
50 <input type='radio' name='type' id='sites' value='Site'>&nbsp;Sites:
51 </TD><TD>
52 <input type='text' onSelect="submit();" onFocus='sites.checked=true' name='site' size=20>
53 </TD></TR><TR><TD>
54 <input type='radio' name='type' id='slices' value='Slice'>&nbsp;Slices:
55 </TD><TD>
56 <input type='text' onSelect="submit();" onFocus='slices.checked=true' name='slice' size=20>
57 </TD></TR></table>
58 </td></tr>
59
60
61 <tr><th>FROM</th> <th>UNTIL</th> </tr>
62
63 <tr>
64       <td>    
65         <SELECT NAME='from_d' >
66         <OPTION>
67         <OPTION VALUE=' 1' >1
68         <OPTION VALUE=' 2' >2
69         <OPTION VALUE=' 3' >3
70         <OPTION VALUE=' 4' >4
71         <OPTION VALUE=' 5' >5
72         <OPTION VALUE=' 6' >6
73         <OPTION VALUE=' 7' >7
74         <OPTION VALUE=' 8' >8
75         <OPTION VALUE=' 9' >9
76         <OPTION VALUE=' 10' >10
77         <OPTION VALUE=' 11' >11
78         <OPTION VALUE=' 12' >12
79         <OPTION VALUE=' 13' >13
80         <OPTION VALUE=' 14' >14
81         <OPTION VALUE=' 15' >15
82         <OPTION VALUE=' 16' >16
83         <OPTION VALUE=' 17' >17
84         <OPTION VALUE=' 18' >18
85         <OPTION VALUE=' 19' >19
86         <OPTION VALUE=' 20' >20
87         <OPTION VALUE=' 21' >21
88         <OPTION VALUE=' 22' >22
89         <OPTION VALUE=' 23' >23
90         <OPTION VALUE=' 24' >24
91         <OPTION VALUE=' 25' >25
92         <OPTION VALUE=' 26' >26
93         <OPTION VALUE=' 27' >27
94         <OPTION VALUE=' 28' >28
95         <OPTION VALUE=' 29' >29
96         <OPTION VALUE=' 30' >30
97         <OPTION VALUE=' 31' >31
98         </SELECT>
99
100         <SELECT NAME='from_m' >
101         <OPTION>
102         <OPTION VALUE=' Jan' >January
103         <OPTION VALUE=' Feb' >February
104         <OPTION VALUE=' Mar' >March
105         <OPTION VALUE=' Apr' >April
106         <OPTION VALUE=' May' >May
107         <OPTION VALUE=' Jun' >June
108         <OPTION VALUE=' Jul' >July
109         <OPTION VALUE=' Aug' >August
110         <OPTION VALUE=' Sep' >September
111         <OPTION VALUE=' Oct' >October
112         <OPTION VALUE=' Nov' >November
113         <OPTION VALUE=' Dec' >December
114         </SELECT>
115
116         <SELECT NAME='from_y' >
117         <OPTION>
118         <OPTION VALUE='2006' >2006
119         <OPTION VALUE='2007' >2007
120         <OPTION VALUE='2008' >2008
121         <OPTION VALUE='2009' >2009
122         <OPTION VALUE='2010' >2010
123         <OPTION VALUE='2011' >2011
124         </SELECT>
125
126 </td>
127
128 <TD>
129    <SELECT NAME=' until_d' >
130         <OPTION>
131         <OPTION VALUE='1' >1
132         <OPTION VALUE='2' >2
133         <OPTION VALUE='3' >3
134         <OPTION VALUE='4' >4
135         <OPTION VALUE='5' >5
136         <OPTION VALUE='6' >6
137         <OPTION VALUE='7' >7
138         <OPTION VALUE='8' >8
139         <OPTION VALUE='9' >9
140         <OPTION VALUE='10' >10
141         <OPTION VALUE='11' >11
142         <OPTION VALUE='12' >12
143         <OPTION VALUE='13' >13
144         <OPTION VALUE='14' >14
145         <OPTION VALUE='15' >15
146         <OPTION VALUE='16' >16
147         <OPTION VALUE='17' >17
148         <OPTION VALUE='18' >18
149         <OPTION VALUE='19' >19
150         <OPTION VALUE='20' >20
151         <OPTION VALUE='21' >21
152         <OPTION VALUE='22' >22
153         <OPTION VALUE='23' >23
154         <OPTION VALUE='24' >24
155         <OPTION VALUE='25' >25
156         <OPTION VALUE='26' >26
157         <OPTION VALUE='27' >27
158         <OPTION VALUE='28' >28
159         <OPTION VALUE='29' >29
160         <OPTION VALUE='30' >30
161         <OPTION VALUE='31' >31
162     </SELECT>
163     <SELECT NAME=' until_m' >
164         <OPTION>
165         <OPTION VALUE='Jan' >January
166         <OPTION VALUE='Feb' >February
167         <OPTION VALUE='Mar' >March
168         <OPTION VALUE='Apr' >April
169         <OPTION VALUE='May' >May
170         <OPTION VALUE='Jun' >June
171         <OPTION VALUE='Jul' >July
172         <OPTION VALUE='Aug' >August
173         <OPTION VALUE='Sep' >September
174         <OPTION VALUE='Oct' >October
175         <OPTION VALUE='Nov' >November
176         <OPTION VALUE='Dec' >December
177    </SELECT>
178  
179     <SELECT NAME=' until_y' >
180         <OPTION>
181         <OPTION VALUE='2006' >2006
182         <OPTION VALUE='2007' >2007
183         <OPTION VALUE='2008' >2008
184         <OPTION VALUE='2009' >2009
185         <OPTION VALUE='2010' >2010
186         <OPTION VALUE='2011' >2011
187     </SELECT>
188 </td></tr>
189
190 <TR><TD colspan=2>
191 <input type='submit' align='middle' value='Show Events'>
192 </TD></TR>
193 </table>
194 </form>
195
196 EOF;
197
198 function parse_date ($day,$month,$year) {
199   // if everything empty -> unspecified date, return 0
200   if ( empty($day) && empty($month) && empty($year)) {
201     return array ("xxx",0);
202   } else {
203     // fill missing fields with current value
204     if (empty($day)) $day=date('d');
205     if (empty($month)) $month=date('M');
206     if (empty($year)) $year=date('Y');
207     $date=sprintf("%s %s %s",$day,$month,$year);
208     $time=strtotime($date);
209     return array($date,$time);
210   }
211 }
212
213 function parse_dates () {
214   list($from_date,$from_time) = parse_date($_GET['from_d'],$_GET['from_m'],$_GET['from_y']);
215   list($until_date,$until_time) = parse_date($_GET['until_d'],$_GET['until_m'],$_GET['until_y']);
216   return array($from_date,$from_time,$until_date,$until_time);
217 }
218
219 function my_is_int ($x) {
220     return (is_numeric($x) ? intval($x) == $x : false);
221 }
222
223 function truncate ($text,$numb,$etc = "...") {
224   if (strlen($text) > $numb) {
225     $text = substr($text, 0, $numb);
226     $text = $text.$etc;
227   }
228   return $text;
229 }
230
231 // layout function to refine a row's content
232 function layout ($param){
233  
234   // format time
235   $time=$param['time'];
236   $date= date('d M Y H:i' ,$time);
237   $param['time']=$date;
238
239   // the call button
240   $message=htmlentities($param['message'], ENT_QUOTES);
241   $call=htmlentities($param['call'], ENT_QUOTES);
242   $detail_text=sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$param['runtime']);
243   $detail="<input type=button name='call' value='" . $param['call_name'] ."' onclick='alert(\"" . $detail_text . "\")'";
244   $detail=sprintf('<span title="%s">%s</span>',$call,$detail);
245   $param['call_name']=$detail;
246   unset ($param['call']);
247
248   // the message button
249   $trunc_mess=htmlentities(truncate($param['message'],40),ENT_QUOTES);
250   $detail="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $detail_text . "\")'";
251   $detail=sprintf('<span title="%s">%s</span>',$message,$detail);
252   $param['message']=$detail;
253
254   // shrink column name : event_id -> id - paginate_id used in paginate and does not show up
255   $param['<span title="event_id">id</span>']=$param['event_id'] ; 
256   // so that event_id shows up
257   $param['paginate_id']=$param['event_id']; unset($param['event_id']);
258
259   //// shrink column names 
260   $param['<span title="fault_code">fault</span>']=$param['fault_code'] ; unset($param['fault_code']);
261   // seem empty on all rows - probably something that I screwed when importing tony's stuff
262   //  $param['<span title="object_type">oty</span>']=$param['object_type'] ; unset($param['object_type']);
263   //  $param['<span title="object_id">oid</span>']=$param['object_id'] ; unset($param['object_id']);
264   $param['<span title="object_types">otys</span>']=$param['object_types'] ; unset($param['object_types']);
265   $param['<span title="object_ids">oids</span>']=$param['object_ids'] ; unset($param['object_ids']);
266   $param['<span title="node_id">nid</span>']=plc_node_link($param['node_id']) ; unset($param['node_id']);
267   $param['<span title="person_id">pid</span>']= plc_person_link($param['person_id']) ; unset($param['person_id']);
268   if (array_key_exists('auth_type',$param)) {
269     $param['<span title="auth_type">at</span>']=$param['auth_type'] ; unset($param['auth_type']);
270   }
271
272   // clears
273   unset($param['object_type']);
274   unset($param['object_id']);
275   unset($param['runtime']);
276   return $param;
277 }
278
279 //plc_debug('GET',$_GET);
280
281 if ( !in_array ('10', $_roles)) {
282   echo "<div class='plc-warning'> You need admin role to see this page. </div>";
283
284  } else if (!$_GET['type']) {
285
286   echo "<h2>What events would you like to consult :</h2>";
287   // print the selection frame
288   echo $event_form;
289   
290  } else {
291
292   //  // xxx this of any use at all ?
293   //  drupal_set_html_head('<script type="text/javascript" src="/planetlab/includes/js/bsn.Ajax.js"></script>
294   //    <script type="text/javascript" src="/planetlab/includes/js/bsn.DOM.js"></script>
295   //    <script type="text/javascript" src="/planetlab/includes/js/bsn.AutoSuggest.js"></script>');
296
297   // handle dates
298   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
299   if ( ($from_time != 0) && ($until_time != 0) && ($from_time > $until_time) ) {
300     $messages[] = "Warning - wrong date selection";
301   }
302   
303   $filter=array();
304   if ($from_time != 0) {
305     $filter[']time']=$from_time;
306   }
307   if ($until_time != 0) {
308     $filter['[time']=$until_time;
309   }
310
311   //////////////////////////////////////// Events
312   $type=$_GET['type'];
313   if ($type == 'Event') {
314
315    // and the filter applied for fetching events using GetEvent
316     $user_desc=$_GET['event'];
317     if ( ! empty($user_desc)) {
318       // should parse stuff like 45-90,230-3000 - some other day
319       $filter['event_id']=intval($user_desc);
320     }
321     // the filter might be void here - in python we need an empty dict but that's not what we get so
322     if (empty($filter)) {
323       $filter[']time']=0;
324     }
325     $events = $api->GetEvents($filter); 
326     if (empty($events)) {
327       $messages[] = "No event found - user input was [" . $user_desc . "]";
328     } else {
329       $title="Events matching " . ($user_desc ? $user_desc : "everything");
330       if ($from_time != 0) 
331         $title .= " From " . $from_date;
332       if ($until_time != 0) 
333         $title .= " Until " . $until_date;
334       drupal_set_title ($title);
335     }
336
337     // Show messages
338     if (!empty($messages)) {
339       print '<div class="messages plc-warning"><ul>';
340       foreach ($messages as $line) {
341         print "<li> $line";
342       }
343       print "</ul></div>";
344     }
345         
346     if ( ! empty ($events)) {
347       $events= array_map(layout,$events);
348       echo paginate( $events, "paginate_id", "Events", $page_size, "event_id");
349     }
350   } else {
351
352     switch ($type) {
353     case 'Person': 
354       $primary_key='person_id';
355       $string_key='email';
356       $user_input=$_GET['person'];
357       $method="GetPersons";
358       $object_type='Person';
359       break;
360
361     case 'Node': 
362       $primary_key='node_id';
363       $string_key='hostname';
364       $user_input=$_GET['node'];
365       $method="GetNodes";
366       $object_type='Node';
367       break;
368       
369     case 'Site': 
370       $primary_key='site_id';
371       $string_key='login_base';
372       $user_input=$_GET['site'];
373       $method="GetSites";
374       $object_type='Site';
375       break;
376
377     case 'Slice': 
378       $primary_key='slice_id';
379       $string_key='name';
380       $user_input=$_GET['slice'];
381       $method="GetSlices";
382       $object_type='Slice';
383       break;
384     }
385
386     $object_ids=array();
387     $title=sprintf('Events for type %s:',$object_type);
388     foreach ( split(",",$user_input) as $user_desc) {
389       # numeric 
390       if (my_is_int($user_desc)) {
391         $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
392         if (empty ($obj_check)) {
393           $messages[] = "No such " . $primary_key . ": " . $user_desc;
394         } else {
395           $object_ids[] = $obj_check[0][$primary_key];
396           $title .= $user_desc . ", " ;
397         }
398       } else {
399         # string
400         $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
401         if (empty($new_object_ids)) {
402           $messages[] = "No " . $string_key . " matching " . $user_desc;
403         } else {
404           foreach ($new_object_ids as $new_obj_id) {
405             $object_ids[] = $new_obj_id[$primary_key];
406             $title .= $new_obj_id[$primary_key] . ", ";
407           }
408         }
409       }
410     }
411       
412     // Show messages
413     if (!empty($messages)) {
414       print '<div class="messages plc-warning"><ul>';
415       foreach ($messages as $line) {
416         print "<li> $line";
417       }
418       print "</ul></div>";
419     }
420         
421     drupal_set_title($title);
422     $events = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type));
423
424     $events=array_map(layout,$events);
425     echo paginate( $events, "paginate_id", "--------" . $type . " EVENTS---------", $page_size, "hostname");
426   }
427  }
428
429 echo "<br /><p><a href='/db/events/index.php'>Back to Events</a>";
430
431   // Print footer
432 include 'plc_footer.php';
433
434 ?>
435