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
32 changes: 22 additions & 10 deletions apps/worker/services/test_analytics/ta_process_flakes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from collections import defaultdict
from datetime import timedelta

import sentry_sdk
Expand Down Expand Up @@ -33,14 +34,20 @@ def fetch_current_flakes(repo_id: int) -> dict[bytes, Flake]:
}


def get_testruns(upload: ReportSession) -> QuerySet[Testrun]:
upload_filter = Q(upload_id=upload.id)

def get_testruns_for_uploads(
upload_ids: list[int],
) -> dict[int, list[Testrun]]:
# we won't process flakes for testruns older than 1 day
return Testrun.objects.filter(
Q(timestamp__gte=timezone.now() - timedelta(days=1)) & upload_filter
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]] = defaultdict(list)
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
Expand Down Expand Up @@ -81,10 +88,11 @@ def handle_failure(

@sentry_sdk.trace
def process_single_upload(
upload: ReportSession, curr_flakes: dict[bytes, Flake], repo_id: int
upload: ReportSession,
curr_flakes: dict[bytes, Flake],
repo_id: int,
testruns: list[Testrun],
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused upload parameter in process_single_upload

Low Severity

The upload parameter in process_single_upload is no longer used anywhere in the function body. It was previously needed to call get_testruns(upload), but now that testruns are pre-fetched and passed directly, upload serves no purpose. This dead parameter adds confusion for future maintainers.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 4373f12. Configure here.

):
testruns = get_testruns(upload)

for testrun in testruns:
test_id = bytes(testrun.test_id)
match testrun.outcome:
Expand All @@ -106,7 +114,7 @@ def process_flakes_for_commit(repo_id: int, commit_id: str):
log.info(
"process_flakes_for_commit: starting processing",
)
uploads = get_relevant_uploads(repo_id, commit_id)
uploads = list(get_relevant_uploads(repo_id, commit_id))

log.info(
"process_flakes_for_commit: fetched uploads",
Expand All @@ -120,8 +128,12 @@ def process_flakes_for_commit(repo_id: int, commit_id: str):
extra={"flakes": [flake.test_id.hex() for flake in curr_flakes.values()]},
)

upload_ids = [upload.id for upload in uploads]
testruns_by_upload = get_testruns_for_uploads(upload_ids)

for upload in uploads:
process_single_upload(upload, curr_flakes, repo_id)
testruns = testruns_by_upload.get(upload.id, [])
process_single_upload(upload, curr_flakes, repo_id, testruns)
log.info(
"process_flakes_for_commit: processed upload",
extra={"upload": upload.id},
Expand Down
Loading