Skip to content

Commit

Permalink
Add some missing team_id filters (#3372)
Browse files Browse the repository at this point in the history
Tested out a solution for #3085 - result is super noisy but helped me
find some bad queries.
  • Loading branch information
macobo committed Feb 19, 2021
1 parent 8ef54b3 commit 07d6974
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ee/clickhouse/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_elements_chain(self, event):


def determine_event_conditions(
conditions: Dict[str, Union[str, List[str]]], long_date_from: bool = False
team: Team, conditions: Dict[str, Union[str, List[str]]], long_date_from: bool
) -> Tuple[str, Dict]:
result = ""
params: Dict[str, Union[str, List[str]]] = {}
Expand All @@ -182,7 +182,7 @@ def determine_event_conditions(
params.update({"before": timestamp})
elif k == "person_id":
result += """AND distinct_id IN (%(distinct_ids)s)"""
distinct_ids = Person.objects.filter(pk=v)[0].distinct_ids
distinct_ids = Person.objects.filter(pk=v, team_id=team.pk)[0].distinct_ids
distinct_ids = [distinct_id.__str__() for distinct_id in distinct_ids]
params.update({"distinct_ids": distinct_ids})
elif k == "distinct_id":
Expand Down
2 changes: 1 addition & 1 deletion ee/clickhouse/models/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse_prop_clauses(

for idx, prop in enumerate(filters):
if prop.type == "cohort":
cohort = Cohort.objects.get(pk=prop.value)
cohort = Cohort.objects.get(pk=prop.value, team_id=team_id)
person_id_query, cohort_filter_params = format_filter_query(cohort)
params = {**params, **cohort_filter_params}
final.append(
Expand Down
2 changes: 1 addition & 1 deletion ee/clickhouse/views/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _calculate_entity_people(self, team: Team, entity: Entity, filter: Filter):
person_filter_params: Dict[str, Any] = {}

if filter.breakdown_type == "cohort" and filter.breakdown_value != "all":
cohort = Cohort.objects.get(pk=filter.breakdown_value)
cohort = Cohort.objects.get(pk=filter.breakdown_value, team_id=team.pk)
person_filter, person_filter_params = format_filter_query(cohort)
person_filter = "AND distinct_id IN ({})".format(person_filter)
elif (
Expand Down
1 change: 1 addition & 0 deletions ee/clickhouse/views/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _get_people(self, query_result: List[Dict], team: Team) -> Dict[str, Any]:
def _query_events_list(self, filter: Filter, team: Team, request: Request, long_date_from: bool = False) -> List:
limit = "LIMIT 101"
conditions, condition_params = determine_event_conditions(
team,
{
"after": (now() - timedelta(days=1)).isoformat(),
"before": (now() + timedelta(seconds=5)).isoformat(),
Expand Down
1 change: 1 addition & 0 deletions posthog/api/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def _calculate_people(events: QuerySet, offset: int):
Person.objects.filter(
**{
"id": OuterRef("person_id"),
"team_id": self.team.pk,
"properties__{}".format(request.GET["breakdown"]): request.GET["breakdown_value"],
}
).only("id")
Expand Down
8 changes: 4 additions & 4 deletions posthog/api/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def _filter_request(self, request: request.Request, queryset: EventManager) -> Q
elif key == "before":
queryset = queryset.filter(timestamp__lt=request.GET["before"])
elif key == "person_id":
person = Person.objects.get(pk=request.GET["person_id"])
person = Person.objects.get(pk=request.GET["person_id"], team_id=self.team_id)
queryset = queryset.filter(
distinct_id__in=PersonDistinctId.objects.filter(person_id=request.GET["person_id"]).values(
"distinct_id"
)
distinct_id__in=PersonDistinctId.objects.filter(
team_id=self.team_id, person_id=request.GET["person_id"]
).values("distinct_id")
)
elif key == "distinct_id":
queryset = queryset.filter(distinct_id=request.GET["distinct_id"])
Expand Down
7 changes: 6 additions & 1 deletion posthog/models/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class Person(models.Model):
def distinct_ids(self) -> List[str]:
if hasattr(self, "distinct_ids_cache"):
return [id.distinct_id for id in self.distinct_ids_cache] # type: ignore
return [id[0] for id in PersonDistinctId.objects.filter(person=self).order_by("id").values_list("distinct_id")]
return [
id[0]
for id in PersonDistinctId.objects.filter(person=self, team_id=self.team_id)
.order_by("id")
.values_list("distinct_id")
]

def add_distinct_id(self, distinct_id: str) -> None:
PersonDistinctId.objects.create(person=self, distinct_id=distinct_id, team_id=self.team_id)
Expand Down

0 comments on commit 07d6974

Please sign in to comment.