Эх сурвалжийг харах

bcp-log verfeinert und an db_run angehängt

gc-server3 1 долоо хоног өмнө
parent
commit
40dfd806cc
2 өөрчлөгдсөн 18 нэмэгдсэн , 4 устгасан
  1. 17 4
      database/bcp_log.py
  2. 1 0
      db.py

+ 17 - 4
database/bcp_log.py

@@ -8,11 +8,13 @@ from pathlib import Path
 class BulkcopyResult:
     file_name: str
     timestamp: datetime
-    imported: int = -1
     exported: int = -1
+    imported: int = -1
     ignored: int = 0
-    import_duration: float = 0.0
     export_duration: float = 0.0
+    import_duration: float = 0.0
+    export_errors: str = ""
+    import_errors: str = ""
     file_size: int = -1
 
     @property
@@ -24,7 +26,7 @@ class BulkcopyResult:
             f"{self.file_name};{self.timestamp.strftime('%Y-%m-%dT%H:%M:%S')};"
             + f"{self.exported};{self.imported};{self.ignored};{self.missing};"
             + f"{self.export_duration};{self.import_duration};"
-            + f"{self.file_size}"
+            + f"{self.export_errors};{self.import_errors};{self.file_size}"
         )
 
     def __str__(self) -> str:
@@ -62,6 +64,7 @@ def check_logfiles(prefix: str, base_dir: str) -> BulkcopyResult:
             raw_logs = frh.read()
             result.exported = rows_copied(raw_logs)
             result.export_duration = total_time(raw_logs)
+            result.export_errors = sql_errors(raw_logs)
 
     # info output of import
     bcp2_log = Path(f"{base_dir}\\{prefix}.bcp2.log")
@@ -70,6 +73,7 @@ def check_logfiles(prefix: str, base_dir: str) -> BulkcopyResult:
             raw_logs = frh.read()
             result.imported = rows_copied(raw_logs)
             result.import_duration = total_time(raw_logs)
+            result.import_errors = sql_errors(raw_logs)
 
     csv_file = Path(f"{base_dir}\\{prefix}.csv")
     if csv_file.exists():
@@ -97,6 +101,13 @@ def total_time(raw_logs: str) -> float:
     return 0.0
 
 
+def sql_errors(raw_logs: str) -> str:
+    match = re.findall(r"SQLState = (\w+),", raw_logs)
+    if match:
+        return ",".join(set(match).difference({"S1000", "22001", "22003", "22005"}))
+    return ""
+
+
 def check_directory(base_dir: str, res: list[BulkcopyResult] | None = None) -> list[BulkcopyResult]:
     if res is None:
         res = []
@@ -113,7 +124,9 @@ def check_directory(base_dir: str, res: list[BulkcopyResult] | None = None) -> l
 
 def export_log_csv(res: list[BulkcopyResult], output_file: str):
     with open(output_file, "w") as fwh:
-        fwh.write("filename;timestamp;imported;exported;ignored;missing;import_duration;export_duration;file_size\n")
+        fwh.write(
+            "filename;timestamp;exported;imported;ignored;missing;export_duration;import_duration;export_errors;import_errors;file_size\n"
+        )
         for log in res:
             fwh.write(log.to_csv() + "\n")
 

+ 1 - 0
db.py

@@ -36,6 +36,7 @@ def run(config_file: str, increment: str = "1", max: int = 5):
     """
     config_file = cfg.system_dir + f"\\SQL\\config\\{config_file}.json"
     database.run(config_file, increment == "1", max)
+    bcp_log()
 
 
 @app.command()