change defaults for the leases area
[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 $duration=$leases_slots*$leases_granularity;
27 $steps=$duration/$leases_granularity;
28 $start=intval($rough_start/$leases_granularity)*$leases_granularity;
29 $end=$rough_start+$duration;
30 $lease_columns=array('lease_id','name','t_from','t_until','hostname','name');
31 $leases=$api->GetLeases(array(']t_until'=>$rough_start,'[t_from'=>$end,'-SORT'=>'t_from'),$lease_columns);
32 // hash nodes -> leases
33 $host_hash=array();
34 foreach ($leases as $lease) {
35   $hostname=$lease['hostname'];
36   if ( ! isset($host_hash[$hostname] )) {
37     $host_hash[$hostname]=array();
38   }
39   // resync within the table
40   $lease['nfrom']=($lease['t_from']-$start)/$leases_granularity;
41   $lease['nuntil']=($lease['t_until']-$start)/$leases_granularity;
42   $host_hash[$hostname] []= $lease;
43 }
44 // leases_data is the name used by leases.js to locate this table
45 //echo "<table id='leases_data' class='hidden'>";
46 // empty upper-left cell
47 echo "<thead><tr><td></td>";
48 // the timeslot headers read (timestamp,label)
49 $day_names=array('Su','M','Tu','W','Th','F','Sa');
50 for ($i=0; $i<$steps; $i++) {
51   $timestamp=($start+$i*$leases_granularity);
52   $day=$day_names[intval(strftime("%w",$timestamp))];
53   $label=$day . strftime(" %H:%M",$timestamp);
54   // expose in each header cell the full timestamp, and how to display it - use & as a separator*/
55   echo "<th>" . implode("&",array($timestamp,$label)) . "</th>";
56 }
57 echo "</tr></thead><tbody>";
58 // todo - sort on hostnames
59 function sort_hostname ($a,$b) { return ($a['hostname']<$b['hostname'])?-1:1;}
60 usort($reservable_nodes,"sort_hostname");
61 foreach ($reservable_nodes as $node) {
62   echo "<tr><th scope='row'>". $node['hostname'] . "</th>";
63   $hostname=$node['hostname'];
64   $leases=NULL;
65   if (array_key_exists($hostname,$host_hash) ) $leases=$host_hash[$hostname];
66   $counter=0;
67   while ($counter<$steps) {
68     if ($leases && ($leases[0]['nfrom']<=$counter)) {
69       $lease=array_shift($leases);
70       /* nicer display, merge two consecutive leases for the same slice 
71          avoid doing that for now, as it might make things confusing */
72       /* while ($leases && ($leases[0]['name']==$lease['name']) && ($leases[0]['nfrom']==$lease['nuntil'])) {
73          $lease['nuntil']=$leases[0]['nuntil'];
74          array_shift($leases);
75          }*/
76       $duration=$lease['nuntil']-$counter;
77       echo "<td colspan='$duration'>" . $lease['lease_id'] . '&' . $lease['name'] . "</td>";
78       $counter=$lease['nuntil']; 
79     } else {
80       echo "<td></td>";
81       $counter+=1;
82     }
83   }
84   echo "</tr>";
85 }
86 echo "</tbody>";
87 //echo "</table>\n";
88
89 ?>