o
    }l~iv                     @  s  d dl mZ d dlmZ d dl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 d d	lmZ ed
dG dd dZed
dG dd dZed
dG dd dZed
dG dd dZed
dG dd dZed
dG dd deZG dd deZG dd dZdS )    )annotations)AnyN)	dataclass)MessageToDict)MapValue)Value)ExplainStats)StringValueT)frozenc                   @  &   e Zd ZU dZdZded< dd ZdS )ExplainOptionsa  
    Explain options for the query.
    Set on a query object using the explain_options attribute at query
    construction time.

    :type analyze: bool
    :param analyze: Optional. Whether to execute this query. When false
    (the default), the query will be planned, returning only metrics from the
    planning stages. When true, the query will be planned and executed,
    returning the full query results along with both planning and execution
    stage metrics.
    Fboolanalyzec                 C  s
   d| j iS )Nr   )r   self r   n/var/www/newdalilibackend/backend/venv/lib/python3.10/site-packages/google/cloud/firestore_v1/query_profile.py_to_dict/   s   
zExplainOptions._to_dictN)__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r      s   
 r   c                   @  r   )PipelineExplainOptionsa  
    Explain options for pipeline queries.

    Set on a pipeline.execution() or pipeline.stream() call, to provide
    explain_stats in the pipeline output

    :type mode: str
    :param mode: Optional. The mode of operation for this explain query.
        When set to 'analyze', the query will be executed and return the full
        query results along with execution statistics.

    :type output_format: str | None
    :param output_format: Optional. The format in which to return the explain
        stats.
    r   strmodec                 C  s$   dt | jdi}t|d}t |dS )Nr   )string_value)fields)	map_value)r   r   r   )r   out_dictvalue_pbr   r   r   	_to_valueG   s   

z PipelineExplainOptions._to_valueN)r   r   r   r   r   r   r!   r   r   r   r   r   3   s   
 r   c                   @  s   e Zd ZU dZded< dS )PlanSummaryz
    Contains planning phase information about a query.`

    :type indexes_used: list[dict[str, Any]]
    :param indexes_used: The indexes selected for this query.
    zlist[dict[str, Any]]indexes_usedNr   r   r   r   r   r   r   r   r   r"   M   s   
 r"   c                   @  s2   e Zd ZU dZded< ded< ded< ded< d	S )
ExecutionStatsa  
    Execution phase information about a query.

    Only available when explain_options.analyze is True.

    :type results_returned: int
    :param results_returned: Total number of results returned, including
        documents, projections, aggregation results, keys.
    :type execution_duration: datetime.timedelta
    :param execution_duration: Total time to execute the query in the backend.
    :type read_operations: int
    :param read_operations: Total billable read operations.
    :type debug_stats: dict[str, Any]
    :param debug_stats: Debugging statistics from the execution of the query.
        Note that the debugging stats are subject to change as Firestore evolves
    intresults_returnedzdatetime.timedeltaexecution_durationread_operationszdict[str, Any]debug_statsNr$   r   r   r   r   r%   Y   s   
 r%   c                   @  s4   e Zd ZU dZded< edd Zeddd	Zd
S )ExplainMetricsa  
    ExplainMetrics contains information about the planning and execution of a query.

    When explain_options.analyze is false, only plan_summary is available.
    When explain_options.analyze is true, execution_stats is also available.

    :type plan_summary: PlanSummary
    :param plan_summary: Planning phase information about the query.
    :type execution_stats: ExecutionStats
    :param execution_stats: Execution phase information about the query.
    r"   plan_summaryc                 C  s   t | jdd}t|di dg d}d|v r>|di }tt|dd| jjt|d	d|d
i d}t||dS t	|dS )NT)preserving_proto_field_namer,   r#   )r#   execution_statsr'   r   r)   r*   )r'   r(   r)   r*   )r,   _execution_stats)r,   )
r   _pbr"   getr%   r&   r.   r(   _ExplainAnalyzeMetricsr+   )
metrics_pb	dict_reprr,   
stats_dictr.   r   r   r   _from_pb   s    

zExplainMetrics._from_pbreturnr%   c                 C  s   t d)NzAexecution_stats not available when explain_options.analyze=False.)QueryExplainErrorr   r   r   r   r.      s   zExplainMetrics.execution_statsNr7   r%   )	r   r   r   r   r   staticmethodr6   propertyr.   r   r   r   r   r+   r   s   
 
r+   c                   @  s0   e Zd ZU dZded< ded< ed
ddZd	S )r2   z|
    Subclass of ExplainMetrics that includes execution_stats.
    Only available when explain_options.analyze is True.
    r"   r,   r%   r/   r7   c                 C     | j S )N)r/   r   r   r   r   r.      s   z&_ExplainAnalyzeMetrics.execution_statsNr9   )r   r   r   r   r   r;   r.   r   r   r   r   r2      s   
 r2   c                   @  s   e Zd ZdZdS )r8   zW
    Error returned when there is a problem accessing query profiling information.
    N)r   r   r   r   r   r   r   r   r8      s    r8   c                   @  s.   e Zd ZdZdddZddd	Zdd
dZdS )r   a[  
    Contains query profiling statistics for a pipeline query.

    This class is not meant to be instantiated directly by the user. Instead, an
    instance of `ExplainStats` may be returned by pipeline execution methods
    when `explain_options` are provided.

    It provides methods to access the explain statistics in different formats.
    stats_pbExplainStats_pbc                 C  s
   || _ dS )zk
        Args:
            stats_pb (ExplainStats_pb): The raw protobuf message for explain stats.
        N	_stats_pb)r   r=   r   r   r   __init__   s   
zExplainStats.__init__r7   r   c                 C  s(   | j jj}t }||r|jS td)a  
        Returns the explain stats as a string.

        This method is suitable for explain formats that have a text-based output,
        such as 'text' or 'json'.

        Returns:
            str: The string representation of the explain stats.

        Raises:
            QueryExplainError: If the explain stats payload from the backend is not
                a string. This can happen if a non-text output format was requested.
        zwUnable to decode explain stats. Did you request an output format that returns a string value, such as 'text' or 'json'?)r@   r0   datar	   Unpackvaluer8   )r   pb_datacontentr   r   r   get_text   s   

zExplainStats.get_textc                 C  r<   )a(  
        Returns the explain stats in an encoded proto format, as returned from the Firestore backend.
        The caller is responsible for unpacking this proto message.

        Returns:
            google.cloud.firestore_v1.types.explain_stats.ExplainStats: the proto from the backend
        r?   r   r   r   r   get_raw   s   zExplainStats.get_rawN)r=   r>   )r7   r   )r7   r>   )r   r   r   r   rA   rG   rH   r   r   r   r   r      s
    


r   )
__future__r   typingr   datetimedataclassesr   google.protobuf.json_formatr   (google.cloud.firestore_v1.types.documentr   r   -google.cloud.firestore_v1.types.explain_statsr   r>   google.protobuf.wrappers_pb2r	   r   r   r"   r%   r+   r2   	Exceptionr8   r   r   r   r   <module>   s.   *