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();
82 // XXX check that the query is not disabled
84 console.log("DATA VALUE: " + data.value);
86 //<p>SLA description</p>
87 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>
88 //<p>SLA description</p>
89 //<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>
90 var promt = $('#modal-body');
96 promt.append('<p>SLA description</p>');
100 for(var iter = 0; iter < arrayselectedresources.length; iter++){
101 var list = '<li class="wi'+iter+'" name=wi"'+iter+'">'+arrayselectedresources[iter].toLowerCase()+'</li>';
103 if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
105 accepted_sla.push({"wilab2":false});
112 wilabForm += "</ul>";
114 //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>';
118 for(var iter = 0; iter < arrayselectedresources.length; iter++){
119 var list = '<li class="wall'+iter+'" name=wall"'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'</li>';
121 if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
123 accepted_sla.push({"wall2":false});
132 var flagDouble = false;
136 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the WiLab2 resources during the sliver lifetime</p>');
137 promt.append(wilabForm);
138 promt.append('<br />');
142 //promt.append(wallmessage);
144 promt.append('<p>Testbed guarantees 0.99 Uptime rate for 0.99 rate of the VirtualWall resources during the sliver lifetime</p>');
145 promt.append(wallForm);
146 promt.append('<br />');
150 // 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>'
152 $('#sla_dialog').show();
153 $('#slamodal-wilab2').modal('show');
156 $("#accept_sla_wilab2").click(function(){
157 console.log("SLA ACCEPTED");
158 console.log("With username: " + username);
160 // var promt = $('#modal-body');
161 // var notchecked = true;
162 // for (var i=0;i<50;i++)
164 // var wielement = $('#wi'+i);
165 // var wallElement = $('#wall'+i);
166 // if(wielement != null && !wielement.checked)
168 // notchecked = false;
170 // if(wallElement!= null && !wallElement.checked)
172 // notchecked = false;
179 $.post("/sla/agreements/simplecreate",
180 { "template_id": "iMindsServiceWiLab2",
182 "expiration_time": new Date().toISOString()
184 $.post("/sla/agreements/simplecreate",
185 { "template_id": "iMindsServiceVirtualwall",
187 "expiration_time": new Date().toISOString()
190 $('#slamodal-wilab2').modal('hide');
191 accepted_sla["wilab2"] = true;
197 $("#dismiss_sla_wilab2").click(function(){
198 console.log("SLA NOT ACCEPTED");
199 $('#slamodal-wilab2').modal('hide');
202 // for(var iter = 0; iter < arrayselectedresources.length; iter++){
203 // var list = '<input type="checkbox" name="'+iter+'" >'+arrayselectedresources[iter].toLowerCase()+'<br>';
204 // promt.append(list);
205 // if (arrayselectedresources[iter].toLowerCase().indexOf("wall2") >= 0){
209 // accepted_sla.push({"wall2":false});
211 // $('#sla_dialog').show();
212 // $('#slamodal-virtualwall').modal('show');
215 // $("#accept_sla_vwall").click(function(){
216 // console.log("SLA ACCEPTED");
217 // console.log("With username: " + username);
219 // $.post("/sla/agreements/simplecreate",
220 // { "template_id": "iMindsServiceVirtualwall",
222 // "expiration_time": new Date()
225 // $('#slamodal-virtualwall').modal('hide');
226 // accepted_sla["wall2"] = true;
229 // $("#dismiss_sla_vwall").click(function(){
230 // console.log("SLA NOT ACCEPTED");
231 // $('#slamodal-vir').modal('hide');
236 // if (arrayselectedresources[iter].toLowerCase().indexOf("wilab2") >= 0){
238 // accepted_sla.push({"wilab2":false});
240 // $('#sla_dialog').show();
241 // $('#slamodal-wilab2').modal('show');
244 // $("#accept_sla_wilab2").click(function(){
245 // console.log("SLA ACCEPTED");
246 // console.log("With username: " + username);
248 // $.post("/sla/agreements/simplecreate",
249 // { "template_id": "iMindsServiceWiLab2",
251 // "expiration_time": new Date()
254 // $('#slamodal-wilab2').modal('hide');
255 // accepted_sla["wilab2"] = true;
258 // $("#dismiss_sla_wilab2").click(function(){
259 // console.log("SLA NOT ACCEPTED");
260 // $('#slamodal-wilab2').modal('hide');
268 // for(var sla in accepted_sla){
269 // if(accepted_sla[sla] == true){
274 // if(count == accepted_sla.length){
275 // this.do_update(e);
280 do_update: function(e) {
283 var username = e.data.options.username;
284 var urn = data.value;
285 // XXX check that the query is not disabled
288 console.log("do_update");
289 // XXX check that the query is not disabled
290 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
292 // how to stop the spinning after the event?
293 // this should be triggered by some on_updatequery_done ?
297 // related buttons are also disabled in the html template
298 do_refresh: function(e)
300 throw 'resource_selected.do_refresh Not implemented';
303 do_reset: function(e)
305 throw 'queryupdater.do_reset Not implemented';
308 do_clear_annotations: function(e)
310 throw 'queryupdater.do_clear_annotations Not implemented';
313 /************************** GUI MANIPULATION **************************/
316 set_button_state: function(name, state)
318 this.elmt(name).attr('disabled', state ? false : 'disabled');
326 find_row: function(key)
328 // key in third position, column id = 2
331 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
332 return (col._aData[KEY_POS] == key);
335 if (cols.length == 0)
338 throw "Too many same-key rows in ResourceSelected plugin";
343 set_state: function(data)
345 console.log("function set_state");
352 // make sure the change is visible : toggle on the whole plugin
353 // this might have to be made an 'auto-toggle' option of this plugin..
354 // also it might be needed to be a little finer-grained here
356 // XXX we don't want to show automaticaly the pending when a checkbox is checked
359 switch(data.request) {
360 case FIELD_REQUEST_ADD_RESET:
361 case FIELD_REQUEST_REMOVE_RESET:
362 // find line and delete it
363 row = this.find_row(data.value);
365 this.table.fnDeleteRow(row.nTr);
366 $("#badge-pending").data('number', $("#badge-pending").data('number') - 1 );
367 $("#badge-pending").text($("#badge-pending").data('number'));
369 case FIELD_REQUEST_CHANGE:
372 case FIELD_REQUEST_ADD:
375 case FIELD_REQUEST_REMOVE:
380 switch(data.status) {
381 case FIELD_REQUEST_PENDING:
383 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
385 case FIELD_REQUEST_SUCCESS:
388 case FIELD_REQUEST_FAILURE:
393 var status = msg + status;
398 // if no, create it, else replace it
399 // XXX it's not just about adding lines, but sometimes removing some
400 // XXX how do we handle status reset ?
401 data.value = JSON.stringify(data.value);
402 data.selected_resources = this.selected_resources;
403 row = this.find_row(data.value);
412 // XXX second parameter refresh = false can improve performance. todo in querytable also
413 this.table.fnAddData(newline);
414 row = this.find_row(data.value);
415 $("#badge-pending").data('number', $("#badge-pending").data('number') + 1 );
416 $("#badge-pending").text($("#badge-pending").data('number'));
418 // Update row text...
419 this.table.fnUpdate(newline, row.nTr);
422 // Change cell color according to status
424 $(row.nTr).removeClass('add remove')
425 var cls = action.toLowerCase();
427 $(row.nTr).addClass(cls);
431 /*************************** QUERY HANDLER ****************************/
435 /*************************** RECORD HANDLER ***************************/
437 on_new_record: function(record)
439 console.log("query_updater on_new_record");
442 // if (not and update) {
444 // initial['resource'], initial['lease'] ?
445 this.initial.push(record);
447 //this.set_record_state(record, RECORD_STATE_ATTACHED);
448 // We simply add to the table
450 // \ this.initial_resources
453 // current_resources \ YES | NO
454 // --------------------+----------+---------
455 // YES | attached | added
463 // +-----------------+--------------+
465 // +-------+ ?G +-------+ +-------+ +---+---+ |
466 // | | -----> | | !G | | | | DA |
467 // | ND | | PG | -----> | D | -----> | PC | <------+ |
468 // | | <----- | | ~G | | C | | | |
469 // +-------+ GE +-------+ +-------+ +-------+ +------+
471 // | DA UE | | ?U | PCA |
473 // +-------+ +-------+ +------+
475 // | AD | <----- | PU | --------+
477 // +-------+ +-------+
482 // Plugins (i) receive state information, (ii) perform actions
485 // ND : No data ?G : Get query
486 // PG : Pending Get !G : Get reply
487 // D : Data present ~G : Get partial reply
488 // PC : Pending changes GE : Get error
489 // PU : Pending update C : Change request
490 // PCA: Pending change with annotation R : Reset request
491 // AD : Annotated data ?U : Update query
493 // ~U : Update partial reply
495 // DA : Delete annotation request
497 // - D -> PU directly if the user chooses 'dynamic update'
498 // - Be careful for updates if partial Get success
500 // ND: No data == initialization state
503 // - on_query_in_progress
504 // NOTE: cannot distinguish get and update here. Is it important ?
506 on_query_in_progress: function()
508 messages.debug("queryupdater.on_query_in_progress");
513 // - on_clear_records (Get)
514 // - on_new_record (shared with AD) XXX
516 // NOTE: cannot distinguish get and update here. Is it important ?
517 // NOTE: Would record key be sufficient for update ?
519 on_clear_records: function()
524 on_query_done: function()
526 console.log("on_query_done");
530 // PC : Pending changes
531 // NOTE: record_key could be sufficient
532 on_added_record: function(record)
534 console.log("on_added_record = ",record);
535 this.set_record_state(record, RECORD_STATE_ADDED);
536 // update pending number
539 on_removed_record: function(record_key)
541 console.log("on_removed_record = ",record_key);
542 this.set_record_state(RECORD_STATE_REMOVED);
545 // PU : Pending update
546 // - on_query_in_progress (already done)
548 // PCA : Pending change with annotation
549 // NOTE: Manifold will inform the plugin about updates, and thus won't
550 // call new record, even if the same channel UUID is used...
551 // - TODO on_updated_record
552 // - Key and confirmation could be sufficient, or key and record state
553 // XXX move record state to the manifold plugin API
555 on_field_state_changed: function(result)
557 console.log("on_field_state_changed");
559 if(result.request == FIELD_REQUEST_ADD){
560 this.selected_resources.push(result.value);
561 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
562 var i = this.selected_resources.indexOf(result.value);
564 this.selected_resources.splice(i,1);
567 console.log("Resources: " + self.selected_resources);
568 messages.debug(result)
569 /* this.set_state(result.request, result.key, result.value, result.status); */
570 this.set_state(result);
573 // XXX we will have the requests for change
574 // XXX + the requests to move into the query cycle = the buttons aforementioned
576 // XXX what happens in case of updates ? not implemented yet
577 // XXX we want resources and leases
578 // listen for SET_ADD and SET_REMOVE for slice query
580 /************************** PRIVATE METHODS ***************************/
582 _close_click: function(e)
586 //jQuery.publish('selected', 'add/'+key_value);
587 // this.parentNode is <td> this.parentNode.parentNode is <tr>
588 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
589 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
590 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
591 var remove_urn = this.id;
592 var current_resources = event.data.instance.current_resources;
593 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
594 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
595 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
598 /******************************** TODO ********************************/
600 update_resources: function(resources, change)
602 messages.debug("update_resources");
603 var my_oTable = this.table.dataTable();
604 var prev_resources = this.current_resources;
607 * The first time the query is advertised, don't do anything. The
608 * component will learn nodes in the slice through the manifest
609 * received through the other subscription
614 var initial = this.initial;
615 //var r_removed = []; //
616 /*-----------------------------------------------------------------------
617 TODO: remove this dirty hack !!!
619 resources = jQuery.map(resources, function(x){
620 if(!('timeslot' in x)){x.timeslot=0;}
624 TODO: handle generic keys instead of specific stuff
628 var initial_urn = $.map(initial, function(x){return x.urn;});
629 var resources_urn = $.map(resources, function(x){return x.urn;});
630 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
631 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
632 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
633 exists = false; // ioi
634 /*-----------------------------------------------------------------------*/
636 my_oTable.fnClearTable();
638 TODO: factorization of this code !!!
640 $.each(r_added, function(i, r) {
641 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
643 // Create the resource objects
648 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
649 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
652 newline.push(type, urn, slot, SPAN); // ioi
653 var line = my_oTable.fnAddData(newline);
654 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
655 nTr.className = type;
657 $.each(r_attached, function(i, r) {
658 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
659 var type = 'attached';
660 // Create the resource objects
665 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
666 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
669 newline.push(type, node, slot, SPAN); // ioi
670 var line = my_oTable.fnAddData(newline);
671 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
672 nTr.className = type;
674 $.each(r_removed, function(i, r) {
675 // The list contains objects
678 var time = r.timeslot;
680 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
681 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
684 newline.push('remove', node, slot, SPAN); // ioi
685 var line = my_oTable.fnAddData(newline);
686 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
687 nTr.className = 'remove';
690 this.current_resources = $.merge(r_attached,r_added);
692 /* Allow the user to update the slice */
693 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
695 }, // update_resources
699 $.plugin('QueryUpdater', QueryUpdater);