2023-06-11 00:59:16 +03:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
2023-06-14 22:21:13 +03:00
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
2023-06-15 15:43:40 +03:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
2023-06-19 23:54:01 +03:00
|
|
|
from models.settings import CommonsDep, common_dependencies
|
2023-06-11 00:59:16 +03:00
|
|
|
from models.users import User
|
|
|
|
|
|
|
|
user_router = APIRouter()
|
2023-06-12 18:58:05 +03:00
|
|
|
|
|
|
|
MAX_BRAIN_SIZE_WITH_OWN_KEY = int(os.getenv("MAX_BRAIN_SIZE_WITH_KEY", 209715200))
|
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_unique_documents(vectors):
|
|
|
|
# Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
|
|
|
|
return [dict(t) for t in set(tuple(d.items()) for d in vectors)]
|
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_user_vectors(commons, email):
|
|
|
|
# Access the supabase table and get the vectors
|
2023-06-11 00:59:16 +03:00
|
|
|
user_vectors_response = commons['supabase'].table("vectors").select(
|
|
|
|
"name:metadata->>file_name, size:metadata->>file_size", count="exact") \
|
2023-06-12 18:58:05 +03:00
|
|
|
.filter("user_id", "eq", email)\
|
2023-06-11 00:59:16 +03:00
|
|
|
.execute()
|
2023-06-12 18:58:05 +03:00
|
|
|
return user_vectors_response.data
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_user_request_stats(commons, email):
|
|
|
|
requests_stats = commons['supabase'].from_('users').select(
|
|
|
|
'*').filter("email", "eq", email).execute()
|
|
|
|
return requests_stats.data
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-15 15:43:40 +03:00
|
|
|
@user_router.get("/user", dependencies=[Depends(AuthBearer())], tags=["User"])
|
2023-06-19 23:54:01 +03:00
|
|
|
async def get_user_endpoint(request: Request, current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Get user information and statistics.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns the user's email, maximum brain size, current brain size, maximum requests number, requests statistics, and the current date.
|
|
|
|
|
|
|
|
This endpoint retrieves information and statistics about the authenticated user. It includes the user's email, maximum brain size,
|
|
|
|
current brain size, maximum requests number, requests statistics, and the current date. The brain size is calculated based on the
|
|
|
|
user's uploaded vectors, and the maximum brain size is obtained from the environment variables. The requests statistics provide
|
|
|
|
information about the user's API usage.
|
|
|
|
"""
|
2023-06-19 23:54:01 +03:00
|
|
|
commons = common_dependencies()
|
2023-06-12 18:58:05 +03:00
|
|
|
user_vectors = get_user_vectors(commons, current_user.email)
|
|
|
|
user_unique_vectors = get_unique_documents(user_vectors)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
current_brain_size = sum(float(doc.get('size', 0)) for doc in user_unique_vectors)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
max_brain_size = int(os.getenv("MAX_BRAIN_SIZE", 0))
|
2023-06-11 00:59:16 +03:00
|
|
|
if request.headers.get('Openai-Api-Key'):
|
2023-06-12 18:58:05 +03:00
|
|
|
max_brain_size = MAX_BRAIN_SIZE_WITH_OWN_KEY
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
date = time.strftime("%Y%m%d")
|
|
|
|
max_requests_number = os.getenv("MAX_REQUESTS_NUMBER")
|
|
|
|
|
|
|
|
requests_stats = get_user_request_stats(commons, current_user.email)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
return {"email": current_user.email,
|
2023-06-11 00:59:16 +03:00
|
|
|
"max_brain_size": max_brain_size,
|
|
|
|
"current_brain_size": current_brain_size,
|
|
|
|
"max_requests_number": max_requests_number,
|
2023-06-12 18:58:05 +03:00
|
|
|
"requests_stats" : requests_stats,
|
2023-06-11 00:59:16 +03:00
|
|
|
"date": date,
|
|
|
|
}
|