This repository was archived by the owner on Jun 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed
api/public/v2/test_results Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff line change 1+ from django .db .models import Count , Q
12from rest_framework import serializers
23
34from 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
You can’t perform that action at this time.
0 commit comments