Skip to content

Commit

Permalink
♻️✨ refactoring of pricing plans (🗃️) (#4812)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Oct 2, 2023
1 parent a7f5940 commit a03ed85
Show file tree
Hide file tree
Showing 61 changed files with 3,203 additions and 2,702 deletions.
13 changes: 13 additions & 0 deletions api/specs/web-server/_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ServiceResourcesGet,
ServiceUpdate,
)
from models_library.api_schemas_webserver.resource_usage import ServicePricingPlanGet
from models_library.generics import Envelope
from simcore_service_webserver._meta import API_VTAG
from simcore_service_webserver.catalog._handlers import (
Expand Down Expand Up @@ -130,3 +131,15 @@ def get_service_resources(
_params: Annotated[ServicePathParams, Depends()],
):
...


@router.get(
"/catalog/services/{service_key:path}/{service_version}/pricing-plan",
response_model=Envelope[ServicePricingPlanGet],
summary="Retrieve default pricing plan for provided service",
tags=["pricing-plans"],
)
async def get_service_pricing_plan(
_params: Annotated[ServicePathParams, Depends()],
):
...
35 changes: 30 additions & 5 deletions api/specs/web-server/_resource_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@

from _common import assert_handler_signature_against_model
from fastapi import APIRouter, Query
from models_library.api_schemas_webserver.resource_usage import ServiceRunGet
from models_library.api_schemas_webserver.resource_usage import (
PricingUnitGet,
ServiceRunGet,
)
from models_library.generics import Envelope
from models_library.resource_tracker import PricingPlanId, PricingUnitId
from models_library.rest_pagination import DEFAULT_NUMBER_OF_ITEMS_PER_PAGE
from models_library.wallets import WalletID
from pydantic import NonNegativeInt
from simcore_service_webserver._meta import API_VTAG
from simcore_service_webserver.resource_usage._pricing_plans_handlers import (
_GetPricingPlanUnitPathParams,
)
from simcore_service_webserver.resource_usage._service_runs_handlers import (
_ListServicesPathParams,
_ListServicesResourceUsagesPathParams,
)

router = APIRouter(prefix=f"/{API_VTAG}", tags=["usage"])
router = APIRouter(prefix=f"/{API_VTAG}")


#
Expand All @@ -30,9 +37,10 @@


@router.get(
"/resource-usage/services",
"/services/-/resource-usages",
response_model=Envelope[list[ServiceRunGet]],
summary="Retrieve finished and currently running user services (user and product are taken from context, optionally wallet_id parameter might be provided).",
tags=["usage"],
)
async def list_resource_usage_services(
wallet_id: WalletID = Query(None),
Expand All @@ -43,5 +51,22 @@ async def list_resource_usage_services(


assert_handler_signature_against_model(
list_resource_usage_services, _ListServicesPathParams
list_resource_usage_services, _ListServicesResourceUsagesPathParams
)


@router.get(
"/pricing-plans/{pricing_plan_id}/pricing-units/{pricing_unit_id}",
response_model=Envelope[PricingUnitGet],
summary="Retrieve detail information about pricing unit",
tags=["pricing-plans"],
)
async def get_pricing_plan_unit(
pricing_plan_id: PricingPlanId, pricing_unit_id: PricingUnitId
):
...


assert_handler_signature_against_model(
get_pricing_plan_unit, _GetPricingPlanUnitPathParams
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from datetime import datetime
from decimal import Decimal
from typing import Any, ClassVar

from models_library.resource_tracker import (
PricingPlanClassification,
PricingPlanId,
PricingUnitCostId,
PricingUnitId,
)
from pydantic import BaseModel


class PricingUnitGet(BaseModel):
pricing_unit_id: PricingUnitId
unit_name: str
current_cost_per_unit: Decimal
current_cost_per_unit_id: PricingUnitCostId
default: bool
specific_info: dict

class Config:
schema_extra: ClassVar[dict[str, Any]] = {
"examples": [
{
"pricing_unit_id": 1,
"unit_name": "SMALL",
"current_cost_per_unit": 5.7,
"current_cost_per_unit_id": 1,
"default": True,
"specific_info": {},
}
]
}


class ServicePricingPlanGet(BaseModel):
pricing_plan_id: PricingPlanId
display_name: str
description: str
classification: PricingPlanClassification
created_at: datetime
pricing_plan_key: str
pricing_units: list[PricingUnitGet]

class Config:
schema_extra: ClassVar[dict[str, Any]] = {
"examples": [
{
"pricing_plan_id": 1,
"display_name": "Pricing Plan for Sleeper",
"description": "Special Pricing Plan for Sleeper",
"classification": "TIER",
"created_at": "2023-01-11 13:11:47.293595",
"pricing_plan_key": "pricing-plan-sleeper",
"pricing_units": [
PricingUnitGet.Config.schema_extra["examples"][0]
],
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime
from decimal import Decimal

from models_library.projects import ProjectID
from models_library.projects_nodes_io import NodeID
from models_library.resource_tracker import (
CreditTransactionStatus,
ServiceRunId,
ServiceRunStatus,
)
from models_library.services import ServiceKey, ServiceVersion
from models_library.users import UserID
from models_library.wallets import WalletID
from pydantic import BaseModel


class ServiceRunGet(BaseModel):
service_run_id: ServiceRunId
wallet_id: WalletID | None
wallet_name: str | None
user_id: UserID
project_id: ProjectID
project_name: str
node_id: NodeID
node_name: str
service_key: ServiceKey
service_version: ServiceVersion
service_type: str
service_resources: dict
started_at: datetime
stopped_at: datetime | None
service_run_status: ServiceRunStatus
# Cost in credits
credit_cost: Decimal | None
transaction_status: CreditTransactionStatus | None
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from decimal import Decimal

from models_library.resource_tracker import (
PricingDetailId,
PricingPlanClassification,
PricingPlanId,
ServiceRunId,
Expand All @@ -14,7 +13,7 @@

from ..projects import ProjectID
from ..projects_nodes_io import NodeID
from ..resource_tracker import ServiceRunStatus
from ..resource_tracker import PricingUnitId, ServiceRunStatus
from ..services import ServiceKey, ServiceVersion
from ._base import OutputSchema

Expand All @@ -41,18 +40,18 @@ class ServiceRunGet(
service_run_status: ServiceRunStatus


class PricingDetailMinimalGet(OutputSchema):
pricing_detail_id: PricingDetailId
class PricingUnitGet(OutputSchema):
pricing_unit_id: PricingUnitId
unit_name: str
cost_per_unit: Decimal
valid_from: datetime
simcore_default: bool
current_cost_per_unit: Decimal
default: bool


class PricingPlanGet(OutputSchema):
class ServicePricingPlanGet(OutputSchema):
pricing_plan_id: PricingPlanId
name: str
display_name: str
description: str
classification: PricingPlanClassification
created_at: datetime
details: list[PricingDetailMinimalGet]
pricing_plan_key: str
pricing_units: list[PricingUnitGet]
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ class RabbitResourceTrackingStartedMessage(RabbitResourceTrackingBaseMessage):
wallet_name: str | None

pricing_plan_id: int | None
pricing_detail_id: int | None
pricing_unit_id: int | None
pricing_unit_cost_id: int | None

product_name: str
simcore_user_agent: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

ServiceRunId: TypeAlias = str
PricingPlanId: TypeAlias = PositiveInt
PricingDetailId: TypeAlias = PositiveInt
PricingUnitId: TypeAlias = PositiveInt
PricingUnitCostId: TypeAlias = PositiveInt
CreditTransactionId: TypeAlias = PositiveInt


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class CreateServiceMetricsAdditionalParams(BaseModel):
wallet_id: WalletID | None
wallet_name: str | None
pricing_plan_id: int | None
pricing_detail_id: int | None
pricing_unit_id: int | None
pricing_unit_cost_id: int | None
product_name: str
simcore_user_agent: str
user_email: str
Expand All @@ -28,7 +29,8 @@ class Config:
"wallet_id": 1,
"wallet_name": "a private wallet for me",
"pricing_plan_id": 1,
"pricing_detail_id": 1,
"pricing_unit_id": 1,
"pricing_unit_detail_id": 1,
"product_name": "osparc",
"simcore_user_agent": "undefined",
"user_email": "[email protected]",
Expand Down
Loading

0 comments on commit a03ed85

Please sign in to comment.