Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions graphql_api/tests/test_test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ def test_flaky_filter_on_test_results(self) -> None:
created_at=datetime.datetime.now(),
repoid=repo.repoid,
branch="main",
flaky_fail_count=0,
)
_ = DailyTestRollupFactory(
test=test2,
created_at=datetime.datetime.now(),
repoid=repo.repoid,
branch="feature",
flaky_fail_count=1000,
)
res = self.fetch_test_analytics(
repo.name,
Expand Down
36 changes: 23 additions & 13 deletions utils/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Avg,
F,
Q,
QuerySet,
Sum,
Value,
)
Expand Down Expand Up @@ -298,6 +299,17 @@
return rows[left:]


def get_relevant_totals(
Copy link
Contributor

Choose a reason for hiding this comment

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

love this refactor!

repoid: int, branch: str | None, since: dt.datetime
) -> QuerySet:
if branch:
return DailyTestRollup.objects.filter(
repoid=repoid, date__gt=since, branch=branch

Check warning on line 307 in utils/test_results.py

View check run for this annotation

Codecov Notifications / codecov/patch

utils/test_results.py#L307

Added line #L307 was not covered by tests
)
else:
return DailyTestRollup.objects.filter(repoid=repoid, date__gt=since)


def generate_test_results(
ordering: str,
ordering_direction: str,
Expand Down Expand Up @@ -341,7 +353,7 @@
test_ids: set[str] | None = None

if term is not None:
totals = DailyTestRollup.objects.filter(repoid=repoid, date__gt=since)
totals = get_relevant_totals(repoid, branch, since)

totals = totals.filter(test__name__icontains=term).values("test_id")

Expand All @@ -357,19 +369,21 @@
test_ids = test_ids & filtered_test_ids if test_ids else filtered_test_ids

if parameter is not None:
totals = get_relevant_totals(repoid, branch, since)
match parameter:
case GENERATE_TEST_RESULT_PARAM.FLAKY:
flakes = Flake.objects.filter(
Q(repository_id=repoid)
& (Q(end_date__date__isnull=True) | Q(end_date__date__gt=since))
flaky_test_ids = (
Copy link
Contributor

Choose a reason for hiding this comment

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

being able to have a generalized pattern for all these makes it so much easier to read

totals.values("test")
.annotate(flaky_fail_count_sum=Sum("flaky_fail_count"))
.filter(flaky_fail_count_sum__gt=0)
.values("test_id")
)
flaky_test_id_set = {test["test_id"] for test in flaky_test_ids}

flaky_test_ids = set([flake.test_id for flake in flakes])

test_ids = test_ids & flaky_test_ids if test_ids else flaky_test_ids
test_ids = (
test_ids & flaky_test_id_set if test_ids else flaky_test_id_set
)
case GENERATE_TEST_RESULT_PARAM.FAILED:
totals = DailyTestRollup.objects.filter(repoid=repoid, date__gt=since)

failed_test_ids = (
totals.values("test")
.annotate(fail_count_sum=Sum("fail_count"))
Expand All @@ -382,8 +396,6 @@
test_ids & failed_test_id_set if test_ids else failed_test_id_set
)
case GENERATE_TEST_RESULT_PARAM.SKIPPED:
totals = DailyTestRollup.objects.filter(repoid=repoid, date__gt=since)

skipped_test_ids = (
totals.values("test")
.annotate(
Expand All @@ -400,8 +412,6 @@
test_ids & skipped_test_id_set if test_ids else skipped_test_id_set
)
case GENERATE_TEST_RESULT_PARAM.SLOWEST:
totals = DailyTestRollup.objects.filter(repoid=repoid, date__gt=since)

num_tests = totals.distinct("test_id").count()

slowest_test_ids = (
Expand Down
Loading