slice creation page - sites sorted and correctly show current site - layout reviewed
[plewww.git] / planetlab / events / events.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 'table.php';
18 require_once 'linetabs.php';
19 require_once '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 // as per index.php, we get here if _GET['type'] is set
28 $type=$_GET['type'];
29 $from_date=$_GET['from_date'];
30 $until_date=$_GET['until_date'];
31
32 $messages=array();
33
34 //////////////////////////////////////////////////////////// dates
35 // our format is yyyy/MMM/dd
36
37 // given the user-typed string, return (*) a visible string, (*) an int time
38 // the flag should be false for the 'from' field, and true for the until field
39
40 $EPOCH="1 January 2008";
41 $DAY=24*60*60-1;
42
43 function parse_date ($user_date,$until_if_true=false,$DAY,$EPOCH) {
44
45   // empty string
46   if (empty ($user_date)) {
47     if ( ! $until_if_true) {
48     // an empty string means the epoch in from-mode
49       $date=$EPOCH; $time=strtotime($date);
50     } else {
51       $date="now"; $time=strtotime($date);
52     }
53   
54   } else {
55     // user-provided string
56     list ($year,$month,$day) = split ('[/.-]',$user_date);
57     $date=sprintf("%s %s %s",$day,$month,$year);
58     $time=strtotime($date);
59     //if the flag is set, we add 23h59'59'', so the 'until' date is inclusive
60     if ($until_if_true) {
61       $time += $DAY;
62     }
63     $date=strftime("%Y/%b/%d @ %H:%M",$time);
64   }
65   return array($date,$time);
66 }
67
68 //////////////////////////////////////////////////////////// layout
69 // outline node ids and person ids with a link
70 function e_node ($node_id) {
71   if (! $node_id) return "";
72   return l_node_t($node_id,$node_id);
73 }
74 function e_person ($person_id) {
75   if (! $person_id) return "";
76   return l_person_t($person_id,$person_id);
77 }
78 // xxx broken
79 function e_event ($event_id) {
80   if (! $event_id) return "";
81   return href(l_event("Event","event",$event_id),$event_id);
82 }
83
84 function e_subject ($type,$id) {
85   $mess=$type . " " . $id;
86   switch ($type) {
87   case 'Node': return l_node_t ($id,$mess);
88   case 'Site': return l_site_t ($id,$mess);
89   case 'Person': return l_person_t ($id,$mess);
90   case 'Slice': return l_slice_t ($id,$mess);
91   case 'Role': case 'Key': case 'PCU': case 'Interface': case 'NodeGroup': case "Address":
92     return "$mess";
93   default: return "Unknown $type" . "-" . $id;
94   }
95 }
96
97 // synthesize links to the subject objects from types and ids
98 function e_subjects ($param) {
99   $types=$param['object_types'];
100   $ids=$param['object_ids'];
101   if ( ! $types) return "";
102   return plc_vertical_table(array_map ("e_subject",$types,$ids));
103 }
104
105 function e_issuer ($param) {
106   if ($param['node_id'])        return e_subject('Node',$param['node_id']);
107   if ($param['person_id'])      return e_subject('Person',$param['person_id']);
108   return '???';
109 }
110
111 function e_auth ($event) {
112   if (array_key_exists('auth_type',$event)) 
113     return $event['auth_type'];
114     else
115       return "";
116 }
117
118 function e_fault ($event) {
119   $f=$event['fault_code'];
120   if ($f==0) return "OK";
121   else return $f;
122 }
123
124 ////////////////////////////////////////////////////////////
125 // for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
126
127 $tabs=array();
128 $tabs['Clear events']=l_events();
129 plekit_linetabs($tabs);
130
131 list($from_string,$from_time) = parse_date ($from_date,false,$DAY,$EPOCH);
132 list($until_string,$until_time) = parse_date ($until_date,true,$DAY,$EPOCH);
133
134 if ($from_time > $until_time) {
135   drupal_set_error("Warning - <from> is after <until>");
136   return;
137  }
138
139 $filter=array();
140 // sort events by time is not good enough, let's use event_id
141 $filter['-SORT']='-event_id';
142 $filter[']time']=$from_time;
143 $filter['[time']=$until_time;
144
145 //////////////////////////////////////// Events
146 if ($type == 'Event') {
147   
148   // and the filter applied for fetching events using GetEvent
149   $user_desc=$_GET['event'];
150   if ( ! empty($user_desc)) {
151     // should parse stuff like 45-90,230-3000 - some other day
152     $filter['event_id']=intval($user_desc);
153   }
154   
155   $events = $api->GetEvents($filter); 
156   $title="Events [ $from_string - $until_string] matching " . ($user_desc ? $user_desc : "everything");
157   
158   // see actual display of $title and $events below
159   
160  } else {
161   
162   switch ($type) {
163   case 'Person': 
164     $primary_key='person_id';
165     $string_key='email';
166     $user_input=$_GET['person'];
167     $method="GetPersons";
168     $object_type='Person';
169     break;
170
171   case 'Node': 
172     $primary_key='node_id';
173     $string_key='hostname';
174     $user_input=$_GET['node'];
175     $method="GetNodes";
176     $object_type='Node';
177     break;
178       
179   case 'Site': 
180     $primary_key='site_id';
181     $string_key='login_base';
182     $user_input=$_GET['site'];
183     $method="GetSites";
184     $object_type='Site';
185     break;
186
187   case 'Slice': 
188     $primary_key='slice_id';
189     $string_key='name';
190     $user_input=$_GET['slice'];
191     $method="GetSlices";
192     $object_type='Slice';
193     break;
194   }
195
196   $object_ids=array();
197   $title="Events [ $from_string - $until_string]";
198   $title .= " type=$object_type";
199   $title .= " id(s)=";
200   foreach ( split(",",$user_input) as $user_desc) {
201 # numeric 
202     if (my_is_int($user_desc)) {
203       $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
204       if (empty ($obj_check)) {
205         $messages[] = "No such " . $primary_key . ": " . $user_desc;
206       } else {
207         $object_ids[] = $obj_check[0][$primary_key];
208         $title .= " $user_desc, " ;
209       }
210     } else {
211 # string
212       $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
213       if (empty($new_object_ids)) {
214         $messages[] = "No " . $string_key . " matching " . $user_desc;
215       } else {
216         foreach ($new_object_ids as $new_obj_id) {
217           $object_ids[] = $new_obj_id[$primary_key];
218           $title .= $new_obj_id[$primary_key] . ", ";
219         }
220       }
221     }
222   }
223
224   $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
225   // get set of event_ids
226   $event_ids = array_map ( create_function ('$eo','return $eo["event_id"];') , $event_objs);
227     
228   $events = $api->GetEvents (array('event_id'=>$event_ids));
229
230   // see actual display of $title and $events below
231
232  }
233
234   drupal_set_title ($title);
235 // Show messages
236 if (!empty($messages)) 
237   foreach ($messages as $line) 
238     drupal_set_message($line);
239
240 $headers=array("Id"=>"int",
241                "Time"=>"EnglishDateTime",
242                "Method"=>"string",
243                "Message"=>"string",
244                "Subjects"=>"string",
245                "Issuer"=>"string",
246                "Auth"=>"string",
247                "R"=>"string",
248                "D"=>"none",
249                );
250
251 $table = new PlekitTable ("events",$headers,"0r");
252 $table->set_options (array ('max_pages'=>20));
253 $table->start ();
254 foreach ($events as $event) {
255   
256   // the call button
257   $message = htmlentities($event['message'], ENT_QUOTES);
258   $call = htmlentities($event['call'], ENT_QUOTES);
259   $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
260   $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
261   //    $method = sprintf('<span title="%s">%s</span>',$call,$method);
262   
263   // the message button
264   $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
265   $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
266   $details="<input type=button name='message' value='+' onclick='alert(\"" . $text . "\")'";
267   //    $message=sprintf('<span title="%s">%s</span>',$message,$message);
268   
269   $message=truncate($event['message'],40);
270   $table->row_start();
271   $table->cell(e_event($event['event_id']));
272   $table->cell(date('M/d/Y H:i', $event['time']));
273   $table->cell($event['call_name']);
274   $table->cell($message);
275   $table->cell(e_subjects($event));
276   $table->cell(e_issuer($event));
277   $table->cell(e_auth($event));
278   $table->cell(e_fault($event));
279   $table->cell($details);
280   $table->row_end();
281 }
282 $table->set_options(array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
283                                          "Click the button in the D(etails) column to get more details")));
284 $table->end();
285   
286 //plekit_linetabs ($tabs,"bottom");
287
288 // Print footer
289 include 'plc_footer.php';
290
291 ?>
292