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._canvas_id = this.id('leases_area');
53 this.query_uuid = options.query_uuid;
56 //this.current_resources = Array();
57 //this.current_leases = Array();
59 /* Managing asynchronous reception of resources and leases */
60 this._tmp_resources = Array();
61 this.listLeases = Array();
62 this._received_resources = false;
63 this._received_leases = false;
65 this.myLeases = Array();
66 this.allLeases = Array();
69 this.resources = Array();
71 this.default_granularity = 1800; /* 30 min */
72 this.initial_timestamp = null;
74 /* This should be updated to be the ppcm of all granularities */
75 this.min_granularity = this.default_granularity;
77 // the data contains slice names, and lease_id, we need this to find our own leases (mine)
84 /* Listening to queries */
85 this.listen_query(options.query_uuid);
86 this.listen_query(options.query_lease_uuid, 'lease');
88 /* XXX GUI setup and event binding */
89 jQuery("#datepicker").datetimepicker({
90 //Timezone wise selection option
91 //timeFormat: 'hh:mm tt z',
94 onSelect: function(value) {
96 //converting time to timestamp
97 currentDate= Math.round(Date.parse(value)/ 1000);
99 // convert value to timestamp --done
100 // Get jQuery("#timepicker").val(); - no need anymore
101 // convert timepicker into seconds - no need
102 // Add time to date - done
103 // pass the result to init_axisx - done
104 //console.log(currentDate);
106 s.init_axisx(currentDate);
108 // Do we need to populate the timeslots with existing leases?
109 // Look how to populate with initial_leases [we have to show the leases]
113 //console.log(s.nodelabels[1]);
114 // Do we need to populate the timeslots with existing leases?
115 // Look how to populate with initial_leases
117 // TODO -- DONE Note: autocomplete is not search box
118 // Implement a filtering functionality, on the name of the node
119 // http://jqueryui.com/autocomplete/
120 // pass s.nodelabels to autocomplete function
121 //jQuery("#search").autocomplete('s.nodelabel');
122 //console.log(s.nodelabel);
126 * During init, there are no resources
127 * So the list of nodes is empty
128 * The function triggered by the subscription to the resources query
129 * var RESULTS_RESOURCES = '/results/' + options.resource_query_uuid + '/changed';
130 * $.subscribe(RESULTS_RESOURCES, function(e, resources) { s.set_resources(resources); });
131 * Will have to update the list of nodes available through autocomplete (availableTags)
133 * Be inspired by QueryEditor plugin
135 * Resources informations in s.axisy ???
137 * Filter what has been selected in other plugins:
142 * Implement an action of filtering while typing
143 * filter what correspond to the user choice
146 var availableTags = ['omf','nitos','ple', s.nodelabel];
147 $( "#search" ).autocomplete({
148 source: availableTags
158 /* Default settings */
163 leases_granularity: 3600,
165 x_nodelabel: 200, /* space for the nodenames */
166 x_sep: 20, /* right space after the nodename - removed from the above */
167 y_header: 12, /* height for the (two) rows of timelabels */
168 y_sep: 10, /* space between nodes */
169 leases_w: 20, /* 1-grain leases attributes */
174 resource_query_uuid: null, /* resources */
175 lease_query_uuid: null, /* leases */
182 /* GUI MANIPULATION */
186 /*************************** RECORD HANDLER ***************************/
188 /* ------------------------------------------------------------------
190 * ------------------------------------------------------------------ */
192 on_new_record: function(record)
194 if ((typeof record.exclusive != 'undefined') && (record.exclusive)) {
195 this.resources.push(Array(record.urn, record.record_hrn, record.type));
197 // ... if we do not have information about slivers (first update), update it
198 if (typeof record.sliver != 'undefined') {
203 on_query_in_progress: function() {
207 on_query_done: function() {
208 /* We have received all leases */
209 if (this._received_resources) {
210 this.draw(this._canvas_id);
213 this._received_leases = true;
215 /* ------------------------------------------------------------------
217 * ------------------------------------------------------------------ */
219 on_lease_new_record: function(record)
221 this.listLeases.push(record);
222 // this.initial_leases=leases;
225 on_lease_query_done: function(record)
227 /* We have received all resources */
228 if (this._received_leases) {
229 this.draw(this._canvas_id);
232 this._received_resources = true;
235 /* INTERNAL FUNCTIONS */
238 * @brief Return the number of time slots
240 nb_grains: function ()
242 return this.axisx.length;
246 * @brief Returns whether there is a pending lease at this timestamp
248 find_lease: function(urn, timestamp)
250 var scheduler = this;
253 $.each(Array(scheduler.myLeases, scheduler.allLeases), function(i, array) {
254 $.each(array, function(i, lease) {
255 if (lease[0] == urn) {
256 if ((timestamp >= lease[1]) && (timestamp < (lease[1] + lease[2] * 1800))) {
274 var canvas_id = this._canvas_id;
275 var o = this.options;
276 var total_width = o.x_nodelabel + this.nb_grains() * this.options.leases_w;
277 var total_height = 2 * o.y_header /* the timelabels */
278 + 2 * o.y_sep /* extra space */
279 + o.y_node /* all-nodes & timebuttons row */
280 + (this.resources.length)*(o.y_node+o.y_sep); /* the regular nodes and preceding space */
282 /* reuse for paper if exists with same size, or (re-)create otherwise */
284 if (this.paper == null) {
285 paper = Raphael (canvas_id, total_width+o.x_sep, total_height);
286 } else if (this.paper.width==total_width && this.paper.height==total_height) {
290 $("#"+canvas_id)[0].innerHTML="";
291 paper = Raphael (canvas_id, total_width+o.x_sep, total_height);
295 /* the path for the triangle-shaped buttons */
296 this.timebutton_path = "M1,0L"+(this.options.leases_w-1)+",0L"+(this.options.leases_w/2)+","+o.y_header+"L1,0";
298 var axisx = this.axisx;
299 var axisy = this.resources;
301 /* maintain the list of nodelabels for the 'all nodes' button */
305 /* create the time slots legend */
307 var left = o.x_nodelabel;
309 var daymarker_height= 2*o.y_header + 2*o.y_sep + (axisy.length+1)*(o.y_node+o.y_sep);
310 var daymarker_path="M0,0L0," + daymarker_height;
312 var half_daymarker_off= 2*o.y_header + o.y_sep;
313 var half_daymarker_path="M0," + half_daymarker_off + "L0," + daymarker_height;
316 for (var i=0, len=axisx.length; i < len; ++i) {
317 /* pick the printable part */
318 var timelabel=axisx[i][1];
319 var y = top + o.y_header;
320 if (col%2 == 0) y += o.y_header;
322 /* display time label */
323 var timelabel=paper.text(left,y,timelabel).attr(txt_timelabel).attr({"text-anchor":"middle"});
324 /* draw vertical line */
325 var path_spec="M"+left+" "+(y+o.y_header/2)+"L"+left+" "+this.total_height;
326 var rule=paper.path(path_spec).attr(attr_rules);
327 /* show a day marker when relevant */
328 var timestamp=parseInt(axisx[i][0]);
329 if ( (timestamp%(24*3600))==0) {
330 paper.path(daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
331 } else if ( (timestamp%(12*3600))==0) {
332 paper.path(half_daymarker_path).attr({'translation':left+','+top}).attr(attr_daymarker);
334 left += this.options.leases_w;
337 ////////// the row with the timeslot buttons (the one labeled 'All nodes')
338 this.granularity= this.min_granularity; // XXX axisx[1][0]-axisx[0][0];
340 // move two lines down
341 top += 2*o.y_header + 2*o.y_sep;
344 var allnodes = paper.text (o.x_nodelabel-o.x_sep,top+o.y_node/2,"All nodes").attr(txt_allnodes)
345 .attr ({"font-size":o.y_node, "text-anchor":"end","baseline":"bottom"});
346 allnodes.scheduler=this;
347 allnodes.click(this.allnodes_methods.click);
349 // timeslot buttons [it's the triangles above the slots]
350 for (var i=0, len=axisx.length; i < len; ++i) {
351 var timebutton=paper.path(this.timebutton_path).attr({'translation':left+','+top}).attr(attr_timebutton);
352 timebutton.from_time=axisx[i][0];
353 timebutton.scheduler=this;
354 timebutton.click(this.timebutton_methods.click);
355 left+=(this.options.leases_w);
358 //////// the body of the scheduler : loop on nodes
359 top += o.y_node + o.y_sep;
362 for (var i=0, len=axisy.length; i<len; ++i) {
364 var nodename=axisy[i][1];
365 var type=axisy[i][2];
369 var nodelabel = paper.text(o.x_nodelabel-x_sep,top+y_node/2,nodename).attr(txt_nodelabel)
370 .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
372 var nodelabel = paper.text(o.x_nodelabel-o.x_sep,top+o.y_node/3,nodename).attr(txt_nodelabel)
373 .attr ({"font-size":"12px", "text-anchor":"end","baseline":"bottom"});
374 //console.log(nodelabel);
375 this.nodelabel_methods.selected(nodelabel,1);
376 //nodelabel_methods.selected( this, ! this.selected );
377 nodelabel.click(this.nodelabel_methods.click); //click action works here
378 //timebutton.click(timebutton_methods.click);
380 // lease_methods.init_free(nodelabel.click, lease_methods.click_mine);
381 //lease_methods.init_free(nodelabel.lease);
382 this.nodelabels.push(nodelabel);
384 left += o.x_nodelabel;
386 // data index contains the full array of leases
387 // not the same amount of grains per node
389 // NOTE: remembering the previous lease might help for long leases.
390 while (grain < this.nb_grains()) {
392 if (l = this.find_lease(urn, this.initial_timestamp + grain * 1800)) {
393 slicename = l.slice_id;
398 //lease_id = this.data[data_index][0];
399 //slicename = this.data[data_index][1];
400 /* Duration should not be the lease duration, but the grain */
401 var duration = 1; // this.data[data_index][2];
403 var lease=paper.rect (left,top,this.options.leases_w*duration,o.y_node,o.radius);
404 // record scheduler in lease - early as we need this in init_other
405 lease.scheduler=this;
406 //lease.lease_id=lease_id;
407 lease.nodename=nodename;
409 lease.nodelabel=nodelabel;
410 if (slicename == "") {
411 lease.initial="free";
412 lease_methods.init_free(lease);
413 } else if (slicename == this.options.slicename) {
414 lease.initial="mine";
415 lease_methods.init_mine(lease);
417 lease.initial="other";
418 lease_methods.init_other(lease,slicename);
420 lease.from_time = axisx[grain%this.nb_grains()][0];
422 lease.until_time = axisx[grain%this.nb_grains()][0];
424 this.leases.push(lease);
425 // move on with the loop
426 left += this.options.leases_w*duration;
429 top += o.y_node + o.y_sep;
433 init_axisx: function(currentDate)
435 this.axisx = Array();
437 if(currentDate=="") {
438 // creating timestamp of the current time
439 currentDate = new Date().getTime() / 1000;
441 this.initial_timestamp = currentDate;
442 //console.log(currentDate);
444 // round it by granularity (becomes an Int)
445 var rounded = Math.round(currentDate/this.min_granularity)*this.min_granularity;
446 // Convert Int to Date
447 rounded = new Date(rounded*1000);
448 // get hours and minutes in a 24h format 00:00
449 var roundedHours=(rounded.getHours()<10?'0':'') + rounded.getHours();
450 var roundedMinutes=(rounded.getMinutes()<10?'0':'') + rounded.getMinutes();
452 //timeFrame.push(rounded);
453 this.axisx.push(Array(rounded, roundedHours+":"+roundedMinutes));
455 // Generate as man slots as we need
456 for(i=0; i<this.options.leases_slots;i++) {
457 rounded = this.min_granularity+(rounded.getTime()/1000);
458 rounded = new Date(rounded*1000);
459 roundedHours=(rounded.getHours()<10?'0':'') + rounded.getHours();
460 roundedMinutes=(rounded.getMinutes()<10?'0':'') + rounded.getMinutes();
462 //timeFrame.push(rounded);
463 this.axisx.push(Array(rounded, roundedHours+":"+roundedMinutes));
469 for (var i=0, len=this.leases.length; i<len; ++i) {
470 var lease=this.leases[i];
471 if (lease.current != lease.initial) {
472 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
473 else lease_methods.init_mine(lease,lease_methods.click_free);
478 // XXX Couldn't find how to inhererit from the raphael objects...
480 /* ---------------------------------------------------------------------
481 * The 'all nodes' button
484 click: function (event) {
485 var scheduler=this.scheduler;
486 /* decide what to do */
488 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
489 if (! scheduler.nodelabels[i].selected) unselected++;
490 /* if at least one is not selected : select all */
491 var new_state = (unselected >0) ? 1 : 0;
492 for (var i=0, len=scheduler.nodelabels.length; i<len; ++i)
493 this.nodelabel_methods.selected(scheduler.nodelabels[i],new_state);
497 /* ---------------------------------------------------------------------
498 * The buttons for managing the whole timeslot
500 timebutton_methods: {
503 click: function (event) {
504 var scheduler = this.scheduler;
505 var from_time = this.from_time;
506 var until_time = from_time + scheduler.granularity;
507 /* scan leases on selected nodes, store in two arrays */
508 var relevant_free=[], relevant_mine=[];
509 for (var i=0,len=scheduler.leases.length; i<len; ++i) {
510 var scan=scheduler.leases[i];
511 if ( ! scan.nodelabel.selected) continue;
513 if (scan.from_time<=from_time && scan.until_time>=until_time) {
514 if (scan.current == "free") relevant_free.push(scan);
515 else if (scan.current == "mine") relevant_mine.push(scan);
518 // window.console.log("Found " + relevant_free.length + " free and " + relevant_mine.length + " mine");
519 /* decide what to do, whether book or release */
520 if (relevant_mine.length==0 && relevant_free.length==0) {
521 alert ("Nothing to do in this timeslot on the selected nodes");
524 // if at least one is free, let's book
525 if (relevant_free.length > 0) {
526 for (var i=0, len=relevant_free.length; i<len; ++i) {
527 var lease=relevant_free[i];
528 lease_methods.init_mine(lease,lease_methods.click_free);
530 // otherwise we unselect
532 for (var i=0, len=relevant_mine.length; i<len; ++i) {
533 var lease=relevant_mine[i];
534 lease_methods.init_free(lease,lease_methods.click_mine);
540 /* ---------------------------------------------------------------------
541 * The nodelabel buttons
545 // set selected mode and render visually
546 selected: function (nodelabel, flag) {
547 nodelabel.selected=flag;
548 nodelabel.attr({'font-weight': (flag ? 'bold' : 'normal')});
551 // loop on axis x and select each timebutton
552 // for (var i=0, len=axisx.length; i < len; ++i)
553 // figure out how to use timebutton_methods.click();
558 click: function (event) {
559 nodelabel_methods.selected( this, ! this.selected );
564 /* ---------------------------------------------------------------------
569 init_free: function (lease, unclick) {
570 var o = lease.scheduler.options;
571 lease.current="free";
573 lease.animate((lease.initial=="free") ? attr_lease_free_free : attr_lease_mine_free, o.anim_delay);
574 // keep track of the current status
576 lease.click (lease_methods.click_free);
577 if (unclick) lease.unclick(unclick);
580 // find out all the currently free leases that overlap this one
581 click_free: function (event) {
582 var scheduler = this.scheduler;
583 lease_methods.init_mine(this,lease_methods.click_free);
589 var start_time=new Date(this.from_time).getTime() / 1000;
590 var end_time=new Date(this.until_time).getTime() / 1000;
591 var duration=(end_time-start_time)/1800; // XXX HARDCODED LEASE GRAIN
593 /* Add a new lease : XXX should be replaced by a dictionary */
594 // Do we have a lease with the same urn just before or just after ?
595 var removeIdBefore = null;
596 var removeIdAfter = null;
597 jQuery.each(scheduler.listLeases, function(i, lease) {
598 if (lease[0] == urn) {
599 if (lease[1] + lease[2] * 1800 == start_time) { // XXX HARDCODED LEASE GRAIN
600 // Merge with previous lease
602 start_time = lease[1];
603 duration += lease[2];
605 if (lease[1] == end_time) {
606 // Merge with following lease
608 duration += lease[2];
612 if (removeIdBefore != null) {
613 scheduler.listLeases.splice(removeIdBefore , 1);
614 if (removeIdAfter != null)
617 if (removeIdAfter != null) {
618 scheduler.listLeases.splice(removeIdAfter , 1);
621 scheduler.listLeases.push([this.urn, start_time, duration]);
623 console.log(scheduler.listLeases);
624 //jQuery.publish('/update-set/' + scheduler.options.query_uuid, [scheduler.listLeases]);
625 jQuery.publish('/update-set/' + scheduler.options.lease_query_uuid, [scheduler.listLeases]);
628 init_mine: function (lease, unclick) {
629 var o = lease.scheduler.options;
630 lease.current="mine";
631 lease.animate((lease.initial=="mine") ? attr_lease_mine_mine : attr_lease_free_mine,o.anim_delay);
632 lease.click (lease_methods.click_mine);
633 if (unclick) lease.unclick(unclick);
636 /* TODO: remove selected lease from array listLeases and publish change */
637 click_mine: function (event) {
638 var scheduler = this.scheduler;
639 // this lease was originally free but is now marked for booking
640 // we free just this lease
641 //console.log('this is mine');
642 lease_methods.init_free(this, lease_methods.click_mine);
646 init_other: function (lease, slicename) {
647 lease.animate (attr_lease_other,anim_delay);
648 /* a text obj to display the name of the slice that owns that lease */
649 var otherslicelabel = lease.scheduler.paper.text (lease.attr("x")+lease.attr("width")/2,
651 lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_otherslice);
652 /* hide it right away */
653 otherslicelabel.hide();
655 lease.label=otherslicelabel;
656 lease.hover ( function (e) {this.label.toFront();this.label.show();},
657 function (e) {this.label.hide();} );
663 $.plugin('Scheduler', Scheduler);