Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ describe('filter_manager', () => {
appStateStub = new StubState();
globalStateStub = new StubState();
indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(indexPatterns as IndexPatterns, setupMock.uiSettings);
filterManager = new FilterManager(
(indexPatterns as unknown) as IndexPatterns,
setupMock.uiSettings
);
readyFilters = getFiltersArray();

// FilterStateManager is tested indirectly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ describe('filter_state_manager', () => {
appStateStub = new StubState();
globalStateStub = new StubState();
const indexPatterns = new StubIndexPatterns();
filterManager = new FilterManager(indexPatterns as IndexPatterns, setupMock.uiSettings);
filterManager = new FilterManager(
(indexPatterns as unknown) as IndexPatterns,
setupMock.uiSettings
);
});

describe('app_state_undefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

export class StubIndexPatterns {
async get(index: any) {
async get(index: string) {
return {
fields: {
byName: {},
Expand Down
1 change: 0 additions & 1 deletion src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export {
getRoutes,
isFilterable,
IndexPatternSelect,
IndexPatternsProvider, // LEGACY
validateIndexPattern,
ILLEGAL_CHARACTERS,
INDEX_PATTERN_ILLEGAL_CHARACTERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/

import { IndexPattern } from './index_pattern';
import { GetFieldsOptions, IndexPatternsApiClient } from './index_patterns_api_client';
import { GetFieldsOptions, IIndexPatternsApiClient } from './index_patterns_api_client';

/** @internal */
export const createFieldsFetcher = (
indexPattern: IndexPattern,
apiClient: IndexPatternsApiClient,
apiClient: IIndexPatternsApiClient,
metaFields: string
) => {
const fieldFetcher = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { createFieldsFetcher } from './_fields_fetcher';
import { getRoutes } from '../utils';
import { formatHitProvider } from './format_hit';
import { flattenHitWrapper } from './flatten_hit';
import { IndexPatternsApiClient } from './index_patterns_api_client';
import { IIndexPatternsApiClient } from './index_patterns_api_client';

const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3;
const type = 'index-pattern';
Expand Down Expand Up @@ -113,7 +113,7 @@ export class IndexPattern implements StaticIndexPattern {
id: string | undefined,
getConfig: any,
savedObjectsClient: SavedObjectsClientContract,
apiClient: IndexPatternsApiClient,
apiClient: IIndexPatternsApiClient,
patternCache: any
) {
this.id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

// eslint-disable-next-line max-classes-per-file
import { IndexPatterns } from './index_patterns';
import { SavedObjectsClientContract, UiSettingsClientContract } from 'kibana/public';
import {
SavedObjectsClientContract,
UiSettingsClientContract,
HttpServiceBase,
} from 'kibana/public';

jest.mock('../errors', () => ({
IndexPatternMissingIndices: jest.fn(),
Expand Down Expand Up @@ -65,12 +69,9 @@ describe('IndexPatterns', () => {
beforeEach(() => {
const savedObjectsClient = {} as SavedObjectsClientContract;
const uiSettings = {} as UiSettingsClientContract;
const http = {} as HttpServiceBase;

indexPatterns = new IndexPatterns(uiSettings, savedObjectsClient);
});

test('does not cache gets without an id', () => {
expect(indexPatterns.get()).not.toBe(indexPatterns.get());
indexPatterns = new IndexPatterns(uiSettings, savedObjectsClient, http);
});

test('does cache gets for the same id', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
SavedObjectsClientContract,
SimpleSavedObject,
UiSettingsClientContract,
HttpServiceBase,
} from 'src/core/public';
// @ts-ignore
import { fieldFormats } from 'ui/registry/field_formats';
Expand All @@ -31,16 +32,22 @@ import { IndexPattern } from './index_pattern';
import { IndexPatternsApiClient } from './index_patterns_api_client';

const indexPatternCache = createIndexPatternCache();
const apiClient = new IndexPatternsApiClient();

export class IndexPatterns {
fieldFormats: fieldFormats;

private config: UiSettingsClientContract;
private savedObjectsClient: SavedObjectsClientContract;
private savedObjectsCache?: Array<SimpleSavedObject<Record<string, any>>> | null;
private apiClient: IndexPatternsApiClient;

constructor(
config: UiSettingsClientContract,
savedObjectsClient: SavedObjectsClientContract,
http: HttpServiceBase
) {
this.apiClient = new IndexPatternsApiClient(http);

constructor(config: UiSettingsClientContract, savedObjectsClient: SavedObjectsClientContract) {
this.config = config;
this.savedObjectsClient = savedObjectsClient;
}
Expand Down Expand Up @@ -105,9 +112,7 @@ export class IndexPatterns {
return null;
};

get = (id?: string) => {
if (!id) return this.make();

get = (id: string) => {
const cache = indexPatternCache.get(id);
return cache || indexPatternCache.set(id, this.make(id));
};
Expand All @@ -117,27 +122,8 @@ export class IndexPatterns {
id,
(cfg: any) => this.config.get(cfg),
this.savedObjectsClient,
apiClient,
this.apiClient,
indexPatternCache
).init();
};
}

// add angular service for backward compatibility
// @ts-ignore
// eslint-disable-next-line
import { uiModules } from 'ui/modules';

const module = uiModules.get('kibana/index_patterns');
let _service: any;
module.service('indexPatterns', function(chrome: any) {
if (!_service)
_service = new IndexPatterns(chrome.getUiSettingsClient(), chrome.getSavedObjectsClient());
return _service;
});

export const IndexPatternsProvider = (chrome: any) => {
if (!_service)
_service = new IndexPatterns(chrome.getUiSettingsClient(), chrome.getSavedObjectsClient());
return _service;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,36 @@
import { http } from './index_patterns_api_client.test.mock';
import { IndexPatternsApiClient } from './index_patterns_api_client';

const requestData = {
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
prependBasePath: true,
query: {},
};

describe('IndexPatternsApiClient', () => {
let fetchSpy: jest.Mock;
let indexPatternsApiClient: IndexPatternsApiClient;

beforeEach(() => {
fetchSpy = jest.spyOn(http, 'fetch').mockImplementation(() => Promise.resolve({}));
indexPatternsApiClient = new IndexPatternsApiClient();
indexPatternsApiClient = new IndexPatternsApiClient(http);
});

test('uses the right URI to fetch fields for time patterns', async function() {
const expectedPath = '/api/index_patterns/_fields_for_time_pattern';

await indexPatternsApiClient.getFieldsForTimePattern();

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, requestData);
expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});

test('uses the right URI to fetch fields for wildcard', async function() {
const expectedPath = '/api/index_patterns/_fields_for_wildcard';

await indexPatternsApiClient.getFieldsForWildcard();

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, requestData);
expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});

test('uses the right URI to fetch fields for wildcard given a type', async function() {
const expectedPath = '/api/index_patterns/rollup/_fields_for_wildcard';

await indexPatternsApiClient.getFieldsForWildcard({ type: 'rollup' });

expect(fetchSpy).toHaveBeenCalledWith(expectedPath, requestData);
expect(fetchSpy).toHaveBeenCalledWith(expectedPath, expect.any(Object));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,9 @@
* under the License.
*/

import { kfetch, KFetchQuery } from 'ui/kfetch';

import { HttpServiceBase } from 'src/core/public';
import { IndexPatternMissingIndices } from '../errors';

function request(url: string, query: KFetchQuery) {
return kfetch({
method: 'GET',
pathname: url,
query,
}).catch(resp => {
if (resp.body.statusCode === 404 && resp.body.statuscode === 'no_matching_indices') {
throw new IndexPatternMissingIndices(resp.body.message);
}

throw new Error(resp.body.message || resp.body.error || `${resp.body.statusCode} Response`);
});
}

const API_BASE_URL: string = `/api/index_patterns/`;

export interface GetFieldsOptions {
Expand All @@ -45,8 +30,28 @@ export interface GetFieldsOptions {
metaFields?: string;
}

export type IIndexPatternsApiClient = PublicMethodsOf<IndexPatternsApiClient>;

export class IndexPatternsApiClient {
constructor() {}
private http: HttpServiceBase;

constructor(http: HttpServiceBase) {
this.http = http;
}

private _request(url: string, query: any) {
return this.http
.fetch(url, {
query,
})
.catch((resp: any) => {
if (resp.body.statusCode === 404 && resp.body.statuscode === 'no_matching_indices') {
throw new IndexPatternMissingIndices(resp.body.message);
}

throw new Error(resp.body.message || resp.body.error || `${resp.body.statusCode} Response`);
});
}

_getUrl(path: string[]) {
return (
Expand All @@ -63,11 +68,11 @@ export class IndexPatternsApiClient {

const url = this._getUrl(['_fields_for_time_pattern']);

return request(url, {
return this._request(url, {
pattern,
look_back: lookBack,
meta_fields: metaFields,
}).then(resp => resp.fields);
}).then((resp: any) => resp.fields);
}

getFieldsForWildcard(options: GetFieldsOptions = {}) {
Expand All @@ -91,6 +96,6 @@ export class IndexPatternsApiClient {
};
}

return request(url, query).then(resp => resp.fields);
return this._request(url, query).then((resp: any) => resp.fields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* under the License.
*/

import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public';
import {
UiSettingsClientContract,
SavedObjectsClientContract,
HttpServiceBase,
} from 'src/core/public';
import { Field, FieldList, FieldType } from './fields';
import { createFlattenHitWrapper } from './index_patterns';
import { createIndexPatternSelect } from './components';
Expand All @@ -31,6 +35,7 @@ import {
export interface IndexPatternDependencies {
uiSettings: UiSettingsClientContract;
savedObjectsClient: SavedObjectsClientContract;
http: HttpServiceBase;
}

/**
Expand All @@ -39,12 +44,12 @@ export interface IndexPatternDependencies {
* @internal
*/
export class IndexPatternsService {
public setup({ uiSettings, savedObjectsClient }: IndexPatternDependencies) {
public setup({ uiSettings, savedObjectsClient, http }: IndexPatternDependencies) {
return {
FieldList,
flattenHitWrapper: createFlattenHitWrapper(),
formatHitProvider,
indexPatterns: new IndexPatterns(uiSettings, savedObjectsClient),
indexPatterns: new IndexPatterns(uiSettings, savedObjectsClient, http),
IndexPatternSelect: createIndexPatternSelect(savedObjectsClient),
__LEGACY: {
// For BWC we must temporarily export the class implementation of Field,
Expand All @@ -67,7 +72,6 @@ export class IndexPatternsService {

/** @public */
export { IndexPatternSelect } from './components';
export { IndexPatternsProvider } from './index_patterns';
export {
CONTAINS_SPACES,
getFromSavedObject,
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/core_plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ export class DataPlugin implements Plugin<DataSetup, {}, DataPluginSetupDependen
private setupApi!: DataSetup;

public setup(core: CoreSetup, { __LEGACY }: DataPluginSetupDependencies): DataSetup {
const { uiSettings } = core;
const { uiSettings, http } = core;
const savedObjectsClient = __LEGACY.savedObjectsClient;

const indexPatternsService = this.indexPatterns.setup({
uiSettings,
savedObjectsClient,
http,
});

this.setupApi = {
Expand Down
Loading