Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sorting to explore view table #1592

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions webapp/javascript/pages/TagExplorerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FlamegraphRenderer } from '@pyroscope/flamegraph/src';
import Dropdown, { MenuItem } from '@webapp/ui/Dropdown';
import LoadingSpinner from '@webapp/ui/LoadingSpinner';
import TagsSelector from '@webapp/pages/tagExplorer/components/TagsSelector';
import TableUI, { BodyRow } from '@webapp/ui/Table';
import TableUI, { useTableSort, BodyRow } from '@webapp/ui/Table';
import useColorMode from '@webapp/hooks/colorMode.hook';
import useTimeZone from '@webapp/hooks/timeZone.hook';
import { appendLabelToQuery } from '@webapp/util/query';
Expand Down Expand Up @@ -102,6 +102,34 @@ const TIMELINE_SERIES_COLORS = [
Color.rgb(222, 218, 247),
];

// structured data to display/style table cells
interface TableValuesData {
color?: Color;
mean: number;
stdDeviation: number;
total: number;
tagName: string;
}

const calculateTableData = (
groupsData: TimelineGroupData[]
): TableValuesData[] =>
groupsData.reduce((acc, { tagName, data, color }) => {
const mean = calculateMean(data.samples);
const total = calculateTotal(data.samples);
const stdDeviation = calculateStdDeviation(data.samples, mean);

acc.push({
tagName,
color,
mean,
total,
stdDeviation,
});

return acc;
}, [] as TableValuesData[]);

const TIMELINE_WRAPPER_ID = 'explore_timeline_wrapper';

const getTimelineColor = (index: number, palette: Color[]): Color =>
Expand Down Expand Up @@ -366,11 +394,11 @@ function Table({
{
name: 'name',
label: `${groupByTag === '' ? 'App' : 'Tag'} name`,
sortable: 0,
sortable: 1,
},
{ name: 'avgSamples', label: 'avg samples', sortable: 0 },
{ name: 'stdDeviation', label: 'std deviation samples', sortable: 0 },
{ name: 'totalSamples', label: 'total samples', sortable: 0 },
{ name: 'avgSamples', label: 'avg samples', sortable: 1 },
{ name: 'stdDeviation', label: 'std deviation samples', sortable: 1 },
{ name: 'totalSamples', label: 'total samples', sortable: 1 },
];

const groupsTotal = useMemo(
Expand All @@ -381,10 +409,40 @@ function Table({
[groupsData]
);

const bodyRows = groupsData.reduce(
(acc, { tagName, color, data }): BodyRow[] => {
const mean = calculateMean(data.samples);
const total = calculateTotal(data.samples);
const tableValuesData = calculateTableData(groupsData);

const { sortByDirection, sortBy, updateSortParams } = useTableSort(headRow);

const sortedTableValuesData = (() => {
const m = sortByDirection === 'asc' ? 1 : -1;
let sorted: TableValuesData[] = [];

switch (sortBy) {
case 'name':
sorted = tableValuesData.sort(
(a, b) => m * a.tagName.localeCompare(b.tagName)
);
break;
case 'totalSamples':
sorted = tableValuesData.sort((a, b) => m * (a.total - b.total));
break;
case 'avgSamples':
sorted = tableValuesData.sort((a, b) => m * (a.mean - b.mean));
break;
case 'stdDeviation':
sorted = tableValuesData.sort(
(a, b) => m * (a.stdDeviation - b.stdDeviation)
);
break;
default:
sorted = tableValuesData;
}

return sorted;
})();

const bodyRows = sortedTableValuesData.reduce(
(acc, { tagName, color, total, mean, stdDeviation }): BodyRow[] => {
const percentage = (total / groupsTotal) * 100;
const row = {
isRowSelected: isTagSelected(tagName),
Expand All @@ -409,7 +467,7 @@ function Table({
),
},
{ value: mean.toFixed(2) },
{ value: calculateStdDeviation(data.samples, mean).toFixed(2) },
{ value: stdDeviation.toFixed(2) },
{ value: total },
],
};
Expand Down Expand Up @@ -453,7 +511,13 @@ function Table({
/>
</div>
</div>
<TableUI table={table} className={styles.tagExplorerTable} />
<TableUI
updateSortParams={updateSortParams}
sortBy={sortBy}
sortByDirection={sortByDirection}
table={table}
className={styles.tagExplorerTable}
/>
</div>
);
}
Expand Down