Improvements to get_progress_shield (#19)
- Use passed in category_slug - Default to {measure}/total when not specified - Use colors with better contrast
This commit is contained in:
parent
ba9bc093b0
commit
e98cbf8159
|
@ -1,6 +1,8 @@
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.template.defaultfilters import title
|
||||||
|
|
||||||
from frog_api.exceptions import (
|
from frog_api.exceptions import (
|
||||||
InvalidDataException,
|
InvalidDataException,
|
||||||
NoEntriesException,
|
NoEntriesException,
|
||||||
|
@ -96,7 +98,8 @@ class ProjectDataView(APIView):
|
||||||
def get_progress_shield(
|
def get_progress_shield(
|
||||||
request: Request, project_slug: str, version_slug: str, category_slug: str
|
request: Request, project_slug: str, version_slug: str, category_slug: str
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
latest = get_latest_entry(project_slug, version_slug, "default")
|
latest = get_latest_entry(project_slug, version_slug, category_slug)
|
||||||
|
latest_measures = latest[0]["measures"]
|
||||||
|
|
||||||
project = get_project(project_slug)
|
project = get_project(project_slug)
|
||||||
version = get_version(version_slug, project)
|
version = get_version(version_slug, project)
|
||||||
|
@ -104,35 +107,41 @@ def get_progress_shield(
|
||||||
|
|
||||||
params = request.query_params
|
params = request.query_params
|
||||||
if not 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")
|
raise InvalidDataException("No measure specified")
|
||||||
|
|
||||||
|
if "measure" in params:
|
||||||
|
measure = params["measure"]
|
||||||
|
else:
|
||||||
|
raise InvalidDataException("No measure specified")
|
||||||
|
if measure not in latest_measures:
|
||||||
|
raise InvalidDataException(f"Measure '{measure}' not found")
|
||||||
|
numerator = latest_measures[measure]
|
||||||
|
|
||||||
label = params.get(
|
label = params.get(
|
||||||
"label",
|
"label",
|
||||||
" ".join(
|
" ".join(
|
||||||
[
|
[
|
||||||
version.name,
|
version.name,
|
||||||
category.name,
|
category.name,
|
||||||
str(measure),
|
title(str(measure)),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
if "total" in params:
|
||||||
total = params["total"]
|
total = params["total"]
|
||||||
denominator = latest[0]["measures"][total]
|
elif f"{measure}/total" in latest_measures:
|
||||||
except:
|
total = f"{measure}/total"
|
||||||
|
else:
|
||||||
raise InvalidDataException("No total specified")
|
raise InvalidDataException("No total specified")
|
||||||
|
if total not in latest_measures:
|
||||||
|
raise InvalidDataException(f"Measure '{total}' not found")
|
||||||
|
denominator = latest_measures[total]
|
||||||
|
|
||||||
fraction = float(numerator) / float(denominator)
|
fraction = float(numerator) / float(denominator)
|
||||||
message = f"{fraction:.2%}"
|
message = f"{fraction:.2%}"
|
||||||
|
|
||||||
color = params.get("color", "yellow" if fraction < 1.0 else "green")
|
color = params.get("color", "informational" if fraction < 1.0 else "success")
|
||||||
|
|
||||||
return {"schemaVersion": 1, "label": label, "message": message, "color": color}
|
return {"schemaVersion": 1, "label": label, "message": message, "color": color}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue