Skip to content

Commit

Permalink
sorting string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MVarshini committed Dec 13, 2024
1 parent 751f376 commit 49f6545
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 86 deletions.
3 changes: 1 addition & 2 deletions backend/app/api/v1/commons/ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions backend/app/api/v1/commons/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions backend/app/api/v1/commons/telco.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions backend/app/api/v1/commons/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from app.services.search import ElasticService
from fastapi import HTTPException, status
import re


async def getMetadata(uuid: str, configpath: str):
Expand Down Expand Up @@ -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}}]
41 changes: 1 addition & 40 deletions backend/app/api/v1/endpoints/ocp/ocpJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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)
Expand Down
41 changes: 1 addition & 40 deletions backend/app/api/v1/endpoints/quay/quayJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 49f6545

Please sign in to comment.