get_array
[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 (get_array2($this->all_headers, $label."w", 'visible'))
105         return "w";
106         else if (get_array2($this->all_headers, $label."m", 'visible'))
107         return "m";
108         else if (get_array2($this->all_headers, $label."y", 'visible'))
109         return "y";
110         else return "";
111 }
112
113 function node_tags() {
114
115         $fetched_tags = array('node_id','hostname');
116
117         foreach ($this->all_headers as $h)
118         {
119                 if ($h['visible'] == true
120                  && $h['tagname'] != ""
121                  && !get_array($h, 'fetched')
122                  && $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
393         foreach ($this->all_headers as $h) {
394
395                 if (! get_array($h, 'fixed')) {
396
397                         if ($h['visible'] != "") {
398
399                                 if ($h['source'] == "comon") {
400                                                 //print("<br>Searching for ".$h['tagname']."at ".$node);
401                                                 if ($this->ComonData != "")
402                                                                 $value = $this->convert_data($this->ComonData[$node['hostname']][$h['tagname']], $h['tagname']);
403                                                 else
404                                                         $value = "n/a";
405
406                                                         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
407                                                 //$node_string.= "\"".$value."\",";
408                                 } else if ($h['source'] == "tophat") {
409                                         if ($this->TopHatData != "")
410                                                         $value = $this->convert_data($this->TopHatData[$node['hostname']][$h['tagname']], $h['type']);
411                                         else
412                                                 $value = "n/a";
413
414                                         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
415                                         //$node_string.= "\"".$value."\",";
416                                 } else {
417                                         //$value = $node[$h['tagname']];
418                                         $value = $this->convert_data($node[$h['tagname']], $h['type']);
419                                         $table->cell($value,array('name'=>$h['header'], 'display'=>'table-cell'));
420                                 //$node_string.= "\"".$value."\",";
421                                 }
422                         } else if (get_array($node, $h['tagname'])) {
423                                 $value = $this->convert_data($node[$h['tagname']], $h['type']);
424                                 $table->cell($value, array('name'=>$h['header'], 'display'=>'none'));
425                         } else {
426                                 $table->cell("n/a", array('name'=>$h['header'], 'display'=>'none'));
427                         }
428                 }
429         }
430
431 }
432
433
434 /*
435
436 HTML
437
438 */
439
440
441 function javascript_init() {
442
443         print("<script type='text/javascript'>");
444         print("highlightOption('AU');");
445         print("overrideTitles();");
446         print("</script>");
447
448 }
449
450 function quickselect_html() {
451
452 $quickselection = "<select id='quicklist' onChange=changeSelectStatus(this.value)><option value='0'>Short column descriptions and quick add/remove</option>";
453 $prev_label="";
454 $optionclass = "out";
455 foreach ($this->all_headers as $h)
456 {
457         if ($h['header'] == "hostname" || $h['header'] == "ID")
458                 continue;
459
460         if ($h['fixed'])
461                 $disabled = "disabled=true";
462         else
463                 $disabled = "";
464
465         if ($this->headerIsVisible($h['label']))
466                 $optionclass = "in";
467         else
468                 $optionclass = "out";
469
470         if ($prev_label == $h['label'])
471                 continue;
472
473         $prev_label = $h['label'];
474
475         $quickselection.="<option id='option'".$h['label']." class='".$optionclass."' value='".$h['label']."'><span class='bold'>".$h['label']."</span>:&nbsp;".$h['title']."</option>";
476
477 }
478
479 $quickselection.="</select>";
480
481 return $quickselection;
482
483 }
484
485
486 function configuration_panel_html($showDescription) {
487
488 if ($showDescription)
489         $table_width = 700;
490 else
491         $table_width = 350;
492
493 print("<table class='center' width='".$table_width."px'>");
494 print("<tr><th class='top'>Add/remove columns</th>");
495
496 if ($showDescription)
497         print("<th class='top'>Column description and configuration</th>");
498
499         print("</tr><tr><td class='top' width='300px'>");
500
501         print('<div id="scrolldiv">');
502         print ("<table>");
503         $prev_label="";
504         $optionclass = "out";
505         foreach ($this->all_headers as $h) {
506                 if ($h['header'] == "hostname" || $h['header'] == "ID")
507                         continue;
508
509                 if (get_array($h, 'fixed'))
510                         $disabled = "disabled=true";
511                 else
512                         $disabled = "";
513
514                 if ($this->headerIsVisible($h['label']))
515                 {
516                         $selected = "checked=true";
517                         $fetch = "true";
518                         //print("header ".$h['label']." checked!");
519                 }
520                 else
521                 {
522                         $selected = "";
523                         if ($h['fetched'])
524                                 $fetch = "true";
525                         else
526                                 $fetch = "false";
527                 }
528
529                 print("<input type='hidden' id='tagname".$h['header']."' value='".$h['tagname']."'></input>");
530
531                 if ($prev_label == $h['label'])
532                         continue;
533
534                 $prev_label = $h['label'];
535                 $period = $this->get_selected_period($h['label']);
536
537                 print ("<tr><td>
538 <input type='hidden' id='fetched".$h['label']."' value=',".$period.",".$fetch."'></input>
539 <input type='hidden' id='period".$h['label']."' value='".$period."'></input>
540 <input type='hidden' id='type".$h['label']."' value='".$h['type']."'></input>
541 <input type='hidden' id='source".$h['label']."' value='".$h['source']."'></input>
542                 <div id='".$h['label']."' name='columnlist' class='".$optionclass."' onclick='highlightOption(this.id)'>
543 <table class='columnlist' id='table".$h['label']."'><tr>
544 <td class='header'><span class='header'>".$h['label']."</span></td>
545 <td align=left>&nbsp;<span class='short' id ='htitle".$h['label']."'>".$h['title']."</span>&nbsp;</td>
546 <td class='smallright'>&nbsp;<span class='short' id ='loading".$h['label']."'></span>&nbsp;</td>
547 <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>
548 </tr></table></div></td></tr>");
549         }
550
551         print("</table> </div></td>");
552
553 if ($showDescription)
554 {
555         print("<td class='top' width='400px'>");
556         print("<div id='selectdescr'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td>");
557 }
558
559 print("</tr>");
560
561 if ($showDescription)
562         print("<td></td>");
563
564 print(" </tr> </table>");
565 }
566
567
568
569 function column_filter () {
570
571 echo <<< EOF
572
573 Highlight <select onChange="filterByType(this.value)">
574 <option value="none">None</option>
575 <option value="capabilities">Capabilities</option>
576 <option value="statistics">Statistics</option>
577 <option value="network">Network</option>
578 <option value="pairwise">Pairwise</option>
579 <option value="other">Other</option>
580 </select>
581 <p>
582
583 EOF;
584 }
585
586   function column_html ($colHeader, $colName, $colId, $fulldesc, $visible) {
587
588         if ($visible)
589                 $display = 'display:table-cell';
590         else
591                 $display = 'color:red;display:none';
592
593     return "
594         <th class='sample plekit_table' name='confheader".$colHeader."' id='testid' style='".$display."'>
595         <div id=\"".$colId."\" onclick=\"showDescription('".$colHeader."')\" onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div>
596         </th>
597         ";
598   }
599
600   function column_fix_html ($colHeader, $colName, $colId) {
601
602         $display = 'display:table-cell';
603
604         $res="<th name='confheader".$colHeader."' class='fix plekit_table' style='$display'>";
605                 $res.= "<div id='$colId' onmouseover=\"showDescription('".$colHeader."')\">$colHeader</div></th>";
606
607         return $res;
608   }
609
610
611
612 /*
613
614 UTILS
615
616 */
617
618 //simple strings
619 function inTypeA($header_name) {
620         $typeA = array('ST','SN','RES','OS','NRR','NTP','NSR','NSF','NDS','NTH','NEC','LRN','LCY','LPR','LCN','LAT','LON','IP','ASN','AST');
621         return in_array($header_name, $typeA);
622 }
623
624 //integers
625 function inTypeB($header_name) {
626         $typeB = array('BW','DS','MS','CC','CR','AS','DU','CN');
627         return in_array($header_name, $typeB);
628 }
629
630 //statistical values
631 function inTypeC($header_name) {
632         $typeC = array('Rw','Rm','Ry','Lw','Lm','Ly','Sw','Sm','Sy','CFw','CFm','CFy','BUw','BUm','BUy','MUw','MUm','MUy','SSHw','SSHm','SSHy');
633         return in_array($header_name, $typeC);
634 }
635
636 //tophat
637 function inTypeD($header_name) {
638         $typeD = array('HC');
639         return in_array($header_name, $typeD);
640 }
641
642
643 function removeDuration($header)
644 {
645         if ($this->inTypeC($header))
646                 return substr($header, 0, strlen($header)-1);
647         else
648                 return $header;
649 }
650
651 }
652
653 ?>
654
655