Skip to content

Commit

Permalink
ref(rules): Translate int minutes to words (#67021)
Browse files Browse the repository at this point in the history
Based on the hardcoded values we have available as options
[here](https://github.com/getsentry/sentry/blob/be87b8cea8131a46248e11507edd8d58d4d59f3d/static/app/views/alerts/rules/issue/index.tsx#L83-L93)
translate the number of minutes to available values for human
readability.

Before:
<img width="423" alt="Screenshot 2024-03-14 at 2 53 47 PM"
src="https://github.com/getsentry/sentry/assets/29959063/411e07a6-2844-421b-a302-58bbdfcd7923">

After:
<img width="431" alt="Screenshot 2024-03-14 at 2 53 52 PM"
src="https://github.com/getsentry/sentry/assets/29959063/f4631d4e-393f-404f-bf58-9b9d4cca2afa">


Closes
getsentry/team-core-product-foundations#158
  • Loading branch information
ceorourke authored Mar 15, 2024
1 parent 1752f52 commit 6f043be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/sentry/rules/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from sentry.models.environment import Environment
from sentry.models.rule import Rule

ONE_HOUR = 60
ONE_DAY = ONE_HOUR * 24
ONE_WEEK = ONE_DAY * 7


def get_updated_rule_data(rule: Rule) -> dict[str, Any]:
rule_data = dict(rule.data)
Expand Down Expand Up @@ -42,6 +46,22 @@ def generate_diff_labels(
return changed_data


def get_frequency_label(value_str: str | None) -> str | None:
if not value_str:
return None

value = int(value_str)
if value < 60:
return f"{value} minutes"
elif value >= 60 and value < 10080:
return f"{int(value / ONE_HOUR)} hours"
elif value == ONE_WEEK:
return f"{int(value / ONE_WEEK)} week"
elif value == ONE_DAY * 30:
return f"{int(value / ONE_DAY)} days"
return None


def get_changed_data(
rule: Rule, rule_data: dict[str, Any], rule_data_before: dict[str, Any]
) -> dict[str, Any]:
Expand All @@ -62,8 +82,10 @@ def get_changed_data(
rule_data, rule_data_before, rule, changed_data, "actions", "Removed action '{}'"
)

frequency_text = check_value_changed(rule_data, rule_data_before, "frequency", "frequency")
if frequency_text:
current_frequency = get_frequency_label(rule_data.get("frequency"))
previous_frequency = get_frequency_label(rule_data_before.get("frequency"))
if current_frequency != previous_frequency:
frequency_text = f"Changed frequency from *{previous_frequency}* to *{current_frequency}*"
changed_data["changed_frequency"].append(frequency_text)

if rule_data.get("environment_id") and not rule_data_before.get("environment_id"):
Expand Down
8 changes: 5 additions & 3 deletions tests/sentry/api/endpoints/test_project_rule_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,9 @@ def test_slack_confirmation_notification_contents(self):
"channel": "#old_channel_name",
}
]
self.rule.update(data={"conditions": conditions, "actions": actions}, label="my rule")
self.rule.update(
data={"conditions": conditions, "actions": actions, "frequency": 5}, label="my rule"
)

actions[0]["channel"] = "#new_channel_name"
actions[0]["channel_id"] = "new_channel_id"
Expand Down Expand Up @@ -1063,7 +1065,7 @@ def test_slack_confirmation_notification_contents(self):
"filterMatch": "any",
"actions": actions,
"conditions": conditions,
"frequency": 30,
"frequency": 180,
"environment": staging_env.name,
"owner": get_actor_for_user(self.user).get_actor_identifier(),
}
Expand All @@ -1081,7 +1083,7 @@ def test_slack_confirmation_notification_contents(self):
changes = "*Changes*\n"
changes += "• Added action 'Send a notification to the Awesome Team Slack workspace to new_channel_name (optionally, an ID: new_channel_id) and show tags [] in notification'\n"
changes += "• Removed action 'Send a notification to the Awesome Team Slack workspace to #old_channel_name (optionally, an ID: old_channel_id) and show tags [] in notification'\n"
changes += "• Changed frequency from *None* to *30*\n"
changes += "• Changed frequency from *5 minutes* to *3 hours*\n"
changes += f"• Added *{staging_env.name}* environment\n"
changes += "• Changed rule name from *my rule* to *new rule*\n"
changes += "• Changed trigger from *None* to *any*\n"
Expand Down

0 comments on commit 6f043be

Please sign in to comment.