
    59i3                     :    S SK Jr  S SKJr  S SKr " S S\5      rg)    )BaseCommand)schedule_catch_up_ml_patternsNc                   $    \ rS rSrSrS rS rSrg)Command   zManually trigger ML patterns catch-up task. This will analyze patterns for all users who need it (users with stamps but missing or outdated patterns).c                 H    UR                  SSSS9  UR                  SSSS9  g )Nz--async
store_truez>Run asynchronously via Celery (default: synchronous execution))actionhelpz	--verbosezShow detailed output)add_argument)selfparsers     DD:\TimeStamps\ml_service\management\commands\catch_up_ml_patterns.pyadd_argumentsCommand.add_arguments	   s<    Q 	 	

 	' 	 	
    c           	         UR                  SS5      nUR                  SS5      nU(       Gar  U R                  R                  U R                  R	                  S5      5        U R                  R                  S5         [
        R                  " 5       nU R                  R                  U R                  R                  S5      5        U R                  R                  SUR                   35        U R                  R                  S5        U R                  R                  S	5        U R                  R                  S
5        U R                  R                  SUR                   S35        U R                  R                  S5        U R                  R                  S5        g U R                  R                  U R                  R	                  S5      5        U R                  R                  S5        U R                  R                  S5        U R                  R                  S5         [        SS9nU(       a  U R                  R                  S5        U R                  R                  U R                  R                  S5      5        U R                  R                  S5        U R                  R                  S5        U R                  R                  [        R                  " US[        S95        g UR                  SS5      nUS:X  Ga  UR                  SS5      nUR                  SS5      n	UR                  S S5      n
UR                  S!S5      nUR                  S"S5      nU R                  R                  S5        U R                  R                  U R                  R                  S#5      5        U R                  R                  S5        U R                  R                  S$U 35        U R                  R                  S%U	 35        U
b]  U R                  R                  S&U
 35        US:  a8  U R                  R                  U R                  R	                  S'U 35      5        g g U R                  R                  S(U 35        US:  ao  U R                  R                  S5        U R                  R                  U R                  R	                  S)U S*35      5        U R                  R                  S+5        g g US,:X  ak  U R                  R                  S5        U R                  R                  U R                  R	                  S-5      5        U R                  R                  S.5        g U R                  R                  S5        U R                  R                  U R                  R                  S/U 35      5        S0U;   a.  U R                  R                  S1UR                  S05       35        g g ! [         ae  nU R                  R                  U R                  R                  S[        U5       35      5        U R                  R                  S5         S nAg S nAff = f! [         a  nU R                  R                  S5        U R                  R                  U R                  R                  S2[        U5       35      5        U(       ac  SS KnU R                  R                  S5        U R                  R                  S35        U R                  R                  UR                  5       5         S nAg S nAff = f)4NasyncFverbosez*Queuing ML catch-up task asynchronously...z8Note: Celery worker must be running for this to execute.u   ✓ Task queued successfully!z	Task ID:  zTo check task status, use:z'  from celery.result import AsyncResultz  result = AsyncResult("z")z8  print(result.state)  # PENDING, SUCCESS, FAILURE, etc.z;  print(result.get())  # Get result (blocks until complete)u   ✗ Failed to queue task: z;Make sure Celery worker is running and Redis is accessible.z)Running ML catch-up task synchronously...z7This may take a while depending on the number of users.z9All user tasks will be executed immediately (not queued).T)execute_synchronouslyzTask completed successfully!zResult:   )indentdefaultstatusunknownsuccesstotal_users_with_stampsr   users_needing_analysisusers_processedusers_failedusers_queuedu    ✓ Task completed successfully!zTotal users with stamps: zUsers needing analysis: zUsers processed: zUsers failed: zUsers queued for analysis: zNote: z/ user(s) have been queued for pattern analysis.z:Make sure Celery worker is running to process these tasks.skippedz2Task skipped: Another instance is already running.z<This is normal if the scheduled task is currently executing.zTask completed with status: errorzError: u   ✗ Task failed with error: z
Traceback:)getstdoutwritestyleWARNINGr   delaySUCCESSid	ExceptionERRORstrjsondumps	traceback
format_exc)r   argsoptionsis_asyncr   resulter   total_usersusers_needingr    r!   r"   r2   s                 r   handleCommand.handle   s   ;;w.++i/KKdjj001]^_KKXY6<<>!!JJ&&)FH !!Ifii["9:!!"%!!">?!!$KM!!$<VYYKr"JK!!$\^!!$_a KKdjj001\]^KKWXKKYZKKb!;6TRKK%%b)KK%%djj&8&89W&XYKK%%b)KK%%i0KK%%djj3&OP#ZZ)<F*&,jj1JA&N(.

3KQ(O*0**5F*J'-zz.!'D'-zz.!'D))"-))$***<*<=_*`a))"-)),Ek]*ST)),D]O*TU*6 KK--0A/AR.ST+a/ $ 1 1$**2D2D~VbUcEd2e f  0 !KK--0KL>.Z[+a/ $ 1 1" 5 $ 1 1$**2D2D&,\N:i$j3" !# !% 1 12n o  0  9,))"-))$***<*<=q*rs))*hi))"-))$***:*:=YZ`Ya;b*cd"f, KK--

78K7L.MN -u  !!JJ$$'A#a&%JK !!"_`z  
!!"%!!JJ$$'CCF8%LM $KK%%b)KK%%l3KK%%i&:&:&<=
sP   =D!W3 %CY% .E7Y% 'BY% ;A0Y% ,BY% 3
Y"=AYY"%
\>/C\99\> N)__name__
__module____qualname____firstlineno__r   r   r;   __static_attributes__r=   r   r   r   r      s     dD

[r   r   )django.core.management.baser   ml_service.tasksr   r0   r   r=   r   r   <module>rE      s    3 : jk jr   