6899f66e39ac9cfe3b94dc32b05b17ceda7eddfb
[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_sorts.php';
18   
19 // needs much memory
20 ini_set("memory_limit","128M");
21
22 //set default title
23 drupal_set_title('Events');
24
25 // paginate unit
26 $page_size=30;
27
28 $messages = array ();
29
30 ////////////////////////////////////////
31 // defaults for day ('j'), 3-letter month ('M') or year ('Y')
32 function the_date ($key,$dateformat) { 
33   if ($_GET[$key]) return $_GET[$key];
34   else return date($dateformat);
35 }
36
37 // fill out dates from now if not specified
38 $from_d = the_date('from_d','j');
39 $from_m = the_date('from_m','M');
40 $from_y = the_date('from_y','Y');
41 $until_d = the_date('until_d','j');
42 $until_m = the_date('until_m','M');
43 $until_y = the_date('until_y','Y');
44
45 // create the options area from a list and the selected entry
46 function dropdown_options ($array,$selected) {
47   $result="";
48   foreach ($array as $item) {
49     $result.= "<option value=" . $item;
50     if ($item == $selected) $result .= ' selected=selected';
51     $result .= '>' . $item . '</option>';
52   }
53   return $result;
54 }
55
56 $days=range(1,31);
57 $from_d_dropdown_options=dropdown_options($days,$from_d);
58 $until_d_dropdown_options=dropdown_options($days,$until_d);
59 $months=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
60 $from_m_dropdown_options=dropdown_options($months,$from_m);
61 $until_m_dropdown_options=dropdown_options($months,$until_m);
62 // only propose years ranging from now + 3 full years back
63 $this_year=date('Y');
64 $years=range($this_year-3,$this_year);
65 $from_y_dropdown_options=dropdown_options($years,$from_y);
66 $until_y_dropdown_options=dropdown_options($years,$until_y);
67  
68 $event_form = <<< EOF
69 <form method=get name='F' action='/db/events/index.php' >
70
71 <table align='bottom'>
72 <tr><td colspan=2>
73 <table> <TR><TD>
74 <input type='radio' name='type' id='events' value='Event' checked='checked'>&nbsp;Events: 
75 </TD><TD>
76 <input type='text' onSelect="submit();" onFocus='events.checked=true' name='event' size=20>
77 </TD></TR><TR><TD>
78 <input type='radio' name='type' id='persons' value='Person'>&nbsp;Persons:
79 </TD><TD>
80 <input type='text' onSelect="submit();" onFocus='persons.checked=true' name='person' size=20>
81 </TD></TR><TR><TD>  
82 <input type='radio' name='type' id='nodes' value='Node'>&nbsp;Nodes:
83 </TD><TD>
84 <input type='text' onSelect="submit();" onFocus='nodes.checked=true' name='node' size=20>
85 </TD></TR><TR><TD>
86 <input type='radio' name='type' id='sites' value='Site'>&nbsp;Sites:
87 </TD><TD>
88 <input type='text' onSelect="submit();" onFocus='sites.checked=true' name='site' size=20>
89 </TD></TR><TR><TD>
90 <input type='radio' name='type' id='slices' value='Slice'>&nbsp;Slices:
91 </TD><TD>
92 <input type='text' onSelect="submit();" onFocus='slices.checked=true' name='slice' size=20>
93 </TD></TR></table>
94 </td></tr>
95
96
97 <tr><th>FROM (inclusive)</th> <th>UNTIL (inclusive)</th> </tr>
98
99 <tr>
100       <td>    
101         <SELECT NAME='from_d'>
102 $from_d_dropdown_options                                                                 
103         </SELECT>
104         <SELECT NAME='from_m' >
105 $from_m_dropdown_options
106         </SELECT>
107         <SELECT NAME='from_y' >
108 $from_y_dropdown_options
109         </SELECT>
110 </td>
111
112 <TD>
113    <SELECT NAME=' until_d' >
114 $until_d_dropdown_options
115     </SELECT>
116     <SELECT NAME=' until_m' >
117 $until_m_dropdown_options
118    </SELECT>
119     <SELECT NAME=' until_y' >
120 $until_y_dropdown_options
121     </SELECT>
122 </td></tr>
123
124 <TR><TD colspan=2>
125 <input type='submit' align='middle' value='Show Events'>
126 </TD></TR>
127 </table>
128 </form>
129
130 EOF;
131
132 function parse_date ($day,$month,$year) {
133   // if everything empty -> unspecified date, return 0
134   if ( empty($day) && empty($month) && empty($year)) {
135     return array ("xxx",0);
136   } else {
137     // fill missing fields with current value
138     if (empty($day)) $day=date('d');
139     if (empty($month)) $month=date('M');
140     if (empty($year)) $year=date('Y');
141     $date=sprintf("%s %s %s",$day,$month,$year);
142     $time=strtotime($date);
143     return array($date,$time);
144   }
145 }
146
147 function parse_dates () {
148   list($from_date,$from_time) = parse_date($_GET['from_d'],$_GET['from_m'],$_GET['from_y']);
149   list($until_date,$until_time) = parse_date($_GET['until_d'],$_GET['until_m'],$_GET['until_y']);
150   return array($from_date,$from_time,$until_date,$until_time);
151 }
152
153 function my_is_int ($x) {
154     return (is_numeric($x) ? intval($x) == $x : false);
155 }
156
157 function truncate ($text,$numb,$etc = "...") {
158   if (strlen($text) > $numb) {
159     $text = substr($text, 0, $numb);
160     $text = $text.$etc;
161   }
162   return $text;
163 }
164
165 // layout function to refine a row's content
166 function layout ($param){
167  
168   // format time
169   $time=$param['time'];
170   $date= date('d M Y H:i' ,$time);
171   $param['time']=$date;
172
173   // the call button
174   $message=htmlentities($param['message'], ENT_QUOTES);
175   $call=htmlentities($param['call'], ENT_QUOTES);
176   $detail_text=sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$param['runtime']);
177   $detail="<input type=button name='call' value='" . $param['call_name'] ."' onclick='alert(\"" . $detail_text . "\")'";
178   $detail=sprintf('<span title="%s">%s</span>',$call,$detail);
179   $param['call_name']=$detail;
180   unset ($param['call']);
181
182   // the message button
183   $trunc_mess=htmlentities(truncate($param['message'],40),ENT_QUOTES);
184   $detail="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $detail_text . "\")'";
185   $detail=sprintf('<span title="%s">%s</span>',$message,$detail);
186   $param['message']=$detail;
187
188   // shrink column name : event_id -> id - paginate_id used in paginate and does not show up
189   $param['<span title="event_id">id</span>']=$param['event_id'] ; 
190   // so that event_id shows up
191   $param['paginate_id']=$param['event_id']; unset($param['event_id']);
192
193   //// shrink column names 
194   $param['<span title="fault_code">fault</span>']=$param['fault_code'] ; unset($param['fault_code']);
195   // seem empty on all rows - probably something that I screwed when importing tony's stuff
196   //  $param['<span title="object_type">oty</span>']=$param['object_type'] ; unset($param['object_type']);
197   //  $param['<span title="object_id">oid</span>']=$param['object_id'] ; unset($param['object_id']);
198   $param['<span title="object_types">otys</span>']=$param['object_types'] ; unset($param['object_types']);
199   $param['<span title="object_ids">oids</span>']=$param['object_ids'] ; unset($param['object_ids']);
200   $param['<span title="node_id">nid</span>']=plc_node_link($param['node_id']) ; unset($param['node_id']);
201   $param['<span title="person_id">pid</span>']= plc_person_link($param['person_id']) ; unset($param['person_id']);
202   if (array_key_exists('auth_type',$param)) {
203     $param['<span title="auth_type">at</span>']=$param['auth_type'] ; unset($param['auth_type']);
204   }
205
206   // clears
207   unset($param['object_type']);
208   unset($param['object_id']);
209   unset($param['runtime']);
210   return $param;
211 }
212
213 //plc_debug('GET',$_GET);
214
215 if ( ! plc_is_admin()) {
216   echo "<div class='plc-warning'> You need admin role to see this page. </div>";
217
218  } else if (! $_GET['type']) {
219   echo "<h2>Select the events to focus on :</h2>";
220   // print the selection frame
221   echo $event_form;
222   
223  } else {
224
225   // handle dates
226   list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
227   // add one day to until_time - otherwise this corresponds to 0:0
228   $until_time += (24*60*60);
229   if ( ($from_time != 0) && ($until_time != 0) && ($from_time > $until_time) ) {
230     $messages[] = "Warning - wrong date selection";
231   }
232   
233   $filter=array();
234   // sort events by time is not good enough, let's use event_id
235   $filter['-SORT']='event_id';
236   if ($from_time != 0) {
237     $filter[']time']=$from_time;
238   }
239   if ($until_time != 0) {
240     $filter['[time']=$until_time;
241   }
242
243   //////////////////////////////////////// Events
244   $type=$_GET['type'];
245   if ($type == 'Event') {
246
247    // and the filter applied for fetching events using GetEvent
248     $user_desc=$_GET['event'];
249     if ( ! empty($user_desc)) {
250       // should parse stuff like 45-90,230-3000 - some other day
251       $filter['event_id']=intval($user_desc);
252     }
253     // the filter might be void here - in python we need an empty dict but that's not what we get so
254     if (empty($filter)) {
255       $filter[']time']=0;
256     }
257     $events = $api->GetEvents($filter); 
258     if (empty($events)) {
259       $messages[] = "No event found - user input was [" . $user_desc . "]";
260     } else {
261       $title="Events matching " . ($user_desc ? $user_desc : "everything");
262       if ($from_time != 0) 
263         $title .= " From " . $from_date;
264       if ($until_time != 0) 
265         $title .= " Until " . $until_date;
266       drupal_set_title ($title);
267     }
268
269     // Show messages
270     if (!empty($messages)) 
271       foreach ($messages as $line) 
272         drupal_set_message($line);
273         
274     if ( ! empty ($events)) {
275       $events= array_map(layout,$events);
276       echo paginate( $events, "paginate_id", "Events", $page_size, "event_id");
277     }
278   } else {
279
280     switch ($type) {
281     case 'Person': 
282       $primary_key='person_id';
283       $string_key='email';
284       $user_input=$_GET['person'];
285       $method="GetPersons";
286       $object_type='Person';
287       break;
288
289     case 'Node': 
290       $primary_key='node_id';
291       $string_key='hostname';
292       $user_input=$_GET['node'];
293       $method="GetNodes";
294       $object_type='Node';
295       break;
296       
297     case 'Site': 
298       $primary_key='site_id';
299       $string_key='login_base';
300       $user_input=$_GET['site'];
301       $method="GetSites";
302       $object_type='Site';
303       break;
304
305     case 'Slice': 
306       $primary_key='slice_id';
307       $string_key='name';
308       $user_input=$_GET['slice'];
309       $method="GetSlices";
310       $object_type='Slice';
311       break;
312     }
313
314     $object_ids=array();
315     $title=sprintf('Events for type %s:',$object_type);
316     foreach ( split(",",$user_input) as $user_desc) {
317       # numeric 
318       if (my_is_int($user_desc)) {
319         $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
320         if (empty ($obj_check)) {
321           $messages[] = "No such " . $primary_key . ": " . $user_desc;
322         } else {
323           $object_ids[] = $obj_check[0][$primary_key];
324           $title .= $user_desc . ", " ;
325         }
326       } else {
327         # string
328         $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
329         if (empty($new_object_ids)) {
330           $messages[] = "No " . $string_key . " matching " . $user_desc;
331         } else {
332           foreach ($new_object_ids as $new_obj_id) {
333             $object_ids[] = $new_obj_id[$primary_key];
334             $title .= $new_obj_id[$primary_key] . ", ";
335           }
336         }
337       }
338     }
339       
340     // Show messages
341     if (!empty($messages)) {
342       print '<div class="messages plc-warning"><ul>';
343       foreach ($messages as $line) {
344         print "<li> $line";
345       }
346       print "</ul></div>";
347     }
348         
349     drupal_set_title($title);
350     $events = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type));
351
352     $events=array_map(layout,$events);
353     echo paginate( $events, "paginate_id", "--------" . $type . " EVENTS---------", $page_size, "hostname");
354   }
355  }
356
357 echo "<br /><p><a href='/db/events/index.php'>Back to Events</a>";
358
359   // Print footer
360 include 'plc_footer.php';
361
362 ?>
363