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

Move legacy/server/index_patterns -> src/plugins/data/server #46537

Merged
merged 23 commits into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
149f639
Removed @ts-ignore
sainthkh Sep 24, 2019
6e851ad
Moved files to plugins/data/server.
sainthkh Sep 24, 2019
062ce32
Renamed IndexPatternsService to IndexPatternsFetcher and created new …
sainthkh Sep 24, 2019
6a8444f
Set routerPath.
sainthkh Sep 24, 2019
c683825
Fixed type error.
sainthkh Sep 24, 2019
738860e
Changed beats_management _fields_for_wildcard request with data/publi…
sainthkh Sep 24, 2019
5315f22
Fixed changed paths.
sainthkh Sep 27, 2019
9915148
Merge branch 'master' into move-index-patterns-server-pt2
elasticmachine Sep 30, 2019
4ae2907
Merge branch 'master' into move-index-patterns-server-pt2
sainthkh Oct 4, 2019
29273cf
Merge branch 'master' into move-index-patterns-server-pt2.
sainthkh Oct 14, 2019
a0d596b
Merge branch 'master' into move-index-patterns-server-pt2
elasticmachine Oct 28, 2019
488e9bb
Merge branch 'master' into move-index-patterns-server-pt2
sainthkh Oct 29, 2019
c42fc81
Fixes crashes after merge.
sainthkh Oct 29, 2019
39901db
Updated path for clarity.
sainthkh Oct 29, 2019
0f0f4a5
Applied Plugin interface to service.
sainthkh Oct 29, 2019
b2c6695
Merge remote-tracking branch 'upstream/master' into move-index-patter…
sainthkh Oct 29, 2019
1f477a5
Fixed test failure caused by non camel case local variable name.
sainthkh Oct 29, 2019
e0bd02c
Merge remote-tracking branch 'upstream/master' into move-index-patter…
sainthkh Oct 31, 2019
392e408
Fixed import to IndexPatternsService to IndexPatternsFetcher.
sainthkh Oct 31, 2019
ec0f0f6
Merge remote-tracking branch 'upstream/master' into move-index-patter…
sainthkh Oct 31, 2019
f05a085
Merge remote-tracking branch 'upstream/master' into move-index-patter…
sainthkh Nov 4, 2019
b1ad415
Merge branch 'master' into move-index-patterns-server-pt2
elasticmachine Nov 4, 2019
52b18c2
Merge branch 'master' into move-index-patterns-server-pt2
elasticmachine Nov 5, 2019
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/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);
lizozom marked this conversation as resolved.
Show resolved Hide resolved
}

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';
lizozom marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sainthkh @ppisljar This is not a blocker for this PR, but I suspect _fields_for_time_pattern API is not being used at all and could be deprecated.

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