Skip to content

Commit

Permalink
Revert "remove broadcast code"
Browse files Browse the repository at this point in the history
This reverts commit 56bc999.
  • Loading branch information
mariusandra committed Oct 25, 2024
1 parent e1c3154 commit ccbdc8c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,9 @@ const api = {
async update(id: HogFunctionType['id'], data: Partial<HogFunctionType>): Promise<HogFunctionType> {
return await new ApiRequest().hogFunction(id).update({ data })
},
async sendBroadcast(id: HogFunctionType['id']): Promise<HogFunctionType> {
return await new ApiRequest().hogFunction(id).withAction('broadcast').create()
},
async logs(
id: HogFunctionType['id'],
params: LogEntryRequestParams = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
templateHasChanged,
forcedSubTemplateId,
type,
broadcastLoading,
} = useValues(logic)
const {
submitConfiguration,
Expand All @@ -80,6 +81,7 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
setConfigurationValue,
deleteHogFunction,
setSubTemplateId,
sendBroadcast,
} = useActions(logic)

if (loading && !loaded) {
Expand Down Expand Up @@ -493,6 +495,37 @@ export function HogFunctionConfiguration({ templateId, id }: HogFunctionConfigur
)}

{!id || id === 'new' ? <HogFunctionTestPlaceholder /> : <HogFunctionTest id={id} />}

{type === 'broadcast' ? (
id && id !== 'new' ? (
<HogFunctionTestPlaceholder
title="Send broadcast"
description={
<div className="space-y-2">
<LemonButton
type="primary"
onClick={sendBroadcast}
loading={personsCountLoading || broadcastLoading}
>
Send to {personsCount} emails
</LemonButton>
<div>
<strong>Please note:</strong> Clicking the button above will
synchronously send to all the e-mails. While this is fine for
testing with small lists, please don't use this for production
usecases yet.
</div>
</div>
}
/>
) : (
<HogFunctionTestPlaceholder
title="Send broadcast"
description="Save to send your broadcast"
/>
)
) : null}

<div className="flex gap-2 justify-end">{saveButtons}</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
setUnsavedConfiguration: (configuration: HogFunctionConfigurationType | null) => ({ configuration }),
persistForUnload: true,
setSampleGlobalsError: (error) => ({ error }),
sendBroadcast: true,
}),
reducers({
showSource: [
Expand Down Expand Up @@ -402,6 +403,22 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
},
},
],

broadcast: [
false,
{
sendBroadcast: async () => {
const id = values.hogFunction?.id
if (!id) {
lemonToast.error('No broadcast to send')
return false
}
await api.hogFunctions.sendBroadcast(id)
lemonToast.success('Broadcast sent!')
return true
},
},
],
})),
forms(({ values, props, asyncActions }) => ({
configuration: {
Expand Down
31 changes: 31 additions & 0 deletions posthog/api/hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from posthog.cdp.templates import HOG_FUNCTION_TEMPLATES_BY_ID
from posthog.cdp.validation import compile_hog, generate_template_bytecode, validate_inputs, validate_inputs_schema
from posthog.constants import AvailableFeature
from posthog.hogql_queries.actors_query_runner import ActorsQueryRunner
from posthog.models.activity_logging.activity_log import log_activity, changes_between, Detail
from posthog.models.hog_functions.hog_function import HogFunction, HogFunctionState
from posthog.plugins.plugin_server_api import create_hog_invocation_test
Expand Down Expand Up @@ -305,6 +306,36 @@ def invocations(self, request: Request, *args, **kwargs):

return Response(res.json())

@action(detail=True, methods=["POST"])
def broadcast(self, request: Request, *args, **kwargs):
hog_function = self.get_object()
actors_query = {
"kind": "ActorsQuery",
"properties": hog_function.filters.get("properties", None),
"select": ["id", "any(pdi.distinct_id)", "properties", "created_at"],
}

response = ActorsQueryRunner(query=actors_query, team=self.team).calculate()

for result in response.results:
globals = {
"person": {
"id": str(result[0]),
"distinct_id": str(result[1]),
"properties": json.loads(result[2]),
"created_at": result[3].isoformat(),
}
}
create_hog_invocation_test(
team_id=hog_function.team_id,
hog_function_id=hog_function.id,
globals=globals,
configuration=HogFunctionSerializer(hog_function).data,
mock_async_functions=False,
)

return Response({"success": True})

def perform_create(self, serializer):
serializer.save()
log_activity(
Expand Down

0 comments on commit ccbdc8c

Please sign in to comment.