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

[PAN-801]: Workspace datasets endpoint added #1207

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 3 additions & 4 deletions server/api/v1/datasets/datasets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from fastapi import APIRouter, Depends

from fastapi import APIRouter, Depends, Path
from app.controllers.datasets import DatasetController
from app.schemas.responses.datasets import WorkspaceDatasetsResponseModel
from core.factory import Factory

dataset_router = APIRouter()


@dataset_router.get("/")
@dataset_router.get("/", response_model=WorkspaceDatasetsResponseModel)
async def get_datasets(datasets_controller: DatasetController = Depends(Factory().get_datasets_controller)):
return await datasets_controller.get_all_datasets()
10 changes: 9 additions & 1 deletion server/api/v1/workspace/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
from app.schemas.responses.users import UserInfo
from core.fastapi.dependencies.current_user import get_current_user
from app.schemas.responses.users import WorkspaceUsersResponse
from app.schemas.responses.datasets import WorkspaceDatasetsResponseModel

workspace_router = APIRouter()


@workspace_router.get("/{workspace_id}/users", response_model=WorkspaceUsersResponse)
async def get_workspace_users(
workspace_id: UUID = Path(..., description="ID of the conversation"),
workspace_id: UUID = Path(..., description="ID of the workspace"),
workspace_controller: WorkspaceController = Depends(
Factory().get_space_controller
),
user: UserInfo = Depends(get_current_user)):
return await workspace_controller.get_workspace_users(workspace_id)

@workspace_router.get("/{workspace_id}/datasets", response_model=WorkspaceDatasetsResponseModel)
async def get_workspace_datasets(
workspace_id: UUID = Path(..., description="ID of the workspace"),
workspace_controller: WorkspaceController = Depends(Factory().get_space_controller)
):
return await workspace_controller.get_workspace_datasets(workspace_id)
16 changes: 10 additions & 6 deletions server/app/controllers/datasets.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from fastapi import status, HTTPException
from app.models import Dataset
from app.repositories import DatasetRepository
from app.repositories import DatasetRepository, WorkspaceRepository
from core.controller import BaseController
from core.exceptions.base import NotFoundException


from app.schemas.responses.datasets import WorkspaceDatasetsResponseModel
from typing import List
class DatasetController(BaseController[Dataset]):
def __init__(
self, dataset_repository: DatasetRepository
self,
dataset_repository: DatasetRepository,
space_repository: WorkspaceRepository
):
super().__init__(model=Dataset, repository=dataset_repository)
self.dataset_repository = dataset_repository
self.space_repository = space_repository

async def get_all_datasets(self):
async def get_all_datasets(self) -> WorkspaceDatasetsResponseModel:
datasets = await self.get_all()

if not datasets:
raise NotFoundException(
"No dataset found. Please restart the server and try again"
)

return {"data": datasets}
return WorkspaceDatasetsResponseModel(datasets=datasets)
30 changes: 24 additions & 6 deletions server/app/controllers/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from core.controller import BaseController
from core.database.transactional import Propagation, Transactional
from app.schemas.responses.users import WorkspaceUsersResponse
from app.schemas.responses.datasets import WorkspaceDatasetsResponseModel

class WorkspaceController(BaseController[Workspace]):
def __init__(
Expand Down Expand Up @@ -42,13 +43,30 @@ async def add_csv_datasets(
dataset_id=dataset.id, workspace_id=workspace_id
)


async def get_workspace_by_id(self, workspace_id: str):
workspace = await self.space_repository.get_by_id(id=workspace_id)
if not workspace:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace with id: {workspace_id} was not found"
)
return workspace

async def get_workspace_users(
self, workspace_id: str,
):
workspace = await self.space_repository.get_by_id(id=workspace_id)
if not workspace:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"Workspace with id: {workspace_id} was not found")

await self.get_workspace_by_id(workspace_id)
users = await self.space_repository.get_users_by_workspace_id(workspace_id)
return WorkspaceUsersResponse(users=users)
return WorkspaceUsersResponse(users=users)


async def get_workspace_datasets(self, workspace_id) -> WorkspaceDatasetsResponseModel:
await self.get_workspace_by_id(workspace_id)
datasets = await self.dataset_repository.get_all_by_workspace_id(workspace_id)
if not datasets:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No dataset found. Please restart the server and try again"
)
return WorkspaceDatasetsResponseModel(datasets=datasets)
11 changes: 10 additions & 1 deletion server/app/repositories/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from app.models import Connector, ConnectorType, Dataset
from app.models import Connector, ConnectorType, Dataset, DatasetSpace
from core.repository import BaseRepository
from uuid import UUID
from sqlalchemy import select


class DatasetRepository(BaseRepository[Dataset]):
Expand Down Expand Up @@ -33,3 +35,10 @@ async def create_dataset(
self.session.add(dataset)
await self.session.flush()
return dataset

async def get_all_by_workspace_id(self, workspace_id: UUID):
result = await self.session.execute(
select(Dataset).join(DatasetSpace).where(DatasetSpace.workspace_id == workspace_id)
)
datasets = result.scalars().all()
return datasets
22 changes: 22 additions & 0 deletions server/app/schemas/responses/datasets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from pydantic import BaseModel, UUID4
from typing import Optional, List
from datetime import datetime

class DatasetModel(BaseModel):
id: UUID4
name: str
table_name: str
description: Optional[str]
created_at: datetime
head: Optional[dict]
user_id: UUID4
organization_id: UUID4
connector_id: UUID4
field_descriptions: Optional[dict]
filterable_columns: Optional[dict]

class Config:
orm_mode = True

class WorkspaceDatasetsResponseModel(BaseModel):
datasets: List[DatasetModel]
5 changes: 3 additions & 2 deletions server/core/factory/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ def get_chat_controller(self, db_session=Depends(get_session)):

def get_datasets_controller(self, db_session=Depends(get_session)):
return DatasetController(
dataset_repository=self.dataset_repository(db_session=db_session)
)
dataset_repository=self.dataset_repository(db_session=db_session),
space_repository=self.space_repository(db_session=db_session)
)

def get_conversation_controller(self, db_session=Depends(get_session)):
return ConversationController(
Expand Down
Loading