Ejemplo n.º 1
0
    def session(self, request: Request, *args: Any, **kwargs: Any) -> Response:

        team = request.user.team
        filter = Filter(request=request)

        limit = int(request.GET.get("limit", SESSIONS_LIST_DEFAULT_LIMIT))
        offset = int(request.GET.get("offset", 0))

        response = ClickhouseSessions().run(team=team,
                                            filter=filter,
                                            limit=limit + 1,
                                            offset=offset)

        if "distinct_id" in request.GET and request.GET["distinct_id"]:
            try:
                person_ids = get_persons_by_distinct_ids(
                    team.pk, [request.GET["distinct_id"]])[0].distinct_ids
                response = [
                    session for i, session in enumerate(response)
                    if response[i]["distinct_id"] in person_ids
                ]
            except IndexError:
                response = []

        if len(response) > limit:
            response.pop()
            return Response({"result": response, "offset": offset + limit})
        else:
            return Response({
                "result": response,
            })
Ejemplo n.º 2
0
    def fetch_distinct_ids(
        self,
        action_filters: ActionFiltersSQL,
        date_from: str,
        date_to: str,
        date_params: Dict[str, Any],
        limit: int,
        distinct_id_offset: int,
    ) -> List[str]:
        if self.filter.distinct_id:
            persons = get_persons_by_distinct_ids(self.team.pk,
                                                  [self.filter.distinct_id])
            return persons[0].distinct_ids if len(persons) > 0 else []

        person_filters, person_filter_params = parse_prop_clauses(
            self.filter.person_filter_properties,
            self.team.pk,
            allow_denormalized_props=False)
        return sync_execute(
            SESSIONS_DISTINCT_ID_SQL.format(
                date_from=date_from,
                date_to=date_to,
                person_filters=person_filters,
                action_filters=action_filters.matches_any_clause,
            ),
            {
                **person_filter_params,
                **action_filters.params,
                "team_id": self.team.pk,
                "distinct_id_limit": distinct_id_offset + limit,
                **date_params,
            },
        )
Ejemplo n.º 3
0
 def _get_people(self, query_result: List[Dict], team: Team) -> Dict[str, Any]:
     distinct_ids = [event[5] for event in query_result]
     persons = get_persons_by_distinct_ids(team.pk, distinct_ids)
     distinct_to_person: Dict[str, Person] = {}
     for person in persons:
         for distinct_id in person.distinct_ids:
             distinct_to_person[distinct_id] = person
     return distinct_to_person
Ejemplo n.º 4
0
 def _get_people(self, query_result: List[Dict], team: Team) -> Dict[str, Any]:
     distinct_ids = [event[5] for event in query_result]
     persons = get_persons_by_distinct_ids(team.pk, distinct_ids)
     persons = persons.prefetch_related(Prefetch("persondistinctid_set", to_attr="distinct_ids_cache"))
     distinct_to_person: Dict[str, Person] = {}
     for person in persons:
         for distinct_id in person.distinct_ids:
             distinct_to_person[distinct_id] = person
     return distinct_to_person
Ejemplo n.º 5
0
    def fetch_distinct_ids(self, date_from: str, date_to: str, limit: int, distinct_id_offset: int) -> List[str]:
        if self.filter.distinct_id:
            persons = get_persons_by_distinct_ids(self.team.pk, [self.filter.distinct_id])
            return persons[0].distinct_ids if len(persons) > 0 else []

        person_filters, person_filter_params = parse_prop_clauses(self.filter.person_filter_properties, self.team.pk)
        return sync_execute(
            SESSIONS_DISTINCT_ID_SQL.format(date_from=date_from, date_to=date_to, person_filters=person_filters),
            {**person_filter_params, "team_id": self.team.pk, "distinct_id_limit": distinct_id_offset + limit,},
        )
Ejemplo n.º 6
0
    def sessions(self, request: Request, *args: Any, **kwargs: Any) -> Response:
        filter = SessionsFilter(request=request)

        sessions, pagination = ClickhouseSessionsList().run(team=self.team, filter=filter)

        if filter.distinct_id:
            try:
                person_ids = get_persons_by_distinct_ids(self.team.pk, [filter.distinct_id])[0].distinct_ids
                sessions = [session for i, session in enumerate(sessions) if session["distinct_id"] in person_ids]
            except IndexError:
                sessions = []

        return Response({"result": sessions, "pagination": pagination})
Ejemplo n.º 7
0
    def _add_person_properties(self, team=Team, sessions=List[Tuple]):
        distinct_id_hash = {}
        for session in sessions:
            distinct_id_hash[session["distinct_id"]] = True
        distinct_ids = list(distinct_id_hash.keys())

        if len(distinct_ids) == 0:
            return

        persons = get_persons_by_distinct_ids(team.pk, distinct_ids)

        distinct_to_person: Dict[str, Dict[str, Any]] = {}
        for person in persons:
            for distinct_id in person["distinct_ids"]:
                distinct_to_person[distinct_id] = person

        for session in sessions:
            if distinct_to_person.get(session["distinct_id"], None):
                session["properties"] = distinct_to_person[session["distinct_id"]]["properties"]
Ejemplo n.º 8
0
    def _add_person_properties(self, sessions: List[Session]):
        distinct_id_hash = {}
        for session in sessions:
            distinct_id_hash[session["distinct_id"]] = True
        distinct_ids = list(distinct_id_hash.keys())

        if len(distinct_ids) == 0:
            return

        persons = get_persons_by_distinct_ids(self.team.pk, distinct_ids)

        distinct_to_person: Dict[str, Person] = {}
        for person in persons:
            for distinct_id in person.distinct_ids:
                distinct_to_person[distinct_id] = person

        for session in sessions:
            if distinct_to_person.get(session["distinct_id"], None):
                session["email"] = distinct_to_person[session["distinct_id"]].properties.get("email")