
    ;i$                        d Z ddl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 ddlmZ dd	lmZmZmZ dd
lmZmZ ddlmZmZ ddlmZ ddlmZmZ dZ dZ!ejD                  jF                  Z$ ejJ                  d      dk(  Z& ejJ                  dd      dk(  Z'dZ(d Z)d Z*ejV                  jY                  e!e e$      Z- e	j\                         j_                         Z0e-jb                  xs e0 ed      z
  Z2e0 ed      z   Z3 e4e3e2z
  jj                  dz         D  cg c]  } e2 e|       z    c} Z6ejV                  jo                  e-e$      Z8e8jo                  ejr                  jt                  ejv                  jt                  ejx                  jt                  g      Z=ejV                  jo                  e-e$      Z>e>jo                  ejr                  jt                  ejv                  jt                  ejx                  jt                  g      Z?ejV                  jo                  e-ejr                  jt                  ejx                  jt                  g      Z@ejV                  jo                  e-ejr                  jt                  ejx                  jt                  g      ZAejV                  jo                  e-ejr                  jt                  ejx                  jt                  g      ZBejV                  jo                  e-ej                  j                  ej                  jv                  ej                  j                  g      ZFejV                  jo                  e-ej                  j                  ej                  jv                  g      ZHejV                  jo                  e-ej                  jr                  ej                  j                  ej                  j                  g      ZLejV                  jo                  e-ej                  jt                  ej                  j                         ZP eQ       ZRe-j                  reRj                  e-j                         eR e)e=d!      z  ZReR e)e?d!      z  ZReR e)e@d"      z  ZReR e)eAd"      z  ZReR e)eBd"      z  ZReR e)eHd#      z  ZReR e)eHd$      z  ZReR e)eLd"      z  ZReR e*e!e       z  ZR eUd%e&         eUd&e-j                   d'e-j                   d'e-j                           eUd(e2 d)e3 d* eYe6       d+        eUd,ePj                                  eUd-e=j                                  eUd.e?j                                  eUd/e@j                                  eUd0eAj                                  eUd1eBj                                  eUd2eFj                                  eUd3eHj                                  eUd4eLj                                  eUd5 eYeR               e[eR      dd6 D ]  ZS eUd7eS          eYeR      d6kD  r eUd8 eYeR      d6z
   d9       e&s eUd:        e\d       e	j\                         Z. ej                         5  ePj                  ej                  j                  e.;       ejV                  jo                  e-j                  <      j                  ejx                  jt                  d=       ejV                  j                  e6D cg c]H  } ee-e$|ej                  jt                  ej                  jt                  e-j                  xs d>e.e(?      J c}d@dAB       ejV                  jo                  e-e$e2e3fC      j                  ej                  jt                  e.e(dD        ejX                  e$g       ZeejV                  j                  e6D cg c]A  }eeD ]:  } ee-e$||ej                  jt                  ej                  jt                  e.e(E      < C c}}d@dAB       ejV                  jo                  e-e$e2e3fC      j                  ej                  jt                  e.e(dD       e@j                  ejx                  jt                  e(dF       eAj                  ejx                  jt                  e(dF       eBj                  ejx                  jt                  e(ddGH       eFj                  ej                  jx                  ej                  j                  ej                  jx                  ej                  jx                  ej                  jx                  e.I       eHj                  ej                  jx                  e.J       eLj                  ej                  jx                  e.J       ddd       dZidZje'r1 e[eR      D ]&  ZS	 ej                  j                  eSd@dKL       eidz  Zi(  eUdOei dPej         eUdQe=j                                  eUdRe?j                                  eUdS       yc c} w c c}w c c}}w # 1 sw Y   xY w# em$ rZnejdz  Zj eUdMeS dNen        Y dZn[ndZn[nww xY w)Ta  
Hard-stop pipeline retries for one Facebook account.

This is intentionally scoped to:
  organization_auth_account.ad_account_id = 336282307415329
  organization_auth_account.platform = FACEBOOK
  organization_auth_account.id = 4e7e1ec9-8754-459e-b8b7-7620db56f52c

Usage:
  UV_CACHE_DIR=/tmp/uv-cache uv run python manage.py shell < /tmp/hard_stop_fb_account_336282307415329.py

Apply:
  UV_CACHE_DIR=/tmp/uv-cache APPLY=1 uv run python manage.py shell < /tmp/hard_stop_fb_account_336282307415329.py
    N)	timedelta)current_app)transaction)timezone)AdCreativeDataAdIdsCacheDataAdMetricCacheDataAccountSyncLog)UpdateStatus)PLATFORM_BREAKDOWNSSyncJobType
SyncStatus)BreakdownSyncStatusPipelineSyncStatus)JobJobTask)UserRoleType)AccountUserAccount336282307415329z$4e7e1ec9-8754-459e-b8b7-7620db56f52cAPPLY1REVOKE_TASKSziHard-stopped manually: Facebook (#200) missing ads_management/ads_read for ad_account_id=336282307415329.c           	          t          | j                  di | ddij                  di |dij                  |d      j                               S )N__isnullT )flat )setexcludevalues_listdistinct)qsfields     ,/tmp/hard_stop_fb_account_336282307415329.pytask_idsr&   ,   s[    	


0x($/0		 2;	 	U	&		     c                    t               }	 t        j                  j                  d      }|j	                         xs i |j                         xs i |j                         xs i d}| |f}|j                         D ]  \  }}|j                         D ]  \  }	}
|
xs g D ]  }|dk(  r|j                  d      n|}t        |t              s,t        |      t        fd|D              sL|j                  d      }|s`|j                  |       t        d	| d
| d|	           |S # t        $ r}t        d|        |cY d }~S d }~ww xY w)N   )timeout)activereserved	scheduledzinspect failed: r-   requestc              3   &   K   | ]  }|v  
 y w)Nr   ).0needlehaystacks     r%   	<genexpr>z#inspect_task_ids.<locals>.<genexpr>J   s     @fv)s   idzinspect matched z task z on )r   r   controlinspectr+   r,   r-   	Exceptionprintitemsget
isinstancedictrepranyadd)
account_idad_account_idfound	inspector	snapshotsexcneedlesstateworkersworkertasksitemtasktask_idr2   s                 @r%   inspect_task_idsrN   5   sH   EE	''///:	&&(.B!**,2",,.4"
	 =)G#//+w$]]_MFE.3{.Btxx	*!$-:@@@"hhtnG		'* 0vgYd6(ST $ - , L#   &'s   AD, ,	E5E
E
E)r4   rA   platformi  )days   )accountrO   )
status__in)rR   sync_status__in)rR   overall_status__in)rR   	state__in)job__accountrV   )rR   rolestatuscelery_task_idrM   group_idchord_idzAPPLY=zaccount= zdate guard range=z..z (z days)zactive owners=zpipeline open rows before=zbreakdown open rows before=zad ids cache open rows before=z!ad metric cache open rows before=zcreative open rows before=zaccount sync logs open before=zjobs open before=zjob tasks open before=ztask ids to revoke=d   z  z  ... z morez)Dry run only. Set APPLY=1 to mutate prod.)rY   last_token_check_time)pk)assets_update_statusrM   UTC)rR   rO   	sync_daterY   job_typeaccount_timezonecompleted_at
last_errorTi  )ignore_conflicts
batch_size)rR   rO   sync_date__range)rY   rf   rg   rZ   )rR   rO   rc   breakdown_keyrY   rd   rf   rg   )sync_statuserror_messagerM      )rl   rm   rM   creative_sync_retry_count)overall_statuscurrent_stagead_creative_sync_statusstats_sync_statusai_reports_statusfinished_at)rG   ru   SIGTERM)	terminatesignalzfailed revoke z: zrevoked=z failed=zpipeline open rows after=zbreakdown open rows after=zhard stop complete)o__doc__osdatetimer   celeryr   	django.dbr   django.utilsr   creative_module.modelsr   r   r	   r
   creative_module.typesr   data_pipeline_module.constantsr   r   r   data_pipeline_module.modelsr   r   jobs.modelsr   r   organization_auth.constantsr   organization_auth.modelsr   r   AD_ACCOUNT_ID
ACCOUNT_IDPlatformFACEBOOKPLATFORMgetenvr   r   REASONr&   rN   objectsr:   rR   nowdatetodayfirst_synced_date
start_dateend_daterangerP   	all_datesfilterpipeline_allPENDINGvalueRUNNINGFAILEDpipeline_openbreakdown_allbreakdown_openad_ids_openmetric_opencreative_openOverallStatusQUEUEDRECONCILINGrun_openJobStatejob_open	TaskStateRECEIVEDSTARTEDjob_task_openOWNERUserAccountStatusACTIVEactive_ownerr   	to_revokerM   r?   r8   r4   ad_account_namerO   lencountsorted
SystemExitatomicupdateEXPIREDr`   bulk_createSKIPPEDMANUAL	time_zonebreakdown_keysStage
CLICKHOUSEStageStatusrevokedfailedr5   revoker7   rE   )irc   rk   s   000r%   <module>r      s~
   
   ! ! d d . W W O $ 4 9 "3
$$		'c!ryy-4) : //

=S[

\&&G59#3F+F
9!$$5:Hz<Q;W;WZ[;[5\]5\Z)++5\]	!))0080T##
0B0B0H0H*J\J\JbJbdndudud{d{/|#}#++227X2V%%*2D2D2J2JJL^L^LdLdfpfwfwf}f}1~%$$++GlNbNbNhNhjvj}j}  kD  kD  NE+  F''..wQ]QeQeQkQkmy  nA  nA  nG  nG  QH.  I&&--gP\PdPdPjPjlxll  mF  mF  PG-  H!!(($$++$$,,$$00 )  ;;g#,,:M:Ms||OcOc9de&&  (('*;*;*D*DgFWFWF_F_` '  ""))				!	!((// *  E	
??MM'//" 	Xm%56 6	 	Xn&67 7	 	Xk9- -	 	Xk9- -	 	XmY/ /	 	Xh
+ +	 	Xh
+ +	 	XmY/ /	 	j-8 8	 ug  Ag556a8H8H7IJ K *RzC	N3C6J K |))+,- . "=#6#6#8"9: ; #N$8$8$:#;< = &{'8'8':&;< = )+*;*;*=)>? @ "=#6#6#8"9: ; &x~~'7&89 : (..*+, - }22456 7 C	N+, -i #&G	Bwi. 'y>C	F3y>C'(
./	
56
Q-hlln[,,44!   OOgjj)00)0066 1 
 ** '	
 '	 !#!))//$++11!(!2!2!;e !	 '	
  + " %%$h/ &  f!!''	   -(,,Xr:N++ '	
 '	!/  !#+!))//$++11 !	 "0	 '	
 ! , $ &&$h/ '  f!!''	   <#6#6#<#<F\`a<#6#6#<#<F\`a ''--"#	   OO%33::$**55 . : : A A(44;;(44;;   OO#,,--3O?w0077SIk n 	
)$	5&&w$y&QqLG % 	&* + !-"5"5"7!89 : ">#7#7#9":; <  O ^Z	
:	
Q |  	5aKFN7)2cU344	5sM   #h5<BiAh:
A>iAh?Fi#i:iii3i..i3