Skip to content

Commit

Permalink
feat(insights): retention appearances columns (#19651)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Jan 8, 2024
1 parent 9efa662 commit 526591f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
3 changes: 2 additions & 1 deletion frontend/src/scenes/retention/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function retentionToActorsQuery(query: RetentionQuery, selectedInterval:
const group = query.aggregation_group_type_index !== undefined
const selectActor = group ? 'group' : 'person'
const totalIntervals = (query.retentionFilter.total_intervals || 11) - selectedInterval
const selects = Array.from({ length: totalIntervals }, (_, intervalNumber) => `appearance_${intervalNumber}`)
const periodName = query.retentionFilter.period?.toLowerCase() ?? 'day'
const selects = Array.from({ length: totalIntervals }, (_, intervalNumber) => `${periodName}_${intervalNumber}`)
return {
kind: NodeKind.ActorsQuery,
select: [selectActor, ...selects],
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql_queries/insights/retention_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def to_actors_query(self, interval: Optional[int] = None) -> ast.SelectQuery:
for i in range(self.query_date_range.total_intervals - interval):
retention_query.select.append(
ast.Alias(
alias=f"appearance_{i}",
alias=f"{self.query_date_range.interval_name}_{i}",
expr=ast.Call(
name="arrayExists",
args=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
(SELECT actor_activity.actor_id AS actor_id,
groupArray(actor_activity.intervals_from_base) AS appearance_intervals,
arraySort(appearance_intervals) AS appearances,
arrayExists(x -> ifNull(equals(x, 0), 0), appearance_intervals) AS appearance_0,
arrayExists(x -> ifNull(equals(x, 1), 0), appearance_intervals) AS appearance_1,
arrayExists(x -> ifNull(equals(x, 2), 0), appearance_intervals) AS appearance_2,
arrayExists(x -> ifNull(equals(x, 3), 0), appearance_intervals) AS appearance_3,
arrayExists(x -> ifNull(equals(x, 4), 0), appearance_intervals) AS appearance_4,
arrayExists(x -> ifNull(equals(x, 5), 0), appearance_intervals) AS appearance_5,
arrayExists(x -> ifNull(equals(x, 6), 0), appearance_intervals) AS appearance_6
arrayExists(x -> ifNull(equals(x, 0), 0), appearance_intervals) AS week_0,
arrayExists(x -> ifNull(equals(x, 1), 0), appearance_intervals) AS week_1,
arrayExists(x -> ifNull(equals(x, 2), 0), appearance_intervals) AS week_2,
arrayExists(x -> ifNull(equals(x, 3), 0), appearance_intervals) AS week_3,
arrayExists(x -> ifNull(equals(x, 4), 0), appearance_intervals) AS week_4,
arrayExists(x -> ifNull(equals(x, 5), 0), appearance_intervals) AS week_5,
arrayExists(x -> ifNull(equals(x, 6), 0), appearance_intervals) AS week_6
FROM
(SELECT DISTINCT breakdown_values AS breakdown_values,
intervals_from_base AS intervals_from_base,
Expand Down Expand Up @@ -213,13 +213,13 @@
(SELECT actor_activity.actor_id AS actor_id,
groupArray(actor_activity.intervals_from_base) AS appearance_intervals,
arraySort(appearance_intervals) AS appearances,
arrayExists(x -> ifNull(equals(x, 0), 0), appearance_intervals) AS appearance_0,
arrayExists(x -> ifNull(equals(x, 1), 0), appearance_intervals) AS appearance_1,
arrayExists(x -> ifNull(equals(x, 2), 0), appearance_intervals) AS appearance_2,
arrayExists(x -> ifNull(equals(x, 3), 0), appearance_intervals) AS appearance_3,
arrayExists(x -> ifNull(equals(x, 4), 0), appearance_intervals) AS appearance_4,
arrayExists(x -> ifNull(equals(x, 5), 0), appearance_intervals) AS appearance_5,
arrayExists(x -> ifNull(equals(x, 6), 0), appearance_intervals) AS appearance_6
arrayExists(x -> ifNull(equals(x, 0), 0), appearance_intervals) AS week_0,
arrayExists(x -> ifNull(equals(x, 1), 0), appearance_intervals) AS week_1,
arrayExists(x -> ifNull(equals(x, 2), 0), appearance_intervals) AS week_2,
arrayExists(x -> ifNull(equals(x, 3), 0), appearance_intervals) AS week_3,
arrayExists(x -> ifNull(equals(x, 4), 0), appearance_intervals) AS week_4,
arrayExists(x -> ifNull(equals(x, 5), 0), appearance_intervals) AS week_5,
arrayExists(x -> ifNull(equals(x, 6), 0), appearance_intervals) AS week_6
FROM
(SELECT DISTINCT breakdown_values AS breakdown_values,
intervals_from_base AS intervals_from_base,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,20 @@ def test_retention_people_basic(self):
self.assertEqual(len(result), 1, result)
self.assertEqual(result[0][0]["id"], person1.uuid, person1.uuid)

# test selecting appearances directly
# test selecting appearances directly (defauly: days)
result_2 = self.run_actors_query(
interval=0,
query={
"dateRange": {"date_to": _date(10, hour=6)},
},
select=["appearance_0", "appearance_1", "appearance_2", "appearance_3", "appearance_4"],
select=["day_0", "day_1", "day_2", "day_3", "day_4"],
)
self.assertEqual(len(result_2), len(result))
self.assertEqual(result_2[0][2], 1) # appearance_0
self.assertEqual(result_2[0][3], 1) # appearance_1
self.assertEqual(result_2[0][4], 1) # appearance_2
self.assertEqual(result_2[0][5], 0) # appearance_3
self.assertEqual(result_2[0][6], 0) # appearance_4
self.assertEqual(result_2[0][2], 1) # day_0
self.assertEqual(result_2[0][3], 1) # day_1
self.assertEqual(result_2[0][4], 1) # day_2
self.assertEqual(result_2[0][5], 0) # day_3
self.assertEqual(result_2[0][6], 0) # day_4

def test_retention_people_first_time(self):
_, _, p3, _ = self._create_first_time_retention_events()
Expand Down

0 comments on commit 526591f

Please sign in to comment.