mirror of
https://github.com/PostHog/posthog.git
synced 2024-12-01 12:21:02 +01:00
871024f8c6
* JOIN in funnel trends query in a more optimal way * Reformat funnel trends assert slightly * Refactor `ClickhouseFunnelTrends` slightly * Add `ClickhouseFunnelTrendsPersons` code * Allow parametrization of funnel trends persons query * Update test_funnel_trends.py * Clean some funnel analysis code up * Fix `drop_off` default in `FunnelTrendsPersonsMixin` * Refactor for `ClickhouseFunnelTrends.get_step_counts_without_aggregation_query` * Update test_funnel_trends.py * Add an API test for funnel trends persons * Use `FUNNEL_PERSONS_BY_STEP_SQL` * Tests persons some more * Remove unused imports
45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
from rest_framework.exceptions import ValidationError
|
|
|
|
from ee.clickhouse.queries.funnels.funnel_trends import TIMESTAMP_FORMAT, ClickhouseFunnelTrends
|
|
from ee.clickhouse.queries.util import get_trunc_func_ch
|
|
from ee.clickhouse.sql.funnels.funnel import FUNNEL_PERSONS_BY_STEP_SQL
|
|
from posthog.constants import DROP_OFF, ENTRANCE_PERIOD_START
|
|
from posthog.models.person import Person
|
|
|
|
|
|
class ClickhouseFunnelTrendsPersons(ClickhouseFunnelTrends):
|
|
def get_query(self) -> str:
|
|
drop_off = self._filter.drop_off
|
|
if drop_off is None:
|
|
raise ValidationError(f"Filter parameter {DROP_OFF} must be provided and a bool for funnel trends persons!")
|
|
|
|
entrance_period_start = self._filter.entrance_period_start
|
|
if not entrance_period_start:
|
|
raise ValidationError(
|
|
f"Filter parameter {ENTRANCE_PERIOD_START} must be provided and a datetime for funnel trends persons!"
|
|
)
|
|
|
|
step_counts_query = self.get_step_counts_without_aggregation_query(
|
|
specific_entrance_period_start=entrance_period_start
|
|
)
|
|
# Expects multiple rows for same person, first event time, steps taken.
|
|
self.params.update(self.funnel_order.params)
|
|
|
|
_, reached_to_step_count_condition, did_not_reach_to_step_count_condition = self.get_steps_reached_conditions()
|
|
|
|
return FUNNEL_PERSONS_BY_STEP_SQL.format(
|
|
offset=self._filter.offset,
|
|
steps_per_person_query=step_counts_query,
|
|
persons_steps=did_not_reach_to_step_count_condition if drop_off else reached_to_step_count_condition,
|
|
)
|
|
|
|
def _summarize_data(self, results):
|
|
return results
|
|
|
|
def _format_results(self, results):
|
|
people = Person.objects.filter(team_id=self._team.pk, uuid__in=[val[0] for val in results])
|
|
|
|
from posthog.api.person import PersonSerializer
|
|
|
|
return PersonSerializer(people, many=True).data
|