+/* Thierry Parmentelat -- INRIA */
+
/* need to put some place else in CSS ? */
// space for the nodenames
var y_sep = 10;
// 1-grain leases attributes
-var x_grain = 20;
+// leases_w is configurable from html
+//var leases_w = 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... */
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':'#bbf', 'stroke': '#338','stroke-width':2,
+var attr_timebutton = {'fill':'#bbf', 'stroke': '#338','stroke-width':1,
'stroke-linecap':'round', 'stroke-linejoin':'miter', 'stroke-miterlimit':3};
-// keep consistent with sizes above - need for something nicer
-var timebutton_path = "M1,0L19,0L10,8L1,0";
+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 */
////////////////////////////////////////////////////////////
// the scheduler object
-function Scheduler (sliceid, slicename, axisx, axisy, data) {
+function Scheduler () {
+
+ // 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;
-
- // utilities to keep track of all the leases
- this.leases=[];
- this.append_lease = function (lease) {
- this.leases.push(lease);
+ this.paper=null;
+
+ ////////////////////
+ // 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;
+ }
+
+ ////////////////////
+ // the names of the hidden fields that hold the input to this class
+ // are hard-wired for now
+ this.parse_html = function () {
+
+ var table = $$("table#leases_data")[0];
+ // no reservable nodes - no data
+ if ( ! table) return false;
+
+ // the nodelabels
+ var data = [], axisx = [], axisy = [];
+ table.getElementsBySelector("tbody>tr>th").each(function (cell) {
+ axisy.push(getInnerText(cell));
+ });
+ // test for at least one entry; other wise this means we haven't retrieved
+ // the html dta yet
+ if (axisy.length <1) return false;
+
+ // 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;
}
// how many time slots
- this.nb_grains = function () { return axisx.length;}
+ this.nb_grains = function () { return this.axisx.length;}
- this.init = function (canvas_id) {
- this.total_width = x_nodelabel + this.nb_grains()*x_grain;
+ ////////////////////
+ // draw
+ this.draw_area = function (canvas_id) {
+ this.total_width = x_nodelabel + this.nb_grains()*this.leases_w;
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;
+
+ // the path for the triangle-shaped buttons
+ this.timebutton_path="M1,0L"+(this.leases_w-1)+",0L"+(this.leases_w/2)+","+y_header+"L1,0";
+ var axisx=this.axisx;
+
+ var axisy=this.axisy;
// maintain the list of nodelabels for the 'all nodes' button
this.nodelabels=[];
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;
// 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.leases_w);
}
+ ////////// 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;
allnodes.click(allnodes_methods.click);
// timeslot buttons
for (var i=0, len=axisx.length; i < len; ++i) {
- var timebutton=paper.path(timebutton_path).attr({'translation':left+','+top}).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.leases_w);
}
//////// 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"});
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,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.leases_w*duration,y_node,radius);
+ // record scheduler in lease - early as we need this in init_other
+ lease.scheduler=this;
lease.lease_id=lease_id;
lease.nodename=nodename;
lease.nodelabel=nodelabel;
lease.initial="mine";
lease_methods.init_mine(lease);
} else {
- lease_initial="other";
+ lease.initial="other";
lease_methods.init_other(lease,slicename);
}
lease.from_time = axisx[grain%this.nb_grains()][0];
grain += duration;
lease.until_time = axisx[grain%this.nb_grains()][0];
- // record scheduler in lease
- lease.scheduler=this;
// and vice versa
- this.append_lease(lease);
+ this.leases.push(lease);
// move on with the loop
- left += x_grain*duration;
+ left += this.leases_w*duration;
data_index +=1;
}
top += y_node + y_sep;
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='+lease.initial+' current='+lease.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',
}
}
}
- 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';
- }
+ slice_id=this.slice_id;
// Ajax.Request comes with prototype
var ajax=new Ajax.Request('/planetlab/common/actions.php',
{method:'post',
parameters:{'action':'manage-leases',
- 'actions':actions.toJSON()},
+ 'actions':Object.toJSON(actions)},
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();
},
});
}
}
}
+ // re-read settings from the html, ajax-aquire the html text for the leases_data table
+ // store it in the html tree, parse it, and refresh graphic
+ this.refresh = function () {
+ this.slice_id=getInnerText($$("span#leases_slice_id")[0]).strip();
+ this.slicename=getInnerText($$("span#leases_slicename")[0]).strip();
+ this.leases_granularity=getInnerText($$("span#leases_granularity")[0]).strip();
+ this.leases_offset=$("leases_offset_input").value.strip();
+ this.leases_slots=$("leases_slots_input").value.strip();
+ this.leases_w=parseInt($("leases_w_input").value.strip());
+
+ document.body.style.cursor = "wait";
+ var ajax=new Ajax.Request('/planetlab/slices/leases-data.php',
+ {method:'post',
+ parameters:{'slice_id':this.slice_id,
+ 'slicename':this.slicename,
+ 'leases_granularity':this.leases_granularity,
+ '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 ("Error: could not parse ajax result..\nIf your session has expired, you need to log back in");
+ 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...
init_other: function (lease, slicename) {
lease.animate (attr_lease_other,anim_delay);
/* 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_otherslice);
+ var otherslicelabel = lease.scheduler.paper.text (lease.attr("x")+lease.attr("width")/2,
+ // xxx
+ lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_otherslice);
/* hide it right away */
otherslicelabel.hide();
/* record it */
function init_scheduler () {
// Grab the data
- var data = [], axisx = [], axisy = [];
- var table = $$("table#leases_data")[0];
- // no reservable nodes - no data
- if ( ! table) return;
- // 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);
- });
- // sliceid & slicename : the upper-left cell
- var slice_attributes = getInnerText(table.getElementsBySelector("thead>tr>td")[0]).split('&');
- var sliceid=slice_attributes[0];
- var slicename=slice_attributes[1];
- var scheduler = new Scheduler (sliceid,slicename, axisx, axisy, data);
- table.hide();
- // leases_area is a <div> created by slice.php as a placeholder
- scheduler.init ("leases_area");
-
- var submit=$$("button#leases_submit")[0];
- submit.onclick = function () { scheduler.submit(); }
- var clear=$$("button#leases_clear")[0];
- clear.onclick = function () { scheduler.clear(); }
+ var scheduler = new Scheduler ();
+ // parse the table with data, and if not empty, draw the scheduler
+ scheduler.refresh();
+
+ // attach behaviour to buttons
+ $("leases_submit").onclick = function () { scheduler.submit(); }
+ $("leases_refresh").onclick = function () { scheduler.refresh();}
}