def query_facet_performance_key_histogram(
    params: Mapping[str, str],
    top_tags: List[Any],
    tag_key: str,
    num_buckets_per_key: int,
    limit: int,
    referrer: str,
    aggregate_column: Optional[str] = None,
    filter_query: Optional[str] = None,
) -> Dict:
    precision = 0

    tag_values = [x["tags_value"] for x in top_tags]

    results = discover.histogram_query(
        fields=[
            aggregate_column,
        ],
        user_query=filter_query,
        params=params,
        num_buckets=num_buckets_per_key,
        precision=precision,
        group_by=["tags_value", "tags_key"],
        extra_conditions=[
            ["tags_key", "IN", [tag_key]],
            ["tags_value", "IN", tag_values],
        ],
        histogram_rows=limit,
        referrer="api.organization-events-facets-performance-histogram",
        normalize_results=False,
    )
    return results
def query_facet_performance_key_histogram(
    params: Mapping[str, str],
    tag_data: Mapping[str, Any],
    tag_key: str,
    aggregate_column: Optional[str] = None,
    filter_query: Optional[str] = None,
    orderby: Optional[str] = None,
    referrer: Optional[str] = None,
    limit: Optional[int] = None,
) -> Dict:
    precision = 0
    num_buckets = 100
    min_value = tag_data["min"]
    max_value = tag_data["max"]

    results = discover.histogram_query(
        [aggregate_column],
        filter_query,
        params,
        num_buckets,
        precision,
        min_value=min_value,
        max_value=max_value,
        referrer="api.organization-events-facets-performance-histogram",
        limit_by=[limit, "tags_key"] if limit else None,
        group_by=["tags_value", "tags_key"],
        extra_conditions=[["tags_key", "IN", [tag_key]]],
        normalize_results=False,
    )
    return results
    def get(self, request: Request, organization) -> Response:
        if not self.has_feature(organization, request):
            return Response(status=404)

        try:
            params = self.get_snuba_params(request, organization)
        except NoProjects:
            return Response({})

        with sentry_sdk.start_span(op="discover.endpoint",
                                   description="histogram"):
            serializer = HistogramSerializer(data=request.GET)
            if serializer.is_valid():
                data = serializer.validated_data

                with self.handle_query_errors():
                    results = discover.histogram_query(
                        data["field"],
                        data.get("query"),
                        params,
                        data["numBuckets"],
                        data["precision"],
                        min_value=data.get("min"),
                        max_value=data.get("max"),
                        data_filter=data.get("dataFilter"),
                        referrer="api.organization-events-histogram",
                        use_snql=features.has(
                            "organizations:performance-use-snql",
                            organization,
                            actor=request.user),
                    )

                return Response(results)
            else:
                return Response(serializer.errors, status=400)