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
30 changes: 24 additions & 6 deletions apps/worker/services/test_analytics/ta_process_flakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_relevant_uploads(repo_id: int, commit_id: str) -> QuerySet[ReportSession
report__commit__repository__repoid=repo_id,
report__commit__commitid=commit_id,
state__in=["processed"],
)
).select_related("report", "report__commit", "report__commit__repository")


def fetch_current_flakes(repo_id: int) -> dict[bytes, Flake]:
Expand All @@ -42,6 +42,19 @@ def get_testruns(upload: ReportSession) -> QuerySet[Testrun]:
).order_by("timestamp")


def get_testruns_for_uploads(upload_ids: list[int]) -> dict[int, 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.

Dead function get_testruns left after refactoring

Low Severity

The get_testruns function in ta_process_flakes.py is now unused dead code. It was previously called by process_single_upload, but this refactoring replaced that call with the new batch-fetching get_testruns_for_uploads. No other file imports get_testruns from this module (the one in detect_flakes.py is a separate, locally-defined function). This dead function adds confusion about which function is canonical for fetching testruns.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 01a2798. Configure here.

# we won't process flakes for testruns older than 1 day
testruns = Testrun.objects.filter(
timestamp__gte=timezone.now() - timedelta(days=1),
upload_id__in=upload_ids,
).order_by("timestamp")

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


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 Down Expand Up @@ -81,10 +94,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],
):
testruns = get_testruns(upload)

for testrun in testruns:
test_id = bytes(testrun.test_id)
match testrun.outcome:
Expand All @@ -106,7 +120,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 +134,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