From 526591f1a43f43c3581dae8991935293cfc31818 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Mon, 8 Jan 2024 16:51:45 +0100 Subject: [PATCH] feat(insights): retention appearances columns (#19651) --- frontend/src/scenes/retention/queries.ts | 3 +- .../insights/retention_query_runner.py | 2 +- .../test_retention_query_runner.ambr | 28 +++++++++---------- .../test/test_retention_query_runner.py | 14 +++++----- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/frontend/src/scenes/retention/queries.ts b/frontend/src/scenes/retention/queries.ts index f049c711582bb..97fb39b2614d0 100644 --- a/frontend/src/scenes/retention/queries.ts +++ b/frontend/src/scenes/retention/queries.ts @@ -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], diff --git a/posthog/hogql_queries/insights/retention_query_runner.py b/posthog/hogql_queries/insights/retention_query_runner.py index d9771bf015246..f9e4cfd71f31d 100644 --- a/posthog/hogql_queries/insights/retention_query_runner.py +++ b/posthog/hogql_queries/insights/retention_query_runner.py @@ -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=[ diff --git a/posthog/hogql_queries/insights/test/__snapshots__/test_retention_query_runner.ambr b/posthog/hogql_queries/insights/test/__snapshots__/test_retention_query_runner.ambr index bf76b434a7048..a09f4c94e6259 100644 --- a/posthog/hogql_queries/insights/test/__snapshots__/test_retention_query_runner.ambr +++ b/posthog/hogql_queries/insights/test/__snapshots__/test_retention_query_runner.ambr @@ -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, @@ -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, diff --git a/posthog/hogql_queries/insights/test/test_retention_query_runner.py b/posthog/hogql_queries/insights/test/test_retention_query_runner.py index fe0c6b4134a9c..93ade3ac992b8 100644 --- a/posthog/hogql_queries/insights/test/test_retention_query_runner.py +++ b/posthog/hogql_queries/insights/test/test_retention_query_runner.py @@ -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()