Skip to content

Commit

Permalink
Merge pull request #162 from deNBI/feat/granted-get
Browse files Browse the repository at this point in the history
feat(api): get granted credits, better quantize
  • Loading branch information
eKatchko authored Jun 28, 2022
2 parents d2d19a5 + 65d727a commit 76d6513
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/os_credits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Config(TypedDict):
API_CONTACT_BASE_URL: str
MAIL_CONTACT_URL: str
ENDPOINTS_ONLY: bool
OS_CREDITS_PRECISION: int
OS_CREDITS_PRECISION: str


default_config = Config(
Expand All @@ -186,7 +186,7 @@ class Config(TypedDict):
API_CONTACT_BASE_URL="",
MAIL_CONTACT_URL="",
ENDPOINTS_ONLY=False,
OS_CREDITS_PRECISION=2
OS_CREDITS_PRECISION="0.01"
)


Expand Down Expand Up @@ -215,8 +215,7 @@ def parse_config_from_environment() -> Config:

for int_value_key in [
"OS_CREDITS_WORKERS",
"POSTGRES_PORT",
"OS_CREDITS_PRECISION"
"POSTGRES_PORT"
]:
try:
int_value = int(environ[int_value_key])
Expand Down
7 changes: 4 additions & 3 deletions src/os_credits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,20 @@ async def costs_per_hour(request: web.Request) -> web.Response:
except JSONDecodeError:
raise web.HTTPBadRequest(reason="Invalid JSON")
returned_costs_per_hour = Decimal(0)
quantize_precision = Decimal(config["OS_CREDITS_PRECISION"])
for metric_name, spec in machine_specs.items():
try:
cost = Decimal(config["METRICS_TO_BILL"][metric_name])
spec = Decimal(spec)
returned_costs_per_hour += (spec * cost).quantize(config["OS_CREDITS_PRECISION"])
returned_costs_per_hour += (spec * cost).quantize(quantize_precision)
except KeyError:
raise web.HTTPNotFound(reason=f"Unknown measurement `{metric_name}`.")
except TypeError:
raise web.HTTPBadRequest(
reason=f"Parameter {metric_name} had wrong type."
)
return web.json_response(
float(returned_costs_per_hour.quantize(config["OS_CREDITS_PRECISION"]))
float(returned_costs_per_hour.quantize(quantize_precision))
)


Expand All @@ -392,5 +393,5 @@ async def get_current_credits(request: web.Request) -> web.Response:
if not project:
raise web.HTTPNotFound(reason=f"No credits found for {project_name}.")
return web.json_response(
{"current_credits": float(project[0].used_credits)}
{"current_credits": float(project[0].used_credits), "granted_credits": float(project[0].granted_credits)}
)

0 comments on commit 76d6513

Please sign in to comment.