
    ip                        d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	m
Z
 ddlmZ ej                  j                  d      dk(  Zd	d
dZd Zd Zd Z ederdnd        ej+                         D ]w  \  ZZ ej0                         5  ej2                  j5                         j7                  d      j                  e      Zej:                  ej<                  ej>                  ej@                  ejB                  dZ" eee      \  Z#Z$e#r> ede dejJ                  jL                   ddjO                  e#              	 ddd       ern ee      Zej:                  ej<                  ej>                  ej@                  ejB                  dZ( ede dejJ                  jL                   de$ de" de( 
       n( ede dejJ                  jL                   de$ de"        ddd       z y# 1 sw Y   xY w)a  
Repair Apr 29 BACKFILL AccountSyncLog rows that are stuck because every
required default AI report failed with "No data to analyze".

Dry run:
    UV_CACHE_DIR=/tmp/uv-cache uv run python manage.py shell < /tmp/repair_no_data_ai_reports_backfill_20260429.py

Apply:
    UV_CACHE_DIR=/tmp/uv-cache APPLY=1 uv run python manage.py shell < /tmp/repair_no_data_ai_reports_backfill_20260429.py
    N)transaction)AccountSyncLogReport)DEFAULT_AI_REPORT_TITLESNO_DATA_TO_ANALYZE_ERRORrefresh_onboarding_run)UpdateStatusAPPLY1z$10a81928-45b1-4577-8b57-79fe7690dce9z$7d6e0349-9b11-492e-9695-50816ff5f1b3)z$fdde801b-f426-4de0-9ade-fe7dd29411fbz$80aa6647-4d6e-4d3f-92d3-cbc69f7035d9c                 p   t         j                  j                  | j                  t        d      }|j                         |j                  t        j                  j                  t              j                         |j                  t        j                  j                        j                         |j                  t        j                  j                        j                         |j                  t        j                  j                        j                  t              j                         dS )NF)account	title__in
is_deleted)sync_statuserror_message)r   )r   )required_ai_reportsno_data_failedpendingsuccessblocking_failed)r   objectsfilterr   r   countr	   FAILEDvaluer   PENDINGSUCCESSexclude)runrequired_reportss     3/tmp/repair_no_data_ai_reports_backfill_20260429.pyget_ai_report_countsr"   !   s    ~~,,* -   0557*11$++112 2 
 %'#**|7K7K7Q7Q*RXXZ#**|7K7K7Q7Q*RXXZ+22$++11 3 

' 8'
9%%'     c                    g }t        | j                        |k7  r!|j                  d| d| j                          | j                  t        j
                  j                  k7  r|j                  d| j                   d       | j                  t        j                  j                  k7  r|j                  d| j                   d       | j                  t        j                  j                  k(  r|j                  d       t        |       }|d   d	k  r|j                  d
       |d   |d   k7  r|j                  d|        |d   s
|d   s|d   r|j                  d|        ||fS )Nzaccount mismatch: expected z, got zsync_type is z, expected BACKFILLzstats_sync_status is z, expected successzalready successr   r   zEno required AI report rows found; this is not the no-data repair caser   z0not all required AI reports are failed/no-data: r   r   r   zunexpected AI report statuses: )str
account_idappend	sync_typer   SyncTypeBACKFILLstats_sync_statusStageStatusr   overall_statusOverallStatusr"   )r   expected_account_idproblemscountss       r!   validate_targetr2   5   sQ   H
3>>1156I5J&QTQ_Q_P`ab
}}//888-6IJK
 : : B BB/0E0E/FFXYZ
^99AAA)*!#&F#$)_`#$/?(@@J6(STiF9-8I1J9&BCVr#   c                 ~    t         j                  j                  | _        | j	                  ddg       t        |       S )Nai_reports_status
updated_at)update_fields)r   r,   r   r4   saver   )r   s    r!   
repair_runr8   K   s7     +66>>CHH/>H?!#&&r#   zMode: zDRY RUNr   )id)r-   current_stagead_creative_sync_statusr+   r4   zSKIP  z: z; z	REPAIRED z: ai_reports=z	, before=z, after=zWOULD REPAIR ))__doc__os	django.dbr   creative_module.modelsr   r   +creative_module.services.onboarding_servicer   r   r   creative_module.typesr	   environgetr
   TARGET_RUNSr"   r2   r8   printitemsrun_idr/   atomicr   select_for_updateselect_relatedr   r-   r:   r;   r+   r4   beforer0   r1   r   ad_account_namejoinafter r#   r!   <module>rQ      s  	 
 ! 9 
 / 	

w3& -S,R(,' %wY/0 1#.#4#4#6F				""446^I&SFS^ 	 "00 ..'*'B'B!$!6!6!$!6!6
 +30CD&E&3;;#>#>"?r$))HBUAVWX# 
	& S/C"%"4"4!$!2!2+.+F+F%(%:%:%(%:%:E F81S[[%@%@$A B$XYvhhugG
 xq)D)D(E F$XYvh8C 
	 $7		s   B7G,BG,,G6	