diff --git a/src/modules/DashboardLogger.py b/src/modules/DashboardLogger.py new file mode 100644 index 0000000..31f3b01 --- /dev/null +++ b/src/modules/DashboardLogger.py @@ -0,0 +1,35 @@ +""" +Dashboard Logger Class +""" +import sqlite3, os, uuid + +class DashboardLogger: + def __init__(self, CONFIGURATION_PATH): + self.loggerdb = sqlite3.connect(os.path.join(CONFIGURATION_PATH, 'db', 'wgdashboard_log.db'), + isolation_level=None, + check_same_thread=False) + self.loggerdb.row_factory = sqlite3.Row + self.__createLogDatabase() + self.log(Message="WGDashboard started") + def __createLogDatabase(self): + with self.loggerdb: + loggerdbCursor = self.loggerdb.cursor() + existingTable = loggerdbCursor.execute("SELECT name from sqlite_master where type='table'").fetchall() + existingTable = [t['name'] for t in existingTable] + if "DashboardLog" not in existingTable: + loggerdbCursor.execute( + "CREATE TABLE DashboardLog (LogID VARCHAR NOT NULL, LogDate DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%S','now', 'localtime')), URL VARCHAR, IP VARCHAR, Status VARCHAR, Message VARCHAR, PRIMARY KEY (LogID))") + if self.loggerdb.in_transaction: + self.loggerdb.commit() + + def log(self, URL: str = "", IP: str = "", Status: str = "true", Message: str = "") -> bool: + try: + loggerdbCursor = self.loggerdb.cursor() + loggerdbCursor.execute( + "INSERT INTO DashboardLog (LogID, URL, IP, Status, Message) VALUES (?, ?, ?, ?, ?);", (str(uuid.uuid4()), URL, IP, Status, Message,)) + loggerdbCursor.close() + self.loggerdb.commit() + return True + except Exception as e: + print(f"[WGDashboard] Access Log Error: {str(e)}") + return False \ No newline at end of file diff --git a/src/Email.py b/src/modules/Email.py similarity index 91% rename from src/Email.py rename to src/modules/Email.py index 774073a..58a04a5 100644 --- a/src/Email.py +++ b/src/modules/Email.py @@ -11,8 +11,8 @@ class EmailSender: def __init__(self, DashboardConfig): self.smtp = None self.DashboardConfig = DashboardConfig - if not os.path.exists('./attachments'): - os.mkdir('./attachments') + if not os.path.exists('../attachments'): + os.mkdir('../attachments') def Server(self): return self.DashboardConfig.GetConfig("Email", "server")[1] @@ -51,10 +51,10 @@ class EmailSender: message.attach(MIMEText(body, "plain")) if includeAttachment and len(attachmentName) > 0: - attachmentPath = os.path.join('./attachments', attachmentName) + attachmentPath = os.path.join('../attachments', attachmentName) if os.path.exists(attachmentPath): attachment = MIMEBase("application", "octet-stream") - with open(os.path.join('attachments', attachmentName), 'rb') as f: + with open(os.path.join('../attachments', attachmentName), 'rb') as f: attachment.set_payload(f.read()) encoders.encode_base64(attachment) attachment.add_header("Content-Disposition", f"attachment; filename= {attachmentName}",) diff --git a/src/modules/Log.py b/src/modules/Log.py new file mode 100644 index 0000000..70f7fbb --- /dev/null +++ b/src/modules/Log.py @@ -0,0 +1,22 @@ +""" +Log Class +""" +class Log: + def __init__(self, LogID: str, JobID: str, LogDate: str, Status: str, Message: str): + self.LogID = LogID + self.JobID = JobID + self.LogDate = LogDate + self.Status = Status + self.Message = Message + + def toJson(self): + return { + "LogID": self.LogID, + "JobID": self.JobID, + "LogDate": self.LogDate, + "Status": self.Status, + "Message": self.Message + } + + def __dict__(self): + return self.toJson() \ No newline at end of file diff --git a/src/modules/PeerJobLogger.py b/src/modules/PeerJobLogger.py new file mode 100644 index 0000000..b975462 --- /dev/null +++ b/src/modules/PeerJobLogger.py @@ -0,0 +1,53 @@ +""" +Peer Job Logger +""" +import sqlite3, os, uuid +from Log import Log + +class PeerJobLogger: + def __init__(self, CONFIGURATION_PATH, AllPeerJobs): + self.loggerdb = sqlite3.connect(os.path.join(CONFIGURATION_PATH, 'db', 'wgdashboard_log.db'), + check_same_thread=False) + self.loggerdb.row_factory = sqlite3.Row + self.logs: list[Log] = [] + self.__createLogDatabase() + self.AllPeerJobs = AllPeerJobs + def __createLogDatabase(self): + with self.loggerdb: + loggerdbCursor = self.loggerdb.cursor() + + existingTable = loggerdbCursor.execute("SELECT name from sqlite_master where type='table'").fetchall() + existingTable = [t['name'] for t in existingTable] + + if "JobLog" not in existingTable: + loggerdbCursor.execute("CREATE TABLE JobLog (LogID VARCHAR NOT NULL, JobID NOT NULL, LogDate DATETIME DEFAULT (strftime('%Y-%m-%d %H:%M:%S','now', 'localtime')), Status VARCHAR NOT NULL, Message VARCHAR, PRIMARY KEY (LogID))") + if self.loggerdb.in_transaction: + self.loggerdb.commit() + def log(self, JobID: str, Status: bool = True, Message: str = "") -> bool: + try: + with self.loggerdb: + loggerdbCursor = self.loggerdb.cursor() + loggerdbCursor.execute(f"INSERT INTO JobLog (LogID, JobID, Status, Message) VALUES (?, ?, ?, ?)", + (str(uuid.uuid4()), JobID, Status, Message,)) + if self.loggerdb.in_transaction: + self.loggerdb.commit() + except Exception as e: + print(f"[WGDashboard] Peer Job Log Error: {str(e)}") + return False + return True + + def getLogs(self, all: bool = False, configName = None) -> list[Log]: + logs: list[Log] = [] + try: + allJobs = self.AllPeerJobs.getAllJobs(configName) + allJobsID = ", ".join([f"'{x.JobID}'" for x in allJobs]) + with self.loggerdb: + loggerdbCursor = self.loggerdb.cursor() + table = loggerdbCursor.execute(f"SELECT * FROM JobLog WHERE JobID IN ({allJobsID}) ORDER BY LogDate DESC").fetchall() + self.logs.clear() + for l in table: + logs.append( + Log(l["LogID"], l["JobID"], l["LogDate"], l["Status"], l["Message"])) + except Exception as e: + return logs + return logs