
    ZTh'                    H   U d 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
mZmZ ddlZddlmZ ddl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  G d d      Z	 ee e	e ef   f   Z!de"d<    G d d      Z# G d d      Z$ G d d      Z% G d de
      Z& G d d      Z'y)a&  
Module containing utilities to construct a data query which is ultimately
passed to `sift_py.data.service.DataService.execute` to download telemetry.

This module also contains types that represent the result of a data query
which can be easily converted into a `pandas` data frame or series.
    )annotations)datetime)AnyDictListOptionalTuple	TypedDictUnioncastN)	Timestamp)NotRequired	TypeAlias)channel_fqn)to_timestamp_nanos)ChannelTimeSeries)_component_deprecation_warning)ChannelDataTypec                  v    e Zd ZU dZdZded<   ded<   ded<   ded	<   ded
<   ded<   def	 	 	 	 	 	 	 	 	 	 	 ddZy)	DataQuerya  
    A query that is meant to be passed to `sift_py.data.service.DataService.execute` to
    retrieve telemetry.

    - `asset_name`: The name of the asset to query telemetry for.
    - `start_time`: The start time of the time range of the data to request.
    - `end_time`: The end time of the time range of the data to request.
    - `sample_ms`:
        The sampling rate to use when retrieving data. The lower the sampling rate, the
        greater the data-fidelity. A sampling rate of `0` retrieves full-fidelity data.
    - `channels`:
        List of either `ChannelQuery` or `CalculatedChannelQuery`, but not both. Represents the
        channels to retrieve data from.
    i str
asset_namezpd.Timestamp
start_timeend_timeint	sample_ms	page_size1List[Union[ChannelQuery, CalculatedChannelQuery]]channelsr   c                    t        |      | _        t        |      | _        || _        || _        || _        | j                  j                  | _        y N)	r   r   r   r   r   r   	__class__DEFAULT_PAGE_SIZEr   )selfr   r   r   r   r   _s          W/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/data/query.py__init__zDataQuery.__init__2   sB     -Z8*84$" 99    N)r   r   r   4Union[pd.Timestamp, TimestampPb, datetime, str, int]r   r)   r   r   r   r   r%   r   )__name__
__module____qualname____doc__r#   __annotations__r'    r(   r&   r   r      sz      ONN?? ":: I: G	:
 D: : :r(   r   r   ChannelLookupInfoc                  <    e Zd ZU dZded<   d	dZd
dZddZddZy)DataQueryResultzI
    The result of a data query which can contain multiple channels.
    "Dict[str, List[ChannelTimeSeries]]_resultc                    || _         y r!   )r4   )r$   merged_channel_datas     r&   r'   zDataQueryResult.__init__R   s	    *r(   c                L    | j                  |      }t        |      dkD  r|d   S y)zL
        Like `channels` but returns a single `DataQueryResultSet`.
        r   N)r   len)r$   lookupresults      r&   channelzDataQueryResult.channelU   s*    
 v&v;?!9r(   c           	        g }|D ]t  }t        |t              r~| j                  j                  |      }|s2t	        |      dkD  rt        d| dt	        |       d      |d   }|j                  t        ||j                  |j                               t        t        t        t        f   |      \  }}| d|j                          }| j                  j                  |      }|st	        |      dk(  r8|d   }|j                  t        ||j                  |j                               +|D ]E  }|j                  |k(  s|j                  t        ||j                  |j                                t w |S )a8  
        Returns a `sift_py.data.channel.ChannelTimeSeries` given the `lookup` argument.
        If a `lookup` is a fully qualified name (FQN) `str` and there are multiple channels
        with the same FQN, this will raise a `ValueError`. In these situations, `lookup` must
        be a tuple where the first item is the channel FQN and the second the
        `sift_py.ingestion.channel.ChannelDataType`.

        If `lookup` is a tuple, then the channel data-type will be appended to the key referencing
        the `sift_py.data.channel.ChannelTimeSeries`.
           zAmbiguous lookup provided: 'z' is associated with z
 channels.r   
identifier
timestampsvalues.)
isinstancer   r4   getr8   
ValueErrorappendDataQueryResultSettime_columnvalue_columnr   r	   r   as_human_str	data_type)	r$   r9   r:   infotime_seriesseriesfqnrK   r?   s	            r&   r   zDataQueryResult.channelsa   s    ,. /	D$$"ll..t4"{#a'$6tf<QRUVaRbQccmn  %Q&#'#)#5#5%22 "&eC,@&A4!HY #uAi&<&<&>%?@
"ll..s3"{#q((^FMM*'1'-'9'9#)#6#6 ) 	F''94.+5+1+=+='-':': 	M/	b r(   c           	        g }| j                   j                         D ]  \  }}t        |      dkD  rZ|D ]T  }|j                  j	                         }| d| }|j                  t        ||j                  |j                               V n|D ]3  }|j                  t        ||j                  |j                               5  |S )z+
        Returns all channel data.
        r=   rB   r>   )	r4   itemsr8   rK   rJ   rF   rG   rH   rI   )r$   r:   rO   rM   rN   human_data_typefqn_extendeds          r&   all_channelszDataQueryResult.all_channels   s    
  $ 2 2 4 	C;!#) 
F&,&6&6&C&C&EO&)U!O+<#=LMM*'3'-'9'9#)#6#6	
 % &#&#)#5#5%22	0 r(   N)r6   r3   )r9   r0   returnzOptional[DataQueryResultSet])r9   r0   rU   List[DataQueryResultSet])rU   rV   )	r*   r+   r,   r-   r.   r'   r;   r   rT   r/   r(   r&   r2   r2   K   s$     0/+
?Br(   r2   c                  d    e Zd ZU dZded<   ded<   ded<   ddZddd
ZdddZ	 	 d	 	 	 	 	 ddZy	)rG   z
    Represents time series data for a single channel. Can easily be converted into a `pandas` data frame like so:

    ```python
    pd.DataFrame(data_query_result_set.all_columns())
    ```

    r   r?   List[pd.Timestamp]r@   	List[Any]rA   c                .    || _         || _        || _        y r!   r>   )r$   r?   r@   rA   s       r&   r'   zDataQueryResultSet.__init__   s    $$r(   Nc                R    || j                   | j                  iS || j                  iS )z
        Returns a single key-value pair dictionary meant to represent the value column of the data-set.
        `column_name` can be used to override the name of the column.
        )r?   rA   r$   column_names     r&   rI   zDataQueryResultSet.value_column   s,     OOT[[11--r(   c                >    |d| j                   iS || j                   iS )z
        Returns a single key-value pair dictionary meant to represent the time column of the data-set.
        `column_name` can be used to override the name of the column.
        time)r@   r\   s     r&   rH   zDataQueryResultSet.time_column   s(    
 DOO,,11r(   c                h    | j                  |      }|j                  | j                  |             |S )zd
        Returns both the time and value columns with options to override the column names.
        )rH   updaterI   )r$   time_column_namevalue_column_namecolss       r&   columnszDataQueryResultSet.columns   s2      01D%%&789r(   )r?   r   r@   rX   rA   rY   r!   )r]   Optional[str]rU   Dict[str, List[Any]]NN)rb   rf   rc   rf   rU   rg   )	r*   r+   r,   r-   r.   r'   rI   rH   re   r/   r(   r&   rG   rG      sR     O""
	.2 +/+/' ) 
	r(   rG   c                  P    e Zd ZU dZded<   ded<   ded<   	 	 d
	 	 	 	 	 ddZdd	Zy)ChannelQueryzW
    Represents a single channel to include in the `sift_py.data.query.DataQuery`.
    r   channel_namerf   	componentrun_nameNc                p    || _         |&t                t        | j                   |      | _         || _        y )N)namerl   )rk   r   r   rm   )r$   rk   rl   rm   s       r&   r'   zChannelQuery.__init__  s4     ) *, +1B1Bi XD r(   c                ,    t        | j                        S r!   )r   rk   )r$   s    r&   rO   zChannelQuery.fqn  s    4,,--r(   rh   )rk   r   rl   rf   rm   rf   )rU   r   )r*   r+   r,   r-   r.   r'   rO   r/   r(   r&   rj   rj      sI     
 $("&	
!
! !
!  	
!.r(   rj   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y)	ExpressionChannelReferencer   	referencerk   zNotRequired[str]rl   zNotRequired[ChannelDataType]rK   N)r*   r+   r,   r.   r/   r(   r&   rr   rr     s    N++r(   rr   c                  T    e Zd ZU dZded<   ded<   ded<   ded<   	 d	 	 	 	 	 	 	 dd
Zy	)CalculatedChannelQueryzb
    Represents a single calculated channel to include in the `sift_py.data.query.DataQuery`.
    r   channel_key
expression List[ExpressionChannelReference]expression_channel_referencesrf   rm   Nc                <    || _         || _        || _        || _        y r!   )rv   rm   rw   ry   )r$   rv   rw   ry   rm   s        r&   r'   zCalculatedChannelQuery.__init__$  s"     ' $-J*r(   r!   )rv   r   rw   r   ry   rx   rm   rf   )r*   r+   r,   r-   r.   r'   r/   r(   r&   ru   ru     sV     O#CC #'
K
K 
K (H	
K
  
Kr(   ru   )(r-   
__future__r   r   typingr   r   r   r   r	   r
   r   r   pandaspdgoogle.protobuf.timestamp_pb2r   TimestampPbtyping_extensionsr   r   sift_py._internal.channelr   sift_py._internal.timer   sift_py.data._channelr   sift_py.errorr   sift_py.ingestion.channelr   r   r   r0   r.   r2   rG   rj   rr   ru   r/   r(   r&   <module>r      s    #  K K K  B 4 1 5 3 8 5(: (:V  %S%_0D*E%EF 9 Fv vr3 3l. .2, ,K Kr(   