Spaces:
Sleeping
Sleeping
File size: 3,255 Bytes
a3c7b61 81c40fc 84d1290 a3c7b61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import os
from dotenv import load_dotenv
from backend.mood_extraction import get_recent_mood_entries
import time
from backend.cache_utils import get_cached_user_data, cache_user_data
from backend.credentials import setup_google_credentials
setup_google_credentials()
# Load from .env file
load_dotenv()
service_account_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
from google.cloud import firestore
db = firestore.Client()
def get_user_profile(user_id: str):
doc_ref = db.collection("users").document(user_id).collection("profile").document("general")
doc = doc_ref.get()
return doc.to_dict() if doc.exists else {}
def get_user_goals(user_id: str):
goals_ref = db.collection("goals")
query_ref = goals_ref.where("user_id", "==", user_id)
results = query_ref.stream()
return [doc.to_dict() for doc in results]
def get_user_data(user_id: str):
start_time = time.time()
# Try to get from cache first
cached_data = get_cached_user_data(user_id)
if cached_data:
print(f"[TIMING] User data (cached): {(time.time() - start_time) * 1000:.2f}ms")
return cached_data
# Cache miss - fetch fresh data
print("[CACHE] User data cache miss, fetching fresh data...")
profile = get_user_profile(user_id)
goals = get_user_goals(user_id)
recent_moods = get_recent_mood_entries(user_id, days=60)
result = {
"profile": profile,
"goals": goals,
"recent_moods": recent_moods
}
# Cache the result
cache_user_data(user_id, result)
fetch_time = (time.time() - start_time) * 1000
print(f"[TIMING] User data fetch (fresh): {fetch_time:.2f}ms")
return result
def format_profile_goals_and_moods(user_data):
profile = user_data.get("profile", {})
goals = user_data.get("goals", [])
moods = user_data.get("recent_moods", [])
profile_text = (
f"User Profile:\n"
f"Name: {profile.get('name', '[unknown]')}\n"
f"Age: {profile.get('age', '[unknown]')}\n"
f"Gender: {profile.get('gender', '[unknown]')}\n"
)
goals_text = ""
if goals:
goals_text = "User Goals:\n" + "\n".join(
[f"- {g.get('goalName', '[No name]')}: {g.get('goalDescription', '[No description]')}" for g in goals]
) + "\n"
moods_text = ""
if moods:
moods_text = "Recent Mood Entries:\n" + "\n".join(
[f"{m.get('endDate', '[no date]')}: {m.get('mood', '[no mood]')} | Emotions: {', '.join(m.get('emotions', []))} | Note: {m.get('note', '')[:40]}..." for m in moods]
) + "\n"
return profile_text + goals_text + moods_text
def format_profile_and_goals(user_data):
profile = user_data.get("profile", {})
goals = user_data.get("goals", [])
profile_text = (
f"User Profile:\n"
f"Name: {profile.get('name', '[unknown]')}\n"
f"Age: {profile.get('age', '[unknown]')}\n"
f"Gender: {profile.get('gender', '[unknown]')}\n"
)
goals_text = ""
if goals:
goals_text = "User Goals:\n" + "\n".join(
[f"- {g.get('goalName', '[No name]')}: {g.get('goalDescription', '[No description]')}" for g in goals]
) + "\n"
return profile_text + goals_text
|