From f8f12548486f68ad950933d7d5c7b1d90c9d9ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obermu=CC=88ller?= Date: Thu, 2 Jan 2025 09:04:59 +0100 Subject: [PATCH] refactor(insights): capture calls to legacy insight calculation endpoints --- posthog/api/insight.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/posthog/api/insight.py b/posthog/api/insight.py index 22b53f07e73af..de522adcdb911 100644 --- a/posthog/api/insight.py +++ b/posthog/api/insight.py @@ -89,6 +89,7 @@ from posthog.models.filters import RetentionFilter from posthog.models.filters.path_filter import PathFilter from posthog.models.filters.stickiness_filter import StickinessFilter +from posthog.models.filters.utils import get_filter from posthog.models.insight import InsightViewed from posthog.models.organization import Organization from posthog.models.team.team import Team @@ -171,6 +172,20 @@ def log_and_report_insight_activity( ) +def capture_legacy_api_call(request: request.Request, team: Team): + event = "legacy insight endpoint called" + distinct_id: str = request.user.distinct_id # type: ignore + properties = { + "path": request._request.path, + "method": request._request.method, + "use_hogql": False, + "filter": get_filter(request=request, team=team), + "was_impersonated": is_impersonated_session(request), + } + + posthoganalytics.capture(distinct_id, event, properties, groups=(groups(team.organization, team.pk))) + + class QuerySchemaParser(JSONParser): """ A query schema parser that only parses the query field and validates it against the schema if it is present @@ -969,6 +984,8 @@ def retrieve(self, request, *args, **kwargs): ) @action(methods=["GET", "POST"], detail=False, required_scopes=["insight:read"]) def trend(self, request: request.Request, *args: Any, **kwargs: Any): + capture_legacy_api_call(request, self.team) + timings = HogQLTimings() try: with timings.measure("calculate"): @@ -1055,6 +1072,8 @@ def calculate_trends(self, request: request.Request) -> dict[str, Any]: ) @action(methods=["GET", "POST"], detail=False, required_scopes=["insight:read"]) def funnel(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: + capture_legacy_api_call(request, self.team) + timings = HogQLTimings() try: with timings.measure("calculate"): @@ -1097,6 +1116,8 @@ def calculate_funnel(self, request: request.Request) -> dict[str, Any]: # ****************************************** @action(methods=["GET", "POST"], detail=False, required_scopes=["insight:read"]) def retention(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: + capture_legacy_api_call(request, self.team) + timings = HogQLTimings() try: with timings.measure("calculate"): @@ -1127,6 +1148,8 @@ def calculate_retention(self, request: request.Request) -> dict[str, Any]: # ****************************************** @action(methods=["GET", "POST"], detail=False, required_scopes=["insight:read"]) def path(self, request: request.Request, *args: Any, **kwargs: Any) -> Response: + capture_legacy_api_call(request, self.team) + timings = HogQLTimings() try: with timings.measure("calculate"):