This commit introduces a protocol-based metrics architecture that allows each module to provide its own statistics for dashboards without creating cross-module dependencies. Key changes: - Add MetricsProviderProtocol and MetricValue dataclass in contracts module - Add StatsAggregatorService in core module that discovers and aggregates metrics from all enabled modules - Implement metrics providers for all modules: - tenancy: vendor/user counts, team members, domains - customers: customer counts - cms: pages, media files - catalog: products - inventory: stock levels - orders: order counts, revenue - marketplace: import jobs, staging products - Update dashboard routes to use StatsAggregator instead of direct imports - Fix VendorPlatform junction table usage (Vendor.platform_id doesn't exist) - Add comprehensive documentation for the pattern This architecture ensures: - Dashboards always work (aggregator in core) - Each module owns its metrics (no cross-module coupling) - Optional modules are truly optional (can be removed without breaking app) - Multi-platform vendors are properly supported via VendorPlatform table Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
# app/modules/catalog/definition.py
|
|
"""Catalog module definition."""
|
|
|
|
from app.modules.base import (
|
|
MenuItemDefinition,
|
|
MenuSectionDefinition,
|
|
ModuleDefinition,
|
|
PermissionDefinition,
|
|
)
|
|
from app.modules.enums import FrontendType
|
|
|
|
|
|
# =============================================================================
|
|
# Router Lazy Imports
|
|
# =============================================================================
|
|
|
|
|
|
def _get_admin_router():
|
|
"""Lazy import of admin router to avoid circular imports."""
|
|
from app.modules.catalog.routes.api.admin import admin_router
|
|
|
|
return admin_router
|
|
|
|
|
|
def _get_vendor_router():
|
|
"""Lazy import of vendor router to avoid circular imports."""
|
|
from app.modules.catalog.routes.api.vendor import vendor_router
|
|
|
|
return vendor_router
|
|
|
|
|
|
def _get_metrics_provider():
|
|
"""Lazy import of metrics provider to avoid circular imports."""
|
|
from app.modules.catalog.services.catalog_metrics import catalog_metrics_provider
|
|
|
|
return catalog_metrics_provider
|
|
|
|
|
|
# Catalog module definition
|
|
catalog_module = ModuleDefinition(
|
|
code="catalog",
|
|
name="Product Catalog",
|
|
description="Product catalog browsing and search for storefronts",
|
|
version="1.0.0",
|
|
is_self_contained=True,
|
|
requires=["inventory"],
|
|
features=[
|
|
"product_catalog", # Core product catalog functionality
|
|
"product_search", # Search and filtering
|
|
"product_variants", # Product variants management
|
|
"product_categories", # Category organization
|
|
"product_attributes", # Custom attributes
|
|
"product_import_export", # Bulk import/export
|
|
],
|
|
# Module-driven permissions
|
|
permissions=[
|
|
PermissionDefinition(
|
|
id="products.view",
|
|
label_key="catalog.permissions.products_view",
|
|
description_key="catalog.permissions.products_view_desc",
|
|
category="products",
|
|
),
|
|
PermissionDefinition(
|
|
id="products.create",
|
|
label_key="catalog.permissions.products_create",
|
|
description_key="catalog.permissions.products_create_desc",
|
|
category="products",
|
|
),
|
|
PermissionDefinition(
|
|
id="products.edit",
|
|
label_key="catalog.permissions.products_edit",
|
|
description_key="catalog.permissions.products_edit_desc",
|
|
category="products",
|
|
),
|
|
PermissionDefinition(
|
|
id="products.delete",
|
|
label_key="catalog.permissions.products_delete",
|
|
description_key="catalog.permissions.products_delete_desc",
|
|
category="products",
|
|
),
|
|
PermissionDefinition(
|
|
id="products.import",
|
|
label_key="catalog.permissions.products_import",
|
|
description_key="catalog.permissions.products_import_desc",
|
|
category="products",
|
|
),
|
|
PermissionDefinition(
|
|
id="products.export",
|
|
label_key="catalog.permissions.products_export",
|
|
description_key="catalog.permissions.products_export_desc",
|
|
category="products",
|
|
),
|
|
],
|
|
# Module-driven menu definitions
|
|
menus={
|
|
FrontendType.VENDOR: [
|
|
MenuSectionDefinition(
|
|
id="products",
|
|
label_key="catalog.menu.products_inventory",
|
|
icon="package",
|
|
order=10,
|
|
items=[
|
|
MenuItemDefinition(
|
|
id="products",
|
|
label_key="catalog.menu.all_products",
|
|
icon="shopping-bag",
|
|
route="/vendor/{vendor_code}/products",
|
|
order=10,
|
|
is_mandatory=True,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
},
|
|
# Metrics provider for dashboard statistics
|
|
metrics_provider=_get_metrics_provider,
|
|
)
|
|
|
|
|
|
def get_catalog_module_with_routers() -> ModuleDefinition:
|
|
"""
|
|
Get catalog module with routers attached.
|
|
|
|
This function attaches the routers lazily to avoid circular imports
|
|
during module initialization.
|
|
"""
|
|
catalog_module.admin_router = _get_admin_router()
|
|
catalog_module.vendor_router = _get_vendor_router()
|
|
return catalog_module
|
|
|
|
|
|
__all__ = ["catalog_module", "get_catalog_module_with_routers"]
|