Shields (#17)
* First attempt * Working * Tweak the mode handling a bit
This commit is contained in:
parent
86104d49d8
commit
ff0116dbb8
|
@ -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}}}
|
||||
|
||||
|
|
Loading…
Reference in New Issue