Skip to content

Commit

Permalink
Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
MVarshini committed Dec 12, 2024
1 parent 93b5d0d commit bd597e7
Show file tree
Hide file tree
Showing 21 changed files with 317 additions and 179 deletions.
10 changes: 9 additions & 1 deletion backend/app/api/v1/commons/ocp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@


async def getData(
start_datetime: date, end_datetime: date, size: int, offset: int, configpath: str
start_datetime: date,
end_datetime: date,
size: int,
offset: int,
sort: str,
configpath: str,
):
query = {
"size": size,
Expand All @@ -14,6 +19,9 @@ async def getData(
"bool": {"filter": {"range": {"timestamp": {"format": "yyyy-MM-dd"}}}}
},
}
if sort:
key, direction = sort.split(":")
query["sort"] = [{key: {"order": direction}}]

es = ElasticService(configpath=configpath)
response = await es.post(
Expand Down
16 changes: 11 additions & 5 deletions backend/app/api/v1/commons/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
from app.services.search import ElasticService


async def getData(start_datetime: date, end_datetime: date, size, offset, configpath: str):
async def getData(
start_datetime: date, end_datetime: date, size, offset, sort: str, configpath: str
):
query = {
"size": size,
"from": offset,
"query": {
"bool": {"filter": {"range": {"timestamp": {"format": "yyyy-MM-dd"}}}}
}
},
}

if sort:
key, direction = sort.split(":")
query["sort"] = [{key: {"order": direction}}]

es = ElasticService(configpath=configpath)
response = await es.post(
query=query,
Expand All @@ -21,7 +27,7 @@ async def getData(start_datetime: date, end_datetime: date, size, offset, config
timestamp_field="timestamp",
)
await es.close()
tasks = [item['_source'] for item in response['data']]
tasks = [item["_source"] for item in response["data"]]
jobs = pd.json_normalize(tasks)
if len(jobs) == 0:
return jobs
Expand All @@ -40,7 +46,7 @@ async def getData(start_datetime: date, end_datetime: date, size, offset, config
jobs["build"] = jobs.apply(utils.getBuild, axis=1)
jobs["shortVersion"] = jobs["ocpVersion"].str.slice(0, 4)

cleanJobs = jobs[jobs['platform'] != ""]
cleanJobs = jobs[jobs["platform"] != ""]

jbs = cleanJobs
return ({'data':jbs, 'total': response['total']})
return {"data": jbs, "total": response["total"]}
2 changes: 2 additions & 0 deletions backend/app/api/v1/commons/telco.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ 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
71 changes: 44 additions & 27 deletions backend/app/api/v1/endpoints/ocp/ocpJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,34 @@
router = APIRouter()



@router.get('/api/v1/ocp/jobs',
summary="Returns a job list",
description="Returns a list of jobs in the specified dates of requested size. \
@router.get(
"/api/v1/ocp/jobs",
summary="Returns a job list",
description="Returns a list of jobs in the specified dates of requested size. \
If not dates are provided the API will default the values. \
`startDate`: will be set to the day of the request minus 5 days.\
`endDate`: will be set to the day of the request.",
responses={
200: ocp_200_response(),
422: response_422(),
},)
async def jobs(start_date: date = Query(None, description="Start date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-10"]),
end_date: date = Query(None, description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"]),
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")):
responses={
200: ocp_200_response(),
422: response_422(),
},
)
async def jobs(
start_date: date = Query(
None,
description="Start date for searching jobs, format: 'YYYY-MM-DD'",
examples=["2020-11-10"],
),
end_date: date = Query(
None,
description="End date for searching jobs, format: 'YYYY-MM-DD'",
examples=["2020-11-15"],
),
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"),
):
if start_date is None:
start_date = datetime.utcnow().date()
start_date = start_date - timedelta(days=5)
Expand All @@ -42,28 +54,33 @@ async def jobs(start_date: date = Query(None, description="Start date for search

if not offset:
offset = 0

if not size:
size = 10000
offset = 0

results = await getData(start_date, end_date, size, offset, 'ocp.elasticsearch')

if 'data' in results and len(results['data']) >= 1:
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
"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
"startDate": start_date.__str__(),
"endDate": end_date.__str__(),
"results": [],
"total": 0,
"offset": 0,
}

if pretty:
Expand Down
71 changes: 44 additions & 27 deletions backend/app/api/v1/endpoints/quay/quayJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,34 @@
router = APIRouter()



@router.get('/api/v1/quay/jobs',
summary="Returns a job list",
description="Returns a list of jobs in the specified dates of requested size. \
@router.get(
"/api/v1/quay/jobs",
summary="Returns a job list",
description="Returns a list of jobs in the specified dates of requested size. \
If not dates are provided the API will default the values. \
`startDate`: will be set to the day of the request minus 5 days.\
`endDate`: will be set to the day of the request.",
responses={
200: quay_200_response(),
422: response_422(),
},)
async def jobs(start_date: date = Query(None, description="Start date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-10"]),
end_date: date = Query(None, description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"]),
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")):
responses={
200: quay_200_response(),
422: response_422(),
},
)
async def jobs(
start_date: date = Query(
None,
description="Start date for searching jobs, format: 'YYYY-MM-DD'",
examples=["2020-11-10"],
),
end_date: date = Query(
None,
description="End date for searching jobs, format: 'YYYY-MM-DD'",
examples=["2020-11-15"],
),
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"),
):
if start_date is None:
start_date = datetime.utcnow().date()
start_date = start_date - timedelta(days=5)
Expand All @@ -42,28 +54,33 @@ async def jobs(start_date: date = Query(None, description="Start date for search

if not offset:
offset = 0

if not size:
size = 10000
offset = 0

results = await getData(start_date, end_date, size, offset, 'quay.elasticsearch')

if 'data' in results and len(results['data']) >= 1:
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
"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
"startDate": start_date.__str__(),
"endDate": end_date.__str__(),
"results": [],
"total": 0,
"offset": 0,
}

if pretty:
Expand Down
Loading

0 comments on commit bd597e7

Please sign in to comment.