[issue-3] [mod] store timestamp of last notification in state file

This commit is contained in:
Christian Fraß 2023-03-22 11:24:13 +01:00
parent 386e1d5891
commit 3ae65c08db

View file

@ -3,6 +3,7 @@ def state_encode(state):
"timestamp": state["timestamp"], "timestamp": state["timestamp"],
"condition": condition_encode(state["condition"]), "condition": condition_encode(state["condition"]),
"count": state["count"], "count": state["count"],
"last_notification_timestamp": state["last_notification_timestamp"],
} }
@ -11,6 +12,7 @@ def state_decode(state_encoded):
"timestamp": state_encoded["timestamp"], "timestamp": state_encoded["timestamp"],
"condition": condition_decode(state_encoded["condition"]), "condition": condition_decode(state_encoded["condition"]),
"count": state_encoded["count"], "count": state_encoded["count"],
"last_notification_timestamp": state_encoded["last_notification_timestamp"],
} }
@ -234,24 +236,55 @@ def main():
"error": str(error), "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 = { new_item_state = {
"timestamp": timestamp, "timestamp": timestamp,
"condition": result["condition"], "condition": result["condition"],
"count": ( "count": count,
1 "last_notification_timestamp": (
if ( timestamp
(old_item_state is None) if shall_send_notification else
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 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")) file_write(state_path, _json.dumps(state_data, indent = "\t"))
### send notifications ### send notifications
if ( if shall_send_notification:
(
(
(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
)
):
for notification in check_data["notifications"]: for notification in check_data["notifications"]:
notification_channel_implementations[notification["kind"]].notify( notification_channel_implementations[notification["kind"]].notify(
notification["parameters"], notification["parameters"],