From be14e3ee1aa0ab4e0df62ffa5555a0faef1f6776 Mon Sep 17 00:00:00 2001 From: Max Metz Date: Wed, 31 Jul 2024 16:38:05 +0200 Subject: [PATCH] including evaluation_times and evaluation_notifications_sent --- .../BreCal/validators/validation_rules.py | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/server/BreCal/validators/validation_rules.py b/src/server/BreCal/validators/validation_rules.py index 03ffc1f..57261f0 100644 --- a/src/server/BreCal/validators/validation_rules.py +++ b/src/server/BreCal/validators/validation_rules.py @@ -1,3 +1,4 @@ +import typing import copy import logging import re @@ -74,6 +75,9 @@ class ValidationRules(ValidationRuleFunctions): """apply 'evaluate_shipcall_from_df' to each individual shipcall in {shipcall_df}. Returns shipcall_df ('evaluation', 'evaluation_message', 'evaluation_time' and 'evaluation_notifications_sent' are updated)""" evaluation_states_old = [state_old for state_old in shipcall_df.loc[:,"evaluation"]] evaluation_states_old = [state_old if not pd.isna(state_old) else 0 for state_old in evaluation_states_old] + + evaluation_notifications_sent_old = [ens for ens in shipcall_df.loc[:,"evaluation_notifications_sent"]] + results = shipcall_df.apply(lambda x: self.evaluate_shipcall_from_df(x), axis=1).values # returns tuple (state, message) # unbundle individual results. evaluation_states becomes an integer, violation @@ -82,15 +86,15 @@ class ValidationRules(ValidationRuleFunctions): violations = [self.concise_evaluation_message_if_too_long(violation) for violation in violations] # 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) # build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created - #evaluation_notifications_sent = self.get_notification_states(evaluation_states_old, evaluation_states_new) + evaluation_notifications_sent = self.get_notification_states(evaluation_states_old, evaluation_states_new, evaluation_notifications_sent_old) shipcall_df.loc[:,"evaluation"] = evaluation_states_new shipcall_df.loc[:,"evaluation_message"] = violations - #shipcall_df.loc[:,"evaluation_time"] = evaluation_time - #shipcall_df.loc[:,"evaluation_notifications_sent"] = evaluation_notifications_sent + shipcall_df.loc[:,"evaluation_time"] = evaluation_time + shipcall_df.loc[:,"evaluation_notifications_sent"] = evaluation_notifications_sent return shipcall_df def concise_evaluation_message_if_too_long(self, violation): @@ -112,16 +116,24 @@ class ValidationRules(ValidationRuleFunctions): """this function should apply the ValidationRules to the respective .shipcall, in regards to .times""" return (StatusFlags.GREEN, False) # (state:str, should_notify:bool) - def determine_notification_state(self, state_old, state_new): + def determine_notification_state(self, state_old, state_new, evaluation_notifications_sent)->typing.Optional[bool]: """ this method determines state changes in the notification state. When the state increases, a user is notified about it. state order: (NONE = GREEN < YELLOW < RED) + + If a notification shall be sent, this method returns False. If no notification shall be sent, this method returns None or the prior state. + The method *never* returns True, as it shall only be called on novel shipcalls. + + args: + evaluation_notifications_sent: the PREVIOUS state (if any) of this boolean. When no notification is required, the prior bool is used (e.g., None, False, True). """ + previous_state = evaluation_notifications_sent + # identify a state increase should_notify = self.identify_notification_state_change(state_old=state_old, state_new=state_new) # when a state increases, a notification must be sent. Thereby, the field should be set to False ({evaluation_notifications_sent}) - evaluation_notifications_sent = False if bool(should_notify) else None + evaluation_notifications_sent = False if bool(should_notify) else previous_state return evaluation_notifications_sent def identify_notification_state_change(self, state_old, state_new) -> bool: @@ -147,13 +159,13 @@ class ValidationRules(ValidationRuleFunctions): return int(state_new) > int(state_old) def get_notification_times(self, evaluation_states_new)->list[datetime.datetime]: - """# build the list of evaluation times ('now', as isoformat)""" - evaluation_times = [datetime.datetime.now().isoformat() for _i in range(len(evaluation_states_new))] + """# build the list of evaluation times ('now'-datetime)""" + evaluation_times = [datetime.datetime.now() for _i in range(len(evaluation_states_new))] # .isoformat() return evaluation_times - def get_notification_states(self, evaluation_states_old, evaluation_states_new)->list[bool]: - """# build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created""" - evaluation_notifications_sent = [self.determine_notification_state(state_old=int(state_old), state_new=int(state_new)) for state_old, state_new in zip(evaluation_states_old, evaluation_states_new)] + def get_notification_states(self, evaluation_states_old, evaluation_states_new, evaluation_notifications_sent_old)->list[typing.Optional[bool]]: + """# build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created and None, when not""" + evaluation_notifications_sent = [self.determine_notification_state(state_old=int(state_old), state_new=int(state_new), evaluation_notifications_sent=ens) for state_old, state_new, ens in zip(evaluation_states_old, evaluation_states_new, evaluation_notifications_sent_old)] return evaluation_notifications_sent