Skip to content

Commit 873f786

Browse files
committed
feat(surveys): add linked_insight_id field
1 parent ce4ca53 commit 873f786

File tree

15 files changed

+239
-3
lines changed

15 files changed

+239
-3
lines changed

ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,99 @@
661661
join_algorithm = 'auto'
662662
'''
663663
# ---
664+
# name: TestCohortQuery.test_cohort_filter_with_extra.16
665+
'''
666+
(
667+
(SELECT persons.id AS id
668+
FROM
669+
(SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', ''), person.version) AS properties___name,
670+
person.id AS id
671+
FROM person
672+
WHERE and(equals(person.team_id, 99999), in(id,
673+
(SELECT where_optimization.id AS id
674+
FROM person AS where_optimization
675+
WHERE and(equals(where_optimization.team_id, 99999), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test'), 0)))))
676+
GROUP BY person.id
677+
HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))) AS persons
678+
WHERE ifNull(equals(persons.properties___name, 'test'), 0)
679+
ORDER BY persons.id ASC
680+
LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
681+
join_algorithm='auto'))
682+
UNION DISTINCT (
683+
(SELECT source.id AS id
684+
FROM
685+
(SELECT actor_id AS actor_id,
686+
count() AS event_count,
687+
groupUniqArray(distinct_id) AS event_distinct_ids,
688+
actor_id AS id
689+
FROM
690+
(SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
691+
toTimeZone(e.timestamp, 'UTC') AS timestamp,
692+
e.uuid AS uuid,
693+
e.distinct_id AS distinct_id
694+
FROM events AS e
695+
LEFT OUTER JOIN
696+
(SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
697+
person_distinct_id_overrides.distinct_id AS distinct_id
698+
FROM person_distinct_id_overrides
699+
WHERE equals(person_distinct_id_overrides.team_id, 99999)
700+
GROUP BY person_distinct_id_overrides.distinct_id
701+
HAVING ifNull(equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
702+
WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
703+
GROUP BY actor_id) AS source
704+
ORDER BY source.id ASC
705+
LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
706+
join_algorithm='auto')) SETTINGS readonly=2,
707+
max_execution_time=600,
708+
allow_experimental_object_type=1,
709+
format_csv_allow_double_quotes=0,
710+
max_ast_elements=4000000,
711+
max_expanded_ast_elements=4000000,
712+
max_bytes_before_external_group_by=0,
713+
transform_null_in=1,
714+
optimize_min_equality_disjunction_chain_length=4294967295,
715+
allow_experimental_join_condition=1
716+
'''
717+
# ---
718+
# name: TestCohortQuery.test_cohort_filter_with_extra.17
719+
'''
720+
721+
SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id
722+
FROM
723+
(SELECT if(not(empty(pdi.distinct_id)), pdi.person_id, e.person_id) AS person_id,
724+
countIf(timestamp > now() - INTERVAL 1 week
725+
AND timestamp < now()
726+
AND event = '$pageview'
727+
AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_1_level_0_0
728+
FROM events e
729+
LEFT OUTER JOIN
730+
(SELECT distinct_id,
731+
argMax(person_id, version) as person_id
732+
FROM person_distinct_id2
733+
WHERE team_id = 99999
734+
GROUP BY distinct_id
735+
HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id
736+
WHERE team_id = 99999
737+
AND event IN ['$pageview']
738+
AND timestamp <= now()
739+
AND timestamp >= now() - INTERVAL 1 week
740+
GROUP BY person_id) behavior_query
741+
FULL OUTER JOIN
742+
(SELECT *,
743+
id AS person_id
744+
FROM
745+
(SELECT id,
746+
argMax(properties, version) as person_props
747+
FROM person
748+
WHERE team_id = 99999
749+
GROUP BY id
750+
HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id
751+
WHERE 1 = 1
752+
AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(person_props, 'name'), '^"|"$', ''))))
753+
OR ((coalesce(performed_event_condition_None_level_level_0_level_1_level_0_0, false))))) SETTINGS optimize_aggregation_in_order = 1,
754+
join_algorithm = 'auto'
755+
'''
756+
# ---
664757
# name: TestCohortQuery.test_cohort_filter_with_extra.2
665758
'''
666759

frontend/src/queries/schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27027,6 +27027,9 @@
2702727027
"linked_flag_id": {
2702827028
"type": "number"
2702927029
},
27030+
"linked_insight_id": {
27031+
"type": "number"
27032+
},
2703027033
"name": {
2703127034
"type": "string"
2703227035
},

frontend/src/queries/schema/schema-surveys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface SurveyCreationSchema {
1818
description: string
1919
type: SurveyType
2020
linked_flag_id?: number
21+
linked_insight_id?: number
2122
questions: SurveyQuestionSchema[]
2223
should_launch?: boolean
2324
conditions?: SurveyDisplayConditionsSchema

posthog/api/survey.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class SurveyRates(TypedDict):
132132
class SurveySerializer(UserAccessControlSerializerMixin, serializers.ModelSerializer):
133133
linked_flag_id = serializers.IntegerField(required=False, allow_null=True, source="linked_flag.id")
134134
linked_flag = MinimalFeatureFlagSerializer(read_only=True)
135+
linked_insight_id = serializers.IntegerField(required=False, allow_null=True, source="linked_insight.id")
135136
targeting_flag = MinimalFeatureFlagSerializer(read_only=True)
136137
internal_targeting_flag = MinimalFeatureFlagSerializer(read_only=True)
137138
created_by = UserBasicSerializer(read_only=True)
@@ -168,6 +169,7 @@ class Meta:
168169
"schedule",
169170
"linked_flag",
170171
"linked_flag_id",
172+
"linked_insight_id",
171173
"targeting_flag",
172174
"internal_targeting_flag",
173175
"questions",
@@ -208,6 +210,7 @@ def get_conditions(self, survey: Survey):
208210
class SurveySerializerCreateUpdateOnly(serializers.ModelSerializer):
209211
linked_flag = MinimalFeatureFlagSerializer(read_only=True)
210212
linked_flag_id = serializers.IntegerField(required=False, write_only=True, allow_null=True)
213+
linked_insight_id = serializers.IntegerField(required=False, write_only=True, allow_null=True)
211214
targeting_flag_id = serializers.IntegerField(required=False, write_only=True)
212215
targeting_flag_filters = serializers.JSONField(required=False, write_only=True, allow_null=True)
213216
remove_targeting_flag = serializers.BooleanField(required=False, write_only=True, allow_null=True)
@@ -232,6 +235,7 @@ class Meta:
232235
"schedule",
233236
"linked_flag",
234237
"linked_flag_id",
238+
"linked_insight_id",
235239
"targeting_flag_id",
236240
"targeting_flag",
237241
"internal_targeting_flag",
@@ -829,7 +833,9 @@ def _create_or_update_targeting_flag(
829833

830834
class SurveyViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.ModelViewSet):
831835
scope_object = "survey"
832-
queryset = Survey.objects.select_related("linked_flag", "targeting_flag", "internal_targeting_flag").all()
836+
queryset = Survey.objects.select_related(
837+
"linked_flag", "linked_insight", "targeting_flag", "internal_targeting_flag"
838+
).all()
833839
filter_backends = [filters.SearchFilter]
834840
search_fields = ["name", "description"]
835841

posthog/api/test/__snapshots__/test_decide.ambr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,7 @@
37453745
"posthog_survey"."description",
37463746
"posthog_survey"."linked_flag_id",
37473747
"posthog_survey"."targeting_flag_id",
3748+
"posthog_survey"."linked_insight_id",
37483749
"posthog_survey"."internal_targeting_flag_id",
37493750
"posthog_survey"."internal_response_sampling_flag_id",
37503751
"posthog_survey"."type",
@@ -4127,6 +4128,7 @@
41274128
"posthog_survey"."description",
41284129
"posthog_survey"."linked_flag_id",
41294130
"posthog_survey"."targeting_flag_id",
4131+
"posthog_survey"."linked_insight_id",
41304132
"posthog_survey"."internal_targeting_flag_id",
41314133
"posthog_survey"."internal_response_sampling_flag_id",
41324134
"posthog_survey"."type",
@@ -4599,6 +4601,7 @@
45994601
"posthog_survey"."description",
46004602
"posthog_survey"."linked_flag_id",
46014603
"posthog_survey"."targeting_flag_id",
4604+
"posthog_survey"."linked_insight_id",
46024605
"posthog_survey"."internal_targeting_flag_id",
46034606
"posthog_survey"."internal_response_sampling_flag_id",
46044607
"posthog_survey"."type",
@@ -5016,6 +5019,7 @@
50165019
"posthog_survey"."description",
50175020
"posthog_survey"."linked_flag_id",
50185021
"posthog_survey"."targeting_flag_id",
5022+
"posthog_survey"."linked_insight_id",
50195023
"posthog_survey"."internal_targeting_flag_id",
50205024
"posthog_survey"."internal_response_sampling_flag_id",
50215025
"posthog_survey"."type",
@@ -5482,6 +5486,7 @@
54825486
"posthog_survey"."description",
54835487
"posthog_survey"."linked_flag_id",
54845488
"posthog_survey"."targeting_flag_id",
5489+
"posthog_survey"."linked_insight_id",
54855490
"posthog_survey"."internal_targeting_flag_id",
54865491
"posthog_survey"."internal_response_sampling_flag_id",
54875492
"posthog_survey"."type",
@@ -5856,6 +5861,7 @@
58565861
"posthog_survey"."description",
58575862
"posthog_survey"."linked_flag_id",
58585863
"posthog_survey"."targeting_flag_id",
5864+
"posthog_survey"."linked_insight_id",
58595865
"posthog_survey"."internal_targeting_flag_id",
58605866
"posthog_survey"."internal_response_sampling_flag_id",
58615867
"posthog_survey"."type",

posthog/api/test/__snapshots__/test_organization_feature_flag.ambr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,7 @@
19491949
"posthog_survey"."description",
19501950
"posthog_survey"."linked_flag_id",
19511951
"posthog_survey"."targeting_flag_id",
1952+
"posthog_survey"."linked_insight_id",
19521953
"posthog_survey"."internal_targeting_flag_id",
19531954
"posthog_survey"."internal_response_sampling_flag_id",
19541955
"posthog_survey"."type",
@@ -1986,6 +1987,7 @@
19861987
"posthog_survey"."description",
19871988
"posthog_survey"."linked_flag_id",
19881989
"posthog_survey"."targeting_flag_id",
1990+
"posthog_survey"."linked_insight_id",
19891991
"posthog_survey"."internal_targeting_flag_id",
19901992
"posthog_survey"."internal_response_sampling_flag_id",
19911993
"posthog_survey"."type",
@@ -2244,6 +2246,7 @@
22442246
"posthog_survey"."description",
22452247
"posthog_survey"."linked_flag_id",
22462248
"posthog_survey"."targeting_flag_id",
2249+
"posthog_survey"."linked_insight_id",
22472250
"posthog_survey"."internal_targeting_flag_id",
22482251
"posthog_survey"."internal_response_sampling_flag_id",
22492252
"posthog_survey"."type",

posthog/api/test/__snapshots__/test_survey.ambr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
"posthog_survey"."description",
206206
"posthog_survey"."linked_flag_id",
207207
"posthog_survey"."targeting_flag_id",
208+
"posthog_survey"."linked_insight_id",
208209
"posthog_survey"."internal_targeting_flag_id",
209210
"posthog_survey"."internal_response_sampling_flag_id",
210211
"posthog_survey"."type",

posthog/api/test/test_survey.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ def test_can_list_surveys(self):
11431143
},
11441144
"linked_flag": None,
11451145
"linked_flag_id": None,
1146+
"linked_insight_id": None,
11461147
"conditions": None,
11471148
"archived": False,
11481149
"start_date": None,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Generated by Django 4.2.26 on 2025-11-18 16:03
2+
3+
import django.db.models.deletion
4+
from django.db import migrations, models
5+
6+
7+
# see 0415_pluginconfig_match_action.py
8+
class Migration(migrations.Migration):
9+
atomic = False # Added to support concurrent index creation
10+
dependencies = [
11+
("posthog", "0904_alter_dashboard_creation_mode"),
12+
]
13+
14+
operations = [
15+
migrations.SeparateDatabaseAndState(
16+
state_operations=[
17+
migrations.AddField(
18+
model_name="survey",
19+
name="linked_insight",
20+
field=models.ForeignKey(
21+
blank=True,
22+
null=True,
23+
on_delete=django.db.models.deletion.SET_NULL,
24+
related_name="surveys_linked_insight",
25+
related_query_name="survey_linked_insight",
26+
to="posthog.insight",
27+
),
28+
)
29+
],
30+
database_operations=[
31+
migrations.RunSQL(
32+
"""
33+
ALTER TABLE "posthog_survey" ADD COLUMN "linked_insight_id" integer NULL CONSTRAINT "posthog_survey_linked_insight_id_586524f3_fk_posthog_d" REFERENCES "posthog_dashboarditem"("id") DEFERRABLE INITIALLY DEFERRED; -- existing-table-constraint-ignore
34+
SET CONSTRAINTS "posthog_survey_linked_insight_id_586524f3_fk_posthog_d" IMMEDIATE; -- existing-table-constraint-ignore
35+
""",
36+
reverse_sql="""
37+
ALTER TABLE "posthog_survey" DROP COLUMN IF EXISTS "linked_insight_id";
38+
""",
39+
),
40+
migrations.RunSQL(
41+
"""
42+
CREATE INDEX CONCURRENTLY IF NOT EXISTS "posthog_survey_linked_insight_id_586524f3" ON "posthog_survey" ("linked_insight_id");
43+
""",
44+
reverse_sql="""
45+
DROP INDEX IF EXISTS "posthog_survey_linked_insight_id_586524f3";
46+
""",
47+
),
48+
],
49+
)
50+
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0904_alter_dashboard_creation_mode
1+
0905_survey_add_linked_insight

0 commit comments

Comments
 (0)