Skip to content

Commit

Permalink
Merge pull request #96 from pepkit/dev
Browse files Browse the repository at this point in the history
Release v0.5.4
  • Loading branch information
khoroshevskyi authored Jul 17, 2023
2 parents 833bba3 + 8cb4d34 commit 0dd8021
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format.

## [0.5.4] -- 2023-07-14
- Added namespaces info method that returns list of namespaces with

## [0.5.3] -- 2023-07-13
- Fixed bugs in updating dates and descriptions

Expand Down
2 changes: 1 addition & 1 deletion pepdbagent/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.3"
__version__ = "0.5.4"
2 changes: 2 additions & 0 deletions pepdbagent/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# db_dialects
POSTGRES_DIALECT = "postgresql"

DEFAULT_LIMIT_INFO = 5
19 changes: 19 additions & 0 deletions pepdbagent/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ def value_should_not_contain_question(cls, v):
class Config:
extra = Extra.forbid
allow_population_by_field_name = True


class NamespaceInfo(BaseModel):
"""
Model with information about namespace
"""

namespace: str
number_of_projects: int


class ListOfNamespaceInfo(BaseModel):
"""
Namespace information response model
"""

number_of_namespaces: int
limit: int
results: List[NamespaceInfo]
52 changes: 48 additions & 4 deletions pepdbagent/modules/namespace.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging
from typing import List, Union

from sqlalchemy import distinct, func, or_, select
from sqlalchemy import distinct, func, or_, select, text
from sqlalchemy.sql.selectable import Select
from sqlalchemy.orm import Session

from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_OFFSET, PKG_NAME
from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_OFFSET, PKG_NAME, DEFAULT_LIMIT_INFO
from pepdbagent.db_utils import Projects, BaseEngine
from pepdbagent.models import Namespace, NamespaceList
from pepdbagent.models import Namespace, NamespaceList, NamespaceInfo, ListOfNamespaceInfo
from pepdbagent.utils import tuple_converter

_LOGGER = logging.getLogger(PKG_NAME)
Expand Down Expand Up @@ -71,6 +71,7 @@ def _get_namespace(
) -> List[Namespace]:
"""
Search for namespace by providing search string.
:param search_str: string of symbols, words, keywords to search in the
namespace name.
:param admin_nsp: tuple of namespaces where project can be retrieved if they are privet
Expand Down Expand Up @@ -114,9 +115,10 @@ def _get_namespace(
)
return results_list

def _count_namespace(self, search_str: str = None, admin_nsp: tuple = None) -> int:
def _count_namespace(self, search_str: str = None, admin_nsp: tuple = tuple()) -> int:
"""
Get number of found namespace. [This function is related to _get_namespaces]
:param search_str: string of symbols, words, keywords to search in the
namespace name.
:param admin_nsp: tuple of namespaces where project can be retrieved if they are privet
Expand Down Expand Up @@ -160,3 +162,45 @@ def _add_condition(
or_(Projects.private.is_(False), Projects.namespace.in_(admin_list))
)
return statement

def info(self, limit: int = DEFAULT_LIMIT_INFO) -> ListOfNamespaceInfo:
"""
Get list of top n namespaces in the database
:param limit: limit of results (top namespace )
:return: number_of_namespaces: int
limit: int
results: { namespace: str
number_of_projects: int
}
"""
total_number_of_namespaces = self._count_namespace()

statement = (
select(
func.count(Projects.namespace).label("number_of_projects"),
Projects.namespace,
)
.select_from(Projects)
.where(Projects.private.is_(False))
.limit(limit)
.order_by(text("number_of_projects desc"))
.group_by(Projects.namespace)
)

with Session(self._sa_engine) as session:
query_results = session.execute(statement).all()

list_of_results = []
for result in query_results:
list_of_results.append(
NamespaceInfo(
namespace=result.namespace,
number_of_projects=result.number_of_projects,
)
)
return ListOfNamespaceInfo(
number_of_namespaces=total_number_of_namespaces,
limit=limit,
results=list_of_results,
)
3 changes: 2 additions & 1 deletion pepdbagent/modules/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ def _overwrite(
found_prj.pep_schema = pep_schema
found_prj.config = project_dict[CONFIG_KEY]
found_prj.description = description
found_prj.last_update_date = datetime.datetime.now(datetime.timezone.utc)

# Deleting old samples and subsamples
if found_prj.samples_mapping:
for sample in found_prj.samples_mapping:
_LOGGER.info(f"deleting samples: {str(sample)}")
_LOGGER.debug(f"deleting samples: {str(sample)}")
session.delete(sample)

if found_prj.subsamples_mapping:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pepagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,14 @@ def test_annotation(self, initiate_pepdb_con):
def test_annotation_private(self, initiate_pepdb_con):
result = initiate_pepdb_con.namespace.get(admin="private_test")
assert len(result.results) == 4

def test_namespace_info(self, initiate_pepdb_con):
initiate_pepdb_con.project.update(
namespace="private_test",
name="derive",
tag="default",
update_dict={"is_private": False},
)
result = initiate_pepdb_con.namespace.info()
assert len(result.results) == 4
assert result.results[3].number_of_projects == 1

0 comments on commit 0dd8021

Please sign in to comment.