Add cif data as attachment to log email

This commit is contained in:
Fred Boniface 2023-06-03 21:57:26 +01:00
parent 30faef09c8
commit feb9775fe2
5 changed files with 45 additions and 75 deletions

View File

@ -2310,7 +2310,7 @@ pis:
- code: 6253 - code: 6253
stops: [wsb,dmh,wmn,sal] stops: [wsb,dmh,wmn,sal]
- code: 6257 - code: 6257
stops: [gcr,yae,bpw,fit,bri,kyn,olf,bth,boa,tro,web,fro] stops: [gcr,yae,bpw,fit,bri,kyn,olf,bth,boa,tro,wsb,fro]
## Non Passenger Codes ## Non Passenger Codes
- code: 0015 - code: 0015

View File

@ -14,96 +14,62 @@
# program. If not, see # program. If not, see
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE # https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
import smtplib, ssl, os import email, smtplib, ssl, os
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.utils import formatdate
import logger as log import logger as log
cif_file_path = "cif_data" smtpHost = os.getenv("OWL_EML_HOST")
smtpPort = os.getenv("OWL_EML_PORT")
smtpUser = os.getenv("OWL_EML_USER")
smtpPass = os.getenv("OWL_EML_PASS")
smtpFrom = os.getenv("OWL_EML_FROM")
log.out("mailer.py: Mailer module loaded", "DBUG") log.out("mailer.py: Mailer module loaded", "DBUG")
def submitLogs(): def submitLogs():
text :str = fetchLogs() text :str = fetchLogs()
cif_data = fetchCifData() sendMail(text)
sendMail(text, cifData)
def fetchLogs(): def fetchLogs():
with open("dbman-log", "r") as tmpfile: with open("dbman-log", "r") as tmpfile:
return tmpfile.read() return tmpfile.read()
def fetchCifData():
try:
with open(cif_file_path, "r") as f:
return f.read()
except Exception as e:
log.out("mailer.fetchCifData: CIF Data is not readable")
print(e)
def deleteLogs(): def deleteLogs():
if os.path.exists("dbman-log"): if os.path.exists("dbman-log"):
os.remove("dbman-log") os.remove("dbman-log")
print("Tidied log file") print("Tidied log file")
else: else:
print("No logfile to tidy") print("No logfile to tidy")
if os.path.exists(cif_file_path):
os.remove(cif_file_path)
else:
print("No cifData file to tidy")
#def sendMail(messageBody :str):
# smtpHost = os.getenv("OWL_EML_HOST")
# smtpPort = os.getenv("OWL_EML_PORT")
# smtpUser = os.getenv("OWL_EML_USER")
# smtpPass = os.getenv("OWL_EML_PASS")
# smtpFrom = os.getenv("OWL_EML_FROM")
# context = ssl.create_default_context()
# message = f"""Subject: OwlBoard-dbman-logs
#{messageBody}""" def sendMail(msg_body :str):
# try: message = MIMEMultipart()
# server = smtplib.SMTP(smtpHost,smtpPort) message['From'] = smtpFrom
# server.ehlo() message['To'] = "server-notification-receipt@fjla.uk"
# server.starttls(context=context) # Secure the connection message['Subject'] = "OwlBoard - dbmanager Logs"
# server.ehlo() filename = "cif_data"
# server.login(smtpUser, smtpPass) message.attach(MIMEText(msg_body, "plain"))
# server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", message) with open(filename, "rb") as attachment:
# except Exception as e: part = MIMEBase("application", "octet-stream")
# # Print any error messages to stdout part.set_payload(attachment.read())
# print(e) encoders.encode_base64(part)
# finally: part.add_header(
# server.quit() "Content-Disposition",
# deleteLogs() f"attachment; filename= {filename}",
)
def sendMail(messageBody, cif_data): message.attach(part)
smtpHost = os.getenv("OWL_EML_HOST") text = message.as_string()
smtpPort = os.getenv("OWL_EML_PORT")
smtpUser = os.getenv("OWL_EML_USER")
smtpPass = os.getenv("OWL_EML_PASS")
smtpFrom = os.getenv("OWL_EML_FROM")
context = ssl.create_default_context() context = ssl.create_default_context()
msg = MIMEMultipart()
msg['From'] = smtpFrom
msg['To'] = "server-notification-receipt@fjla.uk"
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "OwlBoard - dbmanager Logs"
msg.attach(MIMEText(messageBody))
if cif_data:
part = MIMEText(cif_data, 'plain')
part.add_header('Content-Disposition', 'attachment', filename="CIF-DATA")
msg.attach(part)
try: try:
with smtplib.SMTP(smtpHost, smtpPort) as server: with smtplib.SMTP(smtpHost, smtpPort) as server:
server.ehlo() server.starttls(context=context)
server.starttls(context=context) # Secure the connection
server.ehlo()
server.login(smtpUser, smtpPass) server.login(smtpUser, smtpPass)
server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", msg.as_string()) server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", text)
except Exception as e: except Exception as e:
# Print any error messages to stdout print("mailer.sendMail: Error sending message", e)
print(e)
finally: finally:
deleteLogs() deleteLogs()

View File

@ -14,7 +14,7 @@
# program. If not, see # program. If not, see
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE # https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
version = "2023.6.3" version = "2023.6.4"
print(f"main.py: Initialising db-manager v{version}") print(f"main.py: Initialising db-manager v{version}")
#Third Party Imports #Third Party Imports

View File

@ -69,8 +69,11 @@ def getTiploc(crs :str):
query = { query = {
'3ALPHA': CRS '3ALPHA': CRS
} }
res = mongo.query("stations", query) try:
print(res) res = mongo.query("stations", query)
if 'TIPLOC' in res: #print(res)
return res['TIPLOC'] if 'TIPLOC' in res:
return "UNKNOWN" return res['TIPLOC']
except Exception as e:
log.out("pis.getTiploc: Error finding tiploc:", query)
print("ERROR", e)

View File

@ -49,7 +49,7 @@ def isUpdateRequired():
timetableLength = mongo.getLength("timetable") timetableLength = mongo.getLength("timetable")
log.out(f"timetable.isUpdateRequired: timetable collection contains {timetableLength} documents", "DBUG") log.out(f"timetable.isUpdateRequired: timetable collection contains {timetableLength} documents", "DBUG")
timetableUpdateTime = mongo.metaCheckTime("timetable") timetableUpdateTime = mongo.metaCheckTime("timetable")
log.out(f"timetable.isUpdateRequired: Timetable last updated at {timetableUpdateDate}", "INFO") log.out(f"timetable.isUpdateRequired: Timetable last updated at {timetableUpdateTime}", "INFO")
timetableDataAge = helpers.getAgeInSeconds(timetableUpdateTime) timetableDataAge = helpers.getAgeInSeconds(timetableUpdateTime)
if (timetableDataAge >= twoDayinSecs and isAfter0800) or REBUILD: if (timetableDataAge >= twoDayinSecs and isAfter0800) or REBUILD:
log.out(f"timetable.isUpdateRequired: timetable collection requires rebuild", "INFO") log.out(f"timetable.isUpdateRequired: timetable collection requires rebuild", "INFO")
@ -64,9 +64,10 @@ def getTimetable(full :bool = False):
response = requests.get(downloadUrl, auth=(CORPUS_USER, CORPUS_PASS)) response = requests.get(downloadUrl, auth=(CORPUS_USER, CORPUS_PASS))
mongo.incrementCounter("schedule_api") mongo.incrementCounter("schedule_api")
log.out(f"timetable.getTimetable: Fetch (Full:{full}) response: {response.status_code}", "DBUG") log.out(f"timetable.getTimetable: Fetch (Full:{full}) response: {response.status_code}", "DBUG")
decompressed = zlib.decompress(response.content, 16+zlib.MAX_WBITS)
with open(filePath, "wb") as f: with open(filePath, "wb") as f:
f.write(response.content) f.write(decompressed)
return zlib.decompress(response.content, 16+zlib.MAX_WBITS) return decompressed
def loopTimetable(data): def loopTimetable(data):
listify = data.splitlines() listify = data.splitlines()