Skip to content

Commit

Permalink
Provide front-end sorting of trees for based on the column
Browse files Browse the repository at this point in the history
Right now the sorting is done using string sorting. If the data in the
columns are numbers, then number sorting should be used. Moreover,
some known string formatting can be reversed to extract the number and
do a number comparison. For example, some data providers provide columns
with duration, where the value is formatted to show 'ns' for nanosecond,
'us' micro-second and so on. Other example, are percentages shown.

The tree of the CPU usage view when using the Trace Compass trace server
is good example for different columns and formatting.

Note that this implementation is a temporary solution until the TSP and
server back-end is augmented to provide a different mechanism to
support sorting.

fixes eclipse-cdt-cloud#577

Signed-off-by: Bernd Hufmann <Bernd.Hufmann@ericsson.com>
  • Loading branch information
bhufmann committed Dec 8, 2021
1 parent 232f2f7 commit ec5c1ed
Showing 1 changed file with 42 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const sortNodes = (nodes: TreeNode[], sortConfig: SortConfig[]): TreeNode
sortedNodes.sort((node1: TreeNode, node2: TreeNode) => {
const index = orderToSort.columnIndex;
const order = (orderToSort.sortState === sortState.asc) ? 'asc' : 'desc';
const value1 = node1.labels[index];
const value2 = node2.labels[index];
const value1: string = node1.labels[index];
const value2: string = node2.labels[index];
let result = 0;
if (!value1 && value2) {
result = -1;
Expand All @@ -46,17 +46,20 @@ export const sortNodes = (nodes: TreeNode[], sortConfig: SortConfig[]): TreeNode
} else if (!value1 && !value2) {
result = 0;
} else {
if (typeof value1 === 'string' && typeof value2 === 'string') {
const comp = (value1 as string).localeCompare(value2);
result = (order === 'asc') ? -comp : comp;
} else {
if (value1 < value2) {
result = (order === 'asc') ? -1 : 1;
} else if (value1 > value2) {
const number1 = parseValue(value1);
const number2 = parseValue(value2);
let comp = 0;
if (number1 !== undefined && number2 !== undefined) {
if (number1 < number2) {
result = (order === 'asc') ? 1 : -1;
} else if (number1 > number2) {
result = (order === 'asc') ? -1 : 1;
} else {
result = 0;
}
} else {
comp = value1.localeCompare(value2);
result = (order === 'asc') ? -comp : comp;
}
}
return result;
Expand All @@ -69,3 +72,33 @@ export const sortNodes = (nodes: TreeNode[], sortConfig: SortConfig[]): TreeNode
}
return sortedNodes;
};

const parseValue = (valueString: string): number | undefined => {
let floatNumber = NaN;
let factor = -1;
const valueArray = valueString.split(' ');
if (valueArray.length === 1) {
floatNumber = Number.parseFloat(valueString);
factor = 1;
} else if (valueArray.length === 2) {
const value = valueArray[0];
const unit = valueArray[1];
if (unit === 'ns' || unit === '%') {
factor = 1;
} else if (unit === 'us' || unit === ('\u00B5' + 's')) {
factor = 1000;
} else if (unit === 'ms') {
factor = 1000 * 1000;
} else if (unit === 's') {
factor = 1000 * 1000 * 1000;
} else {
return undefined;
}
floatNumber = Number.parseFloat(value);
}
if (!Number.isNaN(floatNumber)) {
return floatNumber * factor;
}
return undefined;
};

0 comments on commit ec5c1ed

Please sign in to comment.