You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by ja...@apache.org on 2022/12/02 18:50:38 UTC
[pinot] branch master updated: add table sort function for table size (#9844)
This is an automated email from the ASF dual-hosted git repository.
jackie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new 9b8809fea5 add table sort function for table size (#9844)
9b8809fea5 is described below
commit 9b8809fea50e3d516a4f4bde8d0c6341f30ce158
Author: Johan Adami <47...@users.noreply.github.com>
AuthorDate: Fri Dec 2 13:50:31 2022 -0500
add table sort function for table size (#9844)
---
.../src/main/resources/app/components/Table.tsx | 21 ++++++----
.../src/main/resources/app/interfaces/types.d.ts | 2 +
.../src/main/resources/app/utils/SortFunctions.tsx | 49 ++++++++++++++++++++++
3 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/pinot-controller/src/main/resources/app/components/Table.tsx b/pinot-controller/src/main/resources/app/components/Table.tsx
index d7cfb2e669..b5871d788e 100644
--- a/pinot-controller/src/main/resources/app/components/Table.tsx
+++ b/pinot-controller/src/main/resources/app/components/Table.tsx
@@ -35,7 +35,7 @@ import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import { TablePagination, Tooltip } from '@material-ui/core';
-import { TableData } from 'Models';
+import {TableData, TableSortFunction} from 'Models';
import IconButton from '@material-ui/core/IconButton';
import FirstPageIcon from '@material-ui/icons/FirstPage';
import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft';
@@ -47,6 +47,7 @@ import { Link } from 'react-router-dom';
import Chip from '@material-ui/core/Chip';
import { get, has, orderBy } from 'lodash';
import app_state from '../app_state';
+import { sortBytes, sortNumberOfSegments } from '../utils/SortFunctions'
import Utils from '../utils/Utils';
import TableToolbar from './TableToolbar';
import SimpleAccordion from './SimpleAccordion';
@@ -73,6 +74,15 @@ type Props = {
tooltipData?: string[]
};
+// These sort functions are applied to any columns with these names. Otherwise, we just
+// sort on the raw data. Ideally users of this class would pass in custom sort functions
+// for their columns, but this pattern already existed, so we're at least making the
+// improvement to pull this out to a common variable.
+let staticSortFunctions: Map<string, TableSortFunction> = new Map()
+staticSortFunctions.set("Number of Segments", sortNumberOfSegments);
+staticSortFunctions.set("Estimated Size", sortBytes);
+staticSortFunctions.set("Reported Size", sortBytes);
+
const StyledTableRow = withStyles((theme) =>
createStyles({
root: {
@@ -464,13 +474,8 @@ export default function CustomizedTables({
className={classes.head}
key={index}
onClick={() => {
- if(column === 'Number of Segments'){
- const data = finalData.sort((a,b)=>{
- const aSegmentInt = parseInt(a[column+app_state.columnNameSeparator+index]);
- const bSegmentInt = parseInt(b[column+app_state.columnNameSeparator+index]);
- const result = order ? (aSegmentInt > bSegmentInt) : (aSegmentInt < bSegmentInt);
- return result ? 1 : -1;
- });
+ if (staticSortFunctions.has(column)) {
+ finalData.sort((a, b) => staticSortFunctions.get(column)(a, b, column, index, order));
setFinalData(finalData);
} else {
setFinalData(orderBy(finalData, column+app_state.columnNameSeparator+index, order ? 'asc' : 'desc'));
diff --git a/pinot-controller/src/main/resources/app/interfaces/types.d.ts b/pinot-controller/src/main/resources/app/interfaces/types.d.ts
index 29d9927e13..79b28d7e16 100644
--- a/pinot-controller/src/main/resources/app/interfaces/types.d.ts
+++ b/pinot-controller/src/main/resources/app/interfaces/types.d.ts
@@ -223,6 +223,8 @@ declare module 'Models' {
}
}
+ export type TableSortFunction = (a: any, b: any, column: string, index: number, order: boolean) => number;
+
export const enum SEGMENT_STATUS {
ONLINE = "ONLINE",
OFFLINE = "OFFLINE",
diff --git a/pinot-controller/src/main/resources/app/utils/SortFunctions.tsx b/pinot-controller/src/main/resources/app/utils/SortFunctions.tsx
new file mode 100644
index 0000000000..a9943533c1
--- /dev/null
+++ b/pinot-controller/src/main/resources/app/utils/SortFunctions.tsx
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {TableSortFunction} from "Models";
+import app_state from "../app_state";
+
+
+// table sorting requires a 1/-1 result. This helper function helps calculate this
+// from any two results.
+const valuesToResultNumber = (aRes: any, bRes: any, order: boolean): number => {
+ const result = order ? aRes > bRes : aRes < bRes;
+ return result ? 1 : -1;
+}
+
+export const sortNumberOfSegments: TableSortFunction = (a: any, b: any, column: string, index: number, order: boolean) => {
+ const aSegmentInt = parseInt(a[column+app_state.columnNameSeparator+index]);
+ const bSegmentInt = parseInt(b[column+app_state.columnNameSeparator+index]);
+ return valuesToResultNumber(aSegmentInt, bSegmentInt, order);
+}
+
+export const sortBytes: TableSortFunction = (a: any, b: any, column: string, index: number, order: boolean) => {
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+ const [aValue, aUnit] = a[column+app_state.columnNameSeparator+index].split(" ");
+ const [bValue, bUnit] = b[column+app_state.columnNameSeparator+index].split(" ");
+ const aUnitIndex = sizes.indexOf(aUnit);
+ const bUnitIndex = sizes.indexOf(bUnit);
+
+ if (sizes.indexOf(aUnit) === sizes.indexOf(bUnit)) {
+ return valuesToResultNumber(parseFloat(aValue), parseFloat(bValue), order);
+ } else {
+ return valuesToResultNumber(aUnitIndex, bUnitIndex, order);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org