a clean version of columns.php
[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_nodes = array();
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 $comon_live_data = "";
24 var $tophat_live_data = "";
25 var $ComonData = array();
26 var $TopHatData = array();
27 var $TopHatAgents = array();
28
29 var $table_ids;
30
31 var $HopCount = array();
32 var $RTT = array();
33
34 function PlekitColumns ($column_configuration, $fix_columns, $tag_columns, $extra_columns=NULL, $this_table_headers=NULL) {
35
36         if ($column_configuration != NULL) {
37         $this->fix_columns = $fix_columns;
38         $this->tag_columns = $tag_columns;
39         $this->extra_columns = $extra_columns;
40
41         $this->prepare_headers();
42         $this->parse_configuration($column_configuration);
43
44         $this->visible_headers = $this->get_visible();
45         }
46 }
47
48
49
50 /*
51
52 INFO/HEADERS
53
54 */
55
56 function prepare_headers() {
57
58 foreach ($this->fix_columns as $column) {
59 $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, 'source'=>'myplc');
60 }
61
62 $tmp_headers = array();
63
64 if ($this->extra_columns)
65 foreach ($this->extra_columns as $column) {
66 $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, 'source'=>$column['source']);
67
68 }
69
70 if ($this->tag_columns)
71 foreach ($this->tag_columns as $column) {
72
73 if ($column['headerId'] != "")
74         $headerId = $column['headerId'];
75 else
76         $headerId = $column['header'];
77
78 $tmp_headers[$headerId]=array('header'=>$headerId,'type'=>$column['type'],'tagname'=>$column['tagname'],'title'=>$column['title'], 'description'=>$column['title'], 'label'=>$column['header'],'visible'=>false, 'source'=>'myplc');
79 }
80
81 usort ($tmp_headers, create_function('$col1,$col2','return strcmp($col1["label"],$col2["label"]);'));
82
83 foreach ($tmp_headers as $t) 
84 $this->all_headers[$t['header']] = $t;
85
86 //$this->all_headers = array_merge($this->all_headers, $tmp_headers);
87
88 //print($this->print_headers());
89
90 return $this->all_headers;
91
92 }
93
94
95 function get_headers() {
96
97 return $this->all_headers;
98
99 }
100
101 function get_selected_period($label) {
102
103 if ($this->all_headers[$label."w"]['visible'])
104         return "w";
105 else if ($this->all_headers[$label."m"]['visible'])
106         return "m";
107 else if ($this->all_headers[$label."y"]['visible'])
108         return "y";
109 else if ($this->all_headers[$label]['visible'])
110         return "";
111         
112 return "";
113 }
114
115 function node_tags() {
116
117         $fetched_tags = array('node_id','hostname');    
118
119         foreach ($this->all_headers as $h)
120         {
121                 if ($h['visible'] == true && $h['tagname'] != "" && !$h['fetched'] && $h['source']=="myplc")
122                         $fetched_tags[] = $h['tagname'];
123         }
124
125         return $fetched_tags;
126 }
127
128 function print_headers() {
129
130         $headers = "";  
131
132         foreach ($this->all_headers as $l => $h)
133         {
134                 $headers.="<br>[".$l."]=".$h['header'].":".$h['label'].":".$h['tagname'].":".$h['visible'];
135         }
136         return $headers;
137 }
138
139 function get_visible() {
140
141         $visibleHeaders = array();      
142
143         foreach ($this->all_headers as $h)
144         {
145                 if ($h['visible'] == true)
146                         $visibleHeaders[] = $h['header'];
147         }
148         return $visibleHeaders;
149 }
150
151 function headerIsVisible($header_name) {
152
153 $headersToShow = $this->visible_headers;
154
155 if (in_array($header_name, $headersToShow))
156         return true;
157
158 if ($this->inTypeC($header_name."w"))
159         return (in_array($header_name."w", $headersToShow) || in_array($header_name."m", $headersToShow) || in_array($header_name."y", $headersToShow));
160 }
161
162
163
164
165 /*
166
167 CONFIGURATION
168
169 */
170
171
172 function parse_configuration($column_configuration) {
173
174         $this->column_configuration = $column_configuration;
175         $columns_conf = explode("|", $column_configuration);
176
177
178         foreach ($columns_conf as $c)
179         {
180                 $conf = explode(":",$c);
181
182                 if ($conf[0] == "default")
183                         continue;
184
185                 if (!$this->all_headers[$conf[0]])
186                         continue;
187
188                 $this->all_headers[$conf[0]]['visible']=true;
189
190                 if ($this->all_headers[$conf[0]]['source'] == "comon")
191                         $this->comon_live_data.=",".$this->all_headers[$conf[0]]['tagname'];
192
193                 if ($this->all_headers[$conf[0]]['source'] == "tophat")
194                         $this->tophat_live_data.=",".$this->all_headers[$conf[0]]['tagname'];
195         }
196
197 }
198
199
200                 
201
202
203 /*
204
205 CELLS
206
207 */
208
209 function convert_data($value, $data_type) {
210
211         //print "converting ".$value." as ".$data_type;
212
213         if ($value == "" || $value == null || $value == "n/a" || $value == "None")
214                 return "n/a";
215
216         if ($data_type == "string")
217                 return $value;
218
219         if ($data_type == "date") 
220                 return date("Y-m-d", $value);
221
222         if ($data_type == "uptime") 
223                 return (int)((int) $value / 86400);
224
225         if (is_numeric($value))
226                 return ((int) ($value * 10))/10;
227         
228         return $value;
229
230 }
231
232 function getTopHatAgents() {
233
234         $tophat_auth = array( 'AuthMethod' => 'password', 'Username' => 'guest@top-hat.info', 'AuthString' => 'guest');
235         $tophat_api = new TopHatAPI($tophat_auth);
236
237         //print ("Requesting tophat agents...");
238         //print_r($r);
239
240         $values = $tophat_api->Get('agents', 'latest', array('colocated.platform_name' => array('SONoMA', 'DIMES', 'ETOMIC', 'TDMI'), 'platform_name'=> 'TDMI'), array('hostname', 'colocated.peer_name', 'colocated.platform_name'));
241
242         $result = array();
243
244         if ($values) foreach ($values as $t) {
245                 //print_r($t);
246                 //print("<hr>");
247                 $result[$t['hostname']] = "";
248                 foreach ($t['colocated'] as $ll) {
249
250                         if (strpos($result[$t['hostname']]['all'],$ll['platform_name']) === false) {
251                                 if ($result[$t['hostname']]['all'] != "")
252                                         $result[$t['hostname']]['all'] .= ",";
253                                 $result[$t['hostname']]['all'] .= $ll['platform_name'];
254                         }
255
256                         if ($ll['platform_name'] == 'SONoMA') {
257                         if (strpos($result[$t['hostname']]['sonoma'],$ll['peer_name']) === false) {
258                                         if ($result[$t['hostname']]['sonoma'] != "")
259                                                 $result[$t['hostname']]['sonoma'] .= ",";
260                                         $result[$t['hostname']]['sonoma'] .= $ll['peer_name'];
261                         }
262                         }
263
264                         if ($ll['platform_name'] == 'TDMI') {
265                         if (strpos($result[$t['hostname']]['tdmi'],$ll['peer_name']) === false) {
266                                 if ($result[$t['hostname']]['tdmi'] != "")
267                                         $result[$t['hostname']]['tdmi'] .= ",";
268                                 $result[$t['hostname']]['tdmi'] .= $ll['peer_name'];
269                         }
270                         }
271                 }
272         }
273
274         $this->TopHatAgents = $result;
275
276         //print_r($this->TopHatAgents);
277
278         return $result;
279 }
280
281 function getTopHatData($data, $planetlab_nodes) {
282
283         $tophat_auth = array( 'AuthMethod' => 'password', 'Username' => 'guest@top-hat.info', 'AuthString' => 'guest');
284         $tophat_api = new TopHatAPI($tophat_auth);
285
286         $requested_data = explode(",", $data);
287
288         $r = array ('hostname');
289         
290         foreach ($requested_data as $rd)
291                 if ($rd) $r[] = $rd;
292
293         //print ("Requesting data from TopHat ...");
294         //print_r($r);
295
296         $values = $tophat_api->Get('ips', 'latest', array('hostname' => $planetlab_nodes), $r );
297
298         $result = array();
299
300         if ($values) foreach ($values as $t)
301                 foreach ($requested_data as $rd)
302                         if ($rd) $result[$t['hostname']][$rd] = $t[$rd];
303
304         //print_r($result);
305
306         return $result;
307 }
308
309
310 function comon_query_nodes($requested_data) {
311
312         $comon_url = "http://comon.cs.princeton.edu";
313         $comon_api_url = "status/tabulator.cgi?table=table_nodeviewshort&format=formatcsv&dumpcols='name";
314
315         if (MYSLICE_COMON_URL != "")
316                 $comon_url = MYSLICE_COMON_URL;
317
318         $url = $comon_url."/".$comon_api_url.$requested_data."'";
319
320         //print ("Retrieving comon data for url ".$url);
321
322         $sPattern = '\', \'';
323         $sReplace = '|';
324
325         $str=@file_get_contents($url);
326
327         if ($str === false)
328                 return '';
329
330         $result=preg_replace( $sPattern, $sReplace, $str );
331         $sPattern = '/\s+/';
332         $sReplace = ';';
333         $result=preg_replace( $sPattern, $sReplace, $result );
334
335         $comon_data = explode(";", $result);
336         $cl = array();
337         $comon_values = array();
338
339         foreach ($comon_data as $cd) {
340                 $cc = explode("|", $cd);
341                 if ($cc[0] == "name") {
342                         $cl = $cc;
343                 }
344                 $comon_values[$cc[0]] = array();
345                 $cindex=1;
346                 foreach ($cl as $cltag) {
347                         if ($cltag != "name")
348                                 $comon_values[$cc[0]][$cltag] = $cc[$cindex++];
349                 }
350         }
351
352         return $comon_values;
353 }
354
355
356 //Depending on the columns selected more data might need to be fetched from
357 //external sources
358
359 function fetch_live_data($all_nodes) {
360
361         //print("<p>fetching live data<p>");
362
363 //comon data
364         if ($this->comon_live_data != "") {
365         
366                 //print ("live data to be fetched =".$this->comon_live_data);
367                 $this->ComonData= $this->comon_query_nodes($this->comon_live_data);
368                 //print_r($this->ComonData);
369         }
370
371 //TopHat per_node data
372         if ($this->tophat_live_data != "")
373         {
374                 $dd = array();
375
376                 if ($all_nodes) foreach ($all_nodes as $n)
377                         $dd[] = $n['hostname'];
378
379                 //print("Calling tophat api for ".$this->tophat_live_data);
380                 $st = time() + microtime();
381                 $this->TopHatData = $this->getTopHatData($this->tophat_live_data, $dd);
382                 //printf(" (%.2f ms)<br/>", (time() + microtime()-$st)*100);
383                 //print_r($this->TopHatData);
384         }
385
386 }
387
388
389 function cells($table, $node) {
390
391 //$node_string = "";
392
393 foreach ($this->all_headers as $h) {
394
395 if (!$h['fixed']) { 
396
397 if ($h['visible'] != "") {
398
399 if ($h['source'] == "comon")
400 {
401         //print("<br>Searching for ".$h['tagname']."at ".$node);
402         if ($this->ComonData != "")
403                 $value = $this->convert_data($this->ComonData[$node['hostname']][$h['tagname']], $h['tagname']);
404         else
405                 $value = "n/a";
406
407         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
408         //$node_string.= "\"".$value."\",";
409 }
410 else if ($h['source'] == "tophat")
411 {
412         if ($this->TopHatData != "")
413                 $value = $this->convert_data($this->TopHatData[$node['hostname']][$h['tagname']], $h['type']);
414         else
415                 $value = "n/a";
416
417         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
418         //$node_string.= "\"".$value."\",";
419 }
420 else
421 {
422         //$value = $node[$h['tagname']];
423         $value = $this->convert_data($node[$h['tagname']], $h['type']);
424         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
425         //$node_string.= "\"".$value."\",";
426 }
427 }
428 else 
429         if ($node[$h['tagname']])
430         {
431                 $value = $this->convert_data($node[$h['tagname']], $h['type']);
432                 $table->cell($value, array('name'=>$h['header'], 'display'=>'none'));
433         }
434         else
435                 $table->cell("n/a", array('name'=>$h['header'], 'display'=>'none'));
436 }
437 }
438
439 //return $node_string;
440
441 }
442
443
444 /*
445
446 HTML
447
448 */
449
450
451 function javascript_init() {
452
453 print("<script type='text/javascript'>");
454 print("highlightOption('AU');");
455 print("overrideTitles();");
456 print("</script>");
457
458 }
459
460 function quickselect_html() {
461
462 $quickselection = "<select id='quicklist' onChange=changeSelectStatus(this.value)><option value='0'>Short column descriptions and quick add/remove</option>";
463 $prev_label="";
464 $optionclass = "out";
465 foreach ($this->all_headers as $h)
466 {
467         if ($h['header'] == "hostname" || $h['header'] == "ID")
468                 continue;
469
470         if ($h['fixed'])
471                 $disabled = "disabled=true";
472         else
473                 $disabled = "";
474
475         if ($this->headerIsVisible($h['label']))
476                 $optionclass = "in";
477         else
478                 $optionclass = "out";
479
480         if ($prev_label == $h['label'])
481                 continue;
482
483         $prev_label = $h['label'];
484
485         $quickselection.="<option id='option'".$h['label']." class='".$optionclass."' value='".$h['label']."'><span class='bold'>".$h['label']."</span>:&nbsp;".$h['title']."</option>";
486
487 }
488
489 $quickselection.="</select>";
490
491 return $quickselection;
492
493 }
494
495
496 function configuration_panel_html($showDescription) {
497
498 if ($showDescription)
499         $table_width = 700;
500 else
501         $table_width = 350;
502
503 print("<table class='center' width='".$table_width."px'>");
504 print("<tr><th class='top'>Add/remove columns</th>");
505
506 if ($showDescription)
507         print("<th class='top'>Column description and configuration</th>");
508
509 print("</tr><tr><td class='top' width='300px'>");
510
511         print('<div id="scrolldiv">');
512 print ("<table>");
513         $prev_label="";
514         $optionclass = "out";
515         foreach ($this->all_headers as $h)
516         {
517                 if ($h['header'] == "hostname" || $h['header'] == "ID")
518                         continue;
519
520                 if ($h['fixed'])
521                         $disabled = "disabled=true";
522                 else
523                         $disabled = "";
524
525                 if ($this->headerIsVisible($h['label']))
526                 {
527                         $selected = "checked=true";
528                         $fetch = "true";
529                         //print("header ".$h['label']." checked!");
530                 }
531                 else
532                 {
533                         $selected = "";
534                         if ($h['fetched'])
535                                 $fetch = "true";
536                         else
537                                 $fetch = "false";
538                 }
539
540                 print("<input type='hidden' id='tagname".$h['header']."' value='".$h['tagname']."'></input>");
541
542                 if ($prev_label == $h['label'])
543                         continue;
544
545                 $prev_label = $h['label'];
546                 $period = $this->get_selected_period($h['label']);
547
548                 print ("<tr><td>
549 <input type='hidden' id='fetched".$h['label']."' value=',".$period.",".$fetch."'></input>
550 <input type='hidden' id='period".$h['label']."' value='".$period."'></input>
551 <input type='hidden' id='type".$h['label']."' value='".$h['type']."'></input>
552 <input type='hidden' id='source".$h['label']."' value='".$h['source']."'></input>
553                 <div id='".$h['label']."' name='columnlist' class='".$optionclass."' onclick='highlightOption(this.id)'>
554 <table class='columnlist' id='table".$h['label']."'><tr>
555 <td class='header'><span class='header'>".$h['label']."</span></td> 
556 <td align=left>&nbsp;<span class='short' id ='htitle".$h['label']."'>".$h['title']."</span>&nbsp;</td>
557 <td class='smallright'>&nbsp;<span class='short' id ='loading".$h['label']."'></span>&nbsp;</td>
558 <td class='smallright'><input id='check".$h['label']."' name='".$h['tagname']."' type='checkbox' ".$selected." ".$disabled." autocomplete='off' value='".$h['label']."' onclick='changeCheckStatus(this.id)'></input></td>
559 </tr></table></div></td></tr>");
560         }
561
562         print("</table> </div></td>");
563
564 if ($showDescription)
565 {
566         print("<td class='top' width='400px'>");
567         print("<div id='selectdescr'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td>");
568 }
569
570 print("</tr>");
571
572 if ($showDescription)
573         print("<td></td>");
574
575 print(" </tr> </table>");
576 }
577
578
579
580 function column_filter () {
581
582 echo <<< EOF
583
584 Highlight <select onChange="filterByType(this.value)">
585 <option value="none">None</option>
586 <option value="capabilities">Capabilities</option>
587 <option value="statistics">Statistics</option>
588 <option value="network">Network</option>
589 <option value="pairwise">Pairwise</option>
590 <option value="other">Other</option>
591 </select>
592 <p>
593
594 EOF;
595 }
596
597   function column_html ($colHeader, $colName, $colId, $fulldesc, $visible) {
598
599         if ($visible) 
600                 $display = 'display:table-cell';
601         else 
602                 $display = 'color:red;display:none';
603
604     return "
605         <th class='sample plekit_table' name='confheader".$colHeader."' id='testid' style='".$display."'>
606         <div id=\"".$colId."\" onclick=\"showDescription('".$colHeader."')\" onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div>
607         </th>
608         ";
609   }
610
611   function column_fix_html ($colHeader, $colName, $colId) {
612
613         $display = 'display:table-cell';
614
615         $res="<th name='confheader".$colHeader."' class='fix plekit_table' style='$display'>";
616                 $res.= "<div id='$colId' onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div></th>";
617
618         return $res;
619   }
620
621
622
623 /*
624
625 UTILS
626
627 */
628
629 //simple strings
630 function inTypeA($header_name) {
631         $typeA = array('ST','SN','RES','OS','NRR','NTP','NSR','NSF','NDS','NTH','NEC','LRN','LCY','LPR','LCN','LAT','LON','IP','ASN','AST');
632         return in_array($header_name, $typeA);
633 }
634
635 //integers
636 function inTypeB($header_name) {
637         $typeB = array('BW','DS','MS','CC','CR','AS','DU','CN');
638         return in_array($header_name, $typeB);
639 }
640
641 //statistical values
642 function inTypeC($header_name) {
643         $typeC = array('Rw','Rm','Ry','Lw','Lm','Ly','Sw','Sm','Sy','CFw','CFm','CFy','BUw','BUm','BUy','MUw','MUm','MUy','SSHw','SSHm','SSHy');
644         return in_array($header_name, $typeC);
645 }
646
647 //tophat
648 function inTypeD($header_name) {
649         $typeD = array('HC');
650         return in_array($header_name, $typeD);
651 }
652
653
654 function removeDuration($header)
655 {
656         if ($this->inTypeC($header))
657                 return substr($header, 0, strlen($header)-1);
658         else
659                 return $header;
660 }
661
662 }
663
664 ?>
665
666