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

Fix trends back/ahead result selection logic #132

Merged
merged 10 commits into from
Mar 30, 2021
36 changes: 22 additions & 14 deletions neoload/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def parse_source_data_spec(json_in, model, report_type, name):
if json_in is not None:
return json.loads(get_file_text(json_in))

# no in file, so go out to source live
if report_type == "single":
return get_single_report(name,model["components"],filter_spec["time_filter"],filter_spec["elements_filter"],filter_spec["exclude_filter"])
elif report_type == "trends":
Expand Down Expand Up @@ -491,6 +490,11 @@ def get_trends_report(name, time_filter, results_filter, elements_filter, exclud

all_transactions = []

logging.debug("Settled on {} result(s):\n{}".format(
len(arr_selected),
"\n".join(list(map(lambda r: " - "+r["id"],arr_selected))))
)

fill_trend_results(arr_selected, all_transactions, elements_filter, time_filter)

all_transaction_names = list(map(lambda t: t["name"], all_transactions))
Expand Down Expand Up @@ -562,15 +566,11 @@ def get_trend_count_back_ahead(arr_directives):

def get_trends_selected_results(arr_ids,count_back,count_ahead):
base_id = arr_ids[0]["id"]
logging.debug('base_id: {}'.format(base_id))
arr_results = get_results_by_result_id(base_id,count_back,count_ahead)
logging.debug('selected results: {}'.format(arr_results))

arr_sorted_by_time = list(sorted(arr_results, key=lambda x: x["startDate"]))
base_index = list(map(lambda x: x["id"],arr_sorted_by_time)).index(base_id)
arr_selected = []
logging.debug('base_index: {}'.format(base_index))
logging.debug('count_ahead: {}'.format(count_ahead))
logging.debug('count_back: {}'.format(count_back))

for i in range(base_index,base_index+1+count_ahead):
if 0 < i < len(arr_sorted_by_time):
Expand All @@ -579,6 +579,17 @@ def get_trends_selected_results(arr_ids,count_back,count_ahead):
if 0 < i < len(arr_sorted_by_time):
arr_selected.append(arr_sorted_by_time[i])

for id in arr_ids:
if id not in list(map(lambda r: r["id"],arr_selected)):
results = get_results_by_result_id(id["id"],0,0)
arr_selected = arr_selected + results

arr_final = []
for result in arr_selected:
if result["id"] not in list(map(lambda r: r["id"],arr_final)):
arr_final.append(result)
arr_selected = arr_final

arr_selected = list(sorted(arr_selected, key=lambda x: x["startDate"]))
for i in range(0,len(arr_selected)):
arr_selected[i]["index"] = i
Expand Down Expand Up @@ -710,13 +721,8 @@ def get_results_by_result_id(__id,count_back,count_ahead):
result = rest_crud.get(get_end_point(__id))
project = result["project"]
scenario = result["scenario"]
logging.debug({'project':project,'scenario':scenario})
total_expected = -count_back + 1 + count_ahead
results = []
logging.debug("based_id: {}".format(__id))
logging.debug("total_expected: {}".format(total_expected))
logging.debug("count_back: {}".format(count_back))
logging.debug("count_ahead: {}".format(count_ahead))

page_size = 200
params = {
Expand All @@ -742,7 +748,6 @@ def get_results_by_result_id(__id,count_back,count_ahead):
params['offset'] += page_size

arr_sorted_by_time = list(sorted(all_entities, key=lambda x: x["startDate"]))

results = compile_results_from_source(__id, arr_sorted_by_time, count_back, count_ahead)

if ret_count < page_size:
Expand All @@ -756,10 +761,13 @@ def compile_results_from_source(base_id, all_entities, count_back, count_ahead):
base_index = list(map(lambda x: x["id"],all_entities)).index(base_id)
back_index = base_index+count_back
ahead_index = base_index+count_ahead
#print({'base_index':base_index,'back_index':back_index,'ahead_index':ahead_index})

for i in range(max(back_index,0),base_index+1):
ret.append(all_entities[i])
for i in range(base_index+1,min(ahead_index,len(all_entities)-1)):
ahead_begin = base_index+1
ahead_end = min(ahead_index,len(all_entities)-1)

for i in range(ahead_begin,ahead_end+1):
ret.append(all_entities[i])
return ret

Expand Down
10 changes: 9 additions & 1 deletion tests/commands/report/test_trends_json_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from helpers.test_utils import assert_success
from neoload_cli_lib import rest_crud

import os

@pytest.mark.report
@pytest.mark.usefixtures("neoload_login") # it's like @Before on the neoload_login function
Expand All @@ -34,8 +35,15 @@ def test_parse_source_data_spec(self, monkeypatch, datafiles):

with open('tests/resources/report/actual_trends.json', 'w', newline='\n') as f:
f.write(json.dumps(json_data, indent=2))

msg = ""
statinfo = os.stat(datafiles.listdir()[0])
msg += "{}".format({ 'statinfo': statinfo, 'st_size': statinfo.st_size })
statinfo = os.stat('tests/resources/report/actual_trends.json')
msg += "{}".format({ 'statinfo': statinfo, 'st_size': statinfo.st_size })

assert filecmp.cmp(datafiles.listdir()[0],
'tests/resources/report/actual_trends.json') is True, "Json output for the report (file tests/resources/report/actual_trends.json) is not the one expected (file tests/resources/report/expected_trends.json)"
'tests/resources/report/actual_trends.json') is True, "Json output for the report (file tests/resources/report/actual_trends.json) is not the one expected (file tests/resources/report/expected_trends.json)" + msg

def __return_json(self, endpoint):
if endpoint == 'v3/workspaces/5f689c3f0860270001606902/test-results/8725e0cd-92a4-4105-803b-86433851fcfc':
Expand Down