
    h^                         d dl Z d dlZd dlZd dlmZmZ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mZmZ d dlmZmZ d dlmZ  G d d	      Zy)
    N)datetime	timedeltatimezone)ListLiteralOptionalUnion)verbose_proxy_logger)LiteLLM_BudgetTableFullLiteLLM_EndUserTableLiteLLM_TeamTableLiteLLM_UserTableLiteLLM_VerificationToken)PrismaClientProxyLogging)ServiceTypesc                   *   e Zd ZdZdedefdZd Zdee	   fdZ
d Zd	 Zd
 Zd Zedeeeef   deded   fd       Zedededee   fd       Zedededee   fd       Zededee   fd       Zede	dede	fd       Zedededee   fd       Zy)ResetBudgetJobzK
    Resets the budget for all the keys, users, and teams that need it
    proxy_logging_objprisma_clientc                      || _         || _        y )N)r   r   )selfr   r   s      g/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/common_utils/reset_budget_job.py__init__zResetBudgetJob.__init__   s    /@+8    c                    K   | j                   a| j                          d{    | j                          d{    | j                          d{    | j	                          d{    yy7 N7 87 "7 w)z
        Gets all the non-expired keys for a db, which need spend to be reset

        Resets their spend

        Updates db
        N)r   reset_budget_for_litellm_keysreset_budget_for_litellm_usersreset_budget_for_litellm_teams%reset_budget_for_litellm_budget_table)r   s    r   reset_budgetzResetBudgetJob.reset_budget   s|      )44666 55777 55777 <<>>> *6 8 8 ?sC    A9A1A9A3A9A5A9*A7+A93A95A97A9budgets_to_resetc                    K   | j                   j                  j                  j                  dd|D cg c]  }|j                  |j                   c}iiddi       d{   S c c}w 7 	w)z\
        Resets the budget for all LiteLLM Team Members if their budget has expired
        	budget_idinNspendr   )wheredata)r   dblitellm_teammembershipupdate_manyr$   )r   r"   budgets      r   %reset_budget_for_litellm_team_membersz4ResetBudgetJob.reset_budget_for_litellm_team_members3   s      ''**AAMM&6"!++7 ((  N 
 
 	

s   0A-A&A-!A+"
A-c                   K   t        j                  t        j                        }t	        j                         }d}d}g }g }	 | j
                  j                  dd|       d{   }|t        |      dkD  r|D ]   }t        j                  ||       d{   }" | j
                  j                  d|d       d{    | j
                  j                  dd|D cg c]  }|j                  |j                   c}	       d{   }| j                  |
       d{    |t        |      dkD  r|D ]I  }	 t        j                  |       d{   }	|	|j                  |	       n|j                  |dd       K t#        j&                  dt)        j*                  |dt                      | j
                  j                  d|d       d{    t	        j                         }t        |      dkD  r3t        dt        |       dt)        j*                  |t                      t-        j.                  | j0                  j2                  j5                  t6        j8                  ||z
  d|||rt        |      ndt)        j*                  |dt               |rt        |      ndt)        j*                  |dt               t        |      t)        j*                  |dt               t        |      t)        j*                  |dt               d             y7 7 7 ec c}w 7 #7 7 # t        $ r>}
|j                  |t!        |
      d       t#        j$                  d|       Y d}
~
=d}
~
ww xY w7 # t        $ r}
t	        j                         }t-        j.                  | j0                  j2                  j;                  t6        j8                  ||z
  |
d|||rt        |      ndt)        j*                  |dt               |rt        |      ndt)        j*                  |dt               d             t#        j$                  d|
       Y d}
~
yd}
~
ww xY ww)z
        Resets the budget for all LiteLLM End-Users (Customers), and Team Members if their budget has expired
        The corresponding Budget duration is also updated.
        Nr,   find_all
table_name
query_typereset_atr   r+   r2   	data_listr1   enduser)r1   r2   budget_id_list)r"   )r6   Returned None without exception)r6   errorz&Failed to reset budget for enduser: %sUpdated users %s   indentdefaultFailed to reset z endusers: r>   reset_budget_budget_table)num_budgets_foundbudgets_foundnum_endusers_foundendusers_foundnum_endusers_updatedendusers_updatednum_endusers_failedendusers_failedserviceduration	call_type
start_timeend_timeevent_metadatareset_budget_endusers)rB   rC   rD   rE   rK   rL   r9   rM   rN   rO   rP   z'Failed to reset budget for endusers: %s)r   nowr   utctimer   get_datalenr   _reset_budget_reset_at_dateupdate_datar$   r-   _reset_budget_for_enduserappend	Exceptionstrr
   	exceptiondebugjsondumpsasynciocreate_taskr   service_logging_objasync_service_success_hookr   RESET_BUDGET_JOBasync_service_failure_hook)r   rS   rN   endusers_to_resetr"   updated_endusersfailed_endusersr,   r6   updated_endusererO   s               r   r    z4ResetBudgetJob.reset_budget_for_litellm_budget_tableH   sg     ll8<<(YY[
BFDH79@	Y%)%7%7%@%@#
S &A &    +4D0E0I.F#1#M#M$ F /
 ((44,.' 5    +/*<*<*E*E() '7$"!++7 (($ +F + %! @@%5 A    !,5F1G!1K0G"0"J"J(/ #K #  (
 +6,33OD+22/6-N!"  1, %**&JJ/3G
 ((44,.( 5    yy{H?#a'&s?';&<K

SbloHpGqr  &&::UU(99%
29)% 6FC 011)-,Q* 7HC 12Q*.**-a+ 144D0E,0JJ,Q- 03?/C+/::+As,%$ V M $% % '..7SQRV/TU,66Dg V  	Yyy{H&&::UU(99%
25)% 6FC 011)-,Q* 7HC 12Q*.**-a+$ V 0 !**+TVWXX5	Ys   A Q%!N $L%2N L!'N ?L$ N L'>N 	L,
N #L/$N >L5L2,L5AN M?EN Q%N !N $N 'N /N 2L55	M<>3M71N 7M<<N 	Q"CQQ%Q""Q%c                   K   t        j                         }t        j                         }d}	 | j                  j	                  dd||       d{   }t        j                  dt        j                  |dt                     g }g }|t        |      dkD  r|D ]J  }	 t        j                  ||	       d{   }||j                  |       n|j                  |d
d       L t        j                  dt        j                  |dt                     |r&| j                  j!                  d|d       d{    t        j                         }	t        |      dkD  r3t        dt        |       dt        j                  |t                     t#        j$                  | j&                  j(                  j+                  t,        j.                  |	|z
  d||	|rt        |      ndt        j                  |dt              t        |      t        j                  |dt              t        |      t        j                  |dt              d             y7 	7 # t        $ r>}|j                  |t        |      d       t        j                  d|       Y d}~d}~ww xY w7 n# t        $ r}t        j                         }	t#        j$                  | j&                  j(                  j1                  t,        j.                  |	|z
  |d||	|rt        |      ndt        j                  |dt              d             t        j                  d|       Y d}~yd}~ww xY ww)zf
        Resets the budget for all the litellm keys

        Catches Exceptions and logs them
        Nkeyr/   )r1   r2   expiresr3   zKeys to reset %sr;   r<   r   rn   current_timer8   )rn   r9   z"Failed to reset budget for key: %szUpdated keys %sr+   r4   r?   z keys: r@   reset_budget_keys)num_keys_found
keys_foundnum_keys_updatedkeys_updatednum_keys_failedkeys_failedrJ   )rs   rt   rR   z#Failed to reset budget for keys: %s)r   utcnowrU   r   rV   r
   r_   r`   ra   r]   rW   r   _reset_budget_for_keyr[   r\   r^   rY   rb   rc   r   rd   re   r   rf   rg   )
r   rS   rN   keys_to_resetupdated_keysfailed_keysrn   updated_keyrl   rO   s
             r   r   z,ResetBudgetJob.reset_budget_for_litellm_keys   s$     ooYY[
CGM	U"&"4"4"="= Zs #> # M !&&"DJJ}QPS$T =?LK(S-?!-C(C,:,P,P ## -Q - ' '2(//<'..(+6W X )" %**%tzz,qRU'V  ,,88#0".#( 9    yy{H;!#&s;'7&8

;`c@d?ef  &&::UU(99%
21)%@M#m*<ST&*jjqRU&V,/,=(,

<SV(W+.{+;'+zz+aQT'U$ V W' % #**3Q+HI,66@# :  	Uyy{H&&::UU(99%
21)%@M#m*<ST&*jjqRU&V$ V  !**+PRSTT!	Us   +M&"J, IAJ, I9I:,I&AJ, ;J)<DJ, M&J, I	J&(3J!J, !J&&J, ,	M#5B$MM&M##M&c                 n  K   t        j                         }t        j                         }d}	 | j                  j	                  dd|       d{   }g }g }|t        |      dkD  r|D ]J  }	 t        j                  ||       d{   }||j                  |       n|j                  |dd       L t        j                  d
t        j                  |dt                     |r&| j                  j!                  d|d       d{    t        j                         }	t        |      dkD  r3t        dt        |       dt        j                  |t                     t#        j$                  | j&                  j(                  j+                  t,        j.                  |	|z
  d||	|rt        |      ndt        j                  |dt              t        |      t        j                  |dt              t        |      t        j                  |dt              d             y7 7 # t        $ r>}|j                  |t        |      d       t        j                  d	|       Y d}~d}~ww xY w7 n# t        $ r}t        j                         }	t#        j$                  | j&                  j(                  j1                  t,        j.                  |	|z
  |d||	|rt        |      ndt        j                  |dt              d             t        j                  d|       Y d}~yd}~ww xY ww)z^
        Resets the budget for all LiteLLM Internal Users if their budget has expired
        Nuserr/   r0   r   r   rq   r8   )r   r9   z#Failed to reset budget for user: %sr:   r;   r<   r+   r4   r?   z users: r@   reset_budget_users)num_users_foundusers_foundnum_users_updatedusers_updatednum_users_failedusers_failedrJ   )r   r   rR   z$Failed to reset budget for users: %s)r   ry   rU   r   rV   rW   r   _reset_budget_for_userr[   r\   r]   r
   r^   r_   r`   ra   rY   rb   rc   r   rd   re   r   rf   rg   )
r   rS   rN   users_to_resetupdated_usersfailed_usersr   updated_userrl   rO   s
             r   r   z-ResetBudgetJob.reset_budget_for_litellm_users.       ooYY[
<@R	V#'#5#5#>#>!j3 $? $ N 68ML)c..AA.E*D-;-R-R!%C .S . ( (3)00>(//,0-N!" +( %**&

=TW(X !,,88#0"/#) 9    yy{H< 1$&s<'8&9$**\cfBgAhi  &&::UU(99%
22)%BP3~+>VW'+zz*1c( .1-?)-)!S* -0,=(,

<SV(W$ V U( % $++TCF,KL,66A4 B  	Vyy{H&&::UU(99%
22)%BP3~+>VW'+zz*1c($ V   !**+QSTUU%	V   +L5!I; H(I; .H.H+	,H.5AI; 
I8DI; 'L5(I; +H..	I573I0*I; 0I55I; ;	L2B$L-(L5-L22L5c                 n  K   t        j                         }t        j                         }d}	 | j                  j	                  dd|       d{   }g }g }|t        |      dkD  r|D ]J  }	 t        j                  ||       d{   }||j                  |       n|j                  |dd       L t        j                  d
t        j                  |dt                     |r&| j                  j!                  d|d       d{    t        j                         }	t        |      dkD  r3t        dt        |       dt        j                  |t                     t#        j$                  | j&                  j(                  j+                  t,        j.                  |	|z
  d||	|rt        |      ndt        j                  |dt              t        |      t        j                  |dt              t        |      t        j                  |dt              d             y7 7 # t        $ r>}|j                  |t        |      d       t        j                  d	|       Y d}~d}~ww xY w7 n# t        $ r}t        j                         }	t#        j$                  | j&                  j(                  j1                  t,        j.                  |	|z
  |d||	|rt        |      ndt        j                  |dt              d             t        j                  d|       Y d}~yd}~ww xY ww)z^
        Resets the budget for all LiteLLM Internal Teams if their budget has expired
        Nteamr/   r0   r   r   rq   r8   )r   r9   z#Failed to reset budget for team: %szUpdated teams %sr;   r<   r+   r4   r?   z teams: r@   reset_budget_teams)num_teams_foundteams_foundnum_teams_updatedteams_updatednum_teams_failedteams_failedrJ   )r   r   rR   z$Failed to reset budget for teams: %s)r   ry   rU   r   rV   rW   r   _reset_budget_for_teamr[   r\   r]   r
   r^   r_   r`   ra   rY   rb   rc   r   rd   re   r   rf   rg   )
r   rS   rN   teams_to_resetupdated_teamsfailed_teamsr   updated_teamrl   rO   s
             r   r   z-ResetBudgetJob.reset_budget_for_litellm_teams  r   r   itemrq   	item_type)rn   r   r   c                    K   	 d| _         t        | d      r*| j                  ddlm}  || j                        | _        | S # t        $ r}t        j                  d|||        |d}~ww xY ww)z
        In-place, updates spend=0, and sets budget_reset_at to current_time + budget_duration

        Common logic for resetting budget for a team, user, or key
                budget_durationNr   )get_budget_reset_time)r   z+Error resetting budget for %s: %s. Item: %s)	r&   hasattrr   )litellm.proxy.common_utils.timezone_utilsr   budget_reset_atr\   r
   r^   )r   rq   r   r   rl   s        r   _reset_budget_commonz#ResetBudgetJob._reset_budget_common  sy     	DJt./D4H4H4T (=$($8$8($ K 	 **=y!T G		s(   A.>A A.	A+A&&A++A.r   returnc                 R   K   t         j                  | |d       d {    | S 7 w)Nr   r   rq   r   r   r   r   s     r   r   z%ResetBudgetJob._reset_budget_for_team  8      11LF 2 
 	
 	
 	
   '%'r   c                 R   K   t         j                  | |d       d {    | S 7 w)Nr   r   r   r   s     r   r   z%ResetBudgetJob._reset_budget_for_user
  r   r   r6   c                 r   K   	 d| _         | S # t        $ r}t        j                  d||        |d }~ww xY ww)Nr   z0Error resetting budget for enduser: %s. Item: %s)r&   r\   r
   r^   )r6   rl   s     r   rZ   z(ResetBudgetJob._reset_budget_for_enduser  sF     	GM   	 **BAw G		s   7 7	4/47r,   c                 f  K   	 | j                   uddlm}  || j                         }| j                  <| j                  t        |      z   |kD  r | j                  t        |      z   | _        | S |t        |      z   | _        | S # t        $ r}t        j                  d||        |d }~ww xY ww)Nr   )duration_in_seconds)rL   )secondsz8Error resetting budget_reset_at for budget: %s. Item: %s)	r   *litellm.litellm_core_utils.duration_parserr   r   
created_atr   r\   r
   r^   )r,   rq   r   
duration_srl   s        r   rX   z*ResetBudgetJob._reset_budget_reset_at_date   s     	%%1 1&:P:PQ
 **2))Ij,IILX-3->-> *B .F*  .:I *= .F*   	 **JAv G		s5   B1A+B /B11B B1	B.B))B..B1rn   c                 R   K   t         j                  | |d       d {    | S 7 w)Nrn   r   r   rp   s     r   rz   z$ResetBudgetJob._reset_budget_for_key?  s8      11<5 2 
 	
 	
 
	
r   N)__name__
__module____qualname____doc__r   r   r   r!   r   r   r-   r    r   r   r   staticmethodr	   r   r   r   r   r   r   r   r   r   r   rZ   rX   rz    r   r   r   r      s   9, 9| 9?.
 $%< =
*LY\VUpYVvYVv %'8:SST 01 8 /7	#	$  /7	#	$  
%
	&	'
 
 '7?	  < &6>	+	, r   r   )rb   r`   rU   r   r   r   typingr   r   r   r	   litellm._loggingr
   litellm.proxy._typesr   r   r   r   r   litellm.proxy.utilsr   r   litellm.types.servicesr   r   r   r   r   <module>r      s;       2 2 1 1 1  ; /s sr   