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 bd597e7 commit d127073
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 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}}]
4 changes: 1 addition & 3 deletions backend/app/api/v1/endpoints/cpt/maps/hce.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ....commons.hce import getData
from datetime import date
import pandas as pd


################################################################
Expand All @@ -16,9 +17,6 @@
# "version"
# "testName"
################################################################
import pandas as pd


async def hceMapper(start_datetime: date, end_datetime: date, size: int, offset: int):
response = await getData(
start_datetime, end_datetime, size, offset, f"hce.elasticsearch"
Expand Down
15 changes: 1 addition & 14 deletions backend/app/services/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@
class ElasticService:
# todo add bulkhead pattern
# todo add error message for unauthorized user
# Define the keywords for sorting.
DIRECTIONS = ("asc", "desc")
FIELDS = (
"ciSystem",
"releaseStream",
"platform",
"workerNodesCount",
"masterNodesCount" "infraNodesCount",
"totalNodesCount",
)

def __init__(self, configpath="", index=""):
"""Init method."""
Expand Down Expand Up @@ -125,7 +115,6 @@ async def post(
"data": response["hits"]["hits"],
"total": response["hits"]["total"]["value"],
}
# previous_results = await self.scan_indices(self.prev_es, self.prev_index, query, timestamp_field, start_date, new_end_date, size)
if self.prev_es and self.new_es:
self.new_index = self.new_index_prefix + (
self.new_index if indice is None else indice
Expand Down Expand Up @@ -167,7 +156,6 @@ async def post(
"data": response["hits"]["hits"],
"total": response["hits"]["total"]["value"],
}
# new_results = await self.scan_indices(self.new_es, self.new_index, query, timestamp_field, new_start_date, end_date, size)
unique_data = await self.remove_duplicates(
previous_results["data"]
if ("data" in previous_results)
Expand All @@ -187,8 +175,7 @@ async def post(
query["query"]["bool"]["filter"]["range"][timestamp_field][
"lte"
] = str(end_date)
# return await self.scan_indices(self.new_es, self.new_index, query, timestamp_field, start_date, end_date, size)
# else:

response = await self.new_es.search(
index=self.new_index + "*",
body=jsonable_encoder(query),
Expand Down

0 comments on commit d127073

Please sign in to comment.