013 add price multiplier
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Application configuration split by architecture component."""
|
||||
|
||||
from decimal import Decimal
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
import sys
|
||||
@@ -29,6 +30,7 @@ class ServiceConfig(BaseModel):
|
||||
|
||||
class BusinessLogicConfig(BaseModel):
|
||||
weight_round_scale: int = 2
|
||||
provider_price_multiplier: Decimal = Field(..., gt=0)
|
||||
|
||||
|
||||
class RepositoryConfig(BaseModel):
|
||||
|
||||
@@ -35,6 +35,7 @@ def _build_aggregator_service(settings: Settings) -> AggregatorService:
|
||||
providers=providers,
|
||||
cache=cache,
|
||||
weight_round_scale=settings.business_logic.weight_round_scale,
|
||||
provider_price_multiplier=settings.business_logic.provider_price_multiplier,
|
||||
)
|
||||
return service
|
||||
|
||||
|
||||
+53
-6
@@ -6,10 +6,12 @@ from decimal import ROUND_HALF_UP, Decimal
|
||||
from typing import Protocol
|
||||
|
||||
DEFAULT_WEIGHT_ROUND_SCALE = 2
|
||||
DEFAULT_PROVIDER_PRICE_MULTIPLIER = Decimal("1")
|
||||
MAX_ROUND_SCALE = 6
|
||||
DIMENSIONS_ROUND_SCALE = 1
|
||||
MIN_WEIGHT_KG = Decimal("0.01")
|
||||
MIN_DIMENSION_CM = Decimal("0.1")
|
||||
INTEGER_PRICE_QUANTIZER = Decimal("1")
|
||||
|
||||
_MISSING = object()
|
||||
|
||||
@@ -77,12 +79,20 @@ def normalize_delivery_request(
|
||||
)
|
||||
|
||||
|
||||
def filter_valid_prices(prices: Iterable[object]) -> list[ProviderPrice]:
|
||||
def filter_valid_prices(
|
||||
prices: Iterable[object],
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> list[ProviderPrice]:
|
||||
"""Normalize price records and keep only domain-valid entries."""
|
||||
|
||||
normalized_multiplier = _normalize_price_multiplier(price_multiplier)
|
||||
valid_prices: list[ProviderPrice] = []
|
||||
for price_entry in prices:
|
||||
normalized_price = _normalize_price(price_entry)
|
||||
normalized_price = _normalize_price(
|
||||
price_entry,
|
||||
price_multiplier=normalized_multiplier,
|
||||
)
|
||||
if normalized_price is not None:
|
||||
valid_prices.append(normalized_price)
|
||||
return valid_prices
|
||||
@@ -94,13 +104,23 @@ def sort_prices_by_price(prices: Iterable[ProviderPrice]) -> list[ProviderPrice]
|
||||
return sorted(prices, key=lambda price_entry: price_entry.price)
|
||||
|
||||
|
||||
def filter_and_sort_prices(prices: Iterable[object]) -> list[ProviderPrice]:
|
||||
def filter_and_sort_prices(
|
||||
prices: Iterable[object],
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> list[ProviderPrice]:
|
||||
"""Apply domain filtering and return prices ordered by value."""
|
||||
|
||||
return sort_prices_by_price(filter_valid_prices(prices))
|
||||
return sort_prices_by_price(
|
||||
filter_valid_prices(prices, price_multiplier=price_multiplier)
|
||||
)
|
||||
|
||||
|
||||
def _normalize_price(candidate: object) -> ProviderPrice | None:
|
||||
def _normalize_price(
|
||||
candidate: object,
|
||||
*,
|
||||
price_multiplier: Decimal,
|
||||
) -> ProviderPrice | None:
|
||||
provider = _normalize_text(_get_optional_attr(candidate, "provider"))
|
||||
service_name = _normalize_text(_get_optional_attr(candidate, "service_name"))
|
||||
currency = _normalize_text(_get_optional_attr(candidate, "currency")).upper()
|
||||
@@ -117,6 +137,12 @@ def _normalize_price(candidate: object) -> ProviderPrice | None:
|
||||
price = _try_to_decimal(price_raw)
|
||||
if price is None or not price.is_finite() or price <= 0:
|
||||
return None
|
||||
adjusted_price = _apply_price_multiplier_and_round(
|
||||
price,
|
||||
price_multiplier=price_multiplier,
|
||||
)
|
||||
if adjusted_price is None:
|
||||
return None
|
||||
|
||||
min_days = _try_to_int(min_days_raw)
|
||||
max_days = _try_to_int(max_days_raw)
|
||||
@@ -129,7 +155,7 @@ def _normalize_price(candidate: object) -> ProviderPrice | None:
|
||||
return ProviderPrice(
|
||||
provider=provider,
|
||||
service_name=service_name,
|
||||
price=price,
|
||||
price=adjusted_price,
|
||||
currency=currency,
|
||||
delivery_days_min=min_days,
|
||||
delivery_days_max=max_days,
|
||||
@@ -192,6 +218,27 @@ def _round_half_up(value: Decimal, scale: int) -> Decimal:
|
||||
return value.quantize(quantizer, rounding=ROUND_HALF_UP)
|
||||
|
||||
|
||||
def _normalize_price_multiplier(value: Decimal) -> Decimal:
|
||||
multiplier = _try_to_decimal(value)
|
||||
if multiplier is None or not multiplier.is_finite() or multiplier <= 0:
|
||||
raise ValueError(f"Price multiplier must be a positive finite Decimal: {value!r}")
|
||||
return multiplier
|
||||
|
||||
|
||||
def _apply_price_multiplier_and_round(
|
||||
price: Decimal,
|
||||
*,
|
||||
price_multiplier: Decimal,
|
||||
) -> Decimal | None:
|
||||
adjusted_price = (price * price_multiplier).quantize(
|
||||
INTEGER_PRICE_QUANTIZER,
|
||||
rounding=ROUND_HALF_UP,
|
||||
)
|
||||
if not adjusted_price.is_finite() or adjusted_price <= 0:
|
||||
return None
|
||||
return adjusted_price
|
||||
|
||||
|
||||
def _to_decimal(value: object) -> Decimal:
|
||||
converted = _try_to_decimal(value)
|
||||
if converted is None:
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
from collections.abc import Callable, Iterable, Sequence
|
||||
from collections.abc import Iterable, Sequence
|
||||
from decimal import Decimal
|
||||
from typing import Protocol
|
||||
|
||||
from app.adapters.delivery_providers.base import DeliveryProvider, ProviderRequestError
|
||||
from app.domain.price import (
|
||||
DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
DEFAULT_WEIGHT_ROUND_SCALE,
|
||||
NormalizedDeliveryRequest,
|
||||
filter_and_sort_prices,
|
||||
@@ -31,6 +33,15 @@ class PriceCacheProtocol(Protocol):
|
||||
async def set(self, key: str, value: object, ttl: int | None = None) -> None: ...
|
||||
|
||||
|
||||
class FilterAndSortPricesFn(Protocol):
|
||||
def __call__(
|
||||
self,
|
||||
prices: Iterable[object],
|
||||
*,
|
||||
price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
) -> list[object]: ...
|
||||
|
||||
|
||||
class AggregatorService:
|
||||
def __init__(
|
||||
self,
|
||||
@@ -38,13 +49,13 @@ class AggregatorService:
|
||||
cache: PriceCacheProtocol | None = None,
|
||||
*,
|
||||
weight_round_scale: int = DEFAULT_WEIGHT_ROUND_SCALE,
|
||||
filter_and_sort_prices_fn: Callable[[Iterable[object]], list[object]] = (
|
||||
filter_and_sort_prices
|
||||
),
|
||||
provider_price_multiplier: Decimal = DEFAULT_PROVIDER_PRICE_MULTIPLIER,
|
||||
filter_and_sort_prices_fn: FilterAndSortPricesFn = filter_and_sort_prices,
|
||||
) -> None:
|
||||
self._providers = tuple(providers)
|
||||
self._cache = cache
|
||||
self._weight_round_scale = weight_round_scale
|
||||
self._provider_price_multiplier = provider_price_multiplier
|
||||
self._filter_and_sort_prices = filter_and_sort_prices_fn
|
||||
|
||||
async def get_all_prices(self, request: DeliveryRequest) -> list[DeliveryPrice]:
|
||||
@@ -86,7 +97,10 @@ class AggregatorService:
|
||||
raise InvalidDeliveryRequestError(
|
||||
"Delivery request is invalid for configured providers."
|
||||
)
|
||||
filtered_and_sorted = self._filter_and_sort_prices(successful_results)
|
||||
filtered_and_sorted = self._filter_and_sort_prices(
|
||||
successful_results,
|
||||
price_multiplier=self._provider_price_multiplier,
|
||||
)
|
||||
return [self._coerce_delivery_price(price) for price in filtered_and_sorted]
|
||||
|
||||
async def _get_provider_price(
|
||||
|
||||
Reference in New Issue
Block a user