svn:keywords
[plewww.git] / planetlab / events / index.php
1 <?php
2 // $Id$
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   // handle dates
293   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
294   if ( ($from_time != 0) && ($until_time != 0) && ($from_time > $until_time) ) {
295     $messages[] = "Warning - wrong date selection";
296   }
297   
298   $filter=array();
299   if ($from_time != 0) {
300     $filter[']time']=$from_time;
301   }
302   if ($until_time != 0) {
303     $filter['[time']=$until_time;
304   }
305
306   //////////////////////////////////////// Events
307   $type=$_GET['type'];
308   if ($type == 'Event') {
309
310    // and the filter applied for fetching events using GetEvent
311     $user_desc=$_GET['event'];
312     if ( ! empty($user_desc)) {
313       // should parse stuff like 45-90,230-3000 - some other day
314       $filter['event_id']=intval($user_desc);
315     }
316     // the filter might be void here - in python we need an empty dict but that's not what we get so
317     if (empty($filter)) {
318       $filter[']time']=0;
319     }
320     $events = $api->GetEvents($filter); 
321     if (empty($events)) {
322       $messages[] = "No event found - user input was [" . $user_desc . "]";
323     } else {
324       $title="Events matching " . ($user_desc ? $user_desc : "everything");
325       if ($from_time != 0) 
326         $title .= " From " . $from_date;
327       if ($until_time != 0) 
328         $title .= " Until " . $until_date;
329       drupal_set_title ($title);
330     }
331
332     // Show messages
333     if (!empty($messages)) {
334       print '<div class="messages plc-warning"><ul>';
335       foreach ($messages as $line) {
336         print "<li> $line";
337       }
338       print "</ul></div>";
339     }
340         
341     if ( ! empty ($events)) {
342       $events= array_map(layout,$events);
343       echo paginate( $events, "paginate_id", "Events", $page_size, "event_id");
344     }
345   } else {
346
347     switch ($type) {
348     case 'Person': 
349       $primary_key='person_id';
350       $string_key='email';
351       $user_input=$_GET['person'];
352       $method="GetPersons";
353       $object_type='Person';
354       break;
355
356     case 'Node': 
357       $primary_key='node_id';
358       $string_key='hostname';
359       $user_input=$_GET['node'];
360       $method="GetNodes";
361       $object_type='Node';
362       break;
363       
364     case 'Site': 
365       $primary_key='site_id';
366       $string_key='login_base';
367       $user_input=$_GET['site'];
368       $method="GetSites";
369       $object_type='Site';
370       break;
371
372     case 'Slice': 
373       $primary_key='slice_id';
374       $string_key='name';
375       $user_input=$_GET['slice'];
376       $method="GetSlices";
377       $object_type='Slice';
378       break;
379     }
380
381     $object_ids=array();
382     $title=sprintf('Events for type %s:',$object_type);
383     foreach ( split(",",$user_input) as $user_desc) {
384       # numeric 
385       if (my_is_int($user_desc)) {
386         $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
387         if (empty ($obj_check)) {
388           $messages[] = "No such " . $primary_key . ": " . $user_desc;
389         } else {
390           $object_ids[] = $obj_check[0][$primary_key];
391           $title .= $user_desc . ", " ;
392         }
393       } else {
394         # string
395         $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
396         if (empty($new_object_ids)) {
397           $messages[] = "No " . $string_key . " matching " . $user_desc;
398         } else {
399           foreach ($new_object_ids as $new_obj_id) {
400             $object_ids[] = $new_obj_id[$primary_key];
401             $title .= $new_obj_id[$primary_key] . ", ";
402           }
403         }
404       }
405     }
406       
407     // Show messages
408     if (!empty($messages)) {
409       print '<div class="messages plc-warning"><ul>';
410       foreach ($messages as $line) {
411         print "<li> $line";
412       }
413       print "</ul></div>";
414     }
415         
416     drupal_set_title($title);
417     $events = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type));
418
419     $events=array_map(layout,$events);
420     echo paginate( $events, "paginate_id", "--------" . $type . " EVENTS---------", $page_size, "hostname");
421   }
422  }
423
424 echo "<br /><p><a href='/db/events/index.php'>Back to Events</a>";
425
426   // Print footer
427 include 'plc_footer.php';
428
429 ?>
430