From 49f65459ef13e11589b2eaa0354364eb2f7d1a92 Mon Sep 17 00:00:00 2001 From: MVarshini Date: Fri, 13 Dec 2024 18:56:45 +0530 Subject: [PATCH] sorting string validation --- backend/app/api/v1/commons/ocp.py | 3 +- backend/app/api/v1/commons/quay.py | 3 +- backend/app/api/v1/commons/telco.py | 2 - backend/app/api/v1/commons/utils.py | 27 ++++++++++++ backend/app/api/v1/endpoints/ocp/ocpJobs.py | 41 +------------------ backend/app/api/v1/endpoints/quay/quayJobs.py | 41 +------------------ 6 files changed, 31 insertions(+), 86 deletions(-) diff --git a/backend/app/api/v1/commons/ocp.py b/backend/app/api/v1/commons/ocp.py index db0ed65..51551fb 100644 --- a/backend/app/api/v1/commons/ocp.py +++ b/backend/app/api/v1/commons/ocp.py @@ -20,8 +20,7 @@ async def getData( }, } if sort: - key, direction = sort.split(":") - query["sort"] = [{key: {"order": direction}}] + query["sort"] = utils.build_sort_terms(sort) es = ElasticService(configpath=configpath) response = await es.post( diff --git a/backend/app/api/v1/commons/quay.py b/backend/app/api/v1/commons/quay.py index b10874d..45dad74 100644 --- a/backend/app/api/v1/commons/quay.py +++ b/backend/app/api/v1/commons/quay.py @@ -16,8 +16,7 @@ async def getData( } if sort: - key, direction = sort.split(":") - query["sort"] = [{key: {"order": direction}}] + query["sort"] = utils.build_sort_terms(sort) es = ElasticService(configpath=configpath) response = await es.post( diff --git a/backend/app/api/v1/commons/telco.py b/backend/app/api/v1/commons/telco.py index 91cba71..b55e746 100644 --- a/backend/app/api/v1/commons/telco.py +++ b/backend/app/api/v1/commons/telco.py @@ -38,8 +38,6 @@ async def getData( "earliest_time": "{}T00:00:00".format(start_datetime.strftime("%Y-%m-%d")), "latest_time": "{}T23:59:59".format(end_datetime.strftime("%Y-%m-%d")), "output_mode": "json", - "sort_dir": "asc", - "sort_key": "test_type", } searchList = " OR ".join( ['test_type="{}"'.format(test_type) for test_type in test_types] diff --git a/backend/app/api/v1/commons/utils.py b/backend/app/api/v1/commons/utils.py index ddcef9d..1c2deb1 100644 --- a/backend/app/api/v1/commons/utils.py +++ b/backend/app/api/v1/commons/utils.py @@ -1,4 +1,6 @@ from app.services.search import ElasticService +from fastapi import HTTPException, status +import re async def getMetadata(uuid: str, configpath: str): @@ -65,3 +67,28 @@ def getReleaseStream(row): elif row["releaseStream"].__contains__("ec"): return "Engineering Candidate" return "Stable" + + +def build_sort_terms(sort_string: str) -> list[dict[str, str]]: + """ + + Validates and transforms a sort string in the format 'sort=key:direction' to + a list of dictionaries [{key: {"order": direction}}]. + + :param sort_string: str, input string in the format 'sort=key:direction' + + :return: list, transformed sort structure or raises a ValueError for invalid input + + """ + + pattern = r"^([\w]+):(asc|desc)$" + match = re.match(pattern, sort_string) + + if not match: + raise HTTPException( + status.HTTP_400_BAD_REQUEST, + f"Invalid sort string format. Expected 'sort=key:direction' with direction as 'asc' or 'desc'.", + ) + + key, direction = match.groups() + return [{key: {"order": direction}}] diff --git a/backend/app/api/v1/endpoints/ocp/ocpJobs.py b/backend/app/api/v1/endpoints/ocp/ocpJobs.py index f161c50..c0308ac 100644 --- a/backend/app/api/v1/endpoints/ocp/ocpJobs.py +++ b/backend/app/api/v1/endpoints/ocp/ocpJobs.py @@ -32,16 +32,10 @@ async def jobs( description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"], ), -<<<<<<< HEAD - pretty: bool = Query(False, description="Output content in pretty format."), - size: int = Query(None, description="Number of jobs to fetch"), - offset: int = Query(None, description="Offset Number to fetch jobs from"), -======= pretty: bool = Query(False, description="Output contet in pretty format."), size: int = Query(None, description="Number of jobs to fetch"), offset: int = Query(None, description="Offset Number to fetch jobs from"), - sort: str = Query(None, descption="To sort fields on specified direction"), ->>>>>>> bd597e7 (Sorting) + sort: str = Query(None, description="To sort fields on specified direction"), ): if start_date is None: start_date = datetime.utcnow().date() @@ -58,7 +52,6 @@ async def jobs( status_code=422, ) -<<<<<<< HEAD if offset and not size: raise HTTPException(400, f"offset {offset} specified without size") elif not offset and not size: @@ -79,38 +72,6 @@ async def jobs( "total": results["total"], "offset": offset + size, } -======= - if not offset: - offset = 0 - - if not size: - size = 10000 - offset = 0 - - if not sort: - sort = None - - results = await getData( - start_date, end_date, size, offset, sort, "ocp.elasticsearch" - ) - - if "data" in results and len(results["data"]) >= 1: - response = { - "startDate": start_date.__str__(), - "endDate": end_date.__str__(), - "results": results["data"].to_dict("records"), - "total": results["total"], - "offset": offset + size, - } - else: - response = { - "startDate": start_date.__str__(), - "endDate": end_date.__str__(), - "results": [], - "total": 0, - "offset": 0, - } ->>>>>>> bd597e7 (Sorting) if pretty: json_str = json.dumps(response, indent=4) diff --git a/backend/app/api/v1/endpoints/quay/quayJobs.py b/backend/app/api/v1/endpoints/quay/quayJobs.py index 3a21262..c7c7ce3 100644 --- a/backend/app/api/v1/endpoints/quay/quayJobs.py +++ b/backend/app/api/v1/endpoints/quay/quayJobs.py @@ -32,16 +32,10 @@ async def jobs( description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"], ), -<<<<<<< HEAD - pretty: bool = Query(False, description="Output content in pretty format."), - size: int = Query(None, description="Number of jobs to fetch"), - offset: int = Query(None, description="Offset Number to fetch jobs from"), -======= pretty: bool = Query(False, description="Output contet in pretty format."), size: int = Query(None, description="Number of jobs to fetch"), offset: int = Query(None, description="Offset Number to fetch jobs from"), - sort: str = Query(None, descption="To sort fields on specified direction"), ->>>>>>> bd597e7 (Sorting) + sort: str = Query(None, description="To sort fields on specified direction"), ): if start_date is None: start_date = datetime.utcnow().date() @@ -58,7 +52,6 @@ async def jobs( status_code=422, ) -<<<<<<< HEAD if offset and not size: raise HTTPException(400, f"offset {offset} specified without size") elif not offset and not size: @@ -80,38 +73,6 @@ async def jobs( "total": 0, "offset": 0, } -======= - if not offset: - offset = 0 - - if not size: - size = 10000 - offset = 0 - - if not sort: - sort = None - - results = await getData( - start_date, end_date, size, offset, sort, "quay.elasticsearch" - ) - - if "data" in results and len(results["data"]) >= 1: - response = { - "startDate": start_date.__str__(), - "endDate": end_date.__str__(), - "results": results["data"].to_dict("records"), - "total": results["total"], - "offset": offset + size, - } - else: - response = { - "startDate": start_date.__str__(), - "endDate": end_date.__str__(), - "results": [], - "total": 0, - "offset": 0, - } ->>>>>>> bd597e7 (Sorting) if pretty: json_str = json.dumps(response, indent=4)