o
    Zh                     @   sf   d dl Z d dlZd dlZd dlmZmZ d dlZd dlmZ d dl	m
Z
 eeZG dd de
ZdS )    N)IteratorList)Document)
BaseLoaderc                   @   sd   e Zd ZdZ				ddededed	ed
edefddZdedee fddZ	de
e fddZdS )CubeSemanticLoadera}  Load `Cube semantic layer` metadata.

    Args:
        cube_api_url: REST API endpoint.
            Use the REST API of your Cube's deployment.
            Please find out more information here:
            https://cube.dev/docs/http-api/rest#configuration-base-path
        cube_api_token: Cube API token.
            Authentication tokens are generated based on your Cube's API secret.
            Please find out more information here:
            https://cube.dev/docs/security#generating-json-web-tokens-jwt
        load_dimension_values: Whether to load dimension values for every string
            dimension or not.
        dimension_values_limit: Maximum number of dimension values to load.
        dimension_values_max_retries: Maximum number of retries to load dimension
            values.
        dimension_values_retry_delay: Delay between retries to load dimension values.
    T'  
      cube_api_urlcube_api_tokenload_dimension_valuesdimension_values_limitdimension_values_max_retriesdimension_values_retry_delayc                 C   s(   || _ || _|| _|| _|| _|| _d S )N)r
   r   r   r   r   r   )selfr
   r   r   r   r   r    r   i/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/cube_semantic.py__init__"   s   	
zCubeSemanticLoader.__init__dimension_namereturnc                    s   t d  d| jd}d g| jdi}d}|| jk rdtjd| j d|t	|d	}|j
d
kr\| }d|v rO|d dkrOt d |d7 }t| j q fdd|d D }|S t d|j
 	 || jkrnt d g S )zMakes a call to Cube's REST API load endpoint to retrieve
        values for dimensions.

        These values can be used to achieve a more accurate filtering.
        z$Loading dimension values for: %s ...application/jsonzContent-TypeAuthorizationquery)
dimensionslimitr   POSTz/load)headersdata   errorzContinue waitzRetrying...   c                    s   g | ]}|  qS r   r   ).0itemr   r   r   
<listcomp>Z   s    z<CubeSemanticLoader._get_dimension_values.<locals>.<listcomp>r   z#Request failed with status code: %szMaximum retries reached.)loggerinfor   r   r   requestsrequestr
   jsondumpsstatus_codetimesleepr   r    )r   r   r   r   retriesresponseZresponse_datadimension_valuesr   r$   r   _get_dimension_values2   sH   






z(CubeSemanticLoader._get_dimension_valuesc                 c   s   d| j d}td| j tj| j d|d}|  | }|dg }tdt| |s5t	d|D ]}|d	}|d
}|d}|dg }	|dg }
td| |sctd| q7|	|
 D ]m}||	v rodnd}g }t
|d	}t
|d
}t|d}|std| qg| jr|dkr|dkr| |}tt
|||t
|dt
|d|||d}t
|d d}|t
|d 7 }t||dV  qgq7dS )a  Makes a call to Cube's REST API metadata endpoint.

        Returns:
            A list of documents with attributes:
                - page_content=column_title + column_description
                - metadata
                    - table_name
                    - column_name
                    - column_data_type
                    - column_member_type
                    - column_title
                    - column_description
                    - column_values
                    - cube_data_obj_type
        r   r   zLoading metadata from %s ...z/meta)r   Zcubesz'Found %s cube data objects in metadata.zNo cubes found in metadata.nametypepublicmeasuresr   zProcessing %s ...z%Skipping %s because it is not public.measure	dimensionstringtitledescription)Z
table_nameZcolumn_nameZcolumn_data_typeZcolumn_titleZcolumn_descriptioncolumn_member_typeZcolumn_valuescube_data_obj_typez, )page_contentmetadataN)r   r&   r'   r
   r(   getraise_for_statusr*   len
ValueErrorstrboolr   r2   dictr   )r   r   r0   Zraw_meta_jsonZcube_data_objectsZcube_data_objZcube_data_obj_namer=   Zcube_data_obj_is_publicr6   r   r#   r<   r1   Z	item_nameZ	item_typeZ	is_publicr?   r>   r   r   r   	lazy_loadh   sf   



zCubeSemanticLoader.lazy_loadN)Tr   r   r	   )__name__
__module____qualname____doc__rD   rE   intr   r   r2   r   r   rG   r   r   r   r   r      s*    
6r   )r*   loggingr-   typingr   r   r(   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   	getLoggerrH   r&   r   r   r   r   r   <module>   s    
