CoolFace
Apppublic

BlessedRemzy/nostalgia_persuasive_system_backend

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
db_utils.cpython-311.pyc30 linesDownload Raw Back to __pycache__
1�

2fi�����ddlZddlZddlmZmZmZddlmZddlm	Z	e	e3��jjZedzZ
ee
��ejdd��Zd�Zd	ed4eeeffd�ZdS)�N)�Optional�Dict�Any)�load_dotenv)�Pathz.env�DATABASE_URLz3postgresql://postgres:postgres@localhost:5432/myappc�4�tjt��S)N)�psycopg2�connectr���YC:\Users\USER\Documents\Code\nostalgic-persuasive-model-final\fastapi-backend\db_utils.py�get_db_connectionrs����L�)�)�)r
�user_id�returnc�P�t��}|���}	|�d|f��|���}|rR|d}|d}|�t	|��nd|r|ndd�|���|���Sn)#t$r}td|����Yd}~nd}~wwxYw|���|���n-#|���|���wxYwddd�S)	a5    Fetch the latest stress and emotion context for a user from daily_habit_logs.6    7    Args:8        user_id: The user's ID9        10    Returns:11        Dict with 'stress_score' (float) and 'emotion' (str)12        Defaults to neutral if no log found.13    z�14            SELECT stress_level, emotion 15            FROM daily_habit_logs 16            WHERE user_id = %s 17            ORDER BY date DESC, created_at DESC 18            LIMIT 1;19        r�Ng�?�neutral)�stress_score�emotionzError fetching latest context: )r�cursor�execute�fetchone�float�close�	Exception�print)r�connr�row�stressr�es       r�fetch_latest_contextr"sY�����D�
�[�[�]�]�F��	�����Z�
	�	�	��o�o�����	���V�F��!�f�G�17�1C��f�
�
�
��&-�<�7�7�9���	�������20�21�����	���5�5�5�
�3��3�3�4�4�4�4�4�4�4�4�����5����	�������22�23������	�������24�25������������s0�AB$�#C6�$26C27�.C�C6�C28�29C6�6*D )�osr30�typingrrr�dotenvr�pathlibr�__file__�parent�PROJECT_ROOT�ENV_FILE�getenvrr�strr"rr
r�<module>r-s���	�	�	�	�����&�&�&�&�&�&�&�&�&�&��������������t�H�~�~�$�+���&� ����H�����r�y��)^�_�_��*�*�*�-�#�-�$�s�C�x�.�-�-�-�-�-�-r