
    ZTh#                    ^   d dl mZ d dlmZ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 d dlmZ d dlmZ d dlmZ d d	lm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 G d de      Z G d de      Z G d de      Z 	 	 	 	 ddZ!y)    )annotations)ABCabstractmethod)Enum)AnyDictListOptionalUnioncast)AnnotationType)
ActionKind)	TypedDict)AsJson)ChannelConfigc            	          e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   d
ed<   ddddddi ddf		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZe	 	 	 	 	 	 dd       Zy)
RuleConfigar  
    Defines a rule to be used during ingestion. If a rule's expression validates to try, then
    a specific action will take place as specified by the `kind` attribute.

    - `name`: Name of the rule.
    - `description`: Description of the rule.
    - `expression`: A CEL string expression that executes the `action` when evaluated to a truthy value.
    - `action`: The action to execute if the result of an `expression` evaluates to a truthy value.
    - `channel_references`: Reference to channel. If an expression is "$1 < 10", then "$1" is the reference and thus should the key in the dict.
    - `rule_client_key`: User defined unique string that uniquely identifies this rule.
    - `asset_names`: A list of asset names that this rule should be applied to. ONLY VALID if defining rules outside of a telemetry config.
    - `tag_names`: A list of asset names that this rule should be applied to. ONLY VALID if defining rules outside of a telemetry config.
    - `contextual_channels`: A list of channel names that provide context but aren't directly used in the expression.
    - `is_external`: If this is an external rule.
    strnamedescription
expressionOptional[RuleAction]action List[ExpressionChannelReference]channel_referencesOptional[str]rule_client_keyz	List[str]asset_namescontextual_channelsboolis_external_rule_id NFc                    t        |      | _        |
xs g | _        || _        |xs g | _        || _        || _        || _        | j                  j                  ||	      | _
        || _        d | _        y N)_channel_references_from_dictsr   r   r   r   r   r   r   	__class__interpolate_sub_expressionsr   r!   r"   )selfr   r   r   r   r   r   r   	tag_namessub_expressionsr   r!   s               X/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/rule/config.py__init__zRuleConfig.__init__+   ss      #AAS"T#6#<" 	&,".&..DDZQ`a&    c                2   | j                   | j                  | j                  | j                  d}| j                  |d<   | j
                  r| j
                  |d<   t        | j                  t              rt        j                  j                  |d<   | j                  j                  |d<   | j                  j                  ;t        | j                  j                        dkD  r| j                  j                  |d<   | j                  j                  ;t        | j                  j                        dkD  r| j                  j                  |d<   |S t        | j                  t              rpt        j                   j                  |d<   | j                  j                  ;t        | j                  j                        dkD  r| j                  j                  |d<   |S | j                  r| j                  j#                         n| j                  }t%        d| d	      )
z\
        Produces the appropriate JSON structure that's suitable for the Rules API.
        )r   r   r   r!   expression_channel_referencescontextual_channel_referencestypeassigneer   tagszUnsupported rule action ''.)r   r   r   r!   r   r   
isinstancer   $RuleActionCreateDataReviewAnnotationRuleActionAnnotationKindREVIEWvaluer3   lenr4   RuleActionCreatePhaseAnnotationPHASEkind	TypeError)r)   hash_mapr>   s      r,   as_jsonzRuleConfig.as_jsonG   s    II++//++	
 	 594K4K01##8<8P8PH45dkk#GH7>>DDHV#';;#7#7HZ {{##/C8L8L4MPQ4Q'+{{';';${{+DKK4D4D0E0I#';;#3#3   %DE7==CCHV{{+DKK4D4D0E0I#';;#3#3 
  *.4;;##%$++D7vR@AAr.   c                    |rp|j                         D ]]  \  }}|| vrt        d| d|  d      t        |t              r| j	                  |d| d      } C| j	                  |t        |            } _ | S )NzCouldn't find 'z' in expression 'r5   ")items
ValueErrorr6   r   replace)r   r+   refexprs       r,   r(   z&RuleConfig.interpolate_sub_expressionsq   s     ,224 D	Tj($se;LZLXZ%[\\dC(!+!3!3C1TF!!EJ!+!3!3CT!CJD r.   )r   r   r   PList[Union[ExpressionChannelReference, ExpressionChannelReferenceChannelConfig]]r   r   r   r   r   r   r   r   r   Optional[List[str]]r*   rJ   r+   zDict[str, Any]r   rJ   r!   r    )returnr   )r   r   r+   zOptional[Dict[str, str]]rK   r   )	__name__
__module____qualname____doc____annotations__r-   rA   staticmethodr(    r.   r,   r   r      s     IO  88"""" '+)-+/)-*,37!
   % ' ) ' ( 1 8(T *B	 r.   r   c                      e Zd Zedd       Zy)
RuleActionc                     y r%   rR   r)   s    r,   r>   zRuleAction.kind   s    r.   NrK   RuleActionKind)rL   rM   rN   r   r>   rR   r.   r,   rT   rT      s     r.   rT   c                  8    e Zd ZU dZded<   ded<   d	d
dZddZy)r7   a  
    Action to create a data-review annotation when a rule evaluates to a truthy value.

    - `tags`: List of tag names to associate with the newly created data-review annotation.
    - `assignee`: Email of user in organization to assign the newly created data-review annotation.
    rJ   r4   r   r3   Nc                     || _         || _        y r%   )r3   r4   )r)   r3   r4   s      r,   r-   z-RuleActionCreateDataReviewAnnotation.__init__   s     	r.   c                "    t         j                  S r%   rX   
ANNOTATIONrV   s    r,   r>   z)RuleActionCreateDataReviewAnnotation.kind       (((r.   )NN)r3   r   r4   rJ   rW   rL   rM   rN   rO   rP   r-   r>   rR   r.   r,   r7   r7      s     )r.   r7   c                  .    e Zd ZU dZded<   dddZd	dZy)
r<   z
    Action to create a phase annotation when a rule evaluates to a truthy value.

    - `tags`: List of tag names to associate with the newly created data-review annotation.
    rJ   r4   Nc                    || _         y r%   )r4   )r)   r4   s     r,   r-   z(RuleActionCreatePhaseAnnotation.__init__   s	    	r.   c                "    t         j                  S r%   r\   rV   s    r,   r>   z$RuleActionCreatePhaseAnnotation.kind   r^   r.   r%   )r4   rJ   rW   r_   rR   r.   r,   r<   r<      s     )r.   r<   c                  N    e Zd Zej                  Zej
                  Zedd       Zy)rX   c                    |dk(  s|t         j                  j                  k(  r| j                  S |dk(  s|t         j                  j                  k(  r| j                  S y )NACTION_KIND_NOTIFICATIONACTION_KIND_ANNOTATION)RuleActionKindStrRepNOTIFICATIONr:   r]   clsvals     r,   from_strzRuleActionKind.from_str   sU    ,,7K7X7X7^7^0^###,,7K7V7V7\7\0\>>!r.   N)rk   r   rK   zOptional['RuleActionKind'])rL   rM   rN   r   rh   r]   classmethodrl   rR   r.   r,   rX   rX      s*    **L&&J r.   rX   c                  8    e Zd ZdZdZedd       Zedd       Zy)r8   reviewphasec                X    |t         j                  k(  r| j                  S | j                  S r%   )r   ANNOTATION_TYPE_PHASEr=   r9   )rj   annotation_types     r,   from_annotation_typez-RuleActionAnnotationKind.from_annotation_type   s$    nBBB99zzr.   c                    || j                   j                  k(  r| j                   S || j                  j                  k(  r| j                  S t        d| d      )Nz
Argument 'z!' is not a valid annotation kind.)r9   r:   r=   rE   ri   s     r,   rl   z!RuleActionAnnotationKind.from_str   sM    #**"""::CIIOO#99z#.OPQQr.   N)rs   r   rK   'RuleActionAnnotationKind')rk   r   rK   rv   )rL   rM   rN   r9   r=   rm   rt   rl   rR   r.   r,   r8   r8      s4    FE 
 R Rr.   r8   c                      e Zd ZdZdZy)rg   notification
annotationN)rL   rM   rN   rh   r]   rR   r.   r,   rg   rg      s    !LJr.   rg   c                  &    e Zd ZU dZded<   ded<   y)ExpressionChannelReferencez
    `channel_reference`: The channel reference (e.g. '$1') used in the expression.
    `channel_identifier`: The channel name.
    r   channel_referencechannel_identifierNrL   rM   rN   rO   rP   rR   r.   r,   r{   r{      s    
 r.   r{   c                  &    e Zd ZU dZded<   ded<   y)'ExpressionChannelReferenceChannelConfigz
    `channel_reference`: The channel reference (e.g. '$1') used in the expression.
    `channel_config`: Instance of `sift_py.ingestion.channel.ChannelConfig`.
    r   r|   r   channel_configNr~   rR   r.   r,   r   r      s    
 !!r.   r   c                    g }| D ]u  }|j                  d      }|6t        t        |      }|j                  |d   |j	                         d       Lt        t
        |      }|j                  |d   |d   d       w |S )Nr   r|   )r|   r}   r}   )getr   r   appendfqnr{   )r   outr|   configchannel_refs        r,   r&   r&      s    
 -/C/ "&&'78-0FJJ):;N)O*0**, 9;LMKJJ)45H)I*56J*K* Jr.   N)r   rI   rK   r   )"
__future__r   abcr   r   enumr   typingr   r   r	   r
   r   r   #sift.annotations.v1.annotations_pb2r   sift.rules.v1.rules_pb2r   typing_extensionsr   sift_py._internal.convert.jsonr   sift_py.ingestion.channelr   r   rT   r7   r<   rX   r8   rg   r{   r   r&   rR   r.   r,   <module>r      s    " #  9 9 > . ' 1 3o od ): )&)j ) T Rt R(4 
 "i " &	r.   