refac: async db

This commit is contained in:
Timothy Jaeryang Baek
2026-04-12 14:22:11 -05:00
parent b618d84065
commit 27169124f2
74 changed files with 4831 additions and 4479 deletions
+16 -16
View File
@@ -130,13 +130,13 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
]
models = models + arena_models
global_action_ids = {function.id for function in Functions.get_global_action_functions()}
enabled_action_ids = {function.id for function in Functions.get_functions_by_type('action', active_only=True)}
global_action_ids = {function.id for function in await Functions.get_global_action_functions()}
enabled_action_ids = {function.id for function in await Functions.get_functions_by_type('action', active_only=True)}
global_filter_ids = {function.id for function in Functions.get_global_filter_functions()}
enabled_filter_ids = {function.id for function in Functions.get_functions_by_type('filter', active_only=True)}
global_filter_ids = {function.id for function in await Functions.get_global_filter_functions()}
enabled_filter_ids = {function.id for function in await Functions.get_functions_by_type('filter', active_only=True)}
custom_models = Models.get_all_models()
custom_models = await Models.get_all_models()
# Single O(1) lookup: Ollama base names first, then exact IDs (exact wins).
base_model_lookup = {}
@@ -278,14 +278,14 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
all_function_ids.update(global_action_ids)
all_function_ids.update(global_filter_ids)
functions_by_id = {f.id: f for f in Functions.get_functions_by_ids(list(all_function_ids))}
functions_by_id = {f.id: f for f in await Functions.get_functions_by_ids(list(all_function_ids))}
# Pre-warm the function module cache once per unique function ID.
# This ensures each function's DB freshness check runs exactly once,
# not once per (model × function) pair.
for function_id in all_function_ids:
try:
get_function_module_from_cache(request, function_id)
await get_function_module_from_cache(request, function_id)
except Exception as e:
log.info(f'Failed to load function module for {function_id}: {e}')
@@ -312,7 +312,7 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
# Batch-fetch all function valves in one query to avoid N+1 DB hits
# inside get_action_priority (previously called per action × per model).
all_function_valves = Functions.get_function_valves_by_ids(list(all_function_ids))
all_function_valves = await Functions.get_function_valves_by_ids(list(all_function_ids))
def get_action_priority(action_id):
try:
@@ -377,11 +377,11 @@ async def get_all_models(request, refresh: bool = False, user: UserModel = None)
return models
def check_model_access(user, model, db=None):
async def check_model_access(user, model, db=None):
if model.get('arena'):
meta = model.get('info', {}).get('meta', {})
access_grants = meta.get('access_grants', [])
if not has_access(
if not await has_access(
user.id,
permission='read',
access_grants=access_grants,
@@ -389,12 +389,12 @@ def check_model_access(user, model, db=None):
):
raise Exception('Model not found')
else:
model_info = Models.get_model_by_id(model.get('id'), db=db)
model_info = await Models.get_model_by_id(model.get('id'), db=db)
if not model_info:
raise Exception('Model not found')
elif not (
user.id == model_info.user_id
or AccessGrants.has_access(
or await AccessGrants.has_access(
user_id=user.id,
resource_type='model',
resource_id=model_info.id,
@@ -405,7 +405,7 @@ def check_model_access(user, model, db=None):
raise Exception('Model not found')
def get_filtered_models(models, user, db=None):
async def get_filtered_models(models, user, db=None):
# Filter out models that the user does not have access to
if (
user.role == 'user' or (user.role == 'admin' and not BYPASS_ADMIN_ACCESS_CONTROL)
@@ -418,10 +418,10 @@ def get_filtered_models(models, user, db=None):
if info:
model_infos[model['id']] = info
user_group_ids = {group.id for group in Groups.get_groups_by_member_id(user.id, db=db)}
user_group_ids = {group.id for group in await Groups.get_groups_by_member_id(user.id, db=db)}
# Batch-fetch accessible resource IDs in a single query instead of N has_access calls
accessible_model_ids = AccessGrants.get_accessible_resource_ids(
accessible_model_ids = await AccessGrants.get_accessible_resource_ids(
user_id=user.id,
resource_type='model',
resource_ids=list(model_infos.keys()),
@@ -435,7 +435,7 @@ def get_filtered_models(models, user, db=None):
if model.get('arena'):
meta = model.get('info', {}).get('meta', {})
access_grants = meta.get('access_grants', [])
if has_access(
if await has_access(
user.id,
permission='read',
access_grants=access_grants,