Skip to content

Commit b4fd1eb

Browse files
[NP] Remove IndexedArray usage in Schemas (#61410)
* Remove IndexedArray usage from schemas.ts * Update unit tests * Revert default value * Update sidebar.tsx * Updated TS * Fix code review comments * Remove ISchemas * Revert ISchemas Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent d0e7f98 commit b4fd1eb

File tree

4 files changed

+30
-42
lines changed

4 files changed

+30
-42
lines changed

src/legacy/core_plugins/vis_default_editor/public/components/agg_group.test.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { IAggConfigs, IAggConfig } from 'src/plugins/data/public';
2424
import { DefaultEditorAggGroup, DefaultEditorAggGroupProps } from './agg_group';
2525
import { DefaultEditorAgg } from './agg';
2626
import { DefaultEditorAggAdd } from './agg_add';
27-
import { Schema } from '../schemas';
27+
import { ISchemas, Schemas } from '../schemas';
2828
import { EditorVisState } from './sidebar/state/reducers';
2929

3030
jest.mock('@elastic/eui', () => ({
@@ -47,6 +47,7 @@ jest.mock('./agg_add', () => ({
4747
describe('DefaultEditorAgg component', () => {
4848
let defaultProps: DefaultEditorAggGroupProps;
4949
let aggs: IAggConfigs;
50+
let schemas: ISchemas;
5051
let setTouched: jest.Mock;
5152
let setValidity: jest.Mock;
5253
let reorderAggs: jest.Mock;
@@ -55,6 +56,18 @@ describe('DefaultEditorAgg component', () => {
5556
setTouched = jest.fn();
5657
setValidity = jest.fn();
5758
reorderAggs = jest.fn();
59+
schemas = new Schemas([
60+
{
61+
name: 'metrics',
62+
group: 'metrics',
63+
max: 1,
64+
},
65+
{
66+
name: 'buckets',
67+
group: 'buckets',
68+
max: 1,
69+
},
70+
]);
5871

5972
aggs = {
6073
aggs: [
@@ -95,18 +108,7 @@ describe('DefaultEditorAgg component', () => {
95108
state: {
96109
data: { aggs },
97110
} as EditorVisState,
98-
schemas: [
99-
{
100-
name: 'metrics',
101-
group: 'metrics',
102-
max: 1,
103-
} as Schema,
104-
{
105-
name: 'buckets',
106-
group: 'buckets',
107-
max: 1,
108-
} as Schema,
109-
],
111+
schemas: schemas.metrics,
110112
setTouched,
111113
setValidity,
112114
reorderAggs,
@@ -133,6 +135,7 @@ describe('DefaultEditorAgg component', () => {
133135

134136
it('should last bucket has truthy isLastBucket prop', () => {
135137
defaultProps.groupName = 'buckets';
138+
defaultProps.schemas = schemas.buckets;
136139
const comp = mount(<DefaultEditorAggGroup {...defaultProps} />);
137140
const lastAgg = comp.find(DefaultEditorAgg).last();
138141

@@ -154,6 +157,8 @@ describe('DefaultEditorAgg component', () => {
154157

155158
it('should show add button when schemas count is less than max', () => {
156159
defaultProps.groupName = 'buckets';
160+
defaultProps.schemas = schemas.buckets;
161+
defaultProps.schemas[0].max = 2;
157162
const comp = shallow(<DefaultEditorAggGroup {...defaultProps} />);
158163

159164
expect(comp.find(DefaultEditorAggAdd).exists()).toBeTruthy();

src/legacy/core_plugins/vis_default_editor/public/components/agg_group.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {
4141
getEnabledMetricAggsCount,
4242
} from './agg_group_helper';
4343
import { aggGroupReducer, initAggsState, AGGS_ACTION_KEYS } from './agg_group_state';
44-
import { Schema, getSchemasByGroup } from '../schemas';
44+
import { Schema } from '../schemas';
4545
import { TimeRange } from '../../../../../plugins/data/public';
4646

4747
export interface DefaultEditorAggGroupProps extends DefaultEditorAggCommonProps {
@@ -73,7 +73,7 @@ function DefaultEditorAggGroup({
7373
}: DefaultEditorAggGroupProps) {
7474
const groupNameLabel = (search.aggs.aggGroupNamesMap() as any)[groupName];
7575
// e.g. buckets can have no aggs
76-
const schemaNames = getSchemasByGroup(schemas, groupName).map(s => s.name);
76+
const schemaNames = schemas.map(s => s.name);
7777
const group: IAggConfig[] = useMemo(
7878
() =>
7979
state.data.aggs!.aggs.filter(

src/legacy/core_plugins/vis_default_editor/public/components/sidebar/sidebar.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import { DefaultEditorAggCommonProps } from '../agg_common_props';
3131
import { SidebarTitle } from './sidebar_title';
3232
import { PersistedState } from '../../../../../../plugins/visualizations/public';
3333
import { SavedSearch } from '../../../../../../plugins/discover/public';
34-
import { AggGroupNames } from '../../../../../../plugins/data/public';
35-
import { getSchemasByGroup } from '../../schemas';
34+
import { Schema } from '../../schemas';
3635
import { TimeRange } from '../../../../../../plugins/data/public';
3736

3837
interface DefaultEditorSideBarProps {
@@ -66,9 +65,7 @@ function DefaultEditorSideBar({
6665
const responseAggs = useMemo(() => (state.data.aggs ? state.data.aggs.getResponseAggs() : []), [
6766
state.data.aggs,
6867
]);
69-
const metricSchemas = getSchemasByGroup(vis.type.schemas.all || [], AggGroupNames.Metrics).map(
70-
s => s.name
71-
);
68+
const metricSchemas = (vis.type.schemas.metrics || []).map((s: Schema) => s.name);
7269
const metricAggs = useMemo(
7370
() => responseAggs.filter(agg => metricSchemas.includes(get(agg, 'schema'))),
7471
[responseAggs, metricSchemas]

src/legacy/core_plugins/vis_default_editor/public/schemas.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
* under the License.
1818
*/
1919

20-
import _ from 'lodash';
20+
import _, { defaults } from 'lodash';
2121

2222
import { Optional } from '@kbn/utility-types';
2323

24-
import { IndexedArray } from 'ui/indexed_array';
2524
import { AggGroupNames, AggParam, IAggGroupNames } from '../../../../plugins/data/public';
2625

2726
export interface ISchemas {
@@ -45,9 +44,10 @@ export interface Schema {
4544
aggSettings?: any;
4645
}
4746

48-
export class Schemas {
49-
// @ts-ignore
50-
all: IndexedArray<Schema>;
47+
export class Schemas implements ISchemas {
48+
all: Schema[] = [];
49+
[AggGroupNames.Buckets]: Schema[] = [];
50+
[AggGroupNames.Metrics]: Schema[] = [];
5151

5252
constructor(
5353
schemas: Array<
@@ -70,7 +70,7 @@ export class Schemas {
7070
] as AggParam[];
7171
}
7272

73-
_.defaults(schema, {
73+
defaults(schema, {
7474
min: 0,
7575
max: Infinity,
7676
group: AggGroupNames.Buckets,
@@ -83,22 +83,12 @@ export class Schemas {
8383
return schema as Schema;
8484
})
8585
.tap((fullSchemas: Schema[]) => {
86-
this.all = new IndexedArray({
87-
index: ['name'],
88-
group: ['group'],
89-
immutable: true,
90-
initialSet: fullSchemas,
91-
});
86+
this.all = fullSchemas;
9287
})
9388
.groupBy('group')
9489
.forOwn((group, groupName) => {
9590
// @ts-ignore
96-
this[groupName] = new IndexedArray({
97-
index: ['name'],
98-
immutable: true,
99-
// @ts-ignore
100-
initialSet: group,
101-
});
91+
this[groupName] = group;
10292
})
10393
.commit();
10494
}
@@ -107,7 +97,3 @@ export class Schemas {
10797
export const getSchemaByName = (schemas: Schema[], schemaName?: string) => {
10898
return schemas.find(s => s.name === schemaName) || ({} as Schema);
10999
};
110-
111-
export const getSchemasByGroup = (schemas: Schema[], schemaGroup?: string) => {
112-
return schemas.filter(s => s.group === schemaGroup);
113-
};

0 commit comments

Comments
 (0)