Stelle den Frontend-MVP und die v0.3-API auf develop bereit, damit Dev API und PWA zusammen deployen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
827e6a2a11
commit
b276758ee1
152 files changed
+25846
-34
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,22 @@
|
||||
from collections.abc import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.settings import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
engine = create_engine(settings.database_url, pool_pre_ping=True)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
|
||||
def get_session() -> Generator[Session, None, None]:
|
||||
session = SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
session.commit()
|
||||
except Exception:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
@@ -0,0 +1,321 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, ForeignKeyConstraint, Integer, Numeric, String, Text, UniqueConstraint
|
||||
from sqlalchemy.dialects.postgresql import JSONB, UUID as PGUUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.persistence.models import Base
|
||||
|
||||
|
||||
class ProductCategoryRecord(Base):
|
||||
__tablename__ = "product_categories"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="ACTIVE")
|
||||
|
||||
|
||||
class BuyingProjectRecord(Base):
|
||||
__tablename__ = "buying_projects"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(
|
||||
["template_id", "template_version"],
|
||||
["product_profiles.id", "product_profiles.version"],
|
||||
name="fk_buying_projects_template",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
workspace_id: Mapped[str | None] = mapped_column(String(64))
|
||||
product_category_id: Mapped[str] = mapped_column(ForeignKey("product_categories.id"), nullable=False)
|
||||
market: Mapped[str] = mapped_column(String(8), nullable=False)
|
||||
currency: Mapped[str] = mapped_column(String(3), nullable=False)
|
||||
target_budget: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
template_id: Mapped[str | None] = mapped_column(String(128))
|
||||
template_version: Mapped[int | None] = mapped_column(Integer)
|
||||
shortlist_size: Mapped[int] = mapped_column(Integer, nullable=False, default=5)
|
||||
criteria_version: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
archived_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
@property
|
||||
def profile_id(self) -> str | None:
|
||||
return self.template_id
|
||||
|
||||
@property
|
||||
def profile_version(self) -> int | None:
|
||||
return self.template_version
|
||||
|
||||
|
||||
class BuyingProjectCriteriaRecord(Base):
|
||||
__tablename__ = "buying_project_criteria"
|
||||
|
||||
project_id: Mapped[UUID] = mapped_column(ForeignKey("buying_projects.id"), primary_key=True)
|
||||
version: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
definition_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class ProjectCriterionRecord(Base):
|
||||
__tablename__ = "project_criteria"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(
|
||||
["project_id", "version"],
|
||||
["buying_project_criteria.project_id", "buying_project_criteria.version"],
|
||||
name="fk_project_criteria_version",
|
||||
),
|
||||
UniqueConstraint("project_id", "version", "item_id", name="uq_project_criterion_item"),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
project_id: Mapped[UUID] = mapped_column(nullable=False)
|
||||
version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
item_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
criterion_definition_id: Mapped[UUID] = mapped_column(ForeignKey("criterion_definitions.id"), nullable=False)
|
||||
criterion_key: Mapped[str | None] = mapped_column(String(128))
|
||||
operator: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
value: Mapped[Any] = mapped_column(JSONB)
|
||||
enabled_for_discovery: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
@property
|
||||
def mandatory(self) -> bool:
|
||||
return self.enabled_for_discovery
|
||||
|
||||
@property
|
||||
def criteria_version(self) -> int:
|
||||
return self.version
|
||||
|
||||
|
||||
class SourceCategoryRecord(Base):
|
||||
__tablename__ = "source_categories"
|
||||
__table_args__ = (UniqueConstraint("source_id", "external_category_key", name="uq_source_category"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
external_category_key: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
source_url: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
product_category_id: Mapped[str | None] = mapped_column(ForeignKey("product_categories.id"))
|
||||
canonical_category: Mapped[str | None] = mapped_column(String(128))
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
last_verified_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
|
||||
class SourceFilterDefinitionRecord(Base):
|
||||
__tablename__ = "source_filter_definitions"
|
||||
__table_args__ = (UniqueConstraint("source_category_id", "external_filter_key", name="uq_source_filter"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
source_category_id: Mapped[UUID] = mapped_column(ForeignKey("source_categories.id"), nullable=False)
|
||||
external_filter_key: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
label: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
filter_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
canonical_unit: Mapped[str | None] = mapped_column(String(32))
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
|
||||
|
||||
class SourceFilterOptionRecord(Base):
|
||||
__tablename__ = "source_filter_options"
|
||||
__table_args__ = (UniqueConstraint("filter_id", "source_value", name="uq_filter_option"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
filter_id: Mapped[UUID] = mapped_column(ForeignKey("source_filter_definitions.id"), nullable=False)
|
||||
source_value: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
normalized_value: Mapped[Any] = mapped_column(JSONB)
|
||||
operator: Mapped[str | None] = mapped_column(String(16))
|
||||
unit: Mapped[str | None] = mapped_column(String(32))
|
||||
|
||||
|
||||
class SourceFilterRangeRecord(Base):
|
||||
__tablename__ = "source_filter_ranges"
|
||||
|
||||
filter_id: Mapped[UUID] = mapped_column(ForeignKey("source_filter_definitions.id"), primary_key=True)
|
||||
minimum: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
maximum: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
step: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
unit: Mapped[str | None] = mapped_column(String(32))
|
||||
|
||||
|
||||
class CriterionDefinitionRecord(Base):
|
||||
__tablename__ = "criterion_definitions"
|
||||
__table_args__ = (UniqueConstraint("key", name="uq_criterion_definition_key"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
origin: Mapped[str] = mapped_column(String(16), nullable=False, default="SOURCE")
|
||||
source: Mapped[str | None] = mapped_column(String(64))
|
||||
product_category_id: Mapped[str | None] = mapped_column(ForeignKey("product_categories.id"))
|
||||
label: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
value_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
unit: Mapped[str | None] = mapped_column(String(32))
|
||||
semantic: Mapped[str | None] = mapped_column(String(128))
|
||||
category: Mapped[str | None] = mapped_column(String(128))
|
||||
description: Mapped[str | None] = mapped_column(Text)
|
||||
scoring_attribute_key: Mapped[str | None] = mapped_column(String(128))
|
||||
|
||||
|
||||
class SourceAttributeMappingRecord(Base):
|
||||
__tablename__ = "source_attribute_mappings"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
source_category_id: Mapped[UUID | None] = mapped_column(ForeignKey("source_categories.id"))
|
||||
raw_name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
criterion_definition_id: Mapped[UUID] = mapped_column(ForeignKey("criterion_definitions.id"), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class CriterionMappingRecord(Base):
|
||||
__tablename__ = "criterion_mappings"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
criterion_definition_id: Mapped[UUID] = mapped_column(ForeignKey("criterion_definitions.id"), nullable=False)
|
||||
criterion_key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
source_category_id: Mapped[UUID] = mapped_column(ForeignKey("source_categories.id"), nullable=False)
|
||||
external_filter_key: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
source_value: Mapped[str | None] = mapped_column(String(256))
|
||||
encoding: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
definition_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class DiscoverySearchRecord(Base):
|
||||
__tablename__ = "discovery_searches"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
buying_project_id: Mapped[UUID] = mapped_column(ForeignKey("buying_projects.id"), nullable=False)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
source_category_id: Mapped[UUID] = mapped_column(ForeignKey("source_categories.id"), nullable=False)
|
||||
criteria_version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
mapping_version: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
generated_url: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
schedule_interval_minutes: Mapped[int] = mapped_column(Integer, nullable=False, default=180)
|
||||
broad_result_threshold: Mapped[int] = mapped_column(Integer, nullable=False, default=300)
|
||||
next_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
last_successful_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class SearchRunRecord(Base):
|
||||
__tablename__ = "search_runs"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
discovery_search_id: Mapped[UUID] = mapped_column(ForeignKey("discovery_searches.id"), nullable=False)
|
||||
criteria_version: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
mapping_version: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
generated_url: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
result_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
page_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
new_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
changed_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
unchanged_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
missing_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
http_status_summary: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict)
|
||||
error_code: Mapped[str | None] = mapped_column(String(64))
|
||||
|
||||
|
||||
class SourceProductReferenceRecord(Base):
|
||||
__tablename__ = "source_product_references"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
source_category_id: Mapped[UUID] = mapped_column(ForeignKey("source_categories.id"), nullable=False)
|
||||
source_external_id: Mapped[str | None] = mapped_column(String(256))
|
||||
source_url: Mapped[str | None] = mapped_column(Text)
|
||||
source_title: Mapped[str] = mapped_column(String(512), nullable=False)
|
||||
manufacturer: Mapped[str | None] = mapped_column(String(256))
|
||||
model: Mapped[str | None] = mapped_column(String(256))
|
||||
manufacturer_sku: Mapped[str | None] = mapped_column(String(128))
|
||||
gtin_ean: Mapped[str | None] = mapped_column(String(32))
|
||||
normalized_spec_fingerprint: Mapped[str | None] = mapped_column(Text)
|
||||
first_seen_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
last_seen_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
lifecycle_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
match_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
consecutive_missing_success_runs: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
product_variant_id: Mapped[UUID | None] = mapped_column(ForeignKey("product_variants.id"))
|
||||
|
||||
|
||||
class ProductObservationRecord(Base):
|
||||
__tablename__ = "product_observations"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
source_product_reference_id: Mapped[UUID] = mapped_column(ForeignKey("source_product_references.id"), nullable=False)
|
||||
search_run_id: Mapped[UUID] = mapped_column(ForeignKey("search_runs.id"), nullable=False)
|
||||
observed_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
raw_title: Mapped[str] = mapped_column(String(512), nullable=False)
|
||||
best_price: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
currency: Mapped[str | None] = mapped_column(String(3))
|
||||
availability: Mapped[str | None] = mapped_column(String(32))
|
||||
seller_count: Mapped[int | None] = mapped_column(Integer)
|
||||
|
||||
|
||||
class ProductObservationAttributeRecord(Base):
|
||||
__tablename__ = "product_observation_attributes"
|
||||
__table_args__ = (UniqueConstraint("observation_id", "attribute_key", name="uq_observation_attribute"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
observation_id: Mapped[UUID] = mapped_column(ForeignKey("product_observations.id"), nullable=False)
|
||||
criterion_definition_id: Mapped[UUID | None] = mapped_column(ForeignKey("criterion_definitions.id"))
|
||||
attribute_key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
raw_name: Mapped[str | None] = mapped_column(String(256))
|
||||
raw_value: Mapped[Any] = mapped_column(JSONB)
|
||||
normalized_value: Mapped[Any] = mapped_column(JSONB)
|
||||
unit: Mapped[str | None] = mapped_column(String(32))
|
||||
|
||||
|
||||
class SearchRunItemRecord(Base):
|
||||
__tablename__ = "search_run_items"
|
||||
__table_args__ = (UniqueConstraint("search_run_id", "source_product_reference_id", name="uq_run_item"),)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
search_run_id: Mapped[UUID] = mapped_column(ForeignKey("search_runs.id"), nullable=False)
|
||||
source_product_reference_id: Mapped[UUID] = mapped_column(ForeignKey("source_product_references.id"), nullable=False)
|
||||
observation_id: Mapped[UUID | None] = mapped_column(ForeignKey("product_observations.id"))
|
||||
diff_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
|
||||
|
||||
class ProjectCandidateRecord(Base):
|
||||
__tablename__ = "project_candidates"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("buying_project_id", "source_product_reference_id", name="uq_project_candidate"),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
buying_project_id: Mapped[UUID] = mapped_column(ForeignKey("buying_projects.id"), nullable=False)
|
||||
source_product_reference_id: Mapped[UUID] = mapped_column(ForeignKey("source_product_references.id"), nullable=False)
|
||||
product_variant_id: Mapped[UUID | None] = mapped_column(ForeignKey("product_variants.id"))
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
match_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
product_eligibility: Mapped[str | None] = mapped_column(String(16))
|
||||
product_score: Mapped[Decimal | None] = mapped_column(Numeric(6, 2))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class CandidateStatusHistoryRecord(Base):
|
||||
__tablename__ = "candidate_status_history"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
candidate_id: Mapped[UUID] = mapped_column(ForeignKey("project_candidates.id"), nullable=False)
|
||||
from_status: Mapped[str | None] = mapped_column(String(32))
|
||||
to_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
reason: Mapped[str | None] = mapped_column(String(128))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
@@ -0,0 +1,105 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from app.domain.enums import (
|
||||
Availability,
|
||||
NormalizationStatus,
|
||||
OfferCondition,
|
||||
ValueType,
|
||||
)
|
||||
from app.domain.models import (
|
||||
AttributeDefinition,
|
||||
AttributeStatement,
|
||||
Offer,
|
||||
PriceObservation,
|
||||
Product,
|
||||
ProductVariant,
|
||||
)
|
||||
from app.persistence import models as orm
|
||||
|
||||
|
||||
def decimal_or_none(value) -> Decimal | None:
|
||||
return None if value is None else Decimal(str(value))
|
||||
|
||||
|
||||
def to_definition(row: orm.AttributeDefinitionRecord) -> AttributeDefinition:
|
||||
return AttributeDefinition(
|
||||
key=row.key,
|
||||
value_type=ValueType(row.value_type),
|
||||
canonical_unit=row.canonical_unit,
|
||||
minimum=decimal_or_none(row.minimum),
|
||||
maximum=decimal_or_none(row.maximum),
|
||||
description=row.description,
|
||||
)
|
||||
|
||||
|
||||
def to_product(row: orm.ProductRecord) -> Product:
|
||||
return Product(
|
||||
id=row.id,
|
||||
category=row.category,
|
||||
brand=row.brand,
|
||||
name=row.name,
|
||||
model=row.model,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
def to_variant(row: orm.ProductVariantRecord) -> ProductVariant:
|
||||
return ProductVariant(
|
||||
id=row.id,
|
||||
product_id=row.product_id,
|
||||
name=row.name,
|
||||
manufacturer_sku=row.manufacturer_sku,
|
||||
identifiers=row.identifiers or {},
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
def to_attribute(row: orm.VariantAttributeRecord) -> AttributeStatement:
|
||||
return AttributeStatement(
|
||||
id=row.id,
|
||||
variant_id=row.variant_id,
|
||||
attribute=row.attribute_key,
|
||||
raw_value=row.raw_value,
|
||||
normalized_value=row.normalized_value,
|
||||
unit=row.unit,
|
||||
normalization_status=NormalizationStatus(row.normalization_status),
|
||||
normalizer_version=row.normalizer_version,
|
||||
confidence=Decimal(str(row.confidence)),
|
||||
source_id=row.source_id,
|
||||
source_reference=row.source_reference,
|
||||
observed_at=row.observed_at,
|
||||
ingested_at=row.ingested_at,
|
||||
normalization_error=row.normalization_error,
|
||||
)
|
||||
|
||||
|
||||
def to_offer(row: orm.OfferRecord) -> Offer:
|
||||
return Offer(
|
||||
id=row.id,
|
||||
variant_id=row.variant_id,
|
||||
source_id=row.source_id,
|
||||
external_id=row.external_id,
|
||||
seller=row.seller,
|
||||
url=row.url,
|
||||
market=row.market,
|
||||
currency=row.currency,
|
||||
condition=OfferCondition(row.condition),
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
def to_observation(row: orm.PriceObservationRecord) -> PriceObservation:
|
||||
return PriceObservation(
|
||||
id=row.id,
|
||||
offer_id=row.offer_id,
|
||||
source_id=row.source_id,
|
||||
external_observation_id=row.external_observation_id,
|
||||
observed_at=row.observed_at,
|
||||
ingested_at=row.ingested_at,
|
||||
item_price=Decimal(str(row.item_price)),
|
||||
shipping_price=decimal_or_none(row.shipping_price),
|
||||
currency=row.currency,
|
||||
availability=Availability(row.availability),
|
||||
raw_payload=row.raw_payload,
|
||||
confidence=Decimal(str(row.confidence)),
|
||||
)
|
||||
@@ -0,0 +1,249 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
ForeignKeyConstraint,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import JSONB, UUID as PGUUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class AttributeDefinitionRecord(Base):
|
||||
__tablename__ = "attribute_definitions"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"value_type IN ('number', 'string', 'boolean', 'string_list')",
|
||||
name="ck_attribute_definitions_value_type",
|
||||
),
|
||||
)
|
||||
|
||||
key: Mapped[str] = mapped_column(String(128), primary_key=True)
|
||||
value_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
canonical_unit: Mapped[str | None] = mapped_column(String(32))
|
||||
minimum: Mapped[Decimal | None] = mapped_column(Numeric(12, 4))
|
||||
maximum: Mapped[Decimal | None] = mapped_column(Numeric(12, 4))
|
||||
description: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
|
||||
class SourceRecord(Base):
|
||||
__tablename__ = "sources"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
adapter_type: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
|
||||
|
||||
class ScoringModelRecord(Base):
|
||||
__tablename__ = "scoring_models"
|
||||
|
||||
version: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
algorithm_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
definition: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
definition_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class ProductProfileRecord(Base):
|
||||
__tablename__ = "product_profiles"
|
||||
__table_args__ = (
|
||||
CheckConstraint("version > 0", name="ck_product_profiles_version"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(128), primary_key=True)
|
||||
version: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
category: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
scoring_version: Mapped[str] = mapped_column(ForeignKey("scoring_models.version"), nullable=False)
|
||||
definition: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
definition_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class ProductRecord(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
category: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
brand: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
model: Mapped[str | None] = mapped_column(String(128))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
variants: Mapped[list["ProductVariantRecord"]] = relationship(back_populates="product")
|
||||
|
||||
|
||||
class ProductVariantRecord(Base):
|
||||
__tablename__ = "product_variants"
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
product_id: Mapped[UUID] = mapped_column(ForeignKey("products.id"), nullable=False, index=True)
|
||||
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
manufacturer_sku: Mapped[str | None] = mapped_column(String(128))
|
||||
identifiers: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False, default=dict)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
product: Mapped[ProductRecord] = relationship(back_populates="variants")
|
||||
|
||||
|
||||
class VariantAttributeRecord(Base):
|
||||
__tablename__ = "variant_attributes"
|
||||
__table_args__ = (
|
||||
CheckConstraint("confidence >= 0 AND confidence <= 1", name="ck_variant_attributes_confidence"),
|
||||
CheckConstraint(
|
||||
"normalization_status IN ('normalized', 'unknown')",
|
||||
name="ck_variant_attributes_status",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
variant_id: Mapped[UUID] = mapped_column(ForeignKey("product_variants.id"), nullable=False)
|
||||
attribute_key: Mapped[str] = mapped_column(ForeignKey("attribute_definitions.key"), nullable=False)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
raw_value: Mapped[Any] = mapped_column(JSONB, nullable=False)
|
||||
normalized_value: Mapped[Any] = mapped_column(JSONB)
|
||||
unit: Mapped[str | None] = mapped_column(String(32))
|
||||
normalization_status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
normalizer_version: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
confidence: Mapped[Decimal] = mapped_column(Numeric(4, 3), nullable=False)
|
||||
source_reference: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
observed_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
ingested_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
normalization_error: Mapped[str | None] = mapped_column(Text)
|
||||
|
||||
|
||||
class OfferRecord(Base):
|
||||
__tablename__ = "offers"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("source_id", "external_id", name="uq_offers_source_external"),
|
||||
CheckConstraint("currency ~ '^[A-Z]{3}$'", name="ck_offers_currency"),
|
||||
CheckConstraint("condition IN ('new', 'refurbished', 'used')", name="ck_offers_condition"),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
variant_id: Mapped[UUID] = mapped_column(ForeignKey("product_variants.id"), nullable=False, index=True)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
external_id: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
seller: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
url: Mapped[str | None] = mapped_column(Text)
|
||||
market: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
currency: Mapped[str] = mapped_column(String(3), nullable=False)
|
||||
condition: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class PriceObservationRecord(Base):
|
||||
__tablename__ = "price_observations"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("offer_id", "external_observation_id", name="uq_price_obs_offer_external"),
|
||||
CheckConstraint("item_price > 0", name="ck_price_obs_item_price"),
|
||||
CheckConstraint("shipping_price IS NULL OR shipping_price >= 0", name="ck_price_obs_shipping"),
|
||||
CheckConstraint("confidence >= 0 AND confidence <= 1", name="ck_price_obs_confidence"),
|
||||
CheckConstraint(
|
||||
"availability IN ('in_stock', 'out_of_stock', 'unknown')",
|
||||
name="ck_price_obs_availability",
|
||||
),
|
||||
CheckConstraint("currency ~ '^[A-Z]{3}$'", name="ck_price_obs_currency"),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
offer_id: Mapped[UUID] = mapped_column(ForeignKey("offers.id"), nullable=False)
|
||||
source_id: Mapped[str] = mapped_column(ForeignKey("sources.id"), nullable=False)
|
||||
external_observation_id: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
observed_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
ingested_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
item_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), nullable=False)
|
||||
shipping_price: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
|
||||
currency: Mapped[str] = mapped_column(String(3), nullable=False)
|
||||
availability: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
raw_payload: Mapped[Any] = mapped_column(JSONB, nullable=False)
|
||||
confidence: Mapped[Decimal] = mapped_column(Numeric(4, 3), nullable=False)
|
||||
|
||||
|
||||
class EvaluationRecord(Base):
|
||||
__tablename__ = "evaluations"
|
||||
__table_args__ = (
|
||||
ForeignKeyConstraint(
|
||||
["profile_id", "profile_version"],
|
||||
["product_profiles.id", "product_profiles.version"],
|
||||
name="fk_evaluations_profile",
|
||||
),
|
||||
CheckConstraint(
|
||||
"product_eligibility IN ('ELIGIBLE', 'INELIGIBLE', 'UNKNOWN')",
|
||||
name="ck_eval_product_eligibility",
|
||||
),
|
||||
CheckConstraint(
|
||||
"offer_eligibility IN ('ELIGIBLE', 'INELIGIBLE', 'UNKNOWN')",
|
||||
name="ck_eval_offer_eligibility",
|
||||
),
|
||||
CheckConstraint(
|
||||
"overall_eligibility IN ('ELIGIBLE', 'INELIGIBLE', 'UNKNOWN')",
|
||||
name="ck_eval_overall_eligibility",
|
||||
),
|
||||
CheckConstraint(
|
||||
"product_score IS NULL OR (product_score >= 0 AND product_score <= 100)",
|
||||
name="ck_eval_product_score",
|
||||
),
|
||||
CheckConstraint(
|
||||
"price_score IS NULL OR (price_score >= 0 AND price_score <= 100)",
|
||||
name="ck_eval_price_score",
|
||||
),
|
||||
CheckConstraint(
|
||||
"deal_score IS NULL OR (deal_score >= 0 AND deal_score <= 100)",
|
||||
name="ck_eval_deal_score",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
variant_id: Mapped[UUID] = mapped_column(ForeignKey("product_variants.id"), nullable=False, index=True)
|
||||
offer_id: Mapped[UUID] = mapped_column(ForeignKey("offers.id"), nullable=False)
|
||||
profile_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
profile_version: Mapped[int] = mapped_column(nullable=False)
|
||||
scoring_version: Mapped[str] = mapped_column(ForeignKey("scoring_models.version"), nullable=False)
|
||||
as_of: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
snapshot_cutoff: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
scoring_hash: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
engine_build: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
product_eligibility: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
offer_eligibility: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
overall_eligibility: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
product_score: Mapped[Decimal | None] = mapped_column(Numeric(6, 2))
|
||||
price_score: Mapped[Decimal | None] = mapped_column(Numeric(6, 2))
|
||||
deal_score: Mapped[Decimal | None] = mapped_column(Numeric(6, 2))
|
||||
classification: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
price_reference_scope: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
input_snapshot: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
result_snapshot: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
components: Mapped[list["EvaluationComponentRecord"]] = relationship(back_populates="evaluation")
|
||||
|
||||
|
||||
class EvaluationComponentRecord(Base):
|
||||
__tablename__ = "evaluation_components"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("evaluation_id", "kind", "component_key", name="uq_eval_component"),
|
||||
)
|
||||
|
||||
id: Mapped[UUID] = mapped_column(PGUUID(as_uuid=True), primary_key=True)
|
||||
evaluation_id: Mapped[UUID] = mapped_column(ForeignKey("evaluations.id"), nullable=False)
|
||||
kind: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
component_key: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
raw_score: Mapped[Decimal | None] = mapped_column(Numeric(12, 6))
|
||||
weight: Mapped[Decimal | None] = mapped_column(Numeric(12, 6))
|
||||
contribution: Mapped[Decimal | None] = mapped_column(Numeric(12, 6))
|
||||
details: Mapped[dict[str, Any]] = mapped_column(JSONB, nullable=False)
|
||||
|
||||
evaluation: Mapped[EvaluationRecord] = relationship(back_populates="components")
|
||||
@@ -0,0 +1,183 @@
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.domain.models import AttributeDefinition, AttributeStatement, Offer, PriceObservation, Product, ProductVariant
|
||||
from app.domain.validation import parse_profile, parse_scoring_model
|
||||
from app.persistence import models as orm
|
||||
from app.persistence.mapping import to_attribute, to_definition, to_observation, to_offer, to_product, to_variant
|
||||
|
||||
|
||||
class CatalogRepository:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self.session = session
|
||||
|
||||
def get_definition(self, key: str) -> AttributeDefinition | None:
|
||||
row = self.session.get(orm.AttributeDefinitionRecord, key)
|
||||
return None if row is None else to_definition(row)
|
||||
|
||||
def list_definitions(self, limit: int, offset: int) -> tuple[list[AttributeDefinition], int]:
|
||||
total = self.session.scalar(select(func.count()).select_from(orm.AttributeDefinitionRecord)) or 0
|
||||
rows = self.session.scalars(
|
||||
select(orm.AttributeDefinitionRecord).order_by(orm.AttributeDefinitionRecord.key).limit(limit).offset(offset)
|
||||
).all()
|
||||
return [to_definition(row) for row in rows], total
|
||||
|
||||
def add_definition(self, row: orm.AttributeDefinitionRecord) -> AttributeDefinition:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_definition(row)
|
||||
|
||||
def list_sources(self) -> list[orm.SourceRecord]:
|
||||
return list(self.session.scalars(select(orm.SourceRecord).order_by(orm.SourceRecord.id)).all())
|
||||
|
||||
def get_source(self, source_id: str) -> orm.SourceRecord | None:
|
||||
return self.session.get(orm.SourceRecord, source_id)
|
||||
|
||||
def get_scoring_model(self, version: str):
|
||||
row = self.session.get(orm.ScoringModelRecord, version)
|
||||
return None if row is None else parse_scoring_model(row.definition)
|
||||
|
||||
def scoring_versions(self) -> set[str]:
|
||||
return set(self.session.scalars(select(orm.ScoringModelRecord.version)).all())
|
||||
|
||||
def all_definitions(self) -> dict[str, AttributeDefinition]:
|
||||
rows = self.session.scalars(select(orm.AttributeDefinitionRecord)).all()
|
||||
return {row.key: to_definition(row) for row in rows}
|
||||
|
||||
def get_profile(self, profile_id: str, version: int):
|
||||
row = self.session.get(orm.ProductProfileRecord, (profile_id, version))
|
||||
if row is None:
|
||||
return None
|
||||
model = self.get_scoring_model(row.scoring_version)
|
||||
if model is None:
|
||||
return None
|
||||
return parse_profile(
|
||||
row.definition,
|
||||
attribute_definitions=self.all_definitions(),
|
||||
scoring_versions=self.scoring_versions(),
|
||||
supported_strategies=set(model.supported_preference_strategies),
|
||||
)
|
||||
|
||||
def add_profile(self, row: orm.ProductProfileRecord):
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return row
|
||||
|
||||
def list_profiles(self, limit: int, offset: int) -> tuple[list[orm.ProductProfileRecord], int]:
|
||||
total = self.session.scalar(select(func.count()).select_from(orm.ProductProfileRecord)) or 0
|
||||
rows = self.session.scalars(
|
||||
select(orm.ProductProfileRecord)
|
||||
.order_by(orm.ProductProfileRecord.id, orm.ProductProfileRecord.version)
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
).all()
|
||||
return list(rows), total
|
||||
|
||||
def get_product(self, product_id: UUID) -> Product | None:
|
||||
row = self.session.get(orm.ProductRecord, product_id)
|
||||
return None if row is None else to_product(row)
|
||||
|
||||
def add_product(self, row: orm.ProductRecord) -> Product:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_product(row)
|
||||
|
||||
def get_variant(self, variant_id: UUID) -> ProductVariant | None:
|
||||
row = self.session.get(orm.ProductVariantRecord, variant_id)
|
||||
return None if row is None else to_variant(row)
|
||||
|
||||
def add_variant(self, row: orm.ProductVariantRecord) -> ProductVariant:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_variant(row)
|
||||
|
||||
def list_attributes(self, variant_id: UUID) -> list[AttributeStatement]:
|
||||
rows = self.session.scalars(
|
||||
select(orm.VariantAttributeRecord)
|
||||
.where(orm.VariantAttributeRecord.variant_id == variant_id)
|
||||
.order_by(
|
||||
orm.VariantAttributeRecord.observed_at,
|
||||
orm.VariantAttributeRecord.ingested_at,
|
||||
orm.VariantAttributeRecord.id,
|
||||
)
|
||||
).all()
|
||||
return [to_attribute(row) for row in rows]
|
||||
|
||||
def get_attribute(self, attribute_id: UUID) -> AttributeStatement | None:
|
||||
row = self.session.get(orm.VariantAttributeRecord, attribute_id)
|
||||
return None if row is None else to_attribute(row)
|
||||
|
||||
def add_attribute(self, row: orm.VariantAttributeRecord) -> AttributeStatement:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_attribute(row)
|
||||
|
||||
def get_offer(self, offer_id: UUID) -> Offer | None:
|
||||
row = self.session.get(orm.OfferRecord, offer_id)
|
||||
return None if row is None else to_offer(row)
|
||||
|
||||
def get_offer_by_external(self, source_id: str, external_id: str) -> Offer | None:
|
||||
row = self.session.scalar(
|
||||
select(orm.OfferRecord).where(
|
||||
orm.OfferRecord.source_id == source_id,
|
||||
orm.OfferRecord.external_id == external_id,
|
||||
)
|
||||
)
|
||||
return None if row is None else to_offer(row)
|
||||
|
||||
def add_offer(self, row: orm.OfferRecord) -> Offer:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_offer(row)
|
||||
|
||||
def list_observations(self, offer_id: UUID, limit: int | None = None, offset: int = 0) -> tuple[list[PriceObservation], int]:
|
||||
total = (
|
||||
self.session.scalar(
|
||||
select(func.count())
|
||||
.select_from(orm.PriceObservationRecord)
|
||||
.where(orm.PriceObservationRecord.offer_id == offer_id)
|
||||
)
|
||||
or 0
|
||||
)
|
||||
query = (
|
||||
select(orm.PriceObservationRecord)
|
||||
.where(orm.PriceObservationRecord.offer_id == offer_id)
|
||||
.order_by(
|
||||
orm.PriceObservationRecord.observed_at,
|
||||
orm.PriceObservationRecord.ingested_at,
|
||||
orm.PriceObservationRecord.id,
|
||||
)
|
||||
.offset(offset)
|
||||
)
|
||||
if limit is not None:
|
||||
query = query.limit(limit)
|
||||
rows = self.session.scalars(query).all()
|
||||
return [to_observation(row) for row in rows], total
|
||||
|
||||
def get_observation_by_external(self, offer_id: UUID, external_id: str) -> orm.PriceObservationRecord | None:
|
||||
return self.session.scalar(
|
||||
select(orm.PriceObservationRecord).where(
|
||||
orm.PriceObservationRecord.offer_id == offer_id,
|
||||
orm.PriceObservationRecord.external_observation_id == external_id,
|
||||
)
|
||||
)
|
||||
|
||||
def add_observation(self, row: orm.PriceObservationRecord) -> PriceObservation:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return to_observation(row)
|
||||
|
||||
def get_evaluation(self, evaluation_id: UUID) -> orm.EvaluationRecord | None:
|
||||
return self.session.get(orm.EvaluationRecord, evaluation_id)
|
||||
|
||||
def add_evaluation(self, row: orm.EvaluationRecord) -> orm.EvaluationRecord:
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
return row
|
||||
|
||||
def add_component(self, row: orm.EvaluationComponentRecord) -> None:
|
||||
self.session.add(row)
|
||||
Reference in new issue
Block a user