0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00

Funnel trends all date range (#5420)

* implementation and test

* change to escape params
This commit is contained in:
Eric Duong 2021-08-04 10:33:39 -04:00 committed by GitHub
parent d25fa2c2d5
commit 24a708c259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 4 deletions

View File

@ -6,7 +6,7 @@ from dateutil.relativedelta import relativedelta
from ee.clickhouse.queries.funnels.base import ClickhouseFunnelBase
from ee.clickhouse.queries.funnels.funnel import ClickhouseFunnel
from ee.clickhouse.queries.util import get_time_diff, get_trunc_func_ch
from ee.clickhouse.queries.util import format_ch_timestamp, get_earliest_timestamp, get_time_diff, get_trunc_func_ch
from posthog.constants import BREAKDOWN
from posthog.models.cohort import Cohort
from posthog.models.filters.filter import Filter
@ -92,11 +92,26 @@ class ClickhouseFunnelTrends(ClickhouseFunnelBase):
reached_from_step_count_condition, reached_to_step_count_condition, _ = self.get_steps_reached_conditions()
interval_method = get_trunc_func_ch(self._filter.interval)
if self._filter.date_from is None:
_date_from = get_earliest_timestamp(self._team.pk)
else:
_date_from = self._filter.date_from
num_intervals, seconds_in_interval, _ = get_time_diff(
self._filter.interval or "day", self._filter.date_from, self._filter.date_to, team_id=self._team.pk
self._filter.interval or "day", _date_from, self._filter.date_to, team_id=self._team.pk
)
breakdown_clause = self._get_breakdown_prop()
formatted_date_from = format_ch_timestamp(_date_from, self._filter)
self.params.update(
{
"formatted_date_from": formatted_date_from,
"seconds_in_interval": seconds_in_interval,
"num_intervals": num_intervals,
}
)
query = f"""
SELECT
@ -117,9 +132,9 @@ class ClickhouseFunnelTrends(ClickhouseFunnelBase):
) data
RIGHT OUTER JOIN (
SELECT
{interval_method}(toDateTime('{self._filter.date_from.strftime(TIMESTAMP_FORMAT)}') + number * {seconds_in_interval}) AS entrance_period_start
{interval_method}(toDateTime(%(formatted_date_from)s) + number * %(seconds_in_interval)s) AS entrance_period_start
{', breakdown_value as prop' if breakdown_clause else ''}
FROM numbers({num_intervals}) AS period_offsets
FROM numbers(%(num_intervals)s) AS period_offsets
{'ARRAY JOIN (%(breakdown_values)s) AS breakdown_value' if breakdown_clause else ''}
) fill
USING (entrance_period_start {breakdown_clause})

View File

@ -2,6 +2,7 @@ from datetime import datetime, timedelta
from uuid import uuid4
import pytz
from freezegun.api import freeze_time
from ee.clickhouse.models.event import create_event
from ee.clickhouse.queries.funnels import ClickhouseFunnel, ClickhouseFunnelStrict, ClickhouseFunnelUnordered
@ -327,6 +328,38 @@ class TestFunnelTrends(ClickhouseTestMixin, APIBaseTest):
[person["distinct_ids"] for person in persons], [["user_one"]],
)
def test_all_date_range(self):
filter = Filter(
data={
"insight": INSIGHT_FUNNELS,
"display": TRENDS_LINEAR,
"interval": "day",
"date_from": "all",
"funnel_window_days": 7,
"events": [
{"id": "step one", "order": 0},
{"id": "step two", "order": 1},
{"id": "step three", "order": 2},
],
}
)
_create_person(distinct_ids=["user_one"], team=self.team)
# full run
_create_event(event="step one", distinct_id="user_one", team=self.team, timestamp="2021-05-01 00:00:00")
_create_event(event="step two", distinct_id="user_one", team=self.team, timestamp="2021-05-01 01:00:00")
_create_event(event="step three", distinct_id="user_one", team=self.team, timestamp="2021-05-01 02:00:00")
with freeze_time("2021-05-20T13:01:01Z"):
results = ClickhouseFunnelTrends(filter, self.team, ClickhouseFunnel)._exec_query()
self.assertEqual(20, len(results))
persons, _ = self._get_people_at_step(filter, "2021-05-01 00:00:00", False)
self.assertEqual(
[person["distinct_ids"] for person in persons], [["user_one"]],
)
def test_all_results_for_day_interval(self):
self._create_sample_data()