
    Ѯh,                    X   d Z ddlmZ ddlZddlmZ ddlmZ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 er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  G d de       Z! G d de      Z" G d de"      Z# G d de"      Z$ G d de"      Z%	 	 ddZ& G d de      Z'y)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.
    )annotationsN)ABC)TYPE_CHECKINGAny	CoroutineListOptionalTupleUnion)gapic_v1)retry)_helpers)	FieldPath)StructuredAggregationQuery)transaction)AsyncStreamGenerator)ExplainOptions)QueryResultsList)StreamGeneratorc                       e Zd ZdZdddZd Zy)AggregationResulta  
    A class representing result from Aggregation Query
    :type alias: str
    :param alias: The alias for the aggregation.
    :type value: int
    :param value: The resulting value from the aggregation.
    :type read_time:
    :param value: The resulting read_time
    Nc                .    || _         || _        || _        y Naliasvalue	read_time)selfr   r   r   s       |/home/www/academy-backend.kofcorporation.com/venv/lib/python3.12/site-packages/google/cloud/firestore_v1/base_aggregation.py__init__zAggregationResult.__init__:   s    

"    c                V    d| j                    d| j                   d| j                   dS )Nz<Aggregation alias=z, value=z, readtime=>r   r   s    r   __repr__zAggregationResult.__repr__?   s.    $TZZLKPTP^P^O__`aar!   r   )r   strr   float)__name__
__module____qualname____doc__r    r%    r!   r   r   r   /   s    #
br!   r   c                  :    e Zd ZdddZej
                  d        Zy)BaseAggregationNc                    || _         y r   r   )r   r   s     r   r    zBaseAggregation.__init__D   s	    
r!   c                     y)4Convert this instance to the protobuf representationNr,   r$   s    r   _to_protobufzBaseAggregation._to_protobufG       r!   r   r   
str | None)r(   r)   r*   r    abcabstractmethodr3   r,   r!   r   r.   r.   C   s#     	C Cr!   r.   c                  (     e Zd Zdd fdZd Z xZS )CountAggregationc                .    t         t        |   |       y Nr0   )superr:   r    )r   r   	__class__s     r   r    zCountAggregation.__init__M   s    .U.;r!   c                    t        j                         }| j                  |_        t         j                  j                         |_        |S r2   )r   Aggregationr   Countcountr   aggregation_pbs     r   r3   zCountAggregation._to_protobufP   s9    3??A#zz9EEKKMr!   r   r5   r(   r)   r*   r    r3   __classcell__r>   s   @r   r:   r:   L   s    <r!   r:   c                  (     e Zd Zdd fdZd Z xZS )SumAggregationc                |    t        |t              r|j                         }|| _        t        t
        |   |       y r<   )
isinstancer   to_api_repr	field_refr=   rJ   r    r   rN   r   r>   s      r   r    zSumAggregation.__init__Y   4    i+!--/I"nd,5,9r!   c                    t        j                         }| j                  |_        t         j                  j                         |_        | j
                  |j                  j                  _        |S r@   )r   rA   r   SumsumrN   field
field_pathrD   s     r   r3   zSumAggregation._to_protobuf`   Q    3??A#zz7CCGGI.2nn  +r!   r   rN   zstr | FieldPathr   r6   rF   rH   s   @r   rJ   rJ   X       :r!   rJ   c                  (     e Zd Zdd fdZd Z xZS )AvgAggregationc                |    t        |t              r|j                         }|| _        t        t
        |   |       y r<   )rL   r   rM   rN   r=   rZ   r    rO   s      r   r    zAvgAggregation.__init__j   rP   r!   c                    t        j                         }| j                  |_        t         j                  j                         |_        | j
                  |j                  j                  _        |S r@   )r   rA   r   AvgavgrN   rT   rU   rD   s     r   r3   zAvgAggregation._to_protobufq   rV   r!   r   rW   rF   rH   s   @r   rZ   rZ   i   rX   r!   rZ   c                >   | j                   j                  j                  j                         D cg c]`  }t	        || j                   j                  |   j
                  xs# | j                   j                  |   j                  | j                        b }}|S c c}w )Nr   )resultaggregate_fieldspbkeysr   integer_valuedouble_valuer   )response_pbkeyresultss      r   _query_response_to_resultri   z   s     %%6699>>@  	$$55c:HH E!!2237DD!++		
G  Ns   A%Bc                  X   e Zd ZdZdddZed        ZdddZdddZdddZ	ddZ
dd	Zdd
Z	 	 	 	 d	 	 	 	 	 	 	 ddZej                  dej"                  j$                  dfdd	 	 	 	 	 	 	 dd       Zej                  dej"                  j$                  dfdd	 	 	 	 	 	 	 	 	 dd       Zy)BaseAggregationQueryz5Represents an aggregation query to the Firestore API.Nc                P    || _         || _        |j                  | _        g | _        y r   )_nested_query_alias_parent_collection_ref_aggregations)r   nested_queryr   s      r   r    zBaseAggregationQuery.__init__   s'    )+3346r!   c                .    | j                   j                  S r   )rp   _clientr$   s    r   rt   zBaseAggregationQuery._client   s    ##+++r!   c                T    t        |      }| j                  j                  |       | S )z4
        Adds a count over the nested query
        r0   )r:   rq   append)r   r   count_aggregations      r   rC   zBaseAggregationQuery.count   s)     -59!!"34r!   c                V    t        ||      }| j                  j                  |       | S )z2
        Adds a sum over the nested query
        r0   )rJ   rq   rv   )r   rN   r   sum_aggregations       r   rS   zBaseAggregationQuery.sum   )     )%@!!/2r!   c                V    t        ||      }| j                  j                  |       | S )z3
        Adds an avg over the nested query
        r0   )rZ   rq   rv   )r   rN   r   avg_aggregations       r   r^   zBaseAggregationQuery.avg   rz   r!   c                :    | j                   j                  |       y)z
        Adds an aggregation operation to the nested query

        :type aggregation: :class:`google.cloud.firestore_v1.aggregation.BaseAggregation`
        :param aggregation: An aggregation operation, e.g. a CountAggregation
        N)rq   rv   )r   aggregations     r   add_aggregationz$BaseAggregationQuery.add_aggregation   s     	!!+.r!   c                :    | j                   j                  |       y)z
        Adds a list of aggregations to the nested query

        :type aggregations: list
        :param aggregations: a list of aggregation operations
        N)rq   extend)r   aggregationss     r   add_aggregationsz%BaseAggregationQuery.add_aggregations   s     	!!,/r!   c                    t               }| j                  j                         |_        | j                  D ]-  }|j                         }|j
                  j                  |       / |S r   )r   rm   r3   structured_queryrq   r   rv   )r   rb   r~   rE   s       r   r3   z!BaseAggregationQuery._to_protobuf   s]    ')"00==?-- 	3K(557NOO"">2	3 	r!   c                    | j                   j                         \  }}|| j                         t        j                  |      d}|r|j                         |d<   t        j                  ||      }||fS )N)parentstructured_aggregation_queryr   explain_options)rp   _parent_infor3   r   get_transaction_id_to_dictmake_retry_timeout_kwargs)	r   r   r   timeoutr   parent_pathexpected_prefixrequestkwargss	            r   _prep_streamz!BaseAggregationQuery._prep_stream   sv     (,';';'H'H'J$_!,0,=,=,?#66{C

 )8)A)A)CG%&33E7Cr!   )r   c                    y)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]] | Coroutine[Any, Any, List[List[AggregationResult]]]):
            The aggregation query results.
        Nr,   r   r   r   r   r   s        r   getzBaseAggregationQuery.get   r4   r!   c                    y)a[  Runs the aggregation query.

        This sends a``RunAggregationQuery`` RPC and returns a generator 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.
            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]] | AsyncStreamGenerator[List[AggregationResult]]:
            A generator of the query results.
        Nr,   r   s        r   streamzBaseAggregationQuery.stream  r4   r!   r   )r   r6   returnNoner5   rW   )r~   r.   r   r   )r   zList[BaseAggregation]r   r   )r   r   )NNNN)r   6Union[retries.Retry, retries.AsyncRetry, None, object]r   float | Noner   Optional[ExplainOptions]r   zTuple[dict, dict])r   r   r   r   r   r   r   zXQueryResultsList[AggregationResult] | Coroutine[Any, Any, List[List[AggregationResult]]])
r   z!Optional[transaction.Transaction]r   z2retries.Retry | retries.AsyncRetry | object | Noner   zOptional[float]r   r   r   zXStreamGenerator[List[AggregationResult]] | AsyncStreamGenerator[List[AggregationResult]])r(   r)   r*   r+   r    propertyrt   rC   rS   r^   r   r   r3   r   r7   r8   r   methodDEFAULTr   r   r,   r!   r   rk   rk      sG   ?7 , ,/0 HL $48 F 	
 2 
& 	  OO## $& 59&
& & 2&	=& &P 	 :> ((#'# 59#6## !# 2#	8# #r!   rk   )r   zList[AggregationResult])(r+   
__future__r   r7   r   typingr   r   r   r   r	   r
   r   google.api_corer   r   retriesgoogle.cloud.firestore_v1r   $google.cloud.firestore_v1.field_pathr   google.cloud.firestore_v1.typesr   r   0google.cloud.firestore_v1.async_stream_generatorr   'google.cloud.firestore_v1.query_profiler   'google.cloud.firestore_v1.query_resultsr   *google.cloud.firestore_v1.stream_generatorr   objectr   r.   r:   rJ   rZ   ri   rk   r,   r!   r   <module>r      s    # 
  N N N $ , . :
 5UFH
b b(Cc C	 	_ "_ " `3 `r!   