U
    >i!                     @  s   d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ erdd
lmZ ddlmZmZ ddlm  m  m  mZ  G dd deZ!dS )aq  Classes for representing Async aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.async_aggregation.AsyncAggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.async_collection.AsyncCollection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
    )annotations)TYPE_CHECKINGAnyAsyncGeneratorListOptionalUnion)gapic_v1)retry_async)transaction)AsyncStreamGenerator)BaseAggregationQuery_query_response_to_result)QueryResultsList)AggregationResult)ExplainMetricsExplainOptionsNc                      s   e Zd ZdZdd fddZdejjdfdddd	d
ddddZdejjddfdddd
ddddZ	dejjdfdddddd
ddddZ
  ZS )AsyncAggregationQueryz5Represents an aggregation query to the Firestore API.None)returnc                   s   t t| | d S N)superr   __init__)selfZnested_query	__class__ O/tmp/pip-unpacked-wheel-599y42ri/google/cloud/firestore_v1/async_aggregation.pyr   -   s    zAsyncAggregationQuery.__init__N)explain_optionsz'Union[retries.AsyncRetry, None, object]zfloat | NonezOptional[ExplainOptions]z)QueryResultsList[List[AggregationResult]])retrytimeoutr   r   c                  sh   d}| j ||||d}z4dd |2 I dH }|dkr:d}n| I dH }W 5 | I dH  X t|||S )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and returns a list of aggregation results in the stream of ``RunAggregationQueryResponse`` messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
                If a ``transaction`` is used and it already has write operations
                added, this method cannot be used (i.e. read-after-write is not
                allowed).
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            QueryResultsList[List[AggregationResult]]: The aggregation query results.

        Nr   r   r    r   c                   s   g | z3 d H W }|q6 S r   r   ).0Zaggregationr   r   r   
<listcomp>\   s     z-AsyncAggregationQuery.get.<locals>.<listcomp>)streamacloseZget_explain_metricsr   )r   r   r   r    r   explain_metricsZstream_resultresultr   r   r   get3   s     zAsyncAggregationQuery.getz!Optional[transaction.Transaction]z"retries.AsyncRetry | object | NonezOptional[float]zNAsyncGenerator[List[AggregationResult] | query_profile_pb.ExplainMetrics, Any])r   r   r    r   r   c                 C st   |  ||||\}}| jjjf || jjd|I dH }|2 z03 dH W }t|}	|	rZ|	V  |jr<|j}
|
V  q<6 dS )a	  Internal method for stream(). Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator which
        consumes each document returned in the stream of ``RunAggregationQueryResponse``
        messages.

        If a ``transaction`` is used and it already has write operations
        added, this method cannot be used (i.e. read-after-write is not
        allowed).

        Args:
            transaction (Optional[:class:`~google.cloud.firestore_v1.transaction.                Transaction`]):
                An existing transaction that the query will run in.
            retry (Optional[google.api_core.retry.Retry]): Designation of what
                errors, if any, should be retried.  Defaults to a
                system-specified policy.
            timeout (Optional[float]): The timeout for this request. Defaults
                to a system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Yields:
            List[AggregationResult] | query_profile_pb.ExplainMetrics:
            The result of aggregations of this query. Query results will be
            yielded as `List[AggregationResult]`. When the result contains
            returned explain metrics, yield `query_profile_pb.ExplainMetrics`
            individually.
        )requestmetadataN)Z_prep_streamZ_clientZ_firestore_apiZrun_aggregation_queryZ_rpc_metadatar   r&   )r   r   r   r    r   r)   kwargsZresponse_iteratorresponser'   Zmetricsr   r   r   _make_streamg   s&    &
z"AsyncAggregationQuery._make_streamz-AsyncStreamGenerator[List[AggregationResult]]c                C  s   | j ||||d}t||S )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator
        which consumes each document returned in the stream of
        ``RunAggregationQueryResponse`` messages.

        If a ``transaction`` is used and it already has write operations added,
        this method cannot be used (i.e. read-after-write is not allowed).

        Args:
            transaction (Optional[:class:`~google.cloud.firestore_v1.transaction.                Transaction`]):
                An existing transaction that the query will run in.
            retry (Optional[google.api_core.retry.Retry]): Designation of what
                errors, if any, should be retried.  Defaults to a
                system-specified policy.
            timeout (Optional[float]): The timeout for this request. Defaults
                to a system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            `AsyncStreamGenerator[List[AggregationResult]]`:
                A generator of the query results.
        r!   )r-   r   )r   r   r   r    r   Zinner_generatorr   r   r   r$      s    $zAsyncAggregationQuery.stream)__name__
__module____qualname____doc__r   r	   methodDEFAULTr(   r-   r$   __classcell__r   r   r   r   r   *   s&   6>r   )"r1   
__future__r   typingr   r   r   r   r   r   Zgoogle.api_corer	   r
   retriesZgoogle.cloud.firestore_v1r   Z0google.cloud.firestore_v1.async_stream_generatorr   Z*google.cloud.firestore_v1.base_aggregationr   r   Z'google.cloud.firestore_v1.query_resultsr   r   Z'google.cloud.firestore_v1.query_profiler   r   Z-google.cloud.firestore_v1.types.query_profileZcloudZfirestore_v1typesZquery_profileZquery_profile_pbr   r   r   r   r   <module>   s    