1 /* need to put some place else in CSS ? */
3 // space for the nodenames
5 // right space after the nodename - removed from the above
7 // height for the (two) rows of timelabels
12 // 1-grain leases attributes
13 // leases_w is configurable from html
20 /* decorations / headers */
21 /* note: looks like the 'font' attr is not effective... */
24 var attr_rules={'fill':"#888", 'stroke-dasharray':'- ', 'stroke-width':0.5};
25 // set font-size separately in here rather than depend on the height
26 var txt_timelabel = {"font": 'Times, "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif',
27 stroke: "none", fill: "#008", 'font-size': 9};
28 var txt_allnodes = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#404"};
29 var txt_nodelabel = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
31 var attr_timebutton = {'fill':'#bbf', 'stroke': '#338','stroke-width':1,
32 'stroke-linecap':'round', 'stroke-linejoin':'miter', 'stroke-miterlimit':3};
33 var attr_daymarker = {'stroke':'#000','stroke-width':2};
34 var attr_half_daymarker = {'stroke':'#444','stroke-width':2};
36 /* lease dimensions and colors */
37 /* refrain from using gradient color, seems to not be animated properly */
38 /* lease was originally free and is still free */
39 var attr_lease_free_free={'fill':"#def", 'stroke-width':0.5, 'stroke-dasharray':''};
40 /* lease was originally free and is now set for our usage */
41 var attr_lease_free_mine={'fill':"green", 'stroke-width':1, 'stroke-dasharray':'-..'};
42 /* was mine and is still mine */
43 var attr_lease_mine_mine={'fill':"#beb", 'stroke-width':0.5, 'stroke-dasharray':''};
44 /* was mine and is about to be released */
45 var attr_lease_mine_free={'fill':"white", 'stroke-width':1, 'stroke-dasharray':'-..'};
46 var attr_lease_other={'fill':"#f88"};
48 /* other slices name */
49 var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#444",
52 ////////////////////////////////////////////////////////////
53 // the scheduler object
54 function Scheduler () {
56 // xxx-hacky dunno how to retrieve this object from an ajax callback
57 Scheduler.scheduler=this;
59 // the data contains slice names, and lease_id, we need this to find our own leases (mine)
63 // store the result of an ajax request in the leases_data table
64 this.set_html = function (html_data) {
65 var table_text = $$("table#leases_data")[0].innerHTML;
66 $$("table#leases_data")[0].innerHTML=html_data;
67 table_text = $$("table#leases_data")[0].innerHTML;
72 // the names of the hidden fields that hold the input to this class
73 // are hard-wired for now
74 this.parse_html = function () {
75 window.console.log('in parse_html');
77 var table = $$("table#leases_data")[0];
78 // no reservable nodes - no data
80 window.console.log('no table');
84 var data = [], axisx = [], axisy = [];
85 table.getElementsBySelector("tbody>tr>th").each(function (cell) {
86 axisy.push(getInnerText(cell));
88 // test for at least one entry; other wise this means we haven't retrieved
90 if (axisy.length <1) {
91 window.console.log('no axisy');
92 window.console.log('have retrieved html text as'+getInnerText(table));
96 // the timeslot labels
97 table.getElementsBySelector("thead>tr>th").each(function (cell) {
98 /* [0]: timestamp -- [1]: displayable*/
99 axisx.push(getInnerText(cell).split("&"));
102 // leases - expect colspan to describe length in grains
103 // the text contents is expected to be lease_id & slicename
104 table.getElementsBySelector("tbody>tr>td").each(function (cell) {
106 var slice_attributes=getInnerText(cell).split('&');
107 // booked leases come with lease id and slice name
108 if (slice_attributes.length == 2) {
109 // leases is booked : slice_id, slice_name, duration in grains
110 cell_data=new Array (slice_attributes[0], slice_attributes[1], cell.colSpan);
112 cell_data = new Array ('','',cell.colSpan);
114 data.push(cell_data);
118 window.console.log('in parse_html, set axisx to'+axisx.length);
124 // how many time slots
125 this.nb_grains = function () { return this.axisx.length;}
129 this.draw_area = function (canvas_id) {
130 window.console.log('in draw_area');
131 this.total_width = x_nodelabel + this.nb_grains()*this.leases_w;
132 this.total_height = 2*y_header /* the timelabels */
133 + 2*y_sep /* extra space */
134 + y_node /* all-nodes & timebuttons row */
135 + (this.axisy.length)*(y_node+y_sep); /* the regular nodes and preceding space */
136 // reuse for paper if exists with same size, or (re-)create otherwise
138 if (this.paper == null) {
139 paper = Raphael (canvas_id, this.total_width+x_sep, this.total_height);
140 } else if (this.paper.width==this.total_width && this.paper.height==this.total_height) {
144 $$("#"+canvas_id)[0].innerHTML="";
145 paper = Raphael (canvas_id, this.total_width+x_sep, this.total_height);
149 // the path for the triangle-shaped buttons
150 this.timebutton_path="M1,0L"+(this.leases_w-1)+",0L"+(this.leases_w/2)+","+y_header+"L1,0";
152 var axisx=this.axisx;
153 window.console.log('in draw_area, retrieved axisx' + axisx.length);
155 var axisy=this.axisy;
156 // maintain the list of nodelabels for the 'all nodes' button
159 ////////// create the time slots legend
161 var left=x_nodelabel;
163 var daymarker_height= 2*y_header+2*y_sep + (axisy.length+1)*(y_node+y_sep);
164 var daymarker_path="M0,0L0," + daymarker_height;
166 var half_daymarker_off= 2*y_header+y_sep;
167 var half_daymarker_path="M0," + half_daymarker_off + "L0," + daymarker_height;
170 for (var i=0, len=axisx.length; i < len; ++i) {
171 // pick the printable part
172 var timelabel=axisx[i][1];
173 var y = top+y_header;
174 if (col%2 == 0) y += y_header;
176 // display time label
177 var timelabel=paper.text(left,y,timelabel).attr(txt_timelabel)
178 .attr({"text-anchor":"middle"});
179 // draw vertical line
180 var path_spec="M"+left+" "+(y+y_header/2)+"L"+left+" "+this.total_height;
181 var rule=paper.path(path_spec).attr(attr_rules);
182 // show a day marker when relevant
183 var timestamp=parseInt(axisx[i][0]);
184 if ( (timestamp%(24*3600))==0) {
185 paper.path(daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
186 } else if ( (timestamp%(12*3600))==0) {
187 paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
189 left+=(this.leases_w);
192 ////////// the row with the timeslot buttons (the one labeled 'All nodes')
193 this.granularity=axisx[1][0]-axisx[0][0];
195 // move two lines down
196 top += 2*y_header+2*y_sep;
199 var allnodes = paper.text (x_nodelabel-x_sep,top+y_node/2,"All nodes").attr(txt_allnodes)
200 .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
201 allnodes.scheduler=this;
202 allnodes.click(allnodes_methods.click);
204 for (var i=0, len=axisx.length; i < len; ++i) {
205 var timebutton=paper.path(this.timebutton_path).attr({'translation':left+','+top}).attr(attr_timebutton);
206 timebutton.from_time=axisx[i][0];
207 timebutton.scheduler=this;
208 timebutton.click(timebutton_methods.click);
209 left+=(this.leases_w);
212 //////// the body of the scheduler : loop on nodes
216 for (var i=0, len=axisy.length; i<len; ++i) {
217 var nodename=axisy[i];
219 var nodelabel = paper.text(x_nodelabel-x_sep,top+y_node/2,nodename).attr(txt_nodelabel)
220 .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
221 nodelabel_methods.selected(nodelabel,1);
222 nodelabel.click(nodelabel_methods.click);
223 this.nodelabels.push(nodelabel);
227 while (grain < this.nb_grains()) {
228 lease_id=this.data[data_index][0];
229 slicename=this.data[data_index][1];
230 duration=this.data[data_index][2];
231 var lease=paper.rect (left,top,this.leases_w*duration,y_node,radius);
232 lease.lease_id=lease_id;
233 lease.nodename=nodename;
234 lease.nodelabel=nodelabel;
235 if (slicename == "") {
236 lease.initial="free";
237 lease_methods.init_free(lease);
238 } else if (slicename == this.slicename) {
239 lease.initial="mine";
240 lease_methods.init_mine(lease);
242 lease_initial="other";
243 lease_methods.init_other(lease,slicename);
245 lease.from_time = axisx[grain%this.nb_grains()][0];
247 lease.until_time = axisx[grain%this.nb_grains()][0];
248 // record scheduler in lease
249 lease.scheduler=this;
251 this.leases.push(lease);
252 // move on with the loop
253 left += this.leases_w*duration;
256 top += y_node + y_sep;
260 this.submit = function () {
261 document.body.style.cursor = "wait";
262 var actions=new Array();
263 for (var i=0, len=this.leases.length; i<len; ++i) {
264 var lease=this.leases[i];
265 if (lease.current != lease.initial) {
266 var from_time=lease.from_time;
267 var until_time=lease.until_time;
268 /* scan the leases just after this one and merge if appropriate */
269 /* this makes sense when adding leases only though */
270 if (lease.current=='mine') {
272 while (j<len && lease_methods.compare (lease, until_time, this.leases[j])) {
273 // window.console.log('merging index='+i+' initial='+this.leases[i].initial+' current='+this.leases[i].current);
274 // window.console.log('merged index='+j+' initial='+this.leases[j].initial+' current='+this.leases[j].current);
275 until_time=this.leases[j].until_time;
279 if (lease.current!='free') { // lease to add
280 actions.push(new Array('add-leases',
281 new Array(lease.nodename),
285 } else { // lease to delete
286 actions.push(new Array ('delete-leases',
291 slice_id=this.slice_id;
292 // Ajax.Request comes with prototype
293 var ajax=new Ajax.Request('/planetlab/common/actions.php',
295 parameters:{'action':'manage-leases',
296 'actions':actions.toJSON()},
297 onSuccess: function(transport) {
298 var response = transport.responseText || "no response text";
299 document.body.style.cursor = "default";
300 // alert("Server answered:\n\n" + response + "\n\nPress OK to refresh page");
301 Scheduler.scheduler.refresh();
303 onFailure: function(){
304 document.body.style.cursor = "default";
305 alert("Could not reach server, sorry...\n\nPress OK to refresh page");
306 // not too sure what to do here ...
307 Scheduler.scheduler.refresh();
312 this.clear = function () {
313 for (var i=0, len=this.leases.length; i<len; ++i) {
314 var lease=this.leases[i];
315 if (lease.current != lease.initial) {
316 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
317 else lease_methods.init_mine(lease,lease_methods.click_free);
322 // re-read settings from the html, ajax-aquire the html text for the leases_data table
323 // store it in the html tree, parse it, and refresh graphic
324 this.refresh = function () {
325 window.console.log('in refresh');
326 this.slice_id=getInnerText($$("span#leases_slice_id")[0]).strip();
327 this.slicename=getInnerText($$("span#leases_slicename")[0]).strip();
328 this.leases_granularity=getInnerText($$("span#leases_granularity")[0]).strip();
329 this.leases_offset=getInnerText($$("span#leases_offset")[0]).strip();
330 this.leases_slots=getInnerText($$("span#leases_slots")[0]).strip();
331 this.leases_w=parseInt(getInnerText($$("span#leases_w")[0]).strip());
333 document.body.style.cursor = "wait";
334 var ajax=new Ajax.Request('/planetlab/slices/leases-data.php',
336 parameters:{'slice_id':this.slice_id,
337 'slicename':this.slicename,
338 'leases_granularity':this.leases_granularity,
339 'leases_offset':this.leases_offset,
340 'leases_slots':this.leases_slots,
341 'leases_w':this.leases_w},
342 onSuccess: function (transport) {
343 var response = transport.responseText || "no response text";
344 window.console.log("received from ajax=[["+response+"]]");
345 var scheduler=Scheduler.scheduler;
346 if ( ! scheduler.set_html (response))
347 alert ("Something wrong .. Could not store ajax result..");
348 else if ( ! scheduler.parse_html())
349 alert ("Something wrong .. Could not parse ajax result..");
351 scheduler.draw_area("leases_area");
352 document.body.style.cursor = "default";
354 onFailure: function(){
355 document.body.style.cursor = "default";
356 alert("Could not reach server, sorry...\n\n");
364 //////////////////////////////////////// couldn't find how to inhererit from the raphael objects...
366 //////////////////// the 'all nodes' button
367 var allnodes_methods = {
368 click: function (event) {
369 var scheduler=this.scheduler;
370 /* decide what to do */
372 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
373 if (! scheduler.nodelabels[i].selected) unselected++;
374 /* if at least one is not selected : select all */
375 var new_state = (unselected >0) ? 1 : 0;
376 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
377 nodelabel_methods.selected(scheduler.nodelabels[i],new_state);
381 //////////////////// the buttons for managing the whole timeslot
382 var timebutton_methods = {
385 click: function (event) {
386 var scheduler = this.scheduler;
387 var from_time = this.from_time;
388 var until_time = from_time + scheduler.granularity;
389 /* scan leases on selected nodes, store in two arrays */
390 var relevant_free=[], relevant_mine=[];
391 for (var i=0,len=scheduler.leases.length; i<len; ++i) {
392 var scan=scheduler.leases[i];
393 if ( ! scan.nodelabel.selected) continue;
395 if (scan.from_time<=from_time && scan.until_time>=until_time) {
396 if (scan.current == "free") relevant_free.push(scan);
397 else if (scan.current == "mine") relevant_mine.push(scan);
400 // window.console.log("Found " + relevant_free.length + " free and " + relevant_mine.length + " mine");
401 /* decide what to do, whether book or release */
402 if (relevant_mine.length==0 && relevant_free.length==0) {
403 alert ("Nothing to do in this timeslot on the selected nodes");
406 // if at least one is free, let's book
407 if (relevant_free.length > 0) {
408 for (var i=0, len=relevant_free.length; i<len; ++i) {
409 var lease=relevant_free[i];
410 lease_methods.init_mine(lease,lease_methods.click_free);
412 // otherwise we unselect
414 for (var i=0, len=relevant_mine.length; i<len; ++i) {
415 var lease=relevant_mine[i];
416 lease_methods.init_free(lease,lease_methods.click_mine);
422 //////////////////// the nodelabel buttons
423 var nodelabel_methods = {
425 // set selected mode and render visually
426 selected: function (nodelabel, flag) {
427 nodelabel.selected=flag;
428 nodelabel.attr({'font-weight': (flag ? 'bold' : 'normal')});
432 click: function (event) {
433 nodelabel_methods.selected( this, ! this.selected );
438 //////////////////// the lease buttons
439 var lease_methods = {
441 /* in the process of merging leases before posting to the API */
442 compare: function (lease, until_time, next_lease) {
443 return (next_lease['nodename'] == lease['nodename'] &&
444 next_lease['from_time'] == until_time &&
445 next_lease['initial'] == lease['initial'] &&
446 next_lease['current'] == lease['current']);
449 init_free: function (lease, unclick) {
450 lease.current="free";
452 lease.animate((lease.initial=="free") ? attr_lease_free_free : attr_lease_mine_free,anim_delay);
453 // keep track of the current status
455 lease.click (lease_methods.click_free);
456 if (unclick) lease.unclick(unclick);
459 // find out all the currently free leases that overlap this one
460 click_free: function (event) {
461 var scheduler = this.scheduler;
462 lease_methods.init_mine(this,lease_methods.click_free);
465 init_mine: function (lease, unclick) {
466 lease.current="mine";
467 lease.animate((lease.initial=="mine") ? attr_lease_mine_mine : attr_lease_free_mine,anim_delay);
468 lease.click (lease_methods.click_mine);
469 if (unclick) lease.unclick(unclick);
472 click_mine: function (event) {
473 var scheduler = this.scheduler;
474 // this lease was originally free but is now marked for booking
475 // we free just this lease
476 lease_methods.init_free(this, lease_methods.click_mine);
480 init_other: function (lease, slicename) {
481 lease.animate (attr_lease_other,anim_delay);
482 /* a text obj to display the name of the slice that owns that lease */
483 var otherslicelabel = paper.text (lease.attr("x")+lease.attr("width")/2,
485 lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_otherslice);
486 /* hide it right away */
487 otherslicelabel.hide();
489 lease.label=otherslicelabel;
490 lease.hover ( function (e) { this.label.toFront(); this.label.show(); },
491 function (e) { this.label.hide(); } );
495 function init_scheduler () {
497 var scheduler = new Scheduler ();
498 // parse the table with data, and if not empty, draw the scheduler
499 window.console.log('in init_scheduler');
502 // attach behaviour to buttons
503 var refresh=$$("button#leases_refresh")[0];
504 if (refresh) refresh.onclick = function () { scheduler.refresh();}
505 var submit=$$("button#leases_submit")[0];
506 submit.onclick = function () { scheduler.submit(); }
510 Event.observe(window, 'load', init_scheduler);