Skip to content
Open
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
34 changes: 26 additions & 8 deletions apps/worker/services/test_analytics/ta_process_flakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ def get_testruns(upload: ReportSession) -> QuerySet[Testrun]:
).order_by("timestamp")


def get_testruns_for_uploads(
uploads: QuerySet[ReportSession],
) -> dict[int, list[Testrun]]:
"""Fetch all testruns for all uploads in a single query, grouped by upload_id."""
upload_ids = [upload.id for upload in uploads]
testruns = Testrun.objects.filter(
Q(timestamp__gte=timezone.now() - timedelta(days=1))
& Q(upload_id__in=upload_ids)
).order_by("timestamp")

grouped: dict[int, list[Testrun]] = {uid: [] for uid in upload_ids}
for testrun in testruns:
grouped[testrun.upload_id].append(testrun)
return grouped


def handle_pass(curr_flakes: dict[bytes, Flake], test_id: bytes):
# possible that we expire it and stop caring about it
if test_id not in curr_flakes:
Expand All @@ -50,9 +66,8 @@ def handle_pass(curr_flakes: dict[bytes, Flake], test_id: bytes):
curr_flakes[test_id].recent_passes_count += 1
curr_flakes[test_id].count += 1
if curr_flakes[test_id].recent_passes_count == 30:
# Mark as expired; the final bulk_create(update_conflicts=True) will persist it
curr_flakes[test_id].end_date = timezone.now()
curr_flakes[test_id].save()
del curr_flakes[test_id]


def handle_failure(
Expand Down Expand Up @@ -81,10 +96,8 @@ def handle_failure(

@sentry_sdk.trace
def process_single_upload(
upload: ReportSession, curr_flakes: dict[bytes, Flake], repo_id: int
testruns: list[Testrun], curr_flakes: dict[bytes, Flake], repo_id: int
):
testruns = get_testruns(upload)

for testrun in testruns:
test_id = bytes(testrun.test_id)
match testrun.outcome:
Expand All @@ -98,8 +111,6 @@ def process_single_upload(
case _:
continue

Testrun.objects.bulk_update(testruns, ["outcome"])


@sentry_sdk.trace
def process_flakes_for_commit(repo_id: int, commit_id: str):
Expand All @@ -120,13 +131,20 @@ def process_flakes_for_commit(repo_id: int, commit_id: str):
extra={"flakes": [flake.test_id.hex() for flake in curr_flakes.values()]},
)

testruns_by_upload = get_testruns_for_uploads(uploads)
all_testruns: list[Testrun] = []

for upload in uploads:
process_single_upload(upload, curr_flakes, repo_id)
upload_testruns = testruns_by_upload.get(upload.id, [])
process_single_upload(upload_testruns, curr_flakes, repo_id)
all_testruns.extend(upload_testruns)
log.info(
"process_flakes_for_commit: processed upload",
extra={"upload": upload.id},
)

Testrun.objects.bulk_update(all_testruns, ["outcome"])

log.info(
"process_flakes_for_commit: bulk creating flakes",
extra={"flakes": [flake.test_id.hex() for flake in curr_flakes.values()]},
Expand Down
Loading