Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user can edit selected config file #2

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion locust/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
150 changes: 111 additions & 39 deletions locust/static/locust.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 */

Expand Down
44 changes: 42 additions & 2 deletions locust/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
Loading