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

Release 0.11.1 #147

Merged
merged 12 commits into from
Sep 18, 2024
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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.11.1] -- 2024-09-04
- Added metadata table for namespace tar files

## [0.11.0] -- 2024-07-24
- Added validation schemas

2 changes: 1 addition & 1 deletion pepdbagent/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.0"
__version__ = "0.11.1"
13 changes: 13 additions & 0 deletions pepdbagent/db_utils.py
Original file line number Diff line number Diff line change
@@ -364,6 +364,19 @@ class SchemaGroupRelations(Base):
)


class TarNamespace(Base):

__tablename__ = "tar_namespace"

id: Mapped[int] = mapped_column(primary_key=True)
namespace: Mapped[str] = mapped_column(ForeignKey("users.namespace", ondelete="CASCADE"))
file_path: Mapped[str] = mapped_column(nullable=False)
submission_date: Mapped[datetime.datetime] = mapped_column(default=deliver_update_date)
start_period: Mapped[datetime.datetime] = mapped_column(nullable=True)
end_period: Mapped[datetime.datetime] = mapped_column(nullable=True)
number_of_projects: Mapped[int] = mapped_column(default=0)


class BaseEngine:
"""
A class with base methods, that are used in several classes. e.g. fetch_one or fetch_all
25 changes: 24 additions & 1 deletion pepdbagent/models.py
Original file line number Diff line number Diff line change
@@ -280,7 +280,7 @@ class SchemaGroupAnnotation(BaseModel):

namespace: str
name: str
description: Optional[str]
description: Optional[str] = ""
schemas: List[SchemaAnnotation]


@@ -293,3 +293,26 @@ class SchemaGroupSearchResult(BaseModel):
limit: int
offset: int
results: List[SchemaGroupAnnotation]


class TarNamespaceModel(BaseModel):
"""
Geo project model
"""

identifier: int = None
namespace: str
file_path: str
submission_date: datetime.datetime = None
start_period: datetime.datetime
end_period: datetime.datetime
number_of_projects: int = 0


class TarNamespaceModelReturn(BaseModel):
"""
Geo project model
"""

count: int
results: List[TarNamespaceModel]
77 changes: 75 additions & 2 deletions pepdbagent/modules/namespace.py
Original file line number Diff line number Diff line change
@@ -3,19 +3,21 @@
from datetime import datetime, timedelta
from typing import List, Tuple, Union

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

from pepdbagent.const import DEFAULT_LIMIT, DEFAULT_LIMIT_INFO, DEFAULT_OFFSET, PKG_NAME
from pepdbagent.db_utils import BaseEngine, Projects, User
from pepdbagent.db_utils import BaseEngine, Projects, User, TarNamespace
from pepdbagent.exceptions import NamespaceNotFoundError
from pepdbagent.models import (
ListOfNamespaceInfo,
Namespace,
NamespaceInfo,
NamespaceList,
NamespaceStats,
TarNamespaceModel,
TarNamespaceModelReturn,
)
from pepdbagent.utils import tuple_converter

@@ -300,3 +302,74 @@ def stats(self, namespace: str = None, monthly: bool = False) -> NamespaceStats:
projects_updated=counts_last_update,
projects_created=counts_submission,
)

def upload_tar_info(self, tar_info: TarNamespaceModel) -> None:
"""
Upload metadata of tar GEO files

tar_info: TarNamespaceModel
:return: None
"""

with Session(self._sa_engine) as session:
new_tar = TarNamespace(
file_path=tar_info.file_path,
namespace=tar_info.namespace,
start_period=tar_info.start_period,
end_period=tar_info.end_period,
number_of_projects=tar_info.number_of_projects,
)
session.add(new_tar)
session.commit()

_LOGGER.info("Geo tar info was uploaded successfully!")

def get_tar_info(self, namespace: str) -> TarNamespaceModelReturn:
"""
Get metadata of tar GEO files

:param namespace: namespace of the tar files

:return: list with geo data
"""

with Session(self._sa_engine) as session:
tar_info = session.scalars(
select(TarNamespace)
.where(TarNamespace.namespace == namespace)
.order_by(TarNamespace.submission_date.desc())
)

results = []
for result in tar_info:
results.append(
TarNamespaceModel(
identifier=result.id,
namespace=result.namespace,
file_path=result.file_path,
start_period=result.start_period,
end_period=result.end_period,
submission_date=result.submission_date,
number_of_projects=result.number_of_projects,
)
)

return TarNamespaceModelReturn(count=len(results), results=results)

def delete_tar_info(self, namespace: str = None) -> None:
"""
Delete all metadata of tar GEO files

:param namespace: namespace of the tar files

:return: None
"""

with Session(self._sa_engine) as session:

delete_statement = delete(TarNamespace)
if namespace:
delete_statement = delete_statement.where(TarNamespace.namespace == namespace)
session.execute(delete_statement)
session.commit()
_LOGGER.info("Geo tar info was deleted successfully!")
6 changes: 5 additions & 1 deletion pepdbagent/modules/project.py
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
Samples,
Schemas,
Subsamples,
TarNamespace,
UpdateTypes,
User,
)
@@ -46,6 +47,8 @@
SchemaDoesNotExistError,
)
from pepdbagent.models import (
TarNamespaceModel,
TarNamespaceModelReturn,
HistoryAnnotationModel,
HistoryChangeModel,
ProjectDict,
@@ -657,6 +660,7 @@ def update(
),
history_sa_model=new_history,
)
found_prj.number_of_samples = len(update_dict["samples"])

if "subsamples" in update_dict:
if found_prj.subsamples_mapping:
@@ -1412,7 +1416,7 @@ def restore(

def clean_history(self, days: int = 90) -> None:
"""
Delete all history data that is older then 3 month, or specific number of days
Delete all history data that is older than 3 month, or specific number of days

:param days: number of days to keep history data
:return: None
52 changes: 52 additions & 0 deletions tests/test_tar_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from datetime import datetime

import pytest

from pepdbagent.models import TarNamespaceModel

from .utils import PEPDBAgentContextManager


@pytest.mark.skipif(
not PEPDBAgentContextManager().db_setup(),
reason="DB is not setup",
)
class TestGeoTar:
"""
Test project methods
"""

test_namespace = "namespace1"

tar_info = TarNamespaceModel(
namespace=test_namespace,
submission_date=datetime.now(),
start_period=datetime.now(),
end_period=datetime.now(),
number_of_projects=1,
file_path="blabla/test.tar",
)

def test_create_meta_tar(self):
with PEPDBAgentContextManager(add_data=True) as agent:

agent.namespace.upload_tar_info(tar_info=self.tar_info)

result = agent.namespace.get_tar_info(namespace=self.test_namespace)

assert result.count == 1
assert result.results[0].end_period.strftime(
"%Y:%m:%d"
) == self.tar_info.end_period.strftime("%Y:%m:%d")

def test_delete_meta_tar(self):
with PEPDBAgentContextManager(add_data=True) as agent:
agent.namespace.upload_tar_info(tar_info=self.tar_info)

result = agent.namespace.get_tar_info(namespace=self.test_namespace)
assert result.count == 1

agent.namespace.delete_tar_info()

result = agent.namespace.get_tar_info(namespace=self.test_namespace)
assert result.count == 0