
    Ng+                       d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlZddlZddlmZmZ ddlmZmZmZmZ ddlmZ  ej        e          Z ej        dd	          Zerdd
lmZ  G d de          Z 	 dddZ!ddZ"dS )z#Wrapper around a Power BI endpoint.    )annotationsN)TYPE_CHECKINGAnyDictIterableListOptionalUnion)ClientTimeoutServerTimeoutError)	BaseModel
ConfigDictFieldmodel_validator)TimeoutPOWERBI_BASE_URLz"https://api.powerbi.com/v1.0/myorg)TokenCredentialc                     e Zd ZU dZded<   ded<   dZded<   dZd	ed
<   dZded<   dZded<    e	ddd          Z
ded<    e	e          Zded<   dZded<    ed          Z ed          ed8d                         Zed9d!            Zed:d"            Zd;d$Zd9d%Zed9d&            Z	 d<d=d)Zd>d+Zd?d,Z	 d<d@d-Z	 d<d@d.ZdAd1ZdAd2ZdBd5Z dCd6Z!dCd7Z"dS )DPowerBIDatasetaO  Create PowerBI engine from dataset ID and credential or token.

    Use either the credential or a supplied token to authenticate.
    If both are supplied the credential is used to generate a token.
    The impersonated_user_name is the UPN of a user to be impersonated.
    If the model is not RLS enabled, this will be ignored.
    str
dataset_id	List[str]table_namesNOptional[str]group_idzOptional[TokenCredential]
credentialtokenimpersonated_user_name   r   
   )defaultgtleintsample_rows_in_table_info)default_factoryDict[str, str]schemaszOptional[aiohttp.ClientSession]
aiosessionT)arbitrary_types_allowedbefore)modevaluesDict[str, Any]returnr   c                ~    |                     dg           }d |D             |d<   d|v sd|v r|S t          d          )z?Validate that at least one of token and credentials is present.r   c                ,    g | ]}t          |          S  fix_table_name.0tables     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/powerbi.py
<listcomp>z2PowerBIDataset.validate_params.<locals>.<listcomp>9   s      P P P5!6!6 P P P    r   r   z.Please provide either a credential or a token.)get
ValueError)clsr-   r   s      r8   validate_paramszPowerBIDataset.validate_params4   sX     jj33 P PK P P P}f 6 6MIJJJr:   c                h    | j         rt           d| j          d| j         dS t           d| j         dS )zGet the request url.z/groups/z
/datasets/z/executeQueries)r   BASE_URLr   selfs    r8   request_urlzPowerBIDataset.request_url>   sK     = 	baaaaaaaaFFdoFFFFr:   c                    | j         rdd| j         z   dS ddlm} | j        rF	 | j                            d          j         }dd|z   dS # t
          $ r} |d          |d}~ww xY w |d	          )
zGet the token.zapplication/jsonzBearer )zContent-TypeAuthorizationr   )ClientAuthenticationErrorz1https://analysis.windows.net/powerbi/api/.defaultz4Could not get a token from the supplied credentials.Nz No credential or token supplied.)r   azure.core.exceptionsrF   r   	get_token	Exception)rB   rF   r   excs       r8   headerszPowerBIDataset.headersE   s     : 	 2!*TZ!7  	
 	
 	
 	
 	
 	
 ? 	11G   %7%.%6      //J  ('(JKKKs   &A
 

A%A  A%Iterable[str]c                    | j         S )zGet names of tables available.r   rA   s    r8   get_table_nameszPowerBIDataset.get_table_names`   s    r:   c                    | j         r6d                    d | j                                         D                       S dS )zGet the available schema's., c                "    g | ]\  }}| d | S )z: r2   )r6   keyvalues      r8   r9   z.PowerBIDataset.get_schemas.<locals>.<listcomp>g   s*    WWWJC////WWWr:   z9No known schema's yet. Use the schema_powerbi tool first.)r(   joinitemsrA   s    r8   get_schemaszPowerBIDataset.get_schemasd   sB    < 	Y99WW$,BTBTBVBVWWWXXXJJr:   c                *    |                                  S )z-Information about all tables in the database.)get_table_inforA   s    r8   
table_infozPowerBIDataset.table_infoj   s     ""$$$r:   Optional[Union[List[str], str]]Optional[List[str]]c                    |t          |t                    r}t          |          dk    rj|d         dk    r^d |D             } fd|D             r.t                              dd                                         fd|D             }|r|ndS t          |t                    r<|dk    r6| j        vrt                              d	|           dS t          |          gS  j        S )
zHGet the tables names that need to be queried, after checking they exist.Nr    c                ,    g | ]}t          |          S r2   r3   r5   s     r8   r9   z7PowerBIDataset._get_tables_to_query.<locals>.<listcomp>y   s     OOO%u 5 5OOOr:   c                &    g | ]}|j         v|S r2   rN   r6   r7   rB   s     r8   r9   z7PowerBIDataset._get_tables_to_query.<locals>.<listcomp>z   s-     ' ' '#uDDT7T7TE7T7T7Tr:   z!Table(s) %s not found in dataset.rQ   c                    g | ]}|v|	S r2   r2   )r6   r7   non_existing_tabless     r8   r9   z7PowerBIDataset._get_tables_to_query.<locals>.<listcomp>   s+       #uDW7W7WE7W7W7Wr:   zTable %s not found in dataset.)	
isinstancelistlenloggerwarningrU   r   r   r4   )rB   r   fixed_tablestablesrc   s   `   @r8   _get_tables_to_queryz#PowerBIDataset._get_tables_to_queryo   sF    ";--2$$q((Nb((OO;OOO' ' ' ''3' ' '# ' NN;		"566     '3   "(1vvT1+s++ 5r0A0Ad&666NN#C[QQQ4&{3344r:   tables_todoc                       fd|D             S )z-Get the tables that still need to be queried.c                &    g | ]}|j         v|S r2   )r(   ra   s     r8   r9   z3PowerBIDataset._get_tables_todo.<locals>.<listcomp>   s%    LLL%%t|2K2K2K2K2Kr:   r2   )rB   rl   s   ` r8   _get_tables_todozPowerBIDataset._get_tables_todo   s    LLLL;LLLLr:   c                x    fd| j                                         D             }d                    |          S )z=Create a string of the table schemas for the supplied tables.c                "    g | ]\  }}|v 	|S r2   r2   )r6   r7   schemar   s      r8   r9   z9PowerBIDataset._get_schema_for_tables.<locals>.<listcomp>   s.     
 
 
$uf@T@TF@T@T@Tr:   rQ   )r(   rV   rU   )rB   r   r(   s    ` r8   _get_schema_for_tablesz%PowerBIDataset._get_schema_for_tables   sM    
 
 
 
(,(:(:(<(<
 
 
 yy!!!r:   c                    |                      |          }|dS |                     |          }|D ]}|                     |           |                     |          S )'Get information about specified tables.NNo (valid) tables requested.)rk   ro   _get_schemars   )rB   r   tables_requestedrl   r7   s        r8   rY   zPowerBIDataset.get_table_info   ss      44[AA#11++,<==  	$ 	$EU####**+;<<<r:   c                    K                         |          }|dS                      |          }t          j         fd|D               d{V                       |          S )ru   Nrv   c                :    g | ]}                     |          S r2   )_aget_schemara   s     r8   r9   z2PowerBIDataset.aget_table_info.<locals>.<listcomp>   s'    QQQEt0077QQQr:   )rk   ro   asynciogatherrs   )rB   r   rx   rl   s   `   r8   aget_table_infozPowerBIDataset.aget_table_info   s        44[AA#11++,<==nQQQQ[QQQRRRRRRRR**+;<<<r:   r7   Nonec                   	 |                      d| j         d| d          }t          |d         d         d         d         d                   | j        |<   dS # t          $ r) t
                              d|           d	| j        |<   Y dS t          $ r1}t
                              d
||           d	| j        |<   Y d}~dS d}~ww xY w)Get the schema for a table.EVALUATE TOPN(rQ   )resultsr   rj   rows'Timeout while getting table info for %sunknown)Error while getting table info for %s: %sN)runr%   
json_to_mdr(   r   rg   rh   rI   rB   r7   resultrJ   s       r8   rw   zPowerBIDataset._get_schema   s    
	,XXK!?KK5KKK F #-VI->q-A(-KA-Nv-V"W"WDL 	, 	, 	,NNDeLLL"+DL 	, 	, 	,NNFsSSS"+DL	,s   AA /C	C&CCc                  K   	 |                      d| j         d| d           d{V }t          |d         d         d         d         d                   | j        |<   dS # t          $ r) t
                              d	|           d
| j        |<   Y dS t          $ r1}t
                              d||           d
| j        |<   Y d}~dS d}~ww xY w)r   r   rQ   r   Nr   r   rj   r   r   r   r   )arunr%   r   r(   r   rg   rh   rI   r   s       r8   r{   zPowerBIDataset._aget_schema   s     
	,99K!?KK5KKK       F #-VI->q-A(-KA-Nv-V"W"WDL! 	, 	, 	,NNDeLLL"+DL 	, 	, 	,NNFsSSS"+DL	,s   AA" "/C	C&C		Ccommanddict[str, Any]c                $    d| ig| j         ddidS )z(Create the json content for the request.queryincludeNullsT)queriesimpersonatedUserNameserializerSettings)r   )rB   r   s     r8   _create_json_contentz#PowerBIDataset._create_json_content   s.     !g-01$($?#14"8
 
 	
r:   c                    t                               d|           t          j        | j        |                     |          | j        d          }|j        dk    r	 dS |                                S )zAExecute a DAX command and return a json representing the results.Running command: %sr    )jsonrK   timeout  FTokenError: Could not login to PowerBI, please check your credentials.)	rg   debugrequestspostrC   r   rK   status_coder   )rB   r   responses      r8   r   zPowerBIDataset.run   sy    *G444=**733L	
 
 
 3&&X  }}r:   c           	       K   t                               d|           | j        r| j                            | j        | j        |                     |          t          d                    4 d{V 	 }|j        dk    r	 ddd          d{V  dS |	                    |j
                   d{V }|cddd          d{V  S # 1 d{V swxY w Y   t          j                    4 d{V }|                    | j        | j        |                     |          t          d                    4 d{V 	 }|j        dk    r%	 ddd          d{V  ddd          d{V  dS |	                    |j
                   d{V }|cddd          d{V  cddd          d{V  S # 1 d{V swxY w Y   	 ddd          d{V  dS # 1 d{V swxY w Y   dS )	z;Execute a DAX command and return the result asynchronously.r   r    )total)rK   r   r   Nr   r   )content_type)rg   r   r)   r   rC   rK   r   r   statusr   r   aiohttpClientSession)rB   r   r   response_jsonsessions        r8   r   zPowerBIDataset.arun   su     *G444? 
	%++ ..w77%B///	 ,   	% 	% 	% 	% 	% 	% 	% 	%
 ?c))c	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% '/mmAVm&W&W W W W W W W$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% (** 
	% 
	% 
	% 
	% 
	% 
	% 
	%g|| ..w77%B///	 $   	% 	% 	% 	% 	% 	% 	% 	%
 ?c))c	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% '/mmAVm&W&W W W W W W W$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	%	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	% 
	%s[   5C
"C


CC4AG F8G1"F8G8
G	GG	G
G'*G')r-   r.   r/   r   )r/   r   )r/   r'   )r/   rL   N)r   r[   r/   r\   )rl   r   r/   r   )r   r   r/   r   )r   r[   r/   r   )r7   r   r/   r   )r   r   r/   r   )r   r   r/   r   )#__name__
__module____qualname____doc____annotations__r   r   r   r   r   r%   dictr(   r)   r   model_configr   classmethodr>   propertyrC   rK   rO   rW   rZ   rk   ro   rs   rY   r~   rw   r{   r   r   r   r2   r:   r8   r   r      s          OOO"H"""",0J0000E,00000%*U1r%B%B%BBBBB#eD999G999926J6666: $  L _(###K K K [ $#K G G G XG L L L XL4       K K K K % % % X%
 >B         <M M M M" " " " >B
= 
= 
= 
= 
= >B	= 	= 	= 	= 	=, , , ,, , , ,
 
 
 
   % % % % % %r:   r   json_contents'List[Dict[str, Union[str, int, float]]]
table_namer   r/   r   c                l   t          |           dk    rdS d}| d                                         }|D ]P}|                    dd                              dd           |r|                    | dd           |d| dz  }Q|dz  }| D ]'}|                                D ]}|d| dz  }|dz  }(|S )	z*Convert a JSON object to a markdown table.r   r^   [.]z|  z|
)rf   keysreplacer-   )r   r   	output_mdrK   headerrowrT   s          r8   r   r      s    
 =QrIA##%%G $ $sC  ((b111 	1NNj+++R000^&^^^#		I  ZZ\\ 	' 	'Ee&IIU		r:   r7   c                n    d| v r0|                      d          s|                     d          sd|  dS | S )z9Add single quotes around table names that contain spaces.r   ')
startswithendswith)r7   s    r8   r4   r4     s@    
e||E,,S11|%..:M:M|5|||Lr:   r   )r   r   r   r   r/   r   )r7   r   r/   r   )#r   
__future__r   r|   loggingostypingr   r   r   r   r   r	   r
   r   r   r   r   pydanticr   r   r   r   requests.exceptionsr   	getLoggerr   rg   getenvr@   azure.core.credentialsr   r   r   r4   r2   r:   r8   <module>r      s   ) ) " " " " " "   				 L L L L L L L L L L L L L L L L L L   5 5 5 5 5 5 5 5            ( ' ' ' ' '		8	$	$29')MNN 7666666]% ]% ]% ]% ]%Y ]% ]% ]%D !%    ,     r:   