
    `gj                        d Z ddlmZ ddlZddlZddlZddl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mZmZ dd	lmZ d
ZddZdddZ G d d      Zy)z2Durable local state for the Teams pipeline plugin.    )annotationsN)deepcopy)datetimetimezone)Path)NamedTemporaryFile)AnyDictOptional)get_hermes_homezteams_pipeline_store.jsonc                 d    t        j                  t        j                        j	                         S N)r   nowr   utc	isoformat     O/root/.hermes/venv/lib/python3.12/site-packages/plugins/teams_pipeline/store.py_utc_now_isor      s    <<%//11r   c                    | &t        |       j                         }|rt        |      S t        j                  dd      j                         }|rt        |      S t               t        z  S )NMSGRAPH_WEBHOOK_STORE_PATH )strstripr   osgetenvr   %DEFAULT_TEAMS_PIPELINE_STORE_FILENAME)pathexplicitenv_paths      r   !resolve_teams_pipeline_store_pathr!      s[    t9??$>!yy5r:@@BHH~DDDr   c                      e Zd ZdZddZddZddZddZddZddZ	ddZ
edd	       Zdd
Z	 ddd	 	 	 	 	 	 	 ddZdd dZd!dZd"dZd#dZd$dZddZd%dZd&dZy)'TeamsPipelineStorez3JSON-backed durable store for Teams pipeline state.c                    t        |      | _        t        j                         | _        i i i i i d| _        | j                          y )Nsubscriptionsnotification_receiptsevent_timestampsjobssink_records)r   r   	threadingRLock_lock_state_load)selfr   s     r   __init__zTeamsPipelineStore.__init__)   s>    J	__&
%' "2
 	

r   c                   | j                   5  | j                  j                         s
	 d d d        y t        j                  | j                  j                  d      xs d      }t        |t              s
	 d d d        y t        |j                  d      xs i       | j                  d<   t        |j                  d      xs i       | j                  d<   t        |j                  d      xs i       | j                  d<   t        |j                  d      xs i       | j                  d<   t        |j                  d      xs i       | j                  d<   d d d        y # 1 sw Y   y xY w)	Nutf-8)encodingz{}r&   r'   r(   r)   r*   )
r-   r   existsjsonloads	read_text
isinstancedictgetr.   )r0   datas     r   r/   zTeamsPipelineStore._load5   s.   ZZ 
	O99##%
	O 
	O ::dii1171CKtLDdD)
	O 
	O ,00I0OR+PDKK(37AX8Y8_]_3`DKK/0.2488<N3O3USU.VDKK*+"&txx'7'=2">DKK*.txx/G/M2*NDKK'
	O 
	O 
	Os   EAE?CEE(c                   | j                   j                  j                  dd       t        ddt	        | j                   j                        d      5 }t        j                  | j                  |dd       |j                          t        |j                        }d d d        j                  | j                          y # 1 sw Y   %xY w)	NT)parentsexist_okwr3   F)r4   dirdelete   )indent	sort_keys)r   parentmkdirr   r   r6   dumpr.   flushr   namereplace)r0   tmptmp_paths      r   _persistzTeamsPipelineStore._persistB   s    		td;DII$$%	
 	&
 IIdkk3qDAIIKCHH~H	& 	#	& 	&s   A	CC
c                v    | j                   5  t        | j                  d         cd d d        S # 1 sw Y   y xY wNr&   r-   r   r.   r0   s    r   list_subscriptionsz%TeamsPipelineStore.list_subscriptionsO   s/    ZZ 	:DKK89	: 	: 	:   /8c                    | j                   5  | j                  d   j                  |      }t        |t              rt        |      nd cd d d        S # 1 sw Y   y xY wrP   r-   r.   r;   r9   r:   r   )r0   subscription_idrecords      r   get_subscriptionz#TeamsPipelineStore.get_subscriptionS   sN    ZZ 	J[[155oFF'1&$'?8F#T	J 	J 	J   ;AAc                z   | j                   5  | j                  d   j                  |i       }i |t        |      }||d<   |j	                  d|j                  d      xs
 t                      t               |d<   || j                  d   |<   | j                          t        |      cd d d        S # 1 sw Y   y xY w)Nr&   rW   
created_at
updated_atr-   r.   r;   r   
setdefaultr   rN   )r0   rW   payloadexistingmergeds        r   upsert_subscriptionz&TeamsPipelineStore.upsert_subscriptionX   s    ZZ 	${{?377LH66HW$56F(7F$%lHLL,F,X,.Y#/>F< <BDKK(9MMOF#	$ 	$ 	$   BB11B:c                    | j                   5  | j                  d   j                  |d       }|
	 d d d        y| j                          	 d d d        y# 1 sw Y   y xY w)Nr&   FT)r-   r.   poprN   )r0   rW   removeds      r   delete_subscriptionz&TeamsPipelineStore.delete_subscriptionc   sY    ZZ 	kk/266MG	 	 MMO	 	 	s   #AAAc                    |j                  d      }|rd| S t        j                  |dd      }t        j                  |j                  d            j                         }d| S )Nidzid:T),:)rE   
separatorsr3   zsha256:)r;   r6   dumpshashlibsha256encode	hexdigest)clsnotificationexplicit_id	canonicaldigests        r   build_notification_receipt_keyz1TeamsPipelineStore.build_notification_receipt_keyk   sc    "&&t,&&JJ|t
S		 0 0 9:DDF!!r   c                h    | j                   5  || j                  d   v cd d d        S # 1 sw Y   y xY w)Nr'   )r-   r.   )r0   receipt_keys     r   has_notification_receiptz+TeamsPipelineStore.has_notification_receiptt   s4    ZZ 	G$++.E"FF	G 	G 	Gs   (1N)received_atc                  | j                   5  || j                  d   v r
	 d d d        y|xs
 t               t        |t              rt        |      n|d| j                  d   |<   | j                          	 d d d        y# 1 sw Y   y xY w)Nr'   F)r|   r`   T)r-   r.   r   r9   r:   r   rN   )r0   rz   r`   r|   s       r   record_notification_receiptz.TeamsPipelineStore.record_notification_receiptx   s     ZZ 	dkk*ABB	 	  +<ln0:7D0I8G,wADKK/0= MMO	 	 	s   A?AA??Bc                    | j                   5  |xs
 t               }|| j                  d   |<   | j                          |cd d d        S # 1 sw Y   y xY wNr(   )r-   r   r.   rN   )r0   	event_key	timestampvalues       r   record_event_timestampz)TeamsPipelineStore.record_event_timestamp   sJ    ZZ 	/E9>DKK*+I6MMO		 	 	s   2A		Ac                    | j                   5  | j                  d   j                  |      }|t        |      nd cd d d        S # 1 sw Y   y xY wr   )r-   r.   r;   r   )r0   r   r   s      r   get_event_timestampz&TeamsPipelineStore.get_event_timestamp   sF    ZZ 	=KK 2377	BE!&!23u:	= 	= 	=s   -AAc           	     2   | j                   5  t        | j                  d         t        | j                  d         t        | j                  d         t        | j                  d         t        | j                  d         dcd d d        S # 1 sw Y   y xY w)Nr&   r'   r(   r)   r*   r%   )r-   lenr.   rR   s    r   statszTeamsPipelineStore.stats   sz    ZZ 	!$T[[%A!B),T[[9P-Q)R$'4F(G$HDKK/0 #DKK$? @	 	 	s   A6BBc                z   | j                   5  | j                  d   j                  |i       }i |t        |      }||d<   |j	                  d|j                  d      xs
 t                      t               |d<   || j                  d   |<   | j                          t        |      cd d d        S # 1 sw Y   y xY w)Nr)   job_idr\   r]   r^   )r0   r   r`   ra   rb   s        r   
upsert_jobzTeamsPipelineStore.upsert_job   s    ZZ 	${{6*..vr:H66HW$56F%F8lHLL,F,X,.Y#/>F< *0DKK'MMOF#	$ 	$ 	$rd   c                    | j                   5  | j                  d   j                  |      }t        |t              rt        |      nd cd d d        S # 1 sw Y   y xY wNr)   rV   )r0   r   rX   s      r   get_jobzTeamsPipelineStore.get_job   sN    ZZ 	J[[(,,V4F'1&$'?8F#T	J 	J 	JrZ   c                v    | j                   5  t        | j                  d         cd d d        S # 1 sw Y   y xY wr   rQ   rR   s    r   	list_jobszTeamsPipelineStore.list_jobs   s/    ZZ 	1DKK/0	1 	1 	1rT   c                z   | j                   5  | j                  d   j                  |i       }i |t        |      }||d<   |j	                  d|j                  d      xs
 t                      t               |d<   || j                  d   |<   | j                          t        |      cd d d        S # 1 sw Y   y xY w)Nr*   sink_keyr\   r]   r^   )r0   r   r`   ra   rb   s        r   upsert_sink_recordz%TeamsPipelineStore.upsert_sink_record   s    ZZ 	${{>266xDH66HW$56F!)F:lHLL,F,X,.Y#/>F< 4:DKK'1MMOF#	$ 	$ 	$rd   c                    | j                   5  | j                  d   j                  |      }t        |t              rt        |      nd cd d d        S # 1 sw Y   y xY w)Nr*   rV   )r0   r   rX   s      r   get_sink_recordz"TeamsPipelineStore.get_sink_record   sN    ZZ 	J[[044X>F'1&$'?8F#T	J 	J 	JrZ   )r   z
str | Path)returnNone)r   zDict[str, Dict[str, Any]])rW   r   r   Optional[Dict[str, Any]])rW   r   r`   Dict[str, Any]r   r   )rW   r   r   bool)rt   r   r   r   )rz   r   r   r   r   )rz   r   r`   r   r|   Optional[str]r   r   )r   r   r   r   r   r   )r   r   r   r   )r   zDict[str, int])r   r   r`   r   r   r   )r   r   r   r   )r   r   r`   r   r   r   )r   r   r   r   )__name__
__module____qualname____doc__r1   r/   rN   rS   rY   rc   rh   classmethodrx   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r#   r#   &   s    =
O$:J
	$ " "G -1
 &* *
 # 
"=
	$J
1	$Jr   r#   )r   r   r   )r   zstr | Path | Noner   r   )r   
__future__r   ro   r6   r   r+   copyr   r   r   pathlibr   tempfiler   typingr	   r
   r   hermes_constantsr   r   r   r!   r#   r   r   r   <module>r      sN    8 "   	   '  ' & & , )D %2
E[J [Jr   