Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Calls Webhook on Cohort Update #18735

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,22 @@
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 0
AND version = NULL
'
---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.2
'

SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 0
'
---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.3
'

SELECT id
FROM person
WHERE team_id = 2
Expand Down
12 changes: 11 additions & 1 deletion ee/clickhouse/queries/test/__snapshots__/test_event_query.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 0
AND version = NULL
'
---
# name: TestEventQuery.test_account_filters.2
'

SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 0
'
---
# name: TestEventQuery.test_account_filters.3
'
SELECT e.timestamp as timestamp,
pdi.person_id as person_id
FROM events e
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion frontend/src/scenes/cohorts/CohortEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import { Form } from 'kea-forms'
import { NotFound } from 'lib/components/NotFound'
import { Query } from '~/queries/Query/Query'
import { pluralize } from 'lib/utils'
import { LemonDivider } from '@posthog/lemon-ui'
import { LemonCheckbox, LemonDivider, Link } from '@posthog/lemon-ui'
import { AndOrFilterSelect } from '~/queries/nodes/InsightViz/PropertyGroupFilters/AndOrFilterSelect'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { NotebookSelectButton } from 'scenes/notebooks/NotebookSelectButton/NotebookSelectButton'
import { teamLogic } from 'scenes/teamLogic'

export function CohortEdit({ id }: CohortLogicProps): JSX.Element {
const logicProps = { id }
const logic = cohortEditLogic(logicProps)
const { currentTeam } = useValues(teamLogic)
const slackEnabled = currentTeam?.slack_incoming_webhook
const { deleteCohort, setOuterGroupsType, setQuery, duplicateCohort } = useActions(logic)
const { cohort, cohortLoading, cohortMissing, query, duplicatedCohortLoading } = useValues(logic)
const { hasAvailableFeature } = useValues(userLogic)
Expand Down Expand Up @@ -230,6 +233,25 @@ export function CohortEdit({ id }: CohortLogicProps): JSX.Element {
/>
</div>
<CohortCriteriaGroups id={logicProps.id} />
<Divider />
<Field name="post_to_webhook">
{({ value, onChange }) => (
<div>
<LemonCheckbox
id="webhook-checkbox"
checked={!!value}
onChange={onChange}
disabledReason={!slackEnabled && 'Configure webhooks in project settings'}
label={<span>Post to webhook when this cohort is updated.</span>}
/>
<div className="mt-1 pl-6">
<Link to={urls.settings('project-integrations', 'integration-webhooks')}>
{slackEnabled ? 'Configure' : 'Enable'} webhooks in project settings.
</Link>
</div>
</div>
)}
</Field>
</>
)}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/scenes/cohorts/cohortUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function isValidCohortGroup(criteria: AnyCohortGroupType): boolean {

export function createCohortFormData(cohort: CohortType): FormData {
const rawCohort = {
...(cohort.post_to_webhook ? { post_to_webhook: cohort.post_to_webhook } : {}),
...(cohort.name ? { name: cohort.name } : {}),
...(cohort.description ? { description: cohort.description } : {}),
...(cohort.csv ? { csv: cohort.csv } : {}),
Expand Down
1 change: 1 addition & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ export interface CohortType {
last_calculation?: string
is_static?: boolean
name?: string
post_to_webhook?: boolean
csv?: UploadFile
groups: CohortGroupType[] // To be deprecated once `filter` takes over
filters: {
Expand Down
2 changes: 1 addition & 1 deletion latest_migrations.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ contenttypes: 0002_remove_content_type_name
ee: 0015_add_verified_properties
otp_static: 0002_throttling
otp_totp: 0002_auto_20190420_0723
posthog: 0364_team_external_data_workspace_rows
posthog: 0365_merge_20231118_0612
sessions: 0001_initial
social_django: 0010_uid_db_index
two_factor: 0007_auto_20201201_1019
1 change: 1 addition & 0 deletions plugin-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ export interface ClickHousePersonDistinctId2 {
export interface Cohort {
id: number
name: string
post_to_webhook: boolean
description: string
deleted: boolean
groups: any[]
Expand Down
5 changes: 3 additions & 2 deletions plugin-server/src/utils/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ export class DB {
`INSERT INTO posthog_cohort (name, description, deleted, groups, team_id,
created_at, created_by_id, is_calculating,
last_calculation, errors_calculating, is_static,
version, pending_version)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
version, pending_version, post_to_webhook)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
RETURNING *;`,
[
cohort.name,
Expand All @@ -978,6 +978,7 @@ export class DB {
cohort.is_static ?? false,
cohort.version,
cohort.pending_version ?? cohort.version ?? 0,
cohort.post_to_webhook ?? false,
],
'createCohort'
)
Expand Down
2 changes: 2 additions & 0 deletions posthog/api/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Meta:
fields = [
"id",
"name",
"post_to_webhook",
"description",
"groups",
"deleted",
Expand Down Expand Up @@ -190,6 +191,7 @@ def update(self, cohort: Cohort, validated_data: Dict, *args: Any, **kwargs: Any
user = cast(User, request.user)

cohort.name = validated_data.get("name", cohort.name)
cohort.post_to_webhook = validated_data.get("post_to_webhook", False)
cohort.description = validated_data.get("description", cohort.description)
cohort.groups = validated_data.get("groups", cohort.groups)
cohort.is_static = validated_data.get("is_static", cohort.is_static)
Expand Down
130 changes: 75 additions & 55 deletions posthog/api/test/__snapshots__/test_cohort.ambr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# name: TestCohort.test_async_deletion_of_cohort
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand All @@ -10,7 +10,56 @@
---
# name: TestCohort.test_async_deletion_of_cohort.1
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = NULL
'
---
# name: TestCohort.test_async_deletion_of_cohort.10
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
SELECT DISTINCT team_id,
cohort_id
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '132'
AND version < '2')
'
---
# name: TestCohort.test_async_deletion_of_cohort.11
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
DELETE
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '132'
AND version < '2')
'
---
# name: TestCohort.test_async_deletion_of_cohort.12
'
SELECT count()
FROM cohortpeople
WHERE cohort_id = 2
'
---
# name: TestCohort.test_async_deletion_of_cohort.13
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
SELECT DISTINCT team_id,
cohort_id
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '132'
AND version < '2')
'
---
# name: TestCohort.test_async_deletion_of_cohort.2
'
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand All @@ -23,7 +72,7 @@
(SELECT pdi.person_id AS person_id,
countIf(timestamp > now() - INTERVAL 1 day
AND timestamp < now()
AND event = '$pageview') > 0 AS performed_event_condition_1_level_level_0_level_1_level_0_0
AND event = '$pageview') > 0 AS performed_event_condition_132_level_level_0_level_1_level_0_0
FROM events e
INNER JOIN
(SELECT distinct_id,
Expand All @@ -49,7 +98,7 @@
HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id
WHERE 1 = 1
AND ((((has(['something'], replaceRegexpAll(JSONExtractRaw(person_props, '$some_prop'), '^"|"$', ''))))
OR ((performed_event_condition_1_level_level_0_level_1_level_0_0)))) ) as person
OR ((performed_event_condition_132_level_level_0_level_1_level_0_0)))) ) as person
UNION ALL
SELECT person_id,
cohort_id,
Expand All @@ -63,57 +112,49 @@
AND sign = 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.10
'
SELECT count()
FROM cohortpeople
WHERE cohort_id = 2
'
---
# name: TestCohort.test_async_deletion_of_cohort.11
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
SELECT DISTINCT team_id,
cohort_id
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '1'
AND version < '2')
'
---
# name: TestCohort.test_async_deletion_of_cohort.2
# name: TestCohort.test_async_deletion_of_cohort.3
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.3
# name: TestCohort.test_async_deletion_of_cohort.4
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.clear_stale_cohort */
/* user_id:501 celery:posthog.tasks.calculate_cohort.clear_stale_cohort */
SELECT count()
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version < 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.4
# name: TestCohort.test_async_deletion_of_cohort.5
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.5
# name: TestCohort.test_async_deletion_of_cohort.6
'
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.7
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -145,44 +186,23 @@
AND sign = 1
'
---
# name: TestCohort.test_async_deletion_of_cohort.6
# name: TestCohort.test_async_deletion_of_cohort.8
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:501 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version = 2
'
---
# name: TestCohort.test_async_deletion_of_cohort.7
# name: TestCohort.test_async_deletion_of_cohort.9
'
/* user_id:119 celery:posthog.tasks.calculate_cohort.clear_stale_cohort */
/* user_id:501 celery:posthog.tasks.calculate_cohort.clear_stale_cohort */
SELECT count()
FROM cohortpeople
WHERE team_id = 2
AND cohort_id = 2
AND version < 2
'
---
# name: TestCohort.test_async_deletion_of_cohort.8
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
SELECT DISTINCT team_id,
cohort_id
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '1'
AND version < '2')
'
---
# name: TestCohort.test_async_deletion_of_cohort.9
'
/* celery:posthog.celery.clickhouse_clear_removed_data */
DELETE
FROM cohortpeople
WHERE (team_id = 2
AND cohort_id = '1'
AND version < '2')
'
---
Loading