From a6f833dbe66427c4e8d2a026599f100389f37c71 Mon Sep 17 00:00:00 2001 From: venkataakhil Date: Wed, 8 Nov 2023 15:08:23 +0530 Subject: [PATCH] Audit report null values fix --- .../report/audit_report/audit_report.py | 265 +++++++++--------- 1 file changed, 140 insertions(+), 125 deletions(-) diff --git a/smart_service/transactions/report/audit_report/audit_report.py b/smart_service/transactions/report/audit_report/audit_report.py index 0ce59bd..701cc93 100644 --- a/smart_service/transactions/report/audit_report/audit_report.py +++ b/smart_service/transactions/report/audit_report/audit_report.py @@ -6,15 +6,13 @@ import frappe from frappe import _ from datetime import datetime from frappe.utils import cstr -from frappe.utils import logger -frappe.utils.logger.set_log_level("DEBUG") -logger = frappe.logger("login_api", allow_site=True, file_count=100) + site_name = cstr(frappe.local.site) current_db_name = frappe.conf.get("db_name") custom_lang = frappe.db.sql( '''SELECT lang_code,lang_name FROM {0}.`tabCustom Languages` where lang_code <> 'en'; '''.format( current_db_name), as_dict=True) -activeStatusQuerycase = "case when active_status = 'Active' then 'A' else 'IA' end as active_status" +active_status_querycase = "case when active_status = 'Active' then 'A' else 'IA' end as active_status" def execute(filters=None): @@ -47,97 +45,111 @@ def get_data(filters): """.format(current_db_name, fil), as_dict=True) filter_data = filters.variant_mapping if filter_data != None and len(data) > 0: - data = modify_data(data, filter_data) + validation_flag, data = modify_data(data, filter_data) + if validation_flag: + return data + else: + return [] return data -def status_length_append(dicVal, parameter): - if len(dicVal) == 1: - system_size = str(len(dicVal)) + " "+parameter +def status_length_append(dic_val, parameter): + if len(dic_val) == 1: + system_size = str(len(dic_val)) + " "+parameter else: - system_size = str(len(dicVal)) + " " + parameter+"s" + system_size = str(len(dic_val)) + " " + parameter+"s" return system_size -def modify_data(dataInput, filterParent): - list1 = [] - input_data = modify_child_data(dataInput, indent=5, header=0) - parent_append = add_parent(filterParent, indent=0, header=1) - s_map, sSize = get_parent_map(input_data, 'systems') - system_size = status_length_append(sSize, 'System') - parent_append[0]['active_status'] = system_size - - for pa in parent_append: - list1.append(pa) - - for i in s_map: - s_map1, sSize1 = get_parent_map(s_map[i], 'systems') - s_map_append = append_to_dic1(s_map1, indent=1, header=0) - ssMap, ssSize = get_parent_map(s_map[i], 'sub_systems') - ssMapAppend = append_to_dic2(ssMap, indent=2, header=1) - - s_map_append[0]['active_status'] = status_length_append( - ssSize, 'Sub System') - list1.append(s_map_append[0]) - - sys_assets = frappe.db.sql('''SELECT system_asset as description, {0} - FROM {1}.`tabSystem Mapping_System Assets` - where parent like '{2}' and systems = '{3}' and language= 'en' order by idx; '''.format(activeStatusQuerycase, current_db_name, list1[0]['procedure_name']+"%", - s_map_append[0]['procedure_name']), as_dict=True) - - for sa in sys_assets: - list1.append(sa.update( - {'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'})) - - for j in ssMapAppend: - val = j['procedure_name'] - proc = j['procedure'] - procMap, procSize = get_parent_map(ssMap[val], 'procedure_name') - procValuesDic = append_to_dic3( - procMap, procSize, j, indent=3, header=0, procedure=proc) - - procLength = status_length_append(procValuesDic, 'Procedure') - j['active_status'] = j['active_status'] + ' / ' + procLength - list1.append(j) - - for pm in procMap: - step_list, steps_size = get_parent_map( - procMap[pm], 'step_name') - step_length = status_length_append(steps_size, 'Step') - list1.append({'procedure_name': pm, 'indent': 3, 'header': 0, - 'active_status': step_length, 'modified': ''}) - for sl in step_list: - list1.append({'procedure_name': sl, 'indent': 4, - 'header': 0, 'active_status': '', 'modified': ''}) - for ss in step_list[sl]: - ss.update({'procedure_name': ss['content_type']}) - list1.append(ss) - - appendData = add_duplicate_data(list1) - appendData = remove_duplicate_and_header(appendData) - return appendData - - -def append_to_dic1(mapData, indent, header): +def modify_data(data_input, filter_parent): + try: + list1 = [] + input_data = modify_child_data(data_input, indent=5, header=0) + parent_append = add_parent(filter_parent, indent=0, header=1) + s_map, s_size = get_parent_map(input_data, 'systems') + system_size = status_length_append(s_size, 'System') + if parent_append: + parent_append[0]['active_status'] = system_size + + for pa in parent_append: + list1.append(pa) + + for i in s_map: + s_map1, s_size1 = get_parent_map(s_map[i], 'systems') + s_map_append = append_to_dic1(s_map1, indent=1, header=0) + ss_map, ss_size = get_parent_map(s_map[i], 'sub_systems') + ss_map_append = append_to_dic2(ss_map, indent=2, header=1) + + if s_map_append: + s_map_append[0]['active_status'] = status_length_append( + ss_size, 'Sub System') + list1.append(s_map_append[0]) + + sys_assets = frappe.db.sql('''SELECT system_asset as description, {0} FROM {1}.`tabSystem Mapping_System Assets` + where parent like '{2}' and systems = '{3}' and language= 'en' order by idx; '''.format( + active_status_querycase, current_db_name, list1[0]['procedure_name']+"%", + s_map_append[0]['procedure_name']), as_dict=True) + + for sa in sys_assets: + list1.append(sa.update( + {'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'})) + + for j in ss_map_append: + val = j['procedure_name'] + proc = j['procedure'] + proc_map, proc_size = get_parent_map( + ss_map[val], 'procedure_name') + proc_values_dic = append_to_dic3( + proc_map, proc_size, j, indent=3, header=0, procedure=proc) + + proc_length = status_length_append( + proc_values_dic, 'Procedure') + j['active_status'] = j['active_status'] + ' / ' + proc_length + list1.append(j) + + for pm in proc_map: + step_list, steps_size = get_parent_map( + proc_map[pm], 'step_name') + step_length = status_length_append(steps_size, 'Step') + list1.append({'procedure_name': pm, 'indent': 3, 'header': 0, + 'active_status': step_length, 'modified': ''}) + for sl in step_list: + list1.append({'procedure_name': sl, 'indent': 4, + 'header': 0, 'active_status': '', 'modified': ''}) + for ss in step_list[sl]: + ss.update({'procedure_name': ss['content_type']}) + list1.append(ss) + + append_data = add_duplicate_data(list1) + append_data = remove_duplicate_and_header(append_data) + # frappe.msgprint(str(append_data)) + return True, append_data + except Exception as e: + frappe.log_error('Audit report', frappe.get_traceback()) + False, None + + +def append_to_dic1(map_data, indent, header): map_data_append = [] - for d in mapData: - keyDict = { - 'procedure_name': d, - 'active_status': mapData[d][0]['active_status'], - 'indent': indent, - 'header': header, - 'type': 'System' - } - map_data_append.append(keyDict) + for d in map_data: + if d: + key_dict = { + 'procedure_name': d, + 'active_status': map_data[d][0]['active_status'], + 'indent': indent, + 'header': header, + 'type': 'System' + } + map_data_append.append(key_dict) return map_data_append -def append_to_dic2(mapData, indent, header): +def append_to_dic2(map_data, indent, header): map_data_append = [] - for d in mapData: - da = mapData[d][0] - keyDict = { + for d in map_data: + da = map_data[d][0] + key_dict = { 'systems': da['systems'], 'sub_systems': da['sub_systems'], 'parent': da['parent'], @@ -156,7 +168,7 @@ def append_to_dic2(mapData, indent, header): other_proc = other_proc[:-2] + c['lang_code'] others = frappe.db.sql('''select case when `tabProcedure`.active_status = 'Active' then 'A' WHEN `tabProcedure`.active_status = 'InActive' then 'IA' end as active_status,count(distinct(`tabProcedure_Details`.procedure_name)) as syscount,`tabProcedure`.workflow_state from ({0}.`tabProcedure_Details` inner join {0}.`tabProcedure` on `tabProcedure_Details`.`parent` = `tabProcedure`.`name`) - where `tabProcedure`.`name` = '{1}'; '''.format(current_db_name, other_proc, activeStatusQuerycase), as_dict=True) + where `tabProcedure`.`name` = '{1}'; '''.format(current_db_name, other_proc, active_status_querycase), as_dict=True) if len(others) > 0: others = others[0] @@ -169,16 +181,15 @@ def append_to_dic2(mapData, indent, header): c['lang_code'])] = others['active_status'] + ' / ' + str(others['syscount']) + ' Procedures' dic1['{}_workflow_state'.format( - + c['lang_code'])] = others['workflow_state'] - keyDict.update(dic1) + key_dict.update(dic1) - map_data_append.append(keyDict) + map_data_append.append(key_dict) return map_data_append -def append_to_dic3(mapData, procSize, j, indent, header, procedure): - # logger = frappe.logger("login_api", allow_site=True, file_count=100) +def append_to_dic3(map_data, proc_size, j, indent, header, procedure): map_data_append = [] i = 0 dic1 = {} @@ -195,21 +206,21 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure): list2.append(l) list1.append(list2) - for u in mapData: + for u in map_data: date_list = [] - for k in mapData[u]: + for k in map_data[u]: date_list.append(str(k['modified'])) date_list.sort(key=lambda date: datetime.strptime( date, "%Y-%m-%d %H:%M:%S.%f")) - if len(mapData[u]) == 1: - step_length = str(len(mapData[u])) + " Step" + if len(map_data[u]) == 1: + step_length = str(len(map_data[u])) + " Step" else: - step_length = str(len(mapData[u])) + " Steps" + step_length = str(len(map_data[u])) + " Steps" proc_dict = { - 'systems': mapData[u][0]['systems'], - 'sub_systems': mapData[u][0]['sub_systems'], + 'systems': map_data[u][0]['systems'], + 'sub_systems': map_data[u][0]['sub_systems'], 'parent': j["parent"], 'procedure_name': u, 'indent': indent, @@ -223,43 +234,46 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure): for cus in range(len(custom_lang)): if len(list1[cus]) > 0: + if i