Browse Source

Fehlerbericht-Korrektur (Nachbau)

gc-server6 1 year ago
parent
commit
99a4076a9e

+ 2 - 1
.gitignore

@@ -10,4 +10,5 @@ unzipped/
 GCStruct_Reisacher_Planung/config/logs/gcstruct.0.log
 o365_token.txt
 tools/pdf/data/mit_Briefpapier
-sandbox/mitoenv/
+sandbox/mitoenv/
+fehlerbericht/temp/

+ 0 - 0
fehlerbericht/logfiles/__init__.py


+ 0 - 0
fehlerbericht/logfiles/analytics_log.py


+ 0 - 0
fehlerbericht/logfiles/batch_log.py


+ 0 - 0
fehlerbericht/logfiles/db_import_log.py


+ 0 - 0
fehlerbericht/logfiles/dtsx_log.py


+ 0 - 0
fehlerbericht/logfiles/impromptu_log.py


+ 0 - 0
fehlerbericht/logfiles/robocopy_log.py


+ 17 - 0
fehlerbericht/logfiles/systeminfo_log.py

@@ -0,0 +1,17 @@
+import re
+
+
+class SysteminfoLog:
+    def __init__(self, logfile_str):
+        self.log = logfile_str
+
+    @property
+    def ip_address(self):
+        res = re.search(
+            r"(LAN\-Verbindung|Ethernet)[^[]*\[01\]:\s([\d\.]+)",
+            self.log["Errors"][0],
+            re.MULTILINE,
+        )
+        if res:
+            return res[2]
+        return "0.0.0.0"

+ 0 - 0
fehlerbericht/logfiles/transformer_log.py


+ 5 - 5
fehlerbericht/mail_import.py

@@ -18,10 +18,10 @@ class MailImport:
 
     def mail_import(self):
         with (
-            Session(create_engine(DSN)) as self.session,
+            Session(create_engine(DSN)) as db_session,
             self.open_mailbox() as mb,
         ):
-            messages = mb.fetch(mark_seen=True, limit=100)
+            messages = mb.fetch(mark_seen=True, limit=500)
             imported = []
 
             for msg in messages:
@@ -29,11 +29,11 @@ class MailImport:
                 if status is None:
                     continue
 
-                self.session.add(status)
+                db_session.add(status)
                 try:
-                    self.session.commit()
+                    db_session.commit()
                 except IntegrityError as e:
-                    self.session.rollback()
+                    db_session.rollback()
                     print(e.args[0])
                 imported.append(msg.uid)
 

+ 11 - 4
fehlerbericht/model.py

@@ -1,6 +1,6 @@
-from sqlalchemy import Integer, String, Date, Time
-from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
-from sqlalchemy.dialects.mysql.base import LONGTEXT
+from sqlalchemy import ForeignKey, Integer, String, Date, Time
+from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
+from sqlalchemy.dialects.mysql.base import LONGTEXT, MEDIUMTEXT
 from datetime import date, time
 
 
@@ -11,7 +11,8 @@ class Base(DeclarativeBase):
 class StatusMeldung(Base):
     __tablename__ = "status_meldung"
     datum: Mapped[date] = mapped_column(Date, primary_key=True)
-    kunde: Mapped[str] = mapped_column(String(50), primary_key=True)
+    kunde: Mapped[str] = mapped_column(ForeignKey("kunden.kunde"))
+    kunde_ref: Mapped["Kunde"] = relationship()
     aufgabe: Mapped[str] = mapped_column(String(30))
     start: Mapped[time] = mapped_column(Time, primary_key=True)
     ende: Mapped[time] = mapped_column(Time)
@@ -20,3 +21,9 @@ class StatusMeldung(Base):
     anzahl: Mapped[int] = mapped_column(Integer, default=0)
     bearbeitet: Mapped[int] = mapped_column(Integer, default=0)
     kommentar_id: Mapped[int] = mapped_column(Integer, default=0)
+
+
+class Kunde(Base):
+    __tablename__ = "kunden"
+    kunde: Mapped[str] = mapped_column(String(50), primary_key=True)
+    whitelist: Mapped[MEDIUMTEXT] = mapped_column(MEDIUMTEXT)

+ 52 - 0
fehlerbericht/prepare_logfiles.py

@@ -0,0 +1,52 @@
+from config import DSN
+from model import StatusMeldung
+from logfiles.systeminfo_log import SysteminfoLog
+
+import json
+import os
+from sqlalchemy import create_engine, select
+from sqlalchemy.orm import Session
+
+
+class PrepareLogfiles:
+    def prepare_logfiles(self):
+        with Session(create_engine(DSN)) as db_session:
+            query = select(StatusMeldung).where(StatusMeldung.fehlerbericht.is_(None))
+            for sm in db_session.scalars(query):
+                # whitelist = self.get_whitelist(sm.kunde_ref.whitelist)
+                fehlerbericht = dict(
+                    [(f["Name"], f) for f in json.loads(sm.fehlerbericht_import)]
+                )
+                # self.export_log_files(sm, fehlerbericht)
+                # batch_log = [f for f in fehlerbericht if ".bat" in f["Name"]].pop(0)
+                systeminfo_log = SysteminfoLog(
+                    fehlerbericht.get("system.info", {"Errors": [""]})
+                )
+                if systeminfo_log.ip_address == "0.0.0.0":
+                    print(sm.kunde)
+
+    def export_log_files(self, status: StatusMeldung, fehlerbericht):
+        kunde = status.kunde.replace(" ", "-").lower()
+        path = f"fehlerbericht/temp/{status.datum}_{kunde}_{status.aufgabe}"
+        os.makedirs(path, exist_ok=True)
+        for file_name in os.listdir(path):
+            file = path + file_name
+            if os.path.isfile(file):
+                os.remove(file)
+        for name, f in fehlerbericht.items():
+            if f["Type"] != "Workflow":
+                continue
+            f["Errors"][0] = f["Errors"][0].replace("\r\n", "\n")
+            with open(f"{path}/{name}.log", "w", encoding="latin-1") as fwh:
+                fwh.write(f["Errors"][0])
+
+    def get_whitelist(self, whitelist2):
+        whitelist = {"Layer": [], "Report": [], "User": []}
+        if whitelist2 is not None and whitelist2 != "":
+            whitelist.update(json.loads(whitelist2))
+        return whitelist
+
+
+if __name__ == "__main__":
+    mi = PrepareLogfiles()
+    mi.prepare_logfiles()