From 2766119a36f21a1606c49b4439ac61ed04dcbc6b Mon Sep 17 00:00:00 2001 From: Matt Garber Date: Tue, 15 Oct 2024 12:43:21 -0400 Subject: [PATCH] Order data from chart endpoint --- src/handlers/dashboard/get_chart_data.py | 6 +++++- tests/dashboard/test_get_chart_data.py | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/handlers/dashboard/get_chart_data.py b/src/handlers/dashboard/get_chart_data.py index 4da4f61..7cee102 100644 --- a/src/handlers/dashboard/get_chart_data.py +++ b/src/handlers/dashboard/get_chart_data.py @@ -70,8 +70,12 @@ def _build_query(query_params: dict, filters: list, path_params: dict) -> str: f"FROM \"{os.environ.get('GLUE_DB_NAME')}\".\"{dp_id}\" " f"{coalesce_str} " f"{query_params['column']} IS NOT Null {filter_str} " - f"GROUP BY {group_str}" + f"GROUP BY {group_str} " ) + if "stratifier" in query_params.keys(): + query_str += f"ORDER BY {query_params['stratifier']}, {query_params['column']}" + else: + query_str += f"ORDER BY {query_params['column']}" logging.debug(query_str) return query_str diff --git a/tests/dashboard/test_get_chart_data.py b/tests/dashboard/test_get_chart_data.py index 7acdbdf..e0ce189 100644 --- a/tests/dashboard/test_get_chart_data.py +++ b/tests/dashboard/test_get_chart_data.py @@ -37,7 +37,7 @@ def mock_data_frame(filter_param): {"data_package_id": "test_study"}, f'SELECT gender, sum(cnt) as cnt FROM "{TEST_GLUE_DB}"."test_study" ' "WHERE COALESCE (race) IS NOT Null AND gender IS NOT Null " - "GROUP BY gender", + "GROUP BY gender ORDER BY gender", ), ( {"column": "gender", "stratifier": "race"}, @@ -45,7 +45,7 @@ def mock_data_frame(filter_param): {"data_package_id": "test_study"}, f'SELECT race, gender, sum(cnt) as cnt FROM "{TEST_GLUE_DB}"."test_study" ' "WHERE gender IS NOT Null " - "GROUP BY race, gender", + "GROUP BY race, gender ORDER BY race, gender", ), ( {"column": "gender"}, @@ -54,7 +54,7 @@ def mock_data_frame(filter_param): f'SELECT gender, sum(cnt) as cnt FROM "{TEST_GLUE_DB}"."test_study" ' "WHERE COALESCE (race) IS NOT Null AND gender IS NOT Null " "AND gender LIKE 'female' " - "GROUP BY gender", + "GROUP BY gender ORDER BY gender", ), ( {"column": "gender", "stratifier": "race"}, @@ -63,7 +63,7 @@ def mock_data_frame(filter_param): f'SELECT race, gender, sum(cnt) as cnt FROM "{TEST_GLUE_DB}"."test_study" ' "WHERE gender IS NOT Null " "AND gender LIKE 'female' " - "GROUP BY race, gender", + "GROUP BY race, gender ORDER BY race, gender", ), ], )