예제 #1
0
    def get_queryset(self):
        if self.request.user.organization.is_feature_available(
                AvailableFeature.INGESTION_TAXONOMY):  # type: ignore
            try:
                from ee.models.event_definition import EnterpriseEventDefinition
            except ImportError:
                pass
            else:
                search = self.request.GET.get("search", None)
                search_query, search_kwargs = term_search_filter_sql(
                    self.search_fields, search)
                ee_event_definitions = EnterpriseEventDefinition.objects.raw(
                    f"""
                    SELECT *
                    FROM ee_enterpriseeventdefinition
                    FULL OUTER JOIN posthog_eventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id
                    WHERE team_id = %(team_id)s {search_query}
                    ORDER BY name
                    """,
                    params={
                        "team_id": self.team_id,
                        **search_kwargs
                    },
                )
                return ee_event_definitions

        return self.filter_queryset_by_parents_lookups(
            EventDefinition.objects.all()).order_by(self.ordering)
예제 #2
0
    def get_queryset(self):
        if self.request.user.organization.is_feature_available(
                AvailableFeature.INGESTION_TAXONOMY):  # type: ignore
            try:
                from ee.models.event_definition import EnterpriseEventDefinition
            except ImportError:
                pass
            else:
                search = self.request.GET.get("search", None)
                search_query, search_kwargs = term_search_filter_sql(
                    self.search_fields, search)
                # Prevent fetching deprecated `tags` field. Tags are separately fetched in TaggedItemSerializerMixin
                event_definition_fields = ", ".join([
                    f'"{f.column}"'
                    for f in EnterpriseEventDefinition._meta.get_fields()
                    if hasattr(f, "column") and f.column != "tags"
                ]  # type: ignore
                                                    )

                ee_event_definitions = EnterpriseEventDefinition.objects.raw(
                    f"""
                    SELECT {event_definition_fields}
                    FROM ee_enterpriseeventdefinition
                    FULL OUTER JOIN posthog_eventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id
                    WHERE team_id = %(team_id)s {search_query}
                    ORDER BY query_usage_30_day DESC NULLS LAST, last_seen_at DESC NULLS LAST, name ASC
                    """,
                    params={
                        "team_id": self.team_id,
                        **search_kwargs
                    },
                )
                return ee_event_definitions.prefetch_related(
                    Prefetch("tagged_items",
                             queryset=TaggedItem.objects.select_related("tag"),
                             to_attr="prefetched_tags"))

        return self.filter_queryset_by_parents_lookups(
            EventDefinition.objects.all()).order_by(self.ordering)
예제 #3
0
    def get_queryset(self):

        if self.request.user.organization.is_feature_available(
                AvailableFeature.INGESTION_TAXONOMY):  # type: ignore
            try:
                from ee.models.property_definition import EnterprisePropertyDefinition
            except ImportError:
                pass
            else:
                properties_to_filter = self.request.GET.get("properties", None)
                if properties_to_filter:
                    names = tuple(properties_to_filter.split(","))
                    name_filter = "AND name IN %(names)s"
                else:
                    names = ()
                    name_filter = ""

                search = self.request.GET.get("search", None)
                search_query, search_kwargs = term_search_filter_sql(
                    self.search_fields, search)
                ee_property_definitions = EnterprisePropertyDefinition.objects.raw(
                    f"""
                    SELECT *
                    FROM ee_enterprisepropertydefinition
                    FULL OUTER JOIN posthog_propertydefinition ON posthog_propertydefinition.id=ee_enterprisepropertydefinition.propertydefinition_ptr_id
                    WHERE team_id = %(team_id)s {name_filter} {search_query}
                    ORDER BY name
                    """,
                    params={
                        "names": names,
                        "team_id": self.team_id,
                        **search_kwargs
                    },
                )
                return ee_property_definitions

        return self.filter_queryset_by_parents_lookups(
            PropertyDefinition.objects.all()).order_by(self.ordering)
예제 #4
0
    def get_queryset(self):
        use_entreprise_taxonomy = self.request.user.organization.is_feature_available(
            AvailableFeature.INGESTION_TAXONOMY)  # type: ignore
        if use_entreprise_taxonomy:
            try:
                from ee.models.property_definition import EnterprisePropertyDefinition
            except ImportError:
                use_entreprise_taxonomy = False

        properties_to_filter = self.request.GET.get("properties", None)
        if properties_to_filter:
            names = tuple(properties_to_filter.split(","))
            name_filter = "AND name IN %(names)s"
        else:
            names = ()
            name_filter = ""

        if self.request.GET.get("is_numerical", None) == "true":
            numerical_filter = "AND is_numerical = true AND name NOT IN ('distinct_id', 'timestamp')"
        else:
            numerical_filter = ""

        # Passed as JSON instead of duplicate properties like event_names[] to work with frontend's combineUrl
        event_names = self.request.GET.get("event_names", None)
        if event_names:
            event_names = json.loads(event_names)

        excluded_properties = self.request.GET.get("excluded_properties", None)
        if excluded_properties:
            excluded_properties = json.loads(excluded_properties)

        event_property_filter = ""
        if event_names and len(event_names) > 0:
            event_property_field = "(SELECT count(1) > 0 FROM posthog_eventproperty WHERE posthog_eventproperty.team_id=posthog_propertydefinition.team_id AND posthog_eventproperty.event IN %(event_names)s AND posthog_eventproperty.property = posthog_propertydefinition.name)"
            if self.request.GET.get("is_event_property", None) == "true":
                event_property_filter = f"AND {event_property_field} = true"
            elif self.request.GET.get("is_event_property", None) == "false":
                event_property_filter = f"AND {event_property_field} = false"
        else:
            event_property_field = "NULL"

        search = self.request.GET.get("search", None)
        search_query, search_kwargs = term_search_filter_sql(
            self.search_fields, search)

        params = {
            "event_names":
            tuple(event_names or []),
            "names":
            names,
            "team_id":
            self.team_id,
            "excluded_properties":
            tuple(
                set.union(set(excluded_properties or []),
                          HIDDEN_PROPERTY_DEFINITIONS)),
            **search_kwargs,
        }

        if use_entreprise_taxonomy:
            # Prevent fetching deprecated `tags` field. Tags are separately fetched in TaggedItemSerializerMixin
            property_definition_fields = ", ".join(
                [
                    f'"{f.column}"'
                    for f in EnterprisePropertyDefinition._meta.get_fields()
                    if hasattr(f, "column") and f.column != "tags"
                ],  # type: ignore
            )

            return EnterprisePropertyDefinition.objects.raw(
                f"""
                            SELECT {property_definition_fields},
                                   {event_property_field} AS is_event_property
                            FROM ee_enterprisepropertydefinition
                            FULL OUTER JOIN posthog_propertydefinition ON posthog_propertydefinition.id=ee_enterprisepropertydefinition.propertydefinition_ptr_id
                            WHERE team_id = %(team_id)s AND name NOT IN %(excluded_properties)s {name_filter} {numerical_filter} {search_query} {event_property_filter}
                            ORDER BY is_event_property DESC, query_usage_30_day DESC NULLS LAST, name ASC
                            """,
                params=params,
            ).prefetch_related(
                Prefetch("tagged_items",
                         queryset=TaggedItem.objects.select_related("tag"),
                         to_attr="prefetched_tags"), )

        property_definition_fields = ", ".join(
            [
                f'"{f.column}"' for f in PropertyDefinition._meta.get_fields()
                if hasattr(f, "column")
            ],  # type: ignore
        )

        return PropertyDefinition.objects.raw(
            f"""
                SELECT {property_definition_fields},
                       {event_property_field} AS is_event_property
                FROM posthog_propertydefinition
                WHERE team_id = %(team_id)s AND name NOT IN %(excluded_properties)s {name_filter} {numerical_filter} {search_query} {event_property_filter}
                ORDER BY is_event_property DESC, query_usage_30_day DESC NULLS LAST, name ASC
            """,
            params=params,
        )
예제 #5
0
    def get_queryset(self):
        use_entreprise_taxonomy = self.request.user.organization.is_feature_available(
            AvailableFeature.INGESTION_TAXONOMY)  # type: ignore
        if use_entreprise_taxonomy:
            try:
                from ee.models.property_definition import EnterprisePropertyDefinition
            except ImportError:
                use_entreprise_taxonomy = False

        properties_to_filter = self.request.GET.get("properties", None)
        if properties_to_filter:
            names = tuple(properties_to_filter.split(","))
            name_filter = "AND name IN %(names)s"
        else:
            names = ()
            name_filter = ""

        if self.request.GET.get("is_numerical", None) == "true":
            numerical_filter = "AND is_numerical = true AND name NOT IN ('distinct_id', 'timestamp')"
        else:
            numerical_filter = ""

        # Passed as JSON instead of duplicate properties like event_names[] to work with frontend's combineUrl
        event_names = self.request.GET.get("event_names", None)
        if event_names:
            event_names = json.loads(event_names)

        # `order_ids_first`
        #   Any definition ids passed into the `order_ids_first` parameter will make sure that those definitions
        #   appear at the beginning of the list of definitions. This is used in the app when we want specific
        #   definitions to show at the top of a table so that they can be highlighted (i.e. viewing an individual
        #   definition's context).
        #
        #   Note that ids included in `order_ids_first` will override the same ids in `excluded_ids`.
        order_ids_first_field, order_ids_first = check_definition_ids_inclusion_field_sql(
            raw_included_definition_ids=self.request.GET.get(
                "order_ids_first", None),
            is_property=True,
            named_key="order_ids_first",
        )

        # `excluded_ids`
        #   Any definitions ids specified in the `excluded_ids` parameter will be omitted from the results.
        excluded_property_ids_field, excluded_property_ids = check_definition_ids_inclusion_field_sql(
            raw_included_definition_ids=self.request.GET.get(
                "excluded_ids", None),
            is_property=True,
            named_key="excluded_ids",
        )

        # Exclude by name
        excluded_properties = self.request.GET.get("excluded_properties", None)
        if excluded_properties:
            excluded_properties = json.loads(excluded_properties)

        event_property_filter = ""
        if event_names and len(event_names) > 0:
            event_property_field = "(SELECT count(1) > 0 FROM posthog_eventproperty WHERE posthog_eventproperty.team_id=posthog_propertydefinition.team_id AND posthog_eventproperty.event IN %(event_names)s AND posthog_eventproperty.property = posthog_propertydefinition.name)"
            if self.request.GET.get("is_event_property", None) == "true":
                event_property_filter = f"AND {event_property_field} = true"
            elif self.request.GET.get("is_event_property", None) == "false":
                event_property_filter = f"AND {event_property_field} = false"
        else:
            event_property_field = "NULL"

        search = self.request.GET.get("search", None)
        search_query, search_kwargs = term_search_filter_sql(
            self.search_fields, search)

        params = {
            "event_names":
            tuple(event_names or []),
            "names":
            names,
            "team_id":
            self.team_id,
            "order_ids_first":
            order_ids_first,
            "excluded_ids":
            excluded_property_ids,
            "excluded_properties":
            tuple(
                set.union(set(excluded_properties or []),
                          HIDDEN_PROPERTY_DEFINITIONS)),
            **search_kwargs,
        }

        if use_entreprise_taxonomy:
            # Prevent fetching deprecated `tags` field. Tags are separately fetched in TaggedItemSerializerMixin
            property_definition_fields = ", ".join(
                [
                    f'"{f.column}"'
                    for f in EnterprisePropertyDefinition._meta.get_fields()
                    if hasattr(f, "column") and f.column != "tags"
                ],  # type: ignore
            )

            return EnterprisePropertyDefinition.objects.raw(
                f"""
                            SELECT {property_definition_fields},
                                   {event_property_field} AS is_event_property,
                                   {order_ids_first_field} AS is_ordered_first
                            FROM ee_enterprisepropertydefinition
                            FULL OUTER JOIN posthog_propertydefinition ON posthog_propertydefinition.id=ee_enterprisepropertydefinition.propertydefinition_ptr_id
                            WHERE team_id = %(team_id)s AND (
                                {order_ids_first_field} = true
                                OR (name NOT IN %(excluded_properties)s AND {excluded_property_ids_field} = false)
                            ) {name_filter} {numerical_filter} {search_query} {event_property_filter}
                            ORDER BY is_ordered_first DESC, is_event_property DESC, query_usage_30_day DESC NULLS LAST, name ASC
                            """,
                params=params,
            ).prefetch_related(
                Prefetch("tagged_items",
                         queryset=TaggedItem.objects.select_related("tag"),
                         to_attr="prefetched_tags"), )

        property_definition_fields = ", ".join(
            [
                f'"{f.column}"' for f in PropertyDefinition._meta.get_fields()
                if hasattr(f, "column")
            ],  # type: ignore
        )

        return PropertyDefinition.objects.raw(
            f"""
                SELECT {property_definition_fields},
                       {event_property_field} AS is_event_property,
                       {order_ids_first_field} AS is_ordered_first
                FROM posthog_propertydefinition
                WHERE team_id = %(team_id)s AND (
                    {order_ids_first_field} = true
                    OR (name NOT IN %(excluded_properties)s AND {excluded_property_ids_field} = false)
                ) {name_filter} {numerical_filter} {search_query} {event_property_filter}
                ORDER BY is_ordered_first DESC, is_event_property DESC, query_usage_30_day DESC NULLS LAST, name ASC
            """,
            params=params,
        )
예제 #6
0
    def get_queryset(self):
        # `order_ids_first`
        #   Any definition ids passed into the `order_ids_first` parameter will make sure that those definitions
        #   appear at the beginning of the list of definitions. This is used in the app when we want specific
        #   definitions to show at the top of a table so that they can be highlighted (i.e. viewing an individual
        #   definition's context).
        #
        #   Note that ids included in `order_ids_first` will override the same ids in `excluded_ids`.
        order_ids_first_field, order_ids_first = check_definition_ids_inclusion_field_sql(
            raw_included_definition_ids=self.request.GET.get("order_ids_first", None),
            is_property=False,
            named_key="order_ids_first",
        )

        # `excluded_ids`
        #   Any definitions ids specified in the `excluded_ids` parameter will be omitted from the results.
        excluded_ids_field, excluded_ids = check_definition_ids_inclusion_field_sql(
            raw_included_definition_ids=self.request.GET.get("excluded_ids", None),
            is_property=False,
            named_key="excluded_ids",
        )

        if self.request.user.organization.is_feature_available(AvailableFeature.INGESTION_TAXONOMY):  # type: ignore
            try:
                from ee.models.event_definition import EnterpriseEventDefinition
            except ImportError:
                pass
            else:
                search = self.request.GET.get("search", None)
                search_query, search_kwargs = term_search_filter_sql(self.search_fields, search)

                params = {
                    "team_id": self.team_id,
                    "order_ids_first": order_ids_first,
                    "excluded_ids": excluded_ids,
                    **search_kwargs,
                }

                # Prevent fetching deprecated `tags` field. Tags are separately fetched in TaggedItemSerializerMixin
                event_definition_fields = ", ".join(
                    [f'"{f.column}"' for f in EnterpriseEventDefinition._meta.get_fields() if hasattr(f, "column") and f.column != "tags"]  # type: ignore
                )

                ee_event_definitions = EnterpriseEventDefinition.objects.raw(
                    f"""
                    SELECT {event_definition_fields},
                           {order_ids_first_field} AS is_ordered_first
                    FROM ee_enterpriseeventdefinition
                    FULL OUTER JOIN posthog_eventdefinition ON posthog_eventdefinition.id=ee_enterpriseeventdefinition.eventdefinition_ptr_id
                    WHERE team_id = %(team_id)s AND (
                        {order_ids_first_field} = true
                        OR {excluded_ids_field} = false
                    ) {search_query}
                    ORDER BY is_ordered_first DESC, query_usage_30_day DESC NULLS LAST, last_seen_at DESC NULLS LAST, name ASC
                    """,
                    params=params,
                )
                return ee_event_definitions.prefetch_related(
                    Prefetch(
                        "tagged_items", queryset=TaggedItem.objects.select_related("tag"), to_attr="prefetched_tags"
                    )
                )

        return (
            self.filter_queryset_by_parents_lookups(EventDefinition.objects.all())
            .annotate(
                is_ordered_first=Case(
                    When(id__in=order_ids_first, then=Value(True)), default=Value(False), output_field=BooleanField()
                )
            )
            .annotate(
                is_not_excluded_event=Case(
                    When(id__in=excluded_ids, then=Value(True)), default=Value(False), output_field=BooleanField()
                )
            )
            .filter(Q(is_ordered_first=True) | Q(is_not_excluded_event=False))
            .order_by("-is_ordered_first", "name")
        )