
    gV,                       d dl mZ d dlm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mZmZ d dlmZ erd d	lmZ  ed
      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 G d de
      Z ede      Z edd      Z  edd      Z! G d dee         Z" G d de"e         Z# G d  de"e         Z$y# e$ rZ ed      edZ[ww xY w)!    )annotations)datetime)TracebackType)TYPE_CHECKINGAnyGenericLiteral	TypedDictTypeVar)AsyncClientClientResponseTimeout)
BaseClientz2httpx is required to use the Logfire query clientsN)Tableg      >@c                      e Zd ZdZy)QueryExecutionErrorz4Raised when the query execution fails on the server.N__name__
__module____qualname____doc__     V/var/www/openai/venv/lib/python3.12/site-packages/logfire/experimental/query_client.pyr   r      s    >r   r   c                      e Zd ZdZy)QueryRequestErrorz)Raised when the query request is invalid.Nr   r   r   r   r   r      s    3r   r   c                  0    e Zd ZU dZded<   ded<   ded<   y)	ColumnDetailszFThe details of a column in the row-oriented JSON-format query results.strnamer   datatypeboolnullableNr   r   r   r   __annotations__r   r   r   r   r      s    P
IMNr   r   c                      e Zd ZU dZded<   y)
ColumnDatazFThe data of a column in the column-oriented JSON-format query results.z	list[Any]valuesNr%   r   r   r   r(   r(   '   s    Pr   r(   c                      e Zd ZU dZded<   y)QueryResultsz5The (column-oriented) results of a JSON-format query.zlist[ColumnData]columnsNr%   r   r   r   r+   r+   -   s    ?r   r+   c                  &    e Zd ZU dZded<   ded<   y)RowQueryResultsz0The row-oriented results of a JSON-format query.zlist[ColumnDetails]r,   zlist[dict[str, Any]]rowsNr%   r   r   r   r.   r.   3   s    :  
r   r.   T)boundSLogfireQueryClientRAsyncLogfireQueryClientc                  D    e Zd ZddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZy)	_BaseLogfireQueryClientc                T    || _         || _        || _         |d||d|id|| _        y )Nauthorization)timeoutbase_urlheadersr   )r;   
read_tokenr:   client)selfr;   r=   r:   r>   client_kwargss         r   __init__z _BaseLogfireQueryClient.__init__@   s<     $ 
h*8U
Yf
r   Nc                    d|i}|t        |      |d<   |rd|d<   |r|j                         |d<   |r|j                         |d<   |S )Nsqllimittrue	json_rowsmin_timestampmax_timestamp)r    	isoformat)r?   rC   rG   rH   rD   row_orientedparamss          r   build_query_paramsz*_BaseLogfireQueryClient.build_query_paramsH   s^     #(!%jF7O"(F;&3&=&=&?F?#&3&=&=&?F?#r   c                    |j                   dk(  rt        |j                               |j                   dk(  rt        |j                               |j                   dk(  sJ |j                         y )Ni  i     )status_coder   jsonr   content)r?   responses     r   handle_response_errorsz._BaseLogfireQueryClient.handle_response_errors[   s_    3&%hmmo663&#HMMO44##s*<H,<,<<*r   )
r;   r    r=   r    r:   r   r>   ztype[T]r@   r   NNNF)rC   r    rG   datetime | NonerH   rU   rD   
int | NonerJ   r#   returnzdict[str, str])rR   r   rW   None)r   r   r   rA   rL   rS   r   r   r   r7   r7   ?   sY    
 *.)- " ' '	
   
&=r   r7   c                      e Zd ZdZdef	 	 	 	 	 	 	 d fdZddZ	 	 	 d	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZ		 	 	 d	 	 	 	 	 	 	 	 	 ddZ
	 	 	 d	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z xZS )r3   z/A synchronous client for querying Logfire data.!https://logfire-api.pydantic.dev/c                4    t        |   |||t        fi | y N)superrA   r   )r?   r=   r;   r:   r@   	__class__s        r   rA   zLogfireQueryClient.__init__f   s     	:wP-Pr   c                :    | j                   j                          | S r\   )r>   	__enter__r?   s    r   r`   zLogfireQueryClient.__enter__o   s    r   c                >    | j                   j                  |||       y r\   )r>   __exit__r?   exc_type	exc_value	tracebacks       r   rc   zLogfireQueryClient.__exit__s   s     	Xy)<r   c                P    | j                  d||||d      }|j                         S )JQuery Logfire data and return the results as a column-oriented dictionary.application/jsonFacceptrC   rG   rH   rD   rJ   _queryrP   r?   rC   rG   rH   rD   rR   s         r   
query_jsonzLogfireQueryClient.query_json{   s7     ;;%''  
 }}r   c                P    | j                  d||||d      }|j                         S )GQuery Logfire data and return the results as a row-oriented dictionary.rj   Trk   rm   ro   s         r   query_json_rowsz"LogfireQueryClient.query_json_rows   s7     ;;%''  
 }}r   c                   	 ddl }| j                  d||||      }|j                  j	                  |j
                        5 }|j                         }	ddd       |	S # t        $ r}t        d      |d}~ww xY w# 1 sw Y   	S xY wzQuery Logfire data and return the results as a pyarrow Table.

        Note that pyarrow must be installed for this method to succeed.

        You can use `polars.from_arrow(result)` to convert the returned table to a polars DataFrame.
        r   Nz1pyarrow is required to use the query_arrow methodz#application/vnd.apache.arrow.streamrl   rC   rG   rH   rD   pyarrowImportErrorrn   ipcopen_streamrQ   read_all
r?   rC   rG   rH   rD   rx   erR   readerarrow_tables
             r   query_arrowzLogfireQueryClient.query_arrow   s    	Z ;;8''  
 [[$$X%5%56&!'!2K 7  	ZQRXYY	Z 7s#   A A9	A6%A11A69Bc                F    | j                  d||||      }|j                  S )Query Logfire data and return the results as a CSV-format string.

        Use `polars.read_csv(StringIO(result))` to convert the returned CSV to a polars DataFrame.
        text/csvrv   rn   textro   s         r   	query_csvzLogfireQueryClient.query_csv   s2     ;;''  
 }}r   c                    | j                  |||||      }| j                  j                  dd|i|      }| j                  |       |S Nz	/v1/queryrl   )r<   rK   rL   r>   getrS   	r?   rl   rC   rG   rH   rD   rJ   rK   rR   s	            r   rn   zLogfireQueryClient._query   sN     ((m]ES_`;;??;68JSY?Z##H-r   )r=   r    r;   r    r:   r   r@   r   )r?   r2   rW   r2   NNNre   ztype[BaseException] | Nonerf   zBaseException | Nonerg   zTracebackType | NonerW   rX   
rC   r    rG   rU   rH   rU   rD   rV   rW   r+   
rC   r    rG   rU   rH   rU   rD   rV   rW   r.   
rC   r    rG   rU   rH   rU   rD   rV   rW   r   
rC   r    rG   rU   rH   rU   rD   rV   rW   r    rT   rl   zNLiteral['application/json', 'application/vnd.apache.arrow.stream', 'text/csv']rC   r    rG   rU   rH   rU   rD   rV   rJ   r#   rW   r   )r   r   r   r   DEFAULT_TIMEOUTrA   r`   rc   rp   rs   r   r   rn   __classcell__r^   s   @r   r3   r3   c   s   9
 <*	QQ Q 	Q
 Q 04*.*.	=,= (= (	=
 
= *.)-  ' '	
  
* *.)-  ' '	
  
* *.)-  ' '	
  
@ *.)-  ' '	
  
0 *.)- "^  '	
 '   
r   c                      e Zd ZdZdef	 	 	 	 	 	 	 d fdZddZ	 	 	 d	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZ		 	 	 d	 	 	 	 	 	 	 	 	 ddZ
	 	 	 d	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z xZS )r5   z1An asynchronous client for querying Logfire data.rZ   c                4    t        |   |||t        fi | y r\   )r]   rA   r   )r?   r=   r;   r:   async_client_kwargsr^   s        r   rA   z AsyncLogfireQueryClient.__init__   s     	:w[GZ[r   c                V   K   | j                   j                          d {    | S 7 wr\   )r>   
__aenter__ra   s    r   r   z"AsyncLogfireQueryClient.__aenter__   s&     kk$$&&& 	's   )')c                Z   K   | j                   j                  |||       d {    y 7 wr\   )r>   	__aexit__rd   s       r   r   z!AsyncLogfireQueryClient.__aexit__   s$      kk##HiCCCs   !+)+c                l   K   | j                  d||||d       d{   }|j                         S 7 w)ri   rj   Frk   Nrm   ro   s         r   rp   z"AsyncLogfireQueryClient.query_json   sE      %'' % 
 
 }}
   424c                l   K   | j                  d||||d       d{   }|j                         S 7 w)rr   rj   Trk   Nrm   ro   s         r   rs   z'AsyncLogfireQueryClient.query_json_rows	  sE      %'' % 
 
 }}
r   c                (  K   	 ddl }| j                  d||||       d{   }|j                  j	                  |j
                        5 }|j                         }	ddd       |	S # t        $ r}t        d      |d}~ww xY w7 a# 1 sw Y   	S xY wwru   rw   r}   s
             r   r   z#AsyncLogfireQueryClient.query_arrow  s     	Z 8'' % 
 
 [[$$X%5%56&!'!2K 7  	ZQRXYY	Z
 7sI   BA& BB)BB
B&	B /A;;B  BB
Bc                b   K   | j                  d||||       d{   }|j                  S 7 w)r   r   rv   Nr   ro   s         r   r   z!AsyncLogfireQueryClient.query_csv8  s@      '' % 
 
 }}
s   /-/c                   K   | j                  |||||      }| j                  j                  dd|i|       d {   }| j                  |       |S 7 wr   r   r   s	            r   rn   zAsyncLogfireQueryClient._queryL  s[      ((m]ES_`x>PY_``##H- as   9AAA)r=   r    r;   r    r:   r   r   r   )r?   r4   rW   r4   r   r   r   r   r   r   rT   r   )r   r   r   r   r   rA   r   r   rp   rs   r   r   rn   r   r   s   @r   r5   r5      s   ;
 <*	\\ \ 	\
  #\ 04*.*.	D,D (D (	D
 
D *.)-  ' '	
  
* *.)-  ' '	
  
* *.)-  ' '	
  
@ *.)-  ' '	
  
0 *.)- "^  '	
 '   
r   )%
__future__r   r   typesr   typingr   r   r   r	   r
   r   httpxr   r   r   r   httpx._clientr   ry   r~   rx   r   r   RuntimeErrorr   r   r   r(   r+   r.   r0   r2   r4   r7   r3   r5   r   r   r   <module>r      s   "   K KS<<( $-	, 		 	I  9 i  Cz"C+,C01!=gaj !=Hy08 yxy5kB yk  S
J
KQRRSs   C C$	CC$