Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Draft
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
8 changes: 7 additions & 1 deletion api/internal/coverage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Comment on lines +70 to +75
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To improve readability and maintainability, consider refactoring the sunburst method to avoid code duplication with the tree method. You can create a helper method to handle the common logic.

Suggested change
@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)
def serialize_paths(self, serializer_class):
paths = self.get_object()
serializer = serializer_class(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:
return self.serialize_paths(SunburstSerializer)

30 changes: 28 additions & 2 deletions api/shared/report/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from rest_framework import serializers

from services.path import Dir
from services.path import Dir, File


class TreeSerializer(serializers.Serializer):
Expand All @@ -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 | File) -> dict:
depth = self.context.get("depth", 1)
max_depth = self.context.get("max_depth", math.inf)
res = super().to_representation(instance)
Expand All @@ -29,3 +29,29 @@ 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 | File) -> dict:
depth = self.context.get("depth", 1)
max_depth = self.context.get("max_depth", math.inf)
res = super().to_representation(instance)

# 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,
many=True,
context={
"depth": depth + 1,
"max_depth": max_depth,
},
).data
return res