BI_ReadLog.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import sys
  2. import os
  3. import yaml
  4. import shutil
  5. import pprint
  6. import lxml.etree as ET
  7. import lxml.builder
  8. import datetime
  9. from datetime import datetime as DT
  10. import time as t
  11. import json
  12. import sqlite3
  13. from sqlite3 import Error
  14. def create_connection(db):
  15. try:
  16. conn = sqlite3.connect(db)
  17. return conn
  18. except Error as e:
  19. print(e)
  20. return None
  21. def check_folder(target_path,folder):
  22. if not os.path.exists(os.path.join(target_path, folder)):
  23. os.makedirs(os.path.join(target_path, folder))
  24. return os.path.join(target_path, folder)
  25. def get_target_path(prod_step, root_path):
  26. switch = {
  27. "GV12 Order Processing": "level1",
  28. "GV12 Production": "level2",
  29. "GV12 Turn Production": "level3",
  30. "GV12 Turn Machining": "level4"
  31. }
  32. return check_folder(os.path.join(root_path, "task3/sortedTemplates"), switch[prod_step])
  33. def check_timestamp(time, rootNode,search):
  34. #convert time-string to date_time
  35. call_node_list = []
  36. time_conv = time.split("+")[0]
  37. datetime_object = DT.strptime(time_conv, '%Y-%m-%d' + 'T' + '%H:%M:%S')
  38. time_upper = str(datetime_object + datetime.timedelta(0, 2)) + "+02:00"
  39. time_lower = str(datetime_object - datetime.timedelta(0, 2)) + "+02:00"
  40. time_lower = time_lower.split(" ")[0] + "T" + time_lower.split(" ")[1]
  41. time_upper = time_upper.split(" ")[0] + "T" + time_upper.split(" ")[1]
  42. #find suitable timestamp among all timestamps for call level
  43. time_range = rootNode.xpath("//" + search + "/@time")
  44. time_range.sort()
  45. for time in time_range:
  46. if time <= time_upper and time >= time_lower:
  47. call_node_list = rootNode.xpath("//" + search + "[@time = '" + time + "']")
  48. # print(call_node_list)
  49. break
  50. if time > time_upper:
  51. break
  52. return call_node_list
  53. def create_json(root_path,list,file):
  54. with open(os.path.join(root_path, 'task3/'+file), 'w') as fp:
  55. json.dump(list, fp, indent=4)
  56. fp.close()
  57. def categorize_logs(root_path, logs_path):
  58. origin_path = logs_path
  59. print(origin_path)
  60. for root, dirs, files in os.walk(origin_path):
  61. for file in files:
  62. with open(os.path.join(root, file), "r") as input_file:
  63. results = yaml.load_all(input_file)
  64. target_path=""
  65. for value in results:
  66. if 'log' in value:
  67. try:
  68. val = value['log']['trace']['cpee:name']
  69. target_path=get_target_path(val, root_path)
  70. print(val)
  71. break
  72. except (KeyError, AttributeError):
  73. continue
  74. input_file.close()
  75. shutil.move(os.path.join(root, file), os.path.join(target_path, file))
  76. def xml_check(xml_file):
  77. if not os.path.isfile(xml_file):
  78. rootNode = ET.Element("ProcessInstances")
  79. tree = ET.ElementTree(rootNode)
  80. level1 = ET.SubElement(rootNode, "level1")
  81. else:
  82. tree = ET.parse(xml_file, ET.XMLParser(remove_blank_text=True))
  83. rootNode = tree.getroot()
  84. if tree.find('level1') is None:
  85. level1 = ET.SubElement(rootNode, "level1")
  86. else:
  87. level1 = tree.find('level1')
  88. return [tree,rootNode,level1]
  89. def recreate_process(root_path, conn, level):
  90. # variables
  91. f = open('taska3.txt', 'a')
  92. cur = conn.cursor()
  93. origin_path = os.path.join(root_path, "task3/sortedTemplates/level"+str(level))
  94. counter = 0;
  95. oknok = {'ok':0, 'nok':0}
  96. ok = 0
  97. nok = 0
  98. # traverse through all logs
  99. for root, dirs, files in os.walk(origin_path):
  100. for file in files:
  101. added = False;
  102. print(file)
  103. with open(os.path.join(root, file), "r") as input_file:
  104. results = yaml.load_all(input_file)
  105. # traverse throug single log
  106. measured_result = 'NULL'
  107. for value in results:
  108. #try:
  109. if 'log' in value:
  110. logname = value['log']['trace']['cpee:name']
  111. uuid = value['log']['trace']['cpee:uuid']
  112. instance = value['log']['trace']['concept:name']
  113. if 'event' in value and value['event']['id:id'] == "external" and value['event']['cpee:lifecycle:transition'] == 'dataelements/change':
  114. for listOut in value['event']['list']['data_values']:
  115. if listOut=='qr' and len(value['event']['list']['data_values'][listOut])>0:
  116. charge = value['event']['list']['data_values'][listOut].split(' ')[0]
  117. part = value['event']['list']['data_values'][listOut].split(' ')[1]
  118. #print("Charge: "+ charge + " Part: " + part)
  119. if 'event' in value and not value['event']['id:id']=="external":
  120. try:
  121. time = value['event']['time:timestamp']
  122. activity = value['event']['cpee:lifecycle:transition']
  123. name = value['event']['concept:name']
  124. step_id = value['event']['id:id']
  125. val="";
  126. if value['event']['lifecycle:transition'] == "start":
  127. val = json.dumps(value['event']['list']['data_send'])
  128. if 'data_values' in value['event']['list'].keys() and name == 'Measure with MicroVu' and activity == 'dataelements/change' :
  129. # print(value['event']['list']['data_values']['status'])
  130. #print("PART: " + str(part))
  131. #print("AKT; " + str(value['event']['list']['data_values']['qr'].split(' ')[1]))
  132. if value['event']['list']['data_values']['qr'].split(' ')[1]!=part:
  133. print("NOTPART: " + str(part))
  134. print("NOTAKT; " + str(value['event']['list']['data_values']['qr'].split(' ')[1]))
  135. continue
  136. measured_result = 'ok'
  137. if value['event']['list']['data_values']['status'] != 'ok':
  138. measured_result = 'nok'
  139. query = "Update MeasureTot SET status = '" + measured_result + "' where part = '" + part + "'" # and status = 'NULL'"
  140. #print(query)
  141. cur.execute(query)
  142. conn.commit()
  143. if 'data_receiver' in value['event']['list'].keys() and name!='Fetch':
  144. #print("ICHICHICH")
  145. for listIn in value['event']['list']['data_receiver']:
  146. #print(listIn.keys())
  147. #print(listIn['name'])
  148. #print(listIn['data'])
  149. if 'results' in listIn['data']:
  150. if 'raw' in listIn['data'] and len(listIn['data']['raw']) > 0:
  151. # print("DOOF")
  152. # print(listIn['data']['raw']['Aufford QR-Code-Eingabe']['Eingabe'][0])
  153. if listIn['data']['raw']['Aufford QR-Code-Eingabe']['Eingabe'][0].split(" ")[1] != part:
  154. continue
  155. #print(listIn['data']['results'])
  156. for entry in listIn['data']['results']:
  157. messungen = listIn['data']['results'][entry]
  158. measured_attr = entry
  159. query = "Insert into MeasureTot('charge','part', 'status', 'measuredAttr', 'instance', 'timestamp') VALUES ('" + charge + "','" + part + "', '" + measured_result + "','" + measured_attr + "', '" + instance + "','" + time + "' )"
  160. #print(query)
  161. cur.execute(query)
  162. conn.commit()
  163. for messung in messungen:
  164. measured_det = messung
  165. measured_value = messungen[messung]['on_scale_from_zero_to_one']
  166. status = messungen[messung]['status']
  167. query = "Insert into MeasureDet('measuredDet','measuredVal', 'status', 'measuredAttr', 'timestamp') VALUES ('" + measured_det + "','" + str(measured_value) + "', '" + status + "','" + measured_attr + "', '" + time + "' )"
  168. cur.execute(query)
  169. conn.commit()
  170. #qrcode=listIn['data']['raw']['Aufford QR-Code-Eingabe']
  171. val = json.dumps(value['event']['list']['data_receiver'])
  172. if not added:
  173. cur.execute("Select called_by from instances where instance = '" + instance + "'")
  174. parent_inst = cur.fetchone()
  175. if parent_inst is None:
  176. print(time)
  177. #print(len(time))
  178. time = time.split('+')[0]
  179. print(len(time))
  180. if len(time)>19:
  181. time = time [:-4]
  182. print(time)
  183. datetime_object = DT.strptime(time, '%Y-%m-%d' + 'T' + '%H:%M:%S')
  184. time_upper = str(datetime_object + datetime.timedelta(0, 2)) + "+02:00"
  185. time_lower = str(datetime_object - datetime.timedelta(0, 2)) + "+02:00"
  186. time_lower = time_lower.split(" ")[0] + "T" + time_lower.split(" ")[1]
  187. time_upper = time_upper.split(" ")[0] + "T" + time_upper.split(" ")[1]
  188. #print(time)
  189. #print(time_upper)
  190. #print(time_lower)
  191. # cur.execute("Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '2','Null')")
  192. query = "Select log.instance from LogEntries log join instances i on i.instance = log.instance where i.level='" + str(level-1) +"' and log.activity ='activity/calling' and log.timestamp>='" + time_lower + "' and log.timestamp<='" + time_upper + "'"
  193. #print(query)
  194. cur.execute(query)
  195. parent_inst = cur.fetchone()
  196. #print(parent_inst)
  197. if parent_inst is None:
  198. #print(parent_inst)
  199. #print(val)
  200. #print("Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '3','Null')")
  201. cur.execute("Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '" + str(level) +"','Null')")
  202. #conn.commit()
  203. else:
  204. cur.execute("Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '" + str(level) +"','" + parent_inst[0] + "')")
  205. else:
  206. #print(instance)
  207. #print(parent_inst)
  208. query = "Update Instances set uuid = '" + uuid + "', name = '" + logname + "' where called_by = '" + parent_inst[0] + "' and instance='" + instance + "'"
  209. #print(query)
  210. cur.execute(query)
  211. #conn.commit()
  212. #con.commit()
  213. added = True
  214. #print("Insert into LogEntries VALUES ('" + step_id + "','" + time + "', '" + name + "','" + activity + "', '" + val + "','" + instance + "' )")
  215. #to add calling instances
  216. if 'data_receiver' in value['event']['list'].keys() and activity == 'activity/receiving':
  217. # if step_id=='a1':
  218. for attr in value['event']['list']['data_receiver']:
  219. # print('addNEw')
  220. if type(attr['data']) is dict and 'CPEE-INSTANCE' in attr['data']:
  221. # print('addNEw1')
  222. c_instance = attr['data']['CPEE-INSTANCE'].split('/')[-1]
  223. query = "Insert into Instances VALUES ('" + c_instance + "','Null', 'Null', '" + str(
  224. level + 1) + "','" + instance + "')"
  225. cur.execute(query)
  226. conn.commit()
  227. break
  228. elif attr['name'] == 'instance':
  229. # print('addNEw2')
  230. c_instance = attr['data']
  231. query = "Insert into Instances VALUES ('" + c_instance + "','Null', 'Null', '" + str(
  232. level + 1) + "','" + instance + "')"
  233. # print(query)
  234. cur.execute(query)
  235. conn.commit()
  236. break
  237. # print("Insert into Instances VALUES ('" + c_instance + "','Null', 'Null', '2','"+instance+"')")
  238. # conn.commit()
  239. #To avoid adding machine_logs afters receiving status continue
  240. if step_id == 'a3' and name== 'Status' and activity == 'dataelements/change':
  241. #print("BR")
  242. #print(value['event']['list']['data_values'].keys())
  243. #print(value['event']['list']['data_values']['lets_continue'])
  244. if value['event']['list']['data_values']['lets_continue']==False:
  245. #print("EAK")
  246. query = "Insert into LogEntries VALUES ('" + step_id + "','" + time + "', '" + name + "','" + activity + "', '" + val + "','" + instance + "' )"
  247. cur.execute(query)
  248. conn.commit()
  249. break
  250. query="Insert into LogEntries VALUES ('" + step_id + "','" + time + "', '" + name + "','" + activity + "', '" + val + "','" + instance + "' )"
  251. if instance in ('446', '447', '448'):
  252. print(query, file = f)
  253. cur.execute(query)
  254. conn.commit()
  255. except(KeyError) as e:
  256. print(e, file = f)
  257. #print(query)
  258. print(activity, file = f)
  259. print(time, file = f)
  260. counter+=1
  261. #except sqlite3.Error as qe:
  262. except sqlite3.IntegrityError as qe:
  263. #if instance in ('446','447','448'):
  264. #print(query, file = f)
  265. #print(qe, file = f)
  266. print(qe, file = f)
  267. counter += 1
  268. pass
  269. #exit()
  270. except sqlite3.Error as qe_all:
  271. print("Error_GEN: " + str(query), file=f)
  272. print("Error_GEN: " + str(qe_all), file=f)
  273. except Exception as e:
  274. counter += 1
  275. #message = template.format(type(ex).__name__, ex.args)
  276. print("Error_GEN" + str(e), file = f)
  277. print("Unexpected error!!", file = f)
  278. try:
  279. # to handle machining:
  280. if step_id == 'a1' and name == 'Fetch' and activity == 'activity/receiving':
  281. for attr in value['event']['list']['data_receiver']:
  282. for entry in attr['data']:
  283. try:
  284. m_id = name = entry['ID']
  285. name = entry['name']
  286. val = entry['value']
  287. clientHandle = entry['meta']['ClientHandle']
  288. statusCode = entry['meta']['StatusCode']
  289. server_timestamp = entry['meta']['ServerTimestamp']
  290. query = "insert into Machining (timestamp, clientHandle, m_id, status, name, value, level4_step_id, level4_activity, level4_timestamp, level4_instance) VALUES('" + server_timestamp + "','" + clientHandle + "','" + m_id + "','" + statusCode + "','" + name + "','" + val + "','" + step_id + "','" + activity + "','" + time + "','" + instance + "')"
  291. #if instance in ('446', '447', '448', '449', '450', '451','452'):
  292. #print(query, file = f)
  293. #if time == '2018-10-17T14:37:40+02:00':
  294. #print(query, file = f)
  295. print(query, file=f)
  296. cur.execute(query)
  297. except(KeyError) as e:
  298. print("ERROR IN ENTRY: " + str(e), file=f)
  299. # print(query)
  300. print(activity, file=f)
  301. print(time, file=f)
  302. counter += 1
  303. # except sqlite3.Error as qe:
  304. except sqlite3.IntegrityError as qe:
  305. # if instance in ('446', '447', '448'):
  306. # print(query, file = f)
  307. # print(qe, file =
  308. print("Error: " + str(query), file=f)
  309. print("Error: " + str(qe), file=f)
  310. counter += 1
  311. pass
  312. # exit()
  313. except sqlite3.Error as qe_all:
  314. print("Error_GEN: " + str(query), file=f)
  315. print("Error_GEN: " + str(qe_all), file=f)
  316. except Exception as e:
  317. counter += 1
  318. # message = template.format(type(ex).__name__, ex.args)
  319. print("Error_GEN" + str(e), file=f)
  320. print("Unexpected error!!", file=f)
  321. conn.commit()
  322. except(KeyError) as e:
  323. print("ERROR IN ATTR: " + str(e), file=f)
  324. #print(query)
  325. print(activity, file = f)
  326. print(time, file = f)
  327. counter += 1
  328. continue
  329. # except sqlite3.Error as qe:
  330. except Exception as e:
  331. counter += 1
  332. #message = template.format(type(ex).__name__, ex.args)
  333. print("Error_GEN" + str(e), file = f)
  334. print("Unexpected error!!", file = f)
  335. #oknok['ok']=oknok['ok']+ok
  336. #oknok['nok'] = oknok['nok'] + nok
  337. #print(oknok)
  338. #conn.commit()
  339. #print(counter)
  340. counter = 0
  341. input_file.close()
  342. target_path = check_folder(os.path.join(root_path, 'task3/processed'), ('level'+str(level)))
  343. #shutil.move(os.path.join(origin_path,file), target_path)
  344. def recreate_process_level1(root_path,conn):
  345. #variables
  346. cur = conn.cursor()
  347. origin_path = os.path.join(root_path,"task3/sortedTemplates/level1")
  348. #traverse through all logs
  349. for root, dirs, files in os.walk(origin_path):
  350. for file in files:
  351. first = True;
  352. print(file)
  353. with open(os.path.join(root, file), "r") as input_file:
  354. results = yaml.load_all(input_file)
  355. #traverse throug single log
  356. for value in results:
  357. try:
  358. if 'log' in value:
  359. logname = value['log']['trace']['cpee:name']
  360. uuid = value['log']['trace']['cpee:uuid']
  361. instance = value['log']['trace']['concept:name']
  362. #print("Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '1','Null')")
  363. query = "Insert into Instances VALUES ('" + instance + "','" + uuid + "', '" + logname + "', '1','Null')"
  364. cur.execute(query)
  365. #conn.commit()
  366. if 'event' in value:
  367. time = value['event']['time:timestamp']
  368. val=""
  369. if value['event']['lifecycle:transition']=="start":
  370. val = json.dumps(value['event']['list']['data_send'])
  371. if 'data_receiver' in value['event']['list'].keys():
  372. val = json.dumps(value['event']['list']['data_receiver'])
  373. step_id = value['event']['id:id']
  374. activity = value['event']['cpee:lifecycle:transition']
  375. name = value['event']['concept:name']
  376. query = "Insert into LogEntries VALUES ('" + step_id + "','" + time + "', '" + name + "','" + activity + "', '" + val + "','" + instance + "' )"
  377. #print("Insert into LogEntries VALUES ('" + step_id + "','" + time + "', '" + name + "','" + activity + "', '" + val + "','" + instance + "' )")
  378. cur.execute(query)
  379. #conn.commit()
  380. for attr in value['event']['list']['data_receiver']:
  381. if attr['name'] == "url":
  382. c_instance = attr['data'].split('/')[-1]
  383. query="Insert into Instances VALUES ('" + c_instance + "','Null', 'Null', '2','"+instance+"')"
  384. cur.execute(query)
  385. #print("Insert into Instances VALUES ('" + c_instance + "','Null', 'Null', '2','"+instance+"')")
  386. #conn.commit()
  387. conn.commit()
  388. except(KeyError) as e:
  389. #print(e)
  390. #print(time)
  391. continue
  392. input_file.close()
  393. target_path = check_folder(os.path.join(root_path,'task3/processed'),'level1')
  394. #shutil.move(os.path.join(origin_path,file), target_path)
  395. root_path = os.getcwd()
  396. db = os.path.join(root_path,"BIII.db")
  397. conn = create_connection(db)
  398. conn.execute('delete from Machining')
  399. conn.execute('delete from LogEntries')
  400. conn.execute('delete from Instances')
  401. conn.execute('delete from MeasureTot')
  402. conn.execute('delete from MeasureDet')
  403. logs_path = os.path.join(root_path, "logs")
  404. #print(logs_path)
  405. categorize_logs(root_path, logs_path)
  406. recreate_process_level1(root_path,conn)
  407. recreate_process(root_path,conn,2)
  408. recreate_process(root_path,conn,3)
  409. recreate_process(root_path,conn,4)
  410. #recreate_process_level3(root_path,conn,"level3")
  411. #recreate_process_level4(root_path,conn,"level4")
  412. conn.close()