[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,10 +236,7 @@ def main():
"error": str(error), "error": str(error),
}, },
} }
new_item_state = { count = (
"timestamp": timestamp,
"condition": result["condition"],
"count": (
1 1
if ( if (
(old_item_state is None) (old_item_state is None)
@ -253,33 +252,47 @@ def main():
) else ) else
None None
) )
), )
} shall_send_notification = (
state_data[check_data["name"]] = state_encode(new_item_state)
file_write(state_path, _json.dumps(state_data, indent = "\t"))
### send notifications
if (
( (
( (
(new_item_state["count"] is not None) (count is not None)
and and
(new_item_state["count"] == check_data["threshold"]) (count == check_data["threshold"])
) )
or or
( (
(new_item_state["count"] is None) (count is None)
and and
check_data["annoy"] check_data["annoy"]
) )
) )
and and
( (
(new_item_state["condition"] != enum_condition.ok) (result["condition"] != enum_condition.ok)
or or
args.send_ok_notifications args.send_ok_notifications
) )
): )
new_item_state = {
"timestamp": timestamp,
"condition": result["condition"],
"count": count,
"last_notification_timestamp": (
timestamp
if shall_send_notification else
(
None
if (old_item_state is None) else
old_item_state["last_notification_timestamp"]
)
),
}
state_data[check_data["name"]] = state_encode(new_item_state)
file_write(state_path, _json.dumps(state_data, indent = "\t"))
### send notifications
if shall_send_notification:
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"],