Cleaned code and improved communication with SLA Collector
[myslice.git] / plugins / queryupdater / static / js / queryupdater.js
index ab6465c..2e73629 100644 (file)
@@ -35,6 +35,7 @@
             var self = this;
 
             this.initial = Array();
+            this.selected_resources = Array();
 
             this.table = this.elmt('table').dataTable({
 // the original querytable layout was
@@ -43,7 +44,7 @@
                 sDom: "<'row'<'col-xs-5'l><'col-xs-1'r><'col-xs-6'f>>t>",
 // so this does not matter anymore now that the pagination area is turned off
 //                sPaginationType: 'bootstrap',
-               bAutoWidth: true,
+                     bAutoWidth: true,
 //                bJQueryUI      : true,
 //                bRetrieve      : true,
 //                sScrollX       : '100%',                 // Horizontal scrolling 
         do_checksla: function(e) {
             var username = e.data.options.username;
             var urn = data.value;
-            // XXX check that the query is not disabled
-
-            console.log("DATA VALUE: " + data.value);
+            var arraySelectedResources = data.selected_resources;
 
-            if (data.value.toLowerCase().indexOf("iminds") >= 0){
+            var accepted_sla = [];
+            var count = 0;
+            var self = e.data;
+            // var testbedsWithSLA = ["iminds", "fuseco", "netmode"];
+            var testbedsWithSLA;
+            
+            var sliverPattern = /IDN\+(.+)\+(node|channel)\+/;
+            var list = [];
 
-                $('#sla_dialog').show();
-                $('#slamodal').modal('show');
-                
-                $(document).ready(function() {
-                    $("#accept_sla").click(function(){
-                        console.log("SLA ACCEPTED");
-                        console.log("With username: " + username);
-                        if(urn.toLowerCase().indexOf("wall2") >= 0){ 
-                            $.post("/sla/agreements/simplecreate", 
-                                { "template_id": "iMindsServiceVirtualwall",
-                                  "user": username,
-                                  "expiration_time": new Date() // jgarcia: FIXME
-                                });
-                        } else if(urn.toLowerCase().indexOf("wilab2") >= 0){
-                            $.post("/sla/agreements/simplecreate", 
-                                { "template_id":"iMindsServiceWiLab2",
-                                  "user":username,
-                                  "expiration_time": new Date() // jgarcia: FIXME
-                                });
+            var promt = $('#sla-table-body');
+            
+            $.get("/sla/testbeds/", function(data) {
+                testbedsWithSLA = data;
+
+                console.log("Testbeds with SLA: " + testbedsWithSLA);
+
+                $(arraySelectedResources).each(function () {
+                    var sliverMatch = sliverPattern.exec(this);
+                    var sliverId = sliverMatch[1];
+                    for (var i = 0; i < testbedsWithSLA.length; i++) {
+                        if(this.indexOf(testbedsWithSLA[i].toLowerCase()) >= 0){ // If it has SLA
+                            if (list.indexOf(sliverId) == -1) { // If it is not in the list
+                                list.push(sliverId);
+                            }
                         }
-                        $('#slamodal').modal('hide');
-                        self.spin();
-                        console.log("Executing do_update after sending SLA");
-                        // XXX check that the query is not disabled
-                        manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
-
-                        // how to stop the spinning after the event? 
-                        // this should be triggered by some on_updatequery_done ?
-                    }); 
+                    }
                 });
 
-                $(document).ready(function() {
-                    $("#dismiss_sla").click(function(){
-                        console.log("SLA NOT ACCEPTED");
-                        $('#slamodal').modal('hide');
-                    }); 
-                });
-
-            } else {
-                self.spin();
-                console.log("do_update");
-                // XXX check that the query is not disabled
-                manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
+                if (list.length > 0) {
+                    for (var i = 0; i < list.length; i++) {
+                        var element = $('<tr>');
+                        element.append(
+                            $('<td>').append(list[i]),
+                            $('<td>').append('99% of Uptime for 99% of resources'),
+                            $('<td align="center">').append('<input type="checkbox" name="slaaccept" value="' + list[i] + '"/> <br />')
+                        );
+                        promt.append(element);
+                    }
+
+                    $('#sla_dialog').show();
+                    $('#slamodal').modal('show');
+                } else {
+                    //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
+                }
+            
+            });
+                    
+            $("#submit_sla").unbind().click(function(){
+                console.log("With username: " + username);
 
-                // how to stop the spinning after the event? 
-                // this should be triggered by some on_updatequery_done ?
-            }
+                var notChecked = $("input[name='slaaccept']:not(:checked)");
+                
+                if (notChecked.length > 0) {
+                    for (var i = 0; i < notChecked.length; i++) {
+                         console.log("SLA not accepted: " + notChecked[i].value);
+                    }    
+
+                    alert("All SLAs have to be accepted to continue with the reservation");
+
+                } else {
+                    // $(list).each(function () {
+                    //     var date = new Date();
+                    //     date.setYear(date.getFullYear() + 1);
+
+                    //     $.post("/sla/agreements/simplecreate", 
+                    //         { "template_id": this.toString(),
+                    //           "user": username,
+                    //           "expiration_time": date.toISOString()
+                    //         });
+
+                        
+                    // });
+
+                    $.ajax({
+                        url: "/sla/agreements/simplecreate", 
+                        data: { testbeds: list,
+                          user: username,
+                          resources: arraySelectedResources,
+                          slice: main_query.filters.slice()[0][2]
+                        },
+                        type: "post",
+                        traditional: true
+                    });
+
+                    console.log(main_query.filters.slice()[0][2]);
+                      
+                    $('#slamodal').modal('hide');
+                    $('#sla-table-body').empty();
+                    //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
+                }
+            }); 
+        
+            $("#cancel_sla").unbind().click(function(){
+                $('#slamodal').modal('hide');
+                $('#sla-table-body').empty();
+            }); 
         },
 
+     
         do_update: function(e) {
             var self = e.data;
 
             var urn = data.value;
             // XXX check that the query is not disabled
 
-            console.log("DATA VALUE: " + data.value);
-
-            if (data.value.toLowerCase().indexOf("iminds") >= 0){
-
-                $('#sla_dialog').show();
-                $('#slamodal').modal('show');
-                
-                $(document).ready(function() {
-                    $("#accept_sla").click(function(){
-                        console.log("SLA ACCEPTED");
-                        console.log("With username: " + username);
-                        if(urn.toLowerCase().indexOf("wall2") >= 0){ 
-                            $.post("/sla/agreements/simplecreate", 
-                                { "template_id": "iMindsServiceVirtualwall",
-                                  "user": username,
-                                  "expiration_time": new Date() // jgarcia: FIXME
-                                });
-                        } else if(urn.toLowerCase().indexOf("wilab2") >= 0){
-                            $.post("/sla/agreements/simplecreate", 
-                                { "template_id":"iMindsServiceWiLab2",
-                                  "user":username,
-                                  "expiration_time": new Date() // jgarcia: FIXME
-                                });
-                        }
-                        $('#slamodal').modal('hide');
-                        self.spin();
-                        console.log("Executing do_update after sending SLA");
-                        // XXX check that the query is not disabled
-                        manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
-
-                        // how to stop the spinning after the event? 
-                        // this should be triggered by some on_updatequery_done ?
-                    }); 
-                });
-
-                $(document).ready(function() {
-                    $("#dismiss_sla").click(function(){
-                        console.log("SLA NOT ACCEPTED");
-                        $('#slamodal').modal('hide');
-                    }); 
-                });
-
-            } else {
-                self.spin();
-                console.log("do_update");
-                // XXX check that the query is not disabled
-                manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
+            self.spin();
+            console.log("do_update");
+            // XXX check that the query is not disabled
+            //manifold.raise_event(self.options.query_uuid, RUN_UPDATE);
 
-                // how to stop the spinning after the event? 
-                // this should be triggered by some on_updatequery_done ?
-            }
+            // how to stop the spinning after the event? 
+            // this should be triggered by some on_updatequery_done ?
 
         },
 
             // XXX it's not just about adding lines, but sometimes removing some
             // XXX how do we handle status reset ?
             data.value = JSON.stringify(data.value);
+            data.selected_resources = this.selected_resources;
             row = this.find_row(data.value);
             newline = [
                 action,
         {
             console.log("on_field_state_changed");
             console.log(result);
+            if(result.request == FIELD_REQUEST_ADD){
+                this.selected_resources.push(result.value);
+            } else if(result.request == FIELD_REQUEST_REMOVE_RESET){
+                var i = this.selected_resources.indexOf(result.value);
+                if(i != -1){
+                    this.selected_resources.splice(i,1);
+                }
+            }
+            console.log("Resources: " + self.selected_resources);
             messages.debug(result)
             /* this.set_state(result.request, result.key, result.value, result.status); */
             this.set_state(result);