Beispiel #1
0
    def run(self, filter: Filter, team: Team, *args, **kwargs) -> List[Dict[str, Any]]:
        limit = kwargs.get("limit", SESSIONS_LIST_DEFAULT_LIMIT)
        offset = kwargs.get("offset", 0)

        result: List = []
        if filter.session_type == SESSION_AVG:

            if filter.compare:
                current_response = self.calculate_avg(filter, team)
                parsed_response = convert_to_comparison(current_response, "current", filter)
                result.extend(parsed_response)

                compared_filter = determine_compared_filter(filter)
                compared_result = self.calculate_avg(compared_filter, team)
                compared_res = convert_to_comparison(compared_result, "previous", filter)
                result.extend(compared_res)
            else:
                result = self.calculate_avg(filter, team)

        elif filter.session_type == SESSION_DIST:
            result = self.calculate_dist(filter, team)
        else:
            result = self.calculate_list(filter, team, limit, offset)

        return result
Beispiel #2
0
    def _calculate_trends(self, filter: Filter,
                          team: Team) -> List[Dict[str, Any]]:
        # format default dates

        if not filter._date_from:
            filter._date_from = relative_date_parse("-7d")
        if not filter._date_to:
            filter._date_to = timezone.now()

        result = []
        for entity in filter.entities:
            if filter.compare:
                compare_filter = determine_compared_filter(filter=filter)
                entity_result = self._serialize_entity(entity, filter, team)
                entity_result = convert_to_comparison(
                    entity_result, filter,
                    "{} - {}".format(entity.name, "current"))
                result.extend(entity_result)
                previous_entity_result = self._serialize_entity(
                    entity, compare_filter, team)
                previous_entity_result = convert_to_comparison(
                    previous_entity_result, filter,
                    "{} - {}".format(entity.name, "previous"))
                result.extend(previous_entity_result)
            else:
                entity_result = self._serialize_entity(entity, filter, team)
                result.extend(entity_result)

        return result
Beispiel #3
0
    def run(self, filter: Filter, team: Team, *args,
            **kwargs) -> List[Dict[str, Any]]:
        events = (Event.objects.filter(team=team).filter(
            filter.properties_to_Q(team_id=team.pk)).add_person_id(
                team.pk).order_by("-timestamp"))

        session_type = kwargs.get("session_type", None)
        offset = kwargs.get("offset", 0)

        if not filter.date_to:
            filter._date_to = now().isoformat()
        calculated = []

        # get compared period
        if filter.compare and filter._date_from != "all" and session_type == "avg":
            calculated = self.calculate_sessions(
                events.filter(filter.date_filter_Q), session_type, filter,
                team, offset)
            calculated = self._convert_to_comparison(calculated, "current")

            compare_filter = determine_compared_filter(filter)
            compared_calculated = self.calculate_sessions(
                events.filter(compare_filter.date_filter_Q), session_type,
                compare_filter, team, offset)
            converted_compared_calculated = self._convert_to_comparison(
                compared_calculated, "previous")
            calculated.extend(converted_compared_calculated)
        else:
            # if session_type is None, it's a list of sessions which shouldn't have any date filtering
            if session_type is not None:
                events = events.filter(filter.date_filter_Q)
            calculated = self.calculate_sessions(events, session_type, filter,
                                                 team, offset)

        return calculated
Beispiel #4
0
    def run(self, filter: Filter, team: Team, *args,
            **kwargs) -> List[Dict[str, Any]]:
        calculated = []

        events = Event.objects.none()
        for entity in filter.entities:
            events |= process_entity_for_events(
                entity, team_id=team.pk).filter(
                    filter_events(team_id=team.pk,
                                  filter=filter,
                                  entity=entity,
                                  include_dates=False))

        if not len(events):
            events = self.events_query(filter, team)

        # get compared period
        if filter.compare and filter._date_from != "all" and filter.session == SESSION_AVG:
            calculated = self.calculate_sessions(
                events.filter(filter.date_filter_Q), filter, team)
            calculated = convert_to_comparison(calculated, filter, "current")

            compare_filter = determine_compared_filter(filter)
            compared_calculated = self.calculate_sessions(
                events.filter(compare_filter.date_filter_Q), compare_filter,
                team)
            converted_compared_calculated = convert_to_comparison(
                compared_calculated, filter, "previous")
            calculated.extend(converted_compared_calculated)
        else:
            events = events.filter(filter.date_filter_Q)
            calculated = self.calculate_sessions(events, filter, team)

        return calculated
Beispiel #5
0
    def run(self, filter: Filter, team: Team, *args,
            **kwargs) -> List[Dict[str, Any]]:
        events = (Event.objects.filter(team=team).filter(
            filter.properties_to_Q(team_id=team.pk)).add_person_id(
                team.pk).order_by("-timestamp"))

        limit = int(kwargs.get("limit", SESSIONS_LIST_DEFAULT_LIMIT))
        offset = filter.offset

        calculated = []

        # get compared period
        if filter.compare and filter._date_from != "all" and filter.session_type == SESSION_AVG:

            calculated = self.calculate_sessions(
                events.filter(filter.date_filter_Q), filter, team, limit,
                offset)
            calculated = convert_to_comparison(calculated, filter, "current")

            compare_filter = determine_compared_filter(filter)
            compared_calculated = self.calculate_sessions(
                events.filter(compare_filter.date_filter_Q), compare_filter,
                team, limit, offset)
            converted_compared_calculated = convert_to_comparison(
                compared_calculated, filter, "previous")
            calculated.extend(converted_compared_calculated)
        else:
            # if session_type is None, it's a list of sessions which shouldn't have any date filtering
            if filter.session_type is not None:
                events = events.filter(filter.date_filter_Q)
            calculated = self.calculate_sessions(events, filter, team, limit,
                                                 offset)

        return calculated
Beispiel #6
0
    def test_determine_compared_filter(self):
        from posthog.queries.base import determine_compared_filter

        filter = PathFilter(data={"date_from": "2020-05-22", "date_to": "2020-05-29"})
        compared_filter = determine_compared_filter(filter)

        self.assertIsInstance(compared_filter, PathFilter)
        self.assertDictContainsSubset({"date_from": "2020-05-15", "date_to": "2020-05-22",}, compared_filter.to_dict())
Beispiel #7
0
    def run(self, filter: Filter, team: Team, *args, **kwargs) -> List[Dict[str, Any]]:
        events = self.events_query(filter, team)
        calculated = []

        # get compared period
        if filter.compare and filter._date_from != "all" and filter.session == SESSION_AVG:

            calculated = self.calculate_sessions(events.filter(filter.date_filter_Q), filter, team)
            calculated = convert_to_comparison(calculated, filter, "current")

            compare_filter = determine_compared_filter(filter)
            compared_calculated = self.calculate_sessions(
                events.filter(compare_filter.date_filter_Q), compare_filter, team
            )
            converted_compared_calculated = convert_to_comparison(compared_calculated, filter, "previous")
            calculated.extend(converted_compared_calculated)
        else:
            events = events.filter(filter.date_filter_Q)
            calculated = self.calculate_sessions(events, filter, team)

        return calculated
    def run(self, filter: Filter, team: Team, *args,
            **kwargs) -> List[Dict[str, Any]]:
        result: List = []

        set_default_dates(filter)
        if filter.session == SESSION_AVG:
            if filter.compare:
                current_response = self.calculate_avg(filter, team)
                parsed_response = convert_to_comparison(
                    current_response, filter, "current")
                result.extend(parsed_response)

                compared_filter = determine_compared_filter(filter)
                compared_result = self.calculate_avg(compared_filter, team)
                compared_res = convert_to_comparison(compared_result, filter,
                                                     "previous")
                result.extend(compared_res)
            else:
                result = self.calculate_avg(filter, team)

        elif filter.session == SESSION_DIST:
            result = self.calculate_dist(filter, team)

        return result