5 require_once 'plc_login.php';
7 // Get session and API handles
8 require_once 'plc_session.php';
12 require_once 'plc_drupal.php';
13 include 'plc_header.php';
16 require_once 'plc_functions.php';
17 require_once 'plc_tables.php';
18 require_once 'plc_minitabs.php';
21 ini_set("memory_limit","256M");
24 drupal_set_title('Events');
31 //////////////////////////////////////////////////////////// form
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);
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');
47 // create the options area from a list and the selected entry
48 function dropdown_options ($array,$selected) {
50 foreach ($array as $item) {
51 $result.= "<option value=" . $item;
52 if ($item == $selected) $result .= ' selected=selected';
53 $result .= '>' . $item . '</option>';
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
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);
71 <form method=get name='F' action='/db/events/index.php' >
73 <table align='bottom'>
76 <input type='radio' name='type' id='events' value='Event' checked='checked'> Events:
78 <input type='text' onSelect="submit();" onFocus='events.checked=true' name='event' size=20>
80 <input type='radio' name='type' id='persons' value='Person'> Persons:
82 <input type='text' onSelect="submit();" onFocus='persons.checked=true' name='person' size=20>
84 <input type='radio' name='type' id='nodes' value='Node'> Nodes:
86 <input type='text' onSelect="submit();" onFocus='nodes.checked=true' name='node' size=20>
88 <input type='radio' name='type' id='sites' value='Site'> Sites:
90 <input type='text' onSelect="submit();" onFocus='sites.checked=true' name='site' size=20>
92 <input type='radio' name='type' id='slices' value='Slice'> Slices:
94 <input type='text' onSelect="submit();" onFocus='slices.checked=true' name='slice' size=20>
99 <tr><th>FROM (inclusive)</th> <th>UNTIL (inclusive)</th> </tr>
103 <SELECT NAME='from_d'>
104 $from_d_dropdown_options
106 <SELECT NAME='from_m' >
107 $from_m_dropdown_options
109 <SELECT NAME='from_y' >
110 $from_y_dropdown_options
115 <SELECT NAME=' until_d' >
116 $until_d_dropdown_options
118 <SELECT NAME=' until_m' >
119 $until_m_dropdown_options
121 <SELECT NAME=' until_y' >
122 $until_y_dropdown_options
127 <input type='submit' align='middle' value='Show Events'>
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);
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);
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);
156 //////////////////////////////////////////////////////////// layout
157 function truncate ($text,$numb,$etc = "...") {
158 if (strlen($text) > $numb) {
159 $text = substr($text, 0, $numb);
165 // outline node ids and person ids with a link
166 function e_node ($node_id) {
167 if (! $node_id) return "";
168 return l_node_t($node_id,$node_id);
170 function e_person ($person_id) {
171 if (! $person_id) return "";
172 return l_person_t($person_id,$person_id);
175 function e_event ($event_id) {
176 if (! $event_id) return "";
177 return href(l_event("Event","event",$event_id),$event_id);
180 function e_subject ($type,$id) {
181 $mess=$type . " " . $id;
183 case 'Node': return l_node_t ($id,$mess);
184 case 'Site': return l_site_t ($id,$mess);
185 case 'Person': return l_person_t ($id,$mess);
186 case 'Slice': return l_slice_t ($id,$mess);
187 case 'Role': case 'Key': case 'PCU': case 'Interface': case 'NodeGroup': case "Address":
189 default: return "Unknown $type" . "-" . $id;
193 // synthesize links to the subject objects from types and ids
194 function e_subjects ($param) {
195 $types=$param['object_types'];
196 $ids=$param['object_ids'];
197 if ( ! $types) return "";
198 return plc_vertical_table(array_map ("e_subject",$types,$ids));
201 function e_issuer ($param) {
202 if ($param['node_id']) return e_subject('Node',$param['node_id']);
203 if ($param['person_id']) return e_subject('Person',$param['person_id']);
207 function e_auth ($event) {
208 if (array_key_exists('auth_type',$event))
209 return $event['auth_type'];
214 function e_fault ($event) {
215 $f=$event['fault_code'];
216 if ($f==0) return "OK";
220 ////////////////////////////////////////////////////////////
221 // for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
224 if ( ! plc_is_admin()) {
225 plc_warning("You need admin role to see this page.");
227 } else if (! $_GET['type']) {
228 echo "<h2>Select the events to focus on :</h2>";
229 // print the selection frame
235 $tabs['Back to events form']=l_events();
239 list($from_date,$from_time,$until_date,$until_time) = parse_dates ();
240 // add one day to until_time - otherwise this corresponds to 0:0
241 $until_time += $STEP;
242 if ( ($from_time != 0) && ($until_time != $STEP) && ($from_time > $until_time) ) {
243 $messages[] = "Warning - <from> is after <until>";
247 // sort events by time is not good enough, let's use event_id
248 $filter['-SORT']='-event_id';
249 if ($from_time != 0) {
250 $filter[']time']=$from_time;
252 if ($until_time != $STEP) {
253 $filter['[time']=$until_time;
256 //////////////////////////////////////// Events
258 if ($type == 'Event') {
260 // and the filter applied for fetching events using GetEvent
261 $user_desc=$_GET['event'];
262 if ( ! empty($user_desc)) {
263 // should parse stuff like 45-90,230-3000 - some other day
264 $filter['event_id']=intval($user_desc);
266 // the filter might be void here - in python we need an empty dict but that's not what we get so
267 if (empty($filter)) {
270 $events = $api->GetEvents($filter);
271 $title="Events matching " . ($user_desc ? $user_desc : "everything");
273 $title .= " From " . $from_date;
274 if ($until_time != $STEP)
275 $title .= " Until " . $until_date;
277 // see actual display of $title and $events below
283 $primary_key='person_id';
285 $user_input=$_GET['person'];
286 $method="GetPersons";
287 $object_type='Person';
291 $primary_key='node_id';
292 $string_key='hostname';
293 $user_input=$_GET['node'];
299 $primary_key='site_id';
300 $string_key='login_base';
301 $user_input=$_GET['site'];
307 $primary_key='slice_id';
309 $user_input=$_GET['slice'];
311 $object_type='Slice';
316 $title=sprintf('Events for type %s:',$object_type);
317 foreach ( split(",",$user_input) as $user_desc) {
319 if (my_is_int($user_desc)) {
320 $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
321 if (empty ($obj_check)) {
322 $messages[] = "No such " . $primary_key . ": " . $user_desc;
324 $object_ids[] = $obj_check[0][$primary_key];
325 $title .= $user_desc . ", " ;
329 $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
330 if (empty($new_object_ids)) {
331 $messages[] = "No " . $string_key . " matching " . $user_desc;
333 foreach ($new_object_ids as $new_obj_id) {
334 $object_ids[] = $new_obj_id[$primary_key];
335 $title .= $new_obj_id[$primary_key] . ", ";
341 $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
342 // get set of event_ids
343 $event_ids = array_map ( create_function ('$eo','return $eo["event_id"];') , $event_objs);
345 $events = $api->GetEvents (array('event_id'=>$event_ids));
347 // see actual display of $title and $events below
351 drupal_set_title ($title);
353 if (!empty($messages))
354 foreach ($messages as $line)
355 drupal_set_message($line);
357 $headers=array("Id"=>"int",
358 "Time"=>"EnglishDateTime",
361 "Subjects"=>"string",
368 $table_options=array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
369 "Click the button in the D(etails) column to get more details",
372 plc_table_start("events",$headers,"0r",$table_options);
373 foreach ($events as $event) {
376 $message = htmlentities($event['message'], ENT_QUOTES);
377 $call = htmlentities($event['call'], ENT_QUOTES);
378 $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
379 $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
380 // $method = sprintf('<span title="%s">%s</span>',$call,$method);
382 // the message button
383 $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
384 $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
385 $details="<input type=button name='message' value='X' onclick='alert(\"" . $text . "\")'";
386 // $message=sprintf('<span title="%s">%s</span>',$message,$message);
388 $message=truncate($event['message'],40);
389 plc_table_row_start();
390 plc_table_cell(e_event($event['event_id']));
391 plc_table_cell(date('M/d/Y H:i', $event['time']));
392 plc_table_cell($event['call_name']);
393 plc_table_cell($message);
394 plc_table_cell(e_subjects($event));
395 plc_table_cell(e_issuer($event));
396 plc_table_cell(e_auth($event));
397 plc_table_cell(e_fault($event));
398 plc_table_cell($details);
401 plc_table_end("events");
406 include 'plc_footer.php';