
    ZThg;                        d dl mZ d dlmZmZ d dlmZ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rd dlZd d	lmZ d d
lmZ  G d de
      Zy)    )annotations)datetimetimezone)TYPE_CHECKINGAny) CalculatedChannelsLowLevelClient)ResourceBase)CalculatedChannelCalculatedChannelUpdate)	cel_utilsN)
SiftClient)ChannelReferencec                      e Zd ZdZd fdZdddd	 	 	 	 	 	 	 ddZddddddddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdd	Zd
dddddd
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdd	 	 	 	 	 	 	 ddZ	ddZ
ddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )CalculatedChannelsAPIAsynca  High-level API for interacting with calculated channels.

    This class provides a Pythonic, notebook-friendly interface for interacting with the CalculatedChannelsAPI.
    It handles automatic handling of gRPC services, seamless type conversion, and clear error handling.

    All methods in this class use the CalculatedChannel class from the low-level wrapper, which is a user-friendly
    representation of a calculated channel using standard Python data structures and types.
    c                n    t         |   |       t        | j                  j                        | _        y)znInitialize the CalculatedChannelsAPI.

        Args:
            sift_client: The Sift client to use.
        )grpc_clientN)super__init__r   clientr   _low_level_client)selfsift_client	__class__s     n/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_client/resources/calculated_channels.pyr   z#CalculatedChannelsAPIAsync.__init__!   s,     	%!A//"
    Ncalculated_channel_id
client_keyorganization_idc                  K   |s|st        d      | j                  j                  |||       d{   }| j                  |      S 7 w)a  Get a Calculated Channel.

        Args:
            calculated_channel_id: The ID of the calculated channel.
            client_key: The client key of the calculated channel.
            organization_id: The organization ID (required if using client_key and user belongs to multiple organizations).

        Returns:
            The CalculatedChannel.

        Raises:
            ValueError: If neither calculated_channel_id nor client_key is provided.
        z;Either calculated_channel_id or client_key must be providedr   N)
ValueErrorr   get_calculated_channel_apply_client_to_instance)r   r   r   r   calculated_channels        r   getzCalculatedChannelsAPIAsync.get,   s]     ( %ZZ[[#'#9#9#P#P"7!+ $Q $
 
 --.@AA
s   1AA	AF)namename_contains
name_regexcreated_aftercreated_beforemodified_aftermodified_before
created_bymodified_byr   asset_id
asset_nametag_idtag_nameversioninclude_archivedfilter_queryorder_bylimitr   c                 K   |sEg }|r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j
                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j
                  d|             |r%|j                  t        j                  d|             |rt        |	rt        |
r%|j                  t        j                  d|
             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d	|             |s$|j                  t        j                  d
             t        j                  | }| j                  j                  ||||       d{   }| j                  |      S 7 w)a  List calculated channels with optional filtering.

        Args:
            name: Exact name of the calculated channel.
            name_contains: Partial name of the calculated channel.
            name_regex: Regular expression string to filter calculated channels by name.
            created_after: Created after this date.
            created_before: Created before this date.
            modified_after: Modified after this date.
            modified_before: Modified before this date.
            created_by: Calculated channels created by this user.
            modified_by: Calculated channels last modified by this user.
            client_key: The client key of the calculated channel.
            asset_id: The asset ID associated with the calculated channel.
            asset_name: The asset name associated with the calculated channel.
            tag_id: The tag ID associated with the calculated channel.
            tag_name: The tag name associated with the calculated channel.
            version: The version of the calculated channel.
            include_archived: Include archived calculated channels.
            filter_query: Explicit CEL query to filter calculated channels.
            order_by: How to order the retrieved calculated channels.
            limit: How many calculated channels to retrieve. If None, retrieves all matches.
            organization_id: The organization ID (required if user belongs to multiple organizations).

        Returns:
            A list of CalculatedChannels that matches the filter.
        r&   created_datemodified_dater   r/   r0   r1   r2   r3   archived_date)query_filterr6   max_resultsr   N)appendcelequalscontainsmatchgreater_than	less_thanNotImplementedErrorequals_nulland_r   list_all_calculated_channels_apply_client_to_instances)r   r&   r'   r(   r)   r*   r+   r,   r-   r.   r   r/   r0   r1   r2   r3   r4   r5   r6   r7   r   filterscalculated_channelss                          r   list_z CalculatedChannelsAPIAsync.list_K   s    f Gszz&$78s||FMBCsyy<=s//NOs}}^^LMs//PQs}}_oNO))))szz,
CDszz*h?@szz,
CDszz(F;<szz*h?@szz)W=>#s?@88W-L$($:$:$W$W%+	 %X %
 
 ../BCC
s   I+J-J.Jc                   K    | j                   di | d{   }t        |      dkD  rt        d| d      t        |      dk(  r|d   S y7 5w)ae  Find a single calculated channel matching the given query. Takes the same arguments as `list` but handles checking for multiple matches.
        Will raise an error if multiple calculated channels are found.

        Args:
            **kwargs: Keyword arguments to pass to `list`.

        Returns:
            The CalculatedChannel found or None.
        N   z.Multiple calculated channels found for query: z8. Use `list` to handle all matching calculated channels.r    )rL   lenr!   )r   kwargsrK   s      r   findzCalculatedChannelsAPIAsync.find   sq      %/DJJ$8$88"#a'@ II I  $%*&q)) 9s   AA6A )descriptionunitsr   	asset_idstag_ids
all_assets
user_notesc       
           K   |	r|s|rt        d      |	s|s|st        d      | j                  j                  ||	||||||
||
       d{   \  }}| j                  |      S 7 w)a  Create a calculated channel.

        Args:
            name: The name of the calculated channel.
            expression: The expression to calculate the value of the calculated channel.
            channel_references: A list of channel references that are used in the expression.
            description: The description of the calculated channel.
            units: The units of the calculated channel.
            client_key: A user-defined unique identifier for the calculated channel.
            asset_ids: A list of asset IDs to make the calculation available for.
            tag_ids: A list of tag IDs to make the calculation available for.
            all_assets: A flag that, when set to True, associates the calculated channel with all assets.
            user_notes: User notes for the calculated channel.

        Returns:
            The created CalculatedChannel.

        Raises:
            ValueError: If asset configuration is invalid.
        z4Cannot specify both all_assets and asset_ids/tag_idsz@Must specify either all_assets=True or provide asset_ids/tag_ids)
r&   rX   rV   rW   
expressionchannel_referencesrT   rY   rU   r   N)r!   r   create_calculated_channelr#   )r   r&   r[   r\   rT   rU   r   rV   rW   rX   rY   r$   inapplicable_assetss                r   createz!CalculatedChannelsAPIAsync.create   s     F 9STT)G_``
 ((BB!!1#!! C 
 
	
 --.@AA
s   AA(A&A()rY   c                 K   t        |t              r|j                  n|}t        |t              rt	        j
                  |      }||_        | j                  j                  ||       d{   \  }}| j                  |      S 7 w)aC  Update a Calculated Channel.

        Args:
            calculated_channel: The CalculatedChannel or id of the CalculatedChannel to update.
            update: Updates to apply to the CalculatedChannel.
            user_notes: User notes for the update.

        Returns:
            The updated CalculatedChannel.
        )updaterY   N)

isinstancer
   id_dictr   model_validateresource_idr   update_calculated_channelr#   )r   r$   ra   rY   r   updated_calculated_channelr^   s          r   ra   z!CalculatedChannelsAPIAsync.update   s     & ,.?@ ""# 	 fd#,;;FCF2
 ((BBj C 
 
	
&
 --.HII	
s   A+B-B.Bc                  K   t        t        j                  t        j                              }| j                  ||       d{    y7 w)zArchive a Calculated Channel.)tz)r;   )r$   ra   N)r   r   nowr   utcra   )r   r$   ra   s      r   archivez"CalculatedChannelsAPIAsync.archive  s9     (",,(,,7
 kk-?kOOOs   AAAA)r   r   r   r&   r'   r(   r/   r0   r1   r2   r3   r4   r6   r7   c                 K   t        d ||||fD              dk7  rt        d      |r|rt        d      |	r|
rt        d      g }|r%|j                  t        j                  d|             |r%|j                  t        j
                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |r%|j                  t        j                  d|             |	r%|j                  t        j                  d	|	             |
r%|j                  t        j                  d
|
             |r%|j                  t        j                  d|             |s$|j                  t        j                  d             t        j                  | }| j                  j                  ||||||       d{   }| j                  |      S 7 w)a  List versions of a calculated channel.

        Args:
            calculated_channel_id: The ID of the calculated channel.
            client_key: The client key of the calculated channel.
            name: The name of the calculated channel.
            name_contains: The name of the calculated channel.
            name_regex: The name of the calculated channel.
            asset_id: The asset ID of the calculated channel.
            asset_name: The asset name of the calculated channel.
            tag_id: The tag ID of the calculated channel.
            tag_name: The tag name of the calculated channel.
            version: The version of the calculated channel.
            include_archived: Whether to include archived calculated channels.
            organization_id: The organization ID. Required if your user belongs to multiple organizations.
            order_by: The field to order by.
            limit: How many versions to retrieve. If None, retrieves all matches.

        Returns:
            A list of CalculatedChannel versions.

        Raises:
            ValueError: If neither calculated_channel_id nor client_key is provided.
        c              3  2   K   | ]  }t        |        y w)N)bool).0vs     r   	<genexpr>z;CalculatedChannelsAPIAsync.list_versions.<locals>.<genexpr>N  s     Y1tAwYs   rN   zYExactly one of calculated_channel_id, name, name_contains, or name_regex must be providedz+Cannot specify both asset_id and asset_namez'Cannot specify both tag_id and tag_namer&   r/   r0   r1   r2   r3   r;   )r   r   r   r<   r6   r7   N)sumr!   r>   r?   r@   rA   rB   rF   rG   r   $list_all_calculated_channel_versionsrI   )r   r   r   r   r&   r'   r(   r/   r0   r1   r2   r3   r4   r6   r7   filter_query_partsr5   versionss                     r   list_versionsz(CalculatedChannelsAPIAsync.list_versions$  s    T Y!6mZ XYY]^^k  
JKKhFGG%%cjj&>?%%cll6=&IJ%%cii
&CD%%cjjX&FG%%cjjz&JK%%cjj6&BC%%cjjX&FG%%cjjG&DE%%cooo&FGxx!34//TT"7!+% U 
 
 ..x88
s   GG3G1G3)r   r   )r   
str | Noner   ry   r   ry   returnr
   )*r&   ry   r'   ry   r(   str | re.Pattern | Noner)   datetime | Noner*   r|   r+   r|   r,   r|   r-   
Any | Noner.   r}   r   ry   r/   ry   r0   ry   r1   ry   r2   ry   r3   
int | Noner4   rp   r5   ry   r6   ry   r7   r~   r   ry   rz   list[CalculatedChannel])rz   zCalculatedChannel | None)r&   strr[   r   r\   zlist[ChannelReference]rT   r   rU   ry   r   ry   rV   list[str] | NonerW   r   rX   rp   rY   r   rz   r
   )r$   str | CalculatedChannelra   zCalculatedChannelUpdate | dictrY   ry   rz   r
   )r$   r   rz   None)r   ry   r   ry   r   ry   r&   ry   r'   ry   r(   r{   r/   ry   r0   ry   r1   ry   r2   ry   r3   r~   r4   rp   r6   ry   r7   r~   rz   r   )__name__
__module____qualname____doc__r   r%   rL   rR   r_   ra   rm   rx   __classcell__)r   s   @r   r   r      s>   	
 -1!%&*B  *B 	B
 $B 
BD  $(.2)-*.*.+/!%"&!%#!%!#"!&#'# &*-]D ]D "	]D
 ,]D ']D (]D (]D )]D ]D  ]D ]D ]D ]D ]D  !]D" #]D$ %]D& !']D( )]D* +]D, $-]D. 
!/]D~4  !%&*$( 8B 8B 	8B
 38B 8B 8B 8B $8B "8B 8B 8B 
8B~ "&#J 4#J /	#J
 #J 
#JJP -1!%&*$(.2#!%!#"!&# !Q9  *Q9 	Q9
 $Q9 Q9 "Q9 ,Q9 Q9 Q9 Q9 Q9 Q9 Q9 Q9  !Q9" 
!#Q9r   r   )
__future__r   r   r   typingr   r   <sift_client._internal.low_level_wrappers.calculated_channelsr   sift_client.resources._baser	   )sift_client.sift_types.calculated_channelr
   r   sift_client.utilr   r?   resift_client.clientr   sift_client.sift_types.channelr   r   rO   r   r   <module>r      s<    " ' % 5 .-?^9 ^9r   