Browse Source

Audit report null values fix

version2
venkataakhil 11 months ago
parent
commit
a6f833dbe6
  1. 161
      smart_service/transactions/report/audit_report/audit_report.py

161
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,63 +45,72 @@ 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):
try:
list1 = [] list1 = []
input_data = modify_child_data(dataInput, indent=5, header=0) input_data = modify_child_data(data_input, indent=5, header=0)
parent_append = add_parent(filterParent, indent=0, header=1) parent_append = add_parent(filter_parent, indent=0, header=1)
s_map, sSize = get_parent_map(input_data, 'systems') s_map, s_size = get_parent_map(input_data, 'systems')
system_size = status_length_append(sSize, 'System') system_size = status_length_append(s_size, 'System')
if parent_append:
parent_append[0]['active_status'] = system_size parent_append[0]['active_status'] = system_size
for pa in parent_append: for pa in parent_append:
list1.append(pa) list1.append(pa)
for i in s_map: for i in s_map:
s_map1, sSize1 = get_parent_map(s_map[i], 'systems') s_map1, s_size1 = get_parent_map(s_map[i], 'systems')
s_map_append = append_to_dic1(s_map1, indent=1, header=0) s_map_append = append_to_dic1(s_map1, indent=1, header=0)
ssMap, ssSize = get_parent_map(s_map[i], 'sub_systems') ss_map, ss_size = get_parent_map(s_map[i], 'sub_systems')
ssMapAppend = append_to_dic2(ssMap, indent=2, header=1) 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( s_map_append[0]['active_status'] = status_length_append(
ssSize, 'Sub System') ss_size, 'Sub System')
list1.append(s_map_append[0]) list1.append(s_map_append[0])
sys_assets = frappe.db.sql('''SELECT system_asset as description, {0} sys_assets = frappe.db.sql('''SELECT system_asset as description, {0} FROM {1}.`tabSystem Mapping_System Assets`
FROM {1}.`tabSystem Mapping_System Assets` where parent like '{2}' and systems = '{3}' and language= 'en' order by idx; '''.format(
where parent like '{2}' and systems = '{3}' and language= 'en' order by idx; '''.format(activeStatusQuerycase, current_db_name, list1[0]['procedure_name']+"%", active_status_querycase, current_db_name, list1[0]['procedure_name']+"%",
s_map_append[0]['procedure_name']), as_dict=True) s_map_append[0]['procedure_name']), as_dict=True)
for sa in sys_assets: for sa in sys_assets:
list1.append(sa.update( list1.append(sa.update(
{'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'})) {'procedure_name': "System Asset", 'indent': 2, 'header': '', 'type': 'Asset'}))
for j in ssMapAppend: for j in ss_map_append:
val = j['procedure_name'] val = j['procedure_name']
proc = j['procedure'] proc = j['procedure']
procMap, procSize = get_parent_map(ssMap[val], 'procedure_name') proc_map, proc_size = get_parent_map(
procValuesDic = append_to_dic3( ss_map[val], 'procedure_name')
procMap, procSize, j, indent=3, header=0, procedure=proc) proc_values_dic = append_to_dic3(
proc_map, proc_size, j, indent=3, header=0, procedure=proc)
procLength = status_length_append(procValuesDic, 'Procedure')
j['active_status'] = j['active_status'] + ' / ' + procLength proc_length = status_length_append(
proc_values_dic, 'Procedure')
j['active_status'] = j['active_status'] + ' / ' + proc_length
list1.append(j) list1.append(j)
for pm in procMap: for pm in proc_map:
step_list, steps_size = get_parent_map( step_list, steps_size = get_parent_map(
procMap[pm], 'step_name') proc_map[pm], 'step_name')
step_length = status_length_append(steps_size, 'Step') step_length = status_length_append(steps_size, 'Step')
list1.append({'procedure_name': pm, 'indent': 3, 'header': 0, list1.append({'procedure_name': pm, 'indent': 3, 'header': 0,
'active_status': step_length, 'modified': ''}) 'active_status': step_length, 'modified': ''})
@ -114,30 +121,35 @@ def modify_data(dataInput, filterParent):
ss.update({'procedure_name': ss['content_type']}) ss.update({'procedure_name': ss['content_type']})
list1.append(ss) list1.append(ss)
appendData = add_duplicate_data(list1) append_data = add_duplicate_data(list1)
appendData = remove_duplicate_and_header(appendData) append_data = remove_duplicate_and_header(append_data)
return appendData # 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(mapData, indent, header): 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:
key_dict = {
'procedure_name': d, 'procedure_name': d,
'active_status': mapData[d][0]['active_status'], 'active_status': map_data[d][0]['active_status'],
'indent': indent, 'indent': indent,
'header': header, 'header': header,
'type': 'System' '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]
@ -171,14 +183,13 @@ def append_to_dic2(mapData, indent, header):
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,7 +234,8 @@ 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: if list1[cus][i][0] == 1:
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str( dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str(
list1[cus][i][0]) + " Step" list1[cus][i][0]) + " Step"
@ -242,24 +254,26 @@ def append_to_dic3(mapData, procSize, j, indent, header, procedure):
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