время в топе и тэги креативов

This commit is contained in:
Artem Tsyrulnikov
2026-01-28 12:34:00 +03:00
parent 0b25566ffb
commit 484e52c077
26 changed files with 326 additions and 36 deletions

View File

@@ -2,6 +2,8 @@ import logging
import uuid
from typing import TYPE_CHECKING
from tortoise import timezone
from src import domain, dto
from .create_placements import _build_placement_output
@@ -39,15 +41,40 @@ async def get_placements(self: 'Usecase', input: dto.GetPlacementsInput) -> dto.
post_ids = [placement_post.post.id for placement_post in placement_posts if placement_post.post]
views_map = await self.database.get_latest_views_data_batch(post_ids) if post_ids else {}
# Collect (channel_id, message_id) pairs for batch next post lookup
channel_message_pairs = [
(placement_post.post.channel_id, placement_post.post.message_id)
for placement_post in placement_posts
if placement_post.post
]
next_posts_map = await self.database.get_next_posts_after_batch(channel_message_pairs)
# Calculate time_on_top for each placement_post
time_on_top_map: dict[uuid.UUID, int] = {}
now = timezone.now()
for placement_post in placement_posts:
post = placement_post.post
if not post or not post.published_at:
continue
published_at = post.published_at
key = (post.channel_id, post.message_id)
next_post = next_posts_map.get(key)
if next_post and next_post.published_at:
time_on_top_map[placement_post.id] = int((next_post.published_at - published_at).total_seconds())
else:
time_on_top_map[placement_post.id] = int((now - published_at).total_seconds())
placement_posts_by_placement_id: dict[uuid.UUID, list[dto.PlacementPostOutput]] = {}
for placement_post in placement_posts:
views_count = None
time_on_top = time_on_top_map.get(placement_post.id)
if placement_post.post:
views_count = views_map.get(placement_post.post.id, (None,))[0]
placement_post_output = _build_placement_post_output(
placement_post,
subscriptions_counts.get(placement_post.placement_id, 0),
views_count,
time_on_top,
)
if placement_post_output is None:
continue