def _breakdown_person_params(self, aggregate_operation: str, filter: Filter, team_id: int): parsed_date_from, parsed_date_to, _ = parse_timestamps(filter=filter, team_id=team_id) prop_filters, prop_filter_params = parse_prop_clauses( filter.properties, team_id, table_name="e", filter_test_accounts=filter.filter_test_accounts) elements_query = TOP_PERSON_PROPS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), prop_filters=prop_filters, aggregate_operation=aggregate_operation, ) top_elements_array = self._get_top_elements(elements_query, filter, team_id, params=prop_filter_params) params = { "values": top_elements_array, } breakdown_filter = BREAKDOWN_PERSON_PROP_JOIN_SQL breakdown_filter_params = { "latest_person_sql": GET_LATEST_PERSON_SQL.format(query=""), } return params, breakdown_filter, breakdown_filter_params, "value"
def _breakdown_person_params(self, aggregate_operation: str, entity: Entity, filter: Filter, team_id: int): parsed_date_from, parsed_date_to, _ = parse_timestamps(filter=filter, team_id=team_id) prop_filters, prop_filter_params = parse_prop_clauses( filter.properties, team_id, table_name="e", filter_test_accounts=filter.filter_test_accounts) person_prop_filters, person_prop_params = parse_prop_clauses( [prop for prop in filter.properties if prop.type == "person"], team_id, table_name="e", filter_test_accounts=filter.filter_test_accounts, is_person_query=True, ) entity_params, entity_format_params = populate_entity_params(entity) elements_query = TOP_PERSON_PROPS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), prop_filters=prop_filters, person_prop_filters=person_prop_filters, aggregate_operation=aggregate_operation, latest_distinct_id_sql=GET_LATEST_PERSON_DISTINCT_ID_SQL, **entity_format_params) top_elements_array = self._get_top_elements(elements_query, filter, team_id, params={ **prop_filter_params, **person_prop_params, **entity_params }) params = { "values": [*top_elements_array, "none"], } breakdown_filter_params = { "latest_person_sql": GET_LATEST_PERSON_SQL.format(query=""), } return ( params, BREAKDOWN_PERSON_PROP_JOIN_SQL, breakdown_filter_params, "value", None if filter.offset else NONE_BREAKDOWN_PERSON_PROP_JOIN_SQL, )
def get_breakdown_person_prop_values(filter: Filter, entity: Entity, aggregate_operation: str, team_id: int, limit: int = 25): parsed_date_from, parsed_date_to, _ = parse_timestamps(filter=filter, team_id=team_id) prop_filters, prop_filter_params = parse_prop_clauses( filter.properties, team_id, table_name="e", filter_test_accounts=filter.filter_test_accounts, ) person_prop_filters, person_prop_params = parse_prop_clauses( [prop for prop in filter.properties if prop.type == "person"], team_id, filter_test_accounts=filter.filter_test_accounts, is_person_query=True, prepend="person", ) entity_params, entity_format_params = populate_entity_params(entity) elements_query = TOP_PERSON_PROPS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), prop_filters=prop_filters, person_prop_filters=person_prop_filters, aggregate_operation=aggregate_operation, GET_TEAM_PERSON_DISTINCT_IDS=GET_TEAM_PERSON_DISTINCT_IDS, **entity_format_params, ) top_elements_array = _get_top_elements( filter=filter, team_id=team_id, query=elements_query, params={ **prop_filter_params, **person_prop_params, **entity_params }, limit=limit, ) return top_elements_array
def _breakdown_person_params(self, filter: Filter, team_id: int): parsed_date_from, parsed_date_to, _ = parse_timestamps(filter=filter, team_id=team_id) elements_query = TOP_PERSON_PROPS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), ) top_elements_array = self._get_top_elements(elements_query, filter, team_id) params = { "values": top_elements_array, } breakdown_filter = BREAKDOWN_PERSON_PROP_JOIN_SQL breakdown_filter_params = { "latest_person_sql": GET_LATEST_PERSON_SQL.format(query=""), } return params, breakdown_filter, breakdown_filter_params, "value"
def _format_breakdown_query(self, entity: Entity, filter: Filter, team: Team) -> List[Dict[str, Any]]: params = {"team_id": team.pk} interval_annotation = get_interval_annotation_ch(filter.interval) num_intervals, seconds_in_interval = get_time_diff( filter.interval or "day", filter.date_from, filter.date_to) parsed_date_from, parsed_date_to = parse_timestamps(filter=filter) props_to_filter = [*filter.properties, *entity.properties] prop_filters, prop_filter_params = parse_prop_clauses( props_to_filter, team) aggregate_operation, join_condition, math_params = self._process_math( entity) action_query = "" action_params: Dict = {} if entity.type == TREND_FILTER_TYPE_ACTIONS: action = Action.objects.get(pk=entity.id) action_query, action_params = format_action_filter(action) null_sql = NULL_BREAKDOWN_SQL.format( interval=interval_annotation, seconds_in_interval=seconds_in_interval, num_intervals=num_intervals, date_to=((filter.date_to or timezone.now())).strftime("%Y-%m-%d %H:%M:%S"), ) params = {**params, **math_params, **prop_filter_params} top_elements_array = [] if filter.breakdown_type == "cohort": breakdown = filter.breakdown if filter.breakdown and isinstance( filter.breakdown, list) else [] if "all" in breakdown: params = {**params, "event": entity.id, **action_params} null_sql = NULL_SQL.format( interval=interval_annotation, seconds_in_interval=seconds_in_interval, num_intervals=num_intervals, date_to=((filter.date_to or timezone.now())).strftime("%Y-%m-%d %H:%M:%S"), ) conditions = BREAKDOWN_CONDITIONS_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, actions_query="AND {}".format(action_query) if action_query else "", event_filter="AND event = %(event)s" if not action_query else "", filters="{filters}".format( filters=prop_filters) if props_to_filter else "", ) breakdown_query = BREAKDOWN_DEFAULT_SQL.format( null_sql=null_sql, conditions=conditions, event_join=join_condition, aggregate_operation=aggregate_operation, interval_annotation=interval_annotation, ) else: cohort_queries, cohort_ids, cohort_params = self._format_breakdown_cohort_join_query( breakdown, team) params = { **params, "values": cohort_ids, "event": entity.id, **action_params, **cohort_params } breakdown_filter = BREAKDOWN_COHORT_JOIN_SQL.format( cohort_queries=cohort_queries, parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, actions_query="AND {}".format(action_query) if action_query else "", event_filter="AND event = %(event)s" if not action_query else "", filters="{filters}".format( filters=prop_filters) if props_to_filter else "", ) breakdown_query = BREAKDOWN_QUERY_SQL.format( null_sql=null_sql, breakdown_filter=breakdown_filter, event_join=join_condition, aggregate_operation=aggregate_operation, interval_annotation=interval_annotation, ) elif filter.breakdown_type == "person": elements_query = TOP_PERSON_PROPS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), ) top_elements_array = self._get_top_elements( elements_query, filter, team) params = { **params, "values": top_elements_array, "key": filter.breakdown, "event": entity.id, **action_params, } breakdown_filter = BREAKDOWN_PERSON_PROP_JOIN_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, actions_query="AND {}".format(action_query) if action_query else "", event_filter="AND event = %(event)s" if not action_query else "", latest_person_sql=GET_LATEST_PERSON_SQL.format(query=""), ) breakdown_query = BREAKDOWN_QUERY_SQL.format( null_sql=null_sql, breakdown_filter=breakdown_filter, event_join=join_condition, aggregate_operation=aggregate_operation, interval_annotation=interval_annotation, ) else: elements_query = TOP_ELEMENTS_ARRAY_OF_KEY_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to) top_elements_array = self._get_top_elements( elements_query, filter, team) params = { **params, "values": top_elements_array, "key": filter.breakdown, "event": entity.id, **action_params, } breakdown_filter = BREAKDOWN_PROP_JOIN_SQL.format( parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, actions_query="AND {}".format(action_query) if action_query else "", event_filter="AND event = %(event)s" if not action_query else "", filters="{filters}".format( filters=prop_filters) if props_to_filter else "", ) breakdown_query = BREAKDOWN_QUERY_SQL.format( null_sql=null_sql, breakdown_filter=breakdown_filter, event_join=join_condition, aggregate_operation=aggregate_operation, interval_annotation=interval_annotation, ) try: result = sync_execute(breakdown_query, params) except: result = [] parsed_results = [] for idx, stats in enumerate(result): breakdown_value = stats[ 2] if not filter.breakdown_type == "cohort" else "" stripped_value = breakdown_value.strip('"') if isinstance( breakdown_value, str) else breakdown_value extra_label = self._determine_breakdown_label( idx, filter.breakdown_type, filter.breakdown, stripped_value) label = "{} - {}".format(entity.name, extra_label) additional_values = { "label": label, "breakdown_value": filter.breakdown[idx] if isinstance(filter.breakdown, list) else filter.breakdown if filter.breakdown_type == "cohort" else stripped_value, } parsed_result = self._parse_response(stats, filter, additional_values) parsed_results.append(parsed_result) return parsed_results