o
    ưi?                     @   s  d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZ ddlmZ e Zdeded	eeef fd
dZejddgeegede
ddde
dddde
ddde
ddde
ddde
ddde
ddde
ddde
ddde
ddde
ddde
ddde
d d!dfd"ed#ed$ee d%ee d&ee d'ee d(ee d)ee d*ee d+ee d,ee d-ee d.efd/d0Zejd1dgeeged2d3id2d4id5d6eefd7ed8efd9d:ZdS );z
AUDIT LOGGING

All /audit logging endpoints. Attempting to write these as CRUD endpoints.

GET - /audit/{id} - Get audit log by id
GET - /audit - Get all audit logs
    )AnyDictListOptional)	APIRouterDependsHTTPExceptionQuery)AuditLogResponsePaginatedAuditLogResponse)CommonProxyErrorsUserAPIKeyAuth)user_api_key_authjson_keyvaluereturnc                 C   s$   dd| g|did| g|digiS )a  
    Build an OR condition that matches a value inside a JSON column at the
    given key, checking both before_value and updated_values.

    Uses Prisma's JSON path filtering (PostgreSQL only).

    Example result (team_id="t1"):
      {"OR": [
          {"before_value":    {"path": ["team_id"], "string_contains": "t1"}},
          {"updated_values":  {"path": ["team_id"], "string_contains": "t1"}},
      ]}
    ORZbefore_value)pathZstring_containsZupdated_values )r   r   r   r   g/home/app/Keep/.python/lib/python3.10/site-packages/litellm_enterprise/proxy/audit_logging_endpoints.py_build_json_field_or_condition   s
   r   z/auditzAudit Logging)tagsdependenciesresponse_model   )ge
   d   )r   leNz2Filter by user or system that performed the action)descriptionz0Filter by API key hash that performed the actionz.Filter by action type (create, update, delete)z&Filter by table name that was modifiedz,Filter by ID of the object that was modifiedzFilter logs after this datezFilter logs before this datezRFilter by team_id present in before_value or updated_values JSON (PostgreSQL only)z[Filter by token (key hash) present in before_value or updated_values JSON (PostgreSQL only)z=Column to sort by (e.g. 'updated_at', 'action', 'table_name')ZdesczSort order ('asc' or 'desc')page	page_size
changed_bychanged_by_api_keyaction
table_name	object_id
start_dateend_dateobject_team_idobject_key_hashsort_by
sort_orderc                    sd  ddl m} |du rtddtjjidi }|r||d< |r#||d< |r)||d	< |r/||d
< |r5||d< |s9|rKi }|rA||d< |rG||d< ||d< |	r[|dg td|	g |d< |
rk|dg td|
g |d< i }|ryt|t	ry|||< n||d< |j
jj||| d | |dI dH }|j
jj|dI dH }| |  }t|rdd |D ng || ||dS )z
    Get all audit logs with filtering and pagination.

    Returns a paginated response of audit logs matching the specified filters.

    Note: object_team_id and object_key_hash use Prisma JSON path filtering,
    which requires PostgreSQL.
    r   prisma_clientN  messagestatus_codedetailr"   r#   r$   r%   r&   ZgteZlteZ
updated_atANDZteam_idtokenr   )whereorderskipZtaker6   c                 S   s   g | ]}t d i | qS )r   )r
   
model_dump).0	audit_logr   r   r   
<listcomp>   s    z"get_audit_logs.<locals>.<listcomp>)
audit_logstotalr    r!   total_pages)litellm.proxy.proxy_serverr.   r   r   db_not_connected_errorr   getr   
isinstancestrdblitellm_auditlogZ	find_manycountr   )r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r.   Zwhere_conditionsZdate_filterZorder_byr>   Ztotal_countr@   r   r   r   get_audit_logs.   sp   2






rI   z/audit/{id}r   zAudit log not foundzDatabase connection error)  r/   )r   r   r   	responsesiduser_api_key_dictc                    sv   ddl m} |du rtddtjjid|jjjd| idI dH }|du r2td	dd
|  didt	di |
 S )a[  
    Get detailed information about a specific audit log entry by its ID.

    Args:
        id (str): The unique identifier of the audit log entry

    Returns:
        AuditLogResponse: Detailed information about the audit log entry

    Raises:
        HTTPException: If the audit log is not found or if there's a database connection error
    r   r-   Nr/   r0   r1   rL   r9   rJ   zAudit log with ID z
 not foundr   )rA   r.   r   r   rB   r   rF   rG   Zfind_uniquer
   r:   )rL   rM   r.   r<   r   r   r   get_audit_log_by_id   s   
rN   )__doc__typingr   r   r   r   Zfastapir   r   r   r	   Z6litellm_enterprise.types.proxy.audit_logging_endpointsr
   r   Zlitellm.proxy._typesr   r   Z$litellm.proxy.auth.user_api_key_authr   ZrouterrE   r   rC   intrI   rN   r   r   r   r   <module>   s    	




"t