Skip to content

Commit

Permalink
chore(environments): Update filtering of product analytics resources (#…
Browse files Browse the repository at this point in the history
…26675)

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Twixes and github-actions[bot] authored Dec 9, 2024
1 parent 2f8cda0 commit 188fc25
Show file tree
Hide file tree
Showing 37 changed files with 242 additions and 413 deletions.
9 changes: 0 additions & 9 deletions mypy-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ posthog/hogql/parser.py:0: error: Item "None" of "list[Expr] | None" has no attr
posthog/hogql/parser.py:0: error: "None" has no attribute "text" [attr-defined]
posthog/hogql/parser.py:0: error: "None" has no attribute "text" [attr-defined]
posthog/hogql/parser.py:0: error: Statement is unreachable [unreachable]
posthog/hogql/functions/cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/functions/cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/database/schema/persons_pdi.py:0: error: Incompatible types in assignment (expression has type "Organization | None", variable has type "Organization") [assignment]
posthog/hogql/database/schema/groups.py:0: error: Incompatible types in assignment (expression has type "dict[str, DatabaseField]", variable has type "dict[str, FieldOrTable]") [assignment]
posthog/hogql/database/schema/groups.py:0: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
Expand Down Expand Up @@ -157,13 +155,9 @@ posthog/hogql/transforms/lazy_tables.py:0: error: Incompatible types in assignme
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible default for argument "context" (default has type "None", argument has type "HogQLContext") [assignment]
posthog/hogql/transforms/in_cohort.py:0: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
posthog/hogql/transforms/in_cohort.py:0: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible default for argument "context" (default has type "None", argument has type "HogQLContext") [assignment]
posthog/hogql/transforms/in_cohort.py:0: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
posthog/hogql/transforms/in_cohort.py:0: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/transforms/in_cohort.py:0: error: Incompatible type for lookup 'team_id': (got "int | None", expected "str | int") [misc]
posthog/hogql/transforms/in_cohort.py:0: error: Argument "table" to "JoinExpr" has incompatible type "Expr"; expected "SelectQuery | SelectSetQuery | Field | None" [arg-type]
posthog/hogql/transforms/in_cohort.py:0: error: List item 0 has incompatible type "SelectQueryType | None"; expected "SelectQueryType" [list-item]
posthog/hogql/transforms/in_cohort.py:0: error: Item "None" of "JoinConstraint | None" has no attribute "expr" [union-attr]
Expand Down Expand Up @@ -603,9 +597,6 @@ posthog/hogql/test/test_parser_python.py:0: error: Unsupported dynamic base clas
posthog/hogql/test/test_parser_cpp.py:0: error: Unsupported dynamic base class "parser_test_factory" [misc]
posthog/hogql/test/test_parse_string_python.py:0: error: Unsupported dynamic base class "parse_string_test_factory" [misc]
posthog/hogql/test/test_parse_string_cpp.py:0: error: Unsupported dynamic base class "parse_string_test_factory" [misc]
posthog/hogql/functions/test/test_cohort.py:0: error: "TestCohort" has no attribute "snapshot" [attr-defined]
posthog/hogql/functions/test/test_cohort.py:0: error: "TestCohort" has no attribute "snapshot" [attr-defined]
posthog/hogql/functions/test/test_cohort.py:0: error: "TestCohort" has no attribute "snapshot" [attr-defined]
posthog/hogql/database/test/test_view.py:0: error: Argument "dialect" to "print_ast" has incompatible type "str"; expected "Literal['hogql', 'clickhouse']" [arg-type]
posthog/hogql/database/test/test_s3_table.py:0: error: Argument "dialect" to "print_ast" has incompatible type "str"; expected "Literal['hogql', 'clickhouse']" [arg-type]
posthog/helpers/full_text_search.py:0: error: Incompatible return value type (got "SearchVector", expected "CombinedExpression") [return-value]
Expand Down
22 changes: 17 additions & 5 deletions posthog/api/activity_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,30 @@ def important_changes(self, request: Request, *args: Any, **kwargs: Any) -> Resp
with timer("gather_query_parts"):
# first things this user created
my_insights = list(
Insight.objects.filter(created_by=user, team_id=self.team.pk).values_list("id", flat=True)
Insight.objects.filter(created_by=user, team__project_id=self.team.project_id).values_list(
"id", flat=True
)
)
my_feature_flags = list(
FeatureFlag.objects.filter(created_by=user, team_id=self.team.pk).values_list("id", flat=True)
FeatureFlag.objects.filter(created_by=user, team__project_id=self.team.project_id).values_list(
"id", flat=True
)
)
my_notebooks = list(
Notebook.objects.filter(created_by=user, team_id=self.team.pk).values_list("short_id", flat=True)
Notebook.objects.filter(created_by=user, team__project_id=self.team.project_id).values_list(
"short_id", flat=True
)
)
my_comments = list(
Comment.objects.filter(created_by=user, team_id=self.team.pk).values_list("id", flat=True)
Comment.objects.filter(created_by=user, team__project_id=self.team.project_id).values_list(
"id", flat=True
)
)
my_cohorts = list(
Cohort.objects.filter(created_by=user, team__project_id=self.team.project_id).values_list(
"id", flat=True
)
)
my_cohorts = list(Cohort.objects.filter(created_by=user, team_id=self.team.pk).values_list("id", flat=True))
my_hog_functions = list(
HogFunction.objects.filter(created_by=user, team_id=self.team.pk).values_list("id", flat=True)
)
Expand Down
12 changes: 6 additions & 6 deletions posthog/api/cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def activity(self, request: request.Request, **kwargs):
page = int(request.query_params.get("page", "1"))

item_id = kwargs["pk"]
if not Cohort.objects.filter(id=item_id, team_id=self.team_id).exists():
if not Cohort.objects.filter(id=item_id, team__project_id=self.project_id).exists():
return Response("", status=status.HTTP_404_NOT_FOUND)

activity_page = load_activity(
Expand Down Expand Up @@ -613,17 +613,17 @@ def insert_actors_into_cohort_by_query(

def get_cohort_actors_for_feature_flag(cohort_id: int, flag: str, team_id: int, batchsize: int = 1_000):
# :TODO: Find a way to incorporate this into the same code path as feature flag evaluation
team: Team = Team.objects.get(pk=team_id)
project_id = Team.objects.only("project_id").get(pk=team_id).project_id
try:
feature_flag = FeatureFlag.objects.get(team__project_id=team.project_id, key=flag)
feature_flag = FeatureFlag.objects.get(team__project_id=project_id, key=flag)
except FeatureFlag.DoesNotExist:
return []

if not feature_flag.active or feature_flag.deleted or feature_flag.aggregation_group_type_index is not None:
return []

cohort = Cohort.objects.get(pk=cohort_id, team__project_id=team.project_id)
matcher_cache = FlagsMatcherCache(team_id)
cohort = Cohort.objects.get(pk=cohort_id, team__project_id=project_id)
matcher_cache = FlagsMatcherCache(team_id=team_id)
uuids_to_add_to_cohort = []
cohorts_cache: dict[int, CohortOrEmpty] = {}

Expand All @@ -632,7 +632,7 @@ def get_cohort_actors_for_feature_flag(cohort_id: int, flag: str, team_id: int,
# because this is currently a lot more inefficient for flag matching,
# as we're required to go to the database for each person.
cohorts_cache = {
cohort.pk: cohort for cohort in Cohort.objects.filter(team__project_id=team.project_id, deleted=False)
cohort.pk: cohort for cohort in Cohort.objects.filter(team__project_id=project_id, deleted=False)
}

default_person_properties = {}
Expand Down
4 changes: 3 additions & 1 deletion posthog/api/dashboards/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ def create(self, validated_data: dict, *args: Any, **kwargs: Any) -> Dashboard:

elif use_dashboard:
try:
existing_dashboard = Dashboard.objects.get(id=use_dashboard, team_id=team_id)
existing_dashboard = Dashboard.objects.get(
id=use_dashboard, team__project_id=self.context["get_team"]().project_id
)
existing_tiles = (
DashboardTile.objects.filter(dashboard=existing_dashboard)
.exclude(deleted=True)
Expand Down
6 changes: 4 additions & 2 deletions posthog/api/feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ def properties_all_match(predicate):

if prop.type == "cohort":
try:
initial_cohort: Cohort = Cohort.objects.get(pk=prop.value, team_id=self.context["team_id"])
initial_cohort: Cohort = Cohort.objects.get(
pk=prop.value, team__project_id=self.context["project_id"]
)
dependent_cohorts = get_dependent_cohorts(initial_cohort)
for cohort in [initial_cohort, *dependent_cohorts]:
if [prop for prop in cohort.properties.flat if prop.type == "behavioral"]:
Expand Down Expand Up @@ -684,7 +686,7 @@ def local_evaluation(self, request: request.Request, **kwargs):
seen_cohorts_cache = {
cohort.pk: cohort
for cohort in Cohort.objects.db_manager(DATABASE_FOR_LOCAL_EVALUATION).filter(
team_id=self.team_id, deleted=False
team__project_id=self.project_id, deleted=False
)
}

Expand Down
2 changes: 1 addition & 1 deletion posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ def activity(self, request: request.Request, **kwargs):
page = int(request.query_params.get("page", "1"))

item_id = kwargs["pk"]
if not Insight.objects.filter(id=item_id, team_id=self.team_id).exists():
if not Insight.objects.filter(id=item_id, team__project_id=self.team.project_id).exists():
return Response("", status=status.HTTP_404_NOT_FOUND)

activity_page = load_activity(
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/organization_feature_flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def copy_flags(self, request, *args, **kwargs):

# search in destination project by name
destination_cohort = Cohort.objects.filter(
name=original_cohort.name, team_id=target_project_id, deleted=False
name=original_cohort.name, team__project_id=target_project_id, deleted=False
).first()

# create new cohort in the destination project
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ def get_serializer_context(

if dashboard_id:
try:
context["dashboard"] = Dashboard.objects.get(id=dashboard_id, team=self.team)
context["dashboard"] = Dashboard.objects.get(id=dashboard_id, team__project_id=self.team.project_id)
except Dashboard.DoesNotExist:
raise NotFound("Dashboard not found.")
if insight_id:
try:
context["insight"] = Insight.objects.get(id=insight_id, team=self.team)
context["insight"] = Insight.objects.get(id=insight_id, team__project_id=self.team.project_id)
except Insight.DoesNotExist:
raise NotFound("Insight not found.")
if recording_id:
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def validate_conditions(self, value):
return value

action_ids = (value.get("id") for value in values)
project_actions = Action.objects.filter(team_id=self.context["team_id"], id__in=action_ids)
project_actions = Action.objects.filter(team__project_id=self.context["project_id"], id__in=action_ids)

for project_action in project_actions:
for step in project_action.steps:
Expand Down Expand Up @@ -562,7 +562,7 @@ def _associate_actions(self, instance: Survey, conditions):

action_ids = (value.get("id") for value in values)

instance.actions.set(Action.objects.filter(team_id=self.context["team_id"], id__in=action_ids))
instance.actions.set(Action.objects.filter(team__project_id=self.context["project_id"], id__in=action_ids))
instance.save()

def _add_user_survey_interacted_filters(self, instance: Survey, end_date=None):
Expand Down
Loading

0 comments on commit 188fc25

Please sign in to comment.