2 * Description: display a query result in a Google map
3 * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
7 /* based on the leases.js code in PLE WWW interface for MyPLC by Thierry Parmentelat -- INRIA */
9 /* XXX Those declarations should not be part of the js file... */
11 /* decorations / headers */
12 /* note: looks like the 'font' attr is not effective... */
15 var attr_rules={'fill':"#888", 'stroke-dasharray':'- ', 'stroke-width':0.5};
16 // set font-size separately in here rather than depend on the height
17 var txt_timelabel = {"font": 'Times, "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif',
18 stroke: "none", fill: "#008", 'font-size': 9};
19 var txt_allnodes = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#404"};
20 var txt_nodelabel = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
22 var attr_timebutton = {'fill':'#bbf', 'stroke': '#338','stroke-width':1,
23 'stroke-linecap':'round', 'stroke-linejoin':'miter', 'stroke-miterlimit':3};
24 var attr_daymarker = {'stroke':'#000','stroke-width':2};
25 var attr_half_daymarker = {'stroke':'#444','stroke-width':2};
27 /* lease dimensions and colors */
28 /* refrain from using gradient color, seems to not be animated properly */
29 /* lease was originally free and is still free */
30 var attr_lease_free_free={'fill':"#def", 'stroke-width':0.5, 'stroke-dasharray':''};
31 /* lease was originally free and is now set for our usage */
32 var attr_lease_free_mine={'fill':"green", 'stroke-width':1, 'stroke-dasharray':'-..'};
33 /* was mine and is still mine */
34 var attr_lease_mine_mine={'fill':"#beb", 'stroke-width':0.5, 'stroke-dasharray':''};
35 /* was mine and is about to be released */
36 var attr_lease_mine_free={'fill':"white", 'stroke-width':1, 'stroke-dasharray':'-..'};
37 var attr_lease_other={'fill':"#f88"};
39 /* other slices name */
40 var txt_otherslice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#444", "font-size": "12px"};
44 var Scheduler = Plugin.extend({
46 init: function(options, element)
48 this._super(options, element);
50 /* Member variables */
51 this.options = options;
52 this.canvas_id = 'leases_area-' + options.plugin_uuid;
54 this.query_uuid = options.query_uuid;
57 //this.current_resources = Array();
58 //this.current_leases = Array();
60 this.myLeases = Array();
61 this.allLeases = Array();
63 this.listLeases = Array();
68 this.default_granularity = 1800; /* 30 min */
69 this.initial_timestamp = null;
71 /* This should be updated to be the ppcm of all granularities */
72 this.min_granularity = this.default_granularity;
74 // the data contains slice names, and lease_id, we need this to find our own leases (mine)
81 /* Listening to queries */
82 this.listen_query(options.query_uuid);
83 this.listen_query(options.query_lease_uuid);
85 /* XXX GUI setup and event binding */
86 jQuery("#datepicker").datetimepicker({
87 //Timezone wise selection option
88 //timeFormat: 'hh:mm tt z',
91 onSelect: function(value) {
93 //converting time to timestamp
94 currentDate= Math.round(Date.parse(value)/ 1000);
96 // convert value to timestamp --done
97 // Get jQuery("#timepicker").val(); - no need anymore
98 // convert timepicker into seconds - no need
99 // Add time to date - done
100 // pass the result to init_axisx - done
101 //console.log(currentDate);
103 s.init_axisx(currentDate);
105 // Do we need to populate the timeslots with existing leases?
106 // Look how to populate with initial_leases [we have to show the leases]
110 //console.log(s.nodelabels[1]);
111 // Do we need to populate the timeslots with existing leases?
112 // Look how to populate with initial_leases
114 // TODO -- DONE Note: autocomplete is not search box
115 // Implement a filtering functionality, on the name of the node
116 // http://jqueryui.com/autocomplete/
117 // pass s.nodelabels to autocomplete function
118 //jQuery("#search").autocomplete('s.nodelabel');
119 //console.log(s.nodelabel);
123 * During init, there are no resources
124 * So the list of nodes is empty
125 * The function triggered by the subscription to the resources query
126 * var RESULTS_RESOURCES = '/results/' + options.resource_query_uuid + '/changed';
127 * $.subscribe(RESULTS_RESOURCES, function(e, resources) { s.set_resources(resources); });
128 * Will have to update the list of nodes available through autocomplete (availableTags)
130 * Be inspired by QueryEditor plugin
132 * Resources informations in s.axisy ???
134 * Filter what has been selected in other plugins:
139 * Implement an action of filtering while typing
140 * filter what correspond to the user choice
144 var availableTags = ['omf','nitos','ple', s.nodelabel];
145 $( "#search" ).autocomplete({
146 source: availableTags
155 /* Default settings */
160 leases_granularity: 3600,
162 x_nodelabel: 200, /* space for the nodenames */
163 x_sep: 20, /* right space after the nodename - removed from the above */
164 y_header: 12, /* height for the (two) rows of timelabels */
165 y_sep: 10, /* space between nodes */
166 leases_w: 20, /* 1-grain leases attributes */
171 resource_query_uuid: null, /* resources */
172 lease_query_uuid: null, /* leases */
179 /* GUI MANIPULATION */
185 set_resources: function(resources)
187 //console.log(resources);
188 var scheduler = this;
189 jQuery.each(resources, function(i, resource) {
190 // ... add reservable ones to the x axis
191 if ((typeof resource.exclusive != 'undefined') && (resource.exclusive)) {
192 scheduler.axisy.push(Array(resource.urn, resource.resource_hrn, resource.type));
194 // ... if we do not have information about slivers (first update), update it
195 if (typeof resource.sliver != 'undefined') {
200 this.draw(this.canvas_id);
203 this.set_leases = function(leases) {
204 this.initial_leases=leases;
205 this.draw(this.canvas_id);
208 this.update_resources = function(resources) {
212 this.update_leases = function(leases) {
216 /* RECORD HANDLERS */
218 /* INTERNAL FUNCTIONS */
221 * @brief Return the number of time slots
223 nb_grains: function ()
225 return this.axisx.length;
229 * @brief Returns whether there is a pending lease at this timestamp
231 find_lease: function(urn, timestamp)
233 var scheduler = this;
236 $.each(Array(scheduler.myLeases, scheduler.allLeases), function(i, array) {
237 $.each(array, function(i, lease) {
238 if (lease[0] == urn) {
239 if ((timestamp >= lease[1]) && (timestamp < (lease[1] + lease[2] * 1800))) {
257 var canvas_id = this.canvas_id;
258 var o = this.options;
259 var total_width = o.x_nodelabel + this.nb_grains() * this.options.leases_w;
260 var total_height = 2 * o.y_header /* the timelabels */
261 + 2 * o.y_sep /* extra space */
262 + o.y_node /* all-nodes & timebuttons row */
263 + (this.axisy.length)*(o.y_node+o.y_sep); /* the regular nodes and preceding space */
265 /* reuse for paper if exists with same size, or (re-)create otherwise */
267 if (this.paper == null) {
268 paper = Raphael (canvas_id, total_width+o.x_sep, total_height);
269 } else if (this.paper.width==total_width && this.paper.height==total_height) {
273 $$("#"+canvas_id)[0].innerHTML="";
274 paper = Raphael (canvas_id, total_width+o.x_sep, total_height);
278 /* the path for the triangle-shaped buttons */
279 this.timebutton_path = "M1,0L"+(this.options.leases_w-1)+",0L"+(this.options.leases_w/2)+","+o.y_header+"L1,0";
281 var axisx = this.axisx;
282 var axisy = this.axisy;
284 /* maintain the list of nodelabels for the 'all nodes' button */
288 /* create the time slots legend */
290 var left = o.x_nodelabel;
292 var daymarker_height= 2*o.y_header + 2*o.y_sep + (axisy.length+1)*(o.y_node+o.y_sep);
293 var daymarker_path="M0,0L0," + daymarker_height;
295 var half_daymarker_off= 2*o.y_header + o.y_sep;
296 var half_daymarker_path="M0," + half_daymarker_off + "L0," + daymarker_height;
299 for (var i=0, len=axisx.length; i < len; ++i) {
300 /* pick the printable part */
301 var timelabel=axisx[i][1];
302 var y = top + o.y_header;
303 if (col%2 == 0) y += o.y_header;
305 /* display time label */
306 var timelabel=paper.text(left,y,timelabel).attr(txt_timelabel).attr({"text-anchor":"middle"});
307 /* draw vertical line */
308 var path_spec="M"+left+" "+(y+o.y_header/2)+"L"+left+" "+this.total_height;
309 var rule=paper.path(path_spec).attr(attr_rules);
310 /* show a day marker when relevant */
311 var timestamp=parseInt(axisx[i][0]);
312 if ( (timestamp%(24*3600))==0) {
313 paper.path(daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
314 } else if ( (timestamp%(12*3600))==0) {
315 paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
317 left += this.options.leases_w;
320 ////////// the row with the timeslot buttons (the one labeled 'All nodes')
321 this.granularity= this.min_granularity; // XXX axisx[1][0]-axisx[0][0];
323 // move two lines down
324 top += 2*o.y_header + 2*o.y_sep;
327 var allnodes = paper.text (o.x_nodelabel-o.x_sep,top+o.y_node/2,"All nodes").attr(txt_allnodes)
328 .attr ({"font-size":o.y_node, "text-anchor":"end","baseline":"bottom"});
329 allnodes.scheduler=this;
330 allnodes.click(allnodes_methods.click);
332 // timeslot buttons [it's the triangles above the slots]
333 for (var i=0, len=axisx.length; i < len; ++i) {
334 var timebutton=paper.path(this.timebutton_path).attr({'translation':left+','+top}).attr(attr_timebutton);
335 timebutton.from_time=axisx[i][0];
336 timebutton.scheduler=this;
337 timebutton.click(timebutton_methods.click);
338 left+=(this.options.leases_w);
341 //////// the body of the scheduler : loop on nodes
342 top += o.y_node + o.y_sep;
345 for (var i=0, len=axisy.length; i<len; ++i) {
347 var nodename=axisy[i][1];
348 var type=axisy[i][2];
352 var nodelabel = paper.text(o.x_nodelabel-x_sep,top+y_node/2,nodename).attr(txt_nodelabel)
353 .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
355 var nodelabel = paper.text(o.x_nodelabel-o.x_sep,top+o.y_node/3,nodename).attr(txt_nodelabel)
356 .attr ({"font-size":"12px", "text-anchor":"end","baseline":"bottom"});
357 //console.log(nodelabel);
358 nodelabel_methods.selected(nodelabel,1);
359 //nodelabel_methods.selected( this, ! this.selected );
360 nodelabel.click(nodelabel_methods.click); //click action works here
361 //timebutton.click(timebutton_methods.click);
363 // lease_methods.init_free(nodelabel.click, lease_methods.click_mine);
364 //lease_methods.init_free(nodelabel.lease);
365 this.nodelabels.push(nodelabel);
367 left += o.x_nodelabel;
369 // data index contains the full array of leases
370 // not the same amount of grains per node
372 // NOTE: remembering the previous lease might help for long leases.
373 while (grain < this.nb_grains()) {
375 if (l = this.find_lease(urn, this.initial_timestamp + grain * 1800)) {
376 slicename = l.slice_id;
381 //lease_id = this.data[data_index][0];
382 //slicename = this.data[data_index][1];
383 /* Duration should not be the lease duration, but the grain */
384 var duration = 1; // this.data[data_index][2];
386 var lease=paper.rect (left,top,this.options.leases_w*duration,o.y_node,o.radius);
387 // record scheduler in lease - early as we need this in init_other
388 lease.scheduler=this;
389 //lease.lease_id=lease_id;
390 lease.nodename=nodename;
392 lease.nodelabel=nodelabel;
393 if (slicename == "") {
394 lease.initial="free";
395 lease_methods.init_free(lease);
396 } else if (slicename == this.options.slicename) {
397 lease.initial="mine";
398 lease_methods.init_mine(lease);
400 lease.initial="other";
401 lease_methods.init_other(lease,slicename);
403 lease.from_time = axisx[grain%this.nb_grains()][0];
405 lease.until_time = axisx[grain%this.nb_grains()][0];
407 this.leases.push(lease);
408 // move on with the loop
409 left += this.options.leases_w*duration;
412 top += o.y_node + o.y_sep;
416 init_axisx: function(currentDate)
418 this.axisx = Array();
420 if(currentDate=="") {
421 // creating timestamp of the current time
422 currentDate = new Date().getTime() / 1000;
424 this.initial_timestamp = currentDate;
425 //console.log(currentDate);
427 // round it by granularity (becomes an Int)
428 var rounded = Math.round(currentDate/this.min_granularity)*this.min_granularity;
429 // Convert Int to Date
430 rounded = new Date(rounded*1000);
431 // get hours and minutes in a 24h format 00:00
432 var roundedHours=(rounded.getHours()<10?'0':'') + rounded.getHours();
433 var roundedMinutes=(rounded.getMinutes()<10?'0':'') + rounded.getMinutes();
435 //timeFrame.push(rounded);
436 this.axisx.push(Array(rounded, roundedHours+":"+roundedMinutes));
438 // Generate as man slots as we need
439 for(i=0; i<this.options.leases_slots;i++) {
440 rounded = this.min_granularity+(rounded.getTime()/1000);
441 rounded = new Date(rounded*1000);
442 roundedHours=(rounded.getHours()<10?'0':'') + rounded.getHours();
443 roundedMinutes=(rounded.getMinutes()<10?'0':'') + rounded.getMinutes();
445 //timeFrame.push(rounded);
446 this.axisx.push(Array(rounded, roundedHours+":"+roundedMinutes));
452 for (var i=0, len=this.leases.length; i<len; ++i) {
453 var lease=this.leases[i];
454 if (lease.current != lease.initial) {
455 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
456 else lease_methods.init_mine(lease,lease_methods.click_free);
461 // XXX Couldn't find how to inhererit from the raphael objects...
463 /* ---------------------------------------------------------------------
464 * The 'all nodes' button
467 click: function (event) {
468 var scheduler=this.scheduler;
469 /* decide what to do */
471 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
472 if (! scheduler.nodelabels[i].selected) unselected++;
473 /* if at least one is not selected : select all */
474 var new_state = (unselected >0) ? 1 : 0;
475 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
476 nodelabel_methods.selected(scheduler.nodelabels[i],new_state);
480 /* ---------------------------------------------------------------------
481 * The buttons for managing the whole timeslot
483 timebutton_methods: {
486 click: function (event) {
487 var scheduler = this.scheduler;
488 var from_time = this.from_time;
489 var until_time = from_time + scheduler.granularity;
490 /* scan leases on selected nodes, store in two arrays */
491 var relevant_free=[], relevant_mine=[];
492 for (var i=0,len=scheduler.leases.length; i<len; ++i) {
493 var scan=scheduler.leases[i];
494 if ( ! scan.nodelabel.selected) continue;
496 if (scan.from_time<=from_time && scan.until_time>=until_time) {
497 if (scan.current == "free") relevant_free.push(scan);
498 else if (scan.current == "mine") relevant_mine.push(scan);
501 // window.console.log("Found " + relevant_free.length + " free and " + relevant_mine.length + " mine");
502 /* decide what to do, whether book or release */
503 if (relevant_mine.length==0 && relevant_free.length==0) {
504 alert ("Nothing to do in this timeslot on the selected nodes");
507 // if at least one is free, let's book
508 if (relevant_free.length > 0) {
509 for (var i=0, len=relevant_free.length; i<len; ++i) {
510 var lease=relevant_free[i];
511 lease_methods.init_mine(lease,lease_methods.click_free);
513 // otherwise we unselect
515 for (var i=0, len=relevant_mine.length; i<len; ++i) {
516 var lease=relevant_mine[i];
517 lease_methods.init_free(lease,lease_methods.click_mine);
523 /* ---------------------------------------------------------------------
524 * The nodelabel buttons
528 // set selected mode and render visually
529 selected: function (nodelabel, flag) {
530 nodelabel.selected=flag;
531 nodelabel.attr({'font-weight': (flag ? 'bold' : 'normal')});
534 // loop on axis x and select each timebutton
535 // for (var i=0, len=axisx.length; i < len; ++i)
536 // figure out how to use timebutton_methods.click();
541 click: function (event) {
542 nodelabel_methods.selected( this, ! this.selected );
547 /* ---------------------------------------------------------------------
552 init_free: function (lease, unclick) {
553 var o = lease.scheduler.options;
554 lease.current="free";
556 lease.animate((lease.initial=="free") ? attr_lease_free_free : attr_lease_mine_free, o.anim_delay);
557 // keep track of the current status
559 lease.click (lease_methods.click_free);
560 if (unclick) lease.unclick(unclick);
563 // find out all the currently free leases that overlap this one
564 click_free: function (event) {
565 var scheduler = this.scheduler;
566 lease_methods.init_mine(this,lease_methods.click_free);
572 var start_time=new Date(this.from_time).getTime() / 1000;
573 var end_time=new Date(this.until_time).getTime() / 1000;
574 var duration=(end_time-start_time)/1800; // XXX HARDCODED LEASE GRAIN
576 /* Add a new lease : XXX should be replaced by a dictionary */
577 // Do we have a lease with the same urn just before or just after ?
578 var removeIdBefore = null;
579 var removeIdAfter = null;
580 jQuery.each(scheduler.listLeases, function(i, lease) {
581 if (lease[0] == urn) {
582 if (lease[1] + lease[2] * 1800 == start_time) { // XXX HARDCODED LEASE GRAIN
583 // Merge with previous lease
585 start_time = lease[1];
586 duration += lease[2];
588 if (lease[1] == end_time) {
589 // Merge with following lease
591 duration += lease[2];
595 if (removeIdBefore != null) {
596 scheduler.listLeases.splice(removeIdBefore , 1);
597 if (removeIdAfter != null)
600 if (removeIdAfter != null) {
601 scheduler.listLeases.splice(removeIdAfter , 1);
604 scheduler.listLeases.push([this.urn, start_time, duration]);
606 console.log(scheduler.listLeases);
607 //jQuery.publish('/update-set/' + scheduler.options.query_uuid, [scheduler.listLeases]);
608 jQuery.publish('/update-set/' + scheduler.options.lease_query_uuid, [scheduler.listLeases]);
611 init_mine: function (lease, unclick) {
612 var o = lease.scheduler.options;
613 lease.current="mine";
614 lease.animate((lease.initial=="mine") ? attr_lease_mine_mine : attr_lease_free_mine,o.anim_delay);
615 lease.click (lease_methods.click_mine);
616 if (unclick) lease.unclick(unclick);
619 /* TODO: remove selected lease from array listLeases and publish change */
620 click_mine: function (event) {
621 var scheduler = this.scheduler;
622 // this lease was originally free but is now marked for booking
623 // we free just this lease
624 //console.log('this is mine');
625 lease_methods.init_free(this, lease_methods.click_mine);
629 init_other: function (lease, slicename) {
630 lease.animate (attr_lease_other,anim_delay);
631 /* a text obj to display the name of the slice that owns that lease */
632 var otherslicelabel = lease.scheduler.paper.text (lease.attr("x")+lease.attr("width")/2,
634 lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_otherslice);
635 /* hide it right away */
636 otherslicelabel.hide();
638 lease.label=otherslicelabel;
639 lease.hover ( function (e) {this.label.toFront();this.label.show();},
640 function (e) {this.label.hide();} );
646 $.plugin('Scheduler', Scheduler);