Browse Source

Audit report null values fix

version2
venkataakhil 1 year ago
parent
commit
a6f833dbe6
  1. 265
      smart_service/transactions/report/audit_report/audit_report.py

265
smart_service/transactions/report/audit_report/audit_report.py

@ -6,15 +6,13 @@ import frappe
from frappe import _ from frappe import _
from datetime import datetime from datetime import datetime
from frappe.utils import cstr 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) site_name = cstr(frappe.local.site)
current_db_name = frappe.conf.get("db_name") current_db_name = frappe.conf.get("db_name")
custom_lang = frappe.db.sql( custom_lang = frappe.db.sql(
'''SELECT lang_code,lang_name FROM {0}.`tabCustom Languages` where lang_code <> 'en'; '''.format( '''SELECT lang_code,lang_name FROM {0}.`tabCustom Languages` where lang_code <> 'en'; '''.format(
current_db_name), as_dict=True) 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): def execute(filters=None):
@ -47,97 +45,111 @@ def get_data(filters):
""".format(current_db_name, fil), as_dict=True) """.format(current_db_name, fil), as_dict=True)
filter_data = filters.variant_mapping filter_data = filters.variant_mapping
if filter_data != None and len(data) > 0: 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 return data
def status_length_append(dicVal, parameter): def status_length_append(dic_val, parameter):
if len(dicVal) == 1: if len(dic_val) == 1:
system_size = str(len(dicVal)) + " "+parameter system_size = str(len(dic_val)) + " "+parameter
else: else:
system_size = str(len(dicVal)) + " " + parameter+"s" system_size = str(len(dic_val)) + " " + parameter+"s"
return system_size return system_size
def modify_data(dataInput, filterParent): def modify_data(data_input, filter_parent):
list1 = [] try:
input_data = modify_child_data(dataInput, indent=5, header=0) list1 = []
parent_append = add_parent(filterParent, indent=0, header=1) input_data = modify_child_data(data_input, indent=5, header=0)
s_map, sSize = get_parent_map(input_data, 'systems') parent_append = add_parent(filter_parent, indent=0, header=1)
system_size = status_length_append(sSize, 'System') s_map, s_size = get_parent_map(input_data, 'systems')
parent_append[0]['active_status'] = system_size system_size = status_length_append(s_size, 'System')
if parent_append:
for pa in parent_append: parent_append[0]['active_status'] = system_size
list1.append(pa)
for pa in parent_append:
for i in s_map: list1.append(pa)
s_map1, sSize1 = get_parent_map(s_map[i], 'systems')
s_map_append = append_to_dic1(s_map1, indent=1, header=0) for i in s_map:
ssMap, ssSize = get_parent_map(s_map[i], 'sub_systems') s_map1, s_size1 = get_parent_map(s_map[i], 'systems')
ssMapAppend = append_to_dic2(ssMap, indent=2, header=1) s_map_append = append_to_dic1(s_map1, indent=1, header=0)
ss_map, ss_size = get_parent_map(s_map[i], 'sub_systems')
s_map_append[0]['active_status'] = status_length_append( ss_map_append = append_to_dic2(ss_map, indent=2, header=1)
ssSize, 'Sub System')
list1.append(s_map_append[0]) if s_map_append:
s_map_append[0]['active_status'] = status_length_append(
sys_assets = frappe.db.sql('''SELECT system_asset as description, {0} ss_size, 'Sub System')
FROM {1}.`tabSystem Mapping_System Assets` list1.append(s_map_append[0])
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) 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(
for sa in sys_assets: active_status_querycase, current_db_name, list1[0]['procedure_name']+"%",
list1.append(sa.update( s_map_append[0]['procedure_name']), as_dict=True)
{'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'}))
for sa in sys_assets:
for j in ssMapAppend: list1.append(sa.update(
val = j['procedure_name'] {'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'}))
proc = j['procedure']
procMap, procSize = get_parent_map(ssMap[val], 'procedure_name') for j in ss_map_append:
procValuesDic = append_to_dic3( val = j['procedure_name']
procMap, procSize, j, indent=3, header=0, procedure=proc) proc = j['procedure']
proc_map, proc_size = get_parent_map(
procLength = status_length_append(procValuesDic, 'Procedure') ss_map[val], 'procedure_name')
j['active_status'] = j['active_status'] + ' / ' + procLength proc_values_dic = append_to_dic3(
list1.append(j) proc_map, proc_size, j, indent=3, header=0, procedure=proc)
for pm in procMap: proc_length = status_length_append(
step_list, steps_size = get_parent_map( proc_values_dic, 'Procedure')
procMap[pm], 'step_name') j['active_status'] = j['active_status'] + ' / ' + proc_length
step_length = status_length_append(steps_size, 'Step') list1.append(j)
list1.append({'procedure_name': pm, 'indent': 3, 'header': 0,
'active_status': step_length, 'modified': ''}) for pm in proc_map:
for sl in step_list: step_list, steps_size = get_parent_map(
list1.append({'procedure_name': sl, 'indent': 4, proc_map[pm], 'step_name')
'header': 0, 'active_status': '', 'modified': ''}) step_length = status_length_append(steps_size, 'Step')
for ss in step_list[sl]: list1.append({'procedure_name': pm, 'indent': 3, 'header': 0,
ss.update({'procedure_name': ss['content_type']}) 'active_status': step_length, 'modified': ''})
list1.append(ss) for sl in step_list:
list1.append({'procedure_name': sl, 'indent': 4,
appendData = add_duplicate_data(list1) 'header': 0, 'active_status': '', 'modified': ''})
appendData = remove_duplicate_and_header(appendData) for ss in step_list[sl]:
return appendData ss.update({'procedure_name': ss['content_type']})
list1.append(ss)
def append_to_dic1(mapData, indent, header): 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 = [] map_data_append = []
for d in mapData: for d in map_data:
keyDict = { if d:
'procedure_name': d, key_dict = {
'active_status': mapData[d][0]['active_status'], 'procedure_name': d,
'indent': indent, 'active_status': map_data[d][0]['active_status'],
'header': header, 'indent': indent,
'type': 'System' 'header': header,
} 'type': 'System'
map_data_append.append(keyDict) }
map_data_append.append(key_dict)
return map_data_append return map_data_append
def append_to_dic2(mapData, indent, header): def append_to_dic2(map_data, indent, header):
map_data_append = [] map_data_append = []
for d in mapData: for d in map_data:
da = mapData[d][0] da = map_data[d][0]
keyDict = { key_dict = {
'systems': da['systems'], 'systems': da['systems'],
'sub_systems': da['sub_systems'], 'sub_systems': da['sub_systems'],
'parent': da['parent'], 'parent': da['parent'],
@ -156,7 +168,7 @@ def append_to_dic2(mapData, indent, header):
other_proc = other_proc[:-2] + c['lang_code'] 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 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`) 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: if len(others) > 0:
others = 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' c['lang_code'])] = others['active_status'] + ' / ' + str(others['syscount']) + ' Procedures'
dic1['{}_workflow_state'.format( dic1['{}_workflow_state'.format(
c['lang_code'])] = others['workflow_state'] 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 return map_data_append
def append_to_dic3(mapData, procSize, j, indent, header, procedure): def append_to_dic3(map_data, proc_size, j, indent, header, procedure):
# logger = frappe.logger("login_api", allow_site=True, file_count=100)
map_data_append = [] map_data_append = []
i = 0 i = 0
dic1 = {} dic1 = {}
@ -195,21 +206,21 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure):
list2.append(l) list2.append(l)
list1.append(list2) list1.append(list2)
for u in mapData: for u in map_data:
date_list = [] date_list = []
for k in mapData[u]: for k in map_data[u]:
date_list.append(str(k['modified'])) date_list.append(str(k['modified']))
date_list.sort(key=lambda date: datetime.strptime( date_list.sort(key=lambda date: datetime.strptime(
date, "%Y-%m-%d %H:%M:%S.%f")) date, "%Y-%m-%d %H:%M:%S.%f"))
if len(mapData[u]) == 1: if len(map_data[u]) == 1:
step_length = str(len(mapData[u])) + " Step" step_length = str(len(map_data[u])) + " Step"
else: else:
step_length = str(len(mapData[u])) + " Steps" step_length = str(len(map_data[u])) + " Steps"
proc_dict = { proc_dict = {
'systems': mapData[u][0]['systems'], 'systems': map_data[u][0]['systems'],
'sub_systems': mapData[u][0]['sub_systems'], 'sub_systems': map_data[u][0]['sub_systems'],
'parent': j["parent"], 'parent': j["parent"],
'procedure_name': u, 'procedure_name': u,
'indent': indent, 'indent': indent,
@ -223,43 +234,46 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure):
for cus in range(len(custom_lang)): for cus in range(len(custom_lang)):
if len(list1[cus]) > 0: if len(list1[cus]) > 0:
if i<len(list1[cus]):
# frappe.msgprint(str(list1[cus]))
if list1[cus][i][0] == 1:
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str(
list1[cus][i][0]) + " Step"
else:
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str(
list1[cus][i][0]) + " Steps"
if list1[cus][i][0] == 1: # Append modified date
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str( dic1['{}_modified'.format(
list1[cus][i][0]) + " Step" custom_lang[cus]['lang_code'])] = list1[cus][i][1]
else:
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str(
list1[cus][i][0]) + " Steps"
# Append modified date
dic1['{}_modified'.format(
custom_lang[cus]['lang_code'])] = list1[cus][i][1]
dic2.update(dic1) dic2.update(dic1)
proc_dict.update(dic1) proc_dict.update(dic1)
i += 1 i += 1
map_data_append.append(proc_dict) map_data_append.append(proc_dict)
return map_data_append return map_data_append
def modify_child_data(childData, indent, header): def modify_child_data(child_data, indent, header):
for a in childData: for a in child_data:
a.update({'indent': indent}) a.update({'indent': indent})
a.update({'header': header}) a.update({'header': header})
a.update({'type': "Step"}) a.update({'type': "Step"})
return childData return child_data
def add_parent(variantParent, indent, header): def add_parent(variant_parent, indent, header):
list1 = [] list1 = []
varParDict = { var_par_dict = {
'procedure_name': variantParent, 'procedure_name': variant_parent,
'indent': indent, 'indent': indent,
'header': header, 'header': header,
} }
list1.append(varParDict) list1.append(var_par_dict)
variant_assests = frappe.db.sql(''' variant_assests = frappe.db.sql('''SELECT category as procedure_name,attach_file as description,{2}
SELECT category as procedure_name,attach_file as description,{2} FROM {0}.`tabVariant Mapping_Assets` where parent = '{1}';'''.format(current_db_name, variantParent, activeStatusQuerycase), as_dict=True) FROM {0}.`tabVariant Mapping_Assets`
where parent = '{1}';'''.format(current_db_name, variant_parent,
active_status_querycase), as_dict=True)
for va in variant_assests: for va in variant_assests:
list1.append( list1.append(
@ -268,24 +282,25 @@ def add_parent(variantParent, indent, header):
def get_parent_map(input_list, parameter): def get_parent_map(input_list, parameter):
sizeList = [] size_list = []
parentChildMap = {}
parent_child_map = {}
for b in input_list: for b in input_list:
parentChildMap.setdefault(b[parameter] or None, []).append(b) parent_child_map.setdefault(b[parameter] or None, []).append(b)
for c in parentChildMap: for c in parent_child_map:
sizeList.append(len(parentChildMap[c])) size_list.append(len(parent_child_map[c]))
return parentChildMap, sizeList return parent_child_map, size_list
def add_duplicate_data(data1): def add_duplicate_data(data1):
prevValue = "" prev_value = ""
for i in data1: for i in data1:
if i['procedure_name'] == prevValue: if i['procedure_name'] == prev_value:
dup = {"duplicate": "yes"} dup = {"duplicate": "yes"}
else: else:
dup = {"duplicate": "no"} dup = {"duplicate": "no"}
i.update(dup) i.update(dup)
prevValue = i['procedure_name'] prev_value = i['procedure_name']
return data1 return data1

Loading…
Cancel
Save