Skip to content

Commit

Permalink
feat(insights): start of week for lifecycle queries (#19135)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Dec 6, 2023
1 parent d3cb954 commit 19b418d
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 72 deletions.
64 changes: 30 additions & 34 deletions posthog/hogql_queries/insights/lifecycle_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def to_query(self) -> ast.SelectQuery | ast.SelectUnionQuery:
FROM {events_query}
GROUP BY start_of_period, status
)
WHERE start_of_period <= dateTrunc({interval}, {date_to})
AND start_of_period >= dateTrunc({interval}, {date_from})
WHERE start_of_period <= {date_to_start_of_interval}
AND start_of_period >= {date_from_start_of_interval}
GROUP BY start_of_period, status
ORDER BY start_of_period ASC
)
Expand All @@ -100,12 +100,8 @@ def to_persons_query(
ast.CompareOperation(
op=ast.CompareOperationOp.Eq,
left=ast.Field(chain=["start_of_period"]),
right=parse_expr(
"dateTrunc({interval}, toDateTime({day}))",
placeholders={
"interval": self.query_date_range.interval_period_string_as_hogql_constant(),
"day": ast.Constant(value=day),
},
right=self.query_date_range.date_to_start_of_interval_hogql(
ast.Call(name="toDateTime", args=[ast.Constant(value=day)])
),
)
)
Expand Down Expand Up @@ -210,23 +206,15 @@ def event_filter(self) -> ast.Expr:
with self.timings.measure("date_range"):
event_filters.append(
parse_expr(
"timestamp >= dateTrunc({interval}, {date_from}) - {one_interval}",
{
"interval": self.query_date_range.interval_period_string_as_hogql_constant(),
"one_interval": self.query_date_range.one_interval_period(),
"date_from": self.query_date_range.date_from_as_hogql(),
},
"timestamp >= {date_from_start_of_interval} - {one_interval_period}",
self.query_date_range.to_placeholders(),
timings=self.timings,
)
)
event_filters.append(
parse_expr(
"timestamp < dateTrunc({interval}, {date_to}) + {one_interval}",
{
"interval": self.query_date_range.interval_period_string_as_hogql_constant(),
"one_interval": self.query_date_range.one_interval_period(),
"date_to": self.query_date_range.date_to_as_hogql(),
},
"timestamp < {date_to_start_of_interval} + {one_interval_period}",
self.query_date_range.to_placeholders(),
timings=self.timings,
)
)
Expand Down Expand Up @@ -275,9 +263,9 @@ def events_query(self):
SELECT
events.person.id as person_id,
min(events.person.created_at) AS created_at,
arraySort(groupUniqArray(dateTrunc({interval}, events.timestamp))) AS all_activity,
arrayPopBack(arrayPushFront(all_activity, dateTrunc({interval}, created_at))) as previous_activity,
arrayPopFront(arrayPushBack(all_activity, dateTrunc({interval}, toDateTime('1970-01-01 00:00:00')))) as following_activity,
arraySort(groupUniqArray({trunc_timestamp})) AS all_activity,
arrayPopBack(arrayPushFront(all_activity, {trunc_created_at})) as previous_activity,
arrayPopFront(arrayPushBack(all_activity, {trunc_epoch})) as following_activity,
arrayMap((previous, current, index) -> (previous = current ? 'new' : ((current - {one_interval_period}) = previous AND index != 1) ? 'returning' : 'resurrecting'), previous_activity, all_activity, arrayEnumerate(all_activity)) as initial_status,
arrayMap((current, next) -> (current + {one_interval_period} = next ? '' : 'dormant'), all_activity, following_activity) as dormant_status,
arrayMap(x -> x + {one_interval_period}, arrayFilter((current, is_dormant) -> is_dormant = 'dormant', all_activity, dormant_status)) as dormant_periods,
Expand All @@ -293,6 +281,15 @@ def events_query(self):
placeholders={
**self.query_date_range.to_placeholders(),
"event_filter": self.event_filter,
"trunc_timestamp": self.query_date_range.date_to_start_of_interval_hogql(
ast.Field(chain=["events", "timestamp"])
),
"trunc_created_at": self.query_date_range.date_to_start_of_interval_hogql(
ast.Field(chain=["created_at"])
),
"trunc_epoch": self.query_date_range.date_to_start_of_interval_hogql(
ast.Call(name="toDateTime", args=[ast.Constant(value="1970-01-01 00:00:00")])
),
},
timings=self.timings,
)
Expand All @@ -308,18 +305,17 @@ def periods_query(self):
with self.timings.measure("periods_query"):
periods_query = parse_select(
"""
SELECT (
dateTrunc({interval}, {date_to}) - {number_interval_period}
) AS start_of_period
FROM numbers(
dateDiff(
{interval},
dateTrunc({interval}, {date_from}),
dateTrunc({interval}, {date_to} + {one_interval_period})
)
)
SELECT ({date_to_start_of_interval} - {number_interval_period}) AS start_of_period
FROM numbers(dateDiff({interval}, {date_from_start_of_interval}, {date_to_plus_interval}))
""",
placeholders=self.query_date_range.to_placeholders(),
placeholders={
**self.query_date_range.to_placeholders(),
"date_to_plus_interval": self.query_date_range.date_to_start_of_interval_hogql(
parse_expr(
"{date_to} + {one_interval_period}", placeholders=self.query_date_range.to_placeholders()
)
),
},
timings=self.timings,
)
return periods_query
Expand Down
Loading

0 comments on commit 19b418d

Please sign in to comment.