o
    ABi+                     @  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rTdd	lmZ dd
lmZ ddlmZ ddl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)ExplainOptionsNc                      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__)selfnested_query	__class__ e/var/www/html/Trade-python/venv/lib/python3.10/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listget_explain_metricsr   )	r   r   r   r"   r   r    explain_metricsresult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   )_prep_stream_client_firestore_apirun_aggregation_query_rpc_metadata)r   r   r   r"   r   r    r0   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DEFAULTr3   r4   
_transportr5   _retry
_predicate)r   excr   r   	transport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.

        N)r8   nextr	   GoogleAPICallErrorrA   r+   r   )r   r   r   r"   r   r    metricsresponse_iteratorresponser>   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'   )rJ   r   )r   r   r   r"   r   r    inner_generatorr   r   r   r(      s   &
zAggregationQuery.stream)r   r   )
r   r!   r"   r#   r   r$   r    r%   r   r&   )NN)r   rB   r   r!   r"   rC   r   r$   r    r%   r   rD   )r   rK   r   r!   r"   rC   r   r$   r    r%   r   rL   )__name__
__module____qualname____doc__r   r
   r9   r:   r.   r8   rA   rJ   r(   __classcell__r   r   r   r   r   -   s4    8
Qr   )rQ   
__future__r   typingr   r   r   r   r   r   google.api_corer	   r
   r   retries*google.cloud.firestore_v1.base_aggregationr   r   r   'google.cloud.firestore_v1.query_resultsr   *google.cloud.firestore_v1.stream_generatorr   google.cloud.firestore_v1r   'google.cloud.firestore_v1.query_profiler   r   datetimer   r   r   r   r   <module>   s    