feat: аналитика
This commit is contained in:
@@ -5,17 +5,21 @@ from uuid import UUID
|
||||
|
||||
from src import domain
|
||||
|
||||
from .analytics.get_creatives_analytics import get_creatives_analytics
|
||||
from .analytics.get_external_channels_analytics import get_external_channels_analytics
|
||||
from .analytics.get_placements_analytics import get_placements_analytics
|
||||
from .analytics.get_spending_analytics import get_spending_analytics
|
||||
from .creative.create_creative import create_creative
|
||||
from .creative.delete_creative import delete_creative
|
||||
from .creative.get_creative import get_creative
|
||||
from .creative.get_creatives import get_creatives
|
||||
from .creative.update_creative import update_creative
|
||||
from .get_me import get_me
|
||||
from .external_channel.create_external_channel import create_external_channel
|
||||
from .external_channel.delete_external_channel import delete_external_channel
|
||||
from .external_channel.get_external_channels import get_external_channels
|
||||
from .external_channel.update_external_channel import update_external_channel
|
||||
from .external_channel.update_external_channel_links import update_external_channel_links
|
||||
from .get_me import get_me
|
||||
from .placement.create_placement import create_placement
|
||||
from .placement.delete_placement import delete_placement
|
||||
from .placement.get_placement import get_placement
|
||||
@@ -70,6 +74,8 @@ class Database(typing.Protocol):
|
||||
|
||||
async def get_external_channels_for_target(self, target_channel_id: UUID) -> list[domain.ExternalChannel]: ...
|
||||
|
||||
async def get_user_external_channels(self, user_id: UUID) -> list[domain.ExternalChannel]: ...
|
||||
|
||||
async def add_external_channel_to_targets(
|
||||
self, external_channel_id: UUID, target_channel_ids: list[UUID]
|
||||
) -> None: ...
|
||||
@@ -87,11 +93,7 @@ class Database(typing.Protocol):
|
||||
async def update_creative(self, creative: domain.Creative) -> domain.Creative: ...
|
||||
|
||||
async def get_user_creatives(
|
||||
self,
|
||||
user_id: UUID,
|
||||
*,
|
||||
target_channel_id: UUID | None = None,
|
||||
include_archived: bool = False,
|
||||
self, user_id: UUID, target_channel_id: UUID | None = None, include_archived: bool = False
|
||||
) -> list[domain.Creative]: ...
|
||||
|
||||
async def delete_creative(self, creative_id: UUID) -> None: ...
|
||||
@@ -105,7 +107,6 @@ class Database(typing.Protocol):
|
||||
async def get_user_placements(
|
||||
self,
|
||||
user_id: UUID,
|
||||
*,
|
||||
target_channel_id: UUID | None = None,
|
||||
external_channel_id: UUID | None = None,
|
||||
creative_id: UUID | None = None,
|
||||
@@ -198,3 +199,7 @@ class Usecase:
|
||||
run_views_worker_cycle = run_views_worker_cycle
|
||||
get_views_history = get_views_history
|
||||
update_views_manually = update_views_manually
|
||||
get_placements_analytics = get_placements_analytics
|
||||
get_creatives_analytics = get_creatives_analytics
|
||||
get_external_channels_analytics = get_external_channels_analytics
|
||||
get_spending_analytics = get_spending_analytics
|
||||
|
||||
11
src/usecase/analytics/__init__.py
Normal file
11
src/usecase/analytics/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from .get_creatives_analytics import get_creatives_analytics
|
||||
from .get_external_channels_analytics import get_external_channels_analytics
|
||||
from .get_placements_analytics import get_placements_analytics
|
||||
from .get_spending_analytics import get_spending_analytics
|
||||
|
||||
__all__ = (
|
||||
'get_placements_analytics',
|
||||
'get_creatives_analytics',
|
||||
'get_external_channels_analytics',
|
||||
'get_spending_analytics',
|
||||
)
|
||||
78
src/usecase/analytics/get_creatives_analytics.py
Normal file
78
src/usecase/analytics/get_creatives_analytics.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
from uuid import UUID
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_creatives_analytics(
|
||||
self: 'Usecase', input: dto.GetCreativesAnalyticsInput
|
||||
) -> dto.GetCreativesAnalyticsOutput:
|
||||
async with self.database.transaction():
|
||||
if input.target_channel_id:
|
||||
target_channel = await self.database.get_target_channel(input.user_id, input.target_channel_id)
|
||||
if not target_channel:
|
||||
raise domain.TargetChannelNotFound(input.target_channel_id)
|
||||
|
||||
creatives = await self.database.get_user_creatives(
|
||||
input.user_id, input.target_channel_id, include_archived=False
|
||||
)
|
||||
|
||||
placements = await self.database.get_user_placements(
|
||||
input.user_id, input.target_channel_id, include_archived=False
|
||||
)
|
||||
|
||||
@dataclass
|
||||
class CreativeStats:
|
||||
total_cost: float = 0.0
|
||||
total_subscriptions: int = 0
|
||||
total_views: int = 0
|
||||
placements_count: int = 0
|
||||
|
||||
creative_stats: dict[UUID, CreativeStats] = {cr.id: CreativeStats() for cr in creatives}
|
||||
|
||||
for p in placements:
|
||||
stats = creative_stats.get(p.creative_id)
|
||||
if stats is None:
|
||||
continue
|
||||
|
||||
stats.total_cost += p.cost if p.cost is not None else 0
|
||||
stats.total_subscriptions += p.subscriptions_count
|
||||
stats.total_views += p.views_count if p.views_count is not None else 0
|
||||
stats.placements_count += 1
|
||||
|
||||
result = []
|
||||
for cr in creatives:
|
||||
stats = creative_stats[cr.id]
|
||||
|
||||
avg_cpf = (
|
||||
(stats.total_cost / stats.total_subscriptions)
|
||||
if stats.total_subscriptions > 0 and stats.total_cost > 0
|
||||
else None
|
||||
)
|
||||
avg_cpm = (
|
||||
(stats.total_cost / stats.total_views * 1000)
|
||||
if stats.total_views > 0 and stats.total_cost > 0
|
||||
else None
|
||||
)
|
||||
|
||||
result.append(
|
||||
dto.CreativeAnalyticsOutput(
|
||||
id=cr.id,
|
||||
name=cr.name,
|
||||
placements_count=stats.placements_count,
|
||||
total_cost=stats.total_cost,
|
||||
total_subscriptions=stats.total_subscriptions,
|
||||
total_views=stats.total_views,
|
||||
avg_cpf=avg_cpf,
|
||||
avg_cpm=avg_cpm,
|
||||
)
|
||||
)
|
||||
|
||||
return dto.GetCreativesAnalyticsOutput(creatives=result)
|
||||
78
src/usecase/analytics/get_external_channels_analytics.py
Normal file
78
src/usecase/analytics/get_external_channels_analytics.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
from uuid import UUID
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def get_external_channels_analytics(
|
||||
self: 'Usecase', input: dto.GetExternalChannelsAnalyticsInput
|
||||
) -> dto.GetExternalChannelsAnalyticsOutput:
|
||||
async with self.database.transaction():
|
||||
if input.target_channel_id:
|
||||
target_channel = await self.database.get_target_channel(input.user_id, input.target_channel_id)
|
||||
if not target_channel:
|
||||
raise domain.TargetChannelNotFound(input.target_channel_id)
|
||||
external_channels = await self.database.get_external_channels_for_target(input.target_channel_id)
|
||||
else:
|
||||
external_channels = await self.database.get_user_external_channels(input.user_id)
|
||||
|
||||
placements = await self.database.get_user_placements(
|
||||
input.user_id, input.target_channel_id, include_archived=False
|
||||
)
|
||||
|
||||
@dataclass
|
||||
class ChannelStats:
|
||||
total_cost: float = 0.0
|
||||
total_subscriptions: int = 0
|
||||
total_views: int = 0
|
||||
placements_count: int = 0
|
||||
|
||||
channel_stats: dict[UUID, ChannelStats] = {ch.id: ChannelStats() for ch in external_channels}
|
||||
|
||||
for p in placements:
|
||||
stats = channel_stats.get(p.external_channel_id)
|
||||
if stats is None:
|
||||
continue
|
||||
|
||||
stats.total_cost += p.cost if p.cost is not None else 0
|
||||
stats.total_subscriptions += p.subscriptions_count
|
||||
stats.total_views += p.views_count if p.views_count is not None else 0
|
||||
stats.placements_count += 1
|
||||
|
||||
result = []
|
||||
for ch in external_channels:
|
||||
stats = channel_stats[ch.id]
|
||||
|
||||
avg_cpf = (
|
||||
(stats.total_cost / stats.total_subscriptions)
|
||||
if stats.total_subscriptions > 0 and stats.total_cost > 0
|
||||
else None
|
||||
)
|
||||
avg_cpm = (
|
||||
(stats.total_cost / stats.total_views * 1000)
|
||||
if stats.total_views > 0 and stats.total_cost > 0
|
||||
else None
|
||||
)
|
||||
|
||||
result.append(
|
||||
dto.ExternalChannelAnalyticsOutput(
|
||||
id=ch.id,
|
||||
title=ch.title,
|
||||
username=ch.username,
|
||||
placements_count=stats.placements_count,
|
||||
total_cost=stats.total_cost,
|
||||
total_subscriptions=stats.total_subscriptions,
|
||||
total_views=stats.total_views,
|
||||
avg_cpf=avg_cpf,
|
||||
avg_cpm=avg_cpm,
|
||||
)
|
||||
)
|
||||
|
||||
return dto.GetExternalChannelsAnalyticsOutput(external_channels=result)
|
||||
56
src/usecase/analytics/get_placements_analytics.py
Normal file
56
src/usecase/analytics/get_placements_analytics.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _calculate_cpf(cost: float | None, subscriptions: int) -> float | None:
|
||||
if cost is None or subscriptions == 0:
|
||||
return None
|
||||
return cost / subscriptions
|
||||
|
||||
|
||||
def _calculate_cpm(cost: float | None, views: int | None) -> float | None:
|
||||
if cost is None or views is None or views == 0:
|
||||
return None
|
||||
return (cost / views) * 1000
|
||||
|
||||
|
||||
async def get_placements_analytics(
|
||||
self: 'Usecase', input: dto.GetPlacementsAnalyticsInput
|
||||
) -> dto.GetPlacementsAnalyticsOutput:
|
||||
async with self.database.transaction():
|
||||
if input.target_channel_id:
|
||||
target_channel = await self.database.get_target_channel(input.user_id, input.target_channel_id)
|
||||
if not target_channel:
|
||||
raise domain.TargetChannelNotFound(input.target_channel_id)
|
||||
|
||||
placements = await self.database.get_user_placements(
|
||||
input.user_id, input.target_channel_id, include_archived=False
|
||||
)
|
||||
|
||||
result = [
|
||||
dto.PlacementAnalyticsOutput(
|
||||
id=p.id,
|
||||
target_channel_id=p.target_channel_id,
|
||||
target_channel_title=p.target_channel.title,
|
||||
external_channel_id=p.external_channel_id,
|
||||
external_channel_title=p.external_channel.title,
|
||||
creative_id=p.creative_id,
|
||||
creative_name=p.creative.name,
|
||||
placement_date=p.placement_date,
|
||||
cost=p.cost,
|
||||
subscriptions_count=p.subscriptions_count,
|
||||
views_count=p.views_count,
|
||||
cpf=_calculate_cpf(p.cost, p.subscriptions_count),
|
||||
cpm=_calculate_cpm(p.cost, p.views_count),
|
||||
)
|
||||
for p in placements
|
||||
]
|
||||
|
||||
return dto.GetPlacementsAnalyticsOutput(placements=result)
|
||||
114
src/usecase/analytics/get_spending_analytics.py
Normal file
114
src/usecase/analytics/get_spending_analytics.py
Normal file
@@ -0,0 +1,114 @@
|
||||
import datetime
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _format_period(dt: 'datetime.datetime', grouping: dto.DateGrouping) -> str:
|
||||
match grouping:
|
||||
case dto.DateGrouping.DAY:
|
||||
return str(dt.strftime('%Y-%m-%d'))
|
||||
case dto.DateGrouping.WEEK:
|
||||
# ISO week
|
||||
return str(dt.strftime('%Y-W%W'))
|
||||
case dto.DateGrouping.MONTH:
|
||||
return str(dt.strftime('%Y-%m'))
|
||||
case dto.DateGrouping.QUARTER:
|
||||
quarter = (dt.month - 1) // 3 + 1
|
||||
return f'{dt.year}-Q{quarter}'
|
||||
case dto.DateGrouping.YEAR:
|
||||
return str(dt.year)
|
||||
|
||||
raise ValueError('Invalid date grouping')
|
||||
|
||||
|
||||
async def get_spending_analytics(
|
||||
self: 'Usecase', input: dto.GetSpendingAnalyticsInput
|
||||
) -> dto.GetSpendingAnalyticsOutput:
|
||||
async with self.database.transaction():
|
||||
if input.target_channel_id:
|
||||
target_channel = await self.database.get_target_channel(input.user_id, input.target_channel_id)
|
||||
if not target_channel:
|
||||
raise domain.TargetChannelNotFound(input.target_channel_id)
|
||||
|
||||
placements = await self.database.get_user_placements(
|
||||
input.user_id, input.target_channel_id, include_archived=False
|
||||
)
|
||||
|
||||
filtered = [
|
||||
p
|
||||
for p in placements
|
||||
if (not input.date_from or p.placement_date >= input.date_from)
|
||||
and (not input.date_to or p.placement_date <= input.date_to)
|
||||
]
|
||||
|
||||
total_cost = 0.0
|
||||
total_subs = 0
|
||||
total_views = 0
|
||||
|
||||
@dataclass
|
||||
class PeriodData:
|
||||
cost: float = 0.0
|
||||
subscriptions: int = 0
|
||||
views: int = 0
|
||||
|
||||
# Группировка по периодам
|
||||
period_data: dict[str, PeriodData] = defaultdict(PeriodData)
|
||||
|
||||
for p in filtered:
|
||||
period = _format_period(p.placement_date, input.grouping)
|
||||
pd = period_data[period]
|
||||
|
||||
if p.cost is not None:
|
||||
total_cost += p.cost
|
||||
pd.cost += p.cost
|
||||
|
||||
total_subs += p.subscriptions_count
|
||||
pd.subscriptions += p.subscriptions_count
|
||||
|
||||
if p.views_count is not None:
|
||||
total_views += p.views_count
|
||||
pd.views += p.views_count
|
||||
|
||||
avg_cpf = total_cost / total_subs if total_subs > 0 and total_cost > 0 else None
|
||||
avg_cpm = (total_cost / total_views * 1000) if total_views > 0 and total_cost > 0 else None
|
||||
|
||||
# Данные для графика
|
||||
chart_data = []
|
||||
for period in sorted(period_data.keys()):
|
||||
d = period_data[period]
|
||||
|
||||
cost = d.cost
|
||||
subs = d.subscriptions
|
||||
views = d.views
|
||||
|
||||
cpf = cost / subs if subs > 0 and cost > 0 else None
|
||||
cpm = (cost / views * 1000) if views > 0 and cost > 0 else None
|
||||
|
||||
chart_data.append(
|
||||
dto.SpendingDataPoint(
|
||||
period=period,
|
||||
cost=cost,
|
||||
subscriptions=subs,
|
||||
views=views,
|
||||
cpf=cpf,
|
||||
cpm=cpm,
|
||||
)
|
||||
)
|
||||
|
||||
return dto.GetSpendingAnalyticsOutput(
|
||||
total_cost=total_cost,
|
||||
total_subscriptions=total_subs,
|
||||
total_views=total_views,
|
||||
avg_cpf=avg_cpf,
|
||||
avg_cpm=avg_cpm,
|
||||
chart_data=chart_data,
|
||||
)
|
||||
@@ -25,7 +25,7 @@ async def telegram_login(self: 'Usecase', input: dto.TelegramLoginInput) -> None
|
||||
|
||||
await self.database.create_login_token(user.id, token, expires_at)
|
||||
|
||||
login_url = f'{settings.app.URL}/auth/complete?token={token}'
|
||||
login_url = f'{settings.app.URL}/api/v1/auth/complete?token={token}'
|
||||
|
||||
username_display = f'@{user.username}' if user.username else 'пользователь'
|
||||
await self.telegram_bot.send_message_with_button(
|
||||
|
||||
Reference in New Issue
Block a user