version digest view

This commit is contained in:
Ethan Roseman 2022-08-22 17:54:38 +09:00
parent e656ab4e64
commit 9848c8db9a
No known key found for this signature in database
GPG Key ID: 27F9FCEB8E4969BD
2 changed files with 48 additions and 18 deletions

View File

@ -3,20 +3,20 @@ from frog_api import views
urlpatterns = [
path("projects/", views.ProjectView.as_view()),
path(
"data/",
views.RootDigestView.as_view(),
# re_path(
# "data/(?P<project>.+)/(?P<version>.+)/(?P<category>.+)/$",
# views.EntryView.as_view(),
# ),
re_path(
"data/(?P<project>.+)/(?P<version>.+)/$",
views.VersionDigestView.as_view(),
),
re_path(
"data/(?P<project>.+)/$",
views.ProjectDigestView.as_view(),
),
# re_path(
# "data/(?P<project>.+)/(?P<version>.+)/$",
# views.EntryView.as_view(),
# ),
# re_path(
# "data/(?P<project>.+)/(?P<version>.+)/(?P<category>.+)/$",
# views.EntryView.as_view(),
# ),
path(
"data/",
views.RootDigestView.as_view(),
),
]

View File

@ -32,16 +32,18 @@ def get_latest_entry(project, version, category) -> Optional[Entry]:
).first()
def get_entry_json(entry) -> dict:
entry_data = TerseEntrySerializer(entry).data
entry_data["measures"] = {m["type"]: m["value"] for m in entry_data["measures"]}
return entry_data
def get_versions_digest_for_project(project) -> dict:
versions = {}
for version in Version.objects.filter(project__slug=project):
entry = get_latest_entry(project, version.slug, "total")
if entry is not None:
entry_data = TerseEntrySerializer(entry).data
entry_data["measures"] = {
m["type"]: m["value"] for m in entry_data["measures"]
}
versions[version.slug] = entry_data
versions[version.slug] = get_entry_json(entry)
return versions
@ -74,8 +76,10 @@ class ProjectDigestView(APIView):
Return the most recent entry for overall progress for each version of a project.
"""
if Project.objects.get(slug=project) is None:
return Response(status=status.HTTP_404_NOT_FOUND)
if not Project.objects.filter(slug=project).exists():
return Response(
f"Project {project} not found", status=status.HTTP_404_NOT_FOUND
)
projects = {}
@ -84,3 +88,29 @@ class ProjectDigestView(APIView):
projects[project] = versions
return Response({"progress": projects})
class VersionDigestView(APIView):
"""
API endpoint that returns the most recent entry for a version of a project.
"""
def get(self, request, project, version):
"""
Return the most recent entry for overall progress for a version of a project.
"""
if not Project.objects.filter(slug=project).exists():
return Response(
f"Project {project} not found", status=status.HTTP_404_NOT_FOUND
)
if not Version.objects.filter(slug=version, project__slug=project).exists():
return Response(
f"Version {version} not found", status=status.HTTP_404_NOT_FOUND
)
entry = get_latest_entry(project, version, "total")
entry_json = get_entry_json(entry)
return Response(entry_json)