Skip to content

Commit 868c687

Browse files
authored
feat: add workspaces list endpoint by provider id (#1099)
* feat: add workspaces list endpoint by provider id * leftover * refactor: simplify return values
1 parent b23effd commit 868c687

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/codegate/api/v1.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from codegate import __version__
1313
from codegate.api import v1_models, v1_processing
1414
from codegate.db.connection import AlreadyExistsError, DbReader
15-
from codegate.db.models import AlertSeverity
15+
from codegate.db.models import AlertSeverity, WorkspaceWithModel
1616
from codegate.providers import crud as provendcrud
1717
from codegate.workspaces import crud
1818

@@ -532,6 +532,22 @@ async def set_workspace_muxes(
532532
return Response(status_code=204)
533533

534534

535+
@v1.get(
536+
"/workspaces/{provider_id}",
537+
tags=["Workspaces"],
538+
generate_unique_id_function=uniq_name,
539+
)
540+
async def list_workspaces_by_provider(
541+
provider_id: UUID,
542+
) -> List[WorkspaceWithModel]:
543+
"""List workspaces by provider ID."""
544+
try:
545+
return await wscrud.workspaces_by_provider(provider_id)
546+
547+
except Exception as e:
548+
raise HTTPException(status_code=500, detail=str(e))
549+
550+
535551
@v1.get("/alerts_notification", tags=["Dashboard"], generate_unique_id_function=uniq_name)
536552
async def stream_sse():
537553
"""

src/codegate/db/connection.py

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ProviderModel,
2929
Session,
3030
WorkspaceRow,
31+
WorkspaceWithModel,
3132
WorkspaceWithSessionInfo,
3233
)
3334
from codegate.db.token_usage import TokenUsageParser
@@ -721,6 +722,23 @@ async def get_workspace_by_name(self, name: str) -> Optional[WorkspaceRow]:
721722
)
722723
return workspaces[0] if workspaces else None
723724

725+
async def get_workspaces_by_provider(self, provider_id: str) -> List[WorkspaceWithModel]:
726+
sql = text(
727+
"""
728+
SELECT
729+
w.id, w.name, m.provider_model_name
730+
FROM workspaces w
731+
JOIN muxes m ON w.id = m.workspace_id
732+
WHERE m.provider_endpoint_id = :provider_id
733+
AND w.deleted_at IS NULL
734+
"""
735+
)
736+
conditions = {"provider_id": provider_id}
737+
workspaces = await self._exec_select_conditions_to_pydantic(
738+
WorkspaceWithModel, sql, conditions, should_raise=True
739+
)
740+
return workspaces
741+
724742
async def get_archived_workspace_by_name(self, name: str) -> Optional[WorkspaceRow]:
725743
sql = text(
726744
"""

src/codegate/db/models.py

+8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ class WorkspaceWithSessionInfo(BaseModel):
189189
session_id: Optional[str]
190190

191191

192+
class WorkspaceWithModel(BaseModel):
193+
"""Returns a workspace ID with model name"""
194+
195+
id: str
196+
name: WorkspaceNameStr
197+
provider_model_name: str
198+
199+
192200
class ActiveWorkspace(BaseModel):
193201
"""Returns a full active workspace object with the
194202
with the session information.

src/codegate/workspaces/crud.py

+7
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ async def get_workspace_by_name(self, workspace_name: str) -> db_models.Workspac
218218
raise WorkspaceDoesNotExistError(f"Workspace {workspace_name} does not exist.")
219219
return workspace
220220

221+
async def workspaces_by_provider(self, provider_id: uuid) -> List[db_models.WorkspaceWithModel]:
222+
"""Get the workspaces by provider."""
223+
224+
workspaces = await self._db_reader.get_workspaces_by_provider(str(provider_id))
225+
226+
return workspaces
227+
221228
async def get_muxes(self, workspace_name: str) -> List[mux_models.MuxRule]:
222229
# Verify if workspace exists
223230
workspace = await self._db_reader.get_workspace_by_name(workspace_name)

0 commit comments

Comments
 (0)