
    ZThK"                        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
mZ d dlmZmZmZmZmZ erd dlmZ d d	lmZ  G d
 de      Zy)    )annotationsN)TYPE_CHECKING)RunsLowLevelClient)ResourceBase)Run	RunUpdate)containsequalsequals_nullmatchnot_)datetime)
SiftClientc                      e Zd ZdZd fdZ	 	 	 	 ddZddddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	Zdd
Z		 	 	 	 ddZ
	 	 	 	 ddZ	 	 	 	 	 	 ddZddZ xZS )RunsAPIAsynca  High-level API for interacting with runs.

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

    All methods in this class use the Run class from the low-level wrapper, which is a user-friendly
    representation of a run using standard Python data structures and types.
    c                n    t         |   |       t        | j                  j                        | _        y)z`Initialize the RunsAPI.

        Args:
            sift_client: The Sift client to use.
        )grpc_clientN)super__init__r   clientr   _low_level_client)selfsift_client	__class__s     _/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_client/resources/runs.pyr   zRunsAPIAsync.__init__   s)     	%!3@W@W!X    c               x   K   | j                   j                  |       d{   }| j                  |      S 7 w)zpGet a Run.

        Args:
            run_id: The ID of the run.

        Returns:
            The Run.
        run_idN)r   get_run_apply_client_to_instance)r   r   runs      r   getzRunsAPIAsync.get$   s;      **22&2AA--c22 Bs    :8:NF)namename_contains
name_regexdescriptiondescription_containsduration_seconds
client_keyasset_id
asset_namecreated_by_user_id
is_stoppedinclude_archivedorder_bylimitc                 K   g }|r|j                  t        d|             na|r|j                  t        d|             nC|rAt        |t        j
                        r|j                  }|j                  t        d|             |r|j                  t        d|             n|r|j                  t        d|             |r|j                  t        d|             |r|j                  t        d|             |r|j                  t        d|             |	r|j                  t        d|	             |
r|j                  t        d|
             |#|j                  t        t        d	                   |s|j                  t        d
d             |rdj                  |      nd}| j                  j                  |||       d{   }| j                  |      S 7 w)a  List runs with optional filtering.

        Args:
            name: Exact name of the run.
            name_contains: Partial name of the run.
            name_regex: Regular expression string to filter runs by name.
            description: Exact description of the run.
            description_contains: Partial description of the run.
            duration_seconds: Duration of the run in seconds.
            client_key: Client key to filter by.
            asset_id: Asset ID to filter by.
            asset_name: Asset name to filter by.
            created_by_user_id: User ID who created the run.
            is_stopped: Whether the run is stopped.
            include_archived: Whether to include archived runs.
            order_by: How to order the retrieved runs.
            limit: How many runs to retrieve. If None, retrieves all matches.

        Returns:
            A list of Runs that matches the filter.
        r$   r'   durationr*   r+   r,   r-   N	stop_timearchived_datez && )query_filterr0   max_results)appendr
   r	   
isinstancerePatternpatternr   r   r   joinr   list_all_runs_apply_client_to_instances)r   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   filter_partsr6   runss                     r   list_zRunsAPIAsync.list_4   s    P vt 45 ?@*bjj1'//
fj 9:}k BC!8L MNz3C DE|Z @Az8 <=|Z @A';=O PQ![%= >? =>4@v{{<0d++99% : 
 

 ..t44
s   GG G	G c                   K    | j                   di | d{   }t        |      dkD  rt        d      t        |      dk(  r|d   S y7 1w)a  Find a single run matching the given query. Takes the same arguments as `list`. If more than one run is found,
        raises an error.

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

        Returns:
            The Run found or None.
        N   zMultiple runs found for queryr    )rB   len
ValueError)r   kwargsrA   s      r   findzRunsAPIAsync.find   sQ       TZZ)&))t9q=<==Y!^7N *s   AA
2Ac	           
        K   | j                   j                  ||||||||       d{   }	| j                  |	      S 7 w)a  Create a new run.

        Args:
            name: The name of the run.
            description: The description of the run.
            tags: Tags to associate with the run.
            start_time: The start time of the run.
            stop_time: The stop time of the run.
            organization_id: The organization ID.
            client_key: A unique client key for the run.
            metadata: Metadata values for the run.

        Returns:
            The created Run.
        )r$   r'   tags
start_timer4   organization_idr*   metadataN)r   
create_runr!   )
r   r$   r'   rK   rL   r4   rM   r*   rN   created_runs
             r   createzRunsAPIAsync.create   sX     4 !22==#!+! > 	
 	
 --k::	
s   'A?Ac                <  K   t        |t              r| j                  |       d{   }t        |t              rt	        j
                  |      }|j                  |_        | j                  j                  ||       d{   }| j                  |      S 7 o7 w)zUpdate a Run.

        Args:
            run: The Run or run ID to update.
            update: Updates to apply to the Run.

        Returns:
            The updated Run.
        r   N)r9   strr#   dictr   model_validateid_resource_idr   
update_runr!   )r   r"   updateupdated_runs       r   rY   zRunsAPIAsync.update   s      c3,,Cfd#--f5F WW 22==c6JJ--k:: - Ks"   &BBABBBBc                  K   t        |t              r|j                  n|}t        |t              st	        dt        |             | j                  j                  |       d{    y7 w)zUArchive a run.

        Args:
            run: The Run or run ID to archive.
        zrun_id must be a string not r   N)r9   r   rV   rS   	TypeErrortyper   archive_runr   r"   r   s      r   archivezRunsAPIAsync.archive   sZ      'sC0c&#&:4<.IJJ$$000???s   A%A/'A-(A/c                  K   t        |t              r|j                  n|}| j                  j	                  |xs d       d{    y7 wz|Stop a run by setting its stop time to the current time.

        Args:
            run: The Run or run ID to stop.
         r   Nr9   r   rV   r   stop_runr_   s      r   stopzRunsAPIAsync.stop   s=      'sC0c$$--V\r-BBBs   AAA
Ac                   K   t        |t              r|j                  xs dn|}| j                  j	                  ||       d{    y7 w)zAssociate assets with a run for automatic data ingestion.

        Args:
            run: The Run or run ID.
            asset_names: List of asset names to associate.
        rc   )r   asset_namesN)r9   r   rV   r   +create_automatic_run_association_for_assets)r   r"   rh   r   s       r   'create_automatic_association_for_assetsz4RunsAPIAsync.create_automatic_association_for_assets   sI      #-S#"6BC$$PP{ Q 
 	
 	
s   AAAAc                   K   t        |t              r|j                  xs dn|}| j                  j	                  |xs d       d{    y7 wrb   rd   r_   s      r   re   zRunsAPIAsync.stop_run  sA      #-S#"6BC$$--V\r-BBBs   AAA	A)r   r   )r   rS   returnr   )r$   
str | Noner%   rm   r&   zstr | re.Pattern | Noner'   rm   r(   rm   r)   
int | Noner*   rm   r+   rm   r,   rm   r-   rm   r.   zbool | Noner/   boolr0   rm   r1   rn   rl   z	list[Run])rl   z
Run | None)NNNNNN)r$   rS   r'   rS   rK   zlist[str] | NonerL   datetime | Noner4   rp   rM   rm   r*   rm   rN   z$dict[str, str | float | bool] | Nonerl   r   )r"   	str | RunrY   zRunUpdate | dictrl   r   )r"   rq   rl   None)r"   rq   rh   z	list[str]rl   rr   )__name__
__module____qualname____doc__r   r#   rB   rI   rQ   rY   r`   rf   rj   re   __classcell__)r   s   @r   r   r      s   Y3 3 
	3&  $(.2"&+/'+!%#!%)-"&!&# !T5 T5 "	T5
 ,T5  T5 )T5 %T5 T5 T5 T5 'T5  T5 T5 T5  !T5" 
#T5l* "&&*%)&*!%9=$;$; $; 	$;
 $$; #$; $$; $; 7$; 
$;L;(@ @ 
	@C C 
	C

 
 
	
 Cr   r   )
__future__r   r:   typingr   -sift_client._internal.low_level_wrappers.runsr   sift_client.resources._baser   sift_client.sift_types.runr   r   sift_client.util.cel_utilsr	   r
   r   r   r   r   sift_client.clientr   r   rE   r   r   <module>r      s6    " 	   L 4 5 Q Q!-wC< wCr   