Skip to content

Commit

Permalink
Merge pull request #1639 from Agenta-AI/1599-age-164-improvement-spee…
Browse files Browse the repository at this point in the history
…d-up-the-endpoint-get_config

[Improvement]: speed up the endpoint get config
  • Loading branch information
mmabrouk authored May 30, 2024
2 parents 7158b3f + da57485 commit ddab8d8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
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))
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))
.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

0 comments on commit ddab8d8

Please sign in to comment.