示例#1
0
    def test_person_query_does_not_include_recording_events_if_flag_not_set(
            self):
        _create_person(team_id=self.team.pk,
                       distinct_ids=["u1"],
                       properties={"email": "bla"})
        _create_event(event="pageview",
                      distinct_id="u1",
                      team=self.team,
                      timestamp=timezone.now())

        event = {
            "id": "pageview",
            "name": "pageview",
            "type": "events",
            "order": 0,
        }
        filter = Filter(
            data={
                "date_from": "2021-01-21T00:00:00Z",
                "date_to": "2021-01-22T00:00:00Z",
                "events": [event],
            })
        entity = Entity(event)
        _, serialized_actors = ClickhouseTrendsActors(self.team, entity,
                                                      filter).get_actors()

        self.assertEqual(serialized_actors[0].get("matched_recordings"), None)
示例#2
0
    def people(self, request: request.Request, *args: Any,
               **kwargs: Any) -> Response:
        team = self.team
        filter = Filter(request=request, team=self.team)
        entity = get_target_entity(filter)

        actors, serialized_actors = ClickhouseTrendsActors(
            team, entity, filter).get_actors()

        current_url = request.get_full_path()
        next_url: Optional[str] = request.get_full_path()
        offset = filter.offset
        if len(actors) > 100 and next_url:
            if "offset" in next_url:
                next_url = next_url[1:]
                next_url = next_url.replace("offset=" + str(offset),
                                            "offset=" + str(offset + 100))
            else:
                next_url = request.build_absolute_uri("{}{}offset={}".format(
                    next_url, "&" if "?" in next_url else "?", offset + 100))
        else:
            next_url = None

        if request.accepted_renderer.format == "csv":
            csvrenderers.CSVRenderer.header = [
                "Distinct ID", "Internal ID", "Email", "Name", "Properties"
            ]
            content = [{
                "Name":
                get_person_name(person),
                "Distinct ID":
                person.distinct_ids[0] if person.distinct_ids else "",
                "Internal ID":
                str(person.uuid),
                "Email":
                person.properties.get("email"),
                "Properties":
                person.properties,
            } for person in actors if isinstance(person, Person)]
            return Response(content)

        return Response({
            "results": [{
                "people": serialized_actors[0:100],
                "count": len(serialized_actors[0:100])
            }],
            "next":
            next_url,
            "previous":
            current_url[1:],
        })
示例#3
0
文件: cohort.py 项目: PostHog/posthog
def insert_cohort_actors_into_ch(cohort: Cohort, filter_data: Dict):
    insight_type = filter_data.get("insight")
    query_builder: ActorBaseQuery

    if insight_type == INSIGHT_TRENDS:
        filter = Filter(data=filter_data, team=cohort.team)
        entity = get_target_entity(filter)
        query_builder = ClickhouseTrendsActors(cohort.team, entity, filter)
    elif insight_type == INSIGHT_STICKINESS:
        stickiness_filter = StickinessFilter(data=filter_data,
                                             team=cohort.team)
        entity = get_target_entity(stickiness_filter)
        query_builder = ClickhouseStickinessActors(cohort.team, entity,
                                                   stickiness_filter)
    elif insight_type == INSIGHT_FUNNELS:
        funnel_filter = Filter(data=filter_data, team=cohort.team)
        if funnel_filter.correlation_person_entity:
            query_builder = FunnelCorrelationActors(filter=funnel_filter,
                                                    team=cohort.team)
        else:
            funnel_actor_class = get_funnel_actor_class(funnel_filter)
            query_builder = funnel_actor_class(filter=funnel_filter,
                                               team=cohort.team)
    elif insight_type == INSIGHT_PATHS:
        path_filter = PathFilter(data=filter_data, team=cohort.team)
        query_builder = ClickhousePathsActors(path_filter,
                                              cohort.team,
                                              funnel_filter=None)
    else:
        if settings.DEBUG:
            raise ValueError(
                f"Insight type: {insight_type} not supported for cohort creation"
            )
        else:
            capture_exception(
                Exception(
                    f"Insight type: {insight_type} not supported for cohort creation"
                ))

    if query_builder.is_aggregating_by_groups:
        if settings.DEBUG:
            raise ValueError(
                f"Query type: Group based queries are not supported for cohort creation"
            )
        else:
            capture_exception(
                Exception(
                    f"Query type: Group based queries are not supported for cohort creation"
                ))
    else:
        query, params = query_builder.actor_query(limit_actors=False)

    insert_actors_into_cohort_by_query(cohort, query, params)
示例#4
0
    def test_person_query_includes_recording_events(self):
        _create_person(team_id=self.team.pk,
                       distinct_ids=["u1"],
                       properties={"email": "bla"})
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now())  # No $session_id, so not included
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now(),
            properties={
                "$session_id": "s2",
                "$window_id": "w2"
            },
        )  # No associated recording, so not included
        _create_session_recording_event(
            self.team.pk,
            "u1",
            "s1",
            timestamp=timezone.now(),
        )
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now() + relativedelta(hours=2),
            properties={
                "$session_id": "s1",
                "$window_id": "w1"
            },
            uuid="b06e5a5e-e001-4293-af81-ac73e194569d",
        )
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now() + relativedelta(hours=3),
            properties={
                "$session_id": "s1",
                "$window_id": "w1"
            },
            uuid="206e5a5e-e001-4293-af81-ac73e194569d",
        )
        event = {
            "id": "pageview",
            "name": "pageview",
            "type": "events",
            "order": 0,
        }
        filter = Filter(
            data={
                "date_from": "2021-01-21T00:00:00Z",
                "date_to": "2021-01-22T00:00:00Z",
                "events": [event],
                "include_recordings": "true",
            })
        entity = Entity(event)

        _, serialized_actors = ClickhouseTrendsActors(self.team, entity,
                                                      filter).get_actors()
        self.assertEqual(len(serialized_actors), 1)
        self.assertEqual(len(serialized_actors[0]["matched_recordings"]), 1)
        self.assertEqual(
            serialized_actors[0]["matched_recordings"][0]["session_id"], "s1")
        self.assertCountEqual(
            serialized_actors[0]["matched_recordings"][0]["events"],
            [
                {
                    "window_id": "w1",
                    "timestamp": timezone.now() + relativedelta(hours=3),
                    "uuid": UUID("206e5a5e-e001-4293-af81-ac73e194569d"),
                },
                {
                    "window_id": "w1",
                    "timestamp": timezone.now() + relativedelta(hours=2),
                    "uuid": UUID("b06e5a5e-e001-4293-af81-ac73e194569d"),
                },
            ],
        )
示例#5
0
    def test_group_query_includes_recording_events(self):
        GroupTypeMapping.objects.create(team=self.team,
                                        group_type="organization",
                                        group_type_index=0)
        create_group(team_id=self.team.pk,
                     group_type_index=0,
                     group_key="bla",
                     properties={})
        _create_session_recording_event(
            self.team.pk,
            "u1",
            "s1",
            timestamp=timezone.now(),
        )
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now(),
            properties={"$group_0": "bla"},
        )
        _create_event(
            event="pageview",
            distinct_id="u1",
            team=self.team,
            timestamp=timezone.now() + relativedelta(hours=2),
            properties={
                "$session_id": "s1",
                "$window_id": "w1",
                "$group_0": "bla"
            },
            uuid="b06e5a5e-e001-4293-af81-ac73e194569d",
        )

        event = {
            "id": "pageview",
            "name": "pageview",
            "type": "events",
            "order": 0,
            "math": "unique_group",
            "math_group_type_index": 0,
        }

        filter = Filter(
            data={
                "date_from": "2021-01-21T00:00:00Z",
                "date_to": "2021-01-22T00:00:00Z",
                "events": [event],
                "include_recordings": "true",
            })
        entity = Entity(event)

        _, serialized_actors = ClickhouseTrendsActors(self.team, entity,
                                                      filter).get_actors()

        self.assertCountEqual(
            serialized_actors[0].get("matched_recordings", []),
            [{
                "session_id":
                "s1",
                "events":
                [{
                    "window_id": "w1",
                    "timestamp": timezone.now() + relativedelta(hours=2),
                    "uuid": UUID("b06e5a5e-e001-4293-af81-ac73e194569d"),
                }],
            }],
        )