o
    ,=j”+  ã                   @  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rPdd	lZdd
lmZ ddlmZ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)ÚStreamGeneratorN)Útransaction)ÚExplainMetricsÚExplainOptionsc                      sŒ   e Zd ZdZd!‡ fdd„Zdejjdfdddœd"dd„Z	d#dd„Z	dd„ Z
dejjdddfd$dd„Zdejjdfdddœd%dd „Z‡  ZS )&ÚAggregationQueryz5Represents an aggregation query to the Firestore API.ÚreturnÚNonec                   s   t t| ƒ |¡ d S )N)Úsuperr   Ú__init__)ÚselfZnested_query©Ú	__class__© úoC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\google/cloud/firestore_v1/aggregation.pyr   0   s   zAggregationQuery.__init__N)Úexplain_optionsÚ	read_timer   ú"Union[retries.Retry, None, object]Útimeoutúfloat | Noner   úOptional[ExplainOptions]r   úOptional[datetime.datetime]ú#QueryResultsList[AggregationResult]c          	      C  sB   d}| j |||||d�}t|ƒ}|du rd}n| ¡ }t|||ƒS )aP  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.
            read_time (Optional[datetime.datetime]): If set, reads documents as they were at the given
                time. This must be a timestamp within the past one hour, or if Point-in-Time Recovery
                is enabled, can additionally be a whole minute timestamp within the past 7 days. If no
                timezone is specified in the :class:`datetime.datetime` object, it is assumed to be UTC.

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

        N©r   r   r!   r   r   )ÚstreamÚlistZget_explain_metricsr   )	r   r   r   r!   r   r   Úexplain_metricsÚresultZresult_listr   r   r   Úget6   s   %ûzAggregationQuery.getc                 C  s4   |   |||||¡\}}| jjjd|| jjdœ|¤ŽS )ú!Helper method for :meth:`stream`.)ÚrequestÚmetadataNr   )Z_prep_streamÚ_clientÚ_firestore_apiÚrun_aggregation_queryZ_rpc_metadata)r   r   r   r!   r   r   r-   Úkwargsr   r   r   Ú_get_stream_iteratorm   s   û
þýz%AggregationQuery._get_stream_iteratorc                 C  s8   |du r|t jju r| jjj}|j}|j}| |¡S dS )r,   NF)	r
   ÚmethodÚDEFAULTr/   r0   Ú
_transportr1   Z_retryZ
_predicate)r   Úexcr   r   Ú	transportZgapic_callabler   r   r   Ú_retry_query_after_exception   s   

z-AggregationQuery._retry_query_after_exceptionr   ú!Optional[transaction.Transaction]úOptional[float]úAGenerator[List[AggregationResult], Any, Optional[ExplainMetrics]]c              
   c  s¨   � d}|   |||||¡}	 zt|dƒ}W n$ tjy8 }	 z|  |	||¡r3|   |||||¡}W Y d}	~	q‚ d}	~	ww |du r@	 |S |du rJ|jrJ|j}t|ƒ}
|
rS|
V  q)aI  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.
            read_time (Optional[datetime.datetime]): If set, reads documents as they were at the given
                time. This must be a timestamp within the past one hour, or if Point-in-Time Recovery
                is enabled, can additionally be a whole minute timestamp within the past 7 days. If no
                timezone is specified in the :class:`datetime.datetime` object, it is assumed to be UTC.

        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.

        NT)r3   Únextr	   ZGoogleAPICallErrorr9   r)   r   )r   r   r   r!   r   r   ZmetricsZresponse_iteratorÚresponser7   r*   r   r   r   Ú_make_streamŠ   sD   €*ûû€õ	ùèzAggregationQuery._make_streamú#Optional['transaction.Transaction']ú(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.
            read_time (Optional[datetime.datetime]): If set, reads documents as they were at the given
                time. This must be a timestamp within the past one hour, or if Point-in-Time Recovery
                is enabled, can additionally be a whole minute timestamp within the past 7 days. If no
                timezone is specified in the :class:`datetime.datetime` object, it is assumed to be UTC.

        Returns:
            `StreamGenerator[List[AggregationResult]]`:
            A generator of the query results.
        r&   N)r?   r   )r   r   r   r!   r   r   Zinner_generatorr   r   r   r'   Ù   s   &û
zAggregationQuery.stream)r   r   )
r   r    r!   r"   r   r#   r   r$   r   r%   )NN)r   r:   r   r    r!   r;   r   r#   r   r$   r   r<   )r   r@   r   r    r!   r;   r   r#   r   r$   r   rA   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r
   r4   r5   r+   r3   r9   r?   r'   Ú__classcell__r   r   r   r   r   -   s4    üù8
ÿúQüùr   )rE   Ú
__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   ÚdatetimeZgoogle.cloud.firestore_v1r   Z'google.cloud.firestore_v1.query_profiler   r   r   r   r   r   r   Ú<module>   s    