From a398dd9b86b1a3a7a8f2575fec606057235c1c6c Mon Sep 17 00:00:00 2001 From: Marcus Hof <13001502+MarconLP@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:02:48 +0200 Subject: [PATCH] feat(cdp): rudderstack hog function migrator (#24955) --- posthog/cdp/templates/__init__.py | 3 +- posthog/cdp/templates/avo/template_avo.py | 2 +- .../rudderstack/template_rudderstack.py | 30 ++++++++++++++++--- .../rudderstack/test_template_rudderstack.py | 30 ++++++++++++++++++- .../test/__snapshots__/test_in_cohort.ambr | 8 ++--- 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/posthog/cdp/templates/__init__.py b/posthog/cdp/templates/__init__.py index 0b818d109c072..5d7901784e300 100644 --- a/posthog/cdp/templates/__init__.py +++ b/posthog/cdp/templates/__init__.py @@ -17,7 +17,7 @@ from .mailgun.template_mailgun import template_mailgun_send_email as mailgun from .avo.template_avo import template as avo from .loops.template_loops import template as loops, TemplateLoopsMigrator -from .rudderstack.template_rudderstack import template as rudderstack +from .rudderstack.template_rudderstack import template as rudderstack, TemplateRudderstackMigrator from .gleap.template_gleap import template as gleap from .google_pubsub.template_google_pubsub import template as google_pubsub, TemplateGooglePubSubMigrator from .engage.template_engage import template as engage, TemplateEngageMigrator @@ -67,6 +67,7 @@ TemplateEngageMigrator.plugin_url: TemplateEngageMigrator, TemplatePostHogMigrator.plugin_url: TemplatePostHogMigrator, TemplateHubspotMigrator.plugin_url: TemplateHubspotMigrator, + TemplateRudderstackMigrator.plugin_url: TemplateRudderstackMigrator, TemplateLoopsMigrator.plugin_url: TemplateLoopsMigrator, } diff --git a/posthog/cdp/templates/avo/template_avo.py b/posthog/cdp/templates/avo/template_avo.py index 98997294e63e8..17fa7f4d32d5b 100644 --- a/posthog/cdp/templates/avo/template_avo.py +++ b/posthog/cdp/templates/avo/template_avo.py @@ -33,7 +33,7 @@ 'eventProperties': [] } -fn getPropValueType(propValue) { +fun getPropValueType(propValue) { let propType := typeof(propValue) if (propValue == null) { return 'null' diff --git a/posthog/cdp/templates/rudderstack/template_rudderstack.py b/posthog/cdp/templates/rudderstack/template_rudderstack.py index efbfafbaeaa9f..40994a6188d8a 100644 --- a/posthog/cdp/templates/rudderstack/template_rudderstack.py +++ b/posthog/cdp/templates/rudderstack/template_rudderstack.py @@ -1,5 +1,7 @@ -from posthog.cdp.templates.hog_function_template import HogFunctionTemplate +import dataclasses +from copy import deepcopy +from posthog.cdp.templates.hog_function_template import HogFunctionTemplate, HogFunctionTemplateMigrator template: HogFunctionTemplate = HogFunctionTemplate( status="alpha", @@ -8,7 +10,7 @@ description="Send data to RudderStack", icon_url="/static/services/rudderstack.png", hog=""" -fn getPayload() { +fun getPayload() { let rudderPayload := { 'context': { 'app': { @@ -101,7 +103,7 @@ "key": "host", "type": "string", "label": "Rudderstack host", - "description": "The destination of the Rudderstack instance", + "description": "The Rudderstack destination instance", "default": "https://hosted.rudderlabs.com", "secret": False, "required": True, @@ -111,7 +113,7 @@ "type": "string", "label": "Write API key", "description": "RudderStack Source Writekey", - "secret": False, + "secret": True, "required": True, }, { @@ -124,3 +126,23 @@ }, ], ) + + +class TemplateRudderstackMigrator(HogFunctionTemplateMigrator): + plugin_url = "https://github.com/PostHog/rudderstack-posthog-plugin" + + @classmethod + def migrate(cls, obj): + hf = deepcopy(dataclasses.asdict(template)) + + host = obj.config.get("dataPlaneUrl", "https://hosted.rudderlabs.com") + token = obj.config.get("writeKey", "") + + hf["inputs"] = { + "host": {"value": host}, + "token": {"value": token}, + "identifier": {"value": "{event.properties.$user_id ?? event.distinct_id ?? person.uuid}"}, + } + hf["filters"] = {} + + return hf diff --git a/posthog/cdp/templates/rudderstack/test_template_rudderstack.py b/posthog/cdp/templates/rudderstack/test_template_rudderstack.py index 61469f8d3f79d..0eb9c52073c98 100644 --- a/posthog/cdp/templates/rudderstack/test_template_rudderstack.py +++ b/posthog/cdp/templates/rudderstack/test_template_rudderstack.py @@ -1,6 +1,12 @@ from inline_snapshot import snapshot + from posthog.cdp.templates.helpers import BaseHogFunctionTemplateTest -from posthog.cdp.templates.rudderstack.template_rudderstack import template as template_rudderstack +from posthog.models import PluginConfig +from posthog.test.base import BaseTest +from posthog.cdp.templates.rudderstack.template_rudderstack import ( + template as template_rudderstack, + TemplateRudderstackMigrator, +) class TestTemplateRudderstack(BaseHogFunctionTemplateTest): @@ -105,3 +111,25 @@ def test_automatic_action_mapping(self): ) assert self.get_mock_fetch_calls()[0][1]["body"]["batch"][0]["type"] == expected_action + + +class TestTemplateMigration(BaseTest): + def get_plugin_config(self, config: dict): + _config = { + "dataPlaneUrl": "us.i.example.com", + "writeKey": "ignored", + } + _config.update(config) + return PluginConfig(enabled=True, order=0, config=_config) + + def test_default_config(self): + obj = self.get_plugin_config({}) + template = TemplateRudderstackMigrator.migrate(obj) + assert template["inputs"] == snapshot( + { + "host": {"value": "us.i.example.com"}, + "token": {"value": "ignored"}, + "identifier": {"value": "{event.properties.$user_id ?? event.distinct_id ?? person.uuid}"}, + } + ) + assert template["filters"] == {} diff --git a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr index f40b7c49597da..369f18ab9d118 100644 --- a/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr +++ b/posthog/hogql/transforms/test/__snapshots__/test_in_cohort.ambr @@ -31,7 +31,7 @@ FROM events LEFT JOIN ( SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id FROM person_static_cohort - WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [1]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id) + WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [2]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id) WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0)) LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0 @@ -42,7 +42,7 @@ FROM events LEFT JOIN ( SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id FROM static_cohort_people - WHERE in(cohort_id, [1])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id) + WHERE in(cohort_id, [2])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id) WHERE and(1, equals(__in_cohort.matched, 1)) LIMIT 100 ''' @@ -55,7 +55,7 @@ FROM events LEFT JOIN ( SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id FROM person_static_cohort - WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [2]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id) + WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [3]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id) WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0)) LIMIT 100 SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0 @@ -66,7 +66,7 @@ FROM events LEFT JOIN ( SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id FROM static_cohort_people - WHERE in(cohort_id, [2])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id) + WHERE in(cohort_id, [3])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id) WHERE and(1, equals(__in_cohort.matched, 1)) LIMIT 100 '''