Skip to content

Commit

Permalink
Merge pull request #230 from ecmwf-projects/COPDS-2173-validity-check
Browse files Browse the repository at this point in the history
Check request validity
  • Loading branch information
mcucchi9 authored Dec 11, 2024
2 parents 1787d68 + 9b5ad0d commit df302d9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions cads_processing_api_service/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def post_process_execution(
adaptor = adaptors.instantiate_adaptor(adaptor_properties=adaptor_properties)
try:
request_inputs = adaptor.normalise_request(request_body.get("inputs", {}))
_ = adaptor.check_validity(request_inputs)
except cads_adaptors.exceptions.InvalidRequest as exc:
raise exceptions.InvalidRequest(detail=str(exc)) from exc
if dataset.api_enforce_constraints:
Expand Down
38 changes: 38 additions & 0 deletions cads_processing_api_service/costing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import Any

import cads_adaptors
import cads_adaptors.exceptions
import cads_catalogue
import fastapi

Expand All @@ -34,6 +35,7 @@ class RequestOrigin(str, enum.Enum):
def estimate_cost(
process_id: str = fastapi.Path(...),
request_origin: RequestOrigin = fastapi.Query("api"),
mandatory_inputs: bool = fastapi.Query(False),
execution_content: models.Execute = fastapi.Body(...),
) -> models.RequestCost:
"""
Expand Down Expand Up @@ -61,15 +63,51 @@ def estimate_cost(
resource_id=process_id, table=table, session=catalogue_session
)
adaptor_properties = adaptors.get_adaptor_properties(dataset)
request_is_valid = check_request_validity(
request=request,
mandatory_inputs=mandatory_inputs,
adaptor_properties=adaptor_properties,
)
costing_info = costing.compute_costing(
request.get("inputs", {}), adaptor_properties, request_origin
)
cost = costing.compute_highest_cost_limit_ratio(costing_info)
if costing_info.cost_bar_steps:
cost.cost_bar_steps = costing_info.cost_bar_steps
costing_info.request_is_valid = request_is_valid
return cost


def check_request_validity(
request: dict[str, Any], mandatory_inputs: bool, adaptor_properties: dict[str, Any]
) -> bool:
"""
Check if the request is valid.
Parameters
----------
request : dict[str, Any]
Request to be processed.
mandatory_inputs : bool
Whether mandatory inputs have been provided.
adaptor_properties : dict[str, Any]
Adaptor properties.
Returns
-------
bool
Whether the request is valid.
"""
if not mandatory_inputs:
return False
try:
adaptor = adaptors.instantiate_adaptor(adaptor_properties=adaptor_properties)
_ = adaptor.check_validity(request.get("inputs", {}))
return True
except cads_adaptors.exceptions.InvalidRequest:
return False


def compute_highest_cost_limit_ratio(
costing_info: models.CostingInfo,
) -> models.RequestCost:
Expand Down
1 change: 1 addition & 0 deletions cads_processing_api_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CostingInfo(pydantic.BaseModel):
costs: dict[str, float] = {}
limits: dict[str, float] = {}
cost_bar_steps: list[int] | None = None
request_is_valid: bool = True


class RequestCost(pydantic.BaseModel):
Expand Down

0 comments on commit df302d9

Please sign in to comment.