2023-06-11 00:59:16 +03:00
|
|
|
import time
|
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
from auth import AuthBearer, get_current_user
|
2023-06-15 15:43:40 +03:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
2023-09-13 14:47:12 +03:00
|
|
|
from models import Brain, UserIdentity, UserUsage
|
2023-08-21 13:25:16 +03:00
|
|
|
from repository.brain import get_user_default_brain
|
2023-08-21 15:05:13 +03:00
|
|
|
from repository.user_identity.get_user_identity import get_user_identity
|
|
|
|
from repository.user_identity.update_user_properties import (
|
|
|
|
UserUpdatableProperties,
|
|
|
|
update_user_properties,
|
2023-08-01 10:24:57 +03:00
|
|
|
)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
user_router = APIRouter()
|
2023-06-12 18:58:05 +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-07-04 18:56:54 +03:00
|
|
|
async def get_user_endpoint(
|
2023-08-21 15:05:13 +03:00
|
|
|
request: Request, current_user: UserIdentity = Depends(get_current_user)
|
2023-07-04 18:56:54 +03:00
|
|
|
):
|
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-11 00:59:16 +03:00
|
|
|
|
2023-09-13 14:47:12 +03:00
|
|
|
userDailyUsage = UserUsage(
|
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
openai_api_key=current_user.openai_api_key,
|
|
|
|
)
|
|
|
|
userSettings = userDailyUsage.get_user_settings()
|
|
|
|
max_brain_size = userSettings.get("max_brain_size", 1000000000)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
date = time.strftime("%Y%m%d")
|
2023-09-13 14:47:12 +03:00
|
|
|
max_requests_number = userSettings.get("max_requests_number", 10)
|
2023-08-21 15:05:13 +03:00
|
|
|
|
|
|
|
userDailyUsage = UserUsage(id=current_user.id)
|
|
|
|
requests_stats = userDailyUsage.get_user_usage()
|
2023-08-03 11:37:13 +03:00
|
|
|
default_brain = get_user_default_brain(current_user.id)
|
2023-06-30 10:08:40 +03:00
|
|
|
|
|
|
|
if default_brain:
|
2023-08-03 11:37:13 +03:00
|
|
|
defaul_brain_size = Brain(id=default_brain.brain_id).brain_size
|
2023-07-04 18:56:54 +03:00
|
|
|
else:
|
2023-06-30 10:08:40 +03:00
|
|
|
defaul_brain_size = 0
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
return {
|
|
|
|
"email": current_user.email,
|
|
|
|
"max_brain_size": max_brain_size,
|
|
|
|
"current_brain_size": defaul_brain_size,
|
|
|
|
"max_requests_number": max_requests_number,
|
|
|
|
"requests_stats": requests_stats,
|
2023-09-13 14:47:12 +03:00
|
|
|
"models": userSettings.get("models", []),
|
2023-07-04 18:56:54 +03:00
|
|
|
"date": date,
|
2023-08-04 21:25:09 +03:00
|
|
|
"id": current_user.id,
|
2023-07-04 18:56:54 +03:00
|
|
|
}
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@user_router.put(
|
|
|
|
"/user/identity",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
def update_user_identity_route(
|
2023-08-21 15:05:13 +03:00
|
|
|
user_identity_updatable_properties: UserUpdatableProperties,
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-08-01 10:24:57 +03:00
|
|
|
) -> UserIdentity:
|
|
|
|
"""
|
|
|
|
Update user identity.
|
|
|
|
"""
|
2023-08-21 15:05:13 +03:00
|
|
|
return update_user_properties(current_user.id, user_identity_updatable_properties)
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@user_router.get(
|
|
|
|
"/user/identity",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
def get_user_identity_route(
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-08-01 10:24:57 +03:00
|
|
|
) -> UserIdentity:
|
|
|
|
"""
|
|
|
|
Get user identity.
|
|
|
|
"""
|
|
|
|
return get_user_identity(current_user.id)
|