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

browse the last deployments #617

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion tdp/cli/commands/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def browse(
return

# Print all deployments
_print_deployments(dao.get_deployments(limit=limit, offset=offset))
_print_deployments(dao.get_last_deployments(limit=limit, offset=offset))


def _print_deployments(deployments: Iterable[DeploymentModel]) -> None:
Expand Down
40 changes: 34 additions & 6 deletions tdp/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Iterable, NamedTuple, Optional

from sqlalchemy import Engine, Select, and_, case, desc, func, or_, select
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import aliased, sessionmaker

from tdp.core.cluster_status import ClusterStatus
from tdp.core.entities.hostable_entity_name import create_hostable_entity_name
Expand Down Expand Up @@ -294,13 +294,41 @@ def get_last_deployment(self) -> Optional[DeploymentModel]:
.first()
)

def get_deployments(
def get_last_deployments(
self, limit: Optional[int] = None, offset: Optional[int] = None
) -> Iterable[DeploymentModel]:
"""Get last deployments in ascending order.

Use limit and offset to paginate the results.

Args:
limit: The maximum number of deployments to return.
offset: The number of deployments to skip.

Example:
# Get the last 10 deployments.
>>> dao.get_last_deployments(limit=10, offset=0)
# Get the next 10 deployments.
>>> dao.get_last_deployments(limit=10, offset=10)
"""
self._check_session()

# Get the last deployments (in descending order).
reversed_deployments_query = self.session.query(DeploymentModel.id).order_by(
desc(DeploymentModel.id)
)
# Apply limit and offset.
if limit is not None:
reversed_deployments_query = reversed_deployments_query.limit(limit)
if offset is not None:
reversed_deployments_query = reversed_deployments_query.offset(offset)
# Alias the subquery for convenience.
reversed_deployments = aliased(
DeploymentModel, reversed_deployments_query.subquery()
)
# Return the deployments in ascending order.
return (
self.session.query(DeploymentModel)
.order_by(DeploymentModel.id)
.limit(limit)
.offset(offset)
self.session.query(reversed_deployments)
.order_by(reversed_deployments.id)
.all()
)
4 changes: 2 additions & 2 deletions tests/unit/test_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ def test_get_deployments(db_engine):
session.commit()
with Dao(db_engine) as dao:
assert assert_equal_values_in_model(
list(dao.get_deployments())[0],
list(dao.get_last_deployments())[0],
DeploymentModel(id=1, state=DeploymentStateEnum.SUCCESS),
)
assert assert_equal_values_in_model(
list(dao.get_deployments())[1],
list(dao.get_last_deployments())[1],
DeploymentModel(id=2, state=DeploymentStateEnum.PLANNED),
)

Expand Down
Loading