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

Input controls crashes if index pattern is not available #79431

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/plugins/input_control_vis/public/control/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export abstract class Control<FilterManager extends BaseFilterManager> {
format = (value: any) => {
const indexPattern = this.filterManager.getIndexPattern();
const field = this.filterManager.getField();
if (field) {
if (field && indexPattern) {
return indexPattern.getFormatterForField(field).convert(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('FilterManager', function () {
let filterManager: FilterManagerTest;
beforeEach(() => {
kbnFilters = [];
filterManager = new FilterManagerTest(controlId, 'field1', indexPatternMock, queryFilterMock);
filterManager = new FilterManagerTest(controlId, 'field1', queryFilterMock, indexPatternMock);
});

test('should not find filters that are not controlled by any visualization', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export abstract class FilterManager {
constructor(
public controlId: string,
public fieldName: string,
public indexPattern: IndexPattern,
public queryFilter: QueryFilterManager
public queryFilter: QueryFilterManager,
public indexPattern?: IndexPattern
) {}

/**
Expand All @@ -41,12 +41,12 @@ export abstract class FilterManager {

abstract getValueFromFilterBar(): any;

getIndexPattern(): IndexPattern {
getIndexPattern(): IndexPattern | undefined {
return this.indexPattern;
}

getField() {
return this.indexPattern.fields.getByName(this.fieldName);
return this.indexPattern?.fields.getByName(this.fieldName);
}

findFilters(): Filter[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('PhraseFilterManager', function () {
filterManager = new PhraseFilterManager(
controlId,
'field1',
indexPatternMock,
queryFilterMock
queryFilterMock,
indexPatternMock
);
});

Expand Down Expand Up @@ -89,10 +89,10 @@ describe('PhraseFilterManager', function () {
constructor(
id: string,
fieldName: string,
indexPattern: IndexPattern,
queryFilter: QueryFilterManager
queryFilter: QueryFilterManager,
indexPattern?: IndexPattern
) {
super(id, fieldName, indexPattern, queryFilter);
super(id, fieldName, queryFilter, indexPattern);
this.mockFilters = [];
}

Expand All @@ -112,8 +112,8 @@ describe('PhraseFilterManager', function () {
filterManager = new MockFindFiltersPhraseFilterManager(
controlId,
'field1',
indexPatternMock,
queryFilterMock
queryFilterMock,
indexPatternMock
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ export class PhraseFilterManager extends FilterManager {
constructor(
controlId: string,
fieldName: string,
indexPattern: IndexPattern,
queryFilter: QueryFilterManager
queryFilter: QueryFilterManager,
indexPattern?: IndexPattern
) {
super(controlId, fieldName, indexPattern, queryFilter);
super(controlId, fieldName, queryFilter, indexPattern);
}

createFilter(phrases: any): PhraseFilter {
const indexPattern = this.indexPattern!;
let newFilter: PhraseFilter;
const value = this.indexPattern.fields.getByName(this.fieldName);
const value = indexPattern.fields.getByName(this.fieldName);

if (!value) {
throw new Error(`Unable to find field with name: ${this.fieldName} on indexPattern`);
}

if (phrases.length === 1) {
newFilter = esFilters.buildPhraseFilter(value, phrases[0], this.indexPattern);
newFilter = esFilters.buildPhraseFilter(value, phrases[0], indexPattern);
} else {
newFilter = esFilters.buildPhrasesFilter(value, phrases, this.indexPattern);
newFilter = esFilters.buildPhrasesFilter(value, phrases, indexPattern);
}

newFilter.meta.key = this.fieldName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ describe('RangeFilterManager', function () {
filterManager = new RangeFilterManager(
controlId,
'field1',
indexPatternMock,
queryFilterMock
queryFilterMock,
indexPatternMock
);
});

Expand All @@ -75,10 +75,10 @@ describe('RangeFilterManager', function () {
constructor(
id: string,
fieldName: string,
indexPattern: IndexPattern,
queryFilter: QueryFilterManager
queryFilter: QueryFilterManager,
indexPattern?: IndexPattern
) {
super(id, fieldName, indexPattern, queryFilter);
super(id, fieldName, queryFilter, indexPattern);
this.mockFilters = [];
}

Expand All @@ -98,8 +98,8 @@ describe('RangeFilterManager', function () {
filterManager = new MockFindFiltersRangeFilterManager(
controlId,
'field1',
indexPatternMock,
queryFilterMock
queryFilterMock,
indexPatternMock
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ export class RangeFilterManager extends FilterManager {
* @return {object} range filter
*/
createFilter(value: SliderValue): RangeFilter {
const indexPattern = this.indexPattern!;
const newFilter = esFilters.buildRangeFilter(
// TODO: Fix type to be required
this.indexPattern.fields.getByName(this.fieldName) as IFieldType,
indexPattern.fields.getByName(this.fieldName) as IFieldType,
toRange(value),
this.indexPattern
indexPattern
);
newFilter.meta.key = this.fieldName;
newFilter.meta.controlledBy = this.controlId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,32 @@ export async function listControlFactory(
deps: InputControlVisDependencies
) {
const [, { data: dataPluginStart }] = await deps.core.getStartServices();
const indexPattern = await dataPluginStart.indexPatterns.get(controlParams.indexPattern);

let indexPattern;
try {
indexPattern = await dataPluginStart.indexPatterns.get(controlParams.indexPattern);
} catch (e) {
// invalid index pattern id
}

// dynamic options are only allowed on String fields but the setting defaults to true so it could
// be enabled for non-string fields (since UI input is hidden for non-string fields).
// If field is not string, then disable dynamic options.
const field = indexPattern.fields.getAll().find(({ name }) => name === controlParams.fieldName);
const field = indexPattern?.fields.getAll().find(({ name }) => name === controlParams.fieldName);
if (field && field.type !== 'string') {
controlParams.options.dynamicOptions = false;
}

const listControl = new ListControl(
return new ListControl(
controlParams,
new PhraseFilterManager(
controlParams.id,
controlParams.fieldName,
indexPattern,
deps.data.query.filterManager
deps.data.query.filterManager,
indexPattern
),
useTimeFilter,
dataPluginStart.search.searchSource,
deps
);
return listControl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,21 @@ export async function rangeControlFactory(
deps: InputControlVisDependencies
): Promise<RangeControl> {
const [, { data: dataPluginStart }] = await deps.core.getStartServices();
const indexPattern = await dataPluginStart.indexPatterns.get(controlParams.indexPattern);

let indexPattern;
try {
indexPattern = await dataPluginStart.indexPatterns.get(controlParams.indexPattern);
} catch (e) {
// invalid index pattern id
}

return new RangeControl(
controlParams,
new RangeFilterManager(
controlParams.id,
controlParams.fieldName,
indexPattern,
deps.data.query.filterManager
deps.data.query.filterManager,
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
indexPattern
),
useTimeFilter,
dataPluginStart.search.searchSource,
Expand Down