Skip to content

Commit

Permalink
chore(SimplifiedPlan): remove redundant state (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug authored Aug 17, 2024
1 parent 0562b0c commit c8a4d3a
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {toExponential} from '../../../../../../utils/utils';
import {MetricsCell} from './MetricsCell';
import {OperationCell} from './OperationCell';
import type {ExtendedSimplifiesPlanItem} from './types';
import {block, getExtendedTreeNodes, getTreeNodesCoordinates} from './utils';
import {block, getExtendedTreeNodes} from './utils';

import './SimplifiedPlan.scss';

Expand Down Expand Up @@ -100,10 +100,7 @@ interface SimplifiedPlanProps {

export function SimplifiedPlan({plan}: SimplifiedPlanProps) {
const planWithLinesInfo = React.useMemo(() => getExtendedTreeNodes(plan), [plan]);
const [expanded, setExpanded] = React.useState<ExpandedState>(() => {
const coordinates = getTreeNodesCoordinates(plan);
return Object.fromEntries(coordinates.map((id) => [id, true]));
});
const [expanded, setExpanded] = React.useState<ExpandedState>(true);

const table = useTable({
columns,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,6 @@
import type {SimplifiedPlanItem} from '../../../../../../../store/reducers/explainQuery/types';
import type {ExtendedSimplifiesPlanItem} from '../types';
import {getExtendedTreeNodes, getTreeNodesCoordinates} from '../utils';

describe('getTreeNodesCoordinates', () => {
test('should handle empty input', () => {
const result = getTreeNodesCoordinates();
expect(result).toEqual([]);
});

test('should handle single node without children', () => {
const items: SimplifiedPlanItem[] = [
{
name: 'TestNode',
operationParams: {},
children: [],
},
];

const expected = ['0'];

const result = getTreeNodesCoordinates(items);
expect(result).toEqual(expected);
});

test('should handle nested nodes', () => {
const items: SimplifiedPlanItem[] = [
{
name: 'RootNode',
operationParams: {},
children: [
{
name: 'ChildNode1',
operationParams: {},
children: [],
},
{
name: 'ChildNode2',
operationParams: {},
children: [
{
name: 'GrandChildNode',
operationParams: {},
children: [],
},
],
},
],
},
];

const expected = ['0', '0.0', '0.1', '0.1.0'];

const result = getTreeNodesCoordinates(items);
expect(result).toEqual(expected);
});

test('should handle multiple root nodes', () => {
const items: SimplifiedPlanItem[] = [
{
name: 'RootNode1',
operationParams: {},
children: [],
},
{
name: 'RootNode2',
operationParams: {},
children: [
{
name: 'ChildNode',
operationParams: {},
children: [],
},
],
},
];

const expected = ['0', '1', '1.0'];

const result = getTreeNodesCoordinates(items);
expect(result).toEqual(expected);
});
});
import {getExtendedTreeNodes} from '../utils';

describe('getExtendedTreeNodes', () => {
test('should handle empty input', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,6 @@ import type {ExtendedSimplifiesPlanItem} from './types';

export const block = cn('ydb-query-explain-simplified-plan');

export function getTreeNodesCoordinates(items?: SimplifiedPlanItem[], prefix = '') {
const result: string[] = [];
const stack: {items?: SimplifiedPlanItem[]; prefix: string}[] = [];

if (items) {
stack.push({items, prefix});
}

while (stack.length > 0) {
const {items, prefix} = stack.pop()!;

items?.forEach((item, index) => {
let newPrefix = `${prefix}.${index}`;
if (!prefix) {
newPrefix = String(index);
}
result.push(newPrefix);

if (item.children) {
stack.push({items: item.children, prefix: newPrefix});
}
});
}

return result;
}

export function getExtendedTreeNodes(
items?: SimplifiedPlanItem[],
prefix = '',
Expand Down

0 comments on commit c8a4d3a

Please sign in to comment.