Skip to content

Commit

Permalink
model and table entity rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ensaremirerol committed Nov 29, 2024
1 parent b11e850 commit f8aa527
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 68 deletions.
Empty file added server/factories/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions server/models/file_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ class FileMetadata:
stem: str
suffix: str
hash: str

def to_table(self):
"""
Convert to table representation
Returns:
dict: Table representation
"""
return {
"uuid": self.uuid,
"name": self.name,
"stem": self.stem,
"suffix": self.suffix,
"hash": self.hash,
}
4 changes: 2 additions & 2 deletions server/models/workspace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

from server.models.workspace_metadata import (
WorkspaceMetadataModel,
WorkspaceMetadata,
)


Expand All @@ -19,7 +19,7 @@ class WorkspaceModel:
used_uri_patterns (list[str]): list of used URI patterns
"""

metadata: WorkspaceMetadataModel
metadata: WorkspaceMetadata
sources: list[str]
mappings: list[str]
prefixes: dict[str, str]
Expand Down
12 changes: 7 additions & 5 deletions server/models/workspace_metadata.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from dataclasses import dataclass

from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceMetadata,
WorkspaceMetadataTable,
WorkspaceType,
)


@dataclass
class WorkspaceMetadataModel:
class WorkspaceMetadata:
"""
Model for workspace metadata
Expand All @@ -28,8 +28,10 @@ class WorkspaceMetadataModel:
enabled_features: list[str]

@staticmethod
def from_table(workspace_metadata: WorkspaceMetadata):
return WorkspaceMetadataModel(
def from_table(
workspace_metadata: WorkspaceMetadataTable,
):
return WorkspaceMetadata(
uuid=workspace_metadata.uuid,
name=workspace_metadata.name,
description=workspace_metadata.description,
Expand All @@ -41,7 +43,7 @@ def from_table(workspace_metadata: WorkspaceMetadata):
)

def to_table(self):
return WorkspaceMetadata(
return WorkspaceMetadataTable(
uuid=self.uuid,
name=self.name,
description=self.description,
Expand Down
4 changes: 2 additions & 2 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ async def get_data():

app.include_router(
workspaces_router,
prefix="/api/workspaces",
tags=["workspaces"],
prefix="/api/workspaces_metadata",
tags=["workspaces_metadata"],
)
9 changes: 0 additions & 9 deletions server/service_protocols/fs_service_protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@


class FSServiceProtocol(Protocol):
def service_type(self) -> str:
"""
Get service type
Returns:
str: service type
"""
...

def upload_file(
self,
name: str,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Protocol

from server.models.workspace_metadata import (
WorkspaceMetadataModel,
WorkspaceMetadata,
)
from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceType,
Expand All @@ -15,7 +15,7 @@ class WorkspaceMetadataService(Protocol):

def get_workspaces(
self,
) -> list[WorkspaceMetadataModel]:
) -> list[WorkspaceMetadata]:
"""
Get all workspaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ class WorkspaceServiceProtocol(Protocol):
Service for workspace operations
"""

def service_type(self) -> str:
"""
Get service type
Returns:
str: service type
"""
...

def get_workspace(self, uuid: str) -> WorkspaceModel:
"""
Get a workspace by UUID
Expand Down
14 changes: 9 additions & 5 deletions server/services/core/config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from server.services.core.sqlite_db_service import DBService
from server.services.core.sqlite_db_service.tables.config import (
Config,
ConfigTable,
)


Expand All @@ -14,21 +14,25 @@ def __init__(self, db_service: DBService):

def get(self, key: str) -> str | None:
with self._db_service.get_session() as session:
result: Config | None = session.get(Config, key)
result: ConfigTable | None = session.get(
ConfigTable, key
)
return result.value if result else None

def set(self, key: str, value: str):
with self._db_service.get_session() as session:
session.merge(Config(key=key, value=value))
session.merge(ConfigTable(key=key, value=value))
session.commit()

def delete(self, key: str):
with self._db_service.get_session() as session:
item = session.get(Config, key)
item = session.get(ConfigTable, key)
if not item:
return
session.execute(
delete(Config).where(Config.key == key)
delete(ConfigTable).where(
ConfigTable.key == key
)
)
session.commit()

Expand Down
8 changes: 4 additions & 4 deletions server/services/core/sqlite_db_service/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from server.services.core.sqlite_db_service.tables.config import (
Config,
ConfigTable,
)
from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceMetadata,
WorkspaceMetadataTable,
)

__all__ = [
"Config",
"WorkspaceMetadata",
"ConfigTable",
"WorkspaceMetadataTable",
]
2 changes: 1 addition & 1 deletion server/services/core/sqlite_db_service/tables/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)


class Config(Base):
class ConfigTable(Base):
__tablename__ = "config"

key: Mapped[str] = mapped_column(
Expand Down
24 changes: 24 additions & 0 deletions server/services/core/sqlite_db_service/tables/file_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column

from server.services.core.sqlite_db_service.base import (
Base,
)


class FileMetadataTable(Base):
__tablename__ = "file_metadata"

uuid: Mapped[str] = mapped_column(
String, primary_key=True
)
name: Mapped[str] = mapped_column(String)
stem: Mapped[str] = mapped_column(String)
suffix: Mapped[str] = mapped_column(String)
hash: Mapped[str] = mapped_column(String)

def __repr__(self):
return f"<FileMetadata(uuid={self.uuid}, name={self.name}, stem={self.stem}, suffix={self.suffix}, hash={self.hash})>"

def __str__(self):
return self.__repr__()
2 changes: 1 addition & 1 deletion server/services/core/sqlite_db_service/tables/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)


class Plugin(Base):
class PluginTable(Base):
"""
Table for plugin metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)


class PluginUIElement(Base):
class PluginUIElementTable(Base):
"""
Table for plugin UI elements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WorkspaceType(str, Enum):
REMOTE = "remote"


class WorkspaceMetadata(Base):
class WorkspaceMetadataTable(Base):
"""
Table for workspace metadata.
Expand Down
46 changes: 24 additions & 22 deletions server/services/core/workspace_metadata_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from server.const.err_enums import ErrCodes
from server.exceptions import ServerException
from server.models.workspace_metadata import (
WorkspaceMetadataModel,
WorkspaceMetadata,
)
from server.services.core.sqlite_db_service import DBService
from server.services.core.sqlite_db_service.tables.workspace_metadata import (
WorkspaceMetadata,
WorkspaceMetadataTable,
WorkspaceType,
)

Expand All @@ -27,15 +27,17 @@ def __init__(self, db_service: DBService):

def get_workspaces(
self,
) -> list[WorkspaceMetadataModel]:
) -> list[WorkspaceMetadata]:
self.logger.info("Getting workspaces")
with self._db_service.get_session() as session:
res = session.query(WorkspaceMetadata).all()
res = session.query(
WorkspaceMetadataTable
).all()
self.logger.info(
f"Fetched {len(res)} workspaces"
)
return list(
map(WorkspaceMetadataModel.from_table, res)
map(WorkspaceMetadata.from_table, res)
)

def create_workspace_metadata(
Expand All @@ -49,7 +51,7 @@ def create_workspace_metadata(
f"Creating workspace metadata for {name}"
)

workspace_metadata = WorkspaceMetadataModel(
workspace_metadata = WorkspaceMetadata(
uuid=uuid4().hex,
name=name,
description=description,
Expand All @@ -74,18 +76,18 @@ def update_workspace_metadata(
self.logger.info(
f"Updating workspace metadata: {uuid}"
)
query: Select[Tuple[WorkspaceMetadata]] = (
select(WorkspaceMetadata)
.where(WorkspaceMetadata.uuid == uuid)
query: Select[Tuple[WorkspaceMetadataTable]] = (
select(WorkspaceMetadataTable)
.where(WorkspaceMetadataTable.uuid == uuid)
.limit(1)
)
with self._db_service.get_session() as session:
res: Result[Tuple[WorkspaceMetadata]] = (
res: Result[Tuple[WorkspaceMetadataTable]] = (
session.execute(query)
)
item: Row[Tuple[WorkspaceMetadata]] | None = (
res.one_or_none()
)
item: (
Row[Tuple[WorkspaceMetadataTable]] | None
) = res.one_or_none()
if item is None:
err_msg = f"Workspace metadata with uuid {uuid} not found"
self.logger.error(err_msg)
Expand All @@ -107,19 +109,19 @@ def delete_workspace_metadata(self, uuid: str) -> None:
f"Deleting workspace metadata: {uuid}"
)

query: Select[Tuple[WorkspaceMetadata]] = (
select(WorkspaceMetadata)
.where(WorkspaceMetadata.uuid == uuid)
query: Select[Tuple[WorkspaceMetadataTable]] = (
select(WorkspaceMetadataTable)
.where(WorkspaceMetadataTable.uuid == uuid)
.limit(1)
)

with self._db_service.get_session() as session:
res: Result[Tuple[WorkspaceMetadata]] = (
res: Result[Tuple[WorkspaceMetadataTable]] = (
session.execute(query)
)
item: Row[Tuple[WorkspaceMetadata]] | None = (
res.one_or_none()
)
item: (
Row[Tuple[WorkspaceMetadataTable]] | None
) = res.one_or_none()
if item is None:
err_msg = f"Workspace metadata with uuid {uuid} not found"
self.logger.error(err_msg)
Expand All @@ -128,8 +130,8 @@ def delete_workspace_metadata(self, uuid: str) -> None:
ErrCodes.WORKSPACE_METADATA_NOT_FOUND,
)
session.execute(
delete(WorkspaceMetadata).where(
WorkspaceMetadata.uuid == uuid
delete(WorkspaceMetadataTable).where(
WorkspaceMetadataTable.uuid == uuid
)
)
session.commit()
Expand Down
Empty file.
15 changes: 15 additions & 0 deletions server/services/local/local_ontology_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from kink import inject

from server.service_protocols.fs_service_protocol import (
FSServiceProtocol,
)
from server.services.local.local_fs_service import (
LocalFSService,
)


@inject
class LocalOntologyService:
def __init__(self, fs_service: LocalFSService):
self.fs_service: FSServiceProtocol = fs_service

Loading

0 comments on commit f8aa527

Please sign in to comment.