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 QueryUpdaterPlugin = Plugin.extend({
31 /**************************************************************************
33 **************************************************************************/
35 init: function(options, element) {
36 this.classname="queryupdater";
37 this._super(options, element);
41 this.initial = Array();
42 this.selected_resources = Array();
44 this.table = this.elmt('table').dataTable({
45 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
49 this.elmt('update').click(this, this.do_ok);
50 this.elmt('refresh').click(this, this.do_cancel);
52 this.listen_query(options.query_uuid);
55 /*************************** PLUGIN EVENTS ****************************/
57 /***************************** GUI EVENTS *****************************/
59 do_checksla: function(e) {
60 var username = e.data.options.username;
62 var arraySelectedResources = data.selected_resources;
64 var accepted_sla = [];
67 // var testbedsWithSLA = ["iminds", "fuseco", "netmode"];
70 var sliverPattern = /IDN\+(.+)\+(node|channel)\+/;
73 var promt = $('#sla-table-body');
75 $.get("/sla/testbeds/", function(data) {
76 testbedsWithSLA = data;
78 console.log("Testbeds with SLA: " + testbedsWithSLA);
80 $(arraySelectedResources).each(function () {
81 var sliverMatch = sliverPattern.exec(this);
82 var sliverId = sliverMatch[1];
83 for (var i = 0; i < testbedsWithSLA.length; i++) {
84 if(this.indexOf(testbedsWithSLA[i].toLowerCase()) >= 0){ // If it has SLA
85 if (list.indexOf(sliverId) == -1) { // If it is not in the list
92 if (list.length > 0) {
93 for (var i = 0; i < list.length; i++) {
94 var element = $('<tr>');
96 $('<td>').append(list[i]),
97 $('<td>').append('99% of Uptime for 99% of resources'),
98 $('<td align="center">').append('<input type="checkbox" name="slaaccept" value="' + list[i] + '"/> <br />')
100 promt.append(element);
103 $('#sla_dialog').show();
104 $('#slamodal').modal('show');
106 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
111 $("#submit_sla").unbind().click(function(){
112 console.log("With username: " + username);
114 var notChecked = $("input[name='slaaccept']:not(:checked)");
116 if (notChecked.length > 0) {
117 for (var i = 0; i < notChecked.length; i++) {
118 console.log("SLA not accepted: " + notChecked[i].value);
121 alert("All SLAs have to be accepted to continue with the reservation");
124 // $(list).each(function () {
125 // var date = new Date();
126 // date.setYear(date.getFullYear() + 1);
128 // $.post("/sla/agreements/simplecreate",
129 // { "template_id": this.toString(),
131 // "expiration_time": date.toISOString()
138 url: "/sla/agreements/simplecreate",
139 data: { testbeds: list,
141 resources: arraySelectedResources,
142 slice: main_query.filters.slice()[0][2]
148 console.log(main_query.filters.slice()[0][2]);
150 $('#slamodal').modal('hide');
151 $('#sla-table-body').empty();
152 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
156 $("#cancel_sla").unbind().click(function(){
157 $('#slamodal').modal('hide');
158 $('#sla-table-body').empty();
161 /************************** GUI MANIPULATION **************************/
163 populate_table: function()
167 // Loop over records and display pending ones
168 manifold.query_store.iter_records(this.options.query_uuid, function (record_key, record) {
169 state = manifold.query_store.get_record_state(this.options.query_uuid, null, STATE_SET);
174 set_button_state: function(name, state)
176 this.elmt(name).attr('disabled', state ? false : 'disabled');
184 find_row: function(key)
186 // key in third position, column id = 2
189 var cols = $.grep(this.table.fnSettings().aoData, function(col) {
190 return (col._aData[KEY_POS] == key);
193 if (cols.length == 0)
196 throw "Too many same-key rows in ResourceSelected plugin";
201 do_update: function(e) {
204 var username = e.data.options.username;
205 var urn = data.value;
206 // XXX check that the query is not disabled
209 console.log("do_update");
210 // XXX check that the query is not disabled
211 //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
213 // how to stop the spinning after the event?
214 // this should be triggered by some on_updatequery_done ?
220 throw 'queryupdater.do_reset Not implemented';
223 do_cancel: function(e)
225 throw 'queryupdater.do_clear_annotations Not implemented';
228 /**************************************************************************
230 **************************************************************************/
232 on_new_record: function(record)
235 // if (not and update) {
237 // initial['resource'], initial['lease'] ?
238 this.initial.push(record);
240 //this.set_record_state(record, RECORD_STATE_ATTACHED);
241 // We simply add to the table
243 // \ this.initial_resources
246 // current_resources \ YES | NO
247 // --------------------+----------+---------
248 // YES | attached | added
256 // +-----------------+--------------+
258 // +-------+ ?G +-------+ +-------+ +---+---+ |
259 // | | -----> | | !G | | | | DA |
260 // | ND | | PG | -----> | D | -----> | PC | <------+ |
261 // | | <----- | | ~G | | C | | | |
262 // +-------+ GE +-------+ +-------+ +-------+ +------+
264 // | DA UE | | ?U | PCA |
266 // +-------+ +-------+ +------+
268 // | AD | <----- | PU | --------+
270 // +-------+ +-------+
275 // Plugins (i) receive state information, (ii) perform actions
278 // ND : No data ?G : Get query
279 // PG : Pending Get !G : Get reply
280 // D : Data present ~G : Get partial reply
281 // PC : Pending changes GE : Get error
282 // PU : Pending update C : Change request
283 // PCA: Pending change with annotation R : Reset request
284 // AD : Annotated data ?U : Update query
286 // ~U : Update partial reply
288 // DA : Delete annotation request
290 // - D -> PU directly if the user chooses 'dynamic update'
291 // - Be careful for updates if partial Get success
293 // ND: No data == initialization state
296 // - on_query_in_progress
297 // NOTE: cannot distinguish get and update here. Is it important ?
299 on_query_in_progress: function()
304 on_query_done: function()
306 this.populate_table();
311 // - on_clear_records (Get)
312 // - on_new_record (shared with AD) XXX
314 // NOTE: cannot distinguish get and update here. Is it important ?
315 // NOTE: Would record key be sufficient for update ?
317 on_clear_records: function()
322 on_query_done: function()
327 // PC : Pending changes
328 // NOTE: record_key could be sufficient
329 on_added_record: function(record)
331 this.set_record_state(record, RECORD_STATE_ADDED);
332 // update pending number
335 on_removed_record: function(record_key)
337 this.set_record_state(RECORD_STATE_REMOVED);
340 // PU : Pending update
341 // - on_query_in_progress (already done)
343 // PCA : Pending change with annotation
344 // NOTE: Manifold will inform the plugin about updates, and thus won't
345 // call new record, even if the same channel UUID is used...
346 // - TODO on_updated_record
347 // - Key and confirmation could be sufficient, or key and record state
348 // XXX move record state to the manifold plugin API
350 on_field_state_changed: function(data)
353 if(result.request == FIELD_REQUEST_ADD){
354 this.selected_resources.push(result.value);
355 } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
356 var i = this.selected_resources.indexOf(result.value);
358 this.selected_resources.splice(i,1);
361 this.set_state(result);
364 var action, msg, row, status, button = '';
369 // XXX other events missing !!
370 case STATE_VALUE_CHANGE_PENDING:
378 case STATE_SET_IN_PENDING:
381 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
384 case STATE_SET_OUT_PENDING:
387 button = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='" + data.key + "'/>";
392 // find line and delete it
393 row = this.find_row(data.value);
395 this.table.fnDeleteRow(row.nTr);
398 case STATE_SET_IN_SUCCESS:
399 case STATE_SET_OUT_SUCCESS:
403 case STATE_SET_IN_FAILURE:
404 case STATE_SET_OUT_FAILURE:
415 status = msg + status;
418 // if no, create it, else replace it
419 // XXX it's not just about adding lines, but sometimes removing some
420 // XXX how do we handle status reset ?
422 // Jordan : I don't understand this. I added this test otherwise we have string = ""..."" double quoted twice.
423 if (typeof(data.value) !== "string")
424 data.value = JSON.stringify(data.value);
425 data.selected_resources = this.selected_resources;
426 row = this.find_row(data.value);
427 newline = [action, data.key, data.value, msg, button];
429 // XXX second parameter refresh = false can improve performance. todo in querytable also
430 this.table.fnAddData(newline);
431 row = this.find_row(data.value);
433 // Update row text...
434 this.table.fnUpdate(newline, row.nTr);
437 // Change cell color according to status
439 $(row.nTr).removeClass('add remove')
440 var cls = action.toLowerCase();
442 $(row.nTr).addClass(cls);
446 // XXX we will have the requests for change
447 // XXX + the requests to move into the query cycle = the buttons aforementioned
449 // XXX what happens in case of updates ? not implemented yet
450 // XXX we want resources and leases
451 // listen for SET_ADD and SET_REMOVE for slice query
453 /************************** PRIVATE METHODS ***************************/
455 _close_click: function(e)
459 //jQuery.publish('selected', 'add/'+key_value);
460 // this.parentNode is <td> this.parentNode.parentNode is <tr>
461 // this.parentNode.parentNode.firstChild is the first cell <td> of this line <tr>
462 // this.parentNode.parentNode.firstChild.firstChild is the text in that cell
463 //var firstCellVal=this.parentNode.parentNode.firstChild.firstChild.data;
464 var remove_urn = this.id;
465 var current_resources = event.data.instance.current_resources;
466 var list_resources = $.grep(current_resources, function(x) {return x.urn != remove_urn});
467 //jQuery.publish('selected', 'cancel/'+this.id+'/'+unfold.get_value(firstCellVal));
468 $.publish('/update-set/' + event.data.instance.options.resource_query_uuid, [list_resources, true]);
471 /******************************** TODO ********************************/
473 update_resources: function(resources, change)
475 messages.debug("update_resources");
476 var my_oTable = this.table.dataTable();
477 var prev_resources = this.current_resources;
480 * The first time the query is advertised, don't do anything. The
481 * component will learn nodes in the slice through the manifest
482 * received through the other subscription
487 var initial = this.initial;
488 //var r_removed = []; //
489 /*-----------------------------------------------------------------------
490 TODO: remove this dirty hack !!!
492 resources = jQuery.map(resources, function(x){
493 if(!('timeslot' in x)){x.timeslot=0;}
497 TODO: handle generic keys instead of specific stuff
501 var initial_urn = $.map(initial, function(x){return x.urn;});
502 var resources_urn = $.map(resources, function(x){return x.urn;});
503 var r_removed = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) == -1 });
504 var r_attached = $.grep(initial, function (x) { return $.inArray(x.urn, resources_urn) > -1 });
505 var r_added = $.grep(resources, function (x) { return $.inArray(x.urn, initial_urn) == -1 });
506 exists = false; // ioi
507 /*-----------------------------------------------------------------------*/
509 my_oTable.fnClearTable();
511 TODO: factorization of this code !!!
513 $.each(r_added, function(i, r) {
514 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
516 // Create the resource objects
521 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+urn+"'/>";
522 var slot = "<span id='resource_"+urn+"'>" + time + "</span>"; //ioi
525 newline.push(type, urn, slot, SPAN); // ioi
526 var line = my_oTable.fnAddData(newline);
527 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
528 nTr.className = type;
530 $.each(r_attached, function(i, r) {
531 //var type = (typeof initial == 'undefined' || r.node != initial.node) ? 'add' : 'attached';
532 var type = 'attached';
533 // Create the resource objects
538 var SPAN = "<span class='glyphicon glyphicon-renomve ResourceSelectedClose' id='"+node+"'/>";
539 var slot = "<span id='resource_"+node+"'>" + time + "</span>"; //ioi
542 newline.push(type, node, slot, SPAN); // ioi
543 var line = my_oTable.fnAddData(newline);
544 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
545 nTr.className = type;
547 $.each(r_removed, function(i, r) {
548 // The list contains objects
551 var time = r.timeslot;
553 var SPAN = "<span class='glyphicon glyphicon-remove ResourceSelectedClose' id='"+node+"'/>";
554 var slot = "<span id='resource_"+node+"'>" + time + "</span>";
557 newline.push('remove', node, slot, SPAN); // ioi
558 var line = my_oTable.fnAddData(newline);
559 var nTr = my_oTable.fnSettings().aoData[ line[0] ].nTr;
560 nTr.className = 'remove';
563 this.current_resources = $.merge(r_attached,r_added);
565 /* Allow the user to update the slice */
566 //jQuery('#updateslice-' + data.ResourceSelected.plugin_uuid).prop('disabled', false);
568 }, // update_resources
572 $.plugin('QueryUpdaterPlugin', QueryUpdaterPlugin);