From 55a27571f275223836b5fb34e2ab8021b5a34bd3 Mon Sep 17 00:00:00 2001 From: Jerry Feng Date: Wed, 19 Feb 2025 16:14:57 -0500 Subject: [PATCH 1/2] Add a sunburst internal endpoint --- api/internal/coverage/views.py | 8 +++++++- api/shared/report/serializers.py | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/api/internal/coverage/views.py b/api/internal/coverage/views.py index 66998f9337..1f8113680f 100644 --- a/api/internal/coverage/views.py +++ b/api/internal/coverage/views.py @@ -9,7 +9,7 @@ import services.components as components_service from api.shared.mixins import RepoPropertyMixin from api.shared.permissions import RepositoryArtifactPermissions -from api.shared.report.serializers import TreeSerializer +from api.shared.report.serializers import SunburstSerializer, TreeSerializer from services.path import ReportPaths @@ -67,3 +67,9 @@ def tree(self, request: HttpRequest, *args: Any, **kwargs: Any) -> Response: paths = self.get_object() serializer = TreeSerializer(paths.single_directory(), many=True) return Response(serializer.data) + + @action(detail=False, methods=["get"], url_path="sunburst") + def sunburst(self, request: HttpRequest, *args: Any, **kwargs: Any) -> Response: + paths = self.get_object() + serializer = SunburstSerializer(paths.single_directory(), many=True) + return Response(serializer.data) diff --git a/api/shared/report/serializers.py b/api/shared/report/serializers.py index e6fe212256..62940b5b1b 100644 --- a/api/shared/report/serializers.py +++ b/api/shared/report/serializers.py @@ -14,7 +14,7 @@ class TreeSerializer(serializers.Serializer): partials = serializers.IntegerField() misses = serializers.IntegerField() - def to_representation(self, instance): + def to_representation(self, instance: Dir) -> dict: depth = self.context.get("depth", 1) max_depth = self.context.get("max_depth", math.inf) res = super().to_representation(instance) @@ -29,3 +29,25 @@ def to_representation(self, instance): }, ).data return res + + +class SunburstSerializer(serializers.Serializer): + name = serializers.CharField() + full_path = serializers.CharField() + value = serializers.FloatField() + + def to_representation(self, instance: Dir) -> dict: + depth = self.context.get("depth", 1) + max_depth = self.context.get("max_depth", math.inf) + res = super().to_representation(instance) + if isinstance(instance, Dir): + if depth < max_depth: + res["children"] = SunburstSerializer( + instance.children, + many=True, + context={ + "depth": depth + 1, + "max_depth": max_depth, + }, + ).data + return res From 81643ce36564d8d911f796df01853fb31d2ff566 Mon Sep 17 00:00:00 2001 From: Jerry Feng Date: Wed, 19 Feb 2025 17:04:11 -0500 Subject: [PATCH 2/2] fix attribute rename --- api/shared/report/serializers.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/api/shared/report/serializers.py b/api/shared/report/serializers.py index 62940b5b1b..7308f1d827 100644 --- a/api/shared/report/serializers.py +++ b/api/shared/report/serializers.py @@ -2,7 +2,7 @@ from rest_framework import serializers -from services.path import Dir +from services.path import Dir, File class TreeSerializer(serializers.Serializer): @@ -14,7 +14,7 @@ class TreeSerializer(serializers.Serializer): partials = serializers.IntegerField() misses = serializers.IntegerField() - def to_representation(self, instance: Dir) -> dict: + def to_representation(self, instance: Dir | File) -> dict: depth = self.context.get("depth", 1) max_depth = self.context.get("max_depth", math.inf) res = super().to_representation(instance) @@ -36,11 +36,15 @@ class SunburstSerializer(serializers.Serializer): full_path = serializers.CharField() value = serializers.FloatField() - def to_representation(self, instance: Dir) -> dict: + def to_representation(self, instance: Dir | File) -> dict: depth = self.context.get("depth", 1) max_depth = self.context.get("max_depth", math.inf) res = super().to_representation(instance) - if isinstance(instance, Dir): + + # Adjust the "value" field based on the instance type + if isinstance(instance, File): + res["value"] = instance.coverage + elif isinstance(instance, Dir): if depth < max_depth: res["children"] = SunburstSerializer( instance.children,