def stickiness(self, entity: Entity, filter: StickinessFilter, team_id: int) -> Dict[str, Any]: 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) trunc_func = get_trunc_func_ch(filter.interval) params: Dict = {"team_id": team_id} params = {**params, **prop_filter_params, "num_intervals": filter.num_intervals} if entity.type == TREND_FILTER_TYPE_ACTIONS: action = Action.objects.get(pk=entity.id) action_query, action_params = format_action_filter(action) if action_query == "": return {} params = {**params, **action_params} content_sql = STICKINESS_ACTIONS_SQL.format( team_id=team_id, actions_query=action_query, parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, filters=prop_filters, trunc_func=trunc_func, ) else: content_sql = STICKINESS_SQL.format( team_id=team_id, event=entity.id, parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, filters=prop_filters, trunc_func=trunc_func, ) counts = sync_execute(content_sql, params) return self.process_result(counts, filter)
def stickiness(self, entity: Entity, filter: Filter, team_id: int) -> Dict[str, Any]: if not filter.date_to or not filter.date_from: raise ValueError("_stickiness needs date_to and date_from set") range_days = (filter.date_to - filter.date_from).days + 2 parsed_date_from, parsed_date_to = parse_timestamps(filter=filter) prop_filters, prop_filter_params = parse_prop_clauses(filter.properties, team_id) params: Dict = {"team_id": team_id} params = {**params, **prop_filter_params} if entity.type == TREND_FILTER_TYPE_ACTIONS: action = Action.objects.get(pk=entity.id) action_query, action_params = format_action_filter(action) if action_query == "": return {} params = {**params, **action_params} content_sql = STICKINESS_ACTIONS_SQL.format( team_id=team_id, actions_query=action_query, parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, filters=prop_filters, ) else: content_sql = STICKINESS_SQL.format( team_id=team_id, event=entity.id, parsed_date_from=parsed_date_from, parsed_date_to=parsed_date_to, filters=prop_filters, ) counts = sync_execute(content_sql, params) return self.process_result(counts, range_days)
def _format_stickiness_query(self, entity: Entity, filter: Filter, team: Team) -> Optional[Dict[str, Any]]: if not filter.date_to or not filter.date_from: raise ValueError("_stickiness needs date_to and date_from set") range_days = (filter.date_to - filter.date_from).days + 2 parsed_date_from, parsed_date_to = parse_timestamps(filter=filter) prop_filters, prop_filter_params = parse_prop_clauses( filter.properties, team) params: Dict = {"team_id": team.pk} params = {**params, **prop_filter_params} if entity.type == TREND_FILTER_TYPE_ACTIONS: action = Action.objects.get(pk=entity.id) action_query, action_params = format_action_filter(action) if action_query == "": return None params = {**params, **action_params} content_sql = STICKINESS_ACTIONS_SQL.format( team_id=team.pk, actions_query=action_query, parsed_date_from=(parsed_date_from or ""), parsed_date_to=(parsed_date_to or ""), filters="{filters}".format( filters=prop_filters) if filter.properties else "", ) else: content_sql = STICKINESS_SQL.format( team_id=team.pk, event=entity.id, parsed_date_from=(parsed_date_from or ""), parsed_date_to=(parsed_date_to or ""), filters="{filters}".format( filters=prop_filters) if filter.properties else "", ) aggregated_counts = sync_execute(content_sql, params) response: Dict[int, int] = {} for result in aggregated_counts: response[result[1]] = result[0] labels = [] data = [] for day in range(1, range_days): label = "{} day{}".format(day, "s" if day > 1 else "") labels.append(label) data.append(response[day] if day in response else 0) return { "labels": labels, "days": [day for day in range(1, range_days)], "data": data, "count": sum(data), }