
    ZTh$F                       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 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mZmZmZmZmZmZmZmZmZmZm Z 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+ d dl,m-Z- d dl.m/Z/  G d d      Z0y)    )annotations)Path)AnyDictListOptionalTupleUnioncast)	FieldMask)	Timestamp)Asset)AssetServiceStub)CalculatedChannel)CalculatedChannelAbstractChannelReference#CalculatedChannelAssetConfigurationCalculatedChannelConfiguration#CalculatedChannelQueryConfiguration!CalculatedChannelValidationResultCreateCalculatedChannelRequestCreateCalculatedChannelResponseGetCalculatedChannelRequestGetCalculatedChannelResponseListCalculatedChannelsRequestListCalculatedChannelsResponse$ListCalculatedChannelVersionsRequestUpdateCalculatedChannelRequest)CalculatedChannelServiceStub)list_assets_impl)CalculatedChannelConfigCalculatedChannelUpdate)SiftChannel)_channel_references_from_dicts)load_calculated_channelsc                     e Zd ZU dZded<   ded<   ddZ	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 dd	Z	 	 	 	 d	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 ddZ
	 d	 	 	 	 	 	 	 ddZ	 	 	 	 ddZe	 	 	 	 dd       Z	 d	 	 	 	 	 ddZy)CalculatedChannelServicez
    A service for managing reusable Calculated Channels. Allows for creating, updating, and retrieving Calculated Channels.
    r    _calculated_channel_service_stubr   _asset_service_stubc                D    t        |      | _        t        |      | _        y N)r   r'   r   r(   )selfchannels     h/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/calculated_channels/service.py__init__z!CalculatedChannelService.__init__-   s    0LW0U-#3G#<     Nc                F    | j                  | j                  ||            S )z
        Get a `CalculatedChannel`.  See `Sift docs`_
        for more information on available arguments.

        .. _Sift docs: https://docs.siftstack.com/docs/api/grpc/protocol-buffers/calculated_channels
        calculated_channel_id
client_key)_calculated_channel_to_config_get_calculated_channel)r+   r2   r3   s      r-   get_calculated_channelz/CalculatedChannelService.get_calculated_channel1   s/     11((&;
 ) 
 	
r/   c                    |s|st        d      |rt        |      }nt        |      }t        t        | j                  j                  |            }t        t        |j                        S )N(Must provide either `id` or `client_key`)r2   r3   )
ValueErrorr   r   r   r'   GetCalculatedChannelr   calculated_channel)r+   r2   r3   reqress        r-   r5   z0CalculatedChannelService._get_calculated_channel@   sm     %ZGHH -&;C .%C (11FFsK
 %s'='=>>r/   c           	     6   i }|||d<   |||d<   |||d<   |||d<   t        di |}t        t        | j                  j	                  |            }|j
                  D cg c]!  }| j                  t        t        |            # c}|j                  fS c c}w )a:  
        List available Calculated Channels. See `Sift docs`_
        for more information on available arguments.

        Returns a tuple of a list of `CalculatedChannel` objects and a next page token.

        .. _Sift docs: https://docs.siftstack.com/docs/api/grpc/protocol-buffers/calculated_channels
        	page_size
page_tokenfilterorder_by )	r   r   r   r'   ListCalculatedChannelscalculated_channelsr4   r   next_page_token)	r+   r@   rA   rB   rC   request_kwargsr=   respchans	            r-   list_calculated_channelsz1CalculatedChannelService.list_calculated_channelsU   s     *, *3N;'!+5N<('-N8$)1N:&+=n=*11HHM
 !44 2248I43PQ   
 	
s   !&Bc           	     R   |s|st        d      i }|||d<   n||d<   |||d<   |||d<   |||d<   |||d<   t        di |}| j                  j                  |      }	|	j                  D 
cg c]!  }
| j                  t        t        |
            # c}
|	j                  fS c c}
w )	a;  
        List versions of Calculated Channel. See `Sift docs`_
        for more information on available arguments.

        Returns a tuple of a list of `CalculatedChannel` objects and a next page token.

        .. _Sift docs: https://docs.siftstack.com/docs/api/grpc/protocol-buffers/calculated_channels
        r8   r2   r3   r@   rA   rB   rC   rD   )	r:   r   r'   ListCalculatedChannelVersionscalculated_channel_versionsr4   r   r   rG   )r+   r2   r3   r@   rA   rB   rC   rH   r=   rI   rJ   s              r-    list_calculated_channel_versionsz9CalculatedChannelService.list_calculated_channel_versions{   s    " %ZGHH)+ ,6KN23+5N<( *3N;'!+5N<('-N8$)1N:&2D^D44RRSVW !<< 2248I43PQ   
 	
s   /&B$c                   t        |j                  |j                  sat        j                  |j                  r4| j	                  |j                        D cg c]  }|j
                   c}nd|j                        nd      }t        t        j                  |j                  |j                  D cg c]  }t        d	i | c}            }t        ||      }t        |j                  |j                  |j                   |j"                  |      }t%        t&        | j(                  j+                  |            }| j-                  t%        t.        |j0                              t%        t2        |j4                        fS c c}w c c}w )
z
        Create a `CalculatedChannel` from a `CalculatedChannelConfig`. See
        `sift_py.calculated_channels.config.CalculatedChannelConfig` for more information on available
        fields to configure.
        namesN	asset_idstag_ids
all_assets	selection
expressionexpression_channel_referencesselasset_configurationquery_configuration)namedescriptionunitsr3    calculated_channel_configurationrD   )r   rW   AssetSelectionasset_names_get_assetsasset_id	tag_namesr   SelrZ   channel_referencesr   r   r   ra   rb   rc   r3   r   r   r'   CreateCalculatedChannelr4   r   r<   r   inapplicable_assets)	r+   configassetr_   chr`   rd   r=   rI   s	            r-   create_calculated_channelz2CalculatedChannelService.create_calculated_channel   sp    B(( $$ :HH%% 8<7G7GfN`N`7G7abe5>>b((	 

 B377!,, %77/ >CC/
 ,J 3I\,
( -**,,((-M
 +11II#N
 11"D$;$;<
143K3KLM 	M? c/s   F2Fc                   | j                  |j                  |j                        }i }d|v r|d   |d<   d|v r|d   |d<   d|v r|d   |d<   d|v sd|v r|j                  d      xs* |j                  j
                  j                  j                  }t        |j                  d      xs g       }|r|D cg c]  }t        di | c}n)|j                  j
                  j                  j                  }	t        t        j                  ||	            |d	<   d
|v s	d|v sd|v r$d
|v r9| j                  |j                  d
            D 
cg c]  }
|
j                   c}
n)|j                  j                  j                   j"                  }d|v r|j                  d      n)|j                  j                  j                   j$                  }d|v r|j                  d      t'        d      d|v r|j                  d      n|j                  j                  j(                  }t+        ||rdnt+        j,                  ||            |d<   d|v r&t/               }|j1                          |d   sdn||d<   t3        |j                  |j                  d|j4                        |j                  d|j6                        |j                  d|j8                        t;        |j                  d|j                  j                        |j                  d	|j                  j
                              |j                  d|j<                              }t?        tA        |jC                                     }tE        |||      }| jF                  jI                  |      }| jK                  tM        t2        |jN                              tM        tP        |jR                        fS c c}w c c}
w )a&  
        Revise a `CalculatedChannel` from a `CalculatedChannelUpdate`.  See
        `sift_py.calculated_channels.config.CalculatedChannelUpdate` for more information on available
        fields to update.

        `revision_notes` may be provided to document the reason for revision.

        r1   ra   rb   rc   rZ   rk   rY   r\   r`   rf   ri   rW   rQ   Nz_Modifying `tag_names` (other than removing them by setting to None) is not currently supported.rS   rV   r_   archivedarchived_dater^   )r2   ra   rb   rc   rd   rt   )paths)r<   update_mask
user_notesrD   )*r5   r2   r3   getrd   r`   r]   rZ   r#   r   r[   r   rj   rg   rh   r_   rX   rT   rU   NotImplementedErrorrW   r   re   r   GetCurrentTimer   ra   rb   rc   r   rt   r   listkeysr   r'   UpdateCalculatedChannelr4   r   r<   r   rm   )r+   calculated_channel_configupdatesupdate_notesr<   
update_maprZ   channel_reference_dictsrp   rk   ro   rT   rU   rW   tschannel_updaterrv   r=   rI   s                      r-   update_calculated_channelz2CalculatedChannelService.update_calculated_channel   s    "99";"Q"Q0;; : 

 &(
W!(JvG#(/(>J}%g")'"2Jw7"&:g&EL) j%FFZZ^^ii  'E017R'#
 + LccR:@R@c'HH\\``~~ 
 1T7;;)2D1J,- G#{g'=QXAX !G+ .2-=-=GKKP]D^-=-_`E`'HH\\ffpp  ') K('HH\\ffnn  g%'++k*B*N)u   7* L)'HH\\gg 
 1T% 8GG'	1J,-  B6=j6I$rJ'+"4"J"J(:(?(?@"}6H6T6TU..*<*B*BC-K$.NN)&GG[[% %/NN)&GG[[%	. %..:L:Z:Z[
"  d:??+<&=>,.KT`
 44LLSQ11"D$;$;<
143K3KLM 	MM d as   7OOc                   t        t        |t              r|n|g      }g }|D ]  }|j                  .	 | j	                  |j                        }|j
                  |_        |j
                  6i }|j                  |j                  |d<   |j                  |j                  |d<   |j                  t        |j                  t              |d<   |j                  |j                  |d<   |j                  |j                  |d<   |j                  $t        |j                  t        t                 |d<   |j                   $t        |j                   t        t                 |d<   |j"                  |j"                  |d	<   |j%                  | j'                  ||
             |j%                  | j)                  |              |S # t        $ r Y uw xY w)zQ
        Creates or updates calculated channel from provided yaml files.
        r9   ra   rb   rc   rZ   rk   rf   ri   rW   )r~   r   )rn   )r$   
isinstancer{   r3   r6   r2   	Exceptionra   rb   rc   r   strrZ   rk   rf   r   ri   rW   appendr   rq   )r+   ru   calculated_channel_configscreated_or_updated_configsrn   found_channelr   s          r-   -create_or_update_calculated_channel_from_yamlzFCalculatedChannelService.create_or_update_calculated_channel_from_yamlD  s    &>t,E5'&
"
  	# 1 !	aF  ,$($?$?6K\K\$?$]M3@3V3VF0 ++735;;*&,kkGFO%%1-3-?-?GM*<<+'+FLL#'>GG$$$0,2,=,=GL),,84:4M4MG01%%1-1&2D2Dd3i-PGM*##/+/0@0@$s)+LGK($$0,2,=,=GL)*112228' 3  +11$2P2PX^2P2_`C!	aD *); ! s   -G

	GGc                4   t        | j                  | j                  | j                  | j                  j
                  j                  j                  | j                  j
                  j                  j                  D cg c]  }|j                  |j                  d c}| j                  | j                  | j                  j                  j                  s8| j                  j                  j                  j                   D cg c]  }| c}nd | j                  j                  j                  s8| j                  j                  j                  j"                  D cg c]  }| c}nd | j                  j                  j                  
      S c c}w c c}w c c}w )N)channel_referencechannel_identifier)
r2   ra   rb   rZ   rk   rc   r3   rf   ri   rW   )r    r2   ra   rb   rd   r`   r]   rZ   r[   r   r   rc   r3   r_   rW   rX   rT   rU   )r<   refrh   tag_ids       r-   r4   z6CalculatedChannelService._calculated_channel_to_configt  sc    '"4"J"J#((*66)JJ^^bbmm .NNbbff  E  E 
  *-)>)>*-*@*@  %**)44
 &FFZZee !3 S S g g q q { { 
 
 &FFZZee 1QQeeooww 
 )JJ^^ii5
 	

 s   < F 	F	Fc                0    t        | j                  ||      S r*   )r   r(   )r+   rR   idss      r-   rg   z$CalculatedChannelService._get_assets  s       8 8%EEr/   )r,   r"   )NN)r2   Optional[str]r3   r   returnr    )r2   r   r3   r   r   r   )NNNN)
r@   Optional[int]rA   r   rB   r   rC   r   r   )Tuple[List[CalculatedChannelConfig], str])NNNNNN)r2   r   r3   r   r@   r   rA   r   rB   r   rC   r   r   r   )rn   r    r   ATuple[CalculatedChannelConfig, CalculatedChannelValidationResult]) )r~   r    r   r!   r   r   r   r   )ru   zUnion[Path, List[Path]]r   zGList[Tuple[CalculatedChannelConfig, CalculatedChannelValidationResult]])r<   r   r   r    )rR   Optional[List[str]]r   r   r   zList[Asset])__name__
__module____qualname____doc____annotations__r.   r6   r5   rK   rO   rq   r   r   staticmethodr4   rg   rD   r/   r-   r&   r&   %   s    'CB))=
 X\
%2
GT
	 
  X\?%2?GT?	?. $($( $"&$
 $
 "$
 	$

  $
 
3$
P 04$(#'$( $"&+
,+
 "+
 !	+

 "+
 +
  +
 
3+
Z,M-,M	J,Md 	lM#:lM )lM 	lM
 
KlM\.*,.*	P.*` 
-
	 
 
B MQF(F6IF	Fr/   r&   N)1
__future__r   pathlibr   typingr   r   r   r   r	   r
   r   google.protobuf.field_mask_pb2r   google.protobuf.timestamp_pb2r   sift.assets.v1.assets_pb2r   sift.assets.v1.assets_pb2_grpcr   3sift.calculated_channels.v2.calculated_channels_pb2r   r   r   r   r   r   r   r   r   r   r   r   r   r   8sift.calculated_channels.v2.calculated_channels_pb2_grpcr   sift_py.asset._internal.sharedr   "sift_py.calculated_channels.configr    r!   sift_py.grpc.transportr"   sift_py.rule.configr#    sift_py.yaml.calculated_channelsr$   r&   rD   r/   r-   <module>r      s\    "  @ @ @ 4 3 + ;     b ; _ . FsF sFr/   