Added notification generation for next 24hrs shipcalls

This commit is contained in:
Daniel Schick 2024-12-12 11:10:05 +01:00
parent 50cecc6a9d
commit e5d9d051ea
2 changed files with 71 additions and 32 deletions

View File

@ -279,6 +279,15 @@ class SQLQuery():
"ORDER BY eta") "ORDER BY eta")
return query return query
def get_next24hrs_shipcalls()->str:
query = ("SELECT s.id as id, ship.name as name FROM shipcall s INNER JOIN ship ON s.ship_id = ship.id LEFT JOIN times t on t.shipcall_id = s.id AND t.participant_type = 8 " + \
"WHERE (type = 1 AND ((t.id IS NOT NULL AND t.eta_berth >= NOW() AND t.eta_berth < (NOW() + INTERVAL 1 DAY))" + \
"OR (eta >= NOW() AND eta < (NOW() + INTERVAL 1 DAY)))) OR " + \
"((type = 2 OR type = 3) AND ((t.id IS NOT NULL AND t.etd_berth >= NOW() AND " + \
"t.etd_berth < (NOW() + INTERVAL 1 DAY)) OR (etd >= NOW() AND etd < (NOW() + INTERVAL 1 DAY))))" + \
"AND s.canceled = 0")
return query
@staticmethod @staticmethod
def get_ships()->str: def get_ships()->str:
query = "SELECT id, name, imo, callsign, participant_id, length, width, is_tug, bollard_pull, eni, created, modified, deleted FROM ship ORDER BY name" query = "SELECT id, name, imo, callsign, participant_id, length, width, is_tug, bollard_pull, eni, created, modified, deleted FROM ship ORDER BY name"

View File

@ -4,6 +4,8 @@ from BreCal.schemas import model
from BreCal.local_db import getPoolConnection from BreCal.local_db import getPoolConnection
from BreCal.database.update_database import evaluate_shipcall_state from BreCal.database.update_database import evaluate_shipcall_state
from BreCal.database.sql_queries import create_sql_query_shipcall_get from BreCal.database.sql_queries import create_sql_query_shipcall_get
from BreCal.database.sql_queries import SQLQuery
from BreCal.database.sql_utils import get_notification_for_shipcall_and_type
import threading import threading
import schedule import schedule
@ -58,12 +60,7 @@ def UpdateNotifications():
query = "SELECT * FROM notification WHERE level = 0 AND created < DATE(NOW() - INTERVAL 10 MINUTE)" query = "SELECT * FROM notification WHERE level = 0 AND created < DATE(NOW() - INTERVAL 10 MINUTE)"
data = commands.query(query, model=model.Notification) data = commands.query(query, model=model.Notification)
for notification in data: for notification in data:
notification.level = 1 commands.execute("UPDATE notification SET level = 1 WHERE id = ?id?", param={"id":notification.id})
commands.update(notification)
# TODO
# send "real" notifications to user
# ... E-Mail, messenger
pooledConnection.close() pooledConnection.close()
except Exception as ex: except Exception as ex:
@ -82,6 +79,35 @@ def add_function_to_schedule__send_notifications(vr, interval_in_minutes:int=10)
schedule.every(interval_in_minutes).minutes.do(vr.notifier.send_notifications) schedule.every(interval_in_minutes).minutes.do(vr.notifier.send_notifications)
return return
def eval_next_24_hrs():
try:
pooledConnection = getPoolConnection()
commands = pydapper.using(pooledConnection)
query = SQLQuery.get_next24hrs_shipcalls()
data = commands.query(query)
nquery = "INSERT INTO notification (shipcall_id, participant_id, level, type, message) VALUES (?shipcall_id?, ?participant_id?, 0, 2, ?message?)"
for shipcall in data:
existing_notifications = get_notification_for_shipcall_and_type(shipcall["id"], 2)
query = SQLQuery.get_shipcall_participant_map_by_shipcall_id()
participants = commands.query(query, model=dict, param={"id":shipcall["id"]})
for participant in participants:
if participant["type"] == 1: # BSMD
continue
if participant["type"] == 32: # PORT AUTHORITY
continue
# check if "open" notification already exists
found_notification = False
for existing_notification in existing_notifications:
if existing_notification["participant_id"] == participant["id"] and existing_notification["level"] == 0:
found_notification = True
break
if not found_notification:
commands.execute(nquery, param={"shipcall_id":shipcall["id"], "participant_id": participant["participant_id"], "message":shipcall["name"]})
except Exception as ex:
logging.error(ex)
return
def setup_schedule(update_shipcalls_interval_in_minutes:int=60): def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
@ -94,9 +120,13 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
add_function_to_evaluate_notifications() add_function_to_evaluate_notifications()
schedule.every().day.at("09:00").do(eval_next_24_hrs)
# placeholder: create/send notifications # placeholder: create/send notifications
# add_function_to_schedule__send_notifications(...) # add_function_to_schedule__send_notifications(...)
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions
return return