Create notifications for each shipcall in the list

This commit is contained in:
Daniel Schick 2025-12-12 09:16:52 +01:00
parent f68e9ee218
commit d6becc43ea

View File

@ -87,24 +87,31 @@ class ValidationRules(ValidationRuleFunctions):
# build the list of evaluation times ('now', as isoformat) # build the list of evaluation times ('now', as isoformat)
#evaluation_time = self.get_notification_times(evaluation_states_new) #evaluation_time = self.get_notification_times(evaluation_states_new)
send_notification = False
if evaluation_states_old is not None and evaluation_states_new is not None: if evaluation_states_old is not None and evaluation_states_new is not None:
if len(evaluation_states_old) == 1 and len(evaluation_states_new) == 1:
if evaluation_states_old[0] != evaluation_states_new[0]:
pooledConnection = None pooledConnection = None
try: try:
pooledConnection = getPoolConnection() pooledConnection = getPoolConnection()
commands = pydapper.using(pooledConnection) commands = pydapper.using(pooledConnection)
for shipcall_id, state_old_raw, state_new_raw, violation in zip(shipcall_df.index, evaluation_states_old, evaluation_states_new, violations):
state_old = int(state_old_raw) if state_old_raw is not None else 0
state_new = int(state_new_raw) if state_new_raw is not None else 0
if state_old == state_new:
continue
notification_type = 3 # RED (mapped to time_conflict) notification_type = 3 # RED (mapped to time_conflict)
if evaluation_states_new[0] == 2: send_notification = False
match evaluation_states_old[0]:
if state_new == 2:
match state_old:
case 0: case 0:
send_notification = True send_notification = True
case 1: case 1:
send_notification = True send_notification = True
notification_type = 6 # YELLOW (mapped to missing_data) notification_type = 6 # YELLOW (mapped to missing_data)
if evaluation_states_new[0] == 3: elif state_new == 3:
match evaluation_states_old[0]: match state_old:
case 0: case 0:
send_notification = True send_notification = True
case 1: case 1:
@ -114,17 +121,17 @@ class ValidationRules(ValidationRuleFunctions):
if send_notification: if send_notification:
query = f"INSERT INTO notification (shipcall_id, type, level, message) VALUES (?shipcall_id?, {notification_type}, 0, ?message?)" query = f"INSERT INTO notification (shipcall_id, type, level, message) VALUES (?shipcall_id?, {notification_type}, 0, ?message?)"
commands.execute(query, param={"shipcall_id" : int(shipcall_df.index[0]), "message" : violations[0]}) commands.execute(query, param={"shipcall_id" : int(shipcall_id), "message" : violation})
if evaluation_states_new[0] == 1 and evaluation_states_old[0] != 0: # this resolves the conflict if state_new == 1 and state_old != 0: # this resolves the conflict
query = f"SELECT * from notification where shipcall_id = ?shipcall_id? and type = {notification_type} and level = 0" query = f"SELECT * from notification where shipcall_id = ?shipcall_id? and type = {notification_type} and level = 0"
existing_notification = commands.query(query, param={"shipcall_id" : int(shipcall_df.index[0])}) existing_notification = commands.query(query, param={"shipcall_id" : int(shipcall_id)})
if len(existing_notification) > 0: if len(existing_notification) > 0:
query = "DELETE from notification where id = ?id?" query = "DELETE from notification where id = ?id?"
commands.execute(query, param={"id" : existing_notification[0]["id"]}) commands.execute(query, param={"id" : existing_notification[0]["id"]})
else: else:
query = "INSERT INTO notification (shipcall_id, type, level) VALUES (?shipcall_id?, 4, 0)" query = "INSERT INTO notification (shipcall_id, type, level) VALUES (?shipcall_id?, 4, 0)"
commands.execute(query, param={"shipcall_id" : int(shipcall_df.index[0])}) commands.execute(query, param={"shipcall_id" : int(shipcall_id)})
finally: finally:
if pooledConnection is not None: if pooledConnection is not None:
pooledConnection.close() pooledConnection.close()