mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
parent
b975901ccb
commit
395f0f1fc7
5 changed files with 67 additions and 45 deletions
|
@ -19,7 +19,7 @@ import { IndexPatternRef } from './types';
|
||||||
import { ChangeIndexPattern } from './change_indexpattern';
|
import { ChangeIndexPattern } from './change_indexpattern';
|
||||||
import { getSwitchIndexPatternAppState } from '../../helpers/get_switch_index_pattern_app_state';
|
import { getSwitchIndexPatternAppState } from '../../helpers/get_switch_index_pattern_app_state';
|
||||||
import { SortPairArr } from '../../angular/doc_table/lib/get_sort';
|
import { SortPairArr } from '../../angular/doc_table/lib/get_sort';
|
||||||
import { MODIFY_COLUMNS_ON_SWITCH } from '../../../../common';
|
import { MODIFY_COLUMNS_ON_SWITCH, SORT_DEFAULT_ORDER_SETTING } from '../../../../common';
|
||||||
import { AppState } from '../../angular/discover_state';
|
import { AppState } from '../../angular/discover_state';
|
||||||
export interface DiscoverIndexPatternProps {
|
export interface DiscoverIndexPatternProps {
|
||||||
/**
|
/**
|
||||||
|
@ -46,10 +46,6 @@ export interface DiscoverIndexPatternProps {
|
||||||
* Discover App state
|
* Discover App state
|
||||||
*/
|
*/
|
||||||
state: AppState;
|
state: AppState;
|
||||||
/**
|
|
||||||
* Read from the Fields API
|
|
||||||
*/
|
|
||||||
useNewFieldsApi?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,7 +58,6 @@ export function DiscoverIndexPattern({
|
||||||
indexPatterns,
|
indexPatterns,
|
||||||
state,
|
state,
|
||||||
setAppState,
|
setAppState,
|
||||||
useNewFieldsApi,
|
|
||||||
}: DiscoverIndexPatternProps) {
|
}: DiscoverIndexPatternProps) {
|
||||||
const options: IndexPatternRef[] = (indexPatternList || []).map((entity) => ({
|
const options: IndexPatternRef[] = (indexPatternList || []).map((entity) => ({
|
||||||
id: entity.id,
|
id: entity.id,
|
||||||
|
@ -80,12 +75,12 @@ export function DiscoverIndexPattern({
|
||||||
state.columns || [],
|
state.columns || [],
|
||||||
(state.sort || []) as SortPairArr[],
|
(state.sort || []) as SortPairArr[],
|
||||||
config.get(MODIFY_COLUMNS_ON_SWITCH),
|
config.get(MODIFY_COLUMNS_ON_SWITCH),
|
||||||
useNewFieldsApi
|
config.get(SORT_DEFAULT_ORDER_SETTING)
|
||||||
);
|
);
|
||||||
setAppState(nextAppState);
|
setAppState(nextAppState);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[selectedIndexPattern, state, config, indexPatterns, setAppState, useNewFieldsApi]
|
[selectedIndexPattern, state, config, indexPatterns, setAppState]
|
||||||
);
|
);
|
||||||
|
|
||||||
const [selected, setSelected] = useState({
|
const [selected, setSelected] = useState({
|
||||||
|
|
|
@ -165,7 +165,6 @@ export function DiscoverSidebar({
|
||||||
indexPatterns={indexPatterns}
|
indexPatterns={indexPatterns}
|
||||||
state={state}
|
state={state}
|
||||||
setAppState={setAppState}
|
setAppState={setAppState}
|
||||||
useNewFieldsApi={useNewFieldsApi}
|
|
||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
|
@ -195,7 +194,6 @@ export function DiscoverSidebar({
|
||||||
indexPatterns={indexPatterns}
|
indexPatterns={indexPatterns}
|
||||||
state={state}
|
state={state}
|
||||||
setAppState={setAppState}
|
setAppState={setAppState}
|
||||||
useNewFieldsApi={useNewFieldsApi}
|
|
||||||
/>
|
/>
|
||||||
</EuiFlexItem>
|
</EuiFlexItem>
|
||||||
<EuiFlexItem grow={false}>
|
<EuiFlexItem grow={false}>
|
||||||
|
|
|
@ -160,7 +160,6 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
|
||||||
indexPatterns={props.indexPatterns}
|
indexPatterns={props.indexPatterns}
|
||||||
state={props.state}
|
state={props.state}
|
||||||
setAppState={props.setAppState}
|
setAppState={props.setAppState}
|
||||||
useNewFieldsApi={props.useNewFieldsApi}
|
|
||||||
/>
|
/>
|
||||||
</section>
|
</section>
|
||||||
<EuiSpacer size="s" />
|
<EuiSpacer size="s" />
|
||||||
|
|
|
@ -7,36 +7,33 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getSwitchIndexPatternAppState } from './get_switch_index_pattern_app_state';
|
import { getSwitchIndexPatternAppState } from './get_switch_index_pattern_app_state';
|
||||||
import { IIndexPatternFieldList, IndexPattern } from '../../../../data/common/index_patterns';
|
import { IndexPattern } from '../../../../data/common/index_patterns';
|
||||||
|
|
||||||
const currentIndexPattern: IndexPattern = {
|
/**
|
||||||
id: 'prev',
|
* Helper function returning an index pattern
|
||||||
getFieldByName(name) {
|
*/
|
||||||
return this.fields.getByName(name);
|
const getIndexPattern = (id: string, timeFieldName: string, fields: string[]) => {
|
||||||
},
|
return {
|
||||||
fields: {
|
id,
|
||||||
getByName: (name: string) => {
|
timeFieldName,
|
||||||
const fields = [
|
getFieldByName(name) {
|
||||||
{ name: 'category', sortable: true },
|
return this.fields.getByName(name);
|
||||||
{ name: 'name', sortable: true },
|
|
||||||
] as IIndexPatternFieldList;
|
|
||||||
return fields.find((field) => field.name === name);
|
|
||||||
},
|
},
|
||||||
},
|
fields: {
|
||||||
} as IndexPattern;
|
getByName: (name: string) => {
|
||||||
|
return fields
|
||||||
|
.map((field) => ({
|
||||||
|
name: field,
|
||||||
|
sortable: true,
|
||||||
|
}))
|
||||||
|
.find((field) => field.name === name);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as IndexPattern;
|
||||||
|
};
|
||||||
|
|
||||||
const nextIndexPattern = {
|
const currentIndexPattern = getIndexPattern('curr', '', ['category', 'name']);
|
||||||
id: 'next',
|
const nextIndexPattern = getIndexPattern('next', '', ['category']);
|
||||||
getFieldByName(name) {
|
|
||||||
return this.fields.getByName(name);
|
|
||||||
},
|
|
||||||
fields: {
|
|
||||||
getByName: (name: string) => {
|
|
||||||
const fields = [{ name: 'category', sortable: true }] as IIndexPatternFieldList;
|
|
||||||
return fields.find((field) => field.name === name);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
} as IndexPattern;
|
|
||||||
|
|
||||||
describe('Discover getSwitchIndexPatternAppState', () => {
|
describe('Discover getSwitchIndexPatternAppState', () => {
|
||||||
test('removing fields that are not part of the next index pattern, keeping unknown fields ', async () => {
|
test('removing fields that are not part of the next index pattern, keeping unknown fields ', async () => {
|
||||||
|
@ -59,10 +56,10 @@ describe('Discover getSwitchIndexPatternAppState', () => {
|
||||||
['name', 'asc'],
|
['name', 'asc'],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
expect(result.columns).toEqual(['_source']);
|
expect(result.columns).toEqual([]);
|
||||||
expect(result.sort).toEqual([['category', 'desc']]);
|
expect(result.sort).toEqual([['category', 'desc']]);
|
||||||
});
|
});
|
||||||
test('removing sorted by fields that without modifying columns', async () => {
|
test('removing sorted by fields not available in the next index pattern without modifying columns', async () => {
|
||||||
const result = getSwitchIndexPatternAppState(
|
const result = getSwitchIndexPatternAppState(
|
||||||
currentIndexPattern,
|
currentIndexPattern,
|
||||||
nextIndexPattern,
|
nextIndexPattern,
|
||||||
|
@ -76,4 +73,29 @@ describe('Discover getSwitchIndexPatternAppState', () => {
|
||||||
expect(result.columns).toEqual(['name']);
|
expect(result.columns).toEqual(['name']);
|
||||||
expect(result.sort).toEqual([['category', 'desc']]);
|
expect(result.sort).toEqual([['category', 'desc']]);
|
||||||
});
|
});
|
||||||
|
test('keep sorting by timefield when switching between index patterns with different timeFields', async () => {
|
||||||
|
const current = getIndexPattern('a', 'timeFieldA', ['timeFieldA']);
|
||||||
|
const next = getIndexPattern('b', 'timeFieldB', ['timeFieldB']);
|
||||||
|
|
||||||
|
const result = getSwitchIndexPatternAppState(current, next, [], [['timeFieldA', 'desc']]);
|
||||||
|
expect(result.columns).toEqual([]);
|
||||||
|
expect(result.sort).toEqual([['timeFieldB', 'desc']]);
|
||||||
|
});
|
||||||
|
test('remove sorting by timefield when switching to an index pattern without timefield that contains the timefield column', async () => {
|
||||||
|
// Why: timefield column is prepended, keeping the sort, user would need to add the column to remove sorting in legacy grid
|
||||||
|
const current = getIndexPattern('a', 'timeFieldA', ['timeFieldA']);
|
||||||
|
const next = getIndexPattern('b', '', ['timeFieldA']);
|
||||||
|
|
||||||
|
const result = getSwitchIndexPatternAppState(current, next, [], [['timeFieldA', 'desc']]);
|
||||||
|
expect(result.columns).toEqual([]);
|
||||||
|
expect(result.sort).toEqual([]);
|
||||||
|
});
|
||||||
|
test('add sorting by timefield when switching from an index pattern without timefield to an indexpattern with timefield', async () => {
|
||||||
|
const current = getIndexPattern('b', '', ['timeFieldA']);
|
||||||
|
const next = getIndexPattern('a', 'timeFieldA', ['timeFieldA']);
|
||||||
|
|
||||||
|
const result = getSwitchIndexPatternAppState(current, next, [], []);
|
||||||
|
expect(result.columns).toEqual([]);
|
||||||
|
expect(result.sort).toEqual([['timeFieldA', 'desc']]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,7 @@ export function getSwitchIndexPatternAppState(
|
||||||
currentColumns: string[],
|
currentColumns: string[],
|
||||||
currentSort: SortPairArr[],
|
currentSort: SortPairArr[],
|
||||||
modifyColumns: boolean = true,
|
modifyColumns: boolean = true,
|
||||||
useNewFieldsApi: boolean = false
|
sortDirection: string = 'desc'
|
||||||
) {
|
) {
|
||||||
const nextColumns = modifyColumns
|
const nextColumns = modifyColumns
|
||||||
? currentColumns.filter(
|
? currentColumns.filter(
|
||||||
|
@ -28,12 +28,20 @@ export function getSwitchIndexPatternAppState(
|
||||||
nextIndexPattern.fields.getByName(column) || !currentIndexPattern.fields.getByName(column)
|
nextIndexPattern.fields.getByName(column) || !currentIndexPattern.fields.getByName(column)
|
||||||
)
|
)
|
||||||
: currentColumns;
|
: currentColumns;
|
||||||
const nextSort = getSortArray(currentSort, nextIndexPattern);
|
const columns = nextColumns.length ? nextColumns : [];
|
||||||
const defaultColumns = useNewFieldsApi ? [] : ['_source'];
|
// when switching from an index pattern with timeField to an index pattern without timeField
|
||||||
const columns = nextColumns.length ? nextColumns : defaultColumns;
|
// filter out sorting by timeField in case it is set. index patterns without timeField don't
|
||||||
|
// prepend this field in the table, so in legacy grid you would need to add this column to
|
||||||
|
// remove sorting
|
||||||
|
const nextSort = getSortArray(currentSort, nextIndexPattern).filter((value) => {
|
||||||
|
return nextIndexPattern.timeFieldName || value[0] !== currentIndexPattern.timeFieldName;
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
index: nextIndexPattern.id,
|
index: nextIndexPattern.id,
|
||||||
columns,
|
columns,
|
||||||
sort: nextSort,
|
sort:
|
||||||
|
nextIndexPattern.timeFieldName && !nextSort.length
|
||||||
|
? [[nextIndexPattern.timeFieldName, sortDirection]]
|
||||||
|
: nextSort,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue