diff --git a/src/legacy/server/index_patterns/index.d.ts b/src/legacy/server/index_patterns/index.ts similarity index 86% rename from src/legacy/server/index_patterns/index.d.ts rename to src/legacy/server/index_patterns/index.ts index a68bc1c34544a..eea8a4a998e2d 100644 --- a/src/legacy/server/index_patterns/index.d.ts +++ b/src/legacy/server/index_patterns/index.ts @@ -17,5 +17,7 @@ * under the License. */ -export { IndexPatternsService } from './service'; +// @ts-ignore no types +export { indexPatternsMixin } from './mixin'; +export { IndexPatternsService, FieldDescriptor } from './service'; export { IndexPatternsServiceFactory } from './mixin'; diff --git a/src/legacy/server/index_patterns/service/index.d.ts b/src/legacy/server/index_patterns/service/index.d.ts deleted file mode 100644 index 55c8811754197..0000000000000 --- a/src/legacy/server/index_patterns/service/index.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 { IndexPatternsService } from './index_patterns_service'; diff --git a/src/legacy/server/index_patterns/service/index.js b/src/legacy/server/index_patterns/service/index.js deleted file mode 100644 index 55c8811754197..0000000000000 --- a/src/legacy/server/index_patterns/service/index.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 { IndexPatternsService } from './index_patterns_service'; diff --git a/src/legacy/server/index_patterns/index.js b/src/legacy/server/index_patterns/service/index.ts similarity index 94% rename from src/legacy/server/index_patterns/index.js rename to src/legacy/server/index_patterns/service/index.ts index a2c2c1d79e031..496c4ba7b5b6f 100644 --- a/src/legacy/server/index_patterns/index.js +++ b/src/legacy/server/index_patterns/service/index.ts @@ -17,4 +17,4 @@ * under the License. */ -export { indexPatternsMixin } from './mixin'; +export * from './index_patterns_service'; diff --git a/src/legacy/server/index_patterns/service/index_patterns_service.d.ts b/src/legacy/server/index_patterns/service/index_patterns_service.d.ts deleted file mode 100644 index 75746e3c79bdc..0000000000000 --- a/src/legacy/server/index_patterns/service/index_patterns_service.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 interface IndexPatternsService { - // ATTENTION: this interface is incomplete - - getFieldsForWildcard(options: { pattern: string | string[] }): Promise; -} - -export interface FieldDescriptor { - aggregatable: boolean; - name: string; - readFromDocValues: boolean; - searchable: boolean; - type: string; - esTypes: string[]; - parent?: string; - subType?: string; -} diff --git a/src/legacy/server/index_patterns/service/index_patterns_service.js b/src/legacy/server/index_patterns/service/index_patterns_service.ts similarity index 75% rename from src/legacy/server/index_patterns/service/index_patterns_service.js rename to src/legacy/server/index_patterns/service/index_patterns_service.ts index 62b99b26ed7e4..6c32d99ad83f9 100644 --- a/src/legacy/server/index_patterns/service/index_patterns_service.js +++ b/src/legacy/server/index_patterns/service/index_patterns_service.ts @@ -17,27 +17,41 @@ * under the License. */ -import { - getFieldCapabilities, - resolveTimePattern, - createNoMatchingIndicesError, -} from './lib'; +import { APICaller } from 'src/core/server'; + +import { getFieldCapabilities, resolveTimePattern, createNoMatchingIndicesError } from './lib'; + +export interface FieldDescriptor { + aggregatable: boolean; + name: string; + readFromDocValues: boolean; + searchable: boolean; + type: string; + esTypes: string[]; + parent?: string; + subType?: string; +} export class IndexPatternsService { - constructor(callDataCluster) { + private _callDataCluster: APICaller; + + constructor(callDataCluster: APICaller) { this._callDataCluster = callDataCluster; } /** * Get a list of field objects for an index pattern that may contain wildcards * - * @param {Object} [options={}] + * @param {Object} [options] * @property {String} options.pattern The index pattern * @property {Number} options.metaFields The list of underscore prefixed fields that should * be left in the field list (all others are removed). * @return {Promise>} */ - async getFieldsForWildcard(options = {}) { + async getFieldsForWildcard(options: { + pattern: string | string[]; + metaFields?: string[]; + }): Promise { const { pattern, metaFields } = options; return await getFieldCapabilities(this._callDataCluster, pattern, metaFields); } @@ -52,7 +66,12 @@ export class IndexPatternsService { * be left in the field list (all others are removed). * @return {Promise>} */ - async getFieldsForTimePattern(options = {}) { + async getFieldsForTimePattern(options: { + pattern: string; + metaFields: string[]; + lookBack: number; + interval: string; + }) { const { pattern, lookBack, metaFields } = options; const { matches } = await resolveTimePattern(this._callDataCluster, pattern); const indices = matches.slice(0, lookBack); @@ -61,5 +80,4 @@ export class IndexPatternsService { } return await getFieldCapabilities(this._callDataCluster, indices, metaFields); } - } diff --git a/src/legacy/server/index_patterns/service/lib/errors.test.js b/src/legacy/server/index_patterns/service/lib/errors.test.js deleted file mode 100644 index 1abcded9d60b4..0000000000000 --- a/src/legacy/server/index_patterns/service/lib/errors.test.js +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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. - */ - -test('test', () => { - // errors module is tested in test/api_integration/apis/index_patterns/es_errors/errors.js - // so it can get real errors from elasticsearch and the es client to test with -}); diff --git a/src/legacy/server/index_patterns/service/lib/es_api.js b/src/legacy/server/index_patterns/service/lib/es_api.ts similarity index 82% rename from src/legacy/server/index_patterns/service/lib/es_api.js rename to src/legacy/server/index_patterns/service/lib/es_api.ts index 9c698bbfe4329..1a22cf23ec0d8 100644 --- a/src/legacy/server/index_patterns/service/lib/es_api.js +++ b/src/legacy/server/index_patterns/service/lib/es_api.ts @@ -17,7 +17,10 @@ * under the License. */ +import { APICaller } from 'src/core/server'; +// @ts-ignore import { convertEsError } from './errors'; +import { FieldCapsResponse } from './field_capabilities'; /** * Call the index.getAlias API for a list of indices. @@ -33,12 +36,12 @@ import { convertEsError } from './errors'; * @param {Array|String} indices * @return {Promise} */ -export async function callIndexAliasApi(callCluster, indices) { +export async function callIndexAliasApi(callCluster: APICaller, indices: string[] | string) { try { return await callCluster('indices.getAlias', { index: indices, ignoreUnavailable: true, - allowNoIndices: false + allowNoIndices: false, }); } catch (error) { throw convertEsError(indices, error); @@ -56,14 +59,14 @@ export async function callIndexAliasApi(callCluster, indices) { * @param {Array|String} indices * @return {Promise} */ -export async function callFieldCapsApi(callCluster, indices) { +export async function callFieldCapsApi(callCluster: APICaller, indices: string[] | string) { try { - return await callCluster('fieldCaps', { + return (await callCluster('fieldCaps', { index: indices, fields: '*', ignoreUnavailable: true, - allowNoIndices: false - }); + allowNoIndices: false, + })) as FieldCapsResponse; } catch (error) { throw convertEsError(indices, error); } diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.js b/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts similarity index 62% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.js rename to src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts index d409a4ed7d314..05e8d591f13a9 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.js +++ b/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts @@ -19,13 +19,15 @@ import { defaults, indexBy, sortBy } from 'lodash'; +import { APICaller } from 'src/core/server'; import { callFieldCapsApi } from '../es_api'; -import { readFieldCapsResponse } from './field_caps_response'; +import { FieldCapsResponse, readFieldCapsResponse } from './field_caps_response'; import { mergeOverrides } from './overrides'; +import { FieldDescriptor } from '../../index_patterns_service'; -export const concatIfUniq = (arr, value) => ( - arr.includes(value) ? arr : arr.concat(value) -); +export function concatIfUniq(arr: T[], value: T) { + return arr.includes(value) ? arr : arr.concat(value); +} /** * Get the field capabilities for field in `indices`, excluding @@ -34,24 +36,29 @@ export const concatIfUniq = (arr, value) => ( * @param {Function} callCluster bound function for accessing an es client * @param {Array} [indices=[]] the list of indexes to check * @param {Array} [metaFields=[]] the list of internal fields to include - * @return {Promise>} + * @return {Promise>} */ -export async function getFieldCapabilities(callCluster, indices = [], metaFields = []) { - const esFieldCaps = await callFieldCapsApi(callCluster, indices); +export async function getFieldCapabilities( + callCluster: APICaller, + indices: string | string[] = [], + metaFields: string[] = [] +) { + const esFieldCaps: FieldCapsResponse = await callFieldCapsApi(callCluster, indices); const fieldsFromFieldCapsByName = indexBy(readFieldCapsResponse(esFieldCaps), 'name'); - const allFieldsUnsorted = Object - .keys(fieldsFromFieldCapsByName) + const allFieldsUnsorted = Object.keys(fieldsFromFieldCapsByName) .filter(name => !name.startsWith('_')) .concat(metaFields) - .reduce(concatIfUniq, []) - .map(name => defaults({}, fieldsFromFieldCapsByName[name], { - name, - type: 'string', - searchable: false, - aggregatable: false, - readFromDocValues: false - })) + .reduce(concatIfUniq, [] as string[]) + .map(name => + defaults({}, fieldsFromFieldCapsByName[name], { + name, + type: 'string', + searchable: false, + aggregatable: false, + readFromDocValues: false, + }) + ) .map(mergeOverrides); return sortBy(allFieldsUnsorted, 'name'); diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.js b/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts similarity index 78% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.js rename to src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts index 682ab01b48ece..dd03e54d15072 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.js +++ b/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts @@ -20,6 +20,20 @@ import { uniq } from 'lodash'; import { castEsToKbnFieldTypeName } from '../../../../../utils'; import { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values'; +import { FieldDescriptor } from '../..'; + +interface FieldCapObject { + type: string; + searchable: boolean; + aggregatable: boolean; + indices?: string[]; + non_searchable_indices?: string[]; + non_aggregatable_indices?: string[]; +} + +export interface FieldCapsResponse { + fields: Record>; +} /** * Read the response from the _field_caps API to determine the type and @@ -75,26 +89,31 @@ import { shouldReadFieldFromDocValues } from './should_read_field_from_doc_value * } * * @param {FieldCapsResponse} fieldCapsResponse - * @return {Promise>} + * @return {Array} */ -export function readFieldCapsResponse(fieldCapsResponse) { +export function readFieldCapsResponse(fieldCapsResponse: FieldCapsResponse): FieldDescriptor[] { const capsByNameThenType = fieldCapsResponse.fields; - const kibanaFormattedCaps = Object.keys(capsByNameThenType).map(fieldName => { + const kibanaFormattedCaps: FieldDescriptor[] = Object.keys(capsByNameThenType).map(fieldName => { const capsByType = capsByNameThenType[fieldName]; const types = Object.keys(capsByType); // If a single type is marked as searchable or aggregatable, all the types are searchable or aggregatable const isSearchable = types.some(type => { - return !!capsByType[type].searchable || - (!!capsByType[type].non_searchable_indices && capsByType[type].non_searchable_indices.length > 0); + return ( + !!capsByType[type].searchable || + (!!capsByType[type].non_searchable_indices && + capsByType[type].non_searchable_indices!.length > 0) + ); }); const isAggregatable = types.some(type => { - return !!capsByType[type].aggregatable || - (!!capsByType[type].non_aggregatable_indices && capsByType[type].non_aggregatable_indices.length > 0); + return ( + !!capsByType[type].aggregatable || + (!!capsByType[type].non_aggregatable_indices && + capsByType[type].non_aggregatable_indices!.length > 0) + ); }); - // If there are multiple types but they all resolve to the same kibana type // ignore the conflict and carry on (my wayward son) const uniqueKibanaTypes = uniq(types.map(castEsToKbnFieldTypeName)); @@ -106,10 +125,13 @@ export function readFieldCapsResponse(fieldCapsResponse) { searchable: isSearchable, aggregatable: isAggregatable, readFromDocValues: false, - conflictDescriptions: types.reduce((acc, esType) => ({ - ...acc, - [esType]: capsByType[esType].indices - }), {}) + conflictDescriptions: types.reduce( + (acc, esType) => ({ + ...acc, + [esType]: capsByType[esType].indices, + }), + {} + ), }; } @@ -132,7 +154,10 @@ export function readFieldCapsResponse(fieldCapsResponse) { // Discern which sub fields are multi fields. If the parent field is not an object or nested field // the child must be a multi field. subFields.forEach(field => { - const parentFieldName = field.name.split('.').slice(0, -1).join('.'); + const parentFieldName = field.name + .split('.') + .slice(0, -1) + .join('.'); const parentFieldCaps = kibanaFormattedCaps.find(caps => caps.name === parentFieldName); if (parentFieldCaps && !['object', 'nested'].includes(parentFieldCaps.type)) { diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/index.js b/src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts similarity index 93% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/index.js rename to src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts index ffc8e16e645da..8ac4225557784 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/index.js +++ b/src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts @@ -18,3 +18,4 @@ */ export { getFieldCapabilities } from './field_capabilities'; +export { FieldCapsResponse } from './field_caps_response'; diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.js b/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts similarity index 80% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.js rename to src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts index 86c06ef6f3c61..6310bf02e4430 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.js +++ b/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts @@ -18,8 +18,9 @@ */ import { merge } from 'lodash'; +import { FieldDescriptor } from '../../index_patterns_service'; -const OVERRIDES = { +const OVERRIDES: Record> = { _source: { type: '_source' }, _index: { type: 'string' }, _type: { type: 'string' }, @@ -27,22 +28,22 @@ const OVERRIDES = { _timestamp: { type: 'date', searchable: true, - aggregatable: true + aggregatable: true, }, _score: { type: 'number', searchable: false, - aggregatable: false + aggregatable: false, }, }; /** * Merge overrides for specific metaFields * - * @param {FieldInfo} field - * @return {FieldInfo} + * @param {FieldDescriptor} field + * @return {FieldDescriptor} */ -export function mergeOverrides(field) { +export function mergeOverrides(field: FieldDescriptor): FieldDescriptor { if (OVERRIDES.hasOwnProperty(field.name)) { return merge(field, OVERRIDES[field.name]); } else { diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.js b/src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.ts similarity index 91% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.js rename to src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.ts index 635f616d00527..6d58f7a02c134 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.js +++ b/src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.ts @@ -17,6 +17,6 @@ * under the License. */ -export function shouldReadFieldFromDocValues(aggregatable, esType) { +export function shouldReadFieldFromDocValues(aggregatable: boolean, esType: string) { return aggregatable && esType !== 'text' && !esType.startsWith('_'); } diff --git a/src/legacy/server/index_patterns/service/lib/index.js b/src/legacy/server/index_patterns/service/lib/index.ts similarity index 97% rename from src/legacy/server/index_patterns/service/lib/index.js rename to src/legacy/server/index_patterns/service/lib/index.ts index 5fe3c93763c2d..cd239b3753aa3 100644 --- a/src/legacy/server/index_patterns/service/lib/index.js +++ b/src/legacy/server/index_patterns/service/lib/index.ts @@ -18,5 +18,7 @@ */ export { getFieldCapabilities } from './field_capabilities'; +// @ts-ignore export { resolveTimePattern } from './resolve_time_pattern'; +// @ts-ignore export { createNoMatchingIndicesError } from './errors'; diff --git a/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.js b/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.js rename to src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.ts diff --git a/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.js b/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.ts similarity index 96% rename from src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.js rename to src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.ts index 7db1cbae267b1..27872cb84810a 100644 --- a/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.js +++ b/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.ts @@ -25,7 +25,7 @@ * @param {String} timePattern * @return {String} */ -export function timePatternToWildcard(timePattern) { +export function timePatternToWildcard(timePattern: string) { let wildcard = ''; let inEscape = false; let inPattern = false; diff --git a/src/legacy/utils/index.d.ts b/src/legacy/utils/index.d.ts index a7317f59637ed..5319f842d76c9 100644 --- a/src/legacy/utils/index.d.ts +++ b/src/legacy/utils/index.d.ts @@ -20,3 +20,5 @@ export function parseCommaSeparatedList(input: string | string[]): string[]; export function formatListAsProse(list: string[], options?: { inclusive?: boolean }): string; + +export function castEsToKbnFieldTypeName(name: string): string;