Skip to content

Commit

Permalink
Revert "erlangga feature create new json keys (locustio#52)" (locusti…
Browse files Browse the repository at this point in the history
…o#57)

This reverts commit acccdf5.
  • Loading branch information
pancaprima authored Mar 16, 2018
1 parent acccdf5 commit 25c4a65
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 267 deletions.
132 changes: 12 additions & 120 deletions locust/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,145 +28,37 @@ def read_json(self):
self.config_data = json.load({})
return self.config_data

def update_json_config(self, data_json, json_added, json_path, options, list_column):
def update_json_config(self, json_added, json_path, options, list_column, config_text):
"""
Write JSON file configuration
"""
data = literal_eval(config_text)

if(options != "replace"):
json_target = jsonpath_rw_ext.match(json_path, data_json)
json_target = jsonpath_rw_ext.match(json_path, data)
if isinstance(json_target[0], dict):
if list_column==1:
if len(list_column)==1:
json_target[0][list_column[0]] = json_added
json_final = json_target[0]
else:
return make_response(json.dumps({'success':False, 'error_type':'type not match', 'message':'last variable JSONPath type not match with data.'}))
return False, json.dumps(data, indent=4)
else:
for json_target_value in json_target[0]:
json_added.append(json_target_value)
json_final = json_added
else:
json_final = json_added

jsonpath_expr = parse(json_path)
matches = jsonpath_expr.find(data_json)

for match in matches:
data_json = ClientConfiguration.update_json(data_json, ClientConfiguration.get_path(match), json_final)

return make_response(json.dumps({'success':True, 'data':json.dumps(data_json, indent=4)}))

def add_new_key(self, temppath, new_key_type, config_text):
"""
Split the jsonpath and trigger create_path
"""
data = literal_eval(config_text)
splitpath = filter(None, temppath.split('.'))

return self.create_path(data, splitpath, new_key_type, 1)

def check_key(self, input_json, json_path):
"""
Split path and trigger check_exist_path
"""
splitpath = filter(None, json_path.split('.'))
status, message = self.check_exist_path(input_json, splitpath, 1)
return status, message

def create_path(self, input_json, splitpath, type_new_key, index):
"""
Recursively search for jsonpath in json and create not found jsonpath
**there are several to do for checking json, such as :
- check dictionary or list type and trigger next iteration
- create path on json when jsonpath not found until last index
- for the last index, the object will created depend on type_new_key
"""
initial_json = input_json
if type(input_json) is dict and input_json:
if splitpath[index] in input_json:
input_json = input_json[splitpath[index]]
self.create_path(input_json, splitpath, type_new_key, index+1)
elif index == len(splitpath)-1:
if type_new_key == "number":
input_json[splitpath[index]] = 0
elif type_new_key == "object":
input_json[splitpath[index]] = {}
elif type_new_key == "array":
input_json[splitpath[index]] = []
else:
input_json[splitpath[index]] = ""
return
else:
input_json[splitpath[index]] = {}
self.create_path(input_json[splitpath[index]], splitpath, type_new_key, index+1)
matches = jsonpath_expr.find(data)

elif type(input_json) is list and input_json:
for entity in input_json:
self.create_path(entity, splitpath, type_new_key, index)

elif index < len(splitpath)-1:
input_json[splitpath[index]] = {}
self.create_path(input_json[splitpath[index]], splitpath, type_new_key, index+1)
if len(matches)==0:
return make_response(json.dumps({'success':False, 'message':'JSON path not found.'}))

elif index == len(splitpath)-1:
if type_new_key == "number":
input_json[splitpath[index]] = 0
elif type_new_key == "object":
input_json[splitpath[index]] = {}
elif type_new_key == "array":
input_json[splitpath[index]] = []
else:
input_json[splitpath[index]] = ""
return

if index == 1:
if splitpath[index] in input_json:
return input_json
else:
initial_json[splitpath[index]] = input_json
return initial_json

def check_exist_path(self, input_json, splitpath, index):
"""
Recursively check jsonpath
"""
if index > len(splitpath)-1:
return True, "Path exist"
elif type(input_json) is dict and input_json:
if splitpath[index] in input_json:
input_json = input_json[splitpath[index]]
status, message = self.check_exist_path(input_json, splitpath, index+1)
else:
return False, splitpath[index]
elif type(input_json) is list and input_json:
for entity in input_json:
status, message = self.check_exist_path(entity, splitpath, index)
if not status:
break
return status, message

def convert_to_json(self, csv_stream, multiple_data_headers):
"""
Convert csv data to json
"""
if(multiple_data_headers and len(multiple_data_headers) > 0):
tempStr = csv_stream.convert(multiple_data_headers)
json_added = tempStr
else:
tempStr = csv_stream.convert([])
if len(csv_stream.get_columns_name()) > 1:
json_added = tempStr
else:
json_added = tempStr.get(csv_stream.get_columns_name()[0])
for match in matches:
data = ClientConfiguration.update_json(data, ClientConfiguration.get_path(match), json_final)

return json_added

def get_last_variable(self, jsonpath):
"""
Get last variable from jsonpath
"""
splitpath = filter(None, jsonpath.split('.'))
return splitpath[-1]

return make_response(json.dumps({'success':True, 'data':json.dumps(data, indent=4)}))

@classmethod
def get_path(self, match):
Expand Down
67 changes: 2 additions & 65 deletions locust/static/locust.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,54 +265,6 @@ $('#import_csv_btn').click(function(event) {
})
});

$('#abort_validation').click(function(){
event.preventDefault();
$(".multiple_column").hide();
$(".json_validation").hide();
$("#column_name").empty();
document.getElementById("import_csv_form").reset();
document.getElementById("jsonpath").value = "";
document.getElementById("json_option").checked = false;
});

var multiple_data_headers;
var jsonpath;
var json_option;
var config_text;

$('#continue_validation').click(function(event) {
event.preventDefault();
var last_var_type = document.getElementById('last_var_type');
$.ajax({
type : 'POST',
url : '/config/validation/create_new_key',
enctype : 'multipart/form-data',
data : JSON.stringify({'multiple_data_headers': multiple_data_headers ? multiple_data_headers.value : null,
'jsonpath': jsonpath ? jsonpath.value : null, 'options':json_option ? json_option.value : null,
'config_text':config_text ? config_text.value : null, 'last_var_type':last_var_type ? last_var_type.value : null}),
cache : false,
contentType : 'application/json;charset=UTF-8',
processData : false,
success: function(response){
if (response.success) {
try{
json_editor.set(JSON.parse(response.data));
$(".multiple_column").hide();
$('#json_validation').hide();
$("#column_name").empty();
document.getElementById("import_csv_form").reset();
}
catch(err){
alert(err.message);
}
}
else {
alert("Convert error : " + response.message);
}
}
});
});

$('#multiple_column_form').submit(function(event) {
event.preventDefault();
$.post($(this).attr("action"), $(this).serialize(),
Expand All @@ -331,7 +283,7 @@ $('#convert_csv_btn').click(function(){
event.preventDefault();
try{
$("#multiple_hidden_config_json").val(JSON.stringify(json_editor.get(), null , 4));
form = $('#multiple_column_form')[0];
var form = $('#multiple_column_form')[0];
var form_data = new FormData(form);
$.ajax({
type:'POST',
Expand All @@ -354,22 +306,7 @@ $('#convert_csv_btn').click(function(){
}
}
else {
switch(response.error_type) {
case "JSON not found" :
multiple_data_headers = document.getElementById('headers_checkbox');
jsonpath = document.getElementById('jsonpath');
json_option = document.querySelector('input[name="json_option"]:checked');
config_text = document.getElementById('multiple_hidden_config_json');

$('#multiple_column').hide();
$('#json_validation').show();
document.getElementById('key_not_found').innerHTML = response.data.missing_element
document.getElementById('jsonpath_label').innerHTML = jsonpath.value
document.getElementById('last_variable_1').innerHTML = document.getElementById('last_variable_2').innerHTML = response.data.last_variable
break;
default :
alert("Convert error : " + response.message);
}
alert("Convert error : " + response.message);
}
}
});
Expand Down
29 changes: 8 additions & 21 deletions locust/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ container_title {
z-index: 2;
}

.multiple_column, .json_validation {
.multiple_column {
display: none;
width: 398px;
position: absolute;
Expand Down Expand Up @@ -213,7 +213,7 @@ container_title {
box-shadow: 0 0 60px rgba(0,0,0,0.3);
}

.start .padder, .edit .padder, .edit_config .padder, .multiple_column .padder, .json_validation .padder {
.start .padder, .edit .padder, .edit_config .padder, .multiple_column .padder {
padding: 30px;
/* padding-top: 0px; */
}
Expand Down Expand Up @@ -242,7 +242,7 @@ label[for="json_option"]+div{
color: red;
}

.multiple_column label, .json_validation label{
.multiple_column label{
font-weight: bold;
font-size: 15px;
}
Expand All @@ -259,7 +259,7 @@ label[for="json_option"]+div{
font-weight: bold;
}

.start h2, .edit h2, .edit_config h2, .title_header, .multiple_column h2, .json_validation h2 {
.start h2, .edit h2, .edit_config h2, .title_header, .multiple_column h2 {
color: #addf82;
font-size: 26px;
font-weight: bold;
Expand All @@ -281,7 +281,7 @@ label[for="json_option"]+div{
font-size: 16px;
}

.start input.val, .edit input.val, .multiple_column input.val, .json_validation input.val {
.start input.val, .edit input.val, .multiple_column input.val {
border: none;
background: #fff;
height: 40px;
Expand Down Expand Up @@ -320,18 +320,6 @@ label[for="json_option"]+div{
cursor: pointer;
}

.json_validation button {
margin: 20px 5px 20px 5px;
float: right;
font-size: 14px;
font-weight: bold;
background: #8adaba;
padding: 8px 30px;
border-style: none;
border-radius: 5px;
cursor: pointer;
}

.ramp button {
position: relative;
top: -53px;
Expand All @@ -346,7 +334,7 @@ label[for="json_option"]+div{
.start button:hover,
.edit button:hover,
.edit_config button:hover,
.multiple_column button:hover, .json_validation button:hover {
.multiple_column button:hover {
background: #74b99d;
}

Expand All @@ -362,7 +350,7 @@ label[for="json_option"]+div{
}

.stopped .edit {display: none;}
.running .edit, .hatching .edit, .multiple_column, .json_validation {
.running .edit, .hatching .edit, .multiple_column{
display: none;
border-radius: 5px;
-moz-border-radius: 5px;
Expand All @@ -388,8 +376,7 @@ label[for="json_option"]+div{
.running a.new_test {display: none;}
.stopped a.new_test {display: block;}


.start a.close_link, .edit a.close_link, .multiple_column a.close_link_headers, .json_validation a.close_link_headers {
.start a.close_link, .edit a.close_link, .multiple_column a.close_link_headers {
position: absolute;
right: 10px;
top: 10px;
Expand Down
28 changes: 0 additions & 28 deletions locust/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,8 @@ <h2>Import CSV</h2>
<label for="jsonpath" title="JSON path input">JSON Path</label>
<input type="text" name="jsonpath" id="jsonpath" class="val" required/>
<small>Example : $.production.accounts (data belongs to accounts section, where accounts belongs to production section)</small>
<br>
</div>
<br>

<div class="required_field">
<label for="json_option" title="Choose desired action for current data">Action</label>
<div>
Expand All @@ -306,32 +304,6 @@ <h2>Import CSV</h2>
</div>
</div>

<div class="json_validation" id="json_validation" style="display:none;">
<div class="padder">
<h2>JSON Path Validation</h2><br>
<form action="/config/validation/create_new_key" method="POST" id="json_validation_form">
Key <label id="key_not_found" >lorem</label> not found in <label id="jsonpath_label">ipsum</label>. Select <label id="last_variable_1">dolor</label> type to create new key
<br><br>
<div id="type_variable" name="type_variable">
<label for="new_column" title="JsonPath as new column">Select type of variable &nbsp;</label><label id="last_variable_2">amet</label><br>
<div>
<select id="last_var_type" name="last_var_type">
<option value="object">Object</option>
<option value="string">String</option>
<option value="array">Array</option>
<option value="number">Number</option>
</select>
</div>
<br>
</div>
<div>
<button id="continue_validation">Continue</button>
<button id="abort_validation">Abort</button>
</div>
</form>
<div style="clear:right;"></div>
</div>
</div>

<div class="status" id="status">
<nav class="menu">
Expand Down
Loading

0 comments on commit 25c4a65

Please sign in to comment.