Skip to content

Commit 1ae38c5

Browse files
ref: fix reassignment of key_errors (#75186)
depending on the part of the code `key_errors` was either `list[tuple[int, int]]` or `list[tuple[Group, None, int]]` -- this is now explicitly separated as `key_errors_by_id: list[tuple[int, int]]` and `key_errors_by_group: list[tuple[Group, int]]` <!-- Describe your PR here. -->
1 parent f68aa36 commit 1ae38c5

File tree

6 files changed

+51
-63
lines changed

6 files changed

+51
-63
lines changed

src/sentry/integrations/slack/message_builder/notifications/daily_summary.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ def build(self) -> SlackBlock:
149149

150150
# Add Top 3 Error/Performance Issues
151151
top_issue_fields = []
152-
if context.key_errors:
152+
if context.key_errors_by_group:
153153
top_errors_text = "*Today's Top 3 Error Issues*\n"
154-
for error in context.key_errors:
155-
linked_title = self.linkify_error_title(error[0])
154+
for group, _ in context.key_errors_by_group:
155+
linked_title = self.linkify_error_title(group)
156156
top_errors_text += f"• {linked_title}\n"
157157
top_issue_fields.append(self.make_field(top_errors_text))
158158

src/sentry/tasks/summaries/daily_summary.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ def build_summary_data(
189189
ctx=ctx, project=project, referrer=Referrer.DAILY_SUMMARY_KEY_ERRORS.value
190190
)
191191
if key_errors:
192-
project_ctx.key_errors = [(e["events.group_id"], e["count()"]) for e in key_errors]
192+
project_ctx.key_errors_by_id = [
193+
(e["events.group_id"], e["count()"]) for e in key_errors
194+
]
193195

194196
# Today's Top 3 Performance Issues
195197
key_performance_issues = project_key_performance_issues(

src/sentry/tasks/summaries/utils.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class ProjectContext:
7575
def __init__(self, project):
7676
self.project = project
7777

78-
# Array of (group_id, group_history, count)
79-
self.key_errors = []
78+
self.key_errors_by_id: list[tuple[int, int]] = []
79+
self.key_errors_by_group: list[tuple[Group, int]] = []
8080
# Array of (transaction_name, count_this_week, p95_this_week, count_last_week, p95_last_week)
8181
self.key_transactions = []
8282
# Array of (Group, count)
@@ -94,7 +94,7 @@ def __init__(self, project):
9494
def __repr__(self):
9595
return "\n".join(
9696
[
97-
f"{self.key_errors}, ",
97+
f"{self.key_errors_by_group}, ",
9898
f"Errors: [Accepted {self.accepted_error_count}, Dropped {self.dropped_error_count}]",
9999
f"Transactions: [Accepted {self.accepted_transaction_count} Dropped {self.dropped_transaction_count}]",
100100
f"Replays: [Accepted {self.accepted_replay_count} Dropped {self.dropped_replay_count}]",
@@ -103,7 +103,7 @@ def __repr__(self):
103103

104104
def check_if_project_is_empty(self):
105105
return (
106-
not self.key_errors
106+
not self.key_errors_by_group
107107
and not self.key_transactions
108108
and not self.key_performance_issues
109109
and not self.accepted_error_count
@@ -116,26 +116,21 @@ def check_if_project_is_empty(self):
116116

117117

118118
class DailySummaryProjectContext:
119-
total_today = 0
120-
comparison_period_total = 0
121-
comparison_period_avg = 0
122-
key_errors: list[tuple[Group, int]] = []
123-
key_performance_issues: list[tuple[Group, int]] = []
124-
escalated_today: list[Group] = []
125-
regressed_today: list[Group] = []
126-
new_in_release: dict[int, list[Group]] = {}
127-
128119
def __init__(self, project: Project):
120+
self.total_today = 0
121+
self.comparison_period_total = 0
122+
self.comparison_period_avg = 0
129123
self.project = project
130-
self.key_errors = []
131-
self.key_performance_issues = []
132-
self.escalated_today = []
133-
self.regressed_today = []
134-
self.new_in_release = {}
124+
self.key_errors_by_id: list[tuple[int, int]] = []
125+
self.key_errors_by_group: list[tuple[Group, int]] = []
126+
self.key_performance_issues: list[tuple[Group, int]] = []
127+
self.escalated_today: list[Group] = []
128+
self.regressed_today: list[Group] = []
129+
self.new_in_release: dict[int, list[Group]] = {}
135130

136131
def check_if_project_is_empty(self):
137132
return (
138-
not self.key_errors
133+
not self.key_errors_by_group
139134
and not self.key_performance_issues
140135
and not self.total_today
141136
and not self.comparison_period_total
@@ -217,7 +212,7 @@ def project_key_errors(
217212
)
218213
query_result = raw_snql_query(request, referrer=referrer)
219214
key_errors = query_result["data"]
220-
# Set project_ctx.key_errors to be an array of (group_id, count) for now.
215+
# Set project_ctx.key_errors_by_id to be an array of (group_id, count) for now.
221216
# We will query the group history later on in `fetch_key_error_groups`, batched in a per-organization basis
222217
return key_errors
223218

@@ -346,7 +341,7 @@ def fetch_key_error_groups(ctx: OrganizationReportContext) -> None:
346341
# Organization pass. Depends on project_key_errors.
347342
all_key_error_group_ids = []
348343
for project_ctx in ctx.projects_context_map.values():
349-
all_key_error_group_ids.extend([group_id for group_id, count in project_ctx.key_errors])
344+
all_key_error_group_ids.extend([group_id for group_id, _ in project_ctx.key_errors_by_id])
350345

351346
if len(all_key_error_group_ids) == 0:
352347
return
@@ -358,19 +353,14 @@ def fetch_key_error_groups(ctx: OrganizationReportContext) -> None:
358353
for project_ctx in ctx.projects_context_map.values():
359354
# note Snuba might have groups that have since been deleted
360355
# we should just ignore those
361-
project_ctx.key_errors = list(
362-
filter(
363-
lambda x: x[0] is not None,
364-
[
365-
(
366-
group_id_to_group.get(group_id),
367-
None,
368-
count,
369-
)
370-
for group_id, count in project_ctx.key_errors
371-
],
356+
project_ctx.key_errors_by_group = [
357+
(group, count)
358+
for group, count in (
359+
(group_id_to_group.get(group_id), count)
360+
for group_id, count in project_ctx.key_errors_by_id
372361
)
373-
)
362+
if group is not None
363+
]
374364

375365

376366
def fetch_key_performance_issue_groups(ctx: OrganizationReportContext):

src/sentry/tasks/summaries/weekly_reports.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def prepare_organization_report(
177177

178178
project_ctx = cast(ProjectContext, ctx.projects_context_map[project.id])
179179
if key_errors:
180-
project_ctx.key_errors = [(e["events.group_id"], e["count()"]) for e in key_errors]
180+
project_ctx.key_errors_by_id = [
181+
(e["events.group_id"], e["count()"]) for e in key_errors
182+
]
181183

182184
if ctx.organization.slug == "sentry":
183185
logger.info(
@@ -636,7 +638,7 @@ def all_key_errors():
636638
"project_id": project_ctx.project.id,
637639
},
638640
)
639-
for group, group_history, count in project_ctx.key_errors:
641+
for group, count in project_ctx.key_errors_by_group:
640642
if ctx.organization.slug == "sentry":
641643
logger.info(
642644
"render_template_context.all_key_errors.found_error",
@@ -656,14 +658,8 @@ def all_key_errors():
656658
yield {
657659
"count": count,
658660
"group": group,
659-
"status": (
660-
group_history.get_status_display() if group_history else "Unresolved"
661-
),
662-
"status_color": (
663-
group_status_to_color[group_history.status]
664-
if group_history
665-
else group_status_to_color[GroupHistoryStatus.NEW]
666-
),
661+
"status": "Unresolved",
662+
"status_color": (group_status_to_color[GroupHistoryStatus.NEW]),
667663
"group_substatus": substatus,
668664
"group_substatus_color": substatus_color,
669665
"group_substatus_border_color": substatus_border_color,

src/sentry/web/frontend/debug/debug_weekly_report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def get_context(self, request):
8282
project_context.dropped_replay_count = int(
8383
random.weibullvariate(5, 1) * random.paretovariate(0.2)
8484
)
85-
project_context.key_errors = [
86-
(g, None, random.randint(0, 1000)) for g in Group.objects.all()[:3]
85+
project_context.key_errors_by_group = [
86+
(g, random.randint(0, 1000)) for g in Group.objects.all()[:3]
8787
]
8888

8989
project_context.new_substatus_count = random.randint(5, 200)

tests/sentry/tasks/test_daily_summary.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ def test_build_summary_data(self):
261261
)
262262
assert project_context_map.total_today == 17 # total outcomes from today
263263
assert project_context_map.comparison_period_avg == 1
264-
assert len(project_context_map.key_errors) == 3
265-
assert (self.group1, None, 3) in project_context_map.key_errors
266-
assert (self.group2, None, 2) in project_context_map.key_errors
267-
assert (self.group3, None, 10) in project_context_map.key_errors
264+
assert len(project_context_map.key_errors_by_group) == 3
265+
assert (self.group1, 3) in project_context_map.key_errors_by_group
266+
assert (self.group2, 2) in project_context_map.key_errors_by_group
267+
assert (self.group3, 10) in project_context_map.key_errors_by_group
268268
assert len(project_context_map.key_performance_issues) == 2
269-
assert (self.perf_event.group, None, 1) in project_context_map.key_performance_issues
270-
assert (self.perf_event2.group, None, 1) in project_context_map.key_performance_issues
269+
assert (self.perf_event.group, 1) in project_context_map.key_performance_issues
270+
assert (self.perf_event2.group, 1) in project_context_map.key_performance_issues
271271
assert project_context_map.escalated_today == [self.group3]
272272
assert project_context_map.regressed_today == [self.group2]
273273
assert len(project_context_map.new_in_release) == 2
@@ -281,7 +281,7 @@ def test_build_summary_data(self):
281281
)
282282
assert project_context_map2.total_today == 2
283283
assert project_context_map2.comparison_period_avg == 0
284-
assert project_context_map2.key_errors == [(self.group4, None, 2)]
284+
assert project_context_map2.key_errors_by_group == [(self.group4, 2)]
285285
assert project_context_map2.key_performance_issues == []
286286
assert project_context_map2.escalated_today == []
287287
assert project_context_map2.regressed_today == []
@@ -328,9 +328,9 @@ def test_build_summary_data_filter_to_unresolved(self):
328328
)
329329
assert project_context_map.total_today == 9 # total outcomes from today
330330
assert project_context_map.comparison_period_avg == 0
331-
assert len(project_context_map.key_errors) == 2
332-
assert (group1, None, 3) in project_context_map.key_errors
333-
assert (group2, None, 3) in project_context_map.key_errors
331+
assert len(project_context_map.key_errors_by_group) == 2
332+
assert (group1, 3) in project_context_map.key_errors_by_group
333+
assert (group2, 3) in project_context_map.key_errors_by_group
334334

335335
def test_build_summary_data_filter_to_error_level(self):
336336
"""Test that non-error level issues are filtered out of the results"""
@@ -371,10 +371,10 @@ def test_build_summary_data_filter_to_error_level(self):
371371
)
372372
assert project_context_map.total_today == 9 # total outcomes from today
373373
assert project_context_map.comparison_period_avg == 0
374-
assert len(project_context_map.key_errors) == 2
375-
assert (group1, None, 3) not in project_context_map.key_errors
376-
assert (group2, None, 3) in project_context_map.key_errors
377-
assert (group3, None, 3) in project_context_map.key_errors
374+
assert len(project_context_map.key_errors_by_group) == 2
375+
assert (group1, 3) not in project_context_map.key_errors_by_group
376+
assert (group2, 3) in project_context_map.key_errors_by_group
377+
assert (group3, 3) in project_context_map.key_errors_by_group
378378

379379
def test_build_summary_data_dedupes_groups(self):
380380
"""

0 commit comments

Comments
 (0)