3 // this ajax hook returns the 'leases_data' html <table>
4 // that holds the data about leases
7 require_once 'plc_login.php';
9 // Get session and API handles
10 require_once 'plc_session.php';
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'];
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);
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
35 foreach ($leases as $lease) {
36 $hostname=$lease['hostname'];
37 if ( ! isset($host_hash[$hostname] )) {
38 $host_hash[$hostname]=array();
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;
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>";
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'];
66 if (array_key_exists($hostname,$host_hash) ) $leases=$host_hash[$hostname];
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'];
77 $duration=$lease['nuntil']-$counter;
78 echo "<td colspan='$duration'>" . $lease['lease_id'] . '&' . $lease['name'] . "</td>";
79 $counter=$lease['nuntil'];