o
    ưi                     @   s   d Z ddlZddlmZ ddlmZmZ ddlZddlmZ ddl	m
Z
mZmZmZmZ 		ddede
d	ee d
edee dedee dee fddZdefddZdS )z2
Functions to create audit logs for LiteLLM Proxy
    N)uuid)datetimetimezone)verbose_proxy_logger)AUDIT_ACTIONSLiteLLM_AuditLogsLitellmTableNamesOptionalUserAPIKeyAuth	object_idactionlitellm_changed_byuser_api_key_dictlitellm_proxy_admin_name
table_namebefore_valueafter_valuec           
         sn   ddl m} tjp|d}	|	durdS tttt t	
tj|p&|jp&||j|| |||d	dI dH  dS )a  
    Create an audit log for an internal user.

    Parameters:
    - user_id: str - The id of the user to create the audit log for.
    - action: AUDIT_ACTIONS - The action to create the audit log for.
    - user_row: LiteLLM_UserTable - The user row to create the audit log for.
    - litellm_changed_by: Optional[str] - The user id of the user who is changing the user.
    - user_api_key_dict: UserAPIKeyAuth - The user api key dictionary.
    - litellm_proxy_admin_name: Optional[str] - The name of the proxy admin.
    r   get_secret_boolLITELLM_STORE_AUDIT_LOGSTN)	idZ
updated_atZ
changed_byZchanged_by_api_keyr   r   r   updated_valuesr   )request_data)litellm.secret_managers.mainr   litellmstore_audit_logscreate_audit_log_for_updater   strr   uuid4r   nowr   utcZuser_idZapi_key)
r   r   r   r   r   r   r   r   r   r    r!   b/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/management_helpers/audit_logs.pycreate_object_audit_log   s.   

r#   r   c              
      s   ddl m} tjp|d}|durdS ddlm}m} |dur"dS |du r*tdt	d|  t
| jtr=t| j| _t
| jtrJt| j| _| jdd	}z|jjji |d
I dH  W dS  ty{ } ztd|  W Y d}~dS d}~ww )z,
    Create an audit log for an object.
    r   r   r   TN)premium_userprisma_clientz&prisma_client is None, no DB connectedzcreating audit log for %s)Zexclude_none)datazFailed Creating audit log )r   r   r   r   Zlitellm.proxy.proxy_serverr$   r%   	Exceptionr   debug
isinstancer   dictjsondumpsr   Z
model_dumpdbZlitellm_auditlogcreateerror)r   r   r   r$   r%   Z_request_dataer!   r!   r"   r   C   s<   	r   )NN)__doc__r+   Zlitellm._uuidr   r   r   r   Zlitellm._loggingr   Zlitellm.proxy._typesr   r   r   r	   r
   r   r#   r   r!   r!   r!   r"   <module>   s6    
/