Compare commits

..

2 Commits

Author SHA1 Message Date
Fred Boniface 30faef09c8 Rebase 2023-06-03 21:05:35 +01:00
Fred Boniface d28c73308b Adjustments 2023-06-03 21:05:10 +01:00
6 changed files with 87 additions and 25 deletions

View File

@ -2314,5 +2314,5 @@ pis:
## Non Passenger Codes ## Non Passenger Codes
- code: 0015 - code: 0015
stops: [xxx,NOT-IN-SERVICE,xxx] stops: [xxx,notINservice,xxx]
### All LTV ### All LTV

6
src/helpers.py Normal file
View File

@ -0,0 +1,6 @@
import time
def getAgeInSeconds(updateTimeInSeconds :int):
now = int(time.time())
ageInSeconds :int = now - updateTimeInSeconds
return ageInSeconds

View File

@ -15,46 +15,95 @@
# 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 smtplib, ssl, os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
import logger as log import logger as log
cif_file_path = "cif_data"
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()
sendMail(text) cif_data = fetchCifData()
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): #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}"""
# try:
# server = smtplib.SMTP(smtpHost,smtpPort)
# server.ehlo()
# server.starttls(context=context) # Secure the connection
# server.ehlo()
# server.login(smtpUser, smtpPass)
# server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", message)
# except Exception as e:
# # Print any error messages to stdout
# print(e)
# finally:
# server.quit()
# deleteLogs()
def sendMail(messageBody, cif_data):
smtpHost = os.getenv("OWL_EML_HOST") smtpHost = os.getenv("OWL_EML_HOST")
smtpPort = os.getenv("OWL_EML_PORT") smtpPort = os.getenv("OWL_EML_PORT")
smtpUser = os.getenv("OWL_EML_USER") smtpUser = os.getenv("OWL_EML_USER")
smtpPass = os.getenv("OWL_EML_PASS") smtpPass = os.getenv("OWL_EML_PASS")
smtpFrom = os.getenv("OWL_EML_FROM") smtpFrom = os.getenv("OWL_EML_FROM")
context = ssl.create_default_context() context = ssl.create_default_context()
message = f"""Subject: OwlBoard-dbman-logs msg = MIMEMultipart()
msg['From'] = smtpFrom
msg['To'] = "server-notification-receipt@fjla.uk"
{messageBody}""" 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:
server = smtplib.SMTP(smtpHost,smtpPort) with smtplib.SMTP(smtpHost, smtpPort) as server:
server.ehlo() server.ehlo()
server.starttls(context=context) # Secure the connection server.starttls(context=context) # Secure the connection
server.ehlo() server.ehlo()
server.login(smtpUser, smtpPass) server.login(smtpUser, smtpPass)
server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", message) server.sendmail(smtpFrom, "server-notification-receipt@fjla.uk", msg.as_string())
except Exception as e: except Exception as e:
# Print any error messages to stdout # Print any error messages to stdout
print(e) print(e)
finally: finally:
server.quit() 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.2" version = "2023.6.3"
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

@ -5,8 +5,8 @@ import mongo
REBUILD :bool = False # Set to True to force rebuild REBUILD :bool = False # Set to True to force rebuild
log.out("pis.py: PIS Module Loaded", "DBUG") log.out("pis.py: PIS Module Loaded", "DBUG")
file_location :str = "/app/data/pis/gwr.yaml" # Production & Testing #file_location :str = "/app/data/pis/gwr.yaml" # Production & Testing
#file_location :str = "/home/fred.boniface/git/owlboard/db-manager/data/pis/gwr.yaml" # Local Development file_location :str = "/home/fred.boniface/git/owlboard/db-manager/data/pis/gwr.yaml" # Local Development
def runUpdate(): def runUpdate():
if (not requiresUpdate()): if (not requiresUpdate()):
@ -37,7 +37,7 @@ def load(): # Programatically add a `toc` field to each entry.
with open(file_location, "r") as data: with open(file_location, "r") as data:
try: try:
pis = yaml.safe_load(data) pis = yaml.safe_load(data)
print(pis) #print(pis)
return pis["pis"] return pis["pis"]
except yaml.YAMLError as exc: except yaml.YAMLError as exc:
print(exc) print(exc)

View File

@ -20,6 +20,7 @@ REBUILD :bool = False ## Set to true to rebuild database
import os import os
import requests import requests
import logger as log import logger as log
import helpers
import zlib import zlib
import json import json
import mongo import mongo
@ -30,6 +31,11 @@ from datetime import datetime, timedelta
now = datetime.now() now = datetime.now()
yesterday = now - timedelta(days=1) yesterday = now - timedelta(days=1)
yesterdayDay = yesterday.strftime("%a").lower() yesterdayDay = yesterday.strftime("%a").lower()
todayDay = now.strftime("%a").lower()
oneDayinSecs = 86400
twoDayinSecs = 86400 * 2
isAfter0800 = (int(now.strftime("%H")) >= 8)
filePath = "cif_data"
TOC_Code = "EF" # Business code for GWR TOC_Code = "EF" # Business code for GWR
fullDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_FULL_DAILY&day=toc-full" fullDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_FULL_DAILY&day=toc-full"
updateDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_UPDATE_DAILY&day=toc-update-{yesterdayDay}" updateDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_UPDATE_DAILY&day=toc-update-{yesterdayDay}"
@ -42,12 +48,13 @@ log.out("timetable.py: Timetable module loaded", "DBUG")
def isUpdateRequired(): 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")
timetableUpdateDate = 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 {timetableUpdateDate}", "INFO")
if (not timetableLength or int(time.time()) > timetableUpdateDate + 172800 or REBUILD): timetableDataAge = helpers.getAgeInSeconds(timetableUpdateTime)
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")
return "full" return "full"
if (int(time.time()) > (timetableUpdateDate + 86400)): if (timetableDataAge >= oneDayinSecs and isAfter0800):
log.out(f"timetable.isUpdateRequired: timetable collection requires update", "INFO") log.out(f"timetable.isUpdateRequired: timetable collection requires update", "INFO")
return "update" return "update"
return False return False
@ -57,9 +64,9 @@ 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:
#print(decompressed) f.write(response.content)
return decompressed return zlib.decompress(response.content, 16+zlib.MAX_WBITS)
def loopTimetable(data): def loopTimetable(data):
listify = data.splitlines() listify = data.splitlines()