feat: убираем денормализацию (антипреждевременная оптимизация)
This commit is contained in:
@@ -497,3 +497,45 @@ class Postgres(DatabaseBase):
|
||||
|
||||
async def clear_telegram_state(self, telegram_id: int) -> None:
|
||||
await domain.TelegramState.filter(telegram_id=telegram_id).delete()
|
||||
|
||||
# Count methods
|
||||
async def count_placements_by_creative(self, creative_id: uuid.UUID) -> int:
|
||||
return await domain.Placement.filter(creative_id=creative_id).count()
|
||||
|
||||
async def count_subscriptions_by_placement(self, placement_id: uuid.UUID) -> int:
|
||||
return await domain.Subscription.filter(placement_id=placement_id).count()
|
||||
|
||||
async def count_placements_by_creative_batch(self, creative_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
if not creative_ids:
|
||||
return {}
|
||||
|
||||
from tortoise.functions import Count
|
||||
|
||||
results = (
|
||||
await domain.Placement.filter(creative_id__in=creative_ids)
|
||||
.group_by('creative_id')
|
||||
.annotate(count=Count('id'))
|
||||
.values('creative_id', 'count')
|
||||
)
|
||||
|
||||
counts = {row['creative_id']: row['count'] for row in results}
|
||||
return {cid: counts.get(cid, 0) for cid in creative_ids}
|
||||
|
||||
async def count_subscriptions_by_placement_batch(self, placement_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
if not placement_ids:
|
||||
return {}
|
||||
|
||||
from tortoise.functions import Count
|
||||
|
||||
results = (
|
||||
await domain.Subscription.filter(placement_id__in=placement_ids)
|
||||
.group_by('placement_id')
|
||||
.annotate(count=Count('id'))
|
||||
.values('placement_id', 'count')
|
||||
)
|
||||
|
||||
counts = {row['placement_id']: row['count'] for row in results}
|
||||
return {pid: counts.get(pid, 0) for pid in placement_ids}
|
||||
|
||||
async def has_placements_for_creative(self, creative_id: uuid.UUID) -> bool:
|
||||
return await domain.Placement.filter(creative_id=creative_id).exists()
|
||||
|
||||
Reference in New Issue
Block a user