2 * MySlice QueryUpdater plugin
4 * URL: http://www.myslice.info
5 * Description: display of selected resources
7 * Author: The MySlice Team
8 * Copyright: Copyright 2012 UPMC Sorbonne Universités
17 // XXX record selected (multiple selection ?)
18 // XXX record disabled ?
19 // XXX out of sync plugin ?
20 // XXX out of date record ?
23 // criticality of the absence of a handler in a plugin
24 // non-critical only can have switch case
26 // Record state through the query cycle
29 var QueryUpdater = Plugin.extend({
31 init: function(options, element) {
32 this.classname="queryupdater";
33 this._super(options, element);
37 this.initial = Array();
38 this.selected_resources = Array();
40 this.table = this.elmt('table').dataTable({
41 // the original querytable layout was
42 // sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t<'row'<'col-xs-5'i><'col-xs-7'p>>",
43 // however the bottom line with 'showing blabla...' and the navigation widget are not really helpful
44 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
45 // so this does not matter anymore now that the pagination area is turned off
46 // sPaginationType: 'bootstrap',
50 // sScrollX : '100%', // Horizontal scrolling
51 // bSortClasses : false, // Disable style for the sorted column
52 // aaSorting : [[ 0, 'asc' ]], // Default sorting on URN
53 // fnDrawCallback: function() { // Reassociate close click every time the table is redrawn
54 // /* Prevent to loop on click while redrawing table */
55 // $('.ResourceSelectedClose').unbind('click');
56 // /* Handle clicks on close span */
57 // /* Reassociate close click every time the table is redrawn */
58 // $('.ResourceSelectedClose').bind('click', self, self._close_click);
62 // XXX This should not be done at init...
63 this.elmt('update').click(this, this.do_checksla);
64 this.elmt('refresh').click(this, this.do_refresh);
65 this.elmt('reset').click(this, this.do_reset);
66 this.elmt('clear_annotations').click(this, this.do_clear_annotations);
68 this.listen_query(options.query_uuid);
71 /*************************** PLUGIN EVENTS ****************************/
73 /***************************** GUI EVENTS *****************************/
75 do_checksla: function(e) {
76 var username = e.data.options.username;
78 var arrayselectedresources = data.selected_resources;
80 var accepted_sla = Array();
83 // XXX check that the query is not disabled
85 console.log("DATA VALUE: " + data.value);
87 //<p>SLA description</p>
88 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>
89 //<p>SLA description</p>
90 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>
91 var promt = $('#modal-body');
97 promt.append('<p>SLA description</p>');
101 for(var iter = 0; iter < arrayselectedresources.length; iter++){
102 var list = '<li class="wi'+iter+'" name=wi"'+iter+'">'+arrayselectedresources[iter].toLowerCase()+'</li>';
104 if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
106 accepted_sla.push({"wilab2":false});
113 wilabForm += "</ul>";
115 //var wallmessage = '<p>SLA description</p><p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>';
119 for(var iter = 0; iter < arrayselectedresources.length; iter++){
120 var list = '<li class="wall'+iter+'" name=wall"'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'</li>';
122 if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
124 accepted_sla.push({"wall2":false});
133 var flagDouble = false;
137 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>');
138 promt.append(wilabForm);
139 promt.append('<br />');
143 //promt.append(wallmessage);
145 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>');
146 promt.append(wallForm);
147 promt.append('<br />');
151 // var wimessage = '<p>SLA description</p><p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>'
153 if(flagWi || flagVW){
154 $('#sla_dialog').show();
156 $('#slamodal-wilab2').modal('show');
162 var username = e.data.options.username;
163 var urn = data.value;
164 // XXX check that the query is not disabled
167 console.log("do_update");
168 // XXX check that the query is not disabled
169 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
173 $("#accept_sla_wilab2").click(function(){
174 console.log("SLA ACCEPTED");
175 console.log("With username: " + username);
177 // var promt = $('#modal-body');
178 // var notchecked = true;
179 // for (var i=0;i<50;i++)
181 // var wielement = $('#wi'+i);
182 // var wallElement = $('#wall'+i);
183 // if(wielement != null && !wielement.checked)
185 // notchecked = false;
187 // if(wallElement!= null && !wallElement.checked)
189 // notchecked = false;
196 $.post("/sla/agreements/simplecreate",
197 { "template_id": "iMindsServiceWiLab2",
199 "expiration_time": new Date().toISOString()
201 $.post("/sla/agreements/simplecreate",
202 { "template_id": "iMindsServiceVirtualwall",
204 "expiration_time": new Date().toISOString()
207 $('#slamodal-wilab2').modal('hide');
208 accepted_sla["wilab2"] = true;
210 manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
214 $("#dismiss_sla_wilab2").click(function(){
215 console.log("SLA NOT ACCEPTED");
216 $('#slamodal-wilab2').modal('hide');
217 $('#modal-body').empty();
221 // this.do_update(e);
224 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
225 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
226 // promt.append(list);
227 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
231 // accepted_sla.push({"wall2":false});
233 // $('#sla_dialog').show();
234 // $('#slamodal-virtualwall').modal('show');
237 // $("#accept_sla_vwall").click(function(){
238 // console.log("SLA ACCEPTED");
239 // console.log("With username: " + username);
241 // $.post("/sla/agreements/simplecreate",
242 // { "template_id": "iMindsServiceVirtualwall",
244 // "expiration_time": new Date()
247 // $('#slamodal-virtualwall').modal('hide');
248 // accepted_sla["wall2"] = true;
251 // $("#dismiss_sla_vwall").click(function(){
252 // console.log("SLA NOT ACCEPTED");
253 // $('#slamodal-vir').modal('hide');
258 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
260 // accepted_sla.push({"wilab2":false});
262 // $('#sla_dialog').show();
263 // $('#slamodal-wilab2').modal('show');
266 // $("#accept_sla_wilab2").click(function(){
267 // console.log("SLA ACCEPTED");
268 // console.log("With username: " + username);
270 // $.post("/sla/agreements/simplecreate",
271 // { "template_id": "iMindsServiceWiLab2",
273 // "expiration_time": new Date()
276 // $('#slamodal-wilab2').modal('hide');
277 // accepted_sla["wilab2"] = true;
280 // $("#dismiss_sla_wilab2").click(function(){
281 // console.log("SLA NOT ACCEPTED");
282 // $('#slamodal-wilab2').modal('hide');
290 // for(var sla in accepted_sla){
291 // if(accepted_sla[sla] == true){
296 // if(count == accepted_sla.length){
297 // this.do_update(e);
302 do_update: function(e) {
305 var username = e.data.options.username;
306 var urn = data.value;
307 // XXX check that the query is not disabled
310 console.log("do_update");
311 // XXX check that the query is not disabled
312 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
314 // how to stop the spinning after the event?
315 // this should be triggered by some on_updatequery_done ?
319 // related buttons are also disabled in the html template
320 do_refresh: function(e)
322 throw 'resource_selected.do_refresh Not implemented';
325 do_reset: function(e)
327 throw 'queryupdater.do_reset Not implemented';
330 do_clear_annotations: function(e)
332 throw 'queryupdater.do_clear_annotations Not implemented';
335 /************************** GUI MANIPULATION **************************/
338 set_button_state: function(name, state)
340 this.elmt(name).attr('disabled', state ? false : 'disabled');
348 find_row: function(key)
350 // key in third position, column id = 2
353 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
354 return (col._aData[KEY_POS] == key);
357 if (cols.length == 0)
360 throw "Too many same-key rows in ResourceSelected plugin";
365 set_state: function(data)
367 console.log("function set_state");
374 // make sure the change is visible : toggle on the whole plugin
375 // this might have to be made an 'auto-toggle' option of this plugin..
376 // also it might be needed to be a little finer-grained here
378 // XXX we don't want to show automaticaly the pending when a checkbox is checked
381 switch(data.request) {
382 case FIELD_REQUEST_ADD_RESET:
383 case FIELD_REQUEST_REMOVE_RESET:
384 // find line and delete it
385 row = this.find_row(data.value);
387 this.table.fnDeleteRow(row.nTr);
388 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
389 $("#badge-pending").text($("#badge-pending").data('number'));
391 case FIELD_REQUEST_CHANGE:
394 case FIELD_REQUEST_ADD:
397 case FIELD_REQUEST_REMOVE:
402 switch(data.status) {
403 case FIELD_REQUEST_PENDING:
405 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
407 case FIELD_REQUEST_SUCCESS:
410 case FIELD_REQUEST_FAILURE:
415 var status = msg + status;
420 // if no, create it, else replace it
421 // XXX it's not just about adding lines, but sometimes removing some
422 // XXX how do we handle status reset ?
423 data.value = JSON.stringify(data.value);
424 data.selected_resources = this.selected_resources;
425 row = this.find_row(data.value);
434 // XXX second parameter refresh = false can improve performance. todo in querytable also
435 this.table.fnAddData(newline);
436 row = this.find_row(data.value);
437 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
438 $("#badge-pending").text($("#badge-pending").data('number'));
440 // Update row text...
441 this.table.fnUpdate(newline, row.nTr);
444 // Change cell color according to status
446 $(row.nTr).removeClass('add remove')
447 var cls = action.toLowerCase();
449 $(row.nTr).addClass(cls);
453 /*************************** QUERY HANDLER ****************************/
457 /*************************** RECORD HANDLER ***************************/
459 on_new_record: function(record)
461 console.log("query_updater on_new_record");
464 // if (not and update) {
466 // initial['resource'], initial['lease'] ?
467 this.initial.push(record);
469 //this.set_record_state(record, RECORD_STATE_ATTACHED);
470 // We simply add to the table
472 // \ this.initial_resources
475 // current_resources \ YES | NO
476 // --------------------+----------+---------
477 // YES | attached | added
485 // +-----------------+--------------+
487 // +-------+ ?G +-------+ +-------+ +---+---+ |
488 // | | -----> | | !G | | | | DA |
489 // | ND | | PG | -----> | D | -----> | PC | <------+ |
490 // | | <----- | | ~G | | C | | | |
491 // +-------+ GE +-------+ +-------+ +-------+ +------+
493 // | DA UE | | ?U | PCA |
495 // +-------+ +-------+ +------+
497 // | AD | <----- | PU | --------+
499 // +-------+ +-------+
504 // Plugins (i) receive state information, (ii) perform actions
507 // ND : No data ?G : Get query
508 // PG : Pending Get !G : Get reply
509 // D : Data present ~G : Get partial reply
510 // PC : Pending changes GE : Get error
511 // PU : Pending update C : Change request
512 // PCA: Pending change with annotation R : Reset request
513 // AD : Annotated data ?U : Update query
515 // ~U : Update partial reply
517 // DA : Delete annotation request
519 // - D -> PU directly if the user chooses 'dynamic update'
520 // - Be careful for updates if partial Get success
522 // ND: No data == initialization state
525 // - on_query_in_progress
526 // NOTE: cannot distinguish get and update here. Is it important ?
528 on_query_in_progress: function()
530 messages.debug("queryupdater.on_query_in_progress");
535 // - on_clear_records (Get)
536 // - on_new_record (shared with AD) XXX
538 // NOTE: cannot distinguish get and update here. Is it important ?
539 // NOTE: Would record key be sufficient for update ?
541 on_clear_records: function()
546 on_query_done: function()
548 console.log("on_query_done");
552 // PC : Pending changes
553 // NOTE: record_key could be sufficient
554 on_added_record: function(record)
556 console.log("on_added_record = ",record);
557 this.set_record_state(record, RECORD_STATE_ADDED);
558 // update pending number
561 on_removed_record: function(record_key)
563 console.log("on_removed_record = ",record_key);
564 this.set_record_state(RECORD_STATE_REMOVED);
567 // PU : Pending update
568 // - on_query_in_progress (already done)
570 // PCA : Pending change with annotation
571 // NOTE: Manifold will inform the plugin about updates, and thus won't
572 // call new record, even if the same channel UUID is used...
573 // - TODO on_updated_record
574 // - Key and confirmation could be sufficient, or key and record state
575 // XXX move record state to the manifold plugin API
577 on_field_state_changed: function(result)
579 console.log("on_field_state_changed");
581 if(result.request == FIELD_REQUEST_ADD){
582 this.selected_resources.push(result.value);
583 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
584 var i = this.selected_resources.indexOf(result.value);
586 this.selected_resources.splice(i,1);
589 console.log("Resources: " + self.selected_resources);
590 messages.debug(result)
591 /* this.set_state(result.request, result.key, result.value, result.status); */
592 this.set_state(result);
595 // XXX we will have the requests for change
596 // XXX + the requests to move into the query cycle = the buttons aforementioned
598 // XXX what happens in case of updates ? not implemented yet
599 // XXX we want resources and leases
600 // listen for SET_ADD and SET_REMOVE for slice query
602 /************************** PRIVATE METHODS ***************************/
604 _close_click: function(e)
608 //jQuery.publish('selected', 'add/'+key_value);
609 // this.parentNode is <td> this.parentNode.parentNode is <tr>
610 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
611 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
612 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
613 var remove_urn = this.id;
614 var current_resources = event.data.instance.current_resources;
615 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
616 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
617 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
620 /******************************** TODO ********************************/
622 update_resources: function(resources, change)
624 messages.debug("update_resources");
625 var my_oTable = this.table.dataTable();
626 var prev_resources = this.current_resources;
629 * The first time the query is advertised, don't do anything. The
630 * component will learn nodes in the slice through the manifest
631 * received through the other subscription
636 var initial = this.initial;
637 //var r_removed = []; //
638 /*-----------------------------------------------------------------------
639 TODO: remove this dirty hack !!!
641 resources = jQuery.map(resources, function(x){
642 if(!('timeslot' in x)){x.timeslot=0;}
646 TODO: handle generic keys instead of specific stuff
650 var initial_urn = $.map(initial, function(x){return x.urn;});
651 var resources_urn = $.map(resources, function(x){return x.urn;});
652 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
653 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
654 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
655 exists = false; // ioi
656 /*-----------------------------------------------------------------------*/
658 my_oTable.fnClearTable();
660 TODO: factorization of this code !!!
662 $.each(r_added, function(i, r) {
663 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
665 // Create the resource objects
670 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
671 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
674 newline.push(type, urn, slot, SPAN); // ioi
675 var line = my_oTable.fnAddData(newline);
676 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
677 nTr.className = type;
679 $.each(r_attached, function(i, r) {
680 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
681 var type = 'attached';
682 // Create the resource objects
687 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
688 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
691 newline.push(type, node, slot, SPAN); // ioi
692 var line = my_oTable.fnAddData(newline);
693 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
694 nTr.className = type;
696 $.each(r_removed, function(i, r) {
697 // The list contains objects
700 var time = r.timeslot;
702 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
703 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
706 newline.push('remove', node, slot, SPAN); // ioi
707 var line = my_oTable.fnAddData(newline);
708 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
709 nTr.className = 'remove';
712 this.current_resources = $.merge(r_attached,r_added);
714 /* Allow the user to update the slice */
715 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
717 }, // update_resources
721 $.plugin('QueryUpdater', QueryUpdater);