o
    ưi                     @   sT   d Z ddlZddlmZ ddlmZmZ ddlZG dd deeZ	G dd dZ
dS )	zPCloudZero Resource Names (CZRN) generation and validation for LiteLLM resources.    N)Enum)Anycastc                   @   s   e Zd ZdZdS )CZEntityTypeZteamN)__name__
__module____qualname__ZTEAM r	   r	   g/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/cloudzero/cz_resource_names.pyr      s    r   c                   @   s   e Zd ZdZedZdd Zdee	e
f de	fddZd	e	d
e	de	de	de	de	de	fddZde	defddZde	dee	e	e	e	e	e	f fddZd
e	de	fddZdde	dede	fddZdS )CZRNGeneratorz@Generate CloudZero Resource Names (CZRNs) for LiteLLM resources.zO^czrn:([a-z0-9-]+):([a-zA-Z0-9-]+):([a-z0-9-]+):([a-z0-9-]+):([a-z0-9-]+):(.+)$c                 C   s   dS )zInitialize CZRN generator.Nr	   )selfr	   r	   r
   __init__#   s   zCZRNGenerator.__init__rowreturnc           
      C   sZ   d}|  |dd}d}|dd}| |}d}|dd}|}	| j||||||	dS )	aa  Create a CZRN from LiteLLM daily spend data.
        
        CZRN format: czrn:<service-type>:<provider>:<region>:<owner-account-id>:<resource-type>:<cloud-local-id>
        
        For LiteLLM resources, we map:
        - service-type: 'litellm' (the service managing the LLM calls)
        - provider: The custom_llm_provider (e.g., 'openai', 'anthropic', 'azure')
        - region: 'cross-region' (LiteLLM operates across regions)
        - owner-account-id: The team_id or user_id (entity_id)
        - resource-type: 'llm-usage' (represents LLM usage/inference)
        - cloud-local-id: model
        litellmZcustom_llm_providerunknownzcross-regionteam_idz	llm-usagemodel)service_typeproviderregionowner_account_idresource_typecloud_local_id)_normalize_providerget_normalize_componentcreate_from_components)
r   r   r   r   r   r   r   r   r   r   r	   r	   r
   create_from_litellm_data'   s    
z&CZRNGenerator.create_from_litellm_datar   r   r   r   r   r   c                 C   sz   | j |dd}|  |}|  |}|  |}|  |}d| d| d| d| d| d| }| |s;td| |S )z)Create a CZRN from individual components.T)allow_uppercasezczrn::zGenerated CZRN is invalid: )r   is_valid
ValueError)r   r   r   r   r   r   r   czrnr	   r	   r
   r   L   s   



(
z$CZRNGenerator.create_from_componentsr#   c                 C   s   t | j|S )z3Validate a CZRN string against the standard format.)bool
CZRN_REGEXmatch)r   r#   r	   r	   r
   r!   e   s   zCZRNGenerator.is_validc                 C   s<   | j |}|std| ttttttttf | S )zExtract all components from a CZRN.
        
        Returns: (service_type, provider, region, owner_account_id, resource_type, cloud_local_id)
        zInvalid CZRN format: )r%   r&   r"   r   tuplestrgroups)r   r#   r&   r	   r	   r
   extract_componentsi   s   z CZRNGenerator.extract_componentsc                 C   s   t jjjdt jjjdt jjjdt jjjdt jjjdt jjjdt jj	jdt jj
jdt jjjdt jjjd	i
}| d
d}||vrB|S |||S )z1Normalize provider names to standard CZRN format.ZazureZ	anthropicZawsZgcpZgoogleZcohereZhuggingfaceZ	replicateztogether-ai_-)r   ZLlmProvidersZAZUREvalueZAZURE_AIZ	ANTHROPICZBEDROCKZ	VERTEX_AIZGEMINIZCOHEREZHUGGINGFACEZ	REPLICATEZTOGETHER_AIlowerreplacer   )r   r   Zprovider_map
normalizedr	   r	   r
   r   t   s   









z!CZRNGenerator._normalize_providerF	componentr   c                 C   sB   |sdS |s
|  }tdd|}tdd|}|d}|p dS )z7Normalize a CZRN component to meet format requirements.r   z[^a-zA-Z0-9-]r,   z-+)r.   resubstrip)r   r1   r   r	   r	   r
   r      s   
z"CZRNGenerator._normalize_componentN)F)r   r   r   __doc__r2   compiler%   r   dictr(   r   r   r   r$   r!   r'   r*   r   r   r	   r	   r	   r
   r      s0    
%
"r   )r5   r2   enumr   typingr   r   r   r(   r   r   r	   r	   r	   r
   <module>   s   