Skip to content

Commit

Permalink
refactor(insights): capture calls to legacy insight calculation endpo…
Browse files Browse the repository at this point in the history
…ints (#27210)
  • Loading branch information
thmsobrmlr authored Jan 3, 2025
1 parent 2f05e8c commit a120caa
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from functools import lru_cache
import logging
from typing import Any, Optional, Union, cast

import posthoganalytics
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit a120caa

Please sign in to comment.