o
    }l~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radd
lmZ ddlmZmZ ddlm  m  m  mZ  ddl!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                      sx   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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 )!AsyncAggregationQueryz5Represents an aggregation query to the Firestore API.returnNonec                   s   t t| | d S N)superr   __init__)selfnested_query	__class__ r/var/www/newdalilibackend/backend/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/async_aggregation.pyr   .   s   zAsyncAggregationQuery.__init__N)explain_options	read_timeretry'Union[retries.AsyncRetry, None, object]timeoutfloat | Noner   Optional[ExplainOptions]r    Optional[datetime.datetime])QueryResultsList[List[AggregationResult]]c          	        sz   d}| j |||||d}z!dd |2 I dH }|du rd}n| I dH }W | I dH  n| I dH  w t|||S )aF  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[List[AggregationResult]]: The aggregation query results.

        Nr   r!   r#   r   r    c                   s   g | z3 d H W }|q6 S r   r   ).0aggregationr   r   r   
<listcomp>a   s    z-AsyncAggregationQuery.get.<locals>.<listcomp>)streamget_explain_metricsacloser   )	r   r   r!   r#   r   r    explain_metricsstream_resultresultr   r   r   get4   s"   # zAsyncAggregationQuery.getr   !Optional[transaction.Transaction]"retries.AsyncRetry | object | NoneOptional[float]NAsyncGenerator[List[AggregationResult] | query_profile_pb.ExplainMetrics, Any]c                 C sx   |  |||||\}}| jjjd|| jjd|I dH }|2 z3 dH W }	t|	}
|
r/|
V  |	jr8|	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.
            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] | 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metadataNr   )_prep_stream_client_firestore_apirun_aggregation_query_rpc_metadatar   r/   )r   r   r!   r#   r   r    r7   kwargsresponse_iteratorresponser1   metricsr   r   r   _make_streaml   s.   *
z"AsyncAggregationQuery._make_stream-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.
            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:
            `AsyncStreamGenerator[List[AggregationResult]]`:
                A generator of the query results.
        r(   )rB   r   )r   r   r!   r#   r   r    inner_generatorr   r   r   r,      s   (
zAsyncAggregationQuery.stream)r   r   )
r!   r"   r#   r$   r   r%   r    r&   r   r'   )r   r3   r!   r4   r#   r5   r   r%   r    r&   r   r6   )r   r3   r!   r4   r#   r5   r   r%   r    r&   r   rC   )__name__
__module____qualname____doc__r   r	   methodDEFAULTr2   rB   r,   __classcell__r   r   r   r   r   +   s.    :Cr   )#rH   
__future__r   typingr   r   r   r   r   r   google.api_corer	   r
   retriesgoogle.cloud.firestore_v1r   0google.cloud.firestore_v1.async_stream_generatorr   *google.cloud.firestore_v1.base_aggregationr   r   'google.cloud.firestore_v1.query_resultsr   r   'google.cloud.firestore_v1.query_profiler   r   -google.cloud.firestore_v1.types.query_profilecloudfirestore_v1typesquery_profilequery_profile_pbdatetimer   r   r   r   r   <module>   s    