Skip to content

Commit

Permalink
fix: order_by on filtered hosts
Browse files Browse the repository at this point in the history
Closes: #511
Change-Id: I4d3af32f5e67cd6e744e0bfa8cc043c9a71d44ea
  • Loading branch information
grafuls committed Aug 27, 2024
1 parent fdcfda3 commit 49a865c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/quads/server/dao/baseDao.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ def safe_commit() -> bool:
return False

@classmethod
def create_query_select(cls, model, filters=None, columns=None, group_by=None):
def create_query_select(cls, model, filters=None, columns=None, group_by=None, order_by=None):
"""
Create a query to select data from a model with filters and columns.
:param model: The model to query.
:param filters: A list of filter expressions.
:param columns: A list of columns to select.
:param group_by: A column to group by.
:param order_by: A column to order by.
:return: The query result.
"""
group_by_column = None
if group_by:
group_by_column = cls.get_group_by_column(model=model, group_by=group_by)
query_columns = [group_by_column, func.count(group_by_column)]
Expand Down Expand Up @@ -113,8 +115,10 @@ def create_query_select(cls, model, filters=None, columns=None, group_by=None):
if value == "null":
value = None
query = query.filter(getattr(column, attr)(value))
if group_by:
if group_by_column:
query = query.group_by(group_by_column)
if order_by is not None and not group_by:
query = query.order_by(order_by)
return query.all()

@classmethod
Expand Down
14 changes: 11 additions & 3 deletions src/quads/server/dao/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

class HostDao(BaseDao):
@classmethod
def create_host(cls, name: str, model: str, host_type: str, default_cloud: str) -> Host:
def create_host(
cls, name: str, model: str, host_type: str, default_cloud: str
) -> Host:
_host_obj = cls.get_host(name)
if _host_obj:
raise EntryExisting
Expand Down Expand Up @@ -93,7 +95,11 @@ def get_hosts() -> List[Host]:

@staticmethod
def get_host_models():
host_models = db.session.query(Host.model, func.count(Host.model)).group_by(Host.model).all()
host_models = (
db.session.query(Host.model, func.count(Host.model))
.group_by(Host.model)
.all()
)
return host_models

@staticmethod
Expand Down Expand Up @@ -145,7 +151,9 @@ def filter_hosts_dict(data: dict) -> List[Host]:
value,
)
)
_hosts = HostDao.create_query_select(Host, filters=filter_tuples, group_by=group_by)
_hosts = HostDao.create_query_select(
Host, filters=filter_tuples, group_by=group_by, order_by=Host.name.asc()
)
return _hosts

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def test_report_available(self, remove_fixture):
assert self._caplog.messages[0].startswith("QUADS report for ")
assert self._caplog.messages[1].startswith("Percentage Utilized: ")
assert self._caplog.messages[2] == "Server Type | Total| Free| Scheduled| 2 weeks| 4 weeks"
assert self._caplog.messages[3].startswith("R930 | 1| 0| 100%")
assert self._caplog.messages[4].startswith("R640 | 1| 0| 100%")
assert self._caplog.messages[3].startswith("R640 | 1| 0| 100%")
assert self._caplog.messages[4].startswith("R930 | 1| 0| 100%")

def test_report_scheduled(self, remove_fixture):
today = datetime.now()
Expand Down

0 comments on commit 49a865c

Please sign in to comment.