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