Browse Source

Audit report validate empty data with error log

master
venkataakhil 11 months ago
parent
commit
2fb885385a
  1. 289
      smart_service/transactions/report/audit_report/audit_report.py

289
smart_service/transactions/report/audit_report/audit_report.py

@ -12,19 +12,19 @@ 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):
data = getData(filters)
data = get_data(filters)
if not data:
data = []
columns = getColumns()
columns = get_columns()
return columns, data
def getData(filters):
def get_data(filters):
data = []
if filters.variant_mapping != None:
fil = filters.variant_mapping + "%"
@ -43,93 +43,110 @@ def getData(filters):
and substring(`tabProcedure_Details`.parent, -2) = 'en'
order by `tabSystem Mapping_Sub System`.systems ,`tabSystem Mapping_Sub System`.sub_systems,`procedure`,idx;
""".format(current_db_name, fil), as_dict=True)
filterData = filters.variant_mapping
if filterData != None and len(data) > 0:
data = modifyData(data, filterData)
filter_data = filters.variant_mapping
if filter_data != None and len(data) > 0:
validation_flag, data = modify_data(data, filter_data)
if validation_flag:
return data
else:
frappe.log_error('Audit report',str(data))
return []
return data
def statusLengthAppend(dicVal,parameter):
if len(dicVal) == 1:
systemSize = str(len(dicVal)) + " "+parameter
else:
systemSize = str(len(dicVal)) + " "+ parameter+"s"
return systemSize
def modifyData(dataInput, filterParent):
list1 = []
inputData = modifyChildData(dataInput, indent=5, header=0)
parentAppend = addParent(filterParent, indent=0, header=1)
sMap, sSize = getParentMap(inputData, 'systems')
systemSize = statusLengthAppend(sSize,'System')
parentAppend[0]['active_status'] = systemSize
for pa in parentAppend:
list1.append(pa)
for i in sMap:
sMap1, sSize1 = getParentMap(sMap[i], 'systems')
sMapAppend = appendToDic1(sMap1, indent=1, header=0)
ssMap, ssSize = getParentMap(sMap[i], 'sub_systems')
ssMapAppend = appendToDic2(ssMap, indent=2, header=1)
sMapAppend[0]['active_status'] = statusLengthAppend(ssSize,'Sub System')
list1.append(sMapAppend[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']+"%",
sMapAppend[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 = getParentMap(ssMap[val], 'procedure_name')
procValuesDic = appendToDic3(
procMap, procSize, j, indent=3, header=0, procedure=proc)
procLength = statusLengthAppend(procValuesDic,'Procedure')
j['active_status'] = j['active_status'] + ' / ' + procLength
list1.append(j)
for pm in procMap:
step_list,steps_size = getParentMap(procMap[pm], 'step_name')
stepLength = statusLengthAppend(steps_size,'Step')
list1.append({'procedure_name': pm, 'indent': 3,'header':0,'active_status':stepLength,'modified':''})
def status_length_append(dic_val, parameter):
if len(dic_val) == 1:
system_size = str(len(dic_val)) + " "+parameter
else:
system_size = str(len(dic_val)) + " " + parameter+"s"
return system_size
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':''})
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']})
ss.update({'procedure_name': ss['content_type']})
list1.append(ss)
appendData = addDuplicateData(list1)
appendData = removeDuplicateAndHeader(appendData)
return appendData
def appendToDic1(mapData, indent, header):
mapDataAppend = []
for d in mapData:
keyDict = {
'procedure_name': d,
'active_status': mapData[d][0]['active_status'],
'indent': indent,
'header': header,
'type': 'System'
}
mapDataAppend.append(keyDict)
return mapDataAppend
append_data = add_duplicate_data(list1)
append_data = remove_duplicate_and_header(append_data)
return True, append_data
except Exception as e:
frappe.log_error('Audit report_modify ',str(e))
return False, str(e)
def append_to_dic1(map_data, indent, header):
map_data_append = []
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 appendToDic2(mapData, indent, header):
mapDataAppend = []
for d in mapData:
da = mapData[d][0]
keyDict = {
def append_to_dic2(map_data, indent, header):
map_data_append = []
for d in map_data:
da = map_data[d][0]
key_dict = {
'systems': da['systems'],
'sub_systems': da['sub_systems'],
'parent': da['parent'],
@ -148,7 +165,7 @@ def appendToDic2(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]
@ -161,15 +178,16 @@ def appendToDic2(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)
mapDataAppend.append(keyDict)
return mapDataAppend
map_data_append.append(key_dict)
return map_data_append
def appendToDic3(mapData, procSize, j, indent, header, procedure):
mapDataAppend = []
def append_to_dic3(map_data, proc_size, j, indent, header, procedure):
map_data_append = []
i = 0
dic1 = {}
list1 = []
@ -185,32 +203,35 @@ def appendToDic3(mapData, procSize, j, indent, header, procedure):
list2.append(l)
list1.append(list2)
for u in mapData:
dateList = []
for k in mapData[u]:
dateList.append(str(k['modified']))
dateList.sort(key=lambda date: datetime.strptime(date, "%Y-%m-%d %H:%M:%S.%f"))
for u in map_data:
date_list = []
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:
stepLength = str(len(mapData[u])) + " Step"
if len(map_data[u]) == 1:
step_length = str(len(map_data[u])) + " Step"
else:
stepLength = str(len(mapData[u])) + " Steps"
step_length = str(len(map_data[u])) + " Steps"
procDict = {
'systems': mapData[u][0]['systems'],
'sub_systems': mapData[u][0]['sub_systems'],
proc_dict = {
'systems': map_data[u][0]['systems'],
'sub_systems': map_data[u][0]['sub_systems'],
'parent': j["parent"],
'procedure_name': u,
'indent': indent,
'header': header,
'active_status': stepLength,
'active_status': step_length,
'type': 'Title',
'modified': dateList[-1]
'modified': date_list[-1]
}
dic2 = {}
for cus in range(len(custom_lang)):
if len(list1[cus]) > 0:
if list1[cus][i][0] == 1:
dic1['{}_active_status'.format(custom_lang[cus]['lang_code'])] = str(
list1[cus][i][0]) + " Step"
@ -219,66 +240,71 @@ def appendToDic3(mapData, procSize, j, indent, header, procedure):
list1[cus][i][0]) + " Steps"
# Append modified date
dic1['{}_modified'.format(custom_lang[cus]['lang_code'])] = list1[cus][i][1]
dic1['{}_modified'.format(
custom_lang[cus]['lang_code'])] = list1[cus][i][1]
dic2.update(dic1)
procDict.update(dic1)
proc_dict.update(dic1)
i += 1
mapDataAppend.append(procDict)
return mapDataAppend
map_data_append.append(proc_dict)
return map_data_append
def modifyChildData(childData, indent, header):
for a in childData:
def modify_child_data(child_data, indent, header):
for a in child_data:
a.update({'indent': indent})
a.update({'header': header})
a.update({'type': "Step"})
return childData
return child_data
def addParent(variantParent, indent, header):
def add_parent(variant_parent, indent, header):
list1 = []
varParDict = {
'procedure_name': variantParent,
var_par_dict = {
'procedure_name': variant_parent,
'indent': indent,
'header': header,
}
list1.append(varParDict)
variant_assests = frappe.db.sql('''
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)
list1.append(var_par_dict)
variant_assests = frappe.db.sql('''SELECT category as procedure_name,attach_file as description,{2}
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:
list1.append(va.update({'indent':1,'header': header,'type':'Asset'}))
list1.append(
va.update({'indent': 1, 'header': header, 'type': 'Asset'}))
return list1
def getParentMap(input_list, parameter):
sizeList = []
parentChildMap = {}
def get_parent_map(input_list, parameter):
size_list = []
parent_child_map = {}
for b in input_list:
parentChildMap.setdefault(b[parameter] or None, []).append(b)
for c in parentChildMap:
sizeList.append(len(parentChildMap[c]))
return parentChildMap, sizeList
parent_child_map.setdefault(b[parameter] or None, []).append(b)
for c in parent_child_map:
size_list.append(len(parent_child_map[c]))
return parent_child_map, size_list
def addDuplicateData(data1):
prevValue = ""
def add_duplicate_data(data1):
prev_value = ""
for i in data1:
if i['procedure_name'] == prevValue:
if i['procedure_name'] == prev_value:
dup = {"duplicate": "yes"}
else:
dup = {"duplicate": "no"}
i.update(dup)
prevValue = i['procedure_name']
prev_value = i['procedure_name']
return data1
def removeDuplicateAndHeader(data1):
def remove_duplicate_and_header(data1):
for k in data1:
if k['header'] == 0:
k['workflow_state'] = None
if k['indent'] == 4 or k['indent']==5:
if k['indent'] == 4 or k['indent'] == 5:
k.pop('active_status')
k.pop('modified')
if k['indent'] == 6:
@ -288,7 +314,7 @@ def removeDuplicateAndHeader(data1):
return data1
def getColumns():
def get_columns():
columns = [
{
"fieldname": "procedure_name",
@ -361,9 +387,10 @@ def getColumns():
columns += lang
return columns
@frappe.whitelist()
def variant(doctype, txt, searchfield, start, page_len, filters):
current_db_name = frappe.conf.get("db_name")
return frappe.db.sql(""" select name,family_code,fuel,transmission,vehicle_segment,active_status
current_db_name = frappe.conf.get("db_name")
return frappe.db.sql(""" select name,family_code,fuel,transmission,vehicle_segment,active_status
FROM {0}.`tabVariant Mapping` where
(active_status like "%%%(txt)s%%" or vehicle_segment like "%%%(txt)s%%" or name like "%%%(txt)s%%" or variant like "%%%(txt)s%%" or family_code like "%%%(txt)s%%" or fuel like "%%%(txt)s%%" or transmission like "%%%(txt)s%%") order by modified desc limit %(start)s, %(page_len)s """.format(current_db_name)%{'txt': txt, 'start': start, 'page_len': page_len})
(active_status like "%%%(txt)s%%" or vehicle_segment like "%%%(txt)s%%" or name like "%%%(txt)s%%" or variant like "%%%(txt)s%%" or family_code like "%%%(txt)s%%" or fuel like "%%%(txt)s%%" or transmission like "%%%(txt)s%%") order by modified desc limit %(start)s, %(page_len)s """.format(current_db_name) % {'txt': txt, 'start': start, 'page_len': page_len})

Loading…
Cancel
Save