Skip to content

Commit

Permalink
feat(cdp): rudderstack hog function migrator (#24955)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarconLP authored Sep 13, 2024
1 parent 8f31adc commit a398dd9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
3 changes: 2 additions & 1 deletion posthog/cdp/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +67,7 @@
TemplateEngageMigrator.plugin_url: TemplateEngageMigrator,
TemplatePostHogMigrator.plugin_url: TemplatePostHogMigrator,
TemplateHubspotMigrator.plugin_url: TemplateHubspotMigrator,
TemplateRudderstackMigrator.plugin_url: TemplateRudderstackMigrator,
TemplateLoopsMigrator.plugin_url: TemplateLoopsMigrator,
}

Expand Down
2 changes: 1 addition & 1 deletion posthog/cdp/templates/avo/template_avo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'eventProperties': []
}
fn getPropValueType(propValue) {
fun getPropValueType(propValue) {
let propType := typeof(propValue)
if (propValue == null) {
return 'null'
Expand Down
30 changes: 26 additions & 4 deletions posthog/cdp/templates/rudderstack/template_rudderstack.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -8,7 +10,7 @@
description="Send data to RudderStack",
icon_url="/static/services/rudderstack.png",
hog="""
fn getPayload() {
fun getPayload() {
let rudderPayload := {
'context': {
'app': {
Expand Down Expand Up @@ -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,
Expand All @@ -111,7 +113,7 @@
"type": "string",
"label": "Write API key",
"description": "RudderStack Source Writekey",
"secret": False,
"secret": True,
"required": True,
},
{
Expand All @@ -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
30 changes: 29 additions & 1 deletion posthog/cdp/templates/rudderstack/test_template_rudderstack.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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"] == {}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
'''
Expand All @@ -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
Expand All @@ -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
'''
Expand Down

0 comments on commit a398dd9

Please sign in to comment.