Skip to content

Commit

Permalink
fix: get last deployments
Browse files Browse the repository at this point in the history
Fixes #616 return the last deployments insteads of the firsts
  • Loading branch information
PaulFarault committed Oct 22, 2024
1 parent 7d3f55a commit 9e8d786
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
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
43 changes: 32 additions & 11 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,20 +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()
subq = (
self.session.query(DeploymentModel)
.order_by(desc(DeploymentModel.id))
.limit(limit)
.subquery()

# 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)
.join(subq, DeploymentModel.id == subq.c.id)
.order_by(DeploymentModel.id)
.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

0 comments on commit 9e8d786

Please sign in to comment.