Repository: Agenta-AI/agenta
Issue category: Performance Issue

Issue:
[AGE-164] [Improvement] Speed up the endpoint /get_config
The call to retrieve the configuration takes 1.20 seconds, indicating that 'get_config' is highly unoptimised.

Here's the current process:

* It fetches the base_id.
* It checks permissions.
* It lists the environment for the app_id, which is obtained from the base_id.
* It searches one by one until it finds the correct name.
* It gets the variant_revision_id from the found environment.
* It returns the configuration from that variant.

This process involves a significant number of queries!

<sub>From [SyncLinear.com](https://synclinear.com) | [AGE-164](https://linear.app/agenta/issue/AGE-164/[improvement]-speed-up-the-endpoint-get-config)</sub>


Hints:

Patch:
diff --git a/agenta-backend/agenta_backend/models/converters.py b/agenta-backend/agenta_backend/models/converters.py
index 541462c3b0..de7c6dabfb 100644
--- a/agenta-backend/agenta_backend/models/converters.py
+++ b/agenta-backend/agenta_backend/models/converters.py
@@ -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),
diff --git a/agenta-backend/agenta_backend/routers/configs_router.py b/agenta-backend/agenta_backend/routers/configs_router.py
index 0670ded7f1..f05531d828 100644
--- a/agenta-backend/agenta_backend/routers/configs_router.py
+++ b/agenta-backend/agenta_backend/routers/configs_router.py
@@ -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,
@@ -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,
@@ -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,
diff --git a/agenta-backend/agenta_backend/services/db_manager.py b/agenta-backend/agenta_backend/services/db_manager.py
index ac1234cfa9..acf8c358cb 100644
--- a/agenta-backend/agenta_backend/services/db_manager.py
+++ b/agenta-backend/agenta_backend/services/db_manager.py
@@ -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
 
 
@@ -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()
     )
@@ -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,


Test patch: