Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/improve upptime format #654

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def is_parameter_in_model(model_params: List[str], payload: Dict[str, Any]) -> b
return any(param in model_params for param in payload.keys())


def has_parameters_in_model(model_params: List[str], payload: Dict[str, Any]) -> int:
"""Returns the number of parameters in the payload that are in the model."""
return sum(1 for param in model_params if param in payload.keys())


def are_all_parameters_in_model(
model_params: List[str], payload: Dict[str, Any]
) -> bool:
Expand Down
6 changes: 4 additions & 2 deletions app/models/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ def validate_string_payload_type(payload: str) -> tuple:
]
model_params = model_utils.get_dict_of_parameters_from_models(known_models)

max_matches = 0
for model, params in model_params.items():
if model_utils.is_parameter_in_model(params, payload_dict):
matches = model_utils.has_parameters_in_model(params, payload_dict)
if matches > max_matches:
max_matches = matches
payload_type = model
break

if payload_type:
return payload_type, payload_dict
Expand Down
2 changes: 1 addition & 1 deletion app/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def handle_string_payload(
case "UpptimePayload":
# Temporary fix for Upptime payloads
text = validated_payload.get("text", "")
header_text = "🟥 Web Application Down!"
header_text = "📈 Web Application Down!"
blocks = [
{"type": "section", "text": {"type": "mrkdwn", "text": " "}},
{
Expand Down
21 changes: 21 additions & 0 deletions app/tests/models/test_models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ def test_is_parameter_in_model():
assert not model_utils.is_parameter_in_model(model_params, non_string_keys_payload)


def test_has_parameters_in_model():
model_params = ["field1", "field2", "field3"]

payload = {"field1": "value", "field2": "value"}
assert model_utils.has_parameters_in_model(model_params, payload) == 2

payload = {"field1": "value", "non_field": "value"}
assert model_utils.has_parameters_in_model(model_params, payload) == 1

empty_payload = {}
assert model_utils.has_parameters_in_model(model_params, empty_payload) == 0

partial_payload = {"field1": "value"}
assert model_utils.has_parameters_in_model(model_params, partial_payload) == 1

non_string_keys_payload = {1: "value", 2: "value"}
assert (
model_utils.has_parameters_in_model(model_params, non_string_keys_payload) == 0
)


def test_are_all_parameters_in_model():
model_params = ["field1", "field2", "field3"]

Expand Down
29 changes: 27 additions & 2 deletions app/tests/models/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,37 @@ def test_validate_string_payload_type_valid_json(
"TestModel": ["type"],
"TestModel2": ["type2"],
}
model_utils_mock.is_parameter_in_model.side_effect = [False, True]
model_utils_mock.has_parameters_in_model.side_effect = [0, 1, 0]
assert webhooks.validate_string_payload_type('{"type": "test"}') == (
"TestModel",
{"type": "test"},
)
assert model_utils_mock.is_parameter_in_model.call_count == 2
assert model_utils_mock.has_parameters_in_model.call_count == 3


@patch("models.webhooks.model_utils")
def test_validate_string_payload_same_params_in_multiple_models_returns_first_found(
model_utils_mock, caplog
):
model_utils_mock.get_dict_of_parameters_from_models.return_value = {
"WrongModel": ["test"],
"TestModel": ["type", "type2"],
"TestModel2": ["type2"],
"TestModel3": ["type"],
}
model_utils_mock.has_parameters_in_model.side_effect = [0, 2, 0, 1]
response = webhooks.validate_string_payload_type(
'{"type": "test", "type2": "test"}'
)
assert response == (
"TestModel",
{"type": "test", "type2": "test"},
)
assert response != (
"TestModel3",
{"type": "test"},
)
assert model_utils_mock.has_parameters_in_model.call_count == 4


def test_validate_string_payload_type_error_loading_json(caplog):
Expand Down
2 changes: 1 addition & 1 deletion app/tests/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_handle_string_payload_with_upptime_payload(validate_string_payload_type
assert response.blocks == [
{"text": {"text": " ", "type": "mrkdwn"}, "type": "section"},
{
"text": {"text": "🟥 Web Application Down!", "type": "plain_text"},
"text": {"text": "📈 Web Application Down!", "type": "plain_text"},
"type": "header",
},
{
Expand Down
Loading