-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d26be62
commit f1e7601
Showing
14 changed files
with
638 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
posthog/cdp/templates/google_pubsub/template_google_pubsub.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import dataclasses | ||
import json | ||
from copy import deepcopy | ||
|
||
from posthog.cdp.templates.hog_function_template import HogFunctionTemplate, HogFunctionTemplateMigrator | ||
from posthog.models.integration import GoogleCloudIntegration | ||
|
||
template: HogFunctionTemplate = HogFunctionTemplate( | ||
status="beta", | ||
id="template-google-pubsub", | ||
name="Google Pub/Sub", | ||
description="Send data to a Google Pub/Sub topic", | ||
icon_url="/static/services/google-cloud.png", | ||
hog=""" | ||
let headers := () -> { | ||
'Authorization': f'Bearer {inputs.auth.access_token}', | ||
'Content-Type': 'application/json' | ||
} | ||
let message := () -> { | ||
'messageId': event.uuid, | ||
'data': base64Encode(jsonStringify(inputs.payload)), | ||
'attributes': inputs.attributes | ||
} | ||
let res := fetch(f'https://pubsub.googleapis.com/v1/{inputs.topicId}:publish', { | ||
'method': 'POST', | ||
'headers': headers(), | ||
'body': jsonStringify({ 'messages': [message()] }) | ||
}) | ||
if (res.status >= 200 and res.status < 300) { | ||
print('Event sent successfully!') | ||
} else { | ||
throw Error('Error sending event', res) | ||
} | ||
""".strip(), | ||
inputs_schema=[ | ||
{ | ||
"key": "auth", | ||
"type": "integration", | ||
"integration": "google-pubsub", | ||
"label": "Google Cloud service account", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "topicId", | ||
"type": "string", | ||
"label": "Topic name", | ||
"secret": False, | ||
"required": True, | ||
}, | ||
{ | ||
"key": "payload", | ||
"type": "json", | ||
"label": "Message Payload", | ||
"default": {"event": "{event}", "person": "{person}"}, | ||
"secret": False, | ||
"required": False, | ||
}, | ||
{ | ||
"key": "attributes", | ||
"type": "json", | ||
"label": "Attributes", | ||
"default": {}, | ||
"secret": False, | ||
"required": False, | ||
}, | ||
], | ||
) | ||
|
||
|
||
class TemplateGooglePubSubMigrator(HogFunctionTemplateMigrator): | ||
plugin_url = "https://github.com/PostHog/pubsub-plugin" | ||
|
||
@classmethod | ||
def migrate(cls, obj): | ||
hf = deepcopy(dataclasses.asdict(template)) | ||
|
||
exportEventsToIgnore = obj.config.get("exportEventsToIgnore", "") | ||
topicId = obj.config.get("topicId", "") | ||
|
||
from posthog.models.plugin import PluginAttachment | ||
|
||
attachment: PluginAttachment | None = PluginAttachment.objects.filter( | ||
plugin_config=obj, key="googleCloudKeyJson" | ||
).first() | ||
if not attachment: | ||
raise Exception("Google Cloud Key JSON not found") | ||
|
||
keyFile = json.loads(attachment.contents.decode("UTF-8")) # type: ignore | ||
integration = GoogleCloudIntegration.integration_from_key("google-pubsub", keyFile, obj.team.pk) | ||
|
||
hf["filters"] = {} | ||
if exportEventsToIgnore: | ||
events = exportEventsToIgnore.split(",") | ||
if len(events) > 0: | ||
event_names = ", ".join(["'{}'".format(event.strip()) for event in events]) | ||
query = f"event not in ({event_names})" | ||
hf["filters"]["events"] = [ | ||
{ | ||
"id": None, | ||
"name": "All events", | ||
"type": "events", | ||
"order": 0, | ||
"properties": [{"key": query, "type": "hogql"}], | ||
} | ||
] | ||
|
||
hf["inputs"] = { | ||
"topicId": {"value": topicId}, | ||
"payload": { | ||
"value": { | ||
"event": "{event.name}", | ||
"distinct_id": "{event.distinct_id}", | ||
"timestamp": "{event.timestamp}", | ||
"uuid": "{event.uuid}", | ||
"properties": "{event.properties}", | ||
"person_id": "{person.id}", | ||
"person_properties": "{person.properties}", | ||
} | ||
}, | ||
"auth": {"value": integration.id}, | ||
"attributes": {"value": {}}, | ||
} | ||
|
||
return hf |
Oops, something went wrong.