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