Skip to content

Commit

Permalink
Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
MVarshini committed Dec 13, 2024
1 parent 5907b40 commit 751f376
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 79 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
6 changes: 5 additions & 1 deletion backend/app/api/v1/commons/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


async def getData(
start_datetime: date, end_datetime: date, size, offset, configpath: str
start_datetime: date, end_datetime: date, size, offset, sort: str, configpath: str
):
query = {
"size": size,
Expand All @@ -15,6 +15,10 @@ async def getData(
},
}

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

es = ElasticService(configpath=configpath)
response = await es.post(
query=query,
Expand Down
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
40 changes: 40 additions & 0 deletions backend/app/api/v1/endpoints/ocp/ocpJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ 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)
):
if start_date is None:
start_date = datetime.utcnow().date()
Expand All @@ -51,6 +58,7 @@ 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 @@ -71,6 +79,38 @@ 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
40 changes: 40 additions & 0 deletions backend/app/api/v1/endpoints/quay/quayJobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ 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)
):
if start_date is None:
start_date = datetime.utcnow().date()
Expand All @@ -51,6 +58,7 @@ 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 @@ -72,6 +80,38 @@ 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
2 changes: 2 additions & 0 deletions backend/app/services/splunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ async def query(
"""
query["count"] = size
query["offset"] = offset
query["sort_dir"] = "asc"
query["sort_key"] = "test_type"

# If additional search parameters are provided, include those in searchindex
searchindex = (
Expand Down
67 changes: 3 additions & 64 deletions frontend/src/actions/commonActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,6 @@ import { setOCPCatFilters } from "./ocpActions";
import { setQuayCatFilters } from "./quayActions";
import { setTelcoCatFilters } from "./telcoActions";

const getSortableRowValues = (result, tableColumns) => {
const tableKeys = tableColumns.map((item) => item.value);
return tableKeys.map((key) => result[key]);
};

export const sortTable = (currState) => (dispatch, getState) => {
const results = [...getState()[currState].filteredResults];
const { activeSortDir, activeSortIndex, tableColumns } =
getState()[currState];
try {
if (activeSortIndex !== null && typeof activeSortIndex !== "undefined") {
const sortedResults = results.sort((a, b) => {
const aValue = getSortableRowValues(a, tableColumns)[activeSortIndex];
const bValue = getSortableRowValues(b, tableColumns)[activeSortIndex];
if (typeof aValue === "number") {
if (activeSortDir === "asc") {
return aValue - bValue;
}
return bValue - aValue;
} else {
if (activeSortDir === "asc") {
return aValue.localeCompare(bValue);
}
return bValue.localeCompare(aValue);
}
});
dispatch(sortedTableRows(currState, sortedResults));
}
} catch (error) {
console.log(error);
}
};

const sortedTableRows = (currState, sortedResults) => (dispatch) => {
if (currState === "cpt") {
dispatch({
type: TYPES.SET_FILTERED_DATA,
payload: sortedResults,
});
return;
}
if (currState === "ocp") {
dispatch({
type: TYPES.SET_OCP_FILTERED_DATA,
payload: sortedResults,
});
return;
}
if (currState === "quay") {
dispatch({
type: TYPES.SET_QUAY_FILTERED_DATA,
payload: sortedResults,
});
return;
}
if (currState === "telco") {
dispatch({
type: TYPES.SET_TELCO_FILTERED_DATA,
payload: sortedResults,
});
}
};

const findItemCount = (data, key, value) => {
return data.reduce(function (n, item) {
return n + (item[key].toLowerCase() === value);
Expand Down Expand Up @@ -203,13 +140,15 @@ export const getSelectedFilter =
};

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

return params;
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/actions/homeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getFilteredData,
getRequestParams,
getSelectedFilter,
sortTable,
} from "./commonActions";

import API from "@/utils/axiosInstance";
Expand Down Expand Up @@ -56,7 +55,6 @@ export const fetchOCPJobsData =
});

dispatch(applyFilters());
dispatch(sortTable("cpt"));
dispatch(tableReCalcValues());
}
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/actions/ocpActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getFilteredData,
getRequestParams,
getSelectedFilter,
sortTable,
} from "./commonActions";

import API from "@/utils/axiosInstance";
Expand Down Expand Up @@ -52,7 +51,6 @@ export const fetchOCPJobs = () => async (dispatch) => {
});

dispatch(applyFilters());
dispatch(sortTable("ocp"));
dispatch(tableReCalcValues());
}
} catch (error) {
Expand Down
71 changes: 65 additions & 6 deletions frontend/src/actions/sortingActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as TYPES from "@/actions/types.js";

import { fetchOCPJobs, setOCPSortDir, setOCPSortIndex } from "./ocpActions";
import {
fetchQuayJobsData,
setQuaySortDir,
setQuaySortIndex,
} from "./quayActions";
import {
fetchTelcoJobsData,
setTelcoSortDir,
setTelcoSortIndex,
} from "./telcoActions";
import { setCPTSortDir, setCPTSortIndex } from "./homeActions";
import { setOCPSortDir, setOCPSortIndex } from "./ocpActions";
import { setQuaySortDir, setQuaySortIndex } from "./quayActions";
import { setTelcoSortDir, setTelcoSortIndex } from "./telcoActions";

import { sortTable } from "./commonActions";
import store from "@/store/store";

const { dispatch } = store;
Expand All @@ -29,6 +38,56 @@ export const setActiveSortIndex = (index, currType) => {
dispatch(setTelcoSortIndex(index));
}
};
export const handleOnSort = (currType) => {
dispatch(sortTable(currType));
export const handleOnSort = (colName, currType) => {
dispatch(sortTable(colName, currType));
};

const offsetActions = {
cpt: TYPES.SET_CPT_OFFSET,
ocp: TYPES.SET_OCP_OFFSET,
quay: TYPES.SET_QUAY_OFFSET,
telco: TYPES.SET_TELCO_OFFSET,
};
const fetchJobsMap = {
ocp: fetchOCPJobs,
quay: fetchQuayJobsData,
telco: fetchTelcoJobsData,
};
const sortObjActions = {
ocp: TYPES.SET_OCP_SORT_OBJ,
quay: TYPES.SET_QUAY_SORT_OBJ,
};
export const sortTable = (colName, currState) => (dispatch, getState) => {
const { activeSortDir, activeSortIndex } = getState()[currState];
const countObj = [
"masterNodesCount",
"workerNodesCount",
"infraNodesCount",
"totalNodesCount",
"startDate",
"endDate",
];
try {
if (
typeof activeSortDir !== "undefined" &&
typeof activeSortIndex !== "undefined"
) {
dispatch({ type: offsetActions[currState], payload: 0 });
let fieldName = countObj.includes(colName)
? colName
: `${colName}.keyword`;
if (colName === "build") {
fieldName = "ocpVersion.keyword";
}

const sortParam = `${fieldName}:${activeSortDir}`;
dispatch({ type: sortObjActions[currState], payload: sortParam });
console.log(sortParam);
const isFromSorting = true;

dispatch(fetchJobsMap[currState](isFromSorting));
}
} catch (error) {
console.log(error);
}
};
Loading

0 comments on commit 751f376

Please sign in to comment.