From a120caa43a3920e398faa52a26521c4444268c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Oberm=C3=BCller?= Date: Fri, 3 Jan 2025 11:34:35 +0100 Subject: [PATCH] refactor(insights): capture calls to legacy insight calculation endpoints (#27210) --- posthog/api/insight.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/posthog/api/insight.py b/posthog/api/insight.py index 22b53f07e73af..924850d51f07b 100644 --- a/posthog/api/insight.py +++ b/posthog/api/insight.py @@ -1,5 +1,6 @@ import json from functools import lru_cache +import logging from typing import Any, Optional, Union, cast import posthoganalytics @@ -89,6 +90,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 +173,24 @@ def log_and_report_insight_activity( ) +def capture_legacy_api_call(request: request.Request, team: Team): + try: + 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))) + except Exception as e: + logging.exception(f"Error in capture_legacy_api_call: {e}") + pass + + 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 +989,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 +1077,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 +1121,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 +1153,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"):