U
    $FZh%                     @  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mZ ddl
mZ ddlmZmZmZ ddlmZ ddlmZ erdd	lmZ dd
lmZ ddlmZ G dd deZdS )aU  Classes for representing aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.aggregation.AggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.collection.Collection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
    )annotations)TYPE_CHECKINGAny	GeneratorListOptionalUnion)
exceptionsgapic_v1)retry)AggregationResultBaseAggregationQuery_query_response_to_result)QueryResultsList)StreamGenerator)transaction)ExplainMetrics)ExplainOptionsc                      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ddZ	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 )AggregationQueryz5Represents an aggregation query to the Firestore API.None)returnc                   s   t t| | d S )N)superr   __init__)selfZnested_query	__class__ l/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/google/cloud/firestore_v1/aggregation.pyr   .   s    zAggregationQuery.__init__N)explain_optionsz"Union[retries.Retry, None, object]zfloat | NonezOptional[ExplainOptions]z#QueryResultsList[AggregationResult])r   timeoutr   r   c                C  s@   d}| j ||||d}t|}|dkr,d}n| }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[AggregationResult]: The aggregation query results.

        Nr   r   r   r   )streamlistZget_explain_metricsr   )r   r   r   r   r   explain_metricsresultZresult_listr   r   r   get4   s    "zAggregationQuery.getc                 C  s2   |  ||||\}}| jjjf || jjd|S )!Helper method for :meth:`stream`.)requestmetadata)Z_prep_stream_client_firestore_apirun_aggregation_queryZ_rpc_metadata)r   r   r   r   r   r'   kwargsr   r   r   _get_stream_iteratorg   s    
z%AggregationQuery._get_stream_iteratorc                 C  s8   |dkr4|t jjkr*| jjj}|j}|j}||S dS )r&   NF)	r
   methodDEFAULTr)   r*   Z
_transportr+   Z_retryZ
_predicate)r   excr   r   	transportZgapic_callabler   r   r   _retry_query_after_exceptionv   s    

z-AggregationQuery._retry_query_after_exceptionz!Optional[transaction.Transaction]zOptional[float]zAGenerator[List[AggregationResult], Any, Optional[ExplainMetrics]])r   r   r   r   r   c           
   
   c  s   d}|  ||||}zt|d}W nL tjk
rn } z,| |||r\|  |||}W Y 
qn W 5 d}~X Y nX |dkrzq|dkr|jr|j}t|}	|	r|	V  q|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 this 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]:
            The result of aggregations of this query.

        Returns:
            (Optional[google.cloud.firestore_v1.types.query_profile.ExplainMetrtics]):
            The results of query profiling, if received from the service.

        N)r-   nextr	   ZGoogleAPICallErrorr2   r#   r   )
r   r   r   r   r   ZmetricsZresponse_iteratorresponser0   r$   r   r   r   _make_stream   s4    '
zAggregationQuery._make_streamz#Optional['transaction.Transaction']z(StreamGenerator[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 this 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 (Optinal[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:
            `StreamGenerator[List[AggregationResult]]`:
            A generator of the query results.
        r    )r5   r   )r   r   r   r   r   Zinner_generatorr   r   r   r!      s    #zAggregationQuery.stream)N)__name__
__module____qualname____doc__r   r
   r.   r/   r%   r-   r2   r5   r!   __classcell__r   r   r   r   r   +   s*   3
Kr   N)r9   
__future__r   typingr   r   r   r   r   r   Zgoogle.api_corer	   r
   r   retriesZ*google.cloud.firestore_v1.base_aggregationr   r   r   Z'google.cloud.firestore_v1.query_resultsr   Z*google.cloud.firestore_v1.stream_generatorr   Zgoogle.cloud.firestore_v1r   Z'google.cloud.firestore_v1.query_profiler   r   r   r   r   r   r   <module>   s    