
    ZTh.                       d dl mZ d dlZd dlmZ d dl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mZ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m Z m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z'  G d d      Z( G d de)      Z*y)    )annotationsN)Path)AnyDictListOptionalcast)Self)channel_fqn)_component_deprecation_warning)ChannelBitFieldElementChannelConfigChannelDataTypeChannelEnumType)load_named_expression_modulesread_and_validate)TelemetryConfigYamlSpec)
FlowConfig)ExpressionChannelReference'ExpressionChannelReferenceChannelConfig
RuleActionRuleActionAnnotationKind$RuleActionCreateDataReviewAnnotationRuleActionCreatePhaseAnnotation
RuleConfig)RuleYamlSpecload_rule_modulesc                      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dg g f	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Ze		 	 d	 	 	 	 	 	 	 dd       Z
e	i g f	 	 	 	 	 	 	 dd       ZddZy)TelemetryConfigau  
    Configurations necessary to start ingestion.
    - `asset_name`: The name of the asset that you wish to telemeter data for.
    - `ingestion_client_key`: Optional string chosen by the user to uniquely identify this ingestion configuration. If this isn't
       supplied a sha256 hash will be used as the client key.
    - `flows`: A single flow can specify a single channel value or a set of channel values that are ingested together.
    - `organization_id`: ID of your organization in Sift. This field is only required if your user belongs to multiple organizations.
    - `rules`: Rules to evaluate during ingestion.
    str
asset_nameingestion_client_keyOptional[str]organization_idList[FlowConfig]flowsList[RuleConfig]rulesbool"_ingestion_client_key_is_generatedNc                   | j                   j                  |       | j                   j                  |       || _        || _        || _        || _        |r|| _        d| _        y| j                         | _        d| _        y)z
        Will raise a `TelemetryConfigValidationError` under the following conditions:
        - Multiple flows with the same name
        - Multiple rules with the same name
        - Identical channels in the same flow
        FTN)
	__class__validate_flowsvalidate_rulesr!   r$   r&   r(   r"   r*   hash)selfr!   r"   r$   r&   r(   s         g/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_py/ingestion/config/telemetry.py__init__zTelemetryConfig.__init__5   sn     	%%e,%%e,$.

(<D%6;D3(,		D%6:D3    c                    t               }| D ]D  }|j                  |v rt        d|j                   d      |j                  |j                         F y)zE
        Ensure that there are no rules with identical names
        z,Can't have two rules with identical names, ''.N)setnameTelemetryConfigValidationErroradd)r(   seen_rule_namesrules      r1   r.   zTelemetryConfig.validate_rulesR   sW    
 % 	+DyyO+4B499+RP  		*	+r3   c           	     b   t               }| D ]  }t               }|j                  |v rt        d|j                   d      |j                  |j                         |j                  D ]C  }|j                         }||v rt        d| d|j                   d      |j                  |       E  y)z
        Ensures no duplicate channels and flows with the same name, otherwise raises a `TelemetryConfigValidationError` exception.
        z*Can't have two flows with the same name, 'r5   z$Can't have two identical channels, 'z', in flow 'N)r6   r7   r8   r9   channelsfqn)r&   
flow_namesflowseen_channelschannelr>   s         r1   r-   zTelemetryConfig.validate_flows`   s    
 U
 	+DEMyyJ&4@2N  NN499%== +kkm-'8>se<PTPYPY{Z\]  "%%c*+	+r3   c                z    t        |      }i }g }|t        |      }|t        |      }| j                  |||      S )z
        Initializes a telemetry config from a YAML file found at the provided `path` as well as optional
        paths to named expression modules if named expressions are leveraged.
        )r   r   r   
_from_yaml)clspathnamed_expression_modulesnamed_rule_modulesconfig_as_yamlnamed_expressionsrule_moduless          r1   try_from_yamlzTelemetryConfig.try_from_yaml{   sP     +40#/ =>V W),-?@L~~n.?NNr3   c                   g }g }|j                  dg       D ]  }g }|d   D ]  }t        t        t        j                  |d               }	g }
|j                  dg       D ](  }|
j	                  t        |d   |d   |d                * g }|j                  d	g       D ]$  }|j	                  t        |d   |d
                & |j	                  t        |d   |	|j                  d      |j                  d      |j                  d      |
|              |j	                  t        |d   |             " |j                  dg       |z   }|D ]  }d }d}t        j                  |d         }|j                  d      }|j                  dd      }t        |      }|t        j                  k(  rt        |j                  d      |      }g }|j                  dg       D ]Z  }|j                         D ]E  \  }}|d   }|j                  d      }|r
t                |j	                  |t        ||      d       G \ |j                  dd      }|j                  dd      }t!        |t"              r%|j	                  t%        |d   |||||             Gt        t"        |j                  d            }|j                  |      }|t'        d| d      |j                  dg       }i }|D ]  } | j                         D ]
  \  }!}"|"||!<    ! |j	                  t%        |d   ||||||               | |d    |j                  d!      |j                  d"      ||#      S )$Nr&   r=   	data_typebit_field_elementsr7   index	bit_count)r7   rP   rQ   
enum_typeskey)r7   rS   descriptionunit	component)r7   rN   rT   rU   rV   rO   rR   )r7   r=   r(    typetagsassignee)rZ   rY   channel_references)channel_referencechannel_identifier
expressionrule_client_key)r7   rT   r^   actionr_   r[   zNamed expression 'z1' could not be found. Make sure it was loaded in.sub_expressions)r7   rT   r^   r`   r_   r[   ra   r!   r"   r$   )r!   r"   r$   r(   r&   )getr	   r   from_strappendr   r   r   r   r   r   REVIEWr   itemsr   r   
isinstancer    r   r8   )#rE   rI   rJ   rK   r(   r&   r@   r=   rB   rN   rO   bit_field_elementrR   	enum_type
yaml_rulesr;   r`   rT   annotation_typerY   r[   r\   refvalr7   rV   r^   r_   expression_nameexprra   	sub_exprssub_expressionidenvalues#                                      r1   rD   zTelemetryConfig._from_yaml   s    "&&w3 *	DH
+   /2J2J7S^K_2`a	%'")05I2)N %&--.!26!:"3G"<&7&D  
!(\2!> I%%'!*6!2 )% 0 !$V_"+$+KK$>$[[0")++k":+=#-
- D LLf%K*	X $''4|C
 J	D+/F!K6??VMO88F#D((="5K4T:F":"A"AA=!XXj1   &*XX.BB%G ! 1 7 7 9 HCv;D !$ 4I 68&--142=dI2N  ,3J"hh'8"=O*c*!&\$/#-%(7+=	 #'sJNN6,B"C(,,_=<8,_,==no  #'((+<b"A,.	&5 0N'5';';'= 0e*/	$00 !&\$/#'%(7+=(1
AJ	X %l3!/!3!34J!K*../@A
 	
r3   c                h   t        j                         }|j                  | j                  j	                                t        | j                  d       D ]J  }|j                  |j                  j	                                |j                  D ]  }|j                  |j                  j	                                |j                  |j                  j                  d      j	                                |j                  |j                  xs dj	                                |j                  |j                  xs dj	                                |j                  |j                  xs dj	                                t        |j                  d       D ]  }|j                  |j                  j	                                |j                  t        |j                         j	                                |j                  t        |j"                        j	                                 t        |j$                  d       D ]]  }|j                  t        |j&                        j	                                |j                  |j                  j	                                _  M |j)                         S )Nc                    | j                   S N)r7   )fs    r1   <lambda>z&TelemetryConfig.hash.<locals>.<lambda>!  s
    QVV r3   rS   T)
api_formatrW   c                    | j                   S rv   )rP   )bfes    r1   rx   z&TelemetryConfig.hash.<locals>.<lambda>,  s
    cii r3   c                    | j                   S rv   ry   )ets    r1   rx   z&TelemetryConfig.hash.<locals>.<lambda>0  s
    bff r3   )hashlibsha256updater!   encodesortedr&   r7   r=   rN   as_human_strrT   rV   rU   rO   r    rP   rQ   rR   rS   	hexdigest)r0   mr@   rB   r|   enums         r1   r/   zTelemetryConfig.hash  s   NN	'')*4::+;< 	1DHHTYY%%'(== 1,,./**7747HOOQR'--3;;=>'++1r99;<',,,"4467!'"<"<BWX :CHHSXX__./HHS^2245HHS/6689: #7#5#5;LM 1DHHS]1134HHTYY--/011	1& {{}r3   )
r!   r    r"   r#   r$   r#   r&   r%   r(   r'   )r(   r'   )r&   r%   )NN)rF   r   rG   Optional[List[Path]]rH   r   returnr
   )rI   r   rJ   zDict[str, str]rK   zList[RuleYamlSpec]r   r
   )r   r    )__name__
__module____qualname____doc____annotations__r2   staticmethodr.   r-   classmethodrL   rD   r/    r3   r1   r   r   #   s-    O""(,,
 /3)-"$"$;; ,; '	;
  ;  ;: + + + +4  :>37	OO #7O 1	O
 
O O,  -/+-	I
/I
 *I
 )	I

 
I
 I
Vr3   r   c                  0     e Zd ZU dZded<   d fdZ xZS )r8   z:
    When the telemetry config has invalid properties
    r    messagec                $    t         |   |       y rv   )superr2   )r0   r   r,   s     r1   r2   z'TelemetryConfigValidationError.__init__>  s    !r3   )r   r    )r   r   r   r   r   r2   __classcell__)r,   s   @r1   r8   r8   7  s     L" "r3   r8   )+
__future__r   r   pathlibr   typingr   r   r   r   r	   typing_extensionsr
   sift_py._internal.channelr   sift_py.errorr   sift_py.ingestion.channelr   r   r   r   "sift_py.ingestion.config.yaml.loadr   r   "sift_py.ingestion.config.yaml.specr   sift_py.ingestion.flowr   sift_py.rule.configr   r   r   r   r   r   r   sift_py.yaml.ruler   r   r   	Exceptionr8   r   r3   r1   <module>r      sb    "   2 2 " 1 8  G -   >Q Qh"Y "r3   