瀏覽代碼

Fehlerbericht-Aufbereitung von php nachgebaut

gc-server6 1 年之前
父節點
當前提交
8bfd9c9c0c
共有 4 個文件被更改,包括 160 次插入8 次删除
  1. 3 1
      .gitignore
  2. 64 0
      fehlerbericht/api.py
  3. 2 2
      fehlerbericht/model.py
  4. 91 5
      fehlerbericht/prepare_logfiles.py

+ 3 - 1
.gitignore

@@ -11,4 +11,6 @@ GCStruct_Reisacher_Planung/config/logs/gcstruct.0.log
 o365_token.txt
 tools/pdf/data/mit_Briefpapier
 sandbox/mitoenv/
-fehlerbericht/temp/
+fehlerbericht/temp/
+fehlerbericht/temp_ori/
+mailserver/temp/

+ 64 - 0
fehlerbericht/api.py

@@ -0,0 +1,64 @@
+import os
+from flask import Flask
+
+app = Flask(__name__)
+
+
+@app.route("/")
+def main():
+    return "Funktioniert!"
+
+
+@app.route("/fehlerbericht", methods=["GET", "POST"])
+def status_report():
+    return "Fehlerbericht"
+
+
+@app.route("/zeit", methods=["GET", "POST"])
+def adjust_time():
+    return "Zeit"
+
+
+@app.route("/whitelist", methods=["GET", "POST"])
+def whitelist():
+    return "Whitelist"
+
+
+@app.route("/changelog", methods=["GET", "POST"])
+def changelog():
+    return "Changelog"
+
+
+@app.route("/kunden", methods=["GET", "POST"])
+def customers():
+    return "Kunden"
+
+
+@app.route("/tickets", methods=["GET", "POST"])
+def tickets():
+    return "Tickets"
+
+
+@app.route("/config", methods=["GET", "POST"])
+def config():
+    return "Config"
+
+
+@app.route("/benutzer", methods=["GET", "POST"])
+def users():
+    return "Benutzer"
+
+
+@app.route("/kommentar", methods=["GET", "POST"])
+def comments():
+    return "Kommentar"
+
+
+@app.route("/liste", methods=["GET", "POST"])
+def status_list():
+    return "Liste"
+
+
+if __name__ == "__main__":
+    app.secret_key = os.urandom(24)
+    app.run(host="0.0.0.0", port="8087", debug=True)

+ 2 - 2
fehlerbericht/model.py

@@ -11,9 +11,9 @@ class Base(DeclarativeBase):
 class StatusMeldung(Base):
     __tablename__ = "status_meldung"
     datum: Mapped[date] = mapped_column(Date, primary_key=True)
-    kunde: Mapped[str] = mapped_column(ForeignKey("kunden.kunde"))
+    kunde: Mapped[str] = mapped_column(ForeignKey("kunden.kunde"), primary_key=True)
     kunde_ref: Mapped["Kunde"] = relationship()
-    aufgabe: Mapped[str] = mapped_column(String(30))
+    aufgabe: Mapped[str] = mapped_column(String(30), primary_key=True)
     start: Mapped[time] = mapped_column(Time, primary_key=True)
     ende: Mapped[time] = mapped_column(Time)
     fehlerbericht_import: Mapped[LONGTEXT] = mapped_column(LONGTEXT, nullable=True)

+ 91 - 5
fehlerbericht/prepare_logfiles.py

@@ -1,3 +1,4 @@
+import re
 from config import DSN
 from model import StatusMeldung
 from logfiles.systeminfo_log import SysteminfoLog
@@ -11,19 +12,104 @@ 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))
+            query = select(
+                StatusMeldung
+            )  # .where(StatusMeldung.fehlerbericht.is_(None))
             for sm in db_session.scalars(query):
-                # whitelist = self.get_whitelist(sm.kunde_ref.whitelist)
+                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)
+
+                for name, report in fehlerbericht.items():
+                    if ".bat" in name:
+                        e = report["Errors"][0]
+                        e = e.replace(
+                            "!! Bitte E-Mailadresse fuer Statusbericht angeben !!",
+                            "! Bitte E-Mailadresse fuer Statusbericht angeben !",
+                        )
+                        e = e.replace(
+                            "!! SMTP-Konfiguration bitte anpassen !!",
+                            "! SMTP-Konfiguration bitte anpassen !",
+                        )
+                        e, err_count = re.subn(r"(!![^\n]*!!)", r"<b>\1</b>", e)
+                        e, err_count2 = re.subn(
+                            r"\((\w{2}\d{4})\)( [^\r]*)",
+                            r"<b><a href='/wiki/\1' target='_blank'>(\1)</a>\2</b>",
+                            e,
+                        )
+
+                        e, _ = re.subn(
+                            r"Error \= \[Micro.* NULL.*\.",
+                            "",
+                            e,
+                        )
+
+                        err_count3 = 0
+                        e, err_count3 = re.subn(
+                            r"(Error \= \[Micro[^\r]*)",
+                            r"<b>\1</b>",
+                            e,
+                        )
+
+                        if sum([err_count, err_count2, err_count3]) > 0:
+                            report["ErrorLevel"] = 2
+                            report["Errors"] = [e] + re.findall(r"<b>[^\n]*</b>", e)
+                            sm.anzahl += 1
+                    elif ".dtsx" in name:
+                        e = re.sub(r"\r\n", "\n", report["Errors"][0])
+                        e = re.sub(r"(\w*)ende\n", "§", e)
+                        e = re.sub(r"Copyright ", "§", e)
+                        loglevel_warn = [b for b in e.split("§") if "Status:" not in b]
+                        loglevel_error = [b for b in loglevel_warn if "Fehler:" in b]
+                        loglevel_combined = [
+                            "<b>" + b + "</b>" if "Fehler:" in b else b
+                            for b in loglevel_warn
+                        ]
+                        loglevel_combined[-1] = loglevel_combined[-1].replace(
+                            "DTSER_FAILURE", "<b>DTSER_FAILURE</b>"
+                        )
+                        if (
+                            len(loglevel_error) > 0
+                            or "DTSER_SUCCESS" not in loglevel_combined[-1]
+                        ):
+                            report["ErrorLevel"] = 2
+                            sm.anzahl += 1
+                        report["Errors"][0] = "\n".join(loglevel_combined)
+                    elif len(report["Errors"]) > 0 and report["Type"] in [
+                        "Portal",
+                        "Versand",
+                    ]:
+                        report["Errors2"] = [
+                            e
+                            for e in report["Errors"]
+                            if self.in_whitelist(e, whitelist)
+                        ]
+                        report["Errors"] = [
+                            e for e in report["Errors"] if e not in report["Errors2"]
+                        ]
+                        if len(report["Errors"]) == 0:
+                            report["ErrorLevel"] = 3
+                            sm.anzahl -= 1
+
+                sm.fehlerbericht = json.dumps(list(fehlerbericht.values()), indent=2)
+                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)
+                # if systeminfo_log.ip_address == "0.0.0.0":
+                print(sm.kunde)
+                db_session.commit()
+
+    def in_whitelist(self, e, whitelist):
+        return any(
+            [
+                e["Layer"] in whitelist["Layer"],
+                e["Report"] in whitelist["Report"],
+                e["User"] in whitelist["User"],
+            ]
+        )
 
     def export_log_files(self, status: StatusMeldung, fehlerbericht):
         kunde = status.kunde.replace(" ", "-").lower()