Skip to content

Commit

Permalink
Move legacy/server/index_patterns -> src/plugins/data/server (#46537) (
Browse files Browse the repository at this point in the history
…#50081)

Dismissed @elastic/kibana-app review of import changes.

ts-ignored @ts-ignore

* Moved files to plugins/data/server.

* Renamed IndexPatternsService to IndexPatternsFetcher and created new IndexPatternsService

* Set routerPath.

* Fixed type error.

* Changed beats_management _fields_for_wildcard request with data/public/legacy api.

* Fixed changed paths.

* Fixes crashes after merge.

* Updated path for clarity.

* Applied Plugin interface to service.

* Fixed test failure caused by non camel case local variable name.

* Fixed import to IndexPatternsService to IndexPatternsFetcher.
  • Loading branch information
Liza Katz authored Nov 7, 2019
1 parent e21a104 commit 19dfe26
Show file tree
Hide file tree
Showing 34 changed files with 109 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/fixtures/logstash_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { castEsToKbnFieldTypeName } from '../plugins/data/common';
// eslint-disable-next-line max-len
import { shouldReadFieldFromDocValues } from '../legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values';
import { shouldReadFieldFromDocValues } from '../plugins/data/server';

function stubbedLogstashFields() {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { fieldFormats } from 'ui/registry/field_formats';

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

const indexPatternCache = createIndexPatternCache();

Expand Down Expand Up @@ -93,6 +93,14 @@ export class IndexPatterns {
});
};

getFieldsForTimePattern = (options: GetFieldsOptions = {}) => {
return this.apiClient.getFieldsForTimePattern(options);
};

getFieldsForWildcard = (options: GetFieldsOptions = {}) => {
return this.apiClient.getFieldsForWildcard(options);
};

clearCache = (id?: string) => {
this.savedObjectsCache = null;
if (id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* under the License.
*/

import { IndexPatternsService } from '../../../../server/index_patterns/service';
import { IndexPatternsFetcher } from '../../../../../plugins/data/server/';
export const getIndexPatternService = {
assign: 'indexPatternsService',
method(req) {
const dataCluster = req.server.plugins.elasticsearch.getCluster('data');
const callDataCluster = (...args) => {
return dataCluster.callWithRequest(req, ...args);
};
return new IndexPatternsService(callDataCluster);
return new IndexPatternsFetcher(callDataCluster);
},
};
6 changes: 4 additions & 2 deletions src/legacy/server/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

// @ts-ignore no types
export { indexPatternsMixin } from './mixin';
export { IndexPatternsService, FieldDescriptor } from './service';
export {
IndexPatternsFetcher,
FieldDescriptor,
} from '../../../plugins/data/server/index_patterns/fetcher';
export { IndexPatternsServiceFactory } from './mixin';
9 changes: 3 additions & 6 deletions src/legacy/server/index_patterns/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
* under the License.
*/

import { IndexPatternsService } from './service';
import { IndexPatternsFetcher } from '../../../plugins/data/server';
import KbnServer from '../kbn_server';
import { APICaller, CallAPIOptions } from '../../../core/server';
import { Legacy } from '../../../../kibana';
import { registerRoutes } from './routes';

export function indexPatternsMixin(kbnServer: KbnServer, server: Legacy.Server) {
/**
Expand All @@ -31,7 +30,7 @@ export function indexPatternsMixin(kbnServer: KbnServer, server: Legacy.Server)
* @type {IndexPatternsService}
*/
server.decorate('server', 'indexPatternsServiceFactory', ({ callCluster }) => {
return new IndexPatternsService(callCluster);
return new IndexPatternsFetcher(callCluster);
});

/**
Expand All @@ -50,10 +49,8 @@ export function indexPatternsMixin(kbnServer: KbnServer, server: Legacy.Server)
) => callWithRequest(request, endpoint, params, options);
return server.indexPatternsServiceFactory({ callCluster });
});

registerRoutes(kbnServer.newPlatform.setup.core);
}

export type IndexPatternsServiceFactory = (args: {
callCluster: (endpoint: string, clientParams: any, options: any) => Promise<any>;
}) => IndexPatternsService;
}) => IndexPatternsFetcher;
2 changes: 1 addition & 1 deletion src/legacy/server/kbn_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ export default class KbnServer {
export { Server, Request, ResponseToolkit } from 'hapi';

// Re-export commonly accessed api types.
export { IndexPatternsService } from './index_patterns';
export { IndexPatternsFetcher as IndexPatternsService } from './index_patterns';
export { SavedObjectsLegacyService, SavedObjectsClient } from 'src/core/server';
5 changes: 5 additions & 0 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export function plugin(initializerContext: PluginInitializerContext) {
}

export { DataServerPlugin as Plugin };
export {
IndexPatternsFetcher,
FieldDescriptor,
shouldReadFieldFromDocValues,
} from './index_patterns';

export * from './search';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
* under the License.
*/

export * from './index_patterns_service';
export * from './index_patterns_fetcher';
export { shouldReadFieldFromDocValues } from './lib';
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface FieldSubType {
nested?: { path: string };
}

export class IndexPatternsService {
export class IndexPatternsFetcher {
private _callDataCluster: APICaller;

constructor(callDataCluster: APICaller) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { APICaller } from 'src/core/server';
// @ts-ignore
import { convertEsError } from './errors';
import { FieldCapsResponse } from './field_capabilities';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { APICaller } from 'src/core/server';
import { callFieldCapsApi } from '../es_api';
import { FieldCapsResponse, readFieldCapsResponse } from './field_caps_response';
import { mergeOverrides } from './overrides';
import { FieldDescriptor } from '../../index_patterns_service';
import { FieldDescriptor } from '../../index_patterns_fetcher';

export function concatIfUniq<T>(arr: T[], value: T) {
return arr.includes(value) ? arr : arr.concat(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import sinon from 'sinon';
import * as shouldReadFieldFromDocValuesNS from './should_read_field_from_doc_values';
import { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values';

import { getKbnFieldType } from '../../../../../../plugins/data/common';
import { getKbnFieldType } from '../../../../../../data/common';
import { readFieldCapsResponse } from './field_caps_response';
import esResponse from './__fixtures__/es_field_caps_response.json';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/

import { uniq } from 'lodash';
import { FieldDescriptor } from '../..';
import { castEsToKbnFieldTypeName } from '../../../../../common';
import { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values';
import { castEsToKbnFieldTypeName } from '../../../../../../plugins/data/common';
import { FieldDescriptor } from '../../../fetcher';

interface FieldCapObject {
type: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

export { getFieldCapabilities } from './field_capabilities';
export { FieldCapsResponse } from './field_caps_response';
export { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values';
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { merge } from 'lodash';
import { FieldDescriptor } from '../../index_patterns_service';
import { FieldDescriptor } from '../../index_patterns_fetcher';

const OVERRIDES: Record<string, Partial<FieldDescriptor>> = {
_source: { type: '_source' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* under the License.
*/

export { getFieldCapabilities } from './field_capabilities';
// @ts-ignore
export { getFieldCapabilities, shouldReadFieldFromDocValues } from './field_capabilities';
export { resolveTimePattern } from './resolve_time_pattern';
// @ts-ignore
export { createNoMatchingIndicesError } from './errors';
21 changes: 21 additions & 0 deletions src/plugins/data/server/index_patterns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { IndexPatternsFetcher, FieldDescriptor, shouldReadFieldFromDocValues } from './fetcher';
export { IndexPatternsService } from './index_patterns_service';
30 changes: 30 additions & 0 deletions src/plugins/data/server/index_patterns/index_patterns_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CoreSetup } from 'kibana/server';
import { Plugin } from '../../../../core/server';
import { registerRoutes } from './routes';

export class IndexPatternsService implements Plugin<void> {
public setup({ http, elasticsearch }: CoreSetup) {
registerRoutes(http, elasticsearch);
}

public start() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ import {
RequestHandlerContext,
APICaller,
CallAPIOptions,
} from '../../../core/server';
import { IndexPatternsService } from './service';
} from '../../../../core/server';
import { IndexPatternsFetcher } from './fetcher';

export function registerRoutes(core: CoreSetup) {
const getIndexPatternsService = async (request: KibanaRequest): Promise<IndexPatternsService> => {
const client = await core.elasticsearch.dataClient$.pipe(first()).toPromise();
export function registerRoutes(http: CoreSetup['http'], elasticsearch: CoreSetup['elasticsearch']) {
const getIndexPatternsService = async (request: KibanaRequest): Promise<IndexPatternsFetcher> => {
const client = await elasticsearch.dataClient$.pipe(first()).toPromise();
const callCluster: APICaller = (
endpoint: string,
params?: Record<string, any>,
options?: CallAPIOptions
) => client.asScoped(request).callAsCurrentUser(endpoint, params, options);
return new Promise(resolve => resolve(new IndexPatternsService(callCluster)));
return new Promise(resolve => resolve(new IndexPatternsFetcher(callCluster)));
};

const parseMetaFields = (metaFields: string | string[]) => {
Expand All @@ -49,7 +49,7 @@ export function registerRoutes(core: CoreSetup) {
return parsedFields;
};

const router = core.http.createRouter();
const router = http.createRouter();
router.get(
{
path: '/api/index_patterns/_fields_for_wildcard',
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/data/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/server';
import { IndexPatternsService } from './index_patterns';
import { ISearchSetup } from './search';
import { SearchService } from './search/search_service';

export interface DataPluginSetup {
search: ISearchSetup;
}

export class DataServerPlugin implements Plugin<DataPluginSetup> {
private readonly searchService: SearchService;
private readonly indexPatterns = new IndexPatternsService();
constructor(initializerContext: PluginInitializerContext) {
this.searchService = new SearchService(initializerContext);
}
public setup(core: CoreSetup) {
this.indexPatterns.setup(core);
return {
search: this.searchService.setup(core),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
createNoMatchingIndicesError,
isNoMatchingIndicesError,
convertEsError
} from '../../../../../src/legacy/server/index_patterns/service/lib/errors';
} from '../../../../../src/plugins/data/server/index_patterns/fetcher/lib/errors';

import {
getIndexNotFoundError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Legacy } from 'kibana';
import { StaticIndexPattern } from 'ui/index_patterns';
import { APICaller } from 'src/core/server';
import { IndexPatternsService } from '../../../../../../../src/legacy/server/index_patterns/service';
import { IndexPatternsFetcher } from '../../../../../../../src/plugins/data/server';
import { Setup } from '../helpers/setup_request';

export const getKueryBarIndexPattern = async ({
Expand All @@ -21,7 +21,7 @@ export const getKueryBarIndexPattern = async ({
}) => {
const { indices } = setup;

const indexPatternsService = new IndexPatternsService(
const indexPatternsFetcher = new IndexPatternsFetcher(
(...rest: Parameters<APICaller>) =>
request.server.plugins.elasticsearch
.getCluster('data')
Expand All @@ -40,7 +40,7 @@ export const getKueryBarIndexPattern = async ({

const configuredIndices = indexNames.map(name => indicesMap[name]);

const fields = await indexPatternsService.getFieldsForWildcard({
const fields = await indexPatternsFetcher.getFieldsForWildcard({
pattern: configuredIndices
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query';
import { isEmpty } from 'lodash';
import { npStart } from 'ui/new_platform';
import { RestAPIAdapter } from '../rest_api/adapter_types';
import { ElasticsearchAdapter } from './adapter_types';
import { AutocompleteSuggestion } from '../../../../../../../../src/plugins/data/public';
import { setup as data } from '../../../../../../../../src/legacy/core_plugins/data/public/legacy';

const getAutocompleteProvider = (language: string) =>
npStart.plugins.data.autocomplete.getProvider(language);

export class RestElasticsearchAdapter implements ElasticsearchAdapter {
private cachedIndexPattern: any = null;
constructor(private readonly api: RestAPIAdapter, private readonly indexPatternName: string) {}
constructor(private readonly indexPatternName: string) {}

public isKueryValid(kuery: string): boolean {
try {
Expand Down Expand Up @@ -65,9 +65,9 @@ export class RestElasticsearchAdapter implements ElasticsearchAdapter {
if (this.cachedIndexPattern) {
return this.cachedIndexPattern;
}
const res = await this.api.get<any>(
`/api/index_patterns/_fields_for_wildcard?pattern=${this.indexPatternName}`
);
const res = await data.indexPatterns.indexPatterns.getFieldsForWildcard({
pattern: this.indexPatternName,
});
if (isEmpty(res.fields)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const onKibanaReady = chrome.dangerouslyGetActiveInjector;

export function compose(): FrontendLibs {
const api = new AxiosRestAPIAdapter(chrome.getXsrfToken(), chrome.getBasePath());
const esAdapter = new RestElasticsearchAdapter(api, INDEX_NAMES.BEATS);
const esAdapter = new RestElasticsearchAdapter(INDEX_NAMES.BEATS);
const elasticsearchLib = new ElasticsearchLib(esAdapter);
const configBlocks = new ConfigBlocksLib(
new RestConfigBlocksAdapter(api),
Expand Down
Loading

0 comments on commit 19dfe26

Please sign in to comment.