0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 12:21:02 +01:00
posthog/ee/clickhouse/views/insights.py
Eric Duong 9514e290a2
Static cohort on person modal (#2952)
* Upload static cohort using CSV, closes #2868

* fix tests

* Fix tests

* Fix e2e test

* Avoid double inserts

* Speed up query

* Move to params

* fix tests

* initial cleanup

* initial scaffold for cohort static logic

* intiial frontend changes

* initial working

* fix error on iterable

* stickiness people func refactor

* Use JSON instead of protobuf

* stickiness working

* clickhouse route

* unused import

* move button

* make interface easier to user

* clickhouse support

* restore cache decorator

* endpoint adjustment

* adjust how clickhouse handles inserting for static cohorts

* split out processing into pg

* use worker for clickhouse querying too

* add pg test

* ee test

* add input for cohort naming

* add stickiness test

* condition for when to show save cohort

* fix types

* minor fix

* remove repeats

* remove hardcoded id

* add date handling to trends cohort func

* remove print

* add link on success creation

* cohort working

* fix backend test

* fix cohort typescript

* remove unnecessary kafka topic

* remove brackets

* shift default

* raise exceptions when debug or test is true

* remove test flag

* move trends insight related components to different folder

* change get_earliest_timestamp

* remove extraneous condition

* fix test call

* put behind ff

* fix test x2

* fix tests

* restore exceptions hog

* fix tests

* restore exceptions hog

* fix test x20

Co-authored-by: Tim Glaser <tim@glsr.nl>
2021-02-24 09:59:13 -05:00

77 lines
3.2 KiB
Python

from typing import Any, Dict, List
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from ee.clickhouse.queries.clickhouse_funnel import ClickhouseFunnel
from ee.clickhouse.queries.clickhouse_paths import ClickhousePaths
from ee.clickhouse.queries.clickhouse_retention import ClickhouseRetention
from ee.clickhouse.queries.clickhouse_stickiness import ClickhouseStickiness
from ee.clickhouse.queries.sessions.clickhouse_sessions import ClickhouseSessions
from ee.clickhouse.queries.trends.clickhouse_trends import ClickhouseTrends
from ee.clickhouse.queries.util import get_earliest_timestamp
from posthog.api.insight import InsightViewSet
from posthog.constants import INSIGHT_FUNNELS, INSIGHT_PATHS, INSIGHT_SESSIONS, TRENDS_STICKINESS
from posthog.decorators import cached_function
from posthog.models import Event
from posthog.models.filters import Filter
from posthog.models.filters.path_filter import PathFilter
from posthog.models.filters.retention_filter import RetentionFilter
from posthog.models.filters.sessions_filter import SessionsFilter
from posthog.models.filters.stickiness_filter import StickinessFilter
class ClickhouseInsightsViewSet(InsightViewSet):
@cached_function()
def calculate_trends(self, request: Request) -> Dict[str, Any]:
team = self.team
filter = Filter(request=request)
if filter.shown_as == TRENDS_STICKINESS:
stickiness_filter = StickinessFilter(
request=request, team=team, get_earliest_timestamp=get_earliest_timestamp
)
result = ClickhouseStickiness().run(stickiness_filter, team)
else:
result = ClickhouseTrends().run(filter, team)
self._refresh_dashboard(request=request)
return {"result": result}
@cached_function()
def calculate_session(self, request: Request) -> Dict[str, Any]:
return {
"result": ClickhouseSessions().run(
team=self.team, filter=SessionsFilter(request=request, data={"insight": INSIGHT_SESSIONS})
)
}
@cached_function()
def calculate_path(self, request: Request) -> Dict[str, Any]:
team = self.team
filter = PathFilter(request=request, data={"insight": INSIGHT_PATHS})
resp = ClickhousePaths().run(filter=filter, team=team)
return {"result": resp}
@action(methods=["GET"], detail=False)
def funnel(self, request: Request, *args: Any, **kwargs: Any) -> Response:
response = self.calculate_funnel(request)
return Response(response)
@cached_function()
def calculate_funnel(self, request: Request) -> Dict[str, Any]:
team = self.team
filter = Filter(request=request, data={"insight": INSIGHT_FUNNELS})
return {"result": ClickhouseFunnel(team=team, filter=filter).run()}
@cached_function()
def calculate_retention(self, request: Request) -> Dict[str, Any]:
team = self.team
data = {}
if not request.GET.get("date_from"):
data.update({"date_from": "-11d"})
filter = RetentionFilter(data=data, request=request)
result = ClickhouseRetention().run(filter, team)
return {"result": result}