From 7d3f55ae15494f74e48f86392010d08e38ae3b0c Mon Sep 17 00:00:00 2001 From: SteBaum Date: Mon, 5 Aug 2024 15:19:57 +0200 Subject: [PATCH 1/2] refactor: show last deployments in ascending order --- tdp/dao.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tdp/dao.py b/tdp/dao.py index 05974da7..f4365a0b 100644 --- a/tdp/dao.py +++ b/tdp/dao.py @@ -298,9 +298,16 @@ def get_deployments( self, limit: Optional[int] = None, offset: Optional[int] = None ) -> Iterable[DeploymentModel]: self._check_session() + subq = ( + self.session.query(DeploymentModel) + .order_by(desc(DeploymentModel.id)) + .limit(limit) + .subquery() + ) return ( self.session.query(DeploymentModel) + .join(subq, DeploymentModel.id == subq.c.id) .order_by(DeploymentModel.id) - .limit(limit) .offset(offset) + .all() ) From 9e8d7862d9abbeff719295fe17a3c498d3041b35 Mon Sep 17 00:00:00 2001 From: Paul Farault Date: Tue, 22 Oct 2024 12:42:11 +0200 Subject: [PATCH 2/2] fix: get last deployments Fixes #616 return the last deployments insteads of the firsts --- tdp/cli/commands/browse.py | 2 +- tdp/dao.py | 43 ++++++++++++++++++++++++++++---------- tests/unit/test_dao.py | 4 ++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/tdp/cli/commands/browse.py b/tdp/cli/commands/browse.py index 5604a708..ba69dafb 100644 --- a/tdp/cli/commands/browse.py +++ b/tdp/cli/commands/browse.py @@ -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: diff --git a/tdp/dao.py b/tdp/dao.py index f4365a0b..a1faf10f 100644 --- a/tdp/dao.py +++ b/tdp/dao.py @@ -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 @@ -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() ) diff --git a/tests/unit/test_dao.py b/tests/unit/test_dao.py index 17acf061..681172b8 100644 --- a/tests/unit/test_dao.py +++ b/tests/unit/test_dao.py @@ -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), )