
    hh(                         S SK Jr  S SKJr  SSKJr  SSKJr  S SKJ	r	J
r
Jr  S SKJr  SrS	r\" S
SSS9S 5       r\" S
SSS9SS j5       r\" S
SSS9S 5       r\S 5       rg)    )shared_task)Stamp   )WorkBalance)*calculate_daily_balances_for_user_and_date)date	timedeltadatetime)cacheschedule_catch_up_balances_lockih  T      )bindmax_retriesdefault_retry_delayc                      SSK J n  UR                  " US5      R                  5       n[        X5      nSXS.$ ! [         a  nU R                  US9eS nAff = f)Nr   )r
   %Y-%m-%dok)statususer_idr   exc)r
   strptimer   r   	Exceptionretry)selfr   date_isor
   dwbr   s          &D:\TimeStamps\worktimeservice\tasks.pycalculate_balance_taskr!      s[    "%h
388:7C'DD "jjSj!!"s   69 
AAANc                     [         R                  " 5       nU(       a&  [        R                  " US5      R                  5       nO[        R
                  R                  US9R                  S5      R                  5       nU(       a  UR                   [        SS9-   nOS[        R
                  R                  US9R                  S5      R                  5       nU(       a  UR                   nOSUS.$ UnXs::  a  [        X5        U[        SS9-  nXs::  a  M  S	XR                  5       UR                  5       S
.$ ! [         a  nU R                  US9eS nAff = f)Nr   r   -dater   daysr   no_datar   r   r   )r   r   
start_dateend_dater   )r   todayr
   r   r   objectsfilterorder_byfirstr	   r   r   	isoformatr   r   )	r   r   start_date_isor+   r)   last_balancefirst_stampcurrent_dater   s	            r    calculate_balance_tasksr5      s-   "

!**>:FKKMJ '..55g5FOOPWX^^`L)..1BB
#mm2272CLLVTZZ\!,!1!1J &/7CC "#6wMI1--L # 'AUAUAWejetetevww "jjSj!!"s$   C,D6 /#D6 !D6 6
E EE   c                 x    [         R                  " 5       [        SS9-
  nU[        SS9-
  n[        R                  R                  US9R                  S5      R                  5       nU(       a  UR                   [        SS9-   nOS[        R                  R                  US9R                  S5      R                  5       nU(       a  UR                   nOSUS.$ [        XS5      nUnXr::  a  [        X5        U[        SS9-  nXr::  a  M  S	XR                  5       S
.$ ! [         a  nU R                  US9eSnAff = f)z
Computes balances for a user from their last recorded balance up to yesterday,
but limits the historical depth to 7 days for efficiency.
r   r%      r#   r$   r   r'   r(   r   )r   r   r*   r   N)r   r+   r	   r   r,   r-   r.   r/   r   maxr   r0   r   r   )	r   r   	yesterday
hard_limitr2   r)   r3   r4   r   s	            r    catch_up_work_balances_for_userr<   :   s%   "JJL9!#44	 !22
 #**11'1BKKGTZZ\%**YA->>J--..w.?HHPVVXK(--
"+@@ 0
!'6wMI1--L ' 7@S@S@UVV "jjSj!!"s$   CD .D D 
D9%D44D9c                  n   [         R                  " [        S[        5      (       af   [        R
                  R                  SSS9R                  5       n U  H  n[        R                  U5        M     [         R                  " [        5        g[        S5        g! [         R                  " [        5        f = f)z
Triggers the catch-up task for all users who have stamps.
Protected by a lock to ensure only one orchestrator runs at a time.
truer   T)flatz3Catch-up orchestrator skipped due to existing lock.N)r   addLOCK_IDLOCK_EXPIREr   r,   values_listdistinctr<   delaydeleteprint)user_idsr   s     r    schedule_catch_up_balancesrI   _   s     yy&+..		"}}000FOOQH#/55g> $
 LL! 	CD LL!s   A
B B4)N)celeryr   stamps.modelsr   modelsr   servicer   r
   r   r	   django.core.cacher   rA   rB   r!   r5   r<   rI        r    <module>rQ      s       ? . . #
+$A1=	" >	" $A1=" >"@ $A2>!" ?!"H E ErP   