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(dev): Show Celery cohort queries in CH debug modal #21173

Merged
merged 14 commits into from
Apr 2, 2024
10 changes: 5 additions & 5 deletions ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# serializer version: 1
# name: TestCohort.test_cohortpeople_basic
'''

/* cohort_calculation: */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -37,7 +37,7 @@
# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator
'''

/* cohort_calculation: */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -71,7 +71,7 @@
# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator.1
'''

/* cohort_calculation: */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -184,7 +184,7 @@
# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts
'''

/* cohort_calculation: */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -225,7 +225,7 @@
# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts.1
'''

/* cohort_calculation: */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
# ---
# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.1
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ---
# name: TestEventQuery.test_account_filters.1
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down
46 changes: 22 additions & 24 deletions frontend/src/lib/components/CommandPalette/DebugCHQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,29 @@ function DebugCHQueries(): JSX.Element {

return (
<>
{!!paths?.length && (
<div className="flex gap-4 items-end justify-between mb-4">
<div className="flex flex-wrap gap-2">
{paths.map(([path, count]) => (
<LemonButton
key={path}
type={pathFilter === path ? 'primary' : 'tertiary'}
size="small"
onClick={() => (pathFilter === path ? setPathFilter(null) : setPathFilter(path))}
>
{path} <span className="ml-0.5 text-muted ligatures-none">({count})</span>
</LemonButton>
))}
</div>
<LemonButton
icon={<IconRefresh />}
disabledReason={queriesLoading ? 'Loading…' : null}
onClick={() => loadQueries()}
size="small"
type="secondary"
>
Refresh
</LemonButton>
<div className="flex gap-4 items-end justify-between mb-4">
<div className="flex flex-wrap gap-2">
{paths?.map(([path, count]) => (
<LemonButton
key={path}
type={pathFilter === path ? 'primary' : 'tertiary'}
size="small"
onClick={() => (pathFilter === path ? setPathFilter(null) : setPathFilter(path))}
>
{path} <span className="ml-0.5 text-muted ligatures-none">({count})</span>
</LemonButton>
))}
</div>
)}
<LemonButton
icon={<IconRefresh />}
disabledReason={queriesLoading ? 'Loading…' : null}
onClick={() => loadQueries()}
size="small"
type="secondary"
>
Refresh
</LemonButton>
</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I wanted to use the button quite often when there were no results yet.


<LemonTable
columns={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ export const commandPaletteLogic = kea<commandPaletteLogicType>([
preflightLogic.values.preflight?.instance_preferences?.debug_queries
? {
icon: IconDatabase,
display: 'Debug ClickHouse Queries',
display: 'Debug ClickHouse queries',
executor: () => openCHQueriesDebugModal(),
}
: [],
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def create(self, validated_data: Dict, *args: Any, **kwargs: Any) -> Cohort:
elif cohort.query is not None:
raise ValidationError("Cannot create a dynamic cohort with a query. Set is_static to true.")
else:
update_cohort(cohort)
update_cohort(cohort, initiating_user=request.user)

report_user_action(request.user, "cohort created", cohort.get_analytics_metadata())
return cohort
Expand Down Expand Up @@ -261,7 +261,7 @@ def update(self, cohort: Cohort, validated_data: Dict, *args: Any, **kwargs: Any
if request.FILES.get("csv"):
self._calculate_static_by_csv(request.FILES["csv"], cohort)
else:
update_cohort(cohort)
update_cohort(cohort, initiating_user=request.user)

report_user_action(
request.user,
Expand Down
8 changes: 4 additions & 4 deletions posthog/api/test/__snapshots__/test_cohort.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ---
# name: TestCohort.test_async_deletion_of_cohort.1
'''
/* user_id:126 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:126 cohort_calculation:posthog.tasks.calculate_cohort.calculate_cohort_ch */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -84,7 +84,7 @@
# ---
# name: TestCohort.test_async_deletion_of_cohort.2
'''
/* user_id:126 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:126 cohort_calculation:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down Expand Up @@ -114,7 +114,7 @@
# ---
# name: TestCohort.test_async_deletion_of_cohort.5
'''
/* user_id:126 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:126 cohort_calculation:posthog.tasks.calculate_cohort.calculate_cohort_ch */
INSERT INTO cohortpeople
SELECT id,
2 as cohort_id,
Expand Down Expand Up @@ -148,7 +148,7 @@
# ---
# name: TestCohort.test_async_deletion_of_cohort.6
'''
/* user_id:126 celery:posthog.tasks.calculate_cohort.calculate_cohort_ch */
/* user_id:126 cohort_calculation:posthog.tasks.calculate_cohort.calculate_cohort_ch */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down
10 changes: 5 additions & 5 deletions posthog/api/test/__snapshots__/test_feature_flag.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
# ---
# name: TestBlastRadius.test_user_blast_radius_with_multiple_precalculated_cohorts.1
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand All @@ -110,7 +110,7 @@
# ---
# name: TestBlastRadius.test_user_blast_radius_with_multiple_precalculated_cohorts.3
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down Expand Up @@ -213,7 +213,7 @@
# ---
# name: TestBlastRadius.test_user_blast_radius_with_multiple_static_cohorts.4
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand All @@ -233,7 +233,7 @@
# ---
# name: TestBlastRadius.test_user_blast_radius_with_multiple_static_cohorts.6
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down Expand Up @@ -308,7 +308,7 @@
# ---
# name: TestBlastRadius.test_user_blast_radius_with_single_cohort.3
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down
40 changes: 22 additions & 18 deletions posthog/api/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from posthog.models.cohort import Cohort
from posthog.models.team.team import Team
from posthog.schema import PropertyOperator
from posthog.tasks.calculate_cohort import calculate_cohort_from_list
from posthog.tasks.calculate_cohort import calculate_cohort_ch, calculate_cohort_from_list
from posthog.tasks.tasks import clickhouse_clear_removed_data
from posthog.test.base import (
APIBaseTest,
Expand All @@ -35,8 +35,9 @@ def capture_select_queries(self):
return self.capture_queries(("INSERT INTO cohortpeople", "SELECT", "ALTER", "select", "DELETE"))

@patch("posthog.api.cohort.report_user_action")
@patch("posthog.tasks.calculate_cohort.calculate_cohort_ch.delay")
def test_creating_update_and_calculating(self, patch_calculate_cohort, patch_capture):
@patch("posthog.tasks.calculate_cohort.calculate_cohort_ch.delay", side_effect=calculate_cohort_ch)
@patch("posthog.models.cohort.util.sync_execute", side_effect=sync_execute)
def test_creating_update_and_calculating(self, patch_sync_execute, patch_calculate_cohort, patch_capture):
self.team.app_urls = ["http://somewebsite.com"]
self.team.save()
Person.objects.create(team=self.team, properties={"team_id": 5})
Expand Down Expand Up @@ -74,21 +75,24 @@ def test_creating_update_and_calculating(self, patch_calculate_cohort, patch_cap
},
)

response = self.client.patch(
f"/api/projects/{self.team.id}/cohorts/{response.json()['id']}",
data={
"name": "whatever2",
"description": "A great cohort!",
"groups": [{"properties": {"team_id": 6}}],
"created_by": "something something",
"last_calculation": "some random date",
"errors_calculating": 100,
"deleted": False,
},
)
self.assertEqual(response.status_code, 200, response.content)
self.assertDictContainsSubset({"name": "whatever2", "description": "A great cohort!"}, response.json())
self.assertEqual(patch_calculate_cohort.call_count, 2)
with self.capture_queries("INSERT INTO cohortpeople") as insert_statements:
response = self.client.patch(
f"/api/projects/{self.team.id}/cohorts/{response.json()['id']}",
data={
"name": "whatever2",
"description": "A great cohort!",
"groups": [{"properties": {"team_id": 6}}],
"created_by": "something something",
"last_calculation": "some random date",
"errors_calculating": 100,
"deleted": False,
},
)
self.assertEqual(response.status_code, 200, response.content)
self.assertDictContainsSubset({"name": "whatever2", "description": "A great cohort!"}, response.json())
self.assertEqual(patch_calculate_cohort.call_count, 2)

self.assertIn(f" user_id:{self.user.id} ", insert_statements[0])

# Assert analytics are sent
patch_capture.assert_called_with(
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/functions/test/test_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_in_cohort_dynamic(self):
team=self.team,
groups=[{"properties": [{"key": "$os", "value": "Chrome", "type": "person"}]}],
)
recalculate_cohortpeople(cohort, pending_version=0)
recalculate_cohortpeople(cohort, pending_version=0, initiating_user_id=None)
response = execute_hogql_query(
f"SELECT event FROM events WHERE person_id IN COHORT {cohort.pk} AND event='{random_uuid}'",
self.team,
Expand Down
2 changes: 1 addition & 1 deletion posthog/hogql/test/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def test_prop_cohort_basic(self):
],
name="cohort",
)
recalculate_cohortpeople(cohort, pending_version=0)
recalculate_cohortpeople(cohort, pending_version=0, initiating_user_id=None)
with override_settings(PERSON_ON_EVENTS_V2_OVERRIDE=False):
response = execute_hogql_query(
"SELECT event, count() FROM events WHERE {cohort_filter} GROUP BY event",
Expand Down
4 changes: 2 additions & 2 deletions posthog/hogql/transforms/test/test_in_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_in_cohort_dynamic(self):
team=self.team,
groups=[{"properties": [{"key": "$os", "value": "Chrome", "type": "person"}]}],
)
recalculate_cohortpeople(cohort, pending_version=0)
recalculate_cohortpeople(cohort, pending_version=0, initiating_user_id=None)
response = execute_hogql_query(
f"SELECT event FROM events WHERE person_id IN COHORT {cohort.pk} AND event='{random_uuid}'",
self.team,
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_in_cohort_conjoined_dynamic(self):
team=self.team,
groups=[{"properties": [{"key": "$os", "value": "Chrome", "type": "person"}]}],
)
recalculate_cohortpeople(cohort, pending_version=0)
recalculate_cohortpeople(cohort, pending_version=0, initiating_user_id=None)
response = execute_hogql_query(
f"SELECT event FROM events WHERE person_id IN COHORT {cohort.pk} AND event='{random_uuid}'",
self.team,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
# ---
# name: TestFOSSFunnel.test_funnel_with_precalculated_cohort_step_filter.1
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down Expand Up @@ -960,6 +960,7 @@
# ---
# name: TestFunnelBreakdown.test_funnel_breakdown_correct_breakdown_props_are_chosen
'''
/* cohort_calculation: */
SELECT [ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, '$browser'), ''), 'null'), '^"|"$', ''), '')] AS value,
count(*) AS count
FROM events AS e
Expand All @@ -981,6 +982,7 @@
# ---
# name: TestFunnelBreakdown.test_funnel_breakdown_correct_breakdown_props_are_chosen.1
'''
/* cohort_calculation: */
SELECT countIf(ifNull(equals(steps, 1), 0)) AS step_1,
countIf(ifNull(equals(steps, 2), 0)) AS step_2,
avg(step_1_average_conversion_time_inner) AS step_1_average_conversion_time,
Expand Down Expand Up @@ -1066,6 +1068,7 @@
# ---
# name: TestFunnelBreakdown.test_funnel_breakdown_correct_breakdown_props_are_chosen_for_step
'''
/* cohort_calculation: */
SELECT [ifNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, '$browser'), ''), 'null'), '^"|"$', ''), '')] AS value,
count(*) AS count
FROM events AS e
Expand All @@ -1087,6 +1090,7 @@
# ---
# name: TestFunnelBreakdown.test_funnel_breakdown_correct_breakdown_props_are_chosen_for_step.1
'''
/* cohort_calculation: */
SELECT countIf(ifNull(equals(steps, 1), 0)) AS step_1,
countIf(ifNull(equals(steps, 2), 0)) AS step_2,
avg(step_1_average_conversion_time_inner) AS step_1_average_conversion_time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ---
# name: TestLifecycleQueryRunner.test_cohort_filter.1
'''

/* cohort_calculation: */
SELECT count(DISTINCT person_id)
FROM cohortpeople
WHERE team_id = 2
Expand Down
Loading
Loading