diff --git a/locust/configuration.py b/locust/configuration.py index 0eef6d1396..f7f974771f 100644 --- a/locust/configuration.py +++ b/locust/configuration.py @@ -5,7 +5,7 @@ from flask import make_response logger = logging.getLogger(__name__) -CONFIG_PATH = '/tests/settings/config.json' +CONFIG_PATH = '/squads/config.json' class ClientConfiguration: """ @@ -68,6 +68,20 @@ def update_json_config(self, json_added, json_path, options, list_column, config return make_response(json.dumps({'success':True, 'data':json.dumps(data, indent=4)})) + def collect_config_path(self, os_path, working_dir): + """ + Collect configuration files path (files with .json extension) under specified path and return it. + """ + collected = dict() + for root, dirs, files in os.walk(os_path+working_dir): + if files: + for file_ in files: + if file_.endswith('.json'): + full_path = os.path.abspath(os.path.join(root, file_)) + truncated_path = full_path.replace(os_path+working_dir, "") + collected.update({full_path.replace(os_path, ""):full_path.replace(os_path+working_dir, "")}) + return collected + @classmethod def get_path(self, match): """ diff --git a/locust/static/locust.js b/locust/static/locust.js index c9ec284c67..cbaea37dd1 100644 --- a/locust/static/locust.js +++ b/locust/static/locust.js @@ -148,32 +148,25 @@ var old_config; $(".edit_config_link").click(function(event) { event.preventDefault(); - try{ - $.ajax({ - type: "GET", - url: "/config/get_config_content", - success: function(response){ - old_config = JSON.parse(response.data) - json_editor.set(old_config); - $("#hidden_config_json").val(response.data); - $("#start").hide(); - $("#ramp").hide(); - $("#edit_config").show(); - $(".status").addClass("none"); - $("#config_tab").trigger("click"); - } - }); - } - catch(err){ - alert("Failed to load configuration data.\n\nOriginal error message:\n" + err); - } - + loadConfig(); }); $("#directories .select2").select2({ placeholder: "Select a state" }); +$('#load_config_btn').click(function(){ + event.preventDefault(); + if(checkConfigChanges()) { + $("#not_save_json_btn").attr("data-origin-link", "load config"); + $("#save_json_btn").attr("data-origin-link", "load config"); + $("#modal_confirm_save_json").modal(); + } + else{ + loadConfig(); + } +}) + let whichform = $('.upload_file_form_test_file')[0]; $('#upload_py_submit').click(function(event){ @@ -182,11 +175,11 @@ $('#upload_py_submit').click(function(event){ $('.upload_file_form_test_file').submit(); }); -$('#upload_json_submit').click(function(event){ - event.preventDefault(); - whichform = $('.upload_file_form_json')[0]; - $('.upload_file_form_json').submit(); -}); +// $('#upload_json_submit').click(function(event){ +// event.preventDefault(); +// whichform = $('.upload_file_form_json')[0]; +// $('.upload_file_form_json').submit(); +// }); $('.upload_file_form_test_file, .upload_file_form_json').submit(function(event) { event.preventDefault(); @@ -209,7 +202,7 @@ $('.upload_file_form_test_file, .upload_file_form_json').submit(function(event) }) }); -$('#submit_json_btn').click(function(){ +$('#save_config_btn').click(function(){ event.preventDefault(); $('#hidden_config_json').val(JSON.stringify(json_editor.get(), null , 4)); $('#json_config_form').submit(); @@ -325,8 +318,9 @@ $('#convert_csv_btn').click(function(){ $(".config_new_test").click(function(event) { event.preventDefault(); - if(JSON.stringify(old_config,null,4) != JSON.stringify(json_editor.get(),null,4)) { + if(checkConfigChanges()) { $("#not_save_json_btn").attr("data-origin-link", "new test"); + $("#save_json_btn").attr("data-origin-link", "new test"); $("#modal_confirm_save_json").modal(); } else { @@ -340,8 +334,9 @@ $(".config_new_test").click(function(event) { $(".config_ramp_test").click(function(event) { event.preventDefault(); - if(JSON.stringify(old_config,null,4) != JSON.stringify(json_editor.get(),null,4)) { + if(checkConfigChanges()) { $("#not_save_json_btn").attr("data-origin-link", "new ramp"); + $("#save_json_btn").attr("data-origin-link", "new ramp"); $("#modal_confirm_save_json").modal(); } else { @@ -353,26 +348,103 @@ $(".config_ramp_test").click(function(event) { } }); - +// if user click save button on unsaved configuration warning modal $("#save_json_btn").click(function(event) { - $("#submit_json_btn").trigger("click"); + if($("#save_json_btn").attr("data-origin-link") == "load config"){ + saveConfigWithoutRefresh(); + loadConfig(); + } + else { + $("#save_config_btn").trigger("click"); + } }); +// if user click don't save button on unsaved configuration warning modal $("#not_save_json_btn").click(function(event) { $("#modal_confirm_save_json").modal("hide"); - if($("#not_save_json_btn").attr("data-origin-link") == "new test") { - $("#start").show(); - $("#ramp").hide(); + + if($("#not_save_json_btn").attr("data-origin-link") == "load config"){ + loadConfig(); } - else if($("#not_save_json_btn").attr("data-origin-link") == "new ramp") { - $("#ramp").show(); - $("#start").hide(); + else{ + // if before warning modal appear, user click new test link + if($("#not_save_json_btn").attr("data-origin-link") == "new test") { + $("#start").show(); + $("#ramp").hide(); + } + // if before warning modal appear, user click new ramp link + else if($("#not_save_json_btn").attr("data-origin-link") == "new ramp") { + $("#ramp").show(); + $("#start").hide(); + } + $("#edit_config").hide(); + $("#locust_count").focus().select(); + $(".status").removeClass("none"); } - $("#edit_config").hide(); - $("#locust_count").focus().select(); - $(".status").removeClass("none"); }); +// to display selected config's content into editor +function loadConfig(){ + try{ + $("#modal_loading").modal(); + $.ajax({ + type: "GET", + url: "/config/get_config_content", + data: { + "path": $("#select_config_options option:selected").val() + }, + success: function(response){ + old_config = JSON.parse(response.data) + json_editor.set(old_config); + $("#hidden_config_json").val(response.data); + $("#hidden_config_path").val($("#select_config_options option:selected").val()); + $("#active_config_path").text($("#select_config_options option:selected").text()) + $("#start").hide(); + $("#ramp").hide(); + $("#edit_config").show(); + $(".status").addClass("none"); + }, + complete: function(){ + setTimeout(function(){ + $("#modal_loading").modal("hide"); + },1000); + } + }); + } + catch(err){ + alert("Failed to load configuration data.\n\nOriginal error message:\n" + err); + } +} + +// to check if there are any changes from the configuration content +function checkConfigChanges(){ + return JSON.stringify(old_config,null,4) != JSON.stringify(json_editor.get(),null,4); +} + +// save configuration without refreshing the page +function saveConfigWithoutRefresh(){ + $("#modal_confirm_save_json").modal("hide"); + $('#hidden_config_json').val(JSON.stringify(json_editor.get(), null , 4)); + var form = $('#json_config_form')[0]; + var form_data = new FormData(form); + try{ + $.ajax({ + type: "POST", + url: "/config/save_json", + data: form_data, + enctype: "multipart/form-data", + contentType: false, + cache: false, + processData: false, + success: function(response){ + alert("Changes has been saved"); + } + }); + } + catch(err){ + alert("Failed to save configuration. \n\nOriginal error message:\n" + err); + } +} /* END OF CONFIGURATION SECTION */ diff --git a/locust/static/style.css b/locust/static/style.css index 92994f4096..46551c00b6 100644 --- a/locust/static/style.css +++ b/locust/static/style.css @@ -302,10 +302,50 @@ label[for="json_option"]+div{ padding: 6px 15px 6px 0px !important; } +.select2-results__option--highlighted[aria-selected]{ + color: black !important; +} + #add-new-file-modal .select2-selection--single { width: 100%; } +#load_config_form > .select2-container, #csv_file{ + width: 328px !important; + float: left; + padding-top: 13px; +} + +#load_config_form, #import_csv_form{ + overflow: hidden !important; +} + +#load_config_form > label{ + float: left !important; + width: 190px; +} + +#load_config_btn { + height: 36px; + width: 70px; + margin-left: 5px; + margin-top: 13px; +} + +#import_csv_form > label { + float: left !important; + width: 100px; +} + +#csv_file { + padding-top: 18px; +} + +#import_csv_btn { + margin-top: 18px; +} + + .start button, .edit button, .edit_config button[type=submit], .multiple_column button, #new-test-confirmation .btn-newtest, #upload_json_submit, #upload_py_submit { @@ -325,9 +365,9 @@ label[for="json_option"]+div{ top: -53px; } -#submit_json_btn{ +#save_config_btn{ position: absolute; - top: 8px; + top: 100px; right: 10px; } diff --git a/locust/templates/index.html b/locust/templates/index.html index 96194d03d0..0773a348ab 100644 --- a/locust/templates/index.html +++ b/locust/templates/index.html @@ -81,13 +81,13 @@

Start new Locust swarm

- +

Edit Config @@ -230,7 +230,7 @@

Ramping