
    ʯei?                        d dl mZ d dlmZ d dlZd dlmZ d dl	Z
ddlmZ ddZe ed      df	 	 	 	 	 	 	 dd	       Ze ed      df	 	 	 	 	 	 	 dd
       Zee
j                   dd              Zy)    )annotations)	timedeltaN   )extrac                     dt         j                  vr6t        j                  d      } | j                         t         j                  d<   t         j                  d   S )z
    Get or create a Snowpark session from the Streamlit connection.

    Returns:
        sp.Session: The Snowpark session object
    snowpark_session	snowflake)stsession_state
connectionsession)r   s    g/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/streamlit_extras/snowflake/connection.py_get_sessionr      sL     !1!11]];/
/9/A/A/C+,.//    )hoursTc                    t        j                  |      dd       } ||       }|r)|j                  j                  j	                         |_        |S )a  
    Execute a SQL query and cache the results.

    Args:
        query (str): The SQL query to execute
        ttl (timedelta | int | None): Time-to-live for the cache. Defaults to 2 hours.
            Set to None to use the default cache invalidation.
        lowercase_columns (bool): Whether to convert column names to lowercase. Defaults to True.

    Returns:
        pd.DataFrame: The query results as a pandas DataFrame
    ttlc                P    t               j                  |       j                         S N)r   sql	to_pandas)querys    r   _run_sqlzrun_sql.<locals>._run_sql,   s    ~!!%(2244r   )r   strreturnpd.DataFrame)r
   
cache_datacolumnsr   lower)r   r   lowercase_columnsr   dfs        r   run_sqlr#      sJ    & ]]s5 5 
%BZZ^^))+
Ir   c                    t        j                  |      	 	 	 	 	 	 	 	 dd       }| j                  j                  d   j                  } || ||      S )a  
    Convert a Snowpark DataFrame to a pandas DataFrame and cache the result.

    Args:
        df (sp.DataFrame): The Snowpark DataFrame to convert
        ttl (timedelta | int | None): Time-to-live for the cache. Defaults to 2 hours.
            Set to None to use the default cache invalidation.
        lowercase_columns (bool): Whether to convert column names to lowercase. Defaults to True.

    Returns:
        pd.DataFrame: The converted pandas DataFrame with cached results
    r   c                    |}| j                         }|r)|j                  j                  j                         |_        |S r   )r   r   r   r    )_dfr   r!   _r"   s        r   _run_snowparkz#run_snowpark.<locals>._run_snowparkK   s4     ]]_--/BJ	r   r   )r!   )r&   sp.DataFramer   r   r!   boolr   r   )r
   r   _planqueriesr   )r"   r   r!   r(   r   s        r   run_snowparkr-   8   sk    & ]]s		"%	:>			 	 HHQ##EU6GHHr   c                4    t               j                  |       S )a3  
    Get a Snowpark table for use in building a query.

    Args:
        table_name (str): Name of the table to retrieve

    Returns:
        sp.Table: A cached Snowpark Table object that can be used for querying.
            The result is cached so that metadata is not re-fetched from the database.
    )r   table)
table_names    r   	get_tabler1   \   s     >
++r   )r   z
sp.Session)r   r   r   timedelta | int | Noner!   r*   r   r   )r"   r)   r   r2   r!   r*   r   r   )r0   r   r   zsp.Table)
__future__r   datetimer   pandaspdsnowflake.snowparksnowparksp	streamlitr
    r   r   r#   r-   cache_resourcer1    r   r   <module>r>      s    "     
0  #,!"4"	  	 <  #,!"4" I I	 I  I 	 I  IF ,  ,r   