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

[COST-4844] Serializer update for ordering by storageclass #5174

Merged
merged 4 commits into from
Jun 27, 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
18 changes: 13 additions & 5 deletions koku/api/report/ocp/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
DISTRIBUTED_COST_INTERNAL = {"distributed_cost": "cost_total_distributed"}


def order_by_field_requires_group_by(data, order_name, group_by_key):
def order_by_field_requires_group_by(data, order_name, group_by_keys):
error = {}
if order_name in data.get("order_by", {}) and group_by_key not in data.get("group_by", {}):
error["order_by"] = gettext(f"Cannot order by field {order_name} without grouping by {group_by_key}.")
raise serializers.ValidationError(error)
if order_name in data.get("order_by", {}):
# Ensure group_by_keys is a list of keys to check
if not isinstance(group_by_keys, list):
group_by_keys = [group_by_keys]

# Check if none of the required group_by keys are present
if not any(key in data.get("group_by", {}) for key in group_by_keys):
error["order_by"] = gettext(
f"Cannot order by field {order_name} without grouping by one of {', '.join(group_by_keys)}."
)
raise serializers.ValidationError(error)


class OCPGroupBySerializer(GroupSerializer):
Expand Down Expand Up @@ -165,7 +173,7 @@ def validate(self, data):
error["order_by"] = gettext("Cannot order by delta without a delta param")
raise serializers.ValidationError(error)
order_by_field_requires_group_by(data, DISTRIBUTED_COST_INTERNAL["distributed_cost"], "project")
order_by_field_requires_group_by(data, "storage_class", "persistentvolumeclaim")
order_by_field_requires_group_by(data, "storage_class", ["persistentvolumeclaim", "storageclass"])
order_by_field_requires_group_by(data, "persistentvolumeclaim", "persistentvolumeclaim")
if data.get("delta") == DISTRIBUTED_COST_INTERNAL["distributed_cost"] and "project" not in data.get(
"group_by", {}
Expand Down
47 changes: 25 additions & 22 deletions koku/api/report/test/ocp/test_ocp_query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,28 +131,31 @@ def test_cpu_memory_order_bys(self):

def test_storage_class_order_bys(self):
"""Test that we can order by the pvc values."""
url = "?group_by[project]=*&group_by[persistentvolumeclaim]=*&order_by[storage_class]=desc"
query_params = self.mocked_query_params(url, OCPVolumeView)
handler = OCPReportQueryHandler(query_params)
query_data = handler.execute_query()
self.assertIsNotNone(query_data.get("data"))
self.assertIsNotNone(query_data.get("total"))
self.assertIsNotNone(query_data["total"].get("storage_class"))
first_date = query_data["data"][0]
tested = False
for cluster in first_date.get("projects", []):
pvc_list = cluster.get("persistentvolumeclaims")
storage_class_order_result = []
expected = None
for pvc in pvc_list:
for pvc_value in pvc.get("values", []):
storage_class_order_result.append(pvc_value.get("storage_class"))
if not expected:
expected = deepcopy(storage_class_order_result)
expected.sort(reverse=True)
self.assertEqual(storage_class_order_result, expected)
tested = True
self.assertTrue(tested)
group_bys = ["persistentvolumeclaim", "storageclass"]
for group_by in group_bys:
with self.subTest(group_by=group_by):
url = f"?group_by[project]=*&group_by[{group_by}]=*&order_by[storage_class]=desc"
query_params = self.mocked_query_params(url, OCPVolumeView)
handler = OCPReportQueryHandler(query_params)
query_data = handler.execute_query()
self.assertIsNotNone(query_data.get("data"))
self.assertIsNotNone(query_data.get("total"))
self.assertIsNotNone(query_data["total"].get("storage_class"))
first_date = query_data["data"][0]
tested = False
for project in first_date.get("projects", []):
group_list = project.get(f"{group_by}s")
storage_class_order_result = []
expected = None
for element in group_list:
for element_value in element.get("values", []):
storage_class_order_result.append(element_value.get("storage_class"))
if not expected:
expected = deepcopy(storage_class_order_result)
expected.sort(reverse=True)
self.assertEqual(storage_class_order_result, expected)
tested = True
self.assertTrue(tested)

def test_persistentvolumeclaim_order_by(self):
"""Test that we can order by the pvc values."""
Expand Down
Loading