148fca39e418b1ee3c83497d655e4fd110e473a2
[plewww.git] / planetlab / slices / leases-data.php
1 <?php
2
3 // this ajax hook returns the 'leases_data' html <table>
4 // that holds the data about leases
5
6 // Require login
7 require_once 'plc_login.php';
8
9 // Get session and API handles
10 require_once 'plc_session.php';
11 global $plc, $api;
12
13 $slice_id=$_POST['slice_id'];
14 $slicename=$_POST['slicename'];
15 $leases_granularity=$_POST['leases_granularity'];
16 $leases_offset=$_POST['leases_offset'];
17 $leases_slots=$_POST['leases_slots'];
18 $leases_w=$_POST['leases_w'];
19
20 // need to recompute reservable nodes for this slice
21 $node_columns=array('hostname','node_id');
22 $reservable_nodes=$api->GetNodes(array('|slice_ids'=>intval($slice_id), 'node_type'=>'reservable'),$node_columns);
23
24 // where to start from, expressed as an offset in hours from now
25 $rough_start=time()+$leases_offset*3600;
26 // show the next 36 grains 
27 $duration=$leases_slots*$leases_granularity;
28 $steps=$duration/$leases_granularity;
29 $start=intval($rough_start/$leases_granularity)*$leases_granularity;
30 $end=$rough_start+$duration;
31 $lease_columns=array('lease_id','name','t_from','t_until','hostname','name');
32 $leases=$api->GetLeases(array(']t_until'=>$rough_start,'[t_from'=>$end,'-SORT'=>'t_from'),$lease_columns);
33 // hash nodes -> leases
34 $host_hash=array();
35 foreach ($leases as $lease) {
36   $hostname=$lease['hostname'];
37   if ( ! isset($host_hash[$hostname] )) {
38     $host_hash[$hostname]=array();
39   }
40   // resync within the table
41   $lease['nfrom']=($lease['t_from']-$start)/$leases_granularity;
42   $lease['nuntil']=($lease['t_until']-$start)/$leases_granularity;
43   $host_hash[$hostname] []= $lease;
44 }
45 // leases_data is the name used by leases.js to locate this table
46 //echo "<table id='leases_data' class='hidden'>";
47 // empty upper-left cell
48 echo "<thead><tr><td></td>";
49 // the timeslot headers read (timestamp,label)
50 $day_names=array('Su','M','Tu','W','Th','F','Sa');
51 for ($i=0; $i<$steps; $i++) {
52   $timestamp=($start+$i*$leases_granularity);
53   $day=$day_names[intval(strftime("%w",$timestamp))];
54   $label=$day . strftime(" %H:%M",$timestamp);
55   // expose in each header cell the full timestamp, and how to display it - use & as a separator*/
56   echo "<th>" . implode("&",array($timestamp,$label)) . "</th>";
57 }
58 echo "</tr></thead><tbody>";
59 // todo - sort on hostnames
60 function sort_hostname ($a,$b) { return ($a['hostname']<$b['hostname'])?-1:1;}
61 usort($reservable_nodes,"sort_hostname");
62 foreach ($reservable_nodes as $node) {
63   echo "<tr><th scope='row'>". $node['hostname'] . "</th>";
64   $hostname=$node['hostname'];
65   $leases=NULL;
66   if (array_key_exists($hostname,$host_hash) ) $leases=$host_hash[$hostname];
67   $counter=0;
68   while ($counter<$steps) {
69     if ($leases && ($leases[0]['nfrom']<=$counter)) {
70       $lease=array_shift($leases);
71       /* nicer display, merge two consecutive leases for the same slice 
72          avoid doing that for now, as it might make things confusing */
73       /* while ($leases && ($leases[0]['name']==$lease['name']) && ($leases[0]['nfrom']==$lease['nuntil'])) {
74          $lease['nuntil']=$leases[0]['nuntil'];
75          array_shift($leases);
76          }*/
77       $duration=$lease['nuntil']-$counter;
78       echo "<td colspan='$duration'>" . $lease['lease_id'] . '&' . $lease['name'] . "</td>";
79       $counter=$lease['nuntil']; 
80     } else {
81       echo "<td></td>";
82       $counter+=1;
83     }
84   }
85   echo "</tr>";
86 }
87 echo "</tbody>";
88 //echo "</table>\n";
89
90 ?>