Skip to content

Commit b2fdfcd

Browse files
authored
fix(aci): Handle incorrect first_seen times (#93860)
Some groups have resolution/regression activities associated with the group that take place before the group's first seen. We can skip those events and start the open periods at the first_seen time, so the first open period would be `{"date_started": group.first_seen, "date_ended": first resolution activity > group.first_seen}`.
1 parent fb69b01 commit b2fdfcd

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/sentry/migrations/0925_backfill_open_periods.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ def get_open_periods_for_group(
6767
)
6868
]
6969

70+
# Since activities can apparently exist from before the start date, we want to ensure the
71+
# first open period starts at the first_seen date and ends at the first resolution activity after it.
72+
start_index = 0
73+
while activities and activities[start_index].type not in RESOLVED_ACTIVITY_TYPES:
74+
start_index += 1
75+
7076
open_periods = []
7177
regression_time: datetime | None = first_seen
72-
for activity in activities:
78+
for activity in activities[start_index:]:
7379
if activity.type == ActivityType.SET_REGRESSION.value and regression_time is None:
7480
regression_time = activity.datetime
7581

@@ -134,6 +140,10 @@ def _backfill_group_open_periods(
134140
group_id__in=group_ids,
135141
type__in=[ActivityType.SET_REGRESSION.value, *RESOLVED_ACTIVITY_TYPES],
136142
).order_by("datetime"):
143+
# Skip activities before the group's first_seen date
144+
if activity.datetime < activity.group.first_seen:
145+
continue
146+
137147
activities[activity.group_id].append(activity)
138148

139149
open_periods = []

tests/sentry/migrations/test_0925_backfill_open_periods.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,21 @@ def setup_before_migration(self, app):
312312
("regressed_group_with_auto_resolved_cycles", group, starts, ends, activities)
313313
)
314314

315+
# Create a group with activities before the first_seen date
316+
group, _, _, activities = self._create_resolved_group()
317+
activities[0].datetime = group.first_seen - timedelta(days=4)
318+
activities[0].save()
319+
320+
self.test_cases.append(
321+
(
322+
"resolved_group_with_activities_before_first_seen",
323+
group,
324+
[group.first_seen],
325+
[activities[-1].datetime],
326+
[activities[-1]],
327+
)
328+
)
329+
315330
def test(self):
316331
for description, group, starts, ends, activities in self.test_cases:
317332
group.refresh_from_db()

0 commit comments

Comments
 (0)