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
  • Loading branch information
thmsobrmlr committed Jan 2, 2025
1 parent 96da932 commit f8f1254
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))

Check failure on line 186 in posthog/api/insight.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Argument 2 to "groups" has incompatible type "int"; expected "Team | None"


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

0 comments on commit f8f1254

Please sign in to comment.