def _serialize_lifecycle(self, entity: Entity, filter: Filter, team_id: int) -> List[Dict[str, Any]]: period = filter.interval or "day" num_intervals, prev_date_from, date_from, date_to, after_date_to = get_time_diff( period, filter.date_from, filter.date_to, team_id) interval_trunc, sub_interval = get_trunc_func(period=period) # include the before and after when filteirng all events filter = filter.with_data({ "date_from": prev_date_from.isoformat(), "date_to": after_date_to.isoformat() }) filtered_events = (Event.objects.filter( team_id=team_id).add_person_id(team_id).filter( filter_events(team_id, filter, entity))) event_query, event_params = queryset_to_named_query( filtered_events, "events") earliest_events_filtered = (Event.objects.filter( team_id=team_id).add_person_id(team_id).filter( filter_events(team_id, filter, entity, include_dates=False))) earliest_events_query, earliest_events_params = queryset_to_named_query( earliest_events_filtered, "earliest_events") with connection.cursor() as cursor: cursor.execute( LIFECYCLE_SQL.format( action_join=ACTION_JOIN if entity.type == TREND_FILTER_TYPE_ACTIONS else "", event_condition="{} = %(event)s".format( "action_id" if entity.type == TREND_FILTER_TYPE_ACTIONS else "event"), events=event_query, earliest_events=earliest_events_query, ), { "team_id": team_id, "event": entity.id, "interval": interval_trunc, "one_interval": "1 " + interval_trunc, "sub_interval": "1 " + sub_interval, "num_intervals": num_intervals, "prev_date_from": prev_date_from, "date_from": date_from, "date_to": date_to, "after_date_to": after_date_to, **event_params, **earliest_events_params, }, ) res = [] for val in cursor.fetchall(): label = "{} - {}".format(entity.name, val[2]) additional_values = {"label": label, "status": val[2]} parsed_result = parse_response(val, filter, additional_values) res.append(parsed_result) return res
def get_people( self, filter: Filter, team_id: int, target_date: datetime, lifecycle_type: str, limit: int = 100, ): entity = filter.entities[0] period = filter.interval or "day" num_intervals, prev_date_from, date_from, date_to, after_date_to = get_time_diff( period, filter.date_from, filter.date_to, team_id) interval_trunc, sub_interval = get_trunc_func(period=period) # include the before and after when filteirng all events filter = Filter( data={ **filter._data, "date_from": prev_date_from.isoformat(), "date_to": after_date_to.isoformat() }) filtered_events = Event.objects.filter(team_id=team_id).filter( filter_events(team_id, filter, entity)) event_query, event_params = queryset_to_named_query(filtered_events) earliest_events_filtered = Event.objects.filter( team_id=team_id).filter( filter_events(team_id, filter, entity, include_dates=False)) earliest_events_query, earliest_events_params = queryset_to_named_query( earliest_events_filtered, "earliest_events") with connection.cursor() as cursor: cursor.execute( LIFECYCLE_PEOPLE_SQL.format( action_join=ACTION_JOIN if entity.type == TREND_FILTER_TYPE_ACTIONS else "", event_condition="{} = %(event)s".format( "action_id" if entity.type == TREND_FILTER_TYPE_ACTIONS else "event"), events=event_query, earliest_events=earliest_events_query, ), { "team_id": team_id, "event": entity.id, "interval": interval_trunc, "one_interval": "1 " + interval_trunc, "sub_interval": "1 " + sub_interval, "num_intervals": num_intervals, "prev_date_from": prev_date_from, "date_from": date_from, "date_to": date_to, "after_date_to": after_date_to, "target_date": target_date, "status": lifecycle_type, "offset": filter.offset, "limit": limit, **event_params, **earliest_events_params, }, ) pids = cursor.fetchall() people = Person.objects.filter( team_id=team_id, id__in=[p[0] for p in pids], ) people = people.prefetch_related( Prefetch("persondistinctid_set", to_attr="distinct_ids_cache")) from posthog.api.person import PersonSerializer return PersonSerializer(people, many=True).data