Skip to content

Commit

Permalink
updated annotation view model
Browse files Browse the repository at this point in the history
  • Loading branch information
khoroshevskyi committed Jan 11, 2024
1 parent 98a89ae commit 92780bc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
18 changes: 13 additions & 5 deletions pepdbagent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,23 @@ class ProjectRegistryPath(BaseModel):

class ViewAnnotation(BaseModel):
"""
View annotation model
Project views model
"""

project_namespace: str
project_name: str
project_tag: str
name: str
description: Optional[str] = None
number_of_samples: int
number_of_samples: int = 0


class ProjectViews(BaseModel):
"""
View annotation model
"""

namespace: str
name: str
tag: str = DEFAULT_TAG
views: List[ViewAnnotation] = []


class CreateViewDictModel(BaseModel):
Expand Down
23 changes: 0 additions & 23 deletions pepdbagent/modules/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,26 +572,3 @@ def get_by_rp_list(

else:
return self.get_by_rp(registry_paths, admin)

def get_views(self, namespace: str, name: str, tag: str = DEFAULT_TAG) -> List[str]:
"""
Get list of views of the project
:param namespace: namespace of the project
:param name: name of the project
:param tag: tag of the project
:return: list of views of the project
"""
statement = select(Views.name).where(
Views.project_mapping.has(namespace=namespace, name=name, tag=tag),
and_(
Projects.name == name,
Projects.namespace == namespace,
Projects.tag == tag,
),
)
views = self._pep_db_engine.session_execute(statement).all()
if views:
return [v[0] for v in views]
else:
return []
36 changes: 35 additions & 1 deletion pepdbagent/modules/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pepdbagent.exceptions import ViewNotFoundError, SampleAlreadyInView

from pepdbagent.db_utils import BaseEngine, Samples, Projects, Views, ViewSampleAssociation
from pepdbagent.models import ViewAnnotation, CreateViewDictModel
from pepdbagent.models import ViewAnnotation, CreateViewDictModel, ProjectViews

_LOGGER = logging.getLogger(PKG_NAME)

Expand Down Expand Up @@ -369,3 +369,37 @@ def get_snap_view(
return peppy.Project.from_dict(
{"_config": config, "_sample_dict": samples, "_subsample_dict": None}
)

def get_views_annotation(
self, namespace: str, name: str, tag: str = DEFAULT_TAG
) -> Union[ProjectViews, None]:
"""
Get list of views of the project
:param namespace: namespace of the project
:param name: name of the project
:param tag: tag of the project
:return: list of views of the project
"""
statement = select(Views).where(
Views.project_mapping.has(namespace=namespace, name=name, tag=tag),
and_(
Projects.name == name,
Projects.namespace == namespace,
Projects.tag == tag,
),
)
views_list = []

with Session(self._sa_engine) as session:
views = session.scalars(statement)
for view in views:
views_list.append(
ViewAnnotation(
name=view.name,
description=view.description,
number_of_samples=len(view.samples),
)
)

return ProjectViews(namespace=namespace, name=name, tag=tag, views=views_list)
10 changes: 9 additions & 1 deletion tests/test_pepagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@ def test_get_snap_view(self, initiate_pepdb_con, namespace, name, sample_name, v
def test_get_view_list_from_project(
self, initiate_pepdb_con, namespace, name, sample_name, view_name
):
assert (
len(initiate_pepdb_con.view.get_views_annotation(namespace, name, "default").views)
== 0
)
initiate_pepdb_con.view.create(
"view1",
{
Expand All @@ -1191,4 +1195,8 @@ def test_get_view_list_from_project(
"sample_list": [sample_name, "pig_1h"],
},
)
assert initiate_pepdb_con.annotation.get_views(namespace, name, "default")[0] == "view1"
result = initiate_pepdb_con.view.get_views_annotation(namespace, name, "default")
assert (
len(initiate_pepdb_con.view.get_views_annotation(namespace, name, "default").views)
== 1
)

0 comments on commit 92780bc

Please sign in to comment.