:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(clickhouse): return frozen dataclasses instead of tuples (#76129)
Arno Hillenf7334d61acd7
diff --git a/posthog/admin/admins/tophog_admin.py b/posthog/admin/admins/tophog_admin.pyindex bfdb6a5a04a9..a7b901d65472 100644--- a/posthog/admin/admins/tophog_admin.py+++ b/posthog/admin/admins/tophog_admin.py@@ -11,7 +11,7 @@ from django.urls import reversefrom posthog.models.event_ingestion_restriction_config import EventIngestionRestrictionConfigfrom posthog.models.team.team import Team-from posthog.models.tophog.queries import query_tophog_filter_options, query_tophog_metrics+from posthog.models.tophog.queries import TopHogFilterOptions, query_tophog_filter_options, query_tophog_metricslogger = logging.getLogger(__name__)@@ -177,13 +177,12 @@ def tophog_dashboard_view(request):selected_pipeline = request.GET.get("pipeline", "")selected_lane = request.GET.get("lane", "")- pipelines: list[str] = []- lanes: list[str] = []+ filter_options = TopHogFilterOptions(pipelines=[], lanes=[])metrics: dict[str, list[dict]] = defaultdict(list)error = ""try:- pipelines, lanes = query_tophog_filter_options(date_from, date_to)+ filter_options = query_tophog_filter_options(date_from, date_to)rows = query_tophog_metrics(date_from,@@ -228,8 +227,8 @@ def tophog_dashboard_view(request):"title": "TopHog Dashboard","error": error,"metrics": dict(metrics),- "pipelines": pipelines,- "lanes": lanes,+ "pipelines": filter_options.pipelines,+ "lanes": filter_options.lanes,"selected_pipeline": selected_pipeline,"selected_lane": selected_lane,"presets": list(PRESETS.keys()),diff --git a/posthog/clickhouse/client/execute.py b/posthog/clickhouse/client/execute.pyindex f7c9981e1c3e..2547a3cbd40a 100644--- a/posthog/clickhouse/client/execute.py+++ b/posthog/clickhouse/client/execute.py@@ -38,6 +38,7 @@ from posthog.clickhouse.query_tagging import (get_query_tags,is_api_key_access_method,)+from posthog.dataclasses import frozenfrom posthog.errors import clickhouse_error_type, wrap_clickhouse_query_errorfrom posthog.settings import CLICKHOUSE_PER_TEAM_QUERY_SETTINGS, DEBUG, TESTfrom posthog.utils import generate_short_id, patchable@@ -132,10 +133,10 @@ def get_team_kill_switch_level(team_id: int) -> KillSwitchLevel:else OFF. This is independent of the global `CLICKHOUSE_KILL_SWITCH` — callersthat want the combined effect should take the more severe of the two levels."""- full_teams, light_teams = _get_kill_switch_team_sets(round(time.time() / 60))- if team_id in full_teams:+ team_sets = _get_kill_switch_team_sets(round(time.time() / 60))+ if team_id in team_sets.full_teams:return KillSwitchLevel.FULL- if team_id in light_teams:+ if team_id in team_sets.light_teams:return KillSwitchLevel.LIGHTreturn KillSwitchLevel.OFF@@ -166,8 +167,14 @@ def _get_kill_switch_level(_ttl: int) -> KillSwitchLevel:return KillSwitchLevel.OFF+@frozen+class KillSwitchTeamSets:+ full_teams: frozenset[int]+ light_teams: frozenset[int]++@lru_cache(maxsize=1)-def _get_kill_switch_team_sets(_ttl: int) -> tuple[frozenset[int], frozenset[int]]:+def _get_kill_switch_team_sets(_ttl: int) -> KillSwitchTeamSets:from posthog.models.instance_setting import get_instance_settingtry:@@ -184,7 +191,7 @@ def _get_kill_switch_team_sets(_ttl: int) -> tuple[frozenset[int], frozenset[intexcept Exception:logger.exception("Failed to read CLICKHOUSE_KILL_SWITCH_LIGHT_TEAMS; per-team kill switch disabled for light")light_teams = frozenset()- return full_teams, light_teams+ return KillSwitchTeamSets(full_teams=full_teams, light_teams=light_teams)def resolve_kill_switch_level(team_id: Optional[int]) -> KillSwitchLevel:diff --git a/posthog/models/tophog/queries.py b/posthog/models/tophog/queries.pyindex eacb3c3e869d..8a3427ae1ac1 100644--- a/posthog/models/tophog/queries.py+++ b/posthog/models/tophog/queries.py@@ -2,6 +2,7 @@ from datetime import datetimefrom posthog.clickhouse.client import sync_executefrom posthog.clickhouse.query_tagging import Feature, Product, tags_context+from posthog.dataclasses import frozenTOPHOG_QUERY = """WITH filtered AS (@@ -78,14 +79,20 @@ def query_tophog_metrics(]+@frozen+class TopHogFilterOptions:+ pipelines: list[str]+ lanes: list[str]++def query_tophog_filter_options(date_from: datetime,date_to: datetime,-) -> tuple[list[str], list[str]]:+) -> TopHogFilterOptions:params: dict[str, datetime] = {"date_from": date_from, "date_to": date_to}with tags_context(product=Product.INGESTION, feature=Feature.DEBUG_QUERY):rows = sync_execute(FILTER_OPTIONS_QUERY, params)pipelines = sorted({row[0] for row in rows})lanes = sorted({row[1] for row in rows})- return pipelines, lanes+ return TopHogFilterOptions(pipelines=pipelines, lanes=lanes)diff --git a/posthog/models/tophog/test/test_tophog_queries.py b/posthog/models/tophog/test/test_tophog_queries.pyindex b7f2ba4257e4..aacd390813b6 100644--- a/posthog/models/tophog/test/test_tophog_queries.py+++ b/posthog/models/tophog/test/test_tophog_queries.py@@ -143,6 +143,6 @@ class TestTopHogQueries(ClickhouseTestMixin, BaseTest):])- pipelines, lanes = query_tophog_filter_options(DATE_FROM, DATE_TO)- self.assertEqual(pipelines, ["events", "recordings"])- self.assertEqual(lanes, ["fast", "slow"])+ options = query_tophog_filter_options(DATE_FROM, DATE_TO)+ self.assertEqual(options.pipelines, ["events", "recordings"])+ self.assertEqual(options.lanes, ["fast", "slow"])