Browse Source

workflow vereinfacht

robert 1 year ago
parent
commit
22f89de657
3 changed files with 28 additions and 36 deletions
  1. 12 12
      fehlerbericht/mail_import.py
  2. 9 24
      fehlerbericht/prepare_logfiles.py
  3. 7 0
      fehlerbericht/workflow.py

+ 12 - 12
fehlerbericht/mail_import.py

@@ -3,31 +3,29 @@ from config import DSN, MAILBOX
 from model import StatusMeldung
 
 from datetime import date, time, timedelta
-from email.message import EmailMessage
 from sqlalchemy import create_engine
 from sqlalchemy.orm import Session
 from sqlalchemy.exc import IntegrityError
-from imap_tools import MailBox, AND
+from imap_tools import MailBox, AND, MailMessage
 import json
 
 
 class MailImport:
     def open_mailbox(self) -> MailBox:
-        return MailBox(MAILBOX["server"]).login(
-            MAILBOX["user"], MAILBOX["password"], MAILBOX["folder"]
-        )
+        return MailBox(MAILBOX["server"]).login(MAILBOX["user"], MAILBOX["password"], MAILBOX["folder"])
 
     def mail_import(self) -> int:
         with (
             Session(create_engine(DSN)) as db_session,
             self.open_mailbox() as mb,
         ):
-            messages = mb.fetch(mark_seen=True, limit=500)
+            messages = mb.fetch(criteria=AND(seen=False), mark_seen=True, limit=500)
             imported = []
 
             for msg in messages:
                 status = self.get_status_message(msg)
                 if status is None:
+                    mb.move([msg.uid], "Unbekannt")
                     continue
 
                 db_session.add(status)
@@ -41,15 +39,13 @@ class MailImport:
             mb.delete(imported)
             return len(imported)
 
-    def get_status_message(self, msg: EmailMessage) -> StatusMeldung:
+    def get_status_message(self, msg: MailMessage) -> StatusMeldung:
         subject = msg.subject.split(";")
         if len(subject) < 5:
             print(msg.subject)
             return None
 
-        attachments = dict(
-            [(att.filename, att.payload) for att in msg.attachments if att.size > 0]
-        )
+        attachments = dict([(att.filename, att.payload) for att in msg.attachments if att.size > 0])
         fehlerbericht_json = json.loads(attachments.get("fehlerbericht.json", "[]"))
         fehlerbericht_import = json.dumps(fehlerbericht_json)
 
@@ -89,9 +85,13 @@ class MailImport:
             mb.delete(selected)
 
 
-if __name__ == "__main__":
+def mail_import():
     mi = MailImport()
     res = mi.mail_import()
     mi.cleanup()
-    if res == 0:
+    return res
+
+
+if __name__ == "__main__":
+    if mail_import() == 0:
         sys.exit(1)

+ 9 - 24
fehlerbericht/prepare_logfiles.py

@@ -20,9 +20,7 @@ class PrepareLogfiles:
     def convert_logfile(self, sm: StatusMeldung):
         whitelist_str = sm.kunde_ref.whitelist if sm.kunde_ref is not None else ""
         whitelist = self.get_whitelist(whitelist_str)
-        fehlerbericht = dict(
-            [(f["Name"], f) for f in json.loads(sm.fehlerbericht_import)]
-        )
+        fehlerbericht = dict([(f["Name"], f) for f in json.loads(sm.fehlerbericht_import)])
 
         for name, report in fehlerbericht.items():
             if ".bat" in name:
@@ -65,16 +63,9 @@ class PrepareLogfiles:
                 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]
-                ):
+                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)
@@ -82,12 +73,8 @@ class PrepareLogfiles:
                 "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"]
-                ]
+                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
@@ -95,9 +82,7 @@ class PrepareLogfiles:
         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": [""]})
-        )
+        SysteminfoLog(fehlerbericht.get("system.info", {"Errors": [""]}))
         # if systeminfo_log.ip_address == "0.0.0.0":
         # print(sm.kunde)
 
@@ -132,13 +117,13 @@ class PrepareLogfiles:
         return whitelist
 
 
-def main():
+def prepare_logfiles():
     mi = PrepareLogfiles()
     mi.prepare_logfiles()
 
 
 if __name__ == "__main__":
-    main()
+    prepare_logfiles()
 
     # import cProfile
 

+ 7 - 0
fehlerbericht/workflow.py

@@ -0,0 +1,7 @@
+from mail_import import mail_import
+from prepare_logfiles import prepare_logfiles
+
+
+if __name__ == "__main__":
+    if mail_import() > 0:
+        prepare_logfiles()