Skip to content

Commit

Permalink
Remove complexity in reports generation - sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
MilanPospisil committed Feb 4, 2025
1 parent 2d50933 commit 90fbbed
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 45 deletions.
103 changes: 61 additions & 42 deletions metrics_utility/test/snapshot_tests/CCSP/CCSP_snapshot_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def select_env_vars(dictionary, position):

return values

def generate():
entry_point_dir = snapshot_utils.get_entry_point_directory()

def get_data():
months = ['2024-02', '2024-03', '2024-04']
since_until_pairs = [
{'since': '2024-02-01', 'until': '2024-02-29'},
Expand Down Expand Up @@ -55,7 +53,6 @@ def generate():
'METRICS_UTILITY_REPORT_RHN_LOGIN': ['test_login', 'admin_user', 'guest_user'],
}


# Create env_vars_CCSPv2 by copying base_dict and extending it
env_vars_ccspv2 = copy.deepcopy(base_dict)
env_vars_ccspv2.update(
Expand All @@ -73,60 +70,82 @@ def generate():
env_vars_ccsp.update(
{
'METRICS_UTILITY_REPORT_COMPANY_BUSINESS_LEADER': ['BUSINESS LEADER', 'DIRECTOR', 'CEO'],
'METRICS_UTILITY_REPORT_COMPANY_PROCUREMENT_LEADER': ['PROCUREMENT LEADER', 'PURCHASING HEAD', 'SUPPLY MANAGER'],
'METRICS_UTILITY_REPORT_COMPANY_PROCUREMENT_LEADER': [
'PROCUREMENT LEADER',
'PURCHASING HEAD',
'SUPPLY MANAGER'
],
}
)

# generate tests for both reports
for report_type in ['CCSP', 'CCSPv2']:
custom_params = {'run_command': 'Yes', 'generated': datetime.now().date().strftime('%Y-%m-%d')}
return (months, since_until_pairs, env_vars_ccspv2, env_vars_ccsp)

dictionary = None
if report_type == 'CCSPv2':
dictionary = env_vars_ccspv2
def generate_monthly_reports(report_type, months, dictionary, custom_params, entry_point_dir):
for month in months:
# Default to first set of environment vars
env_vars = select_env_vars(dictionary, 0)

if report_type == 'CCSP':
dictionary = env_vars_ccsp
# For specific month, use the third set of environment vars
if month == '2024-04':
env_vars = select_env_vars(dictionary, 2)

env_vars['METRICS_UTILITY_REPORT_TYPE'] = report_type

path = entry_point_dir + f'/data/{report_type}/snapshot_def_{month}.json'
data = {
'env_vars': env_vars,
'params': ['manage.py', 'build_report', f'--month={month}', '--force'],
'custom_params': custom_params
}
snapshot_utils.save_snapshot_definition(data, path)

# monthly reports
for month in months:
def generate_since_until_reports(report_type, since_until_pairs, dictionary, custom_params, entry_point_dir):
for pair in since_until_pairs:
# Default to second set of environment vars
env_vars = select_env_vars(dictionary, 1)

# Adjust for these specific date ranges
if pair['since'] == '2024-02-01' and pair['until'] == '2024-02-29':
env_vars = select_env_vars(dictionary, 0)
if pair['since'] == '2024-03-01' and pair['until'] == '2024-03-31':
env_vars = select_env_vars(dictionary, 0)

# test also some different env_vars
if month == '2024-04':
env_vars = select_env_vars(dictionary, 2)
env_vars['METRICS_UTILITY_REPORT_TYPE'] = report_type

env_vars['METRICS_UTILITY_REPORT_TYPE'] = report_type
since = pair['since']
until = pair['until']
suffix = since + '--' + until

path = entry_point_dir + f'/data/{report_type}/snapshot_def_{month}.json'
data = {'env_vars': env_vars, 'params': ['manage.py', 'build_report', f'--month={month}', '--force'], 'custom_params': custom_params}
snapshot_utils.save_snapshot_definition(data, path)
path = entry_point_dir + f'/data/{report_type}/snapshot_def_{suffix}.json'
data = {
'env_vars': env_vars,
'params': ['manage.py', 'build_report', f'--since={since}', f'--until={until}', '--force'],
'custom_params': custom_params,
}
snapshot_utils.save_snapshot_definition(data, path)

# reports with arbitrary ranges
for since_until_pair in since_until_pairs:
env_vars = select_env_vars(dictionary, 1)
def generate():
entry_point_dir = snapshot_utils.get_entry_point_directory()

# Those files are going to be compared to months above, so they need to have the same env vars
if since_until_pair['since'] == '2024-02-01' and since_until_pair['until'] == '2024-02-29':
env_vars = select_env_vars(dictionary, 0)
months, since_until_pairs, env_vars_ccspv2, env_vars_ccsp = get_data()

if since_until_pair['since'] == '2024-03-01' and since_until_pair['until'] == '2024-03-31':
env_vars = select_env_vars(dictionary, 0)
for report_type in ['CCSP', 'CCSPv2']:
custom_params = {
'run_command': 'Yes',
'generated': datetime.now().date().strftime('%Y-%m-%d')
}

env_vars['METRICS_UTILITY_REPORT_TYPE'] = report_type
# Decide which dictionary to use
if report_type == 'CCSP':
dictionary = env_vars_ccsp
else:
dictionary = env_vars_ccspv2

since = since_until_pair['since']
until = since_until_pair['until']
suffix = since + '--' + until
path = entry_point_dir + f'/data/{report_type}/snapshot_def_{suffix}.json'
data = {
'env_vars': env_vars,
'params': ['manage.py', 'build_report', f'--since={since}', f'--until={until}', '--force'],
'custom_params': custom_params,
}
snapshot_utils.save_snapshot_definition(data, path)
# Generate the monthly and custom range reports
generate_monthly_reports(report_type, months, dictionary, custom_params, entry_point_dir)
generate_since_until_reports(report_type, since_until_pairs, dictionary, custom_params, entry_point_dir)

# run generated definitions
# Finally, run all generated definitions
snapshot_utils.run_and_generate_snapshot_definitions(entry_point_dir + '/data/')

if __name__ == "__main__":
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 3 additions & 3 deletions metrics_utility/test/snapshot_tests/snapshot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def create_directory_if_not_exists(directory_path: str) -> None:
print(f'Error creating directory: {e}')


def parse_json_file(file_path: str) -> dict:
def parse_json_file(file_path: str) -> dict | None:
"""
Parses a JSON file and returns the data as a dictionary.
Expand Down Expand Up @@ -342,7 +342,7 @@ def compare_worksheets(workbook_generated: openpyxl.Workbook,
)


def get_file_name(params: List[str], env_vars: Dict[str, str]) -> str:
def get_file_name(params: List[str], env_vars: Dict[str, str]) -> str | None:
"""
Attempts to deduce the filename for the generated report based on report type and parameters.
Expand All @@ -363,7 +363,7 @@ def get_file_name(params: List[str], env_vars: Dict[str, str]) -> str:
return None


def get_param_value(params: List[str], name: str) -> str:
def get_param_value(params: List[str], name: str) -> str | None:
"""
Extracts the value from a command-line parameter of the form "name=value".
Expand Down

0 comments on commit 90fbbed

Please sign in to comment.