* First attempt

* Working

* Tweak the mode handling a bit
This commit is contained in:
EllipticEllipsis 2022-09-22 13:03:46 +01:00 committed by GitHub
parent 86104d49d8
commit ff0116dbb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 0 deletions

View File

@ -93,6 +93,53 @@ class ProjectDataView(APIView):
return Response({project_slug: versions})
def get_progress_shield(
request: Request, project_slug: str, version_slug: str, category_slug: str
) -> dict[str, Any]:
latest = get_latest_entry(project_slug, version_slug, "default")
project = get_project(project_slug)
version = get_version(version_slug, project)
category = get_category(category_slug, version)
params = request.query_params
if not params:
raise InvalidDataException("No measure or total specified")
try:
measure = params["measure"]
numerator = latest[0]["measures"][measure]
except:
raise InvalidDataException("No measure specified")
label = params.get(
"label",
" ".join(
[
version.name,
category.name,
str(measure),
]
),
)
try:
total = params["total"]
denominator = latest[0]["measures"][total]
except:
raise InvalidDataException("No total specified")
fraction = float(numerator) / float(denominator)
message = f"{fraction:.2%}"
color = params.get("color", "yellow" if fraction < 1.0 else "green")
return {"schemaVersion": 1, "label": label, "message": message, "color": color}
VALID_MODES: set[str] = {"latest", "all", "shield"}
class VersionDataView(APIView):
"""
API endpoint that returns data for overall progress for a version of a project.
@ -145,10 +192,19 @@ class VersionDataView(APIView):
category_slug = DEFAULT_CATEGORY_SLUG
mode = self.request.query_params.get("mode", "latest")
if mode not in VALID_MODES:
raise InvalidDataException(f"Invalid mode specified: {mode}")
if mode == "latest":
entries = get_latest_entry(project_slug, version_slug, category_slug)
elif mode == "all":
entries = get_all_entries(project_slug, version_slug, category_slug)
elif mode == "shield":
return Response(
get_progress_shield(
self.request, project_slug, version_slug, category_slug
)
)
response_json = {project_slug: {version_slug: {category_slug: entries}}}
@ -181,10 +237,19 @@ class CategoryDataView(APIView):
"""
mode = self.request.query_params.get("mode", "latest")
if mode not in VALID_MODES:
raise InvalidDataException(f"Invalid mode specified: {mode}")
if mode == "latest":
entries = get_latest_entry(project_slug, version_slug, category_slug)
elif mode == "all":
entries = get_all_entries(project_slug, version_slug, category_slug)
elif mode == "shield":
return Response(
get_progress_shield(
self.request, project_slug, version_slug, category_slug
)
)
response_json = {project_slug: {version_slug: {category_slug: entries}}}