From 82064e95b696f61377c09a4b7fd6cedd312dff84 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Thu, 8 Feb 2024 11:07:58 +0100 Subject: [PATCH] fix(insights): hogql stickiness interval ranges (#20190) --- .../insights/stickiness_query_runner.py | 4 ++-- .../insights/test/test_stickiness_query_runner.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/posthog/hogql_queries/insights/stickiness_query_runner.py b/posthog/hogql_queries/insights/stickiness_query_runner.py index 34478fac1dd32..af4d64035719b 100644 --- a/posthog/hogql_queries/insights/stickiness_query_runner.py +++ b/posthog/hogql_queries/insights/stickiness_query_runner.py @@ -136,7 +136,7 @@ def to_query(self) -> List[ast.SelectQuery]: # type: ignore interval_addition = ast.Call( name=f"toInterval{date_range.interval_name.capitalize()}", - args=[ast.Constant(value=0 if date_range.interval_name == "week" else 1)], + args=[ast.Constant(value=1)], ) select_query = parse_select( @@ -146,7 +146,7 @@ def to_query(self) -> List[ast.SelectQuery]: # type: ignore SELECT sum(aggregation_target) as aggregation_target, num_intervals FROM ( SELECT 0 as aggregation_target, (number + 1) as num_intervals - FROM numbers(dateDiff({interval}, {date_from}, {date_to} + {interval_addition})) + FROM numbers(dateDiff({interval}, {date_from_start_of_interval}, {date_to_start_of_interval} + {interval_addition})) UNION ALL {events_query} ) diff --git a/posthog/hogql_queries/insights/test/test_stickiness_query_runner.py b/posthog/hogql_queries/insights/test/test_stickiness_query_runner.py index 4e101d4c4e78e..3de1fb6ce865e 100644 --- a/posthog/hogql_queries/insights/test/test_stickiness_query_runner.py +++ b/posthog/hogql_queries/insights/test/test_stickiness_query_runner.py @@ -348,6 +348,19 @@ def test_interval_week(self): assert result["days"] == [1, 2, 3] assert result["data"] == [0, 0, 2] + def test_interval_full_weeks(self): + self._create_test_events() + + with freeze_time("2020-01-23T12:00:00Z"): + response = self._run_query(interval=IntervalType.week, date_from="-30d", date_to="now") + + result = response.results[0] + + assert result["label"] == "$pageview" + assert result["labels"] == ["1 week", "2 weeks", "3 weeks", "4 weeks", "5 weeks"] + assert result["days"] == [1, 2, 3, 4, 5] + assert result["data"] == [0, 0, 2, 0, 0] + def test_interval_month(self): self._create_test_events()