bcp_log.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import re
  2. from collections import namedtuple
  3. from dataclasses import dataclass
  4. @dataclass
  5. class BulkcopyResult:
  6. imported: int = 0
  7. exported: int = 0
  8. ignored: int = 0
  9. import_duration: int = 0
  10. export_duration: int = 0
  11. def missing(self) -> int:
  12. return self.exported - self.imported - self.ignored
  13. def __str__(self) -> str:
  14. return (f'Exported: {self.exported:>7}\n'
  15. + f'Imported: {self.imported:>7}\n'
  16. + f'Ignored: {self.ignored:>7}\n'
  17. + f'Missing: {self.missing():>7}\n\n'
  18. + f'Duration: {self.export_duration:>11} s\n'
  19. + f' {self.import_duration:>11} s')
  20. def check_logfiles(prefix):
  21. result = BulkcopyResult()
  22. with open(base_dir + '/' + prefix + '.in.log', 'r') as frh:
  23. result.ignored = len(frh.readlines())
  24. # info output of export
  25. with open(base_dir + '/' + prefix + '.bcp1.log', 'r', encoding='cp850', errors='ignore') as frh:
  26. raw_logs = frh.read()
  27. match = re.search(r'(\d+) Zeilen kopiert.', raw_logs)
  28. result.exported = int(match.group(1)) if match else 0
  29. match2 = re.search(r'Zeit .* gesamt: (\d+)', raw_logs)
  30. result.export_duration = int(match2.group(1)) / 1000 if match2 else 0
  31. # info output of import
  32. with open(base_dir + '/' + prefix + '.bcp2.log', 'r', encoding='cp850', errors='ignore') as frh:
  33. raw_logs = frh.read()
  34. match = re.search(r'(\d+) Zeilen kopiert.', raw_logs)
  35. result.imported = int(match.group(1)) if match else 0
  36. match2 = re.search(r'Zeit .* gesamt: (\d+)', raw_logs)
  37. result.import_duration = int(match2.group(1)) / 1000 if match2 else 0
  38. print(result)
  39. if __name__ == '__main__':
  40. base_dir = '/home/robert/projekte/python/dbtools/SQL/temp'
  41. check_logfiles('ORDER_LINE_1')