
    ᴺh;                     (   S r SSKrSSKrSSKrSSKrSSKJrJrJrJ	r	J
r
  SSKJr  SSKrSSKJrJr  SSKJrJrJr  \R(                  " \5      r " S S5      rSS	\4S
 jjrSS	\4S jjrSS	\4S jjrSSS.S\4S jjjrSS\S\4S jjr\" 5       r g)z
Error monitoring decorators and middleware for automatic error detection and notification.
This module provides decorators to automatically monitor API calls and system functions for errors.
    N)DictAnyOptionalCallableUnion)datetime)capture_exceptioncapture_message) send_critical_error_notificationsend_api_failure_notificationsend_system_alertc            
           \ rS rSrSr\   SS\S\S\S\4S jj5       r\  SS\S	\S
\4S jj5       r	\  SS\S\
S\4S jj5       rSrg)ErrorMonitor   z4Error monitoring class with decorators and utilitiesapi_namecriticalinclude_request_datainclude_response_datac                 <   ^ ^^^ S[         S[         4U UUU4S jjnU$ )af  
Decorator to monitor API calls and send notifications on failure

Args:
    api_name: Name of the API (OpenAI, Jira, Azure, etc.)
    critical: Whether this API call is critical (sends email notification)
    include_request_data: Whether to include request data in notifications
    include_response_data: Whether to include response data in notifications
funcreturnc                 P   >^  [         R                  " T 5      UUU UU4S j5       nU$ )Nc                  *  > [         R                   " 5       nSnS nS nS n U (       a$  [        U S   S5      (       a  U S   R                  nOSU;   a  US   nOSU;   a  US   nT(       aS  U (       a  [        U 5      S S OS U(       a2  UR	                  5        VVs0 s H  u  pxU[        U5      S S _M     snnOS S.n[
        R                  ST S	U 35        T" U 0 UD6n	[         R                   " 5       U-
  n
[        S
T 3STUU
[        R                  " 5       R                  5       S.S9  U	$ s  snnf ! [        R                  R                   a  n[         R                   " 5       U-
  n
[        U5      n[        US5      (       an  UR                  ba  UR                  R                  nT(       aD   UR                  R                   (       a  UR                  R#                  5       OS nO!   SS0n O= f[%        UTUUU
UUS.5        T(       a  ['        TUUUUUS9  e S nAf[(         ai  n[         R                   " 5       U-
  n
[        U5      n[%        UTUU
U[+        U5      R,                  S.5        T(       a  [/        ST 3UTUU
US.US9  e S nAff = f)Nunknownr   urlendpointi     )argskwargszMaking API call to : zAPI call successful: info)r   r   execution_time	timestamplevelcontextresponseerrorzUnable to parse response)r   r   status_coder"   request_dataresponse_data)r   r   error_messager)   r+   r*   )r   r   r"   r*   
error_type
API_ERROR_)r   r   r"   r*   r-   r,   r&   	exception)timehasattrr   stritemsloggerr!   r
   r   utcnow	isoformatrequests
exceptionsRequestExceptionr'   r)   contentjsonr	   r   	Exceptiontype__name__r   )r   r   
start_timer   r*   r+   r)   kvresultr"   er,   r   r   r   r   r   s                7D:\Projects\AI-TestCaseGenerator\utils\error_monitor.pywrapperAErrorMonitor.monitor_api_call.<locals>.decorator.<locals>.wrapper'   s   !YY[
$# $"iQ 7 7#'7;;&#)%=#v-#)*#5 ,7;CIdsOV\6<<>&R>41q#a&#,>&Rbf(
 KK"5hZr( LM "4262F &*YY[:%=N $/z:$(0(0.<)1):)D)D)F	!	 "M/ 'S2  **;; $%)YY[:%=N$'FM q*--!**2H&'jj&<&<0VEFZZEWEW

0A]aV18:T0U &a$,$,'2*8(4)6*   5%-%-*7(3*7)5   %)YY[:%=N$'FM &a$,$,*8(4&*1g&6&6*   8)3H:'>*7,4,42@0<	% '(
 ;sQ   A9D3 D-4A8D3 -D3 3JAH07G('H(G0..HJ)A$JJ	functoolswraps)r   rF   r   r   r   r   s   ` rE   	decorator0ErrorMonitor.monitor_api_call.<locals>.decorator&   s.    __T"p p #pd N    r   )r   r   r   r   rK   s   ```` rE   monitor_api_callErrorMonitor.monitor_api_call   s(    t	H t	 t	 t	j rM   function_nameinclude_contextalert_on_failurec                 8   ^ ^^ S[         S[         4UU U4S jjnU$ )a   
Decorator to monitor critical system functions

Args:
    function_name: Name of the function for logging/notification
    include_context: Whether to include function context in notifications
    alert_on_failure: Whether to send email alerts on failure
r   r   c                 N   >^  [         R                  " T 5      UU UU4S j5       nU$ )Nc                    > [         R                   " 5       nS n T(       aQ  T[        U 5      U(       a  [        UR                  5       5      O/ [        R
                  " 5       R                  5       S.n[        R                  ST 35        T
" U 0 UD6n[         R                   " 5       U-
  n[        ST 3STU[        R
                  " 5       R                  5       S.S9  U$ ! [         a  n[         R                   " 5       U-
  n[        U5      nTU[        U5      R                  [        R
                  " 5       R                  5       S.nU(       a  UR                  U5        [        Xh5        T	(       a  [!        SS	T S
U 3UUS9  e S nAff = f)N)rQ   
args_countkwargs_keysr#   zExecuting critical function: z)Critical function executed successfully: r!   )rQ   r"   r#   r$   )rQ   r"   r-   r#   CRITICAL_FUNCTION_ERRORzCritical function '
' failed: r/   )r1   lenlistkeysr   r6   r7   r5   r!   r
   r=   r3   r>   r?   updater	   r   )r   r   r@   r&   rC   r"   rD   r,   error_contextrS   r   rQ   rR   s            rE   rF   JErrorMonitor.monitor_critical_function.<locals>.decorator.<locals>.wrapper   sh   !YY[
<&-:*-d)BH4+>b)1):)D)D)F	# KK"? OP "4262F &*YY[:%=N $CM?S$-:.<)1):)D)D)F! "M  %)YY[:%=N$'FM *7*8&*1g&6&6%-__%6%@%@%B	%M %,,W5 &a7 (8)@,?jYfXg*h$1&'	 ;s   CC 
F	)BFF	rH   )r   rF   rS   rQ   rR   s   ` rE   rK   9ErrorMonitor.monitor_critical_function.<locals>.decorator   s(    __T"@ #@D NrM   rN   )rQ   rR   rS   rK   s   ``` rE   monitor_critical_function&ErrorMonitor.monitor_critical_function   s(    D	H D	 D	 D	J rM   component_namealert_thresholdalert_on_slow_executionc                 8   ^ ^^ S[         S[         4UUU 4S jjnU$ )z
Decorator to monitor system health and performance

Args:
    component_name: Name of the system component
    alert_threshold: Execution time threshold in seconds for alerts
    alert_on_slow_execution: Whether to send alerts for slow execution
r   r   c                 N   >^  [         R                  " T 5      UUUU 4S j5       nU$ )Nc                    > [         R                   " 5       n T
" U 0 UD6n[         R                   " 5       U-
  nUT:  a-  T(       a&  [        SST	 SUS ST S3ST	UTT
R                  S.S	9  [        S
T	 3ST	UT
R                  [        R
                  " 5       R                  5       S.S9  U$ ! [         a  n[         R                   " 5       U-
  n[        U5      n[        UT	UT
R                  [        U5      R                  S.5        [        SST	 SU 3ST	UT
R                  [        U5      R                  S.S	9  e S nAff = f)NPERFORMANCE_WARNINGzSlow execution detected in r    z.2fzs (threshold: zs)WARNING)rd   r"   	thresholdrQ   )
alert_typemessageseverityr&   zSystem component executed: r!   )rd   r"   rQ   r#   r$   )rd   r"   rQ   r-   COMPONENT_FAILUREzSystem component 'rZ   CRITICAL)r1   r   r?   r
   r   r6   r7   r=   r3   r	   r>   )r   r   r@   rC   r"   rD   r,   rf   re   rd   r   s          rE   rF   FErrorMonitor.monitor_system_health.<locals>.decorator.<locals>.wrapper   ss   !YY[
>!4262F &*YY[:%=N &7<S)'<&A.AQQSTbcfSggu  wF  vG  GI  %J%.2@2@-<15	%	
 $5n5EF$.<.<-1]])1):)D)D)F	!	 "M  %)YY[:%=N$'FM &a*8*8)-&*1g&6&6	*  &#6"4^4DJ}o ^!+.<.<-1]]*.q'*:*:	!	
 7s   BB+ +
E 5BD;;E rH   )r   rF   rf   re   rd   s   ` rE   rK   5ErrorMonitor.monitor_system_health.<locals>.decorator   s(    __T"A #AF NrM   rN   )rd   re   rf   rK   s   ``` rE   monitor_system_health"ErrorMonitor.monitor_system_health   s(    E	H E	 E	 E	L rM    N)TTT)TT)      @T)r?   
__module____qualname____firstlineno____doc__staticmethodr3   boolrO   rb   floatrt   __static_attributes__rv   rM   rE   r   r      s    >9=596:A3 A$ A.2A/3A AF 8<9=P P15P26P Pd 58<@Qc Q-2Q59Q QrM   r   r   c                 *    [         R                  SU S9$ )zMonitor OpenAI API callsOpenAIr   r   rO   r   s    rE   monitor_openai_apir   E  s    ((H(EErM   c                 *    [         R                  SU S9$ )zMonitor Jira API callsJirar   r   r   s    rE   monitor_jira_apir   I  s    ((((CCrM   c                 *    [         R                  SU S9$ )zMonitor Azure DevOps API callszAzure DevOpsr   r   r   s    rE   monitor_azure_apir   M  s    ((((KKrM   )rQ   rQ   c                *   ^ U4S jnU c  U$ U" U 5      $ )z!Monitor critical system functionsc                 d   > T=(       d    U R                   n[        R                  U5      " U 5      $ N)r?   r   rb   )fnamerQ   s     rE   rK   *monitor_critical_system.<locals>.decoratorS  s'    *

55d;A>>rM   rv   )r   rQ   rK   s    ` rE   monitor_critical_systemr   Q  s    ? |rM   rd   rl   c                    ^ ^ U U4S jnU$ )zMonitor system performancec                 V   > T=(       d    U R                   n[        R                  UTS9$ )N)re   )r?   r   rt   )r   r   rd   rl   s     rE   rK   -monitor_system_performance.<locals>.decorator^  s'    .11$	1RRrM   rv   )rd   rl   rK   s   `` rE   monitor_system_performancer   \  s    S rM   )Tr   )Nrw   )!r{   rI   loggingr1   	tracebacktypingr   r   r   r   r   r   r8   utils.error_loggerr	   r
   utils.email_notifierr   r   r   	getLoggerr?   r5   r   r}   r   r   r   r3   r   r~   r   error_monitorrv   rM   rE   <module>r      s   
     7 7   A  
		8	$l l^	F FDt DL L	t 	 	s e  rM   