perf(campaign): batch tenant summaries
This commit is contained in:
@@ -161,6 +161,26 @@ def _tenant_summary(session, tenant_id: str) -> dict[str, int]:
|
||||
return {"campaigns": session.query(Campaign).filter(Campaign.tenant_id == tenant_id).count()}
|
||||
|
||||
|
||||
def _tenant_summary_batch(session, tenant_ids) -> dict[str, dict[str, int]]:
|
||||
from sqlalchemy import func
|
||||
|
||||
from govoplan_campaign.backend.db.models import Campaign
|
||||
|
||||
ids = tuple(dict.fromkeys(str(tenant_id) for tenant_id in tenant_ids if tenant_id))
|
||||
if not ids:
|
||||
return {}
|
||||
rows = (
|
||||
session.query(Campaign.tenant_id, func.count(Campaign.id))
|
||||
.filter(Campaign.tenant_id.in_(ids))
|
||||
.group_by(Campaign.tenant_id)
|
||||
.all()
|
||||
)
|
||||
return {
|
||||
tenant_id: {"campaigns": int(count)}
|
||||
for tenant_id, count in rows
|
||||
}
|
||||
|
||||
|
||||
def _campaigns_router(context: ModuleContext):
|
||||
from govoplan_campaign.backend.runtime import configure_runtime
|
||||
|
||||
@@ -236,6 +256,7 @@ manifest = ModuleManifest(
|
||||
route_factory=_campaigns_router,
|
||||
role_templates=ROLE_TEMPLATES,
|
||||
tenant_summary_providers=(_tenant_summary,),
|
||||
tenant_summary_batch_providers=(_tenant_summary_batch,),
|
||||
nav_items=(
|
||||
NavItem(path="/campaigns", label="Campaigns", icon="campaign", required_any=CAMPAIGN_MODULE_REQUIRED_ANY, order=20),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user