
    Ng                         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j        e          Z G d de
          ZdS )    N)IteratorList)Document)
BaseLoaderc                   p    e Zd ZdZ	 	 	 	 ddededed	ed
edefdZdedee         fdZ	de
e         f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                 Z    || _         || _        || _        || _        || _        || _        d S )N)r   r   r   r   r   r   )selfr   r   r   r   r   r   s          n/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/cube_semantic.py__init__zCubeSemanticLoader.__init__"   s:     ),%:"&<#,H),H)))    dimension_namereturnc                 p   t                               d           d| j        d}dg| j        di}d}|| j        k     rt          j        d| j         d|t          j	        |          	          }|j
        d
k    rs|                                }d|v rE|d         dk    r9t                               d           |dz  }t          j        | j                   fd|d         D             }|S t                               d|j
                   	 || j        k    rt                               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.
        z1Loading dimension values for: {dimension_name}...application/jsonzContent-TypeAuthorizationquery)
dimensionslimitr   POSTz/load)headersdata   errorzContinue waitzRetrying...   c                      g | ]
}|         S  r'   ).0itemr   s     r   
<listcomp>z<CubeSemanticLoader._get_dimension_values.<locals>.<listcomp>Z   s+     ( ( (15^,( ( (r   r"   z Request failed with status code:zMaximum retries reached.)loggerinfor   r   r   requestsrequestr   jsondumpsstatus_codetimesleepr   r$   )r   r   r!   r   retriesresponseresponse_datadimension_valuess    `      r   _get_dimension_valuesz(CubeSemanticLoader._get_dimension_values2   s    	GHHH /!0
 
 -.4 
 999'$+++Z&&	  H #s** (},,%g./AAKK...qLGJt@AAA( ( ( (9Fv9N( ( ($ ,+?AUVVVd777KK2333	r   c              #   z  K   d| j         d}t                              d| j         d           t	          j        | j         d|          }|                                 |                                }|                    dg           }t                              dt          |           d	           |st          d
          |D ]}|                    d          }|                    d          }|                    d          }|                    dg           }	|                    dg           }
t                              d| d           |st                              d| d           |	|
z   D ];}||	v rdnd}g }t          |                    d                    }t          |                    d                    }| j        r!|dk    r|dk    r|                     |          }t          t          |          ||t          |                    d                    t          |                    d                    |||          }t          |                    d                     d}|t          |                    d                     z  }t          ||          V  =d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 z...z/meta)r!   cubeszFound z cube data objects in metadata.zNo cubes found in metadata.nametypepublicmeasuresr   zProcessing z	Skipping z because it is not public.measure	dimensionstringtitledescription)
table_namecolumn_namecolumn_data_typecolumn_titlecolumn_descriptioncolumn_member_typecolumn_valuescube_data_obj_typez, )page_contentmetadataN)r   r+   r,   r   r-   getraise_for_statusr/   len
ValueErrorstrr   r8   dictr   )r   r!   r5   raw_meta_jsoncube_data_objectscube_data_objcube_data_obj_namerK   cube_data_obj_is_publicr>   r   r)   rI   r7   	item_name	item_typerM   rL   s                     r   	lazy_loadzCubeSemanticLoader.lazy_loadf   s     " /!0
 

 	CT->CCCDDD<4#4 ; ; ;WMMM!!### )--gr::TS!233TTTUUU  	<:;;;. (	M (	MM!.!2!26!:!:!.!2!26!:!:&3&7&7&A&A#$((R88H&**<<<JKK=&8===>>>* V(:VVVWWW :- M M26(2B2BYY"#%  0 011	 0 011	 .M*k99!X--'+'A'A)'L'L$"#566 )%.!$TXXg%6%6!7!7'*488M+B+B'C'C'9"2'9	 	 	 #&dhhw&7&7"8"8<<<3txx'>'>#?#? AAL8LLLLLLL7M(	M (	Mr   N)Tr	   r
   r   )__name__
__module____qualname____doc__rR   boolintr   r   r8   r   r   r[   r'   r   r   r   r      s         . '+&,,.,-I II I  $	I
 !$I '*I '*I I I I 2C 2DI 2 2 2 2hHM8H- HM HM HM HM HM HMr   r   )r/   loggingr2   typingr   r   r-   langchain_core.documentsr   )langchain_community.document_loaders.baser   	getLoggerr\   r+   r   r'   r   r   <module>rg      s       ! ! ! ! ! ! ! !  - - - - - - @ @ @ @ @ @		8	$	$`M `M `M `M `M `M `M `M `M `Mr   