Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit da2bd3e

Browse files
committed
format doc
1 parent 4096022 commit da2bd3e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

api/public/v2/test_results/serializers.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.db.models import Count, Q
12
from rest_framework import serializers
23

34
from reports.models import TestInstance
@@ -21,12 +22,24 @@ class TestInstanceSerializer(serializers.ModelSerializer):
2122
)
2223

2324
def get_failure_rate(self, obj):
24-
test_instances = TestInstance.objects.filter(test=obj.test)
25-
total_runs = test_instances.count()
25+
"""Calculate the failure rate for a test.
26+
The failure rate is calculated as:
27+
number of failed test runs / total number of test runs
28+
Returns:
29+
float: A value between 0.0 and 1.0 representing the failure rate
30+
0.0 means no failures
31+
1.0 means all runs failed
32+
"""
33+
stats = TestInstance.objects.filter(test=obj.test).aggregate(
34+
total=Count("id"),
35+
failures=Count("id", filter=Q(outcome=TestInstance.Outcome.FAILURE.value)),
36+
)
37+
38+
total_runs = stats["total"]
2639
if total_runs == 0:
2740
return 0.0
28-
fail_count = test_instances.filter(outcome=TestInstance.Outcome.FAILURE.value).count()
29-
return fail_count / total_runs
41+
42+
return stats["failures"] / total_runs
3043

3144
class Meta:
3245
model = TestInstance

0 commit comments

Comments
 (0)