From 3ae65c08db955ebaf5f9437d53abdbf9b76e6de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Wed, 22 Mar 2023 11:24:13 +0100 Subject: [PATCH] [issue-3] [mod] store timestamp of last notification in state file --- source/logic/main.py | 81 +++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/source/logic/main.py b/source/logic/main.py index 20654e6..3c3404c 100644 --- a/source/logic/main.py +++ b/source/logic/main.py @@ -3,6 +3,7 @@ def state_encode(state): "timestamp": state["timestamp"], "condition": condition_encode(state["condition"]), "count": state["count"], + "last_notification_timestamp": state["last_notification_timestamp"], } @@ -11,6 +12,7 @@ def state_decode(state_encoded): "timestamp": state_encoded["timestamp"], "condition": condition_decode(state_encoded["condition"]), "count": state_encoded["count"], + "last_notification_timestamp": state_encoded["last_notification_timestamp"], } @@ -234,24 +236,55 @@ def main(): "error": str(error), }, } + count = ( + 1 + if ( + (old_item_state is None) + or + (old_item_state["condition"] != result["condition"]) + ) else + ( + (old_item_state["count"] + 1) + if ( + (old_item_state["count"] is not None) + and + ((old_item_state["count"] + 1) <= check_data["threshold"]) + ) else + None + ) + ) + shall_send_notification = ( + ( + ( + (count is not None) + and + (count == check_data["threshold"]) + ) + or + ( + (count is None) + and + check_data["annoy"] + ) + ) + and + ( + (result["condition"] != enum_condition.ok) + or + args.send_ok_notifications + ) + ) new_item_state = { "timestamp": timestamp, "condition": result["condition"], - "count": ( - 1 - if ( - (old_item_state is None) - or - (old_item_state["condition"] != result["condition"]) - ) else + "count": count, + "last_notification_timestamp": ( + timestamp + if shall_send_notification else ( - (old_item_state["count"] + 1) - if ( - (old_item_state["count"] is not None) - and - ((old_item_state["count"] + 1) <= check_data["threshold"]) - ) else None + if (old_item_state is None) else + old_item_state["last_notification_timestamp"] ) ), } @@ -259,27 +292,7 @@ def main(): file_write(state_path, _json.dumps(state_data, indent = "\t")) ### send notifications - if ( - ( - ( - (new_item_state["count"] is not None) - and - (new_item_state["count"] == check_data["threshold"]) - ) - or - ( - (new_item_state["count"] is None) - and - check_data["annoy"] - ) - ) - and - ( - (new_item_state["condition"] != enum_condition.ok) - or - args.send_ok_notifications - ) - ): + if shall_send_notification: for notification in check_data["notifications"]: notification_channel_implementations[notification["kind"]].notify( notification["parameters"],