Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement]: speed up the endpoint get config #1639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agenta-backend/agenta_backend/models/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ async def environment_db_to_output(
deployed_variant_name = None
revision = None

await environment_db.fetch_link(AppEnvironmentDB.deployed_app_variant_revision)
environment_output = EnvironmentOutput(
name=environment_db.name,
app_id=str(environment_db.app.id),
Expand Down
32 changes: 18 additions & 14 deletions agenta-backend/agenta_backend/routers/configs_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def get_config(
try:
base_db = await db_manager.fetch_base_by_id(base_id)

# detemine whether the user has access to the base
# determine whether the user has access to the base
if isCloudEE():
has_permission = await check_action_access(
user_uid=request.state.user_id,
Expand All @@ -111,15 +111,16 @@ async def get_config(
# in case environment_name is provided, find the variant deployed
if environment_name:
app_environments = await db_manager.list_environments(
app_id=str(base_db.app.id)
app_id=str(base_db.app.ref.id)
)
found_variant_revision = next(
(
app_environment.deployed_app_variant_revision
for app_environment in app_environments
if app_environment.name == environment_name
),
None,
)
found_variant = None
for app_environment in app_environments:
if app_environment.name == environment_name:
found_variant_revision = (
app_environment.deployed_app_variant_revision
)
break
if not found_variant_revision:
raise HTTPException(
status_code=400,
Expand All @@ -134,11 +135,14 @@ async def get_config(
config = found_variant_revision.config
elif config_name:
variants_db = await db_manager.list_variants_for_base(base_db)
found_variant = None
for variant_db in variants_db:
if variant_db.config_name == config_name:
found_variant = variant_db
break
found_variant = next(
(
variant_db
for variant_db in variants_db
if variant_db.config_name == config_name
),
None,
)
if not found_variant:
raise HTTPException(
status_code=400,
Expand Down
19 changes: 11 additions & 8 deletions agenta-backend/agenta_backend/services/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,15 @@ async def fetch_base_by_id(base_id: str) -> Optional[VariantBaseDB]:
Returns:
VariantBaseDB: The fetched base, or None if no base was found.
"""

if base_id is None:
raise Exception("No base_id provided")
base = await VariantBaseDB.find_one(
VariantBaseDB.id == ObjectId(base_id), fetch_links=True
)
if base is None:

base = await VariantBaseDB.find_one(VariantBaseDB.id == ObjectId(base_id))
mmabrouk marked this conversation as resolved.
Show resolved Hide resolved
if not base:
logger.error("Base not found")
return False
return None

return base


Expand Down Expand Up @@ -651,9 +652,7 @@ async def list_variants_for_base(base: VariantBaseDB) -> List[AppVariantDB]:
"""
assert base is not None, "base cannot be None"
app_variants_db = (
await AppVariantDB.find(
AppVariantDB.base.id == ObjectId(base.id), fetch_links=True
)
await AppVariantDB.find(AppVariantDB.base.id == ObjectId(base.id))
mmabrouk marked this conversation as resolved.
Show resolved Hide resolved
.sort("variant_name")
.to_list()
)
Expand Down Expand Up @@ -864,6 +863,10 @@ async def add_variant_from_base_and_config(
config_name=new_config_name,
parameters=parameters,
)

# Prefetch image in base_db
await base_db.fetch_link(VariantBaseDB.image)

db_app_variant = AppVariantDB(
app=previous_app_variant_db.app,
variant_name=new_variant_name,
Expand Down
Loading