mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-06-28 09:16:55 +00:00
Refactored some custom classess
This commit is contained in:
parent
a7317af413
commit
645db97c14
35
src/modules/DashboardLogger.py
Normal file
35
src/modules/DashboardLogger.py
Normal file
@ -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
|
@ -11,8 +11,8 @@ class EmailSender:
|
|||||||
def __init__(self, DashboardConfig):
|
def __init__(self, DashboardConfig):
|
||||||
self.smtp = None
|
self.smtp = None
|
||||||
self.DashboardConfig = DashboardConfig
|
self.DashboardConfig = DashboardConfig
|
||||||
if not os.path.exists('./attachments'):
|
if not os.path.exists('../attachments'):
|
||||||
os.mkdir('./attachments')
|
os.mkdir('../attachments')
|
||||||
|
|
||||||
def Server(self):
|
def Server(self):
|
||||||
return self.DashboardConfig.GetConfig("Email", "server")[1]
|
return self.DashboardConfig.GetConfig("Email", "server")[1]
|
||||||
@ -51,10 +51,10 @@ class EmailSender:
|
|||||||
message.attach(MIMEText(body, "plain"))
|
message.attach(MIMEText(body, "plain"))
|
||||||
|
|
||||||
if includeAttachment and len(attachmentName) > 0:
|
if includeAttachment and len(attachmentName) > 0:
|
||||||
attachmentPath = os.path.join('./attachments', attachmentName)
|
attachmentPath = os.path.join('../attachments', attachmentName)
|
||||||
if os.path.exists(attachmentPath):
|
if os.path.exists(attachmentPath):
|
||||||
attachment = MIMEBase("application", "octet-stream")
|
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())
|
attachment.set_payload(f.read())
|
||||||
encoders.encode_base64(attachment)
|
encoders.encode_base64(attachment)
|
||||||
attachment.add_header("Content-Disposition", f"attachment; filename= {attachmentName}",)
|
attachment.add_header("Content-Disposition", f"attachment; filename= {attachmentName}",)
|
22
src/modules/Log.py
Normal file
22
src/modules/Log.py
Normal file
@ -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()
|
53
src/modules/PeerJobLogger.py
Normal file
53
src/modules/PeerJobLogger.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user