Skip to content

Commit

Permalink
🎨 Is922/adding filtering fields to usage endpoint (#5277)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Jan 29, 2024
1 parent 8b6562e commit 125ca14
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
15 changes: 8 additions & 7 deletions api/specs/web-server/_resource_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
_GetPricingPlanUnitPathParams,
)
from simcore_service_webserver.resource_usage._service_runs_handlers import (
ORDER_BY_DESCRIPTION,
_ListServicesResourceUsagesQueryParams,
_ListServicesResourceUsagesQueryParamsWithPagination,
)
Expand All @@ -47,12 +48,12 @@
)
async def list_resource_usage_services(
order_by: Annotated[
Json | None,
Json,
Query(
description="Order by field (started_at|stopped_at|credit_cost) and direction (asc|desc). The default sorting order is ascending.",
description="Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status) and direction (asc|desc). The default sorting order is ascending.",
example='{"field": "started_at", "direction": "desc"}',
),
] = None,
] = '{"field": "started_at", "direction": "desc"}',
filters: Annotated[
Json | None,
Query(
Expand Down Expand Up @@ -85,16 +86,16 @@ async def list_resource_usage_services(
)
async def export_resource_usage_services(
order_by: Annotated[
Json | None,
Json,
Query(
description="Order by field (started_at|stopped_at|credit_cost) and direction (asc|desc). The default sorting order is ascending.",
description="",
example='{"field": "started_at", "direction": "desc"}',
),
] = None,
] = '{"field": "started_at", "direction": "desc"}',
filters: Annotated[
Json | None,
Query(
description="Filters to process on the resource usages list, encoded as JSON. Currently supports the filtering of 'started_at' field with 'from' and 'until' parameters in <yyyy-mm-dd> ISO 8601 format. The date range specified is inclusive.",
description=ORDER_BY_DESCRIPTION,
example='{"started_at": {"from": "yyyy-mm-dd", "until": "yyyy-mm-dd"}}',
),
] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3095,15 +3095,14 @@ paths:
are taken from context, optionally wallet_id parameter might be provided).
operationId: list_resource_usage_services
parameters:
- description: Order by field (started_at|stopped_at|credit_cost) and direction
(asc|desc). The default sorting order is ascending.
- description: Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status)
and direction (asc|desc). The default sorting order is ascending.
required: false
schema:
title: Order By
type: string
description: Order by field (started_at|stopped_at|credit_cost) and direction
(asc|desc). The default sorting order is ascending.
format: json-string
description: Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status)
and direction (asc|desc). The default sorting order is ascending.
default: '{"field": "started_at", "direction": "desc"}'
example: '{"field": "started_at", "direction": "desc"}'
name: order_by
in: query
Expand Down Expand Up @@ -3162,30 +3161,21 @@ paths:
wallet_id parameter might be provided).
operationId: export_resource_usage_services
parameters:
- description: Order by field (started_at|stopped_at|credit_cost) and direction
(asc|desc). The default sorting order is ascending.
required: false
- required: false
schema:
title: Order By
type: string
description: Order by field (started_at|stopped_at|credit_cost) and direction
(asc|desc). The default sorting order is ascending.
format: json-string
default: '{"field": "started_at", "direction": "desc"}'
example: '{"field": "started_at", "direction": "desc"}'
name: order_by
in: query
- description: Filters to process on the resource usages list, encoded as JSON.
Currently supports the filtering of 'started_at' field with 'from' and 'until'
parameters in <yyyy-mm-dd> ISO 8601 format. The date range specified is
inclusive.
- description: Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status)
and direction (asc|desc). The default sorting order is ascending.
required: false
schema:
title: Filters
type: string
description: Filters to process on the resource usages list, encoded as
JSON. Currently supports the filtering of 'started_at' field with 'from'
and 'until' parameters in <yyyy-mm-dd> ISO 8601 format. The date range
specified is inclusive.
description: Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status)
and direction (asc|desc). The default sorting order is ascending.
format: json-string
example: '{"started_at": {"from": "yyyy-mm-dd", "until": "yyyy-mm-dd"}}'
name: filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def list_usage_services(
wallet_id: WalletID | None,
offset: int,
limit: NonNegativeInt,
order_by: OrderBy | None,
order_by: OrderBy,
filters: ServiceResourceUsagesFilters | None,
) -> ServiceRunPage:
access_all_wallet_usage = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ServiceRunPage,
)
from models_library.resource_tracker import ServiceResourceUsagesFilters
from models_library.rest_ordering import OrderBy
from models_library.rest_ordering import OrderBy, OrderDirection
from models_library.rest_pagination import (
DEFAULT_NUMBER_OF_ITEMS_PER_PAGE,
MAXIMUM_NUMBER_OF_ITEMS_PER_PAGE,
Expand Down Expand Up @@ -59,11 +59,14 @@ class _RequestContext(BaseModel):
product_name: str = Field(..., alias=RQ_PRODUCT_KEY) # type: ignore[pydantic-alias]


ORDER_BY_DESCRIPTION = "Order by field (wallet_id|wallet_name|user_id|project_id|project_name|node_id|node_name|service_key|service_version|service_type|started_at|stopped_at|service_run_status|credit_cost|transaction_status) and direction (asc|desc). The default sorting order is ascending."


class _ListServicesResourceUsagesQueryParams(BaseModel):
wallet_id: WalletID | None = Field(default=None)
order_by: Json[OrderBy | None] = Field( # pylint: disable=unsubscriptable-object
default=None,
description="Order by field (started_at|stopped_at|credit_cost) and direction (asc|desc). The default sorting order is ascending.",
order_by: Json[OrderBy] = Field( # pylint: disable=unsubscriptable-object
default=OrderBy(field="started_at", direction=OrderDirection.DESC),
description=ORDER_BY_DESCRIPTION,
example='{"field": "started_at", "direction": "desc"}',
)
filters: Json[ # pylint: disable=unsubscriptable-object
Expand All @@ -77,8 +80,26 @@ class _ListServicesResourceUsagesQueryParams(BaseModel):
@validator("order_by", allow_reuse=True)
@classmethod
def validate_order_by_field(cls, v):
if v.field not in {"started_at", "stopped_at", "credit_cost"}:
if v.field not in {
"wallet_id",
"wallet_name",
"user_id",
"project_id",
"project_name",
"node_id",
"node_name",
"service_key",
"service_version",
"service_type",
"started_at",
"stopped_at",
"service_run_status",
"credit_cost",
"transaction_status",
}:
raise ValueError(f"We do not support ordering by provided field {v.field}")
if v.field == "credit_cost":
v.field = "osparc_credits"
return v

class Config:
Expand Down Expand Up @@ -126,7 +147,7 @@ async def list_resource_usage_services(request: web.Request):
wallet_id=query_params.wallet_id,
offset=query_params.offset,
limit=query_params.limit,
order_by=parse_obj_as(OrderBy | None, query_params.order_by),
order_by=parse_obj_as(OrderBy, query_params.order_by),
filters=parse_obj_as(ServiceResourceUsagesFilters | None, query_params.filters),
)

Expand Down

0 comments on commit 125ca14

Please sign in to comment.