Skip to content

Commit 0dd7ec6

Browse files
committed
sorting string validation
1 parent 49f6545 commit 0dd7ec6

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Define the keywords for sorting.
2+
DIRECTIONS = ("asc", "desc")
3+
FIELDS = (
4+
"ciSystem.keyword",
5+
"benchmark.keyword",
6+
"ocpVersion.keyword",
7+
"releaseStream.keyword",
8+
"platform.keyword",
9+
"networkType.keyword",
10+
"ipsec.keyword",
11+
"fips.keyword",
12+
"encrypted.keyword",
13+
"publish.keyword",
14+
"computeArch.keyword",
15+
"controlPlaneArch.keyword",
16+
"jobStatus.keyword",
17+
"startDate",
18+
"endDate",
19+
"workerNodesCount",
20+
"masterNodesCount",
21+
"infraNodesCount",
22+
"totalNodesCount",
23+
)

backend/app/api/v1/commons/utils.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from app.services.search import ElasticService
22
from fastapi import HTTPException, status
33
import re
4+
import app.api.v1.commons.constants as constants
45

56

67
async def getMetadata(uuid: str, configpath: str):
@@ -80,15 +81,18 @@ def build_sort_terms(sort_string: str) -> list[dict[str, str]]:
8081
:return: list, transformed sort structure or raises a ValueError for invalid input
8182
8283
"""
83-
84-
pattern = r"^([\w]+):(asc|desc)$"
85-
match = re.match(pattern, sort_string)
86-
87-
if not match:
88-
raise HTTPException(
89-
status.HTTP_400_BAD_REQUEST,
90-
f"Invalid sort string format. Expected 'sort=key:direction' with direction as 'asc' or 'desc'.",
91-
)
92-
93-
key, direction = match.groups()
94-
return [{key: {"order": direction}}]
84+
sort_terms = []
85+
if sort_string:
86+
key, dir = sort_string.split(":", maxsplit=1)
87+
if dir not in constants.DIRECTIONS:
88+
raise HTTPException(
89+
status.HTTP_400_BAD_REQUEST,
90+
f"Sort direction {dir!r} must be one of {','.join(constants.DIRECTIONS)}",
91+
)
92+
if key not in constants.FIELDS:
93+
raise HTTPException(
94+
status.HTTP_400_BAD_REQUEST,
95+
f"Sort key {key!r} must be one of {','.join(constants.FIELDS)}",
96+
)
97+
sort_terms.append({f"{key}": {"order": dir}})
98+
return sort_terms

backend/app/api/v1/endpoints/ocp/ocpJobs.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@router.get(
1313
"/api/v1/ocp/jobs",
1414
summary="Returns a job list",
15-
description="Returns a list of jobs in the specified dates of requested size. \
15+
description="Returns a list of jobs in the specified dates. \
1616
If not dates are provided the API will default the values. \
1717
`startDate`: will be set to the day of the request minus 5 days.\
1818
`endDate`: will be set to the day of the request.",
@@ -51,7 +51,8 @@ async def jobs(
5151
),
5252
status_code=422,
5353
)
54-
54+
if not sort:
55+
sort = None
5556
if offset and not size:
5657
raise HTTPException(400, f"offset {offset} specified without size")
5758
elif not offset and not size:
@@ -60,7 +61,9 @@ async def jobs(
6061
elif not offset:
6162
offset = 0
6263

63-
results = await getData(start_date, end_date, size, offset, "ocp.elasticsearch")
64+
results = await getData(
65+
start_date, end_date, size, offset, sort, "ocp.elasticsearch"
66+
)
6467
jobs = []
6568
if "data" in results and len(results["data"]) >= 1:
6669
jobs = results["data"].to_dict("records")

backend/app/api/v1/endpoints/quay/quayJobs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def jobs(
3232
description="End date for searching jobs, format: 'YYYY-MM-DD'",
3333
examples=["2020-11-15"],
3434
),
35-
pretty: bool = Query(False, description="Output contet in pretty format."),
35+
pretty: bool = Query(False, description="Output content in pretty format."),
3636
size: int = Query(None, description="Number of jobs to fetch"),
3737
offset: int = Query(None, description="Offset Number to fetch jobs from"),
3838
sort: str = Query(None, description="To sort fields on specified direction"),
@@ -60,7 +60,12 @@ async def jobs(
6060
elif not offset:
6161
offset = 0
6262

63-
results = await getData(start_date, end_date, size, offset, "quay.elasticsearch")
63+
if not sort:
64+
sort = None
65+
66+
results = await getData(
67+
start_date, end_date, size, offset, sort, "quay.elasticsearch"
68+
)
6469

6570
jobs = []
6671
if "data" in results and len(results["data"]) >= 1:

backend/app/services/splunk.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ async def query(
4545
"""
4646
query["count"] = size
4747
query["offset"] = offset
48-
query["sort_dir"] = "asc"
49-
query["sort_key"] = "test_type"
5048

5149
# If additional search parameters are provided, include those in searchindex
5250
searchindex = (

frontend/src/actions/commonActions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ export const getSelectedFilter =
141141

142142
export const getRequestParams = (type) => (dispatch, getState) => {
143143
const { start_date, end_date, size, offset, sort } = getState()[type];
144-
// const sortParam = `${activeSortIndex}:${activeSortDir}`;
145144
const params = {
146145
pretty: true,
147146
...(start_date && { start_date }),

frontend/src/reducers/ocpReducer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ const initialState = {
4848
size: DEFAULT_PER_PAGE,
4949
offset: INITAL_OFFSET,
5050
totalJobs: 0,
51-
//tableData: [],
5251
filterData: [],
5352
categoryFilterValue: "",
5453
filterOptions: [],

0 commit comments

Comments
 (0)