Skip to content

Commit

Permalink
Initial conversion of Index Patterns service to TypeScript.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored and lukeelmers committed Jul 30, 2019
1 parent 9a321ae commit ba8a453
Show file tree
Hide file tree
Showing 86 changed files with 1,291 additions and 1,504 deletions.
6 changes: 5 additions & 1 deletion packages/kbn-es-query/src/filters/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
* under the License.
*/

import { Field, IndexPattern } from 'ui/index_patterns';
import { CustomFilter, ExistsFilter, PhraseFilter, PhrasesFilter, RangeFilter } from './lib';
import { RangeFilterParams } from './lib/range_filter';

export * from './lib';

// We can't import the real types from the data plugin, so need to either duplicate
// them here or figure out another solution, perhaps housing them in this package
type Field = any;
type IndexPattern = any;

export function buildExistsFilter(field: Field, indexPattern: IndexPattern): ExistsFilter;

export function buildPhraseFilter(
Expand Down
4 changes: 1 addition & 3 deletions packages/kbn-es-query/src/kuery/ast/ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
* WARNING: these typings are incomplete
*/

import { StaticIndexPattern } from 'ui/index_patterns';

export type KueryNode = any;

export interface KueryParseOptions {
Expand All @@ -46,6 +44,6 @@ export function fromKueryExpression(
parseOptions?: KueryParseOptions
): KueryNode;

export function toElasticsearchQuery(node: KueryNode, indexPattern: StaticIndexPattern): JsonObject;
export function toElasticsearchQuery(node: KueryNode, indexPattern: any): JsonObject;

export function doesKueryExpressionHaveLuceneSyntaxError(expression: string): boolean;
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class FilterEditorUI extends Component<Props, State> {

if (isCustomEditorOpen) {
const { index, disabled, negate } = this.props.filter.meta;
const newIndex = index || this.props.indexPatterns[0].id;
const newIndex = index || this.props.indexPatterns[0].id!;
const body = JSON.parse(queryDsl);
const filter = buildCustomFilter(newIndex, body, disabled, negate, alias, $state.store);
this.props.onSubmit(filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

import { FilterStateStore, toggleFilterNegated } from '@kbn/es-query';

import { fixtures } from '../../../../index_patterns';
import { IndexPattern, Field } from '../../../../index';
import {
buildFilter,
getFieldFromFilter,
Expand Down Expand Up @@ -58,6 +58,8 @@ jest.mock(
);

const { mockFields, mockIndexPattern } = fixtures;
const mockedFields = mockFields as Field[];
const mockedIndexPattern = mockIndexPattern as IndexPattern;

describe('Filter editor utils', () => {
describe('getQueryDslFromFilter', () => {
Expand All @@ -70,14 +72,14 @@ describe('Filter editor utils', () => {

describe('getIndexPatternFromFilter', () => {
it('should return the index pattern from the filter', () => {
const indexPattern = getIndexPatternFromFilter(phraseFilter, [mockIndexPattern]);
expect(indexPattern).toBe(mockIndexPattern);
const indexPattern = getIndexPatternFromFilter(phraseFilter, [mockedIndexPattern]);
expect(indexPattern).toBe(mockedIndexPattern);
});
});

describe('getFieldFromFilter', () => {
it('should return the field from the filter', () => {
const field = getFieldFromFilter(phraseFilter, mockIndexPattern);
const field = getFieldFromFilter(phraseFilter, mockedIndexPattern);
expect(field).not.toBeUndefined();
expect(field && field.name).toBe(phraseFilter.meta.key);
});
Expand Down Expand Up @@ -169,12 +171,12 @@ describe('Filter editor utils', () => {

describe('getFilterableFields', () => {
it('returns the list of fields from the given index pattern', () => {
const fieldOptions = getFilterableFields(mockIndexPattern);
const fieldOptions = getFilterableFields(mockedIndexPattern);
expect(fieldOptions.length).toBeGreaterThan(0);
});

it('limits the fields to the filterable fields', () => {
const fieldOptions = getFilterableFields(mockIndexPattern);
const fieldOptions = getFilterableFields(mockedIndexPattern);
const nonFilterableFields = fieldOptions.filter(field => !field.filterable);
expect(nonFilterableFields.length).toBe(0);
});
Expand All @@ -183,67 +185,72 @@ describe('Filter editor utils', () => {
describe('getOperatorOptions', () => {
it('returns range for number fields', () => {
const [field] = mockFields.filter(({ type }) => type === 'number');
const operatorOptions = getOperatorOptions(field);
const operatorOptions = getOperatorOptions(field as Field);
const rangeOperator = operatorOptions.find(operator => operator.type === 'range');
expect(rangeOperator).not.toBeUndefined();
});

it('does not return range for string fields', () => {
const [field] = mockFields.filter(({ type }) => type === 'string');
const operatorOptions = getOperatorOptions(field);
const operatorOptions = getOperatorOptions(field as Field);
const rangeOperator = operatorOptions.find(operator => operator.type === 'range');
expect(rangeOperator).toBeUndefined();
});
});

describe('isFilterValid', () => {
it('should return false if index pattern is not provided', () => {
const isValid = isFilterValid(undefined, mockFields[0], isOperator, 'foo');
const isValid = isFilterValid(undefined, mockedFields[0], isOperator, 'foo');
expect(isValid).toBe(false);
});

it('should return false if field is not provided', () => {
const isValid = isFilterValid(mockIndexPattern, undefined, isOperator, 'foo');
const isValid = isFilterValid(mockedIndexPattern, undefined, isOperator, 'foo');
expect(isValid).toBe(false);
});

it('should return false if operator is not provided', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], undefined, 'foo');
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], undefined, 'foo');
expect(isValid).toBe(false);
});

it('should return false for phrases filter without phrases', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], isOneOfOperator, []);
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], isOneOfOperator, []);
expect(isValid).toBe(false);
});

it('should return true for phrases filter with phrases', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], isOneOfOperator, ['foo']);
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], isOneOfOperator, ['foo']);
expect(isValid).toBe(true);
});

it('should return false for range filter without range', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], isBetweenOperator, undefined);
const isValid = isFilterValid(
mockedIndexPattern,
mockedFields[0],
isBetweenOperator,
undefined
);
expect(isValid).toBe(false);
});

it('should return true for range filter with from', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], isBetweenOperator, {
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], isBetweenOperator, {
from: 'foo',
});
expect(isValid).toBe(true);
});

it('should return true for range filter with from/to', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], isBetweenOperator, {
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], isBetweenOperator, {
from: 'foo',
too: 'goo',
});
expect(isValid).toBe(true);
});

it('should return true for exists filter without params', () => {
const isValid = isFilterValid(mockIndexPattern, mockFields[0], existsOperator);
const isValid = isFilterValid(mockedIndexPattern, mockedFields[0], existsOperator);
expect(isValid).toBe(true);
});
});
Expand All @@ -253,7 +260,14 @@ describe('Filter editor utils', () => {
const params = 'foo';
const alias = 'bar';
const state = FilterStateStore.APP_STATE;
const filter = buildFilter(mockIndexPattern, mockFields[0], isOperator, params, alias, state);
const filter = buildFilter(
mockedIndexPattern,
mockedFields[0],
isOperator,
params,
alias,
state
);
expect(filter.meta.negate).toBe(isOperator.negate);
expect(filter.meta.alias).toBe(alias);

Expand All @@ -268,8 +282,8 @@ describe('Filter editor utils', () => {
const alias = 'bar';
const state = FilterStateStore.APP_STATE;
const filter = buildFilter(
mockIndexPattern,
mockFields[0],
mockedIndexPattern,
mockedFields[0],
isOneOfOperator,
params,
alias,
Expand All @@ -289,8 +303,8 @@ describe('Filter editor utils', () => {
const alias = 'bar';
const state = FilterStateStore.APP_STATE;
const filter = buildFilter(
mockIndexPattern,
mockFields[0],
mockedIndexPattern,
mockedFields[0],
isBetweenOperator,
params,
alias,
Expand All @@ -309,8 +323,8 @@ describe('Filter editor utils', () => {
const alias = 'bar';
const state = FilterStateStore.APP_STATE;
const filter = buildFilter(
mockIndexPattern,
mockFields[0],
mockedIndexPattern,
mockedFields[0],
existsOperator,
params,
alias,
Expand All @@ -329,8 +343,8 @@ describe('Filter editor utils', () => {
const alias = 'bar';
const state = FilterStateStore.APP_STATE;
const filter = buildFilter(
mockIndexPattern,
mockFields[0],
mockedIndexPattern,
mockedFields[0],
doesNotExistOperator,
params,
alias,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Filter, FilterStateStore } from '@kbn/es-query';
import { FilterStateManager } from './filter_state_manager';
import { FilterManager } from './filter_manager';

import { IndexPatterns } from 'ui/index_patterns';
import { getFilter } from './test_helpers/get_stub_filter';
import { StubIndexPatterns } from './test_helpers/stub_index_pattern';
import { StubState } from './test_helpers/stub_state';
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('filter_manager', () => {
appStateStub = new StubState();
globalStateStub = new StubState();
indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(indexPatterns);
filterManager = new FilterManager(indexPatterns as IndexPatterns);
readyFilters = getFiltersArray();

// FilterStateManager is tested indirectly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import sinon from 'sinon';
import { FilterStateStore } from '@kbn/es-query';
import { FilterStateManager } from './filter_state_manager';

import { IndexPatterns } from 'ui/index_patterns';
import { StubState } from './test_helpers/stub_state';
import { getFilter } from './test_helpers/get_stub_filter';
import { FilterManager } from './filter_manager';
Expand Down Expand Up @@ -54,7 +55,7 @@ describe('filter_state_manager', () => {
appStateStub = new StubState();
globalStateStub = new StubState();
const indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(indexPatterns);
filterManager = new FilterManager(indexPatterns as IndexPatterns);
});

describe('app_state_undefined', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface DataSetup {
export { ExpressionRenderer, ExpressionRendererProps, ExpressionRunner } from './expressions';

/** @public types */
export { IndexPattern, StaticIndexPattern, StaticIndexPatternField, Field } from './index_patterns';
export { IndexPattern, StaticIndexPattern, Field } from './index_patterns';
export { Query, QueryBar } from './query';
export { FilterBar } from './filter';
export {
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/core_plugins/data/public/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
export {
IndexPatternsService,
IndexPatterns,
fixtures,
utils,
// types
IndexPatternsSetup,
IndexPattern,
StaticIndexPattern,
StaticIndexPatternField,
Field,
fixtures,
utils,
FieldType,
} from './index_patterns_service';
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { validateIndexPattern } from 'ui/index_patterns/index';

import { isFilterable, getFromSavedObject } from 'ui/index_patterns/static_utils';

// IndexPattern, StaticIndexPattern, StaticIndexPatternField, Field
// IndexPattern, StaticIndexPattern, Field
import * as types from 'ui/index_patterns';

const config = chrome.getUiSettingsClient();
Expand Down Expand Up @@ -94,7 +94,7 @@ export type IndexPattern = types.IndexPattern;
export type StaticIndexPattern = types.StaticIndexPattern;

/** @public */
export type StaticIndexPatternField = types.StaticIndexPatternField;
export type Field = types.Field;

/** @public */
export type Field = types.Field;
export type FieldType = types.FieldType;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import React from 'react';
import { shallowWithIntl } from 'test_utils/enzyme_helpers';
import './query_bar.test.mocks';
import { QueryBar } from './query_bar';
import { IndexPattern } from '../../../index';

const noop = () => {
return;
Expand Down Expand Up @@ -63,7 +64,7 @@ const mockIndexPattern = {
searchable: true,
},
],
};
} as IndexPattern;

describe('QueryBar', () => {
const QUERY_INPUT_SELECTOR = 'InjectIntl(QueryBarInputUI)';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import React from 'react';
import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { QueryLanguageSwitcher } from './language_switcher';
import { QueryBarInput, QueryBarInputUI } from './query_bar_input';
import { IndexPattern } from '../../../index';

const noop = () => {
return;
Expand Down Expand Up @@ -73,7 +74,7 @@ const mockIndexPattern = {
searchable: true,
},
],
};
} as IndexPattern;

describe('QueryBarInput', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class QueryBarInputUI extends Component<Props, State> {
indexPattern => typeof indexPattern !== 'string'
) as IndexPattern[];

const objectPatternsFromStrings = await fetchIndexPatterns(stringPatterns);
const objectPatternsFromStrings = (await fetchIndexPatterns(stringPatterns)) as IndexPattern[];

this.setState({
indexPatterns: [...objectPatterns, ...objectPatternsFromStrings],
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/core_plugins/kibana/public/context/api/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
// @ts-ignore
import { SearchSourceProvider, SearchSource } from 'ui/courier';
import { IPrivate } from 'ui/private';
import { IndexPatternEnhanced, IndexPatternGetProvider } from 'ui/index_patterns/_index_pattern';
import { Filter } from '@kbn/es-query';
import { IndexPatterns, IndexPattern } from 'ui/index_patterns';
import { reverseSortDir, SortDirection } from './utils/sorting';
import { extractNanos, convertIsoToMillis } from './utils/date_conversion';
import { fetchHitsInInterval } from './utils/fetch_hits_in_interval';
Expand All @@ -42,7 +42,7 @@ const DAY_MILLIS = 24 * 60 * 60 * 1000;
// look from 1 day up to 10000 days into the past and future
const LOOKUP_OFFSETS = [0, 1, 7, 30, 365, 10000].map(days => days * DAY_MILLIS);

function fetchContextProvider(indexPatterns: IndexPatternGetProvider, Private: IPrivate) {
function fetchContextProvider(indexPatterns: IndexPatterns, Private: IPrivate) {
const SearchSourcePrivate: any = Private(SearchSourceProvider);

return {
Expand Down Expand Up @@ -112,7 +112,7 @@ function fetchContextProvider(indexPatterns: IndexPatternGetProvider, Private: I
return documents;
}

async function createSearchSource(indexPattern: IndexPatternEnhanced, filters: Filter[]) {
async function createSearchSource(indexPattern: IndexPattern, filters: Filter[]) {
return new SearchSourcePrivate()
.setParent(false)
.setField('index', indexPattern)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('DashboardState', function() {
isAutoRefreshSelectorEnabled: true,
isTimeRangeSelectorEnabled: true,
};

function initDashboardState() {
dashboardState = new DashboardStateManager({
savedDashboard,
Expand Down
Loading

0 comments on commit ba8a453

Please sign in to comment.