reservations add/delete use ajax, no need to wait for the page to reload
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 14 Feb 2011 16:36:38 +0000 (17:36 +0100)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Mon, 14 Feb 2011 16:36:38 +0000 (17:36 +0100)
planetlab/css/my_slice.css
planetlab/slices/leases-data.php [new file with mode: 0644]
planetlab/slices/leases.js
planetlab/slices/slice.php
plekit/prototype/prototype.js
plekit/table/table.js

index 0a20eb2..2cf7430 100644 (file)
@@ -28,8 +28,9 @@ div#leases_area {
   padding: 10px 25px;
 }
 
-/* don't display the scheduler data table - not quite sure this works */
-table#leases_data {
+/* this class is used to store data that needs to get passed to javascript code */
+/* these elements re not meant to be rendered */
+.hidden {
     display: none;
 }
 
diff --git a/planetlab/slices/leases-data.php b/planetlab/slices/leases-data.php
new file mode 100644 (file)
index 0000000..ae23a79
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+// this ajax hook returns the 'leases_data' html <table>
+// that holds the data about leases
+
+// Require login
+require_once 'plc_login.php';
+
+// Get session and API handles
+require_once 'plc_session.php';
+global $plc, $api;
+
+$sliceid=$_POST['sliceid'];
+$slicename=$_POST['slicename'];
+$leases_grain=$_POST['leases_grain'];
+$leases_offset=$_POST['leases_offset'];
+$leases_slots=$_POST['leases_slots'];
+$leases_w=$_POST['leases_w'];
+
+// need to recompute reservable nodes for this slice
+$node_columns=array('hostname','node_id');
+$reservable_nodes=$api->GetNodes(array('|slice_ids'=>intval($sliceid), 'node_type'=>'reservable'),$node_columns);
+
+// where to start from, expressed as an offset in hours from now
+$rough_start=time()+$leases_offset*3600;
+// show the next 36 grains 
+$duration=$leases_slots*$leases_grain;
+$steps=$duration/$leases_grain;
+$start=intval($rough_start/$leases_grain)*$leases_grain;
+$end=$rough_start+$duration;
+$lease_columns=array('lease_id','name','t_from','t_until','hostname','name');
+$leases=$api->GetLeases(array(']t_until'=>$rough_start,'[t_from'=>$end,'-SORT'=>'t_from'),$lease_columns);
+// hash nodes -> leases
+$host_hash=array();
+foreach ($leases as $lease) {
+  $hostname=$lease['hostname'];
+  if ( ! isset($host_hash[$hostname] )) {
+    $host_hash[$hostname]=array();
+  }
+  // resync within the table
+  $lease['nfrom']=($lease['t_from']-$start)/$leases_grain;
+  $lease['nuntil']=($lease['t_until']-$start)/$leases_grain;
+  $host_hash[$hostname] []= $lease;
+}
+// leases_data is the name used by leases.js to locate this table
+//echo "<table id='leases_data' class='hidden'>";
+// empty upper-left cell
+echo "<thead><tr><td></td>";
+// the timeslot headers read (timestamp,label)
+$day_names=array('Su','M','Tu','W','Th','F','Sa');
+for ($i=0; $i<$steps; $i++) {
+  $timestamp=($start+$i*$leases_grain);
+  $day=$day_names[intval(strftime("%w",$timestamp))];
+  $label=$day . strftime(" %H:%M",$timestamp);
+  // expose in each header cell the full timestamp, and how to display it - use & as a separator*/
+  echo "<th>" . implode("&",array($timestamp,$label)) . "</th>";
+}
+echo "</tr></thead><tbody>";
+// todo - sort on hostnames
+function sort_hostname ($a,$b) { return ($a['hostname']<$b['hostname'])?-1:1;}
+usort($reservable_nodes,"sort_hostname");
+foreach ($reservable_nodes as $node) {
+  echo "<tr><th scope='row'>". $node['hostname'] . "</th>";
+  $hostname=$node['hostname'];
+  $leases=$host_hash[$hostname];
+  $counter=0;
+  while ($counter<$steps) {
+    if ($leases && ($leases[0]['nfrom']<=$counter)) {
+      $lease=array_shift($leases);
+      /* nicer display, merge two consecutive leases for the same slice 
+        avoid doing that for now, as it might makes things confusing */
+      /* while ($leases && ($leases[0]['name']==$lease['name']) && ($leases[0]['nfrom']==$lease['nuntil'])) {
+        $lease['nuntil']=$leases[0]['nuntil'];
+        array_shift($leases);
+        }*/
+      $duration=$lease['nuntil']-$counter;
+      echo "<td colspan='$duration'>" . $lease['lease_id'] . '&' . $lease['name'] . "</td>";
+      $counter=$lease['nuntil']; 
+    } else {
+      echo "<td></td>";
+      $counter+=1;
+    }
+  }
+  echo "</tr>";
+}
+echo "</tbody>";
+//echo "</table>\n";
+
+?>
index 67019a6..bf98641 100644 (file)
@@ -10,8 +10,8 @@ var y_header = 12;
 var y_sep = 10;
 
 // 1-grain leases attributes
-// x_grain is configurable from $_GET
-//var x_grain = 20;
+// w_grain is configurable from $_GET
+//var w_grain = 20;
 var y_node = 15;
 var radius= 6;
 
@@ -51,36 +51,103 @@ var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-s
 
 ////////////////////////////////////////////////////////////
 // the scheduler object
-function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
+function Scheduler (sliceid, slicename, w_grain) {
+
+    // xxx-hacky dunno how to retrieve this object from an ajax callback
+    Scheduler.scheduler=this;
 
     // the data contains slice names, and lease_id, we need this to find our own leases (mine)
     this.sliceid=sliceid;
     this.slicename=slicename;
-    this.axisx=axisx;
-    this.axisy=axisy;
-    this.data=data;
+    this.paper=null;
 
-    this.x_grain = parseInt(x_grain);
+    this.w_grain = parseInt(w_grain);
     // the path for the triangle-shaped buttons
-    this.timebutton_path="M1,0L"+(this.x_grain-1)+",0L"+(this.x_grain/2)+","+y_header+"L1,0";
+    this.timebutton_path="M1,0L"+(this.w_grain-1)+",0L"+(this.w_grain/2)+","+y_header+"L1,0";
 
-    // utilities to keep track of all the leases
-    this.leases=[];
-    this.append_lease = function (lease) { 
-       this.leases.push(lease);
+    // how many time slots 
+    this.nb_grains = function () { return this.axisx.length;}
+
+    ////////////////////
+    // store the result of an ajax request in the leases_data table 
+    this.set_html = function (html_data) {
+       var table_text = $$("table#leases_data")[0].innerHTML;
+       $$("table#leases_data")[0].innerHTML=html_data;
+       table_text = $$("table#leases_data")[0].innerHTML;
+       return true;
     }
 
-    // how many time slots 
-    this.nb_grains = function () { return axisx.length;}
+    ////////////////////
+    // the names of the hidden fields that hold the input to this class
+    // are hard-wired for now
+    this.parse_html = function () {
+       this.sliceid=getInnerText($$("span#leases_sliceid")[0]).strip();
+       this.slicename=getInnerText($$("span#leases_slicename")[0]).strip();
+       this.leases_grain=getInnerText($$("span#leases_grain")[0]).strip();
+       this.leases_offset=getInnerText($$("span#leases_offset")[0]).strip();
+       this.leases_slots=getInnerText($$("span#leases_slots")[0]).strip();
+       this.leases_w=getInnerText($$("span#leases_w")[0]).strip();
+       
+       var table = $$("table#leases_data")[0];
+       // no reservable nodes - no data
+       if ( ! table) return false;
+       // check for the body too xxx
+       // the nodelabels
+       var data = [], axisx = [], axisy = [];
+       table.getElementsBySelector("tbody>tr>th").each(function (cell) {
+            axisy.push(getInnerText(cell));
+       });
+
+       // the timeslot labels
+       table.getElementsBySelector("thead>tr>th").each(function (cell) {
+           /* [0]: timestamp -- [1]: displayable*/
+            axisx.push(getInnerText(cell).split("&"));
+       });
+
+       // leases - expect colspan to describe length in grains
+       // the text contents is expected to be lease_id & slicename
+       table.getElementsBySelector("tbody>tr>td").each(function (cell) {
+           var cell_data;
+           var slice_attributes=getInnerText(cell).split('&');
+           // booked leases come with lease id and slice name
+           if (slice_attributes.length == 2) {
+               // leases is booked : slice_id, slice_name, duration in grains
+               cell_data=new Array (slice_attributes[0], slice_attributes[1], cell.colSpan);
+           } else {
+               cell_data = new Array ('','',cell.colSpan);
+           }
+            data.push(cell_data);
+       });
+
+       this.axisx=axisx;
+       this.axisy=axisy;
+       this.data=data;
+       return true;
+    }
 
-    this.init = function (canvas_id) {
-       this.total_width = x_nodelabel + this.nb_grains()*this.x_grain; 
+    ////////////////////
+    // draw 
+    this.draw_area = function (canvas_id) {
+       this.total_width = x_nodelabel + this.nb_grains()*this.w_grain; 
        this.total_height =   2*y_header /* the timelabels */
                            + 2*y_sep    /* extra space */
                            + y_node     /* all-nodes & timebuttons row */ 
                            + (this.axisy.length)*(y_node+y_sep);  /* the regular nodes and preceding space */
-       paper = Raphael (canvas_id, this.total_width+x_sep, this.total_height);
+       // reuse for paper if exists with same size, or (re-)create otherwise
+       var paper;
+       if (this.paper == null) {
+           paper = Raphael (canvas_id, this.total_width+x_sep, this.total_height);
+       } else if (this.paper.width==this.total_width && this.paper.height==this.total_height) {
+           paper=this.paper;
+           paper.clear();
+       } else {
+           $$("#"+canvas_id)[0].innerHTML="";
+           paper = Raphael (canvas_id, this.total_width+x_sep, this.total_height);
+       }
+       this.paper=paper;
 
+       var axisx=this.axisx;
+       var axisy=this.axisy;
        // maintain the list of nodelabels for the 'all nodes' button
        this.nodelabels=[];
 
@@ -114,7 +181,7 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
            } else if ( (timestamp%(12*3600))==0) {
                paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
            }
-           left+=(this.x_grain);
+           left+=(this.w_grain);
        }
 
        ////////// the row with the timeslot buttons (the one labeled 'All nodes')
@@ -134,14 +201,15 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
            timebutton.from_time=axisx[i][0];
            timebutton.scheduler=this;
            timebutton.click(timebutton_methods.click);
-           left+=(this.x_grain);
+           left+=(this.w_grain);
        }
        
        //////// the body of the scheduler : loop on nodes
        top += y_node+y_sep;
        var data_index=0;
+       this.leases=[];
        for (var i=0, len=axisy.length; i<len; ++i) {
-           nodename=axisy[i];
+           var nodename=axisy[i];
            left=0;
            var nodelabel = paper.text(x_nodelabel-x_sep,top+y_node/2,nodename).attr(txt_nodelabel)
                .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
@@ -152,10 +220,10 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
            left += x_nodelabel;
            var grain=0;
            while (grain < this.nb_grains()) {
-               lease_id=data[data_index][0];
-               slicename=data[data_index][1];
-               duration=data[data_index][2];
-               var lease=paper.rect (left,top,this.x_grain*duration,y_node,radius);
+               lease_id=this.data[data_index][0];
+               slicename=this.data[data_index][1];
+               duration=this.data[data_index][2];
+               var lease=paper.rect (left,top,this.w_grain*duration,y_node,radius);
                lease.lease_id=lease_id;
                lease.nodename=nodename;
                lease.nodelabel=nodelabel;
@@ -175,9 +243,9 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
                // record scheduler in lease
                lease.scheduler=this;
                // and vice versa
-               this.append_lease(lease);
+               this.leases.push(lease);
                // move on with the loop
-               left += this.x_grain*duration;
+               left += this.w_grain*duration;
                data_index +=1;
            }
            top += y_node + y_sep;
@@ -193,11 +261,15 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
                var from_time=lease.from_time;
                var until_time=lease.until_time;
                /* scan the leases just after this one and merge if appropriate */
-               var j=i+1;
-               while (j<len && lease_methods.compare (lease, until_time, this.leases[j])) {
-//                 window.console.log('merged index='+j);
-                   until_time=this.leases[j].until_time;
-                   ++j; ++i;
+               /* this makes sense when adding leases only though */
+               if (lease.current=='mine') {
+                   var j=i+1;
+                   while (j<len && lease_methods.compare (lease, until_time, this.leases[j])) {
+//                     window.console.log('merging index='+i+' initial='+this.leases[i].initial+' current='+this.leases[i].current);
+//                     window.console.log('merged index='+j+' initial='+this.leases[j].initial+' current='+this.leases[j].current);
+                       until_time=this.leases[j].until_time;
+                       ++j; ++i;
+                   }
                }
                if (lease.current!='free') { // lease to add
                    actions.push(new Array('add-leases',
@@ -212,10 +284,6 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
            }
        }
        sliceid=this.sliceid;
-       // once we're done with the side-effect performed in actions.php, we need to refresh this view
-       redirect = function (sliceid) {
-           window.location = '/db/slices/slice.php?id=' + sliceid + '&show_details=0&show_nodes=1&show_nodes_resa=1';
-       }
        // Ajax.Request comes with prototype
        var ajax=new Ajax.Request('/planetlab/common/actions.php', 
                                  {method:'post',
@@ -224,14 +292,14 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
                                   onSuccess: function(transport) {
                                       var response = transport.responseText || "no response text";
                                       document.body.style.cursor = "default";
-                                      alert("Server answered:\n\n" + response + "\n\nPress OK to refresh page");
-                                      redirect(sliceid);
+//                                    alert("Server answered:\n\n" + response + "\n\nPress OK to refresh page");
+                                      Scheduler.scheduler.refresh();
                                   },
                                   onFailure: function(){ 
                                       document.body.style.cursor = "default";
                                       alert("Could not reach server, sorry...\n\nPress OK to refresh page");
                                       // not too sure what to do here ...
-                                      redirect(sliceid);
+                                      Scheduler.scheduler.refresh();
                                   },
                                  });
     }
@@ -246,6 +314,36 @@ function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
        }
     }
 
+    this.refresh = function () {
+       document.body.style.cursor = "wait";
+       var ajax=new Ajax.Request('/planetlab/slices/leases-data.php',
+                                 {method:'post',
+                                  parameters:{'sliceid':this.sliceid,
+                                              'slicename':this.slicename,
+                                              'leases_grain':this.leases_grain,
+                                              'leases_offset':this.leases_offset,
+                                              'leases_slots':this.leases_slots,
+                                              'leases_w':this.leases_w},
+                                  onSuccess: function (transport) {
+                                      var response = transport.responseText || "no response text";
+//                                    window.console.log("received from ajax=[["+response+"]]");
+                                      var scheduler=Scheduler.scheduler;
+                                      if ( ! scheduler.set_html (response)) 
+                                          alert ("Something wrong .. Could not store ajax result..");
+                                      else if ( ! scheduler.parse_html()) 
+                                          alert ("Something wrong .. Could not parse ajax result..");
+                                      else
+                                          scheduler.draw_area("leases_area");
+                                      document.body.style.cursor = "default";
+                                  },
+                                  onFailure: function(){ 
+                                      document.body.style.cursor = "default";
+                                      alert("Could not reach server, sorry...\n\n");
+                                  },
+                                 });
+                                      
+    }
+
 } // end Scheduler
 
 //////////////////////////////////////// couldn't find how to inhererit from the raphael objects...
@@ -382,46 +480,22 @@ var lease_methods = {
 function init_scheduler () {
     // Grab the data
     var data = [], axisx = [], axisy = [];
-    var table = $$("table#leases_data")[0];
-    // no reservable nodes - no data
-    if ( ! table) return;
-    // upper-left cell : sliceid & slicename & x_grain
-    var slice_attributes = getInnerText(table.getElementsBySelector("thead>tr>td")[0]).split('&');
-    var sliceid=slice_attributes[0];
-    var slicename=slice_attributes[1];
-    var x_grain=slice_attributes[2];
-    // the nodelabels
-    table.getElementsBySelector("tbody>tr>th").each(function (cell) {
-        axisy.push(getInnerText(cell));
-    });
-    // the timeslot labels
-    table.getElementsBySelector("thead>tr>th").each(function (cell) {
-       /* [0]: timestamp -- [1]: displayable*/
-        axisx.push(getInnerText(cell).split("&"));
-    });
-    // leases - expect colspan to describe length in grains
-    // the text contents is expected to be lease_id & slicename
-    table.getElementsBySelector("tbody>tr>td").each(function (cell) {
-       var cell_data;
-       slice_attributes=getInnerText(cell).split('&');
-       // booked leases come with lease id and slice name
-       if (slice_attributes.length == 2) {
-           // leases is booked : slice_id, slice_name, duration in grains
-           cell_data=new Array (slice_attributes[0], slice_attributes[1], cell.colSpan);
-       } else {
-           cell_data = new Array ('','',cell.colSpan);
-       }
-        data.push(cell_data);
-    });
-    var scheduler = new Scheduler (sliceid,slicename, x_grain, axisx, axisy, data);
-    table.hide();
-    // leases_area is a <div> created by slice.php as a placeholder
-    scheduler.init ("leases_area");
-
+    var sliceid = getInnerText($$("span#leases_sliceid")[0]).strip();
+    var slicename = getInnerText($$("span#leases_slicename")[0]).strip();
+    var w_grain = getInnerText($$("span#leases_w")[0]).strip();
+    var scheduler = new Scheduler (sliceid,slicename,w_grain);
+    // parse the table with data, and if not empty, draw the scheduler
+    if (scheduler.parse_html ()) {
+       scheduler.draw_area("leases_area");
+    }
+    
+    // attach behaviour to buttons
+    var refresh=$$("button#leases_refresh")[0];
+    if (refresh) refresh.onclick = function () { scheduler.refresh();}
     var submit=$$("button#leases_submit")[0];
     submit.onclick = function () { scheduler.submit(); }
-    var clear=$$("button#leases_clear")[0];
-    clear.onclick = function () { scheduler.clear(); }
+
+    scheduler.refresh();
 
 }
 
index 51248f9..dd71375 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 
-// $Id$
-
 // Require login
 require_once 'plc_login.php';
 
@@ -608,21 +606,32 @@ EOF;
 
   // get settings from environment, otherwise set to defaults
   // when to start, in hours in the future from now
-  $resa_offset=$_GET['resa_offset'];
-  if ( ! $resa_offset ) $resa_offset=0;
+  $leases_offset=$_GET['leases_offset'];
+  if ( ! $leases_offset ) $leases_offset=0;
   // how many timeslots to show
-  $resa_slots=$_GET['resa_slots'];
-  if ( ! $resa_slots ) $resa_slots = 36;
-  // the width in pixel for each timeslot
-  $resa_x_grain = $_GET['resa_x_grain'];
-  if ( ! $resa_x_grain) $resa_x_grain=20;
+  $leases_slots=$_GET['leases_slots'];
+  if ( ! $leases_slots ) $leases_slots = 36;
+  // offset in hours (in the future) from now 
+  $leases_w = $_GET['leases_w'];
+  if ( ! $leases_w) $leases_w=20;
+  // number of timeslots to display
 
   $grain=$api->GetLeaseGranularity();
+
+  // these elements are for passing data to the javascript layer
+  echo "<span class='hidden' id='leases_slicename'>" . $slice['name'] . "</span>";
+  echo "<span class='hidden' id='leases_sliceid'>" . $slice['slice_id']. "</span>";
+  echo "<span class='hidden' id='leases_grain'>" . $grain . "</span>";
+  echo "<span class='hidden' id='leases_offset'>" . $leases_offset . "</span>";
+  echo "<span class='hidden' id='leases_slots'>" . $leases_slots . "</span>";
+  echo "<span class='hidden' id='leases_w'>" . $leases_w . "</span>";
+
+  // cut off
   if ($profiling) plc_debug_prof('6 granul',$grain);
   // where to start from, expressed as an offset in hours from now
-  $rough_start=time()+$resa_offset*3600;
+  $rough_start=time()+$leases_offset*3600;
   // show the next 36 grains 
-  $duration=$resa_slots*$grain;
+  $duration=$leases_slots*$grain;
   $steps=$duration/$grain;
   $start=intval($rough_start/$grain)*$grain;
   $end=$rough_start+$duration;
@@ -642,9 +651,9 @@ EOF;
     $host_hash[$hostname] []= $lease;
   }
   // leases_data is the name used by leases.js to locate this table
-  echo "<table id='leases_data'>";
+  echo "<table id='leases_data' class='hidden'>";
   // pass (slice_id,slicename,x_grain) in the upper-left cell, as thead>tr>td
-  echo "<thead><tr><td>" . $slice['slice_id'] . '&' . $slice['name'] . '&' . $resa_x_grain . "</td>";
+  echo "<thead><tr><td>" . $slice['slice_id'] . '&' . $slice['name'] . '&' . $leases_w . "</td>";
   // the timeslot headers read (timestamp,label)
   $day_names=array('Su','M','Tu','W','Th','F','Sa');
   for ($i=0; $i<$steps; $i++) {
@@ -689,7 +698,7 @@ EOF;
 <div id='leases_area'></div>
 
 <div id='leases_buttons'>
-  <button id='leases_clear' type='submit'>Clear</button>
+  <button id='leases_refresh' type='submit'>Refresh</button>
   <button id='leases_submit' type='submit'>Submit</button>
 </div>
 EOF;
index d229ea8..84020f2 120000 (symlink)
@@ -1 +1 @@
-prototype-1.6.0.3.js
\ No newline at end of file
+prototype-1.7.0.0.js
\ No newline at end of file
index 0f86acb..31ab09c 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 var debug=false;
 
 /* for debugging purposes */