Skip to content

Commit

Permalink
chore(stickiness): Added a basic set of stickiness tests (#19908)
Browse files Browse the repository at this point in the history
Added a basic set of stickiness tests
  • Loading branch information
Gilbert09 authored Jan 23, 2024
1 parent 253c8b5 commit 135c205
Show file tree
Hide file tree
Showing 2 changed files with 514 additions and 4 deletions.
18 changes: 14 additions & 4 deletions posthog/hogql_queries/insights/stickiness_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ def _refresh_frequency(self):
return refresh_frequency

def _events_query(self, series_with_extra: SeriesWithExtras) -> ast.SelectQuery:
num_intervals_column_expr = ast.Alias(
alias="num_intervals",
expr=ast.Call(
distinct=True,
name="count",
args=[self.query_date_range.date_to_start_of_interval_hogql(ast.Field(chain=["e", "timestamp"]))],
),
)

select_query = parse_select(
"""
SELECT count(DISTINCT aggregation_target) as aggregation_target, num_intervals
FROM (
SELECT e.person_id as aggregation_target, count(DISTINCT toStartOfDay(e.timestamp)) as num_intervals
SELECT e.person_id as aggregation_target, {num_intervals_column_expr}
FROM events e
SAMPLE {sample}
WHERE {where_clause}
Expand All @@ -100,6 +109,7 @@ def _events_query(self, series_with_extra: SeriesWithExtras) -> ast.SelectQuery:
"where_clause": self.where_clause(series_with_extra),
"num_intervals": ast.Constant(value=self.intervals_num()),
"sample": self._sample_value(),
"num_intervals_column_expr": num_intervals_column_expr,
},
)

Expand All @@ -111,7 +121,7 @@ def to_query(self) -> List[ast.SelectQuery]: # type: ignore
for series in self.series:
date_range = self.date_range(series)

interval_subtract = ast.Call(
interval_addition = ast.Call(
name=f"toInterval{date_range.interval_name.capitalize()}",
args=[ast.Constant(value=2)],
)
Expand All @@ -123,7 +133,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} - {interval_subtract}, {date_to}))
FROM numbers(dateDiff({interval}, {date_from}, {date_to} + {interval_addition}))
UNION ALL
{events_query}
)
Expand All @@ -133,7 +143,7 @@ def to_query(self) -> List[ast.SelectQuery]: # type: ignore
""",
placeholders={
**date_range.to_placeholders(),
"interval_subtract": interval_subtract,
"interval_addition": interval_addition,
"events_query": self._events_query(series),
},
)
Expand Down
Loading

0 comments on commit 135c205

Please sign in to comment.