including evaluation_times and evaluation_notifications_sent

This commit is contained in:
Max Metz 2024-07-31 16:38:05 +02:00
parent 2485a894a1
commit be14e3ee1a

View File

@ -1,3 +1,4 @@
import typing
import copy import copy
import logging import logging
import re 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)""" """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 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_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) 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 # 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] violations = [self.concise_evaluation_message_if_too_long(violation) for violation in violations]
# 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)
# build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created # 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"] = evaluation_states_new
shipcall_df.loc[:,"evaluation_message"] = violations shipcall_df.loc[:,"evaluation_message"] = violations
#shipcall_df.loc[:,"evaluation_time"] = evaluation_time shipcall_df.loc[:,"evaluation_time"] = evaluation_time
#shipcall_df.loc[:,"evaluation_notifications_sent"] = evaluation_notifications_sent shipcall_df.loc[:,"evaluation_notifications_sent"] = evaluation_notifications_sent
return shipcall_df return shipcall_df
def concise_evaluation_message_if_too_long(self, violation): 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""" """this function should apply the ValidationRules to the respective .shipcall, in regards to .times"""
return (StatusFlags.GREEN, False) # (state:str, should_notify:bool) 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. 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) 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 # identify a state increase
should_notify = self.identify_notification_state_change(state_old=state_old, state_new=state_new) 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}) # 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 return evaluation_notifications_sent
def identify_notification_state_change(self, state_old, state_new) -> bool: 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) return int(state_new) > int(state_old)
def get_notification_times(self, evaluation_states_new)->list[datetime.datetime]: def get_notification_times(self, evaluation_states_new)->list[datetime.datetime]:
"""# build the list of evaluation times ('now', as isoformat)""" """# build the list of evaluation times ('now'-datetime)"""
evaluation_times = [datetime.datetime.now().isoformat() for _i in range(len(evaluation_states_new))] evaluation_times = [datetime.datetime.now() for _i in range(len(evaluation_states_new))] # .isoformat()
return evaluation_times return evaluation_times
def get_notification_states(self, evaluation_states_old, evaluation_states_new)->list[bool]: 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""" """# 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)) for state_old, state_new in zip(evaluation_states_old, evaluation_states_new)] 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 return evaluation_notifications_sent