права фикс

This commit is contained in:
Artem Tsyrulnikov
2026-01-27 19:29:10 +03:00
parent b5d839ddae
commit 2d35588a85
21 changed files with 309 additions and 115 deletions

View File

@@ -101,6 +101,7 @@ class PeriodMetrics:
def _calculate_metrics(
period_metrics: PeriodMetrics,
requested_metrics: list[dto.ProjectMetrics] | None,
hide_subscriptions: bool = False,
) -> dto.ProjectMetricsData:
all_metrics = requested_metrics is None or len(requested_metrics) == 0
@@ -116,13 +117,13 @@ def _calculate_metrics(
metrics.purchases_count = period_metrics.purchases_count
if should_include(dto.ProjectMetrics.TOTAL_SUBSCRIPTIONS):
metrics.total_subscriptions = period_metrics.total_subscriptions
metrics.total_subscriptions = 0 if hide_subscriptions else period_metrics.total_subscriptions
if should_include(dto.ProjectMetrics.TOTAL_VIEWS):
metrics.total_views = period_metrics.total_views
if should_include(dto.ProjectMetrics.CLICKS_COUNT):
metrics.clicks_count = period_metrics.clicks_count
metrics.clicks_count = 0 if hide_subscriptions else period_metrics.clicks_count
if should_include(dto.ProjectMetrics.REACH_VOLUME):
metrics.reach_volume = period_metrics.reach_volume
@@ -132,11 +133,14 @@ def _calculate_metrics(
# Средние значения
if should_include(dto.ProjectMetrics.AVG_CPF):
metrics.avg_cpf = (
period_metrics.total_cost / period_metrics.total_subscriptions
if period_metrics.total_subscriptions > 0 and period_metrics.total_cost > 0
else None
)
if hide_subscriptions:
metrics.avg_cpf = None
else:
metrics.avg_cpf = (
period_metrics.total_cost / period_metrics.total_subscriptions
if period_metrics.total_subscriptions > 0 and period_metrics.total_cost > 0
else None
)
if should_include(dto.ProjectMetrics.AVG_CPM):
metrics.avg_cpm = (
@@ -160,11 +164,14 @@ def _calculate_metrics(
if should_include(dto.ProjectMetrics.AVG_CONVERSION):
# Конверсия = подписки / просмотры * 100
metrics.avg_conversion = (
(period_metrics.total_subscriptions / period_metrics.total_views) * 100
if period_metrics.total_views > 0 and period_metrics.total_subscriptions > 0
else 0.0
)
if hide_subscriptions:
metrics.avg_conversion = None
else:
metrics.avg_conversion = (
(period_metrics.total_subscriptions / period_metrics.total_views) * 100
if period_metrics.total_views > 0 and period_metrics.total_subscriptions > 0
else 0.0
)
return metrics
@@ -175,11 +182,10 @@ async def get_projects_analytics(
if input.date_from and input.date_to and input.date_from > input.date_to:
raise HTTPException(status.HTTP_400_BAD_REQUEST, 'date_from must be before date_to')
context = await self.ensure_workspace_permission(
input.workspace_id, input.user_id, domain.PermissionKey.ANALYTICS_READ
)
context = await self.ensure_analytics_permission(input.workspace_id, input.user_id)
allowed_project_ids = context.allowed_project_ids(domain.PermissionKey.ANALYTICS_READ)
hide_subscriptions = context.should_hide_subscriptions()
# Фильтрация по project_ids если указаны
if input.project_ids:
@@ -293,7 +299,7 @@ async def get_projects_analytics(
period_dt = datetime.datetime.now()
period_label = _format_period_label(period_dt, input.grouping)
metrics = _calculate_metrics(pd, input.metrics)
metrics = _calculate_metrics(pd, input.metrics, hide_subscriptions)
periods.append(
dto.ProjectAnalyticsPeriod(
@@ -303,6 +309,6 @@ async def get_projects_analytics(
)
)
totals = _calculate_metrics(total_metrics, input.metrics)
totals = _calculate_metrics(total_metrics, input.metrics, hide_subscriptions)
return dto.GetProjectsAnalyticsOutput(periods=periods, totals=totals)