api.services.catalog_index ========================== .. py:module:: api.services.catalog_index .. autoapi-nested-parse:: In-memory index of the Airtable catalog (brands, equipment types, Models with their dimensions, Variations with how many units they have) to rank which existing Référence variant a received item most likely is, before any AI is involved; the AI then decides within that shortlist. Loaded once and refreshed in the background when older than ``INDEX_TTL_SECONDS`` (about 40 Airtable pages); ``invalidate()`` after creating records. Attributes ---------- .. autoapisummary:: api.services.catalog_index.logger api.services.catalog_index.INDEX_TTL_SECONDS api.services.catalog_index.BRAND_TABLE api.services.catalog_index.FAMILLE_TABLE api.services.catalog_index.WEIGHTS Classes ------- .. autoapisummary:: api.services.catalog_index.Model api.services.catalog_index.Variation api.services.catalog_index.CatalogIndex Functions --------- .. autoapisummary:: api.services.catalog_index.normalize api.services.catalog_index.words api.services.catalog_index.similarity api.services.catalog_index.load_index api.services.catalog_index.get_index api.services.catalog_index.invalidate api.services.catalog_index.set_index Module Contents --------------- .. py:data:: logger .. py:data:: INDEX_TTL_SECONDS :value: 900 .. py:data:: BRAND_TABLE :value: 'Marque' .. py:data:: FAMILLE_TABLE :value: 'Familles' .. py:data:: WEIGHTS .. py:function:: normalize(text: str | None) -> str Lowercase, no accents nor punctuation: "Mikro 220 R" ~ "mikro-220r". .. py:function:: words(text: str | None) -> set[str] .. py:function:: similarity(a: str, b: str) -> float 0-1 between two normalized strings; containment counts a lot. .. py:class:: Model .. py:attribute:: id :type: str .. py:attribute:: modele :type: str .. py:attribute:: brand_id :type: str | None .. py:attribute:: brand_name :type: str | None .. py:attribute:: type_id :type: str | None .. py:attribute:: type_name :type: str | None .. py:attribute:: dims :type: dict[str, float | None] .. py:attribute:: variation_ids :type: list[str] :value: [] .. py:property:: key :type: str .. py:class:: Variation .. py:attribute:: id :type: str .. py:attribute:: name :type: str .. py:attribute:: model_id :type: str | None .. py:attribute:: is_variant :type: bool .. py:attribute:: variant_value :type: str | None .. py:attribute:: status :type: str | None .. py:attribute:: units :type: int .. py:attribute:: sale_price :type: float | None :value: None .. py:attribute:: buy_price :type: float | None :value: None .. py:class:: CatalogIndex .. py:attribute:: brands :type: dict[str, str] .. py:attribute:: types :type: dict[str, dict[str, Any]] .. py:attribute:: familles :type: dict[str, str] .. py:attribute:: models :type: dict[str, Model] .. py:attribute:: variations :type: dict[str, Variation] .. py:method:: model_of(variation: Variation) -> Model | None .. py:method:: describe(variation: Variation, **extra: Any) -> dict[str, Any] .. py:method:: rank_variations(identity: dict[str, Any], limit: int = 12) -> list[dict[str, Any]] Existing Variations scored against what is known of the item: brand, model reference, label text, product name, popularity. .. py:method:: rank_brands(*names: str | None, text: str = '', limit: int = 3) -> list[dict] .. py:method:: rank_models(reference: str | None, brand_ids: list[str] | None = None, limit: int = 3) -> list[dict] .. py:method:: rank_types(*names: str | None, limit: int = 3) -> list[dict] .. py:method:: candidate(model: Model, variation_ids: list[str] | None = None) -> dict[str, Any] A Model with its Variations, as the app's search lists them. .. py:method:: search_models(query: str, limit: int = 10) -> list[dict[str, Any]] Models matching a free-text search over brand, reference, equipment type and variation names: every word must match (whole word, start of a word, inside a word, or a close spelling), in any order; the most relevant first ("Rotor", "2424" and "rotor 2424" all find Rotor 2424). .. py:method:: suggest_models(identity: dict[str, Any], limit: int = 3) -> list[dict[str, Any]] Suggestions from what is known of the item (no query): its best ranked Variations, each shown as its Model with that Variation. .. py:method:: model_prices(model_ids: list[str]) -> dict[str, dict[str, Any]] Per known Model: its name and the mean of its Variations' sale and advised buy prices (None when no Variation has one). .. py:method:: brand_by_name(name: str | None) -> tuple[str, str] | None .. py:method:: type_by_name(name: str | None) -> tuple[str, dict[str, Any]] | None .. py:function:: load_index(fetch: collections.abc.Callable[..., list[dict[str, Any]]] = airtable.list_records) -> CatalogIndex .. py:function:: get_index() -> CatalogIndex The index; loaded on first use, refreshed in the background when stale. .. py:function:: invalidate() -> None After creating records: next use reloads in the background. .. py:function:: set_index(index: CatalogIndex | None) -> None Tests.