reservation : timeslot width in pixels can be set with $_GET['resa_x_grain']
[plewww.git] / planetlab / slices / leases.js
index a1eea8d..67019a6 100644 (file)
@@ -10,23 +10,31 @@ var y_header = 12;
 var y_sep = 10;
 
 // 1-grain leases attributes
-var x_grain = 24;
+// x_grain is configurable from $_GET
+//var x_grain = 20;
 var y_node = 15;
 var radius= 6;
 
-var anim_delay=500;
+var anim_delay=350;
 
 /* decorations / headers */
+/* note: looks like the 'font' attr is not effective... */
 
 // vertical rules
 var attr_rules={'fill':"#888", 'stroke-dasharray':'- ', 'stroke-width':0.5};
-var txt_timelabel = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
+// set font-size separately in here rather than depend on the height
+var txt_timelabel = {"font": 'Times, "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', 
+                    stroke: "none", fill: "#008", 'font-size': 9};
 var txt_allnodes = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#404"};
 var txt_nodelabel = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
 
-var attr_timebutton = {'fill': '#888','stroke-width':2};
+var attr_timebutton = {'fill':'#bbf', 'stroke': '#338','stroke-width':1, 
+                      'stroke-linecap':'round', 'stroke-linejoin':'miter', 'stroke-miterlimit':3};
+var attr_daymarker = {'stroke':'#000','stroke-width':2};
+var attr_half_daymarker = {'stroke':'#444','stroke-width':2};
 
 /* lease dimensions and colors */
+/* refrain from using gradient color, seems to not be animated properly */
 /* lease was originally free and is still free */
 var attr_lease_free_free={'fill':"#def", 'stroke-width':0.5, 'stroke-dasharray':''};
 /* lease was originally free and is now set for our usage */
@@ -35,24 +43,27 @@ var attr_lease_free_mine={'fill':"green", 'stroke-width':1, 'stroke-dasharray':'
 var attr_lease_mine_mine={'fill':"#beb", 'stroke-width':0.5, 'stroke-dasharray':''};
 /* was mine and is about to be released */
 var attr_lease_mine_free={'fill':"white", 'stroke-width':1, 'stroke-dasharray':'-..'};
-// refrained from using gradient color, was not animated properly
-// var color_lease_mine_free="0-#fff-#def:50-#fff";
 var attr_lease_other={'fill':"#f88"};
 
 /* other slices name */
-var txt_slice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#444",
-                "font-size": 15 };
+var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#444",
+                     "font-size": 12 };
 
 ////////////////////////////////////////////////////////////
 // the scheduler object
-function Scheduler (slicename, axisx, axisy, data) {
+function Scheduler (sliceid, slicename, x_grain, axisx, axisy, data) {
 
-    // the data only contain slice names, we need this to find our own leases (mine)
+    // 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.x_grain = parseInt(x_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";
+
     // utilities to keep track of all the leases
     this.leases=[];
     this.append_lease = function (lease) { 
@@ -63,7 +74,7 @@ function Scheduler (slicename, axisx, axisy, data) {
     this.nb_grains = function () { return axisx.length;}
 
     this.init = function (canvas_id) {
-       this.total_width = x_nodelabel + this.nb_grains()*x_grain; 
+       this.total_width = x_nodelabel + this.nb_grains()*this.x_grain; 
        this.total_height =   2*y_header /* the timelabels */
                            + 2*y_sep    /* extra space */
                            + y_node     /* all-nodes & timebuttons row */ 
@@ -77,25 +88,38 @@ function Scheduler (slicename, axisx, axisy, data) {
        var top=0;
        var left=x_nodelabel;
 
+       var daymarker_height= 2*y_header+2*y_sep + (axisy.length+1)*(y_node+y_sep);
+       var daymarker_path="M0,0L0," + daymarker_height;
+
+       var half_daymarker_off= 2*y_header+y_sep;
+       var half_daymarker_path="M0," + half_daymarker_off + "L0," + daymarker_height;
+
        var col=0;
        for (var i=0, len=axisx.length; i < len; ++i) {
            // pick the printable part
-           timelabel=axisx[i][1];
+           var timelabel=axisx[i][1];
            var y = top+y_header;
            if (col%2 == 0) y += y_header;
            col +=1;
            // display time label
            var timelabel=paper.text(left,y,timelabel).attr(txt_timelabel)
-               .attr({"font-size":y_header, "text-anchor":"middle"});
+               .attr({"text-anchor":"middle"});
            // draw vertical line
            var path_spec="M"+left+" "+(y+y_header/2)+"L"+left+" "+this.total_height;
            var rule=paper.path(path_spec).attr(attr_rules);
-           left+=x_grain;
+           // show a day marker when relevant
+           var timestamp=parseInt(axisx[i][0]);
+           if ( (timestamp%(24*3600))==0) {
+               paper.path(daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
+           } else if ( (timestamp%(12*3600))==0) {
+               paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
+           }
+           left+=(this.x_grain);
        }
 
+       ////////// the row with the timeslot buttons (the one labeled 'All nodes')
        this.granularity=axisx[1][0]-axisx[0][0];
 
-       ////////// the row with the timeslot buttons
        // move two lines down
        top += 2*y_header+2*y_sep;
        left=x_nodelabel;
@@ -106,14 +130,14 @@ function Scheduler (slicename, axisx, axisy, data) {
        allnodes.click(allnodes_methods.click);
        // timeslot buttons
        for (var i=0, len=axisx.length; i < len; ++i) {
-           var timebutton = paper.rect(left,top,x_grain,y_node,radius).attr(attr_timebutton);
+           var timebutton=paper.path(this.timebutton_path).attr({'translation':left+','+top}).attr(attr_timebutton);
            timebutton.from_time=axisx[i][0];
            timebutton.scheduler=this;
            timebutton.click(timebutton_methods.click);
-           left+=x_grain;
+           left+=(this.x_grain);
        }
        
-       //////// the body of the scheduler: nodes
+       //////// the body of the scheduler : loop on nodes
        top += y_node+y_sep;
        var data_index=0;
        for (var i=0, len=axisy.length; i<len; ++i) {
@@ -128,9 +152,11 @@ function Scheduler (slicename, axisx, axisy, data) {
            left += x_nodelabel;
            var grain=0;
            while (grain < this.nb_grains()) {
-               slicename=data[data_index][0];
-               duration=data[data_index][1];
-               var lease=paper.rect (left,top,x_grain*duration,y_node,radius);
+               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.lease_id=lease_id;
                lease.nodename=nodename;
                lease.nodelabel=nodelabel;
                if (slicename == "") {
@@ -151,7 +177,7 @@ function Scheduler (slicename, axisx, axisy, data) {
                // and vice versa
                this.append_lease(lease);
                // move on with the loop
-               left += x_grain*duration;
+               left += this.x_grain*duration;
                data_index +=1;
            }
            top += y_node + y_sep;
@@ -159,6 +185,8 @@ function Scheduler (slicename, axisx, axisy, data) {
     }
 
     this.submit = function () {
+       document.body.style.cursor = "wait";
+       var actions=new Array();
        for (var i=0, len=this.leases.length; i<len; ++i) {
            var lease=this.leases[i];
            if (lease.current != lease.initial) {
@@ -167,18 +195,45 @@ function Scheduler (slicename, axisx, axisy, data) {
                /* 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);
+//                 window.console.log('merged index='+j);
                    until_time=this.leases[j].until_time;
                    ++j; ++i;
                }
-               var method=(lease.current=='free') ? 'DeleteLeases' : 'AddLeases';
-               window.console.log(method + "(" + 
-                                  "[" + lease.nodename + "]," + 
-                                  this.slicename + "," +
-                                  from_time + "," +
-                                  until_time + ')');
+               if (lease.current!='free') { // lease to add
+                   actions.push(new Array('add-leases',
+                                          new Array(lease.nodename),
+                                          this.slicename,
+                                          from_time,
+                                          until_time));
+               } else { // lease to delete
+                   actions.push(new Array ('delete-leases',
+                                           lease.lease_id));
+               }
            }
        }
+       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',
+                                  parameters:{'action':'manage-leases',
+                                              'actions':actions.toJSON()},
+                                  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);
+                                  },
+                                  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);
+                                  },
+                                 });
     }
 
     this.clear = function () {
@@ -314,7 +369,7 @@ var lease_methods = {
        /* a text obj to display the name of the slice that owns that lease */
        var otherslicelabel = paper.text (lease.attr("x")+lease.attr("width")/2,
                                          // xxx
-                                         lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_slice);
+                                         lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_otherslice);
        /* hide it right away */
        otherslicelabel.hide();
        /* record it */
@@ -330,6 +385,11 @@ function init_scheduler () {
     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));
@@ -340,11 +400,20 @@ function init_scheduler () {
         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) {
-        data.push(new Array (getInnerText(cell),cell.colSpan));
+       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);
     });
-    // slicename : the upper-left cell
-    var scheduler = new Scheduler (getInnerText(table.getElementsBySelector("thead>tr>td")[0]), axisx, axisy, 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");