Skip to content
Closed
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
68 changes: 40 additions & 28 deletions src/sentry/seer/autofix/autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from sentry.services.eventstore.models import Event, GroupEvent
from sentry.snuba.ourlogs import OurLogs
from sentry.snuba.referrer import Referrer
from sentry.tagstore.types import GroupTagKey, TagKey
from sentry.tasks.autofix import check_autofix_status
from sentry.users.models.user import User
from sentry.users.services.user.model import RpcUser
Expand Down Expand Up @@ -479,20 +480,26 @@ def _call_autofix(
return response.json().get("run_id")


def get_all_tags_overview(group: Group) -> dict[str, Any] | None:
def get_all_tags_overview(
group: Group, tag_keys: list[TagKey | GroupTagKey] | None = None
) -> dict[str, Any] | None:
"""
Get high-level overview of all tags for an issue.
Returns aggregated tag data with percentages for all tags.
Optionally pass a list of prefetched TagKey objects - otherwise this queries TagStore.
"""
tag_keys = tagstore.backend.get_group_tag_keys_and_top_values(
group,
[], # all environments
keys=None, # Get all tags
value_limit=3, # Get top 3 values per tag
tenant_ids={"organization_id": group.project.organization_id},
)
if tag_keys is None:
tag_keys = tagstore.backend.get_group_tag_keys_and_top_values(
group,
[], # all environments
keys=None, # Get all tags
value_limit=3, # Get top 3 values per tag
tenant_ids={"organization_id": group.project.organization_id},
)

all_tags: list[dict] = []
assert tag_keys is not None

all_tags: list[dict[str, Any]] = []

KEYS_TO_EXCLUDE = {
"release",
Expand All @@ -509,43 +516,48 @@ def get_all_tags_overview(group: Group) -> dict[str, Any] | None:
if tag.key.lower() in KEYS_TO_EXCLUDE:
continue

# Calculate percentages for each tag value
tag_data = {
total_values = tag.count or 0
top_values_list: list[dict[str, Any]] = []
tag_data: dict[str, Any] = {
"key": tag.key,
"name": tagstore.backend.get_tag_key_label(tag.key),
"total_values": tag.count,
"total_values": total_values,
"unique_values": getattr(tag, "values_seen", 0),
"top_values": [],
"top_values": top_values_list,
}

# Calculate percentages for each top value
if hasattr(tag, "top_values") and tag.top_values:
# Calculate total from top values
top_values_total = sum(tag_value.times_seen for tag_value in tag.top_values)
top_values_total = sum(int(tag_value.times_seen) for tag_value in tag.top_values)

for tag_value in tag.top_values:
percentage = round((tag_value.times_seen / tag.count) * 100) if tag.count > 0 else 0
times_seen = int(tag_value.times_seen)
percentage = round((times_seen / total_values) * 100) if total_values > 0 else 0

# Ensure no single value shows 100% when there are multiple values
has_multiple_values = len(tag.top_values) > 1 or top_values_total < tag.count
has_multiple_values = len(tag.top_values) > 1 or top_values_total < total_values
if has_multiple_values and percentage >= 100:
percentage = ">99"
percentage_str = ">99%"
elif percentage < 1:
percentage = "<1"
percentage_str = "<1%"
else:
percentage_str = f"{percentage}%"

tag_data["top_values"].append(
top_values_list.append(
{
"value": tag_value.value,
"count": tag_value.times_seen,
"percentage": (
f"{percentage}%" if isinstance(percentage, (int, float)) else percentage
),
"count": times_seen,
"percentage": percentage_str,
}
)

# Add "other" category if there are more values than the top values shown
if top_values_total < tag.count:
other_count = tag.count - top_values_total
other_percentage = round((other_count / tag.count) * 100) if tag.count > 0 else 0
if top_values_total < total_values:
other_count = total_values - top_values_total
other_percentage = (
round((other_count / total_values) * 100) if total_values > 0 else 0
)

# Apply the same percentage formatting rules
if other_percentage < 1:
Expand All @@ -555,15 +567,15 @@ def get_all_tags_overview(group: Group) -> dict[str, Any] | None:
else:
other_percentage_str = f"{other_percentage}%"

tag_data["top_values"].append(
top_values_list.append(
{
"value": "other",
"count": other_count,
"percentage": other_percentage_str,
}
)

if tag_data["top_values"]: # Only include tags that have values
if top_values_list: # Only include key in result if it has top_values
all_tags.append(tag_data)

logger.info(
Expand Down
Loading
Loading