new version (removed slice2.php, nodes2.php, fixed bug related to
[plewww.git] / plekit / php / columns.php
1 <?php
2
3 require_once 'tophat_api.php';
4
5 drupal_set_html_head('
6 <script type="text/javascript" src="/plekit/table/columns.js"></script>
7 ');
8
9 class PlekitColumns {
10
11 var $column_configuration = "";
12 var $reference_node = "";
13 var $first_time = false;
14
15 var $all_headers = array();
16 var $this_table_headers = array();
17 var $visible_headers = array();
18
19 var $fix_columns = array();
20 var $tag_columns = array();
21 var $extra_columns = array();
22
23 var $table_ids;
24
25 var $HopCount = array();
26
27   function PlekitColumns ($column_configuration, $fix_columns, $tag_columns, $extra_columns=NULL, $this_table_headers=NULL) {
28
29         $this->fix_columns = $fix_columns;
30         $this->tag_columns = $tag_columns;
31         $this->extra_columns = $extra_columns;
32
33         //print("<p>FIX<p>");
34         //print_r($this->fix_columns);
35         //print("<p>TAG<p>");
36         //print_r($this->tag_columns);
37         //print("<p>EXTRA<p>");
38         //print_r($this->extra_columns);
39
40         $this->prepare_headers();
41         $this->parse_configuration($column_configuration);
42
43         $this->visible_headers = $this->get_visible();
44
45         //print("<p>VISIBLE<p>");
46         //print_r($this->visible_headers);
47
48 }
49
50
51
52 /*
53
54 INFO
55
56 */
57
58 function prepare_headers() {
59
60 foreach ($this->fix_columns as $column) {
61 $this->all_headers[$column['header']]=array('header'=>$column['header'],'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'], 'fixed'=>true, 'visible'=>false);
62 }
63
64 $tmp_headers = array();
65 foreach ($this->tag_columns as $column) {
66
67 if ($column['headerId'] != "")
68         $headerId = $column['headerId'];
69 else
70         $headerId = $column['header'];
71
72 //$this->all_headers[$headerId]=array('header'=>$headerId,'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'],'visible'=>false);
73 $tmp_headers[$headerId]=array('header'=>$headerId,'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'],'visible'=>false);
74 }
75
76 if ($this->extra_columns)
77 foreach ($this->extra_columns as $column) {
78 //$this->all_headers[$column['header']]=array('header'=>$column['header'],'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'], 'fetched'=>$column['fetched'],'visible'=>false);
79 $tmp_headers[$column['header']]=array('header'=>$column['header'],'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'], 'fetched'=>$column['fetched'], 'visible'=>false);
80
81 usort ($tmp_headers, create_function('$col1,$col2','return strcmp($col1["label"],$col2["label"]);'));
82 }
83
84 foreach ($tmp_headers as $t) 
85 $this->all_headers[$t['header']] = $t;
86
87 //$this->all_headers = array_merge($this->all_headers, $tmp_headers);
88
89 //print($this->print_headers());
90
91 return $this->all_headers;
92
93 }
94
95
96 function get_headers() {
97
98 return $this->all_headers;
99
100 }
101
102 function get_selected_period($label) {
103
104 if ($this->all_headers[$label."w"]['visible'])
105         return "w";
106 else if ($this->all_headers[$label."m"]['visible'])
107         return "m";
108 else if ($this->all_headers[$label."y"]['visible'])
109         return "y";
110 else if ($this->all_headers[$label]['visible'])
111         return "";
112         
113 return "";
114 }
115
116 function node_tags() {
117
118         $fetched_tags = array('node_id');       
119
120         foreach ($this->all_headers as $h)
121         {
122                 if ($h['visible'] == true && $h['tagname'] != "" && !$h['fetched'])
123                         $fetched_tags[] = $h['tagname'];
124         }
125
126         return $fetched_tags;
127 }
128
129 function print_headers() {
130
131         $headers = "";  
132
133         foreach ($this->all_headers as $l => $h)
134         {
135                 $headers.="<br>[".$l."]=".$h['header'].":".$h['label'].":".$h['tagname'].":".$h['visible'];
136         }
137         return $headers;
138 }
139
140 function get_visible() {
141
142         $visibleHeaders = array();      
143
144         foreach ($this->all_headers as $h)
145         {
146                 if ($h['visible'] == true)
147                         $visibleHeaders[] = $h['header'];
148         }
149         return $visibleHeaders;
150 }
151
152 function headerIsVisible($header_name) {
153
154 $headersToShow = $this->visible_headers;
155
156 if (in_array($header_name, $headersToShow))
157         return true;
158
159 if ($this->inTypeC($header_name."w"))
160         return (in_array($header_name."w", $headersToShow) || in_array($header_name."m", $headersToShow) || in_array($header_name."y", $headersToShow));
161 }
162
163
164
165
166 /*
167
168 CONFIGURATION
169
170 */
171
172
173 function parse_configuration($column_configuration) {
174
175         $this->column_configuration = $column_configuration;
176         //$this->default_configuration = $default_configuration;
177
178         //print($this->print_headers());
179
180         $columns_conf = explode("|", $column_configuration);
181
182         foreach ($columns_conf as $c)
183         {
184                 $conf = explode(":",$c);
185
186                 if ($conf[0] == "default")
187                         continue;
188
189                 $this->all_headers[$conf[0]]['visible']=true;
190                 //print("<p>-".$conf[0]."-should be visible now - ".$this->all_headers[$conf[0]]['visible']);
191                 //print_r($this->all_headers[$conf[0]]);
192
193 /*
194                 if ($conf[1] == "f")
195                         continue;
196
197                 else if ($this->inTypeC($conf[0]))
198                 {
199                         $this->all_headers[$conf[0]]['duration']= substr($conf[0], strlen($conf[0])-1, strlen($conf[0]));
200                         $threshold = explode(",",$conf[1]);
201                         $this->all_headers[$conf[0]]['threshold']=$threshold;
202                 }
203                 else if ($this->inTypeD($conf[0]))
204                 {
205                         $this->reference_node = $conf[1];
206                         $this->reference_node = "planetlab-europe-07.ipv6.lip6.fr";
207                         $this->all_headers[$conf[0]]['refnode']=$this->reference_node;
208                         $threshold = explode(",",$conf[1]);
209                         $this->all_headers[$conf[0]]['threshold']=$threshold;
210                 }
211                 else if ($this->inTypeA($conf[0]))
212                 {
213                         $exclude_list = explode(",",$conf[1]);
214                         $this->all_headers[$conf[0]]['exclude_list']=$exclude_list;
215                 }
216                 else
217                 {
218                         $threshold = explode(",",$conf[1]);
219                         $this->all_headers[$conf[0]]['threshold']=$threshold;
220                 }
221 */
222         }
223 }
224
225
226                 
227
228
229 /*
230
231 CELLS
232
233 */
234
235 function getHopCount($ref_node, $planetlab_nodes)
236 {
237
238 $tophat_auth = array( 'AuthMethod' => 'password', 'Username' => 'guest', 'AuthString' => 'guest');
239 $tophat_api = new TopHatAPI($tophat_auth);
240
241 $traceroute = $tophat_api->Get('traceroute', 'latest', array('src_hostname' => $ref_node, 'dst_hostname' => $planetlab_nodes), array('dst_hostname', 'hop_count') );
242
243 $hopcount = array();
244
245 if ($traceroute) foreach ($traceroute as $t)
246 $hopcount[$t['dst_hostname']]=$t['hop_count'];
247 return $hopcount;
248 }
249
250
251 //Depending on the columns selected more data might need to be fetched from
252 //external sources
253
254 function fetch_data($nodes) {
255
256 //TopHat pairwise data
257
258         if ($this->reference_node != "")
259         {
260                 $dd = array();
261
262                 if ($nodes) foreach ($nodes as $n)
263                         $dd[] = $n['hostname'];
264
265                 if ($potential_nodes) foreach ($potential_nodes as $n)
266                         $dd[] = $n['hostname'];
267
268                 print("Calling tophat api for reference node = ".$this->reference_node);
269                 $st = time() + microtime();
270                 $HopCount = $this->getHopCount($this->reference_node, $dd);
271                 printf(" (%.2f ms)<br/>", (time() + microtime()-$st)*100);
272                 //print_r($HopCount);
273         }
274
275 }
276
277
278 function excludeItems($value, $exclude_list, $hh) {
279
280         if ($value == "")
281                 $value = "n/a";
282
283         if ($exclude_list)
284         if (in_array($value, $exclude_list))
285                 return array($value, array('name'=>$hh, 'display'=>'table-cell'));
286         else
287                 return array($value, array('name'=>$hh, 'display'=>'table-cell'));
288
289         return array($value, array('name'=>$hh, 'display'=>'table-cell'));
290 }
291
292
293 function checkThreshold($value, $threshold, $hh) {
294
295         if ($value == "")
296                 return array("n/a", array('name'=>$hh, 'display'=>'table-cell'));
297
298         if ($threshold)
299         if ((float) $value >= (float) $threshold[0] && (float) $value <= (float) $threshold[1])
300                 return array(round($value,1), array('name'=>$hh, 'display'=>'table-cell'));
301         else
302                 return array(round($value,1), array('name'=>$hh, 'display'=>'table-cell'));
303
304         return array(round($value,1), array('name'=>$hh, 'display'=>'table-cell'));
305 }
306
307
308 function cells($table, $node) {
309
310 $this->fetch_data($node);
311
312 foreach ($this->all_headers as $h)
313 {
314 if (!$h['fixed']) { 
315
316 if ($h['visible'] != "")
317 {
318 if ($this->inTypeC($h['header']))
319 {
320         $tagname = $h['tagname'];
321         $value = $node[$tagname];
322         $v = $this->checkThreshold($value, $h['threshold'], $h['header']);
323         $table->cell($v[0],$v[1]);
324 }
325 else if ($this->inTypeB($h['header']))
326 {
327         $value = $node[$h['tagname']];
328         $v = $this->checkThreshold($value, $h['threshold'], $h['header']);
329         $table->cell($v[0],$v[1]);
330 }
331 else if ($this->inTypeD($h['header']))
332 {
333         $value = $this->HopCount[$node['hostname']];
334         $v = $this->excludeItems($value, $h['threshold'], $h['header']);
335         $table->cell($v[0],$v[1]);
336 }
337 else if ($this->inTypeA($h['header']))
338 {
339         $value = $node[$h['tagname']];
340         $v = $this->excludeItems($value, $h['exclude_list'], $h['header']);
341         $table->cell($v[0],$v[1]);
342 }
343 else
344 {
345         $value = $node[$h['tagname']];
346         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
347 }
348 }
349 else 
350         if ($node[$h['tagname']])
351                 $table->cell($node[$h['tagname']], array('name'=>$h['header'], 'display'=>'none'));
352         else
353                 $table->cell("??", array('name'=>$h['header'], 'display'=>'none'));
354 }
355 }
356
357 }
358
359
360 /*
361
362 HTML
363
364 */
365
366
367 function javascript_init() {
368
369 print("<input type='hidden' id='reference_node' value='".$this->reference_node."' />");
370
371 print("<script type='text/javascript'>");
372 print("highlightOption('AU');");
373 print("overrideTitles();");
374 print("</script>");
375
376 }
377
378
379
380 function quickselect_html() {
381
382 //return '<p>This link uses the onclick event handler.<br><a href="#" onclick="setVisible(\'quicklist\');return false" target="_self">Open popup</a></p>';
383
384
385 $quickselection = "<select id='quicklist' onChange=changeSelectStatus(this.value)><option value='0'>Short column descriptions and quick add/remove</option>";
386 //$quickselection = "<select id='quicklist'><option value='0'>Short column descriptions and quick add/remove</option>";
387 $prev_label="";
388 $optionclass = "out";
389 foreach ($this->all_headers as $h)
390 {
391         if ($h['header'] == "hostname" || $h['header'] == "ID")
392                 continue;
393
394         if ($h['fixed'])
395                 $disabled = "disabled=true";
396         else
397                 $disabled = "";
398
399         if ($this->headerIsVisible($h['label']))
400         {
401                 $optionclass = "in";
402                 //$selected = "selected=selected";
403         }
404         else
405         {
406                 $optionclass = "out";
407                 //$selected = "";
408         }
409
410         if ($prev_label == $h['label'])
411                 continue;
412
413         $prev_label = $h['label'];
414
415
416 //$quickselection.="<option onclick=\"debugfilter('here2');removeSelectHandler(this);\" id='option'".$h['label']." class='".$optionclass."' value='".$h['label']."'><b>".$h['label']."</b>:&nbsp;".$h['title']."</option>";
417 $quickselection.="<option id='option'".$h['label']." class='".$optionclass."' value='".$h['label']."'><b>".$h['label']."</b>:&nbsp;".$h['title']."</option>";
418 }
419
420
421 $quickselection.="</select>";
422
423 return $quickselection;
424
425 }
426
427
428 function configuration_panel_html($showDescription) {
429
430 if ($showDescription)
431         $table_width = 700;
432 else
433         $table_width = 350;
434
435 print("<table align=center cellpadding=10 width=".$table_width.">");
436 print("<tr><th>Add/remove columns</th>");
437
438 if ($showDescription)
439         print("<th>Column description and configuration</th>");
440
441 print("</tr><tr><td valign=top width=300>");
442
443         print('<div id="scrolldiv" style="border : solid 2px grey; padding:4px; width:300px; height:180px; overflow:auto;">');
444 print ("<table>");
445         $prev_label="";
446         $optionclass = "out";
447         foreach ($this->all_headers as $h)
448         {
449                 if ($h['header'] == "hostname" || $h['header'] == "ID")
450                         continue;
451
452                 if ($h['fixed'])
453                         $disabled = "disabled=true";
454                 else
455                         $disabled = "";
456
457                 if ($this->headerIsVisible($h['label']))
458                 {
459                         $selected = "checked=true";
460                         $fetch = "true";
461                         //print("header ".$h['label']." checked!");
462                 }
463                 else
464                 {
465                         $selected = "";
466                         if ($h['fetched'])
467                                 $fetch = "true";
468                         else
469                                 $fetch = "false";
470                 }
471
472                 print("<input type='hidden' id='tagname".$h['header']."' value='".$h['tagname']."'></input>");
473
474                 if ($prev_label == $h['label'])
475                         continue;
476
477                 $prev_label = $h['label'];
478                 $period = $this->get_selected_period($h['label']);
479
480 //<input type='hidden' id='fdesc".$h['label']."' value='".$h['description']."'></input>
481                 print ("<tr><td>
482 <input type='hidden' id='fetched".$h['label']."' value=',".$period.",".$fetch."'></input>
483 <input type='hidden' id='period".$h['label']."' value='".$period."'></input>
484                 <div id='".$h['label']."' name='columnlist' class='".$optionclass."' onclick='highlightOption(this.id)'>
485 <table width=280 id='table".$h['label']."'><tr>
486 <td bgcolor=#CAE8EA align=center width=30><b><span style='color:#3399CC'>".$h['label']."</span></b></td> 
487 <td align=left>&nbsp;<span style='height:10px' id ='htitle".$h['label']."'>".$h['title']."</span>&nbsp;</td>
488 <td align=right width=20>&nbsp;<span style='height:10px' id ='loading".$h['label']."'></span>&nbsp;</td>
489 <td align=right width=20><input id='check".$h['label']."' name='".$h['tagname']."' type='checkbox' ".$selected." ".$disabled." autocomplete='off' value='".$h['label']."' onclick='changeCheckStatus(this.id)'></input></td>
490 </tr></table></div></td></tr>");
491         }
492
493         print("</table> </div></td>");
494
495 if ($showDescription)
496 {
497         print("<td valign=top width=400>");
498         print("<div class='myslice' id='selectdescr'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td>");
499 }
500
501 print("</tr>");
502 //print("<tr><td align=center>");
503 //print("<input type='button' value='Reset' onclick=resetCols('previousConf') />");
504 //print("<input type='button' value='Default' onclick=saveConfiguration('defaultConf') />");
505 //print("<input type='button' value='Reset table' onclick=\"resetConfiguration()\" />");
506 //print("</td>");
507 //print("&nbsp;<input type='button' value='Save configuration' onclick=saveConfiguration('column_configuration') />");
508 //print("&nbsp;<input type='button' id='fetchbutton' onclick='fetchData()' value='Fetch data' disabled=true /> </td>");
509
510 if ($showDescription)
511         print("<td></td>");
512
513 print(" </tr> </table>");
514 }
515
516
517
518 function column_filter () {
519
520 echo <<< EOF
521
522 Highlight <select onChange="filterByType(this.value)">
523 <option value="none">None</option>
524 <option value="capabilities">Capabilities</option>
525 <option value="statistics">Statistics</option>
526 <option value="network">Network</option>
527 <option value="pairwise">Pairwise</option>
528 <option value="other">Other</option>
529 </select>
530 <p>
531
532 EOF;
533 }
534
535   function column_html ($colHeader, $colName, $colId, $fulldesc, $visible) {
536
537         if ($visible) 
538                 $display = 'display:table-cell';
539         else 
540                 $display = 'color:red;display:none';
541
542     return "
543         <th class='sample plekit_table' name='confheader".$colHeader."' id='testid' style='".$display."'>
544         <div id=\"".$colId."\" onclick=\"showDescription('".$colHeader."')\" onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div>
545         </th>
546         ";
547   }
548
549   function column_fix_html ($colHeader, $colName, $colId) {
550
551         $display = 'display:table-cell';
552
553         $res="<th name='confheader".$colHeader."' class='fix plekit_table' style='$display'>";
554                 $res.= "<div id='$colId' onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div></th>";
555
556         return $res;
557   }
558
559
560 function graph_html($colHeader) {
561
562         return "<p><img src='/planetlab/slices/graph.png' width='20' align='BOTTOM'><input type='checkbox' id='graph".$colHeader."'></input> Show details on mouse over";
563
564         }
565
566 function threshold_html($colHeader) {
567
568         $updatecall = "updateColumnThreshold('".$colHeader."',window.document.getElementById('min".$colHeader."').value,window.document.getElementById('max".$colHeader."').value);";
569
570         $bubble="<b>Grey-out values between</b>  <input type='text' id='min".$colHeader."' size='2' value='5'> (low) and <input type='text' id='max".$colHeader."' size='2' value='90'> (high) <input type='submit' value='Update' onclick=".$updatecall.">&nbsp;</input>"; 
571
572         return $bublle;
573 }
574
575
576 /*
577
578 UTILS
579
580 */
581
582 //simple strings
583 function inTypeA($header_name) {
584         $typeA = array('ST','SN','RES','OS','NRR','NTP','NSR','NSF','NDS','NTH','NEC','LRN','LCY','LPR','LCN','LAT','LON','IP','ASN','AST');
585         return in_array($header_name, $typeA);
586 }
587
588 //integers
589 function inTypeB($header_name) {
590         $typeB = array('BW','DS','MS','CC','CR','AS','DU','CN');
591         return in_array($header_name, $typeB);
592 }
593
594 //statistical values
595 function inTypeC($header_name) {
596         $typeC = array('Rw','Rm','Ry','Lw','Lm','Ly','Sw','Sm','Sy','CFw','CFm','CFy','BUw','BUm','BUy','MUw','MUm','MUy','SSHw','SSHm','SSHy');
597         return in_array($header_name, $typeC);
598 }
599
600 //tophat
601 function inTypeD($header_name) {
602         $typeD = array('HC');
603         return in_array($header_name, $typeD);
604 }
605
606
607 function removeDuration($header)
608 {
609         if ($this->inTypeC($header))
610                 return substr($header, 0, strlen($header)-1);
611         else
612                 return $header;
613 }
614
615 }
616
617 ?>
618
619