diff --git a/tcms/telemetry/api.py b/tcms/telemetry/api.py index 6b42a06b06..2feab7d3a3 100644 --- a/tcms/telemetry/api.py +++ b/tcms/telemetry/api.py @@ -226,6 +226,44 @@ def test_case_health(query=None): return data +@http_basic_auth_login_required +@rpc_method(name="Testing.individual_test_case_health") +def individual_test_case_health(query=None): + + if query is None: + query = {} + + res = {} + plan_id = 0 + positive, negative, all_count = 0, 0, 0 + for test_execution in ( + TestExecution.objects.filter(**query) + .values("run__plan", "case_id", "status__name", "status__weight") + .order_by("case", "run__plan", "status__weight") + ): + if test_execution["status__weight"] > 0: + positive += 1 + elif test_execution["status__weight"] < 0: + negative += 1 + all_count += 1 + + if test_execution["run__plan"] != plan_id: + plan_id = test_execution["run__plan"] + res[plan_id] = { + "completion_rate": (positive + negative) / all_count, + "failure_rate": negative / all_count, + } + positive, negative, all_count = 0, 0, 0 + + # append the last result + res[plan_id] = { + "completion_rate": (positive + negative) / all_count, + "failure_rate": negative / all_count, + } + + return res + + def _remove_all_excellent_executions(data): for key in dict.fromkeys(data): if data[key]["count"]["fail"] == 0: