diff --git a/.ci/jobs.yml b/.ci/jobs.yml index 89fce3cf488d59..a2d8100f78efd0 100644 --- a/.ci/jobs.yml +++ b/.ci/jobs.yml @@ -14,6 +14,7 @@ JOB: - kibana-ciGroup10 - kibana-ciGroup11 - kibana-ciGroup12 + - kibana-accessibility - kibana-visualRegression # make sure all x-pack-ciGroups are listed in test/scripts/jenkins_xpack_ci_group.sh @@ -28,6 +29,7 @@ JOB: - x-pack-ciGroup8 - x-pack-ciGroup9 - x-pack-ciGroup10 + - x-pack-accessibility - x-pack-visualRegression # `~` is yaml for `null` diff --git a/.ci/run.sh b/.ci/run.sh index 88ce0bd9986a19..9f77438be62d0f 100755 --- a/.ci/run.sh +++ b/.ci/run.sh @@ -21,6 +21,9 @@ kibana-ciGroup*) kibana-visualRegression*) ./test/scripts/jenkins_visual_regression.sh ;; +kibana-accessibility*) + ./test/scripts/jenkins_accessibility.sh + ;; kibana-firefoxSmoke*) ./test/scripts/jenkins_firefox_smoke.sh ;; @@ -34,6 +37,9 @@ x-pack-ciGroup*) x-pack-visualRegression*) ./test/scripts/jenkins_xpack_visual_regression.sh ;; +x-pack-accessibility*) + ./test/scripts/jenkins_xpack_accessibility.sh + ;; x-pack-firefoxSmoke*) ./test/scripts/jenkins_xpack_firefox_smoke.sh ;; diff --git a/.eslintrc.js b/.eslintrc.js index 310ee47819e30a..16a80f01278a54 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -400,6 +400,7 @@ module.exports = { 'x-pack/test/functional/apps/**/*.js', 'x-pack/legacy/plugins/apm/**/*.js', 'test/*/config.ts', + 'test/*/{tests,test_suites,apis,apps}/**/*', 'test/visual_regression/tests/**/*', 'x-pack/test/*/{tests,test_suites,apis,apps}/**/*', 'x-pack/test/*/*config.*ts', diff --git a/Jenkinsfile b/Jenkinsfile index 12600788ccbcb2..8d8579736f6392 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -25,6 +25,7 @@ stage("Kibana Pipeline") { // This stage is just here to help the BlueOcean UI a 'oss-ciGroup11': kibanaPipeline.getOssCiGroupWorker(11), 'oss-ciGroup12': kibanaPipeline.getOssCiGroupWorker(12), 'oss-firefoxSmoke': kibanaPipeline.getPostBuildWorker('firefoxSmoke', { runbld './test/scripts/jenkins_firefox_smoke.sh' }), + 'oss-accessibility': kibanaPipeline.getPostBuildWorker('accessibility', { runbld './test/scripts/jenkins_accessibility.sh' }), 'oss-visualRegression': kibanaPipeline.getPostBuildWorker('visualRegression', { runbld './test/scripts/jenkins_visual_regression.sh' }), ]), 'kibana-xpack-agent': kibanaPipeline.withWorkers('kibana-xpack-tests', { kibanaPipeline.buildXpack() }, [ @@ -39,6 +40,7 @@ stage("Kibana Pipeline") { // This stage is just here to help the BlueOcean UI a 'xpack-ciGroup9': kibanaPipeline.getXpackCiGroupWorker(9), 'xpack-ciGroup10': kibanaPipeline.getXpackCiGroupWorker(10), 'xpack-firefoxSmoke': kibanaPipeline.getPostBuildWorker('xpack-firefoxSmoke', { runbld './test/scripts/jenkins_xpack_firefox_smoke.sh' }), + 'xpack-accessibility': kibanaPipeline.getPostBuildWorker('xpack-accessibility', { runbld './test/scripts/jenkins_xpack_accessibility.sh' }), 'xpack-visualRegression': kibanaPipeline.getPostBuildWorker('xpack-visualRegression', { runbld './test/scripts/jenkins_xpack_visual_regression.sh' }), ]), ]) @@ -47,4 +49,4 @@ stage("Kibana Pipeline") { // This stage is just here to help the BlueOcean UI a } } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 2c772c1fe67203..a8e60e9749f724 100644 --- a/package.json +++ b/package.json @@ -350,6 +350,7 @@ "@typescript-eslint/parser": "^2.5.0", "angular-mocks": "^1.7.8", "archiver": "^3.1.1", + "axe-core": "^3.3.2", "babel-eslint": "^10.0.3", "babel-jest": "^24.9.0", "babel-plugin-dynamic-import-node": "^2.3.0", @@ -359,7 +360,7 @@ "chance": "1.0.18", "cheerio": "0.22.0", "chokidar": "3.2.1", - "chromedriver": "^77.0.0", + "chromedriver": "^78.0.1", "classnames": "2.2.6", "dedent": "^0.7.0", "delete-empty": "^2.0.0", diff --git a/packages/kbn-babel-preset/node_preset.js b/packages/kbn-babel-preset/node_preset.js index 5ef4219df59f75..793044e3796ea0 100644 --- a/packages/kbn-babel-preset/node_preset.js +++ b/packages/kbn-babel-preset/node_preset.js @@ -18,6 +18,25 @@ */ module.exports = (_, options = {}) => { + const overrides = []; + if (!process.env.ALLOW_PERFORMANCE_HOOKS_IN_TASK_MANAGER) { + overrides.push( + { + test: [/x-pack[\/\\]legacy[\/\\]plugins[\/\\]task_manager/], + plugins: [ + [ + require.resolve('babel-plugin-filter-imports'), + { + imports: { + perf_hooks: ['performance'], + }, + }, + ], + ], + } + ); + } + return { presets: [ [ @@ -39,7 +58,7 @@ module.exports = (_, options = {}) => { modules: 'cjs', corejs: 3, - ...(options['@babel/preset-env'] || {}) + ...(options['@babel/preset-env'] || {}), }, ], require('./common_preset'), @@ -48,9 +67,10 @@ module.exports = (_, options = {}) => { [ require.resolve('babel-plugin-transform-define'), { - 'global.__BUILT_WITH_BABEL__': 'true' - } - ] - ] + 'global.__BUILT_WITH_BABEL__': 'true', + }, + ], + ], + overrides, }; }; diff --git a/packages/kbn-babel-preset/package.json b/packages/kbn-babel-preset/package.json index 4b183577453606..c22cf175b29e59 100644 --- a/packages/kbn-babel-preset/package.json +++ b/packages/kbn-babel-preset/package.json @@ -8,10 +8,11 @@ "@babel/plugin-syntax-dynamic-import": "^7.2.0", "@babel/plugin-transform-modules-commonjs": "^7.5.0", "@babel/preset-env": "^7.5.5", - "@babel/preset-react":"^7.0.0", + "@babel/preset-react": "^7.0.0", "@babel/preset-typescript": "^7.3.3", "@kbn/elastic-idx": "1.0.0", "babel-plugin-add-module-exports": "^1.0.2", + "babel-plugin-filter-imports": "^3.0.0", "babel-plugin-transform-define": "^1.3.1", "babel-plugin-typescript-strip-namespaces": "^1.1.1" } diff --git a/packages/kbn-es-query/src/es_query/migrate_filter.js b/packages/kbn-es-query/src/es_query/migrate_filter.js index d5f52648b027e2..b74fc485a6184f 100644 --- a/packages/kbn-es-query/src/es_query/migrate_filter.js +++ b/packages/kbn-es-query/src/es_query/migrate_filter.js @@ -18,7 +18,7 @@ */ import _ from 'lodash'; -import { getConvertedValueForField } from '../filters'; +import { getConvertedValueForField } from '../utils/filters'; export function migrateFilter(filter, indexPattern) { if (filter.match) { diff --git a/packages/kbn-es-query/src/filters/__tests__/phrase.js b/packages/kbn-es-query/src/filters/__tests__/phrase.js deleted file mode 100644 index dbd794a018d9ea..00000000000000 --- a/packages/kbn-es-query/src/filters/__tests__/phrase.js +++ /dev/null @@ -1,92 +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. - */ - - -import { buildInlineScriptForPhraseFilter, buildPhraseFilter } from '../phrase'; -import expect from '@kbn/expect'; -import _ from 'lodash'; -import indexPattern from '../../__fixtures__/index_pattern_response.json'; -import filterSkeleton from '../../__fixtures__/filter_skeleton'; - -let expected; - -describe('Filter Manager', function () { - describe('Phrase filter builder', function () { - beforeEach(() => { - expected = _.cloneDeep(filterSkeleton); - }); - - it('should be a function', function () { - expect(buildPhraseFilter).to.be.a(Function); - }); - - it('should return a match query filter when passed a standard field', function () { - const field = getField(indexPattern, 'bytes'); - expected.query = { - match_phrase: { - bytes: 5 - } - }; - expect(buildPhraseFilter(field, 5, indexPattern)).to.eql(expected); - }); - - it('should return a script filter when passed a scripted field', function () { - const field = getField(indexPattern, 'script number'); - expected.meta.field = 'script number'; - _.set(expected, 'script.script', { - source: '(' + field.script + ') == value', - lang: 'expression', - params: { - value: 5, - } - }); - expect(buildPhraseFilter(field, 5, indexPattern)).to.eql(expected); - }); - }); - - describe('buildInlineScriptForPhraseFilter', function () { - - it('should wrap painless scripts in a lambda', function () { - const field = { - lang: 'painless', - script: 'return foo;', - }; - - const expected = `boolean compare(Supplier s, def v) {return s.get() == v;}` + - `compare(() -> { return foo; }, params.value);`; - - expect(buildInlineScriptForPhraseFilter(field)).to.be(expected); - }); - - it('should create a simple comparison for other langs', function () { - const field = { - lang: 'expression', - script: 'doc[bytes].value', - }; - - const expected = `(doc[bytes].value) == value`; - - expect(buildInlineScriptForPhraseFilter(field)).to.be(expected); - }); - }); -}); - -function getField(indexPattern, name) { - return indexPattern.fields.find(field => field.name === name); -} diff --git a/packages/kbn-es-query/src/filters/__tests__/query.js b/packages/kbn-es-query/src/filters/__tests__/query.js deleted file mode 100644 index 8b774f05c29d38..00000000000000 --- a/packages/kbn-es-query/src/filters/__tests__/query.js +++ /dev/null @@ -1,46 +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. - */ - -import { buildQueryFilter } from '../query'; -import { cloneDeep } from 'lodash'; -import expect from '@kbn/expect'; -import indexPattern from '../../__fixtures__/index_pattern_response.json'; -import filterSkeleton from '../../__fixtures__/filter_skeleton'; - -let expected; - -describe('Filter Manager', function () { - describe('Phrase filter builder', function () { - beforeEach(() => { - expected = cloneDeep(filterSkeleton); - }); - - it('should be a function', function () { - expect(buildQueryFilter).to.be.a(Function); - }); - - it('should return a query filter when passed a standard field', function () { - expected.query = { - foo: 'bar' - }; - expect(buildQueryFilter({ foo: 'bar' }, indexPattern.id)).to.eql(expected); - }); - - }); -}); diff --git a/packages/kbn-es-query/src/filters/__tests__/range.js b/packages/kbn-es-query/src/filters/__tests__/range.js deleted file mode 100644 index 9b23fc23908d4d..00000000000000 --- a/packages/kbn-es-query/src/filters/__tests__/range.js +++ /dev/null @@ -1,156 +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. - */ - -import { buildRangeFilter } from '../range'; -import expect from '@kbn/expect'; -import _ from 'lodash'; -import indexPattern from '../../__fixtures__/index_pattern_response.json'; -import filterSkeleton from '../../__fixtures__/filter_skeleton'; - -let expected; - -describe('Filter Manager', function () { - describe('Range filter builder', function () { - beforeEach(() => { - expected = _.cloneDeep(filterSkeleton); - }); - - it('should be a function', function () { - expect(buildRangeFilter).to.be.a(Function); - }); - - it('should return a range filter when passed a standard field', function () { - const field = getField(indexPattern, 'bytes'); - expected.range = { - bytes: { - gte: 1, - lte: 3 - } - }; - expect(buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern)).to.eql(expected); - }); - - it('should return a script filter when passed a scripted field', function () { - const field = getField(indexPattern, 'script number'); - expected.meta.field = 'script number'; - _.set(expected, 'script.script', { - lang: 'expression', - source: '(' + field.script + ')>=gte && (' + field.script + ')<=lte', - params: { - value: '>=1 <=3', - gte: 1, - lte: 3 - } - }); - expect(buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern)).to.eql(expected); - }); - - it('should wrap painless scripts in comparator lambdas', function () { - const field = getField(indexPattern, 'script date'); - const expected = `boolean gte(Supplier s, def v) {return !s.get().toInstant().isBefore(Instant.parse(v))} ` + - `boolean lte(Supplier s, def v) {return !s.get().toInstant().isAfter(Instant.parse(v))}` + - `gte(() -> { ${field.script} }, params.gte) && ` + - `lte(() -> { ${field.script} }, params.lte)`; - - const inlineScript = buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern).script.script.source; - expect(inlineScript).to.be(expected); - }); - - it('should throw an error when gte and gt, or lte and lt are both passed', function () { - const field = getField(indexPattern, 'script number'); - expect(function () { - buildRangeFilter(field, { gte: 1, gt: 3 }, indexPattern); - }).to.throwError(); - expect(function () { - buildRangeFilter(field, { lte: 1, lt: 3 }, indexPattern); - }).to.throwError(); - }); - - it('to use the right operator for each of gte, gt, lt and lte', function () { - const field = getField(indexPattern, 'script number'); - _.each({ gte: '>=', gt: '>', lte: '<=', lt: '<' }, function (operator, key) { - const params = {}; - params[key] = 5; - const filter = buildRangeFilter(field, params, indexPattern); - - expect(filter.script.script.source).to.be( - '(' + field.script + ')' + operator + key); - expect(filter.script.script.params[key]).to.be(5); - expect(filter.script.script.params.value).to.be(operator + 5); - - }); - }); - - describe('when given params where one side is infinite', function () { - const field = getField(indexPattern, 'script number'); - let filter; - beforeEach(function () { - filter = buildRangeFilter(field, { gte: 0, lt: Infinity }, indexPattern); - }); - - describe('returned filter', function () { - it('is a script filter', function () { - expect(filter).to.have.property('script'); - }); - - it('contain a param for the finite side', function () { - expect(filter.script.script.params).to.have.property('gte', 0); - }); - - it('does not contain a param for the infinite side', function () { - expect(filter.script.script.params).not.to.have.property('lt'); - }); - - it('does not contain a script condition for the infinite side', function () { - const field = getField(indexPattern, 'script number'); - const script = field.script; - expect(filter.script.script.source).to.equal(`(${script})>=gte`); - }); - }); - }); - - describe('when given params where both sides are infinite', function () { - const field = getField(indexPattern, 'script number'); - let filter; - beforeEach(function () { - filter = buildRangeFilter( - field, { gte: -Infinity, lt: Infinity }, indexPattern); - }); - - describe('returned filter', function () { - it('is a match_all filter', function () { - expect(filter).not.to.have.property('script'); - expect(filter).to.have.property('match_all'); - }); - - it('does not contain params', function () { - expect(filter).not.to.have.property('params'); - }); - - it('meta field is set to field name', function () { - expect(filter.meta.field).to.equal('script number'); - }); - }); - }); - }); -}); - -function getField(indexPattern, name) { - return indexPattern.fields.find(field => field.name === name); -} diff --git a/packages/kbn-es-query/src/filters/index.d.ts b/packages/kbn-es-query/src/filters/index.d.ts deleted file mode 100644 index c05e32dbf07b95..00000000000000 --- a/packages/kbn-es-query/src/filters/index.d.ts +++ /dev/null @@ -1,51 +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. - */ - -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( - field: Field, - value: string, - indexPattern: IndexPattern -): PhraseFilter; - -export function buildPhrasesFilter( - field: Field, - values: string[], - indexPattern: IndexPattern -): PhrasesFilter; - -export function buildQueryFilter(query: any, index: string, alias?: string): CustomFilter; - -export function buildRangeFilter( - field: Field, - params: RangeFilterParams, - indexPattern: IndexPattern, - formattedValue?: string -): RangeFilter; diff --git a/packages/kbn-es-query/src/filters/lib/index.ts b/packages/kbn-es-query/src/filters/lib/index.ts deleted file mode 100644 index ea023987103414..00000000000000 --- a/packages/kbn-es-query/src/filters/lib/index.ts +++ /dev/null @@ -1,95 +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. - */ - -// The interface the other filters extend -export * from './meta_filter'; - -// The actual filter types -import { CustomFilter } from './custom_filter'; -import { ExistsFilter, isExistsFilter } from './exists_filter'; -import { GeoBoundingBoxFilter, isGeoBoundingBoxFilter } from './geo_bounding_box_filter'; -import { GeoPolygonFilter, isGeoPolygonFilter } from './geo_polygon_filter'; -import { - PhraseFilter, - isPhraseFilter, - isScriptedPhraseFilter, - getPhraseFilterField, - getPhraseFilterValue, -} from './phrase_filter'; -import { PhrasesFilter, isPhrasesFilter } from './phrases_filter'; -import { QueryStringFilter, isQueryStringFilter } from './query_string_filter'; -import { - RangeFilter, - isRangeFilter, - isScriptedRangeFilter, - RangeFilterParams, -} from './range_filter'; -import { MatchAllFilter, isMatchAllFilter } from './match_all_filter'; -import { MissingFilter, isMissingFilter } from './missing_filter'; - -export { - CustomFilter, - ExistsFilter, - isExistsFilter, - GeoBoundingBoxFilter, - isGeoBoundingBoxFilter, - GeoPolygonFilter, - isGeoPolygonFilter, - PhraseFilter, - isPhraseFilter, - isScriptedPhraseFilter, - getPhraseFilterField, - getPhraseFilterValue, - PhrasesFilter, - isPhrasesFilter, - QueryStringFilter, - isQueryStringFilter, - RangeFilter, - isRangeFilter, - isScriptedRangeFilter, - RangeFilterParams, - MatchAllFilter, - isMatchAllFilter, - MissingFilter, - isMissingFilter, -}; - -// Any filter associated with a field (used in the filter bar/editor) -export type FieldFilter = - | ExistsFilter - | GeoBoundingBoxFilter - | GeoPolygonFilter - | PhraseFilter - | PhrasesFilter - | RangeFilter - | MatchAllFilter - | MissingFilter; - -export enum FILTERS { - CUSTOM = 'custom', - PHRASES = 'phrases', - PHRASE = 'phrase', - EXISTS = 'exists', - MATCH_ALL = 'match_all', - MISSING = 'missing', - QUERY_STRING = 'query_string', - RANGE = 'range', - GEO_BOUNDING_BOX = 'geo_bounding_box', - GEO_POLYGON = 'geo_polygon', -} diff --git a/packages/kbn-es-query/src/filters/lib/phrase_filter.ts b/packages/kbn-es-query/src/filters/lib/phrase_filter.ts deleted file mode 100644 index 124a5da3724871..00000000000000 --- a/packages/kbn-es-query/src/filters/lib/phrase_filter.ts +++ /dev/null @@ -1,65 +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. - */ - -import { get, isPlainObject } from 'lodash'; -import { Filter, FilterMeta } from './meta_filter'; - -export type PhraseFilterMeta = FilterMeta & { - params: { - query: string; // The unformatted value - }; - script?: { - script: { - params: any; - }; - }; - field?: any; -}; - -export type PhraseFilter = Filter & { - meta: PhraseFilterMeta; -}; - -type PhraseFilterValue = string | number | boolean; - -export const isPhraseFilter = (filter: any): filter is PhraseFilter => { - const isMatchPhraseQuery = filter && filter.query && filter.query.match_phrase; - - const isDeprecatedMatchPhraseQuery = - filter && - filter.query && - filter.query.match && - Object.values(filter.query.match).find((params: any) => params.type === 'phrase'); - - return !!(isMatchPhraseQuery || isDeprecatedMatchPhraseQuery); -}; - -export const isScriptedPhraseFilter = (filter: any): filter is PhraseFilter => - Boolean(get(filter, 'script.script.params.value')); - -export const getPhraseFilterField = (filter: PhraseFilter) => { - const queryConfig = filter.query.match_phrase || filter.query.match; - return Object.keys(queryConfig)[0]; -}; - -export const getPhraseFilterValue = (filter: PhraseFilter): PhraseFilterValue => { - const queryConfig = filter.query.match_phrase || filter.query.match; - const queryValue = Object.values(queryConfig)[0] as any; - return isPlainObject(queryValue) ? queryValue.query : queryValue; -}; diff --git a/packages/kbn-es-query/src/filters/lib/range_filter.ts b/packages/kbn-es-query/src/filters/lib/range_filter.ts deleted file mode 100644 index fc8d05d575d597..00000000000000 --- a/packages/kbn-es-query/src/filters/lib/range_filter.ts +++ /dev/null @@ -1,58 +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. - */ -import { get, keys } from 'lodash'; -import { Filter, FilterMeta } from './meta_filter'; - -export interface RangeFilterParams { - from?: number | string; - to?: number | string; - gt?: number | string; - lt?: number | string; - gte?: number | string; - lte?: number | string; - format?: string; -} - -export type RangeFilterMeta = FilterMeta & { - params: RangeFilterParams; - field?: any; -}; - -export type RangeFilter = Filter & { - meta: RangeFilterMeta; - script?: { - script: { - params: any; - }; - }; - range: { [key: string]: RangeFilterParams }; -}; - -const hasRangeKeys = (params: RangeFilterParams) => - Boolean( - keys(params).find((key: string) => ['gte', 'gt', 'lte', 'lt', 'from', 'to'].includes(key)) - ); - -export const isRangeFilter = (filter: any): filter is RangeFilter => filter && filter.range; - -export const isScriptedRangeFilter = (filter: any): filter is RangeFilter => { - const params: RangeFilterParams = get(filter, 'script.script.params', {}); - - return hasRangeKeys(params); -}; diff --git a/packages/kbn-es-query/src/filters/phrase.js b/packages/kbn-es-query/src/filters/phrase.js deleted file mode 100644 index f0134f289ad9d2..00000000000000 --- a/packages/kbn-es-query/src/filters/phrase.js +++ /dev/null @@ -1,85 +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. - */ - -// Creates an filter where the given field matches the given value -export function buildPhraseFilter(field, value, indexPattern) { - const filter = { meta: { index: indexPattern.id } }; - const convertedValue = getConvertedValueForField(field, value); - - if (field.scripted) { - filter.script = getPhraseScript(field, value); - filter.meta.field = field.name; - } else { - filter.query = { match_phrase: {} }; - filter.query.match_phrase[field.name] = convertedValue; - } - return filter; -} - -export function getPhraseScript(field, value) { - const convertedValue = getConvertedValueForField(field, value); - const script = buildInlineScriptForPhraseFilter(field); - - return { - script: { - source: script, - lang: field.lang, - params: { - value: convertedValue - } - } - }; -} - -// See https://github.com/elastic/elasticsearch/issues/20941 and https://github.com/elastic/kibana/issues/8677 -// and https://github.com/elastic/elasticsearch/pull/22201 -// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0. -export function getConvertedValueForField(field, value) { - if (typeof value !== 'boolean' && field.type === 'boolean') { - if ([1, 'true'].includes(value)) { - return true; - } - else if ([0, 'false'].includes(value)) { - return false; - } - else { - throw new Error(`${value} is not a valid boolean value for boolean field ${field.name}`); - } - } - return value; -} - -/** - * Takes a scripted field and returns an inline script appropriate for use in a script query. - * Handles lucene expression and Painless scripts. Other langs aren't guaranteed to generate valid - * scripts. - * - * @param {object} scriptedField A Field object representing a scripted field - * @returns {string} The inline script string - */ -export function buildInlineScriptForPhraseFilter(scriptedField) { - // We must wrap painless scripts in a lambda in case they're more than a simple expression - if (scriptedField.lang === 'painless') { - return `boolean compare(Supplier s, def v) {return s.get() == v;}` + - `compare(() -> { ${scriptedField.script} }, params.value);`; - } - else { - return `(${scriptedField.script}) == value`; - } -} diff --git a/packages/kbn-es-query/src/index.d.ts b/packages/kbn-es-query/src/index.d.ts index ca4455da33f45d..c06cef6367fe78 100644 --- a/packages/kbn-es-query/src/index.d.ts +++ b/packages/kbn-es-query/src/index.d.ts @@ -19,4 +19,3 @@ export * from './es_query'; export * from './kuery'; -export * from './filters'; diff --git a/packages/kbn-es-query/src/index.js b/packages/kbn-es-query/src/index.js index 086b2f6db8d0dc..963999bd0999b2 100644 --- a/packages/kbn-es-query/src/index.js +++ b/packages/kbn-es-query/src/index.js @@ -18,5 +18,4 @@ */ export * from './kuery'; -export * from './filters'; export * from './es_query'; diff --git a/packages/kbn-es-query/src/kuery/functions/is.js b/packages/kbn-es-query/src/kuery/functions/is.js index 33ae2112e3c0cb..63ade9e8793a7b 100644 --- a/packages/kbn-es-query/src/kuery/functions/is.js +++ b/packages/kbn-es-query/src/kuery/functions/is.js @@ -21,7 +21,7 @@ import _ from 'lodash'; import * as ast from '../ast'; import * as literal from '../node_types/literal'; import * as wildcard from '../node_types/wildcard'; -import { getPhraseScript } from '../../filters'; +import { getPhraseScript } from '../../utils/filters'; import { getFields } from './utils/get_fields'; import { getTimeZoneFromSettings } from '../../utils/get_time_zone_from_settings'; import { getFullFieldNameNode } from './utils/get_full_field_name_node'; diff --git a/packages/kbn-es-query/src/kuery/functions/range.js b/packages/kbn-es-query/src/kuery/functions/range.js index 80181cfc003f1c..f7719998ad5240 100644 --- a/packages/kbn-es-query/src/kuery/functions/range.js +++ b/packages/kbn-es-query/src/kuery/functions/range.js @@ -20,7 +20,7 @@ import _ from 'lodash'; import { nodeTypes } from '../node_types'; import * as ast from '../ast'; -import { getRangeScript } from '../../filters'; +import { getRangeScript } from '../../utils/filters'; import { getFields } from './utils/get_fields'; import { getTimeZoneFromSettings } from '../../utils/get_time_zone_from_settings'; import { getFullFieldNameNode } from './utils/get_full_field_name_node'; diff --git a/packages/kbn-es-query/src/utils/filters.js b/packages/kbn-es-query/src/utils/filters.js new file mode 100644 index 00000000000000..6e4f5c342688ce --- /dev/null +++ b/packages/kbn-es-query/src/utils/filters.js @@ -0,0 +1,133 @@ +/* + * 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 { pick, get, reduce, map } from 'lodash'; + +/** @deprecated + * @see src/plugins/data/public/es_query/filters/phrase_filter.ts + * Code was already moved into src/plugins/data/public. + * This method will be removed after moving 'es_query' into new platform + * */ +export const getConvertedValueForField = (field, value) => { + if (typeof value !== 'boolean' && field.type === 'boolean') { + if ([1, 'true'].includes(value)) { + return true; + } else if ([0, 'false'].includes(value)) { + return false; + } else { + throw new Error(`${value} is not a valid boolean value for boolean field ${field.name}`); + } + } + return value; +}; + +/** @deprecated + * @see src/plugins/data/public/es_query/filters/phrase_filter.ts + * Code was already moved into src/plugins/data/public. + * This method will be removed after moving 'es_query' into new platform + * */ +export const buildInlineScriptForPhraseFilter = (scriptedField) => { + // We must wrap painless scripts in a lambda in case they're more than a simple expression + if (scriptedField.lang === 'painless') { + return ( + `boolean compare(Supplier s, def v) {return s.get() == v;}` + + `compare(() -> { ${scriptedField.script} }, params.value);` + ); + } else { + return `(${scriptedField.script}) == value`; + } +}; + +/** @deprecated + * @see src/plugins/data/public/es_query/filters/phrase_filter.ts + * Code was already moved into src/plugins/data/public. + * This method will be removed after moving 'es_query' into new platform + * */ +export function getPhraseScript(field, value) { + const convertedValue = getConvertedValueForField(field, value); + const script = buildInlineScriptForPhraseFilter(field); + + return { + script: { + source: script, + lang: field.lang, + params: { + value: convertedValue, + }, + }, + }; +} + +/** @deprecated + * @see src/plugins/data/public/es_query/filters/range_filter.ts + * Code was already moved into src/plugins/data/public. + * This method will be removed after moving 'kuery' into new platform + * */ +export function getRangeScript(field, params) { + const operators = { + gt: '>', + gte: '>=', + lte: '<=', + lt: '<', + }; + const comparators = { + gt: 'boolean gt(Supplier s, def v) {return s.get() > v}', + gte: 'boolean gte(Supplier s, def v) {return s.get() >= v}', + lte: 'boolean lte(Supplier s, def v) {return s.get() <= v}', + lt: 'boolean lt(Supplier s, def v) {return s.get() < v}', + }; + + const dateComparators = { + gt: 'boolean gt(Supplier s, def v) {return s.get().toInstant().isAfter(Instant.parse(v))}', + gte: 'boolean gte(Supplier s, def v) {return !s.get().toInstant().isBefore(Instant.parse(v))}', + lte: 'boolean lte(Supplier s, def v) {return !s.get().toInstant().isAfter(Instant.parse(v))}', + lt: 'boolean lt(Supplier s, def v) {return s.get().toInstant().isBefore(Instant.parse(v))}', + }; + + const knownParams = pick(params, (val, key) => { + return key in operators; + }); + let script = map(knownParams, (val, key) => { + return '(' + field.script + ')' + get(operators, key) + key; + }).join(' && '); + + // We must wrap painless scripts in a lambda in case they're more than a simple expression + if (field.lang === 'painless') { + const comp = field.type === 'date' ? dateComparators : comparators; + const currentComparators = reduce( + knownParams, + (acc, val, key) => acc.concat(get(comp, key)), + [] + ).join(' '); + + const comparisons = map(knownParams, (val, key) => { + return `${key}(() -> { ${field.script} }, params.${key})`; + }).join(' && '); + + script = `${currentComparators}${comparisons}`; + } + + return { + script: { + source: script, + params: knownParams, + lang: field.lang, + }, + }; +} diff --git a/src/core/server/http/http_server.test.ts b/src/core/server/http/http_server.test.ts index f61371c5437e6b..acae9d8ff0e704 100644 --- a/src/core/server/http/http_server.test.ts +++ b/src/core/server/http/http_server.test.ts @@ -577,45 +577,6 @@ test('exposes route details of incoming request to a route handler', async () => }); }); -describe('conditional compression', () => { - test('disables compression when there is a referer', async () => { - const { registerRouter, server: innerServer } = await server.setup(config); - - const router = new Router('', logger, enhanceWithContext); - router.get({ path: '/', validate: false }, (context, req, res) => - // we need the large body here so that compression would normally be used - res.ok({ body: 'hello'.repeat(500), headers: { 'Content-Type': 'text/html; charset=UTF-8' } }) - ); - registerRouter(router); - - await server.start(); - const response = await supertest(innerServer.listener) - .get('/') - .set('accept-encoding', 'gzip') - .set('referer', 'http://some-other-site/'); - - expect(response.header).not.toHaveProperty('content-encoding'); - }); - - test(`enables compression when there isn't a referer`, async () => { - const { registerRouter, server: innerServer } = await server.setup(config); - - const router = new Router('', logger, enhanceWithContext); - router.get({ path: '/', validate: false }, (context, req, res) => - // we need the large body here so that compression will be used - res.ok({ body: 'hello'.repeat(500), headers: { 'Content-Type': 'text/html; charset=UTF-8' } }) - ); - registerRouter(router); - - await server.start(); - const response = await supertest(innerServer.listener) - .get('/') - .set('accept-encoding', 'gzip'); - - expect(response.header).toHaveProperty('content-encoding', 'gzip'); - }); -}); - describe('setup contract', () => { describe('#createSessionStorage', () => { it('creates session storage factory', async () => { diff --git a/src/core/server/http/http_server.ts b/src/core/server/http/http_server.ts index d6077200d3c757..3354324c12407d 100644 --- a/src/core/server/http/http_server.ts +++ b/src/core/server/http/http_server.ts @@ -96,7 +96,6 @@ export class HttpServer { const basePathService = new BasePath(config.basePath); this.setupBasePathRewrite(config, basePathService); - this.setupConditionalCompression(); return { registerRouter: this.registerRouter.bind(this), @@ -176,23 +175,6 @@ export class HttpServer { }); } - private setupConditionalCompression() { - if (this.server === undefined) { - throw new Error('Server is not created yet'); - } - - this.server.ext('onRequest', (request, h) => { - // whenever there is a referrer, don't use compression even if the client supports it - if (request.info.referrer !== '') { - this.log.debug( - `Not using compression because there is a referer: ${request.info.referrer}` - ); - request.info.acceptEncoding = ''; - } - return h.continue; - }); - } - private registerOnPostAuth(fn: OnPostAuthHandler) { if (this.server === undefined) { throw new Error('Server is not created yet'); diff --git a/src/fixtures/logstash_fields.js b/src/fixtures/logstash_fields.js index 0ae6c62def0f7c..ab96b69851b713 100644 --- a/src/fixtures/logstash_fields.js +++ b/src/fixtures/logstash_fields.js @@ -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 [ diff --git a/src/legacy/core_plugins/data/public/filter/action/apply_filter_action.ts b/src/legacy/core_plugins/data/public/filter/action/apply_filter_action.ts index 5db3d779d12fae..8d2337264d02fd 100644 --- a/src/legacy/core_plugins/data/public/filter/action/apply_filter_action.ts +++ b/src/legacy/core_plugins/data/public/filter/action/apply_filter_action.ts @@ -18,21 +18,20 @@ */ import { i18n } from '@kbn/i18n'; -import { Filter } from '@kbn/es-query'; import { CoreStart } from 'src/core/public'; import { IAction, createAction, IncompatibleActionError, } from '../../../../../../plugins/ui_actions/public'; -import { FilterManager } from '../../../../../../plugins/data/public'; +import { FilterManager, esFilters } from '../../../../../../plugins/data/public'; import { TimefilterContract, changeTimeFilter, extractTimeFilter } from '../../timefilter'; import { applyFiltersPopover } from '../apply_filters/apply_filters_popover'; import { IndexPatternsStart } from '../../index_patterns'; export const GLOBAL_APPLY_FILTER_ACTION = 'GLOBAL_APPLY_FILTER_ACTION'; interface ActionContext { - filters: Filter[]; + filters: esFilters.Filter[]; timeFieldName?: string; } @@ -64,7 +63,7 @@ export function createFilterAction( throw new IncompatibleActionError(); } - let selectedFilters: Filter[] = filters; + let selectedFilters: esFilters.Filter[] = filters; if (selectedFilters.length > 1) { const indexPatterns = await Promise.all( @@ -73,7 +72,7 @@ export function createFilterAction( }) ); - const filterSelectionPromise: Promise = new Promise(resolve => { + const filterSelectionPromise: Promise = new Promise(resolve => { const overlay = overlays.openModal( applyFiltersPopover( filters, @@ -82,7 +81,7 @@ export function createFilterAction( overlay.close(); resolve([]); }, - (filterSelection: Filter[]) => { + (filterSelection: esFilters.Filter[]) => { overlay.close(); resolve(filterSelection); } diff --git a/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filter_popover_content.tsx b/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filter_popover_content.tsx index 8fc6b33f3f68a3..e9d05d6340e584 100644 --- a/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filter_popover_content.tsx +++ b/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filter_popover_content.tsx @@ -28,19 +28,18 @@ import { EuiModalHeaderTitle, EuiSwitch, } from '@elastic/eui'; -import { Filter } from '@kbn/es-query'; import { FormattedMessage } from '@kbn/i18n/react'; import React, { Component } from 'react'; import { IndexPattern } from '../../index_patterns'; import { getFilterDisplayText } from '../filter_bar/filter_editor/lib/get_filter_display_text'; -import { mapAndFlattenFilters } from '../../../../../../plugins/data/public'; +import { mapAndFlattenFilters, esFilters } from '../../../../../../plugins/data/public'; import { getDisplayValueFromFilter } from '../filter_bar/filter_editor/lib/get_display_value'; interface Props { - filters: Filter[]; + filters: esFilters.Filter[]; indexPatterns: IndexPattern[]; onCancel: () => void; - onSubmit: (filters: Filter[]) => void; + onSubmit: (filters: esFilters.Filter[]) => void; } interface State { @@ -58,7 +57,7 @@ export class ApplyFiltersPopoverContent extends Component { isFilterSelected: props.filters.map(() => true), }; } - private getLabel(filter: Filter) { + private getLabel(filter: esFilters.Filter) { const filterDisplayValue = getDisplayValueFromFilter(filter, this.props.indexPatterns); return getFilterDisplayText(filter, filterDisplayValue); } diff --git a/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filters_popover.tsx b/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filters_popover.tsx index 0687701429866d..41f757e726c40b 100644 --- a/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filters_popover.tsx +++ b/src/legacy/core_plugins/data/public/filter/apply_filters/apply_filters_popover.tsx @@ -18,15 +18,15 @@ */ import { EuiModal, EuiOverlayMask } from '@elastic/eui'; -import { Filter } from '@kbn/es-query'; import React, { Component } from 'react'; import { ApplyFiltersPopoverContent } from './apply_filter_popover_content'; import { IndexPattern } from '../../index_patterns/index_patterns'; +import { esFilters } from '../../../../../../plugins/data/public'; interface Props { - filters: Filter[]; + filters: esFilters.Filter[]; onCancel: () => void; - onSubmit: (filters: Filter[]) => void; + onSubmit: (filters: esFilters.Filter[]) => void; indexPatterns: IndexPattern[]; } @@ -56,9 +56,9 @@ export class ApplyFiltersPopover extends Component { } type cancelFunction = () => void; -type submitFunction = (filters: Filter[]) => void; +type submitFunction = (filters: esFilters.Filter[]) => void; export const applyFiltersPopover = ( - filters: Filter[], + filters: esFilters.Filter[], indexPatterns: IndexPattern[], onCancel: cancelFunction, onSubmit: submitFunction diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_bar.tsx b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_bar.tsx index 8a8fb36ea24bfd..333e1e328651d4 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_bar.tsx +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_bar.tsx @@ -18,16 +18,6 @@ */ import { EuiButtonEmpty, EuiFlexGroup, EuiFlexItem, EuiPopover } from '@elastic/eui'; -import { - buildEmptyFilter, - disableFilter, - enableFilter, - Filter, - pinFilter, - toggleFilterDisabled, - toggleFilterNegated, - unpinFilter, -} from '@kbn/es-query'; import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react'; import classNames from 'classnames'; import React, { useState } from 'react'; @@ -38,10 +28,11 @@ import { FilterEditor } from './filter_editor'; import { FilterItem } from './filter_item'; import { FilterOptions } from './filter_options'; import { useKibana, KibanaContextProvider } from '../../../../../../plugins/kibana_react/public'; +import { esFilters } from '../../../../../../plugins/data/public'; interface Props { - filters: Filter[]; - onFiltersUpdated?: (filters: Filter[]) => void; + filters: esFilters.Filter[]; + onFiltersUpdated?: (filters: esFilters.Filter[]) => void; className: string; indexPatterns: IndexPattern[]; intl: InjectedIntl; @@ -87,7 +78,7 @@ function FilterBarUI(props: Props) { return content; } - function onFiltersUpdated(filters: Filter[]) { + function onFiltersUpdated(filters: esFilters.Filter[]) { if (props.onFiltersUpdated) { props.onFiltersUpdated(filters); } @@ -112,7 +103,7 @@ function FilterBarUI(props: Props) { const isPinned = uiSettings!.get('filters:pinnedByDefault'); const [indexPattern] = props.indexPatterns; const index = indexPattern && indexPattern.id; - const newFilter = buildEmptyFilter(isPinned, index); + const newFilter = esFilters.buildEmptyFilter(isPinned, index); const button = ( void; + onSubmit: (filter: esFilters.Filter) => void; onCancel: () => void; intl: InjectedIntl; } @@ -379,7 +379,9 @@ class FilterEditorUI extends Component { private getFieldFromFilter() { const indexPattern = this.getIndexPatternFromFilter(); - return indexPattern && getFieldFromFilter(this.props.filter as FieldFilter, indexPattern); + return ( + indexPattern && getFieldFromFilter(this.props.filter as esFilters.FieldFilter, indexPattern) + ); } private getSelectedOperator() { diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.test.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.test.ts index 734c5d00e58d5d..dbff5096f2287d 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.test.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.test.ts @@ -17,7 +17,6 @@ * under the License. */ -import { FilterStateStore, toggleFilterNegated } from '@kbn/es-query'; import { mockFields, mockIndexPattern } from '../../../../index_patterns'; import { IndexPattern, Field } from '../../../../index'; import { @@ -42,6 +41,7 @@ import { existsFilter } from './fixtures/exists_filter'; import { phraseFilter } from './fixtures/phrase_filter'; import { phrasesFilter } from './fixtures/phrases_filter'; import { rangeFilter } from './fixtures/range_filter'; +import { esFilters } from '../../../../../../../../plugins/data/public'; jest.mock('ui/new_platform'); @@ -81,7 +81,7 @@ describe('Filter editor utils', () => { }); it('should return "is not" for phrase filter', () => { - const negatedPhraseFilter = toggleFilterNegated(phraseFilter); + const negatedPhraseFilter = esFilters.toggleFilterNegated(phraseFilter); const operator = getOperatorFromFilter(negatedPhraseFilter); expect(operator).not.toBeUndefined(); expect(operator && operator.type).toBe('phrase'); @@ -96,7 +96,7 @@ describe('Filter editor utils', () => { }); it('should return "is not one of" for negated phrases filter', () => { - const negatedPhrasesFilter = toggleFilterNegated(phrasesFilter); + const negatedPhrasesFilter = esFilters.toggleFilterNegated(phrasesFilter); const operator = getOperatorFromFilter(negatedPhrasesFilter); expect(operator).not.toBeUndefined(); expect(operator && operator.type).toBe('phrases'); @@ -111,7 +111,7 @@ describe('Filter editor utils', () => { }); it('should return "is not between" for negated range filter', () => { - const negatedRangeFilter = toggleFilterNegated(rangeFilter); + const negatedRangeFilter = esFilters.toggleFilterNegated(rangeFilter); const operator = getOperatorFromFilter(negatedRangeFilter); expect(operator).not.toBeUndefined(); expect(operator && operator.type).toBe('range'); @@ -126,7 +126,7 @@ describe('Filter editor utils', () => { }); it('should return "does not exists" for negated exists filter', () => { - const negatedExistsFilter = toggleFilterNegated(existsFilter); + const negatedExistsFilter = esFilters.toggleFilterNegated(existsFilter); const operator = getOperatorFromFilter(negatedExistsFilter); expect(operator).not.toBeUndefined(); expect(operator && operator.type).toBe('exists'); @@ -246,7 +246,7 @@ describe('Filter editor utils', () => { it('should build phrase filters', () => { const params = 'foo'; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], @@ -268,7 +268,7 @@ describe('Filter editor utils', () => { it('should build phrases filters', () => { const params = ['foo', 'bar']; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], @@ -290,7 +290,7 @@ describe('Filter editor utils', () => { it('should build range filters', () => { const params = { from: 'foo', to: 'qux' }; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], @@ -311,7 +311,7 @@ describe('Filter editor utils', () => { it('should build exists filters', () => { const params = undefined; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], @@ -332,7 +332,7 @@ describe('Filter editor utils', () => { it('should include disabled state', () => { const params = undefined; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], @@ -348,7 +348,7 @@ describe('Filter editor utils', () => { it('should negate based on operator', () => { const params = undefined; const alias = 'bar'; - const state = FilterStateStore.APP_STATE; + const state = esFilters.FilterStateStore.APP_STATE; const filter = buildFilter( mockedIndexPattern, mockedFields[0], diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.ts index f0628f03c173e1..b7d20526a6b924 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/filter_editor_utils.ts @@ -18,42 +18,30 @@ */ import dateMath from '@elastic/datemath'; -import { - buildExistsFilter, - buildPhraseFilter, - buildPhrasesFilter, - buildRangeFilter, - FieldFilter, - Filter, - FilterMeta, - FilterStateStore, - PhraseFilter, - PhrasesFilter, - RangeFilter, -} from '@kbn/es-query'; import { omit } from 'lodash'; import { Ipv4Address } from '../../../../../../../../plugins/kibana_utils/public'; import { Field, IndexPattern, isFilterable } from '../../../../index_patterns'; import { FILTER_OPERATORS, Operator } from './filter_operators'; +import { esFilters } from '../../../../../../../../plugins/data/public'; export function getIndexPatternFromFilter( - filter: Filter, + filter: esFilters.Filter, indexPatterns: IndexPattern[] ): IndexPattern | undefined { return indexPatterns.find(indexPattern => indexPattern.id === filter.meta.index); } -export function getFieldFromFilter(filter: FieldFilter, indexPattern: IndexPattern) { +export function getFieldFromFilter(filter: esFilters.FieldFilter, indexPattern: IndexPattern) { return indexPattern.fields.find(field => field.name === filter.meta.key); } -export function getOperatorFromFilter(filter: Filter) { +export function getOperatorFromFilter(filter: esFilters.Filter) { return FILTER_OPERATORS.find(operator => { return filter.meta.type === operator.type && filter.meta.negate === operator.negate; }); } -export function getQueryDslFromFilter(filter: Filter) { +export function getQueryDslFromFilter(filter: esFilters.Filter) { return omit(filter, ['$state', 'meta']); } @@ -67,16 +55,16 @@ export function getOperatorOptions(field: Field) { }); } -export function getFilterParams(filter: Filter) { +export function getFilterParams(filter: esFilters.Filter) { switch (filter.meta.type) { case 'phrase': - return (filter as PhraseFilter).meta.params.query; + return (filter as esFilters.PhraseFilter).meta.params.query; case 'phrases': - return (filter as PhrasesFilter).meta.params; + return (filter as esFilters.PhrasesFilter).meta.params; case 'range': return { - from: (filter as RangeFilter).meta.params.gte, - to: (filter as RangeFilter).meta.params.lt, + from: (filter as esFilters.RangeFilter).meta.params.gte, + to: (filter as esFilters.RangeFilter).meta.params.lt, }; } } @@ -133,8 +121,8 @@ export function buildFilter( disabled: boolean, params: any, alias: string | null, - store: FilterStateStore -): Filter { + store: esFilters.FilterStateStore +): esFilters.Filter { const filter = buildBaseFilter(indexPattern, field, operator, params); filter.meta.alias = alias; filter.meta.negate = operator.negate; @@ -148,17 +136,17 @@ function buildBaseFilter( field: Field, operator: Operator, params: any -): Filter { +): esFilters.Filter { switch (operator.type) { case 'phrase': - return buildPhraseFilter(field, params, indexPattern); + return esFilters.buildPhraseFilter(field, params, indexPattern); case 'phrases': - return buildPhrasesFilter(field, params, indexPattern); + return esFilters.buildPhrasesFilter(field, params, indexPattern); case 'range': const newParams = { gte: params.from, lt: params.to }; - return buildRangeFilter(field, newParams, indexPattern); + return esFilters.buildRangeFilter(field, newParams, indexPattern); case 'exists': - return buildExistsFilter(field, indexPattern); + return esFilters.buildExistsFilter(field, indexPattern); default: throw new Error(`Unknown operator type: ${operator.type}`); } @@ -170,10 +158,10 @@ export function buildCustomFilter( disabled: boolean, negate: boolean, alias: string | null, - store: FilterStateStore -): Filter { - const meta: FilterMeta = { index, type: 'custom', disabled, negate, alias }; - const filter: Filter = { ...queryDsl, meta }; + store: esFilters.FilterStateStore +): esFilters.Filter { + const meta: esFilters.FilterMeta = { index, type: 'custom', disabled, negate, alias }; + const filter: esFilters.Filter = { ...queryDsl, meta }; filter.$state = { store }; return filter; } diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/exists_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/exists_filter.ts index a17f767006f3ea..5af97818f9bfbd 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/exists_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/exists_filter.ts @@ -17,9 +17,9 @@ * under the License. */ -import { ExistsFilter, FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../../plugins/data/public'; -export const existsFilter: ExistsFilter = { +export const existsFilter: esFilters.ExistsFilter = { meta: { index: 'logstash-*', negate: false, @@ -29,6 +29,6 @@ export const existsFilter: ExistsFilter = { alias: null, }, $state: { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }, }; diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrase_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrase_filter.ts index 77bb8e06c801ad..b6c8b9905e6b33 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrase_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrase_filter.ts @@ -17,9 +17,9 @@ * under the License. */ -import { FilterStateStore, PhraseFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../../plugins/data/public'; -export const phraseFilter: PhraseFilter = { +export const phraseFilter: esFilters.PhraseFilter = { meta: { negate: false, index: 'logstash-*', @@ -33,6 +33,6 @@ export const phraseFilter: PhraseFilter = { }, }, $state: { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }, }; diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrases_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrases_filter.ts index e86c3ee1318e34..2e2ba4f798bddf 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrases_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/phrases_filter.ts @@ -17,9 +17,9 @@ * under the License. */ -import { FilterStateStore, PhrasesFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../../plugins/data/public'; -export const phrasesFilter: PhrasesFilter = { +export const phrasesFilter: esFilters.PhrasesFilter = { meta: { index: 'logstash-*', type: 'phrases', @@ -31,6 +31,6 @@ export const phrasesFilter: PhrasesFilter = { alias: null, }, $state: { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }, }; diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/range_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/range_filter.ts index 46a5181450feae..c6438e30ecec61 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/range_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/fixtures/range_filter.ts @@ -17,9 +17,9 @@ * under the License. */ -import { FilterStateStore, RangeFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../../plugins/data/public'; -export const rangeFilter: RangeFilter = { +export const rangeFilter: esFilters.RangeFilter = { meta: { index: 'logstash-*', negate: false, @@ -34,7 +34,7 @@ export const rangeFilter: RangeFilter = { }, }, $state: { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }, range: {}, }; diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_display_value.ts b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_display_value.ts index 551b99d01b7da8..d8af7b3e97ad23 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_display_value.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_display_value.ts @@ -18,7 +18,7 @@ */ import { get } from 'lodash'; -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../plugins/data/public'; import { IndexPattern } from '../../../../index_patterns/index_patterns'; import { Field } from '../../../../index_patterns/fields'; import { getIndexPatternFromFilter } from './filter_editor_utils'; @@ -33,7 +33,10 @@ function getValueFormatter(indexPattern?: IndexPattern, key?: string) { return format; } -export function getDisplayValueFromFilter(filter: Filter, indexPatterns: IndexPattern[]): string { +export function getDisplayValueFromFilter( + filter: esFilters.Filter, + indexPatterns: IndexPattern[] +): string { const indexPattern = getIndexPatternFromFilter(filter, indexPatterns); if (typeof filter.meta.value === 'function') { diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_filter_display_text.tsx b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_filter_display_text.tsx index 429381694ddf86..21abcd8510046d 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_filter_display_text.tsx +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_editor/lib/get_filter_display_text.tsx @@ -19,11 +19,11 @@ import React, { Fragment } from 'react'; import { EuiTextColor } from '@elastic/eui'; -import { Filter } from '@kbn/es-query'; import { i18n } from '@kbn/i18n'; import { existsOperator, isOneOfOperator } from './filter_operators'; +import { esFilters } from '../../../../../../../../plugins/data/public'; -export function getFilterDisplayText(filter: Filter, filterDisplayName: string) { +export function getFilterDisplayText(filter: esFilters.Filter, filterDisplayName: string) { const prefixText = filter.meta.negate ? ` ${i18n.translate('data.filter.filterBar.negatedFilterPrefix', { defaultMessage: 'NOT ', diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_item.tsx b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_item.tsx index 2e98cbd306e9c6..50c1672333801e 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_item.tsx +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_item.tsx @@ -18,13 +18,6 @@ */ import { EuiContextMenu, EuiPopover } from '@elastic/eui'; -import { - Filter, - isFilterPinned, - toggleFilterDisabled, - toggleFilterNegated, - toggleFilterPinned, -} from '@kbn/es-query'; import { InjectedIntl, injectI18n } from '@kbn/i18n/react'; import classNames from 'classnames'; import React, { Component } from 'react'; @@ -33,13 +26,14 @@ import { IndexPattern } from '../../index_patterns'; import { FilterEditor } from './filter_editor'; import { FilterView } from './filter_view'; import { getDisplayValueFromFilter } from './filter_editor/lib/get_display_value'; +import { esFilters } from '../../../../../../plugins/data/public'; interface Props { id: string; - filter: Filter; + filter: esFilters.Filter; indexPatterns: IndexPattern[]; className?: string; - onUpdate: (filter: Filter) => void; + onUpdate: (filter: esFilters.Filter) => void; onRemove: () => void; intl: InjectedIntl; uiSettings: UiSettingsClientContract; @@ -62,7 +56,7 @@ class FilterItemUI extends Component { 'globalFilterItem', { 'globalFilterItem-isDisabled': disabled, - 'globalFilterItem-isPinned': isFilterPinned(filter), + 'globalFilterItem-isPinned': esFilters.isFilterPinned(filter), 'globalFilterItem-isExcluded': negate, }, this.props.className @@ -91,7 +85,7 @@ class FilterItemUI extends Component { id: 0, items: [ { - name: isFilterPinned(filter) + name: esFilters.isFilterPinned(filter) ? this.props.intl.formatMessage({ id: 'data.filter.filterBar.unpinFilterButtonLabel', defaultMessage: 'Unpin', @@ -209,23 +203,23 @@ class FilterItemUI extends Component { }); }; - private onSubmit = (filter: Filter) => { + private onSubmit = (filter: esFilters.Filter) => { this.closePopover(); this.props.onUpdate(filter); }; private onTogglePinned = () => { - const filter = toggleFilterPinned(this.props.filter); + const filter = esFilters.toggleFilterPinned(this.props.filter); this.props.onUpdate(filter); }; private onToggleNegated = () => { - const filter = toggleFilterNegated(this.props.filter); + const filter = esFilters.toggleFilterNegated(this.props.filter); this.props.onUpdate(filter); }; private onToggleDisabled = () => { - const filter = toggleFilterDisabled(this.props.filter); + const filter = esFilters.toggleFilterDisabled(this.props.filter); this.props.onUpdate(filter); }; } diff --git a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_view/index.tsx b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_view/index.tsx index 1dc93335d42bef..6421691c4ef416 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_bar/filter_view/index.tsx +++ b/src/legacy/core_plugins/data/public/filter/filter_bar/filter_view/index.tsx @@ -18,13 +18,13 @@ */ import { EuiBadge, useInnerText } from '@elastic/eui'; -import { Filter, isFilterPinned } from '@kbn/es-query'; import { i18n } from '@kbn/i18n'; import React, { SFC } from 'react'; import { getFilterDisplayText } from '../filter_editor/lib/get_filter_display_text'; +import { esFilters } from '../../../../../../../plugins/data/public'; interface Props { - filter: Filter; + filter: esFilters.Filter; displayName: string; [propName: string]: any; } @@ -44,7 +44,7 @@ export const FilterView: SFC = ({ values: { innerText }, }); - if (isFilterPinned(filter)) { + if (esFilters.isFilterPinned(filter)) { title = `${i18n.translate('data.filter.filterBar.pinnedFilterPrefix', { defaultMessage: 'Pinned', })} ${title}`; diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts index aae9c0754a8d8d..08d5955d3fae9c 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.test.ts @@ -19,12 +19,11 @@ import sinon from 'sinon'; -import { FilterStateStore } from '@kbn/es-query'; import { FilterStateManager } from './filter_state_manager'; import { StubState } from './test_helpers/stub_state'; import { getFilter } from './test_helpers/get_stub_filter'; -import { FilterManager } from '../../../../../../plugins/data/public'; +import { FilterManager, esFilters } from '../../../../../../plugins/data/public'; import { coreMock } from '../../../../../../core/public/mocks'; const setupMock = coreMock.createSetup(); @@ -59,7 +58,7 @@ describe('filter_state_manager', () => { }); test('should NOT watch state until both app and global state are defined', done => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); globalStateStub.filters.push(f1); setTimeout(() => { @@ -72,8 +71,8 @@ describe('filter_state_manager', () => { appStateStub.save = sinon.stub(); globalStateStub.save = sinon.stub(); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); filterManager.setFilters([f1, f2]); @@ -109,7 +108,7 @@ describe('filter_state_manager', () => { done(); }); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, true, true, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, true, true, 'age', 34); globalStateStub.filters.push(f1); }); @@ -122,7 +121,7 @@ describe('filter_state_manager', () => { done(); }); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); appStateStub.filters.push(f1); }); @@ -130,8 +129,8 @@ describe('filter_state_manager', () => { appStateStub.save = sinon.stub(); globalStateStub.save = sinon.stub(); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); filterManager.setFilters([f1, f2]); @@ -143,8 +142,8 @@ describe('filter_state_manager', () => { appStateStub.save = sinon.stub(); globalStateStub.save = sinon.stub(); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); filterManager.addFilters([f1, f2]); @@ -160,7 +159,7 @@ describe('filter_state_manager', () => { ** And triggers *another* filter manager update. */ test('should NOT re-trigger filter manager', done => { - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); filterManager.setFilters([f1]); const setFiltersSpy = sinon.spy(filterManager, 'setFilters'); diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts index af8722c37c703c..61821b7ad45e94 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/filter_state_manager.ts @@ -17,11 +17,9 @@ * under the License. */ -import { FilterStateStore } from '@kbn/es-query'; - import _ from 'lodash'; import { State } from 'ui/state_management/state'; -import { FilterManager } from '../../../../../../plugins/data/public'; +import { FilterManager, esFilters } from '../../../../../../plugins/data/public'; type GetAppStateFunc = () => State | undefined | null; @@ -73,8 +71,8 @@ export class FilterStateManager { const newGlobalFilters = _.cloneDeep(globalFilters); const newAppFilters = _.cloneDeep(appFilters); - FilterManager.setFiltersStore(newAppFilters, FilterStateStore.APP_STATE); - FilterManager.setFiltersStore(newGlobalFilters, FilterStateStore.GLOBAL_STATE); + FilterManager.setFiltersStore(newAppFilters, esFilters.FilterStateStore.APP_STATE); + FilterManager.setFiltersStore(newGlobalFilters, esFilters.FilterStateStore.GLOBAL_STATE); this.filterManager.setFilters(newGlobalFilters.concat(newAppFilters)); }, 10); diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/get_stub_filter.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/get_stub_filter.ts index 20d9e236f49be8..5238efe5efa59c 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/get_stub_filter.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/get_stub_filter.ts @@ -17,15 +17,15 @@ * under the License. */ -import { Filter, FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../plugins/data/public'; export function getFilter( - store: FilterStateStore, + store: esFilters.FilterStateStore, disabled: boolean, negated: boolean, queryKey: string, queryValue: any -): Filter { +): esFilters.Filter { return { $state: { store, diff --git a/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/stub_state.ts b/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/stub_state.ts index ab92016d1b9ab3..f0a4bdef0229d0 100644 --- a/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/stub_state.ts +++ b/src/legacy/core_plugins/data/public/filter/filter_manager/test_helpers/stub_state.ts @@ -19,11 +19,11 @@ import sinon from 'sinon'; -import { Filter } from '@kbn/es-query'; import { State } from 'ui/state_management/state'; +import { esFilters } from '../../../../../../../plugins/data/public'; export class StubState implements State { - filters: Filter[]; + filters: esFilters.Filter[]; save: sinon.SinonSpy; constructor() { diff --git a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_patterns.ts b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_patterns.ts index d445e2e58cd542..4767b6d3a3ca7a 100644 --- a/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_patterns.ts +++ b/src/legacy/core_plugins/data/public/index_patterns/index_patterns/index_patterns.ts @@ -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(); @@ -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) { diff --git a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx index bfa29bef634623..9f03f7fd307789 100644 --- a/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx +++ b/src/legacy/core_plugins/data/public/query/query_bar/components/query_bar_input.tsx @@ -33,7 +33,6 @@ import { import { InjectedIntl, injectI18n, FormattedMessage } from '@kbn/i18n/react'; import { debounce, compact, isEqual } from 'lodash'; -import { documentationLinks } from 'ui/documentation_links'; import { Toast } from 'src/core/public'; import { AutocompleteSuggestion, @@ -348,7 +347,7 @@ export class QueryBarInputUI extends Component { suggestion.field.subType.nested && !this.services.storage.get('kibana.KQLNestedQuerySyntaxInfoOptOut') ) { - const notifications = this.services.notifications; + const { notifications, docLinks } = this.services; const onKQLNestedQuerySyntaxInfoOptOut = (toast: Toast) => { if (!this.services.storage) return; @@ -356,7 +355,7 @@ export class QueryBarInputUI extends Component { notifications!.toasts.remove(toast); }; - if (notifications) { + if (notifications && docLinks) { const toast = notifications.toasts.add({ title: this.props.intl.formatMessage({ id: 'data.query.queryBar.KQLNestedQuerySyntaxInfoTitle', @@ -372,7 +371,7 @@ export class QueryBarInputUI extends Component { Learn more in our {link}." values={{ link: ( - + { - return (filters: Filter[]) => { + return (filters: esFilters.Filter[]) => { data.query.filterManager.setFilters(filters); }; }; diff --git a/src/legacy/core_plugins/data/public/search/search_bar/components/saved_query_management/saved_query_management_component.tsx b/src/legacy/core_plugins/data/public/search/search_bar/components/saved_query_management/saved_query_management_component.tsx index 56116e155eb2f4..b73b8edb39e547 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/components/saved_query_management/saved_query_management_component.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/components/saved_query_management/saved_query_management_component.tsx @@ -29,6 +29,7 @@ import { EuiPagination, EuiText, EuiSpacer, + EuiIcon, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; @@ -116,8 +117,6 @@ export const SavedQueryManagementComponent: FunctionComponent = ({ const savedQueryPopoverButton = ( { setIsOpen(!isOpen); }} @@ -129,7 +128,8 @@ export const SavedQueryManagementComponent: FunctionComponent = ({ })} data-test-subj="saved-query-management-popover-button" > - # + + ); diff --git a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx index c7f8b02caf853f..d3a26239e1006c 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/components/search_bar.tsx @@ -18,7 +18,6 @@ */ import { compact } from 'lodash'; -import { Filter } from '@kbn/es-query'; import { InjectedIntl, injectI18n } from '@kbn/i18n/react'; import classNames from 'classnames'; import React, { Component } from 'react'; @@ -39,14 +38,15 @@ import { KibanaReactContextValue, } from '../../../../../../../plugins/kibana_react/public'; import { IDataPluginServices } from '../../../types'; +import { esFilters } from '../../../../../../../plugins/data/public'; interface SearchBarInjectedDeps { kibana: KibanaReactContextValue; intl: InjectedIntl; timeHistory: TimeHistoryContract; // Filter bar - onFiltersUpdated?: (filters: Filter[]) => void; - filters?: Filter[]; + onFiltersUpdated?: (filters: esFilters.Filter[]) => void; + filters?: esFilters.Filter[]; // Date picker dateRangeFrom?: string; dateRangeTo?: string; diff --git a/src/legacy/core_plugins/data/public/search/search_bar/index.tsx b/src/legacy/core_plugins/data/public/search/search_bar/index.tsx index 0c677bea985365..ebde9d60b0b51e 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/index.tsx +++ b/src/legacy/core_plugins/data/public/search/search_bar/index.tsx @@ -17,9 +17,9 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { RefreshInterval, TimeRange } from 'src/plugins/data/public'; import { Query } from '../../query/query_bar'; +import { esFilters } from '../../../../../../plugins/data/public'; export * from './components'; @@ -36,6 +36,6 @@ export interface SavedQueryAttributes { title: string; description: string; query: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; timefilter?: SavedQueryTimeFilter; } diff --git a/src/legacy/core_plugins/data/public/search/search_bar/lib/saved_query_service.test.ts b/src/legacy/core_plugins/data/public/search/search_bar/lib/saved_query_service.test.ts index ac5fdb7fe99d54..415da8a2c32cc1 100644 --- a/src/legacy/core_plugins/data/public/search/search_bar/lib/saved_query_service.test.ts +++ b/src/legacy/core_plugins/data/public/search/search_bar/lib/saved_query_service.test.ts @@ -19,7 +19,7 @@ import { SavedQueryAttributes } from '../index'; import { createSavedQueryService } from './saved_query_service'; -import { FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../plugins/data/public'; const savedQueryAttributes: SavedQueryAttributes = { title: 'foo', @@ -43,7 +43,7 @@ const savedQueryAttributesWithFilters: SavedQueryAttributes = { filters: [ { query: { match_all: {} }, - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, negate: false, diff --git a/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.test.ts b/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.test.ts index 5e16120f3b3c25..df3e33060b01f2 100644 --- a/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.test.ts +++ b/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.test.ts @@ -16,10 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -import { RangeFilter } from '@kbn/es-query'; import { changeTimeFilter } from './change_time_filter'; import { TimeRange } from 'src/plugins/data/public'; import { timefilterServiceMock } from '../timefilter_service.mock'; +import { esFilters } from '../../../../../../plugins/data/public'; const timefilterMock = timefilterServiceMock.createSetupContract(); const timefilter = timefilterMock.timefilter; @@ -42,7 +42,7 @@ describe('changeTimeFilter()', () => { test('should change the timefilter to match the range gt/lt', () => { const filter: any = { range: { '@timestamp': { gt, lt } } }; - changeTimeFilter(timefilter, filter as RangeFilter); + changeTimeFilter(timefilter, filter as esFilters.RangeFilter); const { to, from } = timefilter.getTime(); @@ -52,7 +52,7 @@ describe('changeTimeFilter()', () => { test('should change the timefilter to match the range gte/lte', () => { const filter: any = { range: { '@timestamp': { gte: gt, lte: lt } } }; - changeTimeFilter(timefilter, filter as RangeFilter); + changeTimeFilter(timefilter, filter as esFilters.RangeFilter); const { to, from } = timefilter.getTime(); diff --git a/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.ts b/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.ts index 4780ddb6b4b448..7943aab3c151f6 100644 --- a/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/lib/change_time_filter.ts @@ -19,10 +19,10 @@ import moment from 'moment'; import { keys } from 'lodash'; -import { RangeFilter } from '@kbn/es-query'; import { TimefilterContract } from '../timefilter'; +import { esFilters } from '../../../../../../plugins/data/public'; -export function convertRangeFilterToTimeRange(filter: RangeFilter) { +export function convertRangeFilterToTimeRange(filter: esFilters.RangeFilter) { const key = keys(filter.range)[0]; const values = filter.range[key]; @@ -32,6 +32,6 @@ export function convertRangeFilterToTimeRange(filter: RangeFilter) { }; } -export function changeTimeFilter(timeFilter: TimefilterContract, filter: RangeFilter) { +export function changeTimeFilter(timeFilter: TimefilterContract, filter: esFilters.RangeFilter) { timeFilter.setTime(convertRangeFilterToTimeRange(filter)); } diff --git a/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.test.ts b/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.test.ts index d55c9babeed796..981c50844c4f3e 100644 --- a/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.test.ts +++ b/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.test.ts @@ -17,15 +17,23 @@ * under the License. */ -import { Filter, buildRangeFilter, buildQueryFilter, buildPhraseFilter } from '@kbn/es-query'; import { extractTimeFilter } from './extract_time_filter'; +import { esFilters } from '../../../../../../plugins/data/public'; describe('filter manager utilities', () => { describe('extractTimeFilter()', () => { test('should detect timeFilter', async () => { - const filters: Filter[] = [ - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'logstash-*'), - buildRangeFilter({ name: 'time' }, { gt: 1388559600000, lt: 1388646000000 }, 'logstash-*'), + const filters: esFilters.Filter[] = [ + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'logstash-*', + '' + ), + esFilters.buildRangeFilter( + { name: 'time' }, + { gt: 1388559600000, lt: 1388646000000 }, + 'logstash-*' + ), ]; const result = await extractTimeFilter('time', filters); @@ -34,9 +42,13 @@ describe('filter manager utilities', () => { }); test("should not return timeFilter when name doesn't match", async () => { - const filters: Filter[] = [ - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'logstash-*'), - buildRangeFilter({ name: '@timestamp' }, { from: 1, to: 2 }, 'logstash-*'), + const filters: esFilters.Filter[] = [ + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'logstash-*', + '' + ), + esFilters.buildRangeFilter({ name: '@timestamp' }, { from: 1, to: 2 }, 'logstash-*', ''), ]; const result = await extractTimeFilter('time', filters); @@ -45,9 +57,13 @@ describe('filter manager utilities', () => { }); test('should not return a non range filter, even when names match', async () => { - const filters: Filter[] = [ - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'logstash-*'), - buildPhraseFilter({ name: 'time' }, 'banana', 'logstash-*'), + const filters: esFilters.Filter[] = [ + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'logstash-*', + '' + ), + esFilters.buildPhraseFilter({ name: 'time' }, 'banana', 'logstash-*'), ]; const result = await extractTimeFilter('time', filters); diff --git a/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.ts b/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.ts index 22bda5b21295ee..4281610cb63e4e 100644 --- a/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.ts +++ b/src/legacy/core_plugins/data/public/timefilter/lib/extract_time_filter.ts @@ -18,13 +18,13 @@ */ import { keys, partition } from 'lodash'; -import { Filter, isRangeFilter, RangeFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; -export function extractTimeFilter(timeFieldName: string, filters: Filter[]) { - const [timeRangeFilter, restOfFilters] = partition(filters, (obj: Filter) => { +export function extractTimeFilter(timeFieldName: string, filters: esFilters.Filter[]) { + const [timeRangeFilter, restOfFilters] = partition(filters, (obj: esFilters.Filter) => { let key; - if (isRangeFilter(obj)) { + if (esFilters.isRangeFilter(obj)) { key = keys(obj.range)[0]; } @@ -33,6 +33,6 @@ export function extractTimeFilter(timeFieldName: string, filters: Filter[]) { return { restOfFilters, - timeRangeFilter: timeRangeFilter[0] as RangeFilter | undefined, + timeRangeFilter: timeRangeFilter[0] as esFilters.RangeFilter | undefined, }; } diff --git a/src/legacy/core_plugins/expressions/public/np_ready/public/types.ts b/src/legacy/core_plugins/expressions/public/np_ready/public/types.ts index a8d89b44566932..9d7b4fb6d04806 100644 --- a/src/legacy/core_plugins/expressions/public/np_ready/public/types.ts +++ b/src/legacy/core_plugins/expressions/public/np_ready/public/types.ts @@ -17,10 +17,9 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { TimeRange } from '../../../../../../plugins/data/public'; import { Adapters } from '../../../../../../plugins/inspector/public'; import { Query } from '../../../../../../plugins/data/public'; -export { TimeRange, Adapters, Filter, Query }; +export { TimeRange, Adapters, Query }; export * from '../../../../../../plugins/expressions/public'; diff --git a/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js b/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js index 9f0ed1dfb5097b..65b1d41fa82393 100644 --- a/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js +++ b/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/phrase_filter_manager.js @@ -20,12 +20,8 @@ import _ from 'lodash'; import { FilterManager } from './filter_manager.js'; import { - buildPhraseFilter, - buildPhrasesFilter, - getPhraseFilterField, - getPhraseFilterValue, - isPhraseFilter, -} from '@kbn/es-query'; + esFilters, +} from '../../../../../../plugins/data/public'; export class PhraseFilterManager extends FilterManager { constructor(controlId, fieldName, indexPattern, queryFilter) { @@ -43,12 +39,12 @@ export class PhraseFilterManager extends FilterManager { createFilter(phrases) { let newFilter; if (phrases.length === 1) { - newFilter = buildPhraseFilter( + newFilter = esFilters.buildPhraseFilter( this.indexPattern.fields.getByName(this.fieldName), phrases[0], this.indexPattern); } else { - newFilter = buildPhrasesFilter( + newFilter = esFilters.buildPhrasesFilter( this.indexPattern.fields.getByName(this.fieldName), phrases, this.indexPattern); @@ -107,12 +103,12 @@ export class PhraseFilterManager extends FilterManager { } // single phrase filter - if (isPhraseFilter(kbnFilter)) { - if (getPhraseFilterField(kbnFilter) !== this.fieldName) { + if (esFilters.isPhraseFilter(kbnFilter)) { + if (esFilters.getPhraseFilterField(kbnFilter) !== this.fieldName) { return; } - return getPhraseFilterValue(kbnFilter); + return esFilters.getPhraseFilterValue(kbnFilter); } // single phrase filter from bool filter diff --git a/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js b/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js index 1c8f5e2aa5a3e3..3a232fd8b543d7 100644 --- a/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js +++ b/src/legacy/core_plugins/input_control_vis/public/control/filter_manager/range_filter_manager.js @@ -19,7 +19,7 @@ import _ from 'lodash'; import { FilterManager } from './filter_manager.js'; -import { buildRangeFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; // Convert slider value into ES range filter function toRange(sliderValue) { @@ -55,7 +55,7 @@ export class RangeFilterManager extends FilterManager { * @return {object} range filter */ createFilter(value) { - const newFilter = buildRangeFilter( + const newFilter = esFilters.buildRangeFilter( this.indexPattern.fields.getByName(this.fieldName), toRange(value), this.indexPattern); diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx index 7a0398e86a60da..5fa3a938ed9dfc 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app.tsx @@ -35,7 +35,6 @@ import { } from 'ui/state_management/app_state'; import { KbnUrl } from 'ui/url/kbn_url'; -import { Filter } from '@kbn/es-query'; import { TimeRange } from 'src/plugins/data/public'; import { IndexPattern } from 'ui/index_patterns'; import { IPrivate } from 'ui/private'; @@ -46,6 +45,7 @@ import { Subscription } from 'rxjs'; import { ViewMode } from '../../../embeddable_api/public/np_ready/public'; import { SavedObjectDashboard } from './saved_dashboard/saved_dashboard'; import { DashboardAppState, SavedDashboardPanel, ConfirmModalFn } from './types'; +import { esFilters } from '../../../../../../src/plugins/data/public'; import { DashboardAppController } from './dashboard_app_controller'; @@ -55,7 +55,7 @@ export interface DashboardAppScope extends ng.IScope { screenTitle: string; model: { query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; timeRestore: boolean; title: string; description: string; @@ -81,9 +81,9 @@ export interface DashboardAppScope extends ng.IScope { isPaused: boolean; refreshInterval: any; }) => void; - onFiltersUpdated: (filters: Filter[]) => void; + onFiltersUpdated: (filters: esFilters.Filter[]) => void; onCancelApplyFilters: () => void; - onApplyFilters: (filters: Filter[]) => void; + onApplyFilters: (filters: esFilters.Filter[]) => void; onQuerySaved: (savedQuery: SavedQuery) => void; onSavedQueryUpdated: (savedQuery: SavedQuery) => void; onClearSavedQuery: () => void; diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app_controller.tsx b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app_controller.tsx index abf7b22a6e48c6..64c75609476813 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app_controller.tsx +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_app_controller.tsx @@ -48,7 +48,6 @@ import { } from 'ui/state_management/app_state'; import { KbnUrl } from 'ui/url/kbn_url'; -import { Filter } from '@kbn/es-query'; import { IndexPattern } from 'ui/index_patterns'; import { IPrivate } from 'ui/private'; import { Query, SavedQuery } from 'src/legacy/core_plugins/data/public'; @@ -59,6 +58,7 @@ import { npStart } from 'ui/new_platform'; import { SavedObjectFinder } from 'ui/saved_objects/components/saved_object_finder'; import { extractTimeFilter, changeTimeFilter } from '../../../data/public'; import { start as data } from '../../../data/public/legacy'; +import { esFilters } from '../../../../../plugins/data/public'; import { DashboardContainer, @@ -514,7 +514,7 @@ export class DashboardAppController { } ); - $scope.$watch('appState.$newFilters', (filters: Filter[] = []) => { + $scope.$watch('appState.$newFilters', (filters: esFilters.Filter[] = []) => { if (filters.length === 1) { $scope.onApplyFilters(filters); } diff --git a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state_manager.ts b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state_manager.ts index 7c1fc771de3491..8ffabe5add1c34 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state_manager.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/dashboard_state_manager.ts @@ -20,7 +20,6 @@ import { i18n } from '@kbn/i18n'; import _ from 'lodash'; -import { Filter } from '@kbn/es-query'; import { stateMonitorFactory, StateMonitor } from 'ui/state_management/state_monitor_factory'; import { Timefilter } from 'ui/timefilter'; import { AppStateClass as TAppStateClass } from 'ui/state_management/app_state'; @@ -29,6 +28,7 @@ import { Moment } from 'moment'; import { DashboardContainer } from 'src/legacy/core_plugins/dashboard_embeddable_container/public/np_ready/public'; import { ViewMode } from '../../../../../../src/plugins/embeddable/public'; +import { esFilters } from '../../../../../../src/plugins/data/public'; import { Query } from '../../../data/public'; import { getAppStateDefaults, migrateAppState } from './lib'; @@ -50,7 +50,7 @@ export class DashboardStateManager { public lastSavedDashboardFilters: { timeTo?: string | Moment; timeFrom?: string | Moment; - filterBars: Filter[]; + filterBars: esFilters.Filter[]; query: Query; }; private stateDefaults: DashboardAppStateDefaults; @@ -303,7 +303,7 @@ export class DashboardStateManager { return this.savedDashboard.timeRestore; } - public getLastSavedFilterBars(): Filter[] { + public getLastSavedFilterBars(): esFilters.Filter[] { return this.lastSavedDashboardFilters.filterBars; } @@ -461,7 +461,7 @@ export class DashboardStateManager { * Applies the current filter state to the dashboard. * @param filter An array of filter bar filters. */ - public applyFilters(query: Query, filters: Filter[]) { + public applyFilters(query: Query, filters: esFilters.Filter[]) { this.appState.query = query; this.savedDashboard.searchSource.setField('query', query); this.savedDashboard.searchSource.setField('filter', filters); diff --git a/src/legacy/core_plugins/kibana/public/dashboard/lib/filter_utils.ts b/src/legacy/core_plugins/kibana/public/dashboard/lib/filter_utils.ts index 1fd50081c58bd6..19a0c32210737b 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/lib/filter_utils.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/lib/filter_utils.ts @@ -19,7 +19,7 @@ import _ from 'lodash'; import moment, { Moment } from 'moment'; -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; /** * @typedef {Object} QueryFilter @@ -65,9 +65,9 @@ export class FilterUtils { * @param filters {Array.} * @returns {Array.} */ - public static cleanFiltersForComparison(filters: Filter[]) { + public static cleanFiltersForComparison(filters: esFilters.Filter[]) { return _.map(filters, filter => { - const f: Partial = _.omit(filter, ['$$hashKey', '$state']); + const f: Partial = _.omit(filter, ['$$hashKey', '$state']); if (f.meta) { // f.meta.value is the value displayed in the filter bar. // It may also be loaded differently and shouldn't be used in this comparison. diff --git a/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.test.ts index 1f503ee6754070..ae3edae3b85d61 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.test.ts @@ -18,12 +18,12 @@ */ import { moveFiltersToQuery, Pre600FilterQuery } from './move_filters_to_query'; -import { Filter, FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; -const filter: Filter = { +const filter: esFilters.Filter = { meta: { disabled: false, negate: false, alias: '' }, query: {}, - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, }; const queryFilter: Pre600FilterQuery = { @@ -38,7 +38,7 @@ test('Migrates an old filter query into the query field', () => { expect(newSearchSource).toEqual({ filter: [ { - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { alias: '', disabled: false, diff --git a/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.ts b/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.ts index 153bdeba9d35f1..8522495b9dedb2 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/migrations/move_filters_to_query.ts @@ -18,7 +18,7 @@ */ import { Query } from 'src/legacy/core_plugins/data/public'; -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; export interface Pre600FilterQuery { // pre 6.0.0 global query:queryString:options were stored per dashboard and would @@ -30,18 +30,18 @@ export interface Pre600FilterQuery { export interface SearchSourcePre600 { // I encountered at least one export from 7.0.0-alpha that was missing the filter property in here. // The maps data in esarchives actually has it, but I don't know how/when they created it. - filter?: Array; + filter?: Array; } export interface SearchSource730 { - filter: Filter[]; + filter: esFilters.Filter[]; query: Query; highlightAll?: boolean; version?: boolean; } -function isQueryFilter(filter: Filter | { query: unknown }): filter is Pre600FilterQuery { - return filter.query && !(filter as Filter).meta; +function isQueryFilter(filter: esFilters.Filter | { query: unknown }): filter is Pre600FilterQuery { + return filter.query && !(filter as esFilters.Filter).meta; } export function moveFiltersToQuery( diff --git a/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts b/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts index 1231ca28ed014b..5b860b0a2cc7c1 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboard.d.ts @@ -19,10 +19,9 @@ import { SearchSource } from 'ui/courier'; import { SavedObject } from 'ui/saved_objects/saved_object'; -import moment from 'moment'; import { RefreshInterval } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; export interface SavedObjectDashboard extends SavedObject { id?: string; @@ -41,5 +40,5 @@ export interface SavedObjectDashboard extends SavedObject { destroy: () => void; refreshInterval?: RefreshInterval; getQuery(): Query; - getFilters(): Filter[]; + getFilters(): esFilters.Filter[]; } diff --git a/src/legacy/core_plugins/kibana/public/dashboard/types.ts b/src/legacy/core_plugins/kibana/public/dashboard/types.ts index ccccc89004e365..5aaca7b62094f8 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/types.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/types.ts @@ -18,7 +18,6 @@ */ import { AppState } from 'ui/state_management/app_state'; -import { Filter } from '@kbn/es-query'; import { Query } from 'src/legacy/core_plugins/data/public'; import { AppState as TAppState } from 'ui/state_management/app_state'; import { ViewMode } from 'src/plugins/embeddable/public'; @@ -30,6 +29,7 @@ import { RawSavedDashboardPanel640To720, RawSavedDashboardPanel730ToLatest, } from './migrations/types'; +import { esFilters } from '../../../../../plugins/data/public'; export type NavAction = (anchorElement?: any) => void; @@ -110,7 +110,7 @@ export interface DashboardAppStateParameters { useMargins: boolean; }; query: Query | string; - filters: Filter[]; + filters: esFilters.Filter[]; viewMode: ViewMode; savedQuery?: string; } diff --git a/src/legacy/core_plugins/kibana/public/discover/angular/context/api/context.ts b/src/legacy/core_plugins/kibana/public/discover/angular/context/api/context.ts index 268f176f2c61e1..3314bbbf189c4b 100644 --- a/src/legacy/core_plugins/kibana/public/discover/angular/context/api/context.ts +++ b/src/legacy/core_plugins/kibana/public/discover/angular/context/api/context.ts @@ -17,7 +17,6 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { IndexPatterns, IndexPattern, getServices } from '../../../kibana_services'; import { reverseSortDir, SortDirection } from './utils/sorting'; import { extractNanos, convertIsoToMillis } from './utils/date_conversion'; @@ -25,6 +24,7 @@ import { fetchHitsInInterval } from './utils/fetch_hits_in_interval'; import { generateIntervals } from './utils/generate_intervals'; import { getEsQuerySearchAfter } from './utils/get_es_query_search_after'; import { getEsQuerySort } from './utils/get_es_query_sort'; +import { esFilters } from '../../../../../../../../plugins/data/public'; export type SurrDocType = 'successors' | 'predecessors'; export interface EsHitRecord { @@ -67,7 +67,7 @@ function fetchContextProvider(indexPatterns: IndexPatterns) { tieBreakerField: string, sortDir: SortDirection, size: number, - filters: Filter[] + filters: esFilters.Filter[] ) { if (typeof anchor !== 'object' || anchor === null) { return []; @@ -112,7 +112,7 @@ function fetchContextProvider(indexPatterns: IndexPatterns) { return documents; } - async function createSearchSource(indexPattern: IndexPattern, filters: Filter[]) { + async function createSearchSource(indexPattern: IndexPattern, filters: esFilters.Filter[]) { return new SearchSource() .setParent(false) .setField('index', indexPattern) diff --git a/src/legacy/core_plugins/kibana/public/discover/angular/doc_table/components/table_row.js b/src/legacy/core_plugins/kibana/public/discover/angular/doc_table/components/table_row.js index 355d9defbb63d6..6f5a94442e9774 100644 --- a/src/legacy/core_plugins/kibana/public/discover/angular/doc_table/components/table_row.js +++ b/src/legacy/core_plugins/kibana/public/discover/angular/doc_table/components/table_row.js @@ -26,10 +26,10 @@ import { noWhiteSpace } from '../../../../../common/utils/no_white_space'; import openRowHtml from './table_row/open.html'; import detailsHtml from './table_row/details.html'; import { getServices } from '../../../kibana_services'; -import { disableFilter } from '@kbn/es-query'; import { dispatchRenderComplete } from '../../../../../../../../plugins/kibana_utils/public'; import cellTemplateHtml from '../components/table_row/cell.html'; import truncateByHeightTemplateHtml from '../components/table_row/truncate_by_height.html'; +import { esFilters } from '../../../../../../../../plugins/data/public'; const module = getServices().uiModules.get('app/discover'); @@ -117,7 +117,7 @@ module.directive('kbnTableRow', function ($compile, $httpParamSerializer, kbnUrl const hash = $httpParamSerializer({ _a: rison.encode({ columns: $scope.columns, - filters: ($scope.filters || []).map(disableFilter), + filters: ($scope.filters || []).map(esFilters.disableFilter), }), }); return `${path}?${hash}`; diff --git a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts index e777501d35ca09..31b28d21fe8d84 100644 --- a/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts +++ b/src/legacy/core_plugins/kibana/public/discover/embeddable/search_embeddable.ts @@ -19,12 +19,12 @@ import _ from 'lodash'; import * as Rx from 'rxjs'; import { Subscription } from 'rxjs'; -import { Filter, FilterStateStore } from '@kbn/es-query'; import { i18n } from '@kbn/i18n'; import { TExecuteTriggerActions } from 'src/plugins/ui_actions/public'; import { TimeRange, onlyDisabledFiltersChanged } from '../../../../../../plugins/data/public'; import { setup as data } from '../../../../data/public/legacy'; import { Query, getTime } from '../../../../data/public'; +import { esFilters } from '../../../../../../plugins/data/public'; import { APPLY_FILTER_TRIGGER, Container, @@ -75,7 +75,7 @@ export interface FilterManager { values: string | string[], operation: string, index: number - ) => Filter[]; + ) => esFilters.Filter[]; } interface SearchEmbeddableConfig { @@ -105,7 +105,7 @@ export class SearchEmbeddable extends Embeddable private abortController?: AbortController; private prevTimeRange?: TimeRange; - private prevFilters?: Filter[]; + private prevFilters?: esFilters.Filter[]; private prevQuery?: Query; constructor( @@ -248,7 +248,7 @@ export class SearchEmbeddable extends Embeddable let filters = this.filterGen.generate(field, value, operator, indexPattern.id); filters = filters.map(filter => ({ ...filter, - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, })); await this.executeTriggerActions(APPLY_FILTER_TRIGGER, { diff --git a/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts b/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts index db8d2afc7aff3a..5473ec0e7b8b4d 100644 --- a/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts +++ b/src/legacy/core_plugins/kibana/public/discover/embeddable/types.ts @@ -19,16 +19,16 @@ import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; -import { Filter } from '@kbn/es-query'; import { EmbeddableInput, EmbeddableOutput, IEmbeddable } from 'src/plugins/embeddable/public'; import { StaticIndexPattern } from '../kibana_services'; import { SavedSearch } from '../types'; import { SortOrder } from '../angular/doc_table/components/table_header/helpers'; +import { esFilters } from '../../../../../../plugins/data/public'; export interface SearchInput extends EmbeddableInput { timeRange: TimeRange; query?: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; hidePanelTitles?: boolean; columns?: string[]; sort?: SortOrder[]; diff --git a/src/legacy/core_plugins/kibana/public/home/components/__snapshots__/home.test.js.snap b/src/legacy/core_plugins/kibana/public/home/components/__snapshots__/home.test.js.snap index fcf60a6009b29c..71c336b1d48d20 100644 --- a/src/legacy/core_plugins/kibana/public/home/components/__snapshots__/home.test.js.snap +++ b/src/legacy/core_plugins/kibana/public/home/components/__snapshots__/home.test.js.snap @@ -2,6 +2,7 @@ exports[`home directories should not render directory entry when showOnHomePage is false 1`] = ` + diff --git a/src/legacy/core_plugins/kibana/public/home/components/home_app.js b/src/legacy/core_plugins/kibana/public/home/components/home_app.js index 005d4bdb0a99eb..e4a6753e0771a2 100644 --- a/src/legacy/core_plugins/kibana/public/home/components/home_app.js +++ b/src/legacy/core_plugins/kibana/public/home/components/home_app.js @@ -18,21 +18,16 @@ */ import React from 'react'; +import { I18nProvider } from '@kbn/i18n/react'; import PropTypes from 'prop-types'; import { Home } from './home'; import { FeatureDirectory } from './feature_directory'; import { TutorialDirectory } from './tutorial_directory'; import { Tutorial } from './tutorial/tutorial'; -import { - HashRouter as Router, - Switch, - Route, -} from 'react-router-dom'; +import { HashRouter as Router, Switch, Route, Redirect } from 'react-router-dom'; import { getTutorial } from '../load_tutorials'; import { replaceTemplateStrings } from './tutorial/replace_template_strings'; -import { - getServices -} from '../kibana_services'; +import { getServices } from '../kibana_services'; export function HomeApp({ directories }) { const { @@ -47,8 +42,9 @@ export function HomeApp({ directories }) { const isCloudEnabled = getInjected('isCloudEnabled', false); const apmUiEnabled = getInjected('apmUiEnabled', true); const mlEnabled = getInjected('mlEnabled', false); + const defaultAppId = getInjected('kbnDefaultAppId', 'discover'); - const renderTutorialDirectory = (props) => { + const renderTutorialDirectory = props => { return ( { + const renderTutorial = props => { return ( - - - - - - - - - - - + + + + + + + + + + + + + + + + + ); } HomeApp.propTypes = { - directories: PropTypes.arrayOf(PropTypes.shape({ - id: PropTypes.string.isRequired, - title: PropTypes.string.isRequired, - description: PropTypes.string.isRequired, - icon: PropTypes.string.isRequired, - path: PropTypes.string.isRequired, - showOnHomePage: PropTypes.bool.isRequired, - category: PropTypes.string.isRequired, - })), + directories: PropTypes.arrayOf( + PropTypes.shape({ + id: PropTypes.string.isRequired, + title: PropTypes.string.isRequired, + description: PropTypes.string.isRequired, + icon: PropTypes.string.isRequired, + path: PropTypes.string.isRequired, + showOnHomePage: PropTypes.bool.isRequired, + category: PropTypes.string.isRequired, + }) + ), }; diff --git a/src/legacy/core_plugins/kibana/public/home/home_ng_wrapper.html b/src/legacy/core_plugins/kibana/public/home/home_ng_wrapper.html deleted file mode 100644 index 645855766fab8e..00000000000000 --- a/src/legacy/core_plugins/kibana/public/home/home_ng_wrapper.html +++ /dev/null @@ -1,5 +0,0 @@ - diff --git a/src/legacy/core_plugins/kibana/public/home/index.js b/src/legacy/core_plugins/kibana/public/home/index.js deleted file mode 100644 index 01f94b8ee4368b..00000000000000 --- a/src/legacy/core_plugins/kibana/public/home/index.js +++ /dev/null @@ -1,61 +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. - */ - -import { getServices } from './kibana_services'; -import template from './home_ng_wrapper.html'; -import { - HomeApp -} from './components/home_app'; -import { i18n } from '@kbn/i18n'; - -const { wrapInI18nContext, uiRoutes, uiModules } = getServices(); - -const app = uiModules.get('apps/home', []); -app.directive('homeApp', function (reactDirective) { - return reactDirective(wrapInI18nContext(HomeApp)); -}); - -const homeTitle = i18n.translate('kbn.home.breadcrumbs.homeTitle', { defaultMessage: 'Home' }); - -function getRoute() { - return { - template, - resolve: { - directories: () => getServices().getFeatureCatalogueEntries() - }, - controller($scope, $route) { - const { chrome, addBasePath } = getServices(); - $scope.directories = $route.current.locals.directories; - $scope.recentlyAccessed = chrome.recentlyAccessed.get().map(item => { - item.link = addBasePath(item.link); - return item; - }); - }, - k7Breadcrumbs: () => [ - { text: homeTitle }, - ] - }; -} - -// All routing will be handled inside HomeApp via react, we just need to make sure angular doesn't -// redirect us to the default page by encountering a url it isn't marked as being able to handle. -uiRoutes.when('/home', getRoute()); -uiRoutes.when('/home/feature_directory', getRoute()); -uiRoutes.when('/home/tutorial_directory/:tab?', getRoute()); -uiRoutes.when('/home/tutorial/:id', getRoute()); diff --git a/src/legacy/core_plugins/kibana/public/home/index.ts b/src/legacy/core_plugins/kibana/public/home/index.ts new file mode 100644 index 00000000000000..4ebf719b86233a --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/home/index.ts @@ -0,0 +1,80 @@ +/* + * 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 { FeatureCatalogueRegistryProvider } from 'ui/registry/feature_catalogue'; +import { npSetup, npStart } from 'ui/new_platform'; +import chrome from 'ui/chrome'; +import { IPrivate } from 'ui/private'; +import { HomePlugin, LegacyAngularInjectedDependencies } from './plugin'; +import { createUiStatsReporter, METRIC_TYPE } from '../../../ui_metric/public'; +import { start as data } from '../../../data/public/legacy'; +import { TelemetryOptInProvider } from '../../../telemetry/public/services'; +import { localApplicationService } from '../local_application_service'; + +export const trackUiMetric = createUiStatsReporter('Kibana_home'); + +/** + * Get dependencies relying on the global angular context. + * They also have to get resolved together with the legacy imports above + */ +async function getAngularDependencies(): Promise { + const injector = await chrome.dangerouslyGetActiveInjector(); + + const Private = injector.get('Private'); + + const telemetryEnabled = npStart.core.injectedMetadata.getInjectedVar('telemetryEnabled'); + const telemetryBanner = npStart.core.injectedMetadata.getInjectedVar('telemetryBanner'); + const telemetryOptInProvider = Private(TelemetryOptInProvider); + + return { + telemetryOptInProvider, + shouldShowTelemetryOptIn: + telemetryEnabled && telemetryBanner && !telemetryOptInProvider.getOptIn(), + }; +} + +let copiedLegacyCatalogue = false; + +(async () => { + const instance = new HomePlugin(); + instance.setup(npSetup.core, { + __LEGACY: { + trackUiMetric, + metadata: npStart.core.injectedMetadata.getLegacyMetadata(), + METRIC_TYPE, + getFeatureCatalogueEntries: async () => { + if (!copiedLegacyCatalogue) { + const injector = await chrome.dangerouslyGetActiveInjector(); + const Private = injector.get('Private'); + // Merge legacy registry with new registry + (Private(FeatureCatalogueRegistryProvider as any) as any).inTitleOrder.map( + npSetup.plugins.feature_catalogue.register + ); + copiedLegacyCatalogue = true; + } + return npStart.plugins.feature_catalogue.get(); + }, + getAngularDependencies, + localApplicationService, + }, + }); + instance.start(npStart.core, { + data, + }); +})(); diff --git a/src/legacy/core_plugins/kibana/public/home/kibana_services.ts b/src/legacy/core_plugins/kibana/public/home/kibana_services.ts index b9f2ae1cfa7e8e..6189204ee4cfc9 100644 --- a/src/legacy/core_plugins/kibana/public/home/kibana_services.ts +++ b/src/legacy/core_plugins/kibana/public/home/kibana_services.ts @@ -17,69 +17,63 @@ * under the License. */ -// @ts-ignore -import { toastNotifications, banners } from 'ui/notify'; -import { kfetch } from 'ui/kfetch'; -import chrome from 'ui/chrome'; +import { + ChromeStart, + DocLinksStart, + HttpStart, + LegacyNavLink, + NotificationsSetup, + OverlayStart, + SavedObjectsClientContract, + UiSettingsClientContract, + UiSettingsState, +} from 'kibana/public'; +import { UiStatsMetricType } from '@kbn/analytics'; +import { FeatureCatalogueEntry } from '../../../../../plugins/feature_catalogue/public'; -import { wrapInI18nContext } from 'ui/i18n'; +export interface HomeKibanaServices { + indexPatternService: any; + getFeatureCatalogueEntries: () => Promise; + metadata: { + app: unknown; + bundleId: string; + nav: LegacyNavLink[]; + version: string; + branch: string; + buildNum: number; + buildSha: string; + basePath: string; + serverName: string; + devMode: boolean; + uiSettings: { defaults: UiSettingsState; user?: UiSettingsState | undefined }; + }; + getInjected: (name: string, defaultValue?: any) => unknown; + chrome: ChromeStart; + telemetryOptInProvider: any; + uiSettings: UiSettingsClientContract; + http: HttpStart; + savedObjectsClient: SavedObjectsClientContract; + toastNotifications: NotificationsSetup['toasts']; + banners: OverlayStart['banners']; + METRIC_TYPE: any; + trackUiMetric: (type: UiStatsMetricType, eventNames: string | string[], count?: number) => void; + getBasePath: () => string; + shouldShowTelemetryOptIn: boolean; + docLinks: DocLinksStart; + addBasePath: (url: string) => string; +} -// @ts-ignore -import { uiModules as modules } from 'ui/modules'; -import routes from 'ui/routes'; -import { npSetup, npStart } from 'ui/new_platform'; -import { IPrivate } from 'ui/private'; -import { FeatureCatalogueRegistryProvider } from 'ui/registry/feature_catalogue'; -import { createUiStatsReporter, METRIC_TYPE } from '../../../ui_metric/public'; -import { TelemetryOptInProvider } from '../../../telemetry/public/services'; -import { start as data } from '../../../data/public/legacy'; +let services: HomeKibanaServices | null = null; -let shouldShowTelemetryOptIn: boolean; -let telemetryOptInProvider: any; +export function setServices(newServices: HomeKibanaServices) { + services = newServices; +} export function getServices() { - return { - getInjected: npStart.core.injectedMetadata.getInjectedVar, - metadata: npStart.core.injectedMetadata.getLegacyMetadata(), - docLinks: npStart.core.docLinks, - - uiRoutes: routes, - uiModules: modules, - - savedObjectsClient: npStart.core.savedObjects.client, - chrome: npStart.core.chrome, - uiSettings: npStart.core.uiSettings, - addBasePath: npStart.core.http.basePath.prepend, - getBasePath: npStart.core.http.basePath.get, - - indexPatternService: data.indexPatterns.indexPatterns, - shouldShowTelemetryOptIn, - telemetryOptInProvider, - getFeatureCatalogueEntries: async () => { - const injector = await chrome.dangerouslyGetActiveInjector(); - const Private = injector.get('Private'); - // Merge legacy registry with new registry - (Private(FeatureCatalogueRegistryProvider as any) as any).inTitleOrder.map( - npSetup.plugins.feature_catalogue.register - ); - return npStart.plugins.feature_catalogue.get(); - }, - - trackUiMetric: createUiStatsReporter('Kibana_home'), - METRIC_TYPE, - - toastNotifications, - banners, - kfetch, - wrapInI18nContext, - }; + if (!services) { + throw new Error( + 'Kibana services not set - are you trying to import this module from outside of the home app?' + ); + } + return services; } - -modules.get('kibana').run((Private: IPrivate) => { - const telemetryEnabled = npStart.core.injectedMetadata.getInjectedVar('telemetryEnabled'); - const telemetryBanner = npStart.core.injectedMetadata.getInjectedVar('telemetryBanner'); - - telemetryOptInProvider = Private(TelemetryOptInProvider); - shouldShowTelemetryOptIn = - telemetryEnabled && telemetryBanner && !telemetryOptInProvider.getOptIn(); -}); diff --git a/src/legacy/core_plugins/kibana/public/home/plugin.ts b/src/legacy/core_plugins/kibana/public/home/plugin.ts new file mode 100644 index 00000000000000..2a2ea371d7f3bd --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/home/plugin.ts @@ -0,0 +1,103 @@ +/* + * 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, CoreStart, LegacyNavLink, Plugin, UiSettingsState } from 'kibana/public'; +import { UiStatsMetricType } from '@kbn/analytics'; + +import { DataStart } from '../../../data/public'; +import { LocalApplicationService } from '../local_application_service'; +import { setServices } from './kibana_services'; +import { FeatureCatalogueEntry } from '../../../../../plugins/feature_catalogue/public'; + +export interface LegacyAngularInjectedDependencies { + telemetryOptInProvider: any; + shouldShowTelemetryOptIn: boolean; +} + +export interface HomePluginStartDependencies { + data: DataStart; +} + +export interface HomePluginSetupDependencies { + __LEGACY: { + trackUiMetric: (type: UiStatsMetricType, eventNames: string | string[], count?: number) => void; + METRIC_TYPE: any; + metadata: { + app: unknown; + bundleId: string; + nav: LegacyNavLink[]; + version: string; + branch: string; + buildNum: number; + buildSha: string; + basePath: string; + serverName: string; + devMode: boolean; + uiSettings: { defaults: UiSettingsState; user?: UiSettingsState | undefined }; + }; + getFeatureCatalogueEntries: () => Promise; + getAngularDependencies: () => Promise; + localApplicationService: LocalApplicationService; + }; +} + +export class HomePlugin implements Plugin { + private dataStart: DataStart | null = null; + private savedObjectsClient: any = null; + + setup( + core: CoreSetup, + { + __LEGACY: { localApplicationService, getAngularDependencies, ...legacyServices }, + }: HomePluginSetupDependencies + ) { + localApplicationService.register({ + id: 'home', + title: 'Home', + mount: async ({ core: contextCore }, params) => { + const angularDependencies = await getAngularDependencies(); + setServices({ + ...legacyServices, + http: contextCore.http, + toastNotifications: core.notifications.toasts, + banners: contextCore.overlays.banners, + getInjected: core.injectedMetadata.getInjectedVar, + docLinks: contextCore.docLinks, + savedObjectsClient: this.savedObjectsClient!, + chrome: contextCore.chrome, + uiSettings: core.uiSettings, + addBasePath: core.http.basePath.prepend, + getBasePath: core.http.basePath.get, + indexPatternService: this.dataStart!.indexPatterns.indexPatterns, + ...angularDependencies, + }); + const { renderApp } = await import('./render_app'); + return await renderApp(params.element); + }, + }); + } + + start(core: CoreStart, { data }: HomePluginStartDependencies) { + // TODO is this really the right way? I though the app context would give us those + this.dataStart = data; + this.savedObjectsClient = core.savedObjects.client; + } + + stop() {} +} diff --git a/src/legacy/core_plugins/kibana/public/home/render_app.tsx b/src/legacy/core_plugins/kibana/public/home/render_app.tsx new file mode 100644 index 00000000000000..a8c35144a45b01 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/home/render_app.tsx @@ -0,0 +1,38 @@ +/* + * 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 React from 'react'; +import { render, unmountComponentAtNode } from 'react-dom'; +import { i18n } from '@kbn/i18n'; +// @ts-ignore +import { HomeApp } from './components/home_app'; +import { getServices } from './kibana_services'; + +export const renderApp = async (element: HTMLElement) => { + const homeTitle = i18n.translate('kbn.home.breadcrumbs.homeTitle', { defaultMessage: 'Home' }); + const { getFeatureCatalogueEntries, chrome } = getServices(); + const directories = await getFeatureCatalogueEntries(); + chrome.setBreadcrumbs([{ text: homeTitle }]); + + render(, element); + + return () => { + unmountComponentAtNode(element); + }; +}; diff --git a/src/legacy/core_plugins/kibana/public/home/sample_data_client.js b/src/legacy/core_plugins/kibana/public/home/sample_data_client.js index 9411373004c25c..eca88604a559da 100644 --- a/src/legacy/core_plugins/kibana/public/home/sample_data_client.js +++ b/src/legacy/core_plugins/kibana/public/home/sample_data_client.js @@ -26,11 +26,11 @@ function clearIndexPatternsCache() { } export async function listSampleDataSets() { - return await getServices().kfetch({ method: 'GET', pathname: sampleDataUrl }); + return await getServices().http.get(sampleDataUrl); } export async function installSampleDataSet(id, sampleDataDefaultIndex) { - await getServices().kfetch({ method: 'POST', pathname: `${sampleDataUrl}/${id}` }); + await getServices().http.post(`${sampleDataUrl}/${id}`); if (getServices().uiSettings.isDefault('defaultIndex')) { getServices().uiSettings.set('defaultIndex', sampleDataDefaultIndex); @@ -40,7 +40,7 @@ export async function installSampleDataSet(id, sampleDataDefaultIndex) { } export async function uninstallSampleDataSet(id, sampleDataDefaultIndex) { - await getServices().kfetch({ method: 'DELETE', pathname: `${sampleDataUrl}/${id}` }); + await getServices().http.delete(`${sampleDataUrl}/${id}`); const uiSettings = getServices().uiSettings; diff --git a/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap b/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap index 08a8cf3898e948..8e7ae33a600682 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap +++ b/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/__snapshots__/field.test.js.snap @@ -463,7 +463,7 @@ exports[`Field for boolean setting should render as read only if saving is disab display="row" error={null} fullWidth={false} - hasChildLabel={true} + hasChildLabel={false} hasEmptyLabelSpace={false} helpText={null} isInvalid={false} @@ -557,7 +557,7 @@ exports[`Field for boolean setting should render as read only with help text if display="row" error={null} fullWidth={false} - hasChildLabel={true} + hasChildLabel={false} hasEmptyLabelSpace={false} helpText={ diff --git a/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/field.js b/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/field.js index e52f789eb8af78..a953d09906ed11 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/field.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/settings/components/field/field.js @@ -195,7 +195,7 @@ export class Field extends PureComponent { this.setState({ unsavedValue: newUnsavedValue, isInvalid, - error + error, }); }; @@ -764,6 +764,7 @@ export class Field extends PureComponent { helpText={this.renderHelpText(setting)} describedByIds={[`${setting.name}-aria`]} className="mgtAdvancedSettings__fieldRow" + hasChildLabel={setting.type !== 'boolean'} > {this.renderField(setting)} diff --git a/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts b/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts index ef9c9a00f980b9..318686b26f6f27 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts +++ b/src/legacy/core_plugins/kibana/public/visualize/embeddable/visualize_embeddable.ts @@ -21,8 +21,11 @@ import _ from 'lodash'; import { EmbeddedVisualizeHandler } from 'ui/visualize/loader/embedded_visualize_handler'; import { Subscription } from 'rxjs'; import * as Rx from 'rxjs'; -import { Filter } from '@kbn/es-query'; -import { TimeRange, onlyDisabledFiltersChanged } from '../../../../../../plugins/data/public'; +import { + TimeRange, + onlyDisabledFiltersChanged, + esFilters, +} from '../../../../../../plugins/data/public'; import { Query } from '../../../../data/public'; import { VISUALIZE_EMBEDDABLE_TYPE } from './constants'; @@ -55,7 +58,7 @@ export interface VisualizeEmbeddableConfiguration { export interface VisualizeInput extends EmbeddableInput { timeRange?: TimeRange; query?: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; vis?: { colors?: { [key: string]: string }; }; @@ -79,7 +82,7 @@ export class VisualizeEmbeddable extends Embeddable { // `config` is used internally and not intended to be set config: Joi.string().default(Joi.ref('$defaultConfigPath')), banner: Joi.boolean().default(true), + lastVersionChecked: Joi.string() + .allow('') + .default(''), url: Joi.when('$dev', { is: true, then: Joi.string().default( @@ -77,7 +80,8 @@ const telemetry = (kibana: any) => { }, }, async replaceInjectedVars(originalInjectedVars: any, request: any) { - const telemetryOptedIn = await getTelemetryOptIn(request); + const currentKibanaVersion = getCurrentKibanaVersion(request.server); + const telemetryOptedIn = await getTelemetryOptIn({ request, currentKibanaVersion }); return { ...originalInjectedVars, @@ -97,7 +101,13 @@ const telemetry = (kibana: any) => { mappings, }, init(server: Server) { - const initializerContext = {} as PluginInitializerContext; + const initializerContext = { + env: { + packageInfo: { + version: getCurrentKibanaVersion(server), + }, + }, + } as PluginInitializerContext; const coreSetup = ({ http: { server }, @@ -116,3 +126,7 @@ const telemetry = (kibana: any) => { // eslint-disable-next-line import/no-default-export export default telemetry; + +function getCurrentKibanaVersion(server: Server): string { + return server.config().get('pkg.version'); +} diff --git a/src/legacy/core_plugins/telemetry/mappings.json b/src/legacy/core_plugins/telemetry/mappings.json index d83f7f59676306..1245ef88f58929 100644 --- a/src/legacy/core_plugins/telemetry/mappings.json +++ b/src/legacy/core_plugins/telemetry/mappings.json @@ -3,6 +3,9 @@ "properties": { "enabled": { "type": "boolean" + }, + "lastVersionChecked": { + "type": "keyword" } } } diff --git a/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.test.ts b/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.test.ts new file mode 100644 index 00000000000000..67ad3aaae427d9 --- /dev/null +++ b/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.test.ts @@ -0,0 +1,214 @@ +/* + * 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 { getTelemetryOptIn } from './get_telemetry_opt_in'; + +describe('get_telemetry_opt_in', () => { + it('returns false when request path is not /app*', async () => { + const params = getCallGetTelemetryOptInParams({ + requestPath: '/foo/bar', + }); + + const result = await callGetTelemetryOptIn(params); + + expect(result).toBe(false); + }); + + it('returns null when saved object not found', async () => { + const params = getCallGetTelemetryOptInParams({ + savedObjectNotFound: true, + }); + + const result = await callGetTelemetryOptIn(params); + + expect(result).toBe(null); + }); + + it('returns false when saved object forbidden', async () => { + const params = getCallGetTelemetryOptInParams({ + savedObjectForbidden: true, + }); + + const result = await callGetTelemetryOptIn(params); + + expect(result).toBe(false); + }); + + it('throws an error on unexpected saved object error', async () => { + const params = getCallGetTelemetryOptInParams({ + savedObjectOtherError: true, + }); + + let threw = false; + try { + await callGetTelemetryOptIn(params); + } catch (err) { + threw = true; + expect(err.message).toBe(SavedObjectOtherErrorMessage); + } + + expect(threw).toBe(true); + }); + + it('returns null if enabled is null or undefined', async () => { + for (const enabled of [null, undefined]) { + const params = getCallGetTelemetryOptInParams({ + enabled, + }); + + const result = await callGetTelemetryOptIn(params); + + expect(result).toBe(null); + } + }); + + it('returns true when enabled is true', async () => { + const params = getCallGetTelemetryOptInParams({ + enabled: true, + }); + + const result = await callGetTelemetryOptIn(params); + + expect(result).toBe(true); + }); + + // build a table of tests with version checks, with results for enabled false + type VersionCheckTable = Array>; + + const EnabledFalseVersionChecks: VersionCheckTable = [ + { lastVersionChecked: '8.0.0', currentKibanaVersion: '8.0.0', result: false }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: '8.0.1', result: false }, + { lastVersionChecked: '8.0.1', currentKibanaVersion: '8.0.0', result: false }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: '8.1.0', result: null }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: '9.0.0', result: null }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: '7.0.0', result: false }, + { lastVersionChecked: '8.1.0', currentKibanaVersion: '8.0.0', result: false }, + { lastVersionChecked: '8.0.0-X', currentKibanaVersion: '8.0.0', result: false }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: '8.0.0-X', result: false }, + { lastVersionChecked: null, currentKibanaVersion: '8.0.0', result: null }, + { lastVersionChecked: undefined, currentKibanaVersion: '8.0.0', result: null }, + { lastVersionChecked: 5, currentKibanaVersion: '8.0.0', result: null }, + { lastVersionChecked: '8.0.0', currentKibanaVersion: 'beta', result: null }, + { lastVersionChecked: 'beta', currentKibanaVersion: '8.0.0', result: null }, + { lastVersionChecked: 'beta', currentKibanaVersion: 'beta', result: false }, + { lastVersionChecked: 'BETA', currentKibanaVersion: 'beta', result: null }, + ].map(el => ({ ...el, enabled: false })); + + // build a table of tests with version checks, with results for enabled true/null/undefined + const EnabledTrueVersionChecks: VersionCheckTable = EnabledFalseVersionChecks.map(el => ({ + ...el, + enabled: true, + result: true, + })); + + const EnabledNullVersionChecks: VersionCheckTable = EnabledFalseVersionChecks.map(el => ({ + ...el, + enabled: null, + result: null, + })); + + const EnabledUndefinedVersionChecks: VersionCheckTable = EnabledFalseVersionChecks.map(el => ({ + ...el, + enabled: undefined, + result: null, + })); + + const AllVersionChecks = [ + ...EnabledFalseVersionChecks, + ...EnabledTrueVersionChecks, + ...EnabledNullVersionChecks, + ...EnabledUndefinedVersionChecks, + ]; + + test.each(AllVersionChecks)( + 'returns expected result for version check with %j', + async (params: Partial) => { + const result = await callGetTelemetryOptIn({ ...DefaultParams, ...params }); + expect(result).toBe(params.result); + } + ); +}); + +interface CallGetTelemetryOptInParams { + requestPath: string; + savedObjectNotFound: boolean; + savedObjectForbidden: boolean; + savedObjectOtherError: boolean; + enabled: boolean | null | undefined; + lastVersionChecked?: any; // should be a string, but test with non-strings + currentKibanaVersion: string; + result?: boolean | null; +} + +const DefaultParams = { + requestPath: '/app/something', + savedObjectNotFound: false, + savedObjectForbidden: false, + savedObjectOtherError: false, + enabled: true, + lastVersionChecked: '8.0.0', + currentKibanaVersion: '8.0.0', +}; + +function getCallGetTelemetryOptInParams( + overrides: Partial +): CallGetTelemetryOptInParams { + return { ...DefaultParams, ...overrides }; +} + +async function callGetTelemetryOptIn(params: CallGetTelemetryOptInParams): Promise { + const { currentKibanaVersion } = params; + const request = getMockRequest(params); + return await getTelemetryOptIn({ request, currentKibanaVersion }); +} + +function getMockRequest(params: CallGetTelemetryOptInParams): any { + return { + path: params.requestPath, + getSavedObjectsClient() { + return getMockSavedObjectsClient(params); + }, + }; +} + +const SavedObjectNotFoundMessage = 'savedObjectNotFound'; +const SavedObjectForbiddenMessage = 'savedObjectForbidden'; +const SavedObjectOtherErrorMessage = 'savedObjectOtherError'; + +function getMockSavedObjectsClient(params: CallGetTelemetryOptInParams) { + return { + async get(type: string, id: string) { + if (params.savedObjectNotFound) throw new Error(SavedObjectNotFoundMessage); + if (params.savedObjectForbidden) throw new Error(SavedObjectForbiddenMessage); + if (params.savedObjectOtherError) throw new Error(SavedObjectOtherErrorMessage); + + const enabled = params.enabled; + const lastVersionChecked = params.lastVersionChecked; + return { attributes: { enabled, lastVersionChecked } }; + }, + errors: { + isNotFoundError(error: any) { + return error.message === SavedObjectNotFoundMessage; + }, + isForbiddenError(error: any) { + return error.message === SavedObjectForbiddenMessage; + }, + }, + }; +} diff --git a/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.ts b/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.ts index 9b365d6dd7ae51..c8bd4a4b6dfbd4 100644 --- a/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.ts +++ b/src/legacy/core_plugins/telemetry/server/get_telemetry_opt_in.ts @@ -17,7 +17,21 @@ * under the License. */ -export async function getTelemetryOptIn(request: any) { +import semver from 'semver'; + +import { SavedObjectAttributes } from './routes/opt_in'; + +interface GetTelemetryOptIn { + request: any; + currentKibanaVersion: string; +} + +// Returns whether telemetry has been opt'ed into or not. +// Returns null not set, meaning Kibana should prompt in the UI. +export async function getTelemetryOptIn({ + request, + currentKibanaVersion, +}: GetTelemetryOptIn): Promise { const isRequestingApplication = request.path.startsWith('/app'); // Prevent interstitial screens (such as the space selector) from prompting for telemetry @@ -27,9 +41,9 @@ export async function getTelemetryOptIn(request: any) { const savedObjectsClient = request.getSavedObjectsClient(); + let savedObject; try { - const { attributes } = await savedObjectsClient.get('telemetry', 'telemetry'); - return attributes.enabled; + savedObject = await savedObjectsClient.get('telemetry', 'telemetry'); } catch (error) { if (savedObjectsClient.errors.isNotFoundError(error)) { return null; @@ -43,4 +57,50 @@ export async function getTelemetryOptIn(request: any) { throw error; } + + const { attributes }: { attributes: SavedObjectAttributes } = savedObject; + + // if enabled is already null, return null + if (attributes.enabled == null) return null; + + const enabled = !!attributes.enabled; + + // if enabled is true, return it + if (enabled === true) return enabled; + + // Additional check if they've already opted out (enabled: false): + // - if the Kibana version has changed by at least a minor version, + // return null to re-prompt. + + const lastKibanaVersion = attributes.lastVersionChecked; + + // if the last kibana version isn't set, or is somehow not a string, return null + if (typeof lastKibanaVersion !== 'string') return null; + + // if version hasn't changed, just return enabled value + if (lastKibanaVersion === currentKibanaVersion) return enabled; + + const lastSemver = parseSemver(lastKibanaVersion); + const currentSemver = parseSemver(currentKibanaVersion); + + // if either version is invalid, return null + if (lastSemver == null || currentSemver == null) return null; + + // actual major/minor version comparison, for cases when to return null + if (currentSemver.major > lastSemver.major) return null; + if (currentSemver.major === lastSemver.major) { + if (currentSemver.minor > lastSemver.minor) return null; + } + + // current version X.Y is not greater than last version X.Y, return enabled + return enabled; +} + +function parseSemver(version: string): semver.SemVer | null { + // semver functions both return nulls AND throw exceptions: "it depends!" + try { + return semver.parse(version); + } catch (err) { + return null; + } } diff --git a/src/legacy/core_plugins/telemetry/server/index.ts b/src/legacy/core_plugins/telemetry/server/index.ts index b8ae5fc231fba6..aa13fab9a5f819 100644 --- a/src/legacy/core_plugins/telemetry/server/index.ts +++ b/src/legacy/core_plugins/telemetry/server/index.ts @@ -25,5 +25,5 @@ export { getTelemetryOptIn } from './get_telemetry_opt_in'; export { telemetryCollectionManager } from './collection_manager'; export const telemetryPlugin = (initializerContext: PluginInitializerContext) => - new TelemetryPlugin(); + new TelemetryPlugin(initializerContext); export { constants }; diff --git a/src/legacy/core_plugins/telemetry/server/plugin.ts b/src/legacy/core_plugins/telemetry/server/plugin.ts index 70de51b2abe995..a5f0f1234799a4 100644 --- a/src/legacy/core_plugins/telemetry/server/plugin.ts +++ b/src/legacy/core_plugins/telemetry/server/plugin.ts @@ -17,14 +17,21 @@ * under the License. */ -import { CoreSetup } from 'src/core/server'; +import { CoreSetup, PluginInitializerContext } from 'src/core/server'; import { registerRoutes } from './routes'; import { telemetryCollectionManager } from './collection_manager'; import { getStats } from './telemetry_collection'; export class TelemetryPlugin { + private readonly currentKibanaVersion: string; + + constructor(initializerContext: PluginInitializerContext) { + this.currentKibanaVersion = initializerContext.env.packageInfo.version; + } + public setup(core: CoreSetup) { + const currentKibanaVersion = this.currentKibanaVersion; telemetryCollectionManager.setStatsGetter(getStats, 'local'); - registerRoutes(core); + registerRoutes({ core, currentKibanaVersion }); } } diff --git a/src/legacy/core_plugins/telemetry/server/routes/index.ts b/src/legacy/core_plugins/telemetry/server/routes/index.ts index 12ba541d699f9f..2eb6bf95b4f45e 100644 --- a/src/legacy/core_plugins/telemetry/server/routes/index.ts +++ b/src/legacy/core_plugins/telemetry/server/routes/index.ts @@ -21,7 +21,12 @@ import { CoreSetup } from 'src/core/server'; import { registerOptInRoutes } from './opt_in'; import { registerTelemetryDataRoutes } from './telemetry_stats'; -export function registerRoutes(core: CoreSetup) { - registerOptInRoutes(core); +interface RegisterRoutesParams { + core: CoreSetup; + currentKibanaVersion: string; +} + +export function registerRoutes({ core, currentKibanaVersion }: RegisterRoutesParams) { + registerOptInRoutes({ core, currentKibanaVersion }); registerTelemetryDataRoutes(core); } diff --git a/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts b/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts index aabc0259f08fc2..3a7194890b5700 100644 --- a/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts +++ b/src/legacy/core_plugins/telemetry/server/routes/opt_in.ts @@ -21,7 +21,17 @@ import Joi from 'joi'; import { boomify } from 'boom'; import { CoreSetup } from 'src/core/server'; -export function registerOptInRoutes(core: CoreSetup) { +interface RegisterOptInRoutesParams { + core: CoreSetup; + currentKibanaVersion: string; +} + +export interface SavedObjectAttributes { + enabled?: boolean; + lastVersionChecked: string; +} + +export function registerOptInRoutes({ core, currentKibanaVersion }: RegisterOptInRoutesParams) { const { server } = core.http as any; server.route({ @@ -36,17 +46,16 @@ export function registerOptInRoutes(core: CoreSetup) { }, handler: async (req: any, h: any) => { const savedObjectsClient = req.getSavedObjectsClient(); + const savedObject: SavedObjectAttributes = { + enabled: req.payload.enabled, + lastVersionChecked: currentKibanaVersion, + }; + const options = { + id: 'telemetry', + overwrite: true, + }; try { - await savedObjectsClient.create( - 'telemetry', - { - enabled: req.payload.enabled, - }, - { - id: 'telemetry', - overwrite: true, - } - ); + await savedObjectsClient.create('telemetry', savedObject, options); } catch (err) { return boomify(err); } diff --git a/src/legacy/core_plugins/timelion/public/vis/timelion_request_handler.ts b/src/legacy/core_plugins/timelion/public/vis/timelion_request_handler.ts index eb4fb3f397149f..156c06a6055287 100644 --- a/src/legacy/core_plugins/timelion/public/vis/timelion_request_handler.ts +++ b/src/legacy/core_plugins/timelion/public/vis/timelion_request_handler.ts @@ -18,12 +18,12 @@ */ // @ts-ignore -import { buildEsQuery, getEsQueryConfig, Filter } from '@kbn/es-query'; +import { buildEsQuery, getEsQueryConfig } from '@kbn/es-query'; // @ts-ignore import { timezoneProvider } from 'ui/vis/lib/timezone'; import { KIBANA_CONTEXT_NAME } from 'src/plugins/expressions/public'; import { Query } from 'src/legacy/core_plugins/data/public'; -import { TimeRange } from 'src/plugins/data/public'; +import { TimeRange, esFilters } from 'src/plugins/data/public'; import { VisParams } from 'ui/vis'; import { i18n } from '@kbn/i18n'; import { TimelionVisualizationDependencies } from '../plugin'; @@ -60,7 +60,7 @@ export function getTimelionRequestHandler(dependencies: TimelionVisualizationDep visParams, }: { timeRange: TimeRange; - filters: Filter[]; + filters: esFilters.Filter[]; query: Query; visParams: VisParams; forceFetch?: boolean; diff --git a/src/legacy/core_plugins/vis_type_timeseries/server/lib/get_index_pattern_service.js b/src/legacy/core_plugins/vis_type_timeseries/server/lib/get_index_pattern_service.js index 0766e6a48765f9..54e90ab7dd9b7f 100644 --- a/src/legacy/core_plugins/vis_type_timeseries/server/lib/get_index_pattern_service.js +++ b/src/legacy/core_plugins/vis_type_timeseries/server/lib/get_index_pattern_service.js @@ -17,7 +17,7 @@ * under the License. */ -import { IndexPatternsService } from '../../../../server/index_patterns/service'; +import { IndexPatternsFetcher } from '../../../../../plugins/data/server/'; export const getIndexPatternService = { assign: 'indexPatternsService', method(req) { @@ -25,6 +25,6 @@ export const getIndexPatternService = { const callDataCluster = (...args) => { return dataCluster.callWithRequest(req, ...args); }; - return new IndexPatternsService(callDataCluster); + return new IndexPatternsFetcher(callDataCluster); }, }; diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts index 22a71bd999d54e..b4c32f37eb90c1 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts @@ -16,13 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter } from '@kbn/es-query'; + import { timefilter } from 'ui/timefilter'; import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; - -// @ts-ignore import { buildEsQuery, getEsQueryConfig } from '@kbn/es-query'; + +import { esFilters } from '../../../../plugins/data/public'; + // @ts-ignore import { VegaParser } from './data_model/vega_parser'; // @ts-ignore @@ -35,7 +36,7 @@ import { VisParams } from './vega_fn'; interface VegaRequestHandlerParams { query: Query; - filters: Filter; + filters: esFilters.Filter; timeRange: TimeRange; visParams: VisParams; } diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js b/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js index ec898080c581e8..5abb99e9bf716a 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js @@ -27,7 +27,7 @@ import { Utils } from '../data_model/utils'; import { VISUALIZATION_COLORS } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { TooltipHandler } from './vega_tooltip'; -import { buildQueryFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../plugins/data/public'; import { getEnableExternalUrls } from '../helpers/vega_config_provider'; @@ -263,7 +263,7 @@ export class VegaBaseView { */ async addFilterHandler(query, index) { const indexId = await this._findIndex(index); - const filter = buildQueryFilter(query, indexId); + const filter = esFilters.buildQueryFilter(query, indexId); this._queryfilter.addFilters(filter); } @@ -274,7 +274,7 @@ export class VegaBaseView { async removeFilterHandler(query, index) { const $injector = await chrome.dangerouslyGetActiveInjector(); const indexId = await this._findIndex(index); - const filter = buildQueryFilter(query, indexId); + const filter = esFilters.buildQueryFilter(query, indexId); // This is a workaround for the https://github.com/elastic/kibana/issues/18863 // Once fixed, replace with a direct call (no await is needed because its not async) diff --git a/src/legacy/server/index_patterns/index.ts b/src/legacy/server/index_patterns/index.ts index eea8a4a998e2dc..75d0038cf9023e 100644 --- a/src/legacy/server/index_patterns/index.ts +++ b/src/legacy/server/index_patterns/index.ts @@ -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'; diff --git a/src/legacy/server/index_patterns/mixin.ts b/src/legacy/server/index_patterns/mixin.ts index a7180d6a2d70ec..6b04c3842007bd 100644 --- a/src/legacy/server/index_patterns/mixin.ts +++ b/src/legacy/server/index_patterns/mixin.ts @@ -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) { /** @@ -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); }); /** @@ -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; -}) => IndexPatternsService; +}) => IndexPatternsFetcher; diff --git a/src/legacy/server/kbn_server.d.ts b/src/legacy/server/kbn_server.d.ts index e7f2f4c85435ff..9cc4e30d4252dd 100644 --- a/src/legacy/server/kbn_server.d.ts +++ b/src/legacy/server/kbn_server.d.ts @@ -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'; diff --git a/src/legacy/server/usage/README.md b/src/legacy/server/usage/README.md index ad1bb822b70cda..5c4bcc05bbc383 100644 --- a/src/legacy/server/usage/README.md +++ b/src/legacy/server/usage/README.md @@ -90,5 +90,3 @@ There are a few ways you can test that your usage collector is working properly. Yes. When you talk to the Platform team about new fields being added, point out specifically which properties will have dynamic inner fields. 5. **If I accumulate an event counter in server memory, which my fetch method returns, won't it reset when the Kibana server restarts?** Yes, but that is not a major concern. A visualization on such info might be a date histogram that gets events-per-second or something, which would be impacted by server restarts, so we'll have to offset the beginning of the time range when we detect that the latest metric is smaller than the earliest metric. That would be a pretty custom visualization, but perhaps future Kibana enhancements will be able to support that. -6. **Who can I talk to with more questions?** - The Kibana Platform team is the owner of the telemetry service. You can bring questions to them. You can also talk to Tim Sullivan, who created the Kibana telemetry service, or Chris Earle, who set up the telemetry cluster and AWS Lambas for the upstream prod and staging endpoints that recieve the data sent from end-user browsers. diff --git a/src/legacy/ui/public/agg_types/buckets/_terms_other_bucket_helper.js b/src/legacy/ui/public/agg_types/buckets/_terms_other_bucket_helper.js index 5dfa2e3d6ae3f6..70bca2e40ae3fa 100644 --- a/src/legacy/ui/public/agg_types/buckets/_terms_other_bucket_helper.js +++ b/src/legacy/ui/public/agg_types/buckets/_terms_other_bucket_helper.js @@ -18,8 +18,9 @@ */ import _ from 'lodash'; -import { buildExistsFilter, buildPhrasesFilter, buildQueryFromFilters } from '@kbn/es-query'; +import { buildQueryFromFilters } from '@kbn/es-query'; import { AggGroupNames } from '../../vis/editors/default/agg_groups'; +import { esFilters } from '../../../../../plugins/data/public'; /** * walks the aggregation DSL and returns DSL starting at aggregation with id of startFromAggId @@ -180,7 +181,7 @@ export const buildOtherBucketAgg = (aggConfigs, aggWithOtherBucket, response) => agg.buckets.some(bucket => bucket.key === '__missing__') ) { filters.push( - buildExistsFilter( + esFilters.buildExistsFilter( aggWithOtherBucket.params.field, aggWithOtherBucket.params.field.indexPattern ) @@ -232,7 +233,7 @@ export const mergeOtherBucketAggResponse = ( ); const requestFilterTerms = getOtherAggTerms(requestAgg, key, otherAgg); - const phraseFilter = buildPhrasesFilter( + const phraseFilter = esFilters.buildPhrasesFilter( otherAgg.params.field, requestFilterTerms, otherAgg.params.field.indexPattern @@ -243,7 +244,7 @@ export const mergeOtherBucketAggResponse = ( if (aggResultBuckets.some(bucket => bucket.key === '__missing__')) { bucket.filters.push( - buildExistsFilter(otherAgg.params.field, otherAgg.params.field.indexPattern) + esFilters.buildExistsFilter(otherAgg.params.field, otherAgg.params.field.indexPattern) ); } aggResultBuckets.push(bucket); diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.test.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.test.ts index f67fa55b278598..9426df7d34c29e 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.test.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.test.ts @@ -18,19 +18,19 @@ */ import moment from 'moment'; -import { RangeFilter } from '@kbn/es-query'; import { createFilterDateHistogram } from './date_histogram'; import { intervalOptions } from '../_interval_options'; import { AggConfigs } from '../../agg_configs'; import { IBucketDateHistogramAggConfig } from '../date_histogram'; import { BUCKET_TYPES } from '../bucket_agg_types'; +import { esFilters } from '../../../../../../plugins/data/public'; jest.mock('ui/new_platform'); describe('AggConfig Filters', () => { describe('date_histogram', () => { let agg: IBucketDateHistogramAggConfig; - let filter: RangeFilter; + let filter: esFilters.RangeFilter; let bucketStart: any; let field: any; diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.ts index 8c6140cc4b37ad..f91a92eab1c33f 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/date_histogram.ts @@ -18,8 +18,8 @@ */ import moment from 'moment'; -import { buildRangeFilter } from '@kbn/es-query'; import { IBucketDateHistogramAggConfig } from '../date_histogram'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterDateHistogram = ( agg: IBucketDateHistogramAggConfig, @@ -28,7 +28,7 @@ export const createFilterDateHistogram = ( const start = moment(key); const interval = agg.buckets.getInterval(); - return buildRangeFilter( + return esFilters.buildRangeFilter( agg.params.field, { gte: start.toISOString(), diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/date_range.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/date_range.ts index cd4b0ffc215b07..01689d954a0723 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/date_range.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/date_range.ts @@ -17,16 +17,16 @@ * under the License. */ -import { buildRangeFilter, RangeFilterParams } from '@kbn/es-query'; import moment from 'moment'; import { IBucketAggConfig } from '../_bucket_agg_type'; import { DateRangeKey } from '../date_range'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterDateRange = (agg: IBucketAggConfig, { from, to }: DateRangeKey) => { - const filter: RangeFilterParams = {}; + const filter: esFilters.RangeFilterParams = {}; if (from) filter.gte = moment(from).toISOString(); if (to) filter.lt = moment(to).toISOString(); if (to && from) filter.format = 'strict_date_optional_time'; - return buildRangeFilter(agg.params.field, filter, agg.getIndexPattern()); + return esFilters.buildRangeFilter(agg.params.field, filter, agg.getIndexPattern()); }; diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/filters.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/filters.ts index bf30b333056bcf..6b614514580b6d 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/filters.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/filters.ts @@ -18,8 +18,8 @@ */ import { get } from 'lodash'; -import { buildQueryFilter } from '@kbn/es-query'; import { IBucketAggConfig } from '../_bucket_agg_type'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterFilters = (aggConfig: IBucketAggConfig, key: string) => { // have the aggConfig write agg dsl params @@ -28,6 +28,6 @@ export const createFilterFilters = (aggConfig: IBucketAggConfig, key: string) => const indexPattern = aggConfig.getIndexPattern(); if (filter && indexPattern && indexPattern.id) { - return buildQueryFilter(filter.query, indexPattern.id, key); + return esFilters.buildQueryFilter(filter.query, indexPattern.id, key); } }; diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/histogram.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/histogram.ts index 37d5c6bc8adc1f..fc587fa9ecdb6b 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/histogram.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/histogram.ts @@ -17,14 +17,14 @@ * under the License. */ -import { buildRangeFilter, RangeFilterParams } from '@kbn/es-query'; import { IBucketAggConfig } from '../_bucket_agg_type'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterHistogram = (aggConfig: IBucketAggConfig, key: string) => { const value = parseInt(key, 10); - const params: RangeFilterParams = { gte: value, lt: value + aggConfig.params.interval }; + const params: esFilters.RangeFilterParams = { gte: value, lt: value + aggConfig.params.interval }; - return buildRangeFilter( + return esFilters.buildRangeFilter( aggConfig.params.field, params, aggConfig.getIndexPattern(), diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/ip_range.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/ip_range.ts index 83769578725f29..803f6d97ae42d3 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/ip_range.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/ip_range.ts @@ -17,13 +17,13 @@ * under the License. */ -import { buildRangeFilter, RangeFilterParams } from '@kbn/es-query'; import { CidrMask } from '../../../utils/cidr_mask'; import { IBucketAggConfig } from '../_bucket_agg_type'; import { IpRangeKey } from '../ip_range'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterIpRange = (aggConfig: IBucketAggConfig, key: IpRangeKey) => { - let range: RangeFilterParams; + let range: esFilters.RangeFilterParams; if (key.type === 'mask') { range = new CidrMask(key.mask).getRange(); @@ -34,7 +34,7 @@ export const createFilterIpRange = (aggConfig: IBucketAggConfig, key: IpRangeKey }; } - return buildRangeFilter( + return esFilters.buildRangeFilter( aggConfig.params.field, { gte: range.from, lte: range.to }, aggConfig.getIndexPattern() diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/range.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/range.ts index cf2c83884651a1..929827c6e3fec6 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/range.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/range.ts @@ -17,11 +17,11 @@ * under the License. */ -import { buildRangeFilter } from '@kbn/es-query'; import { IBucketAggConfig } from '../_bucket_agg_type'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterRange = (aggConfig: IBucketAggConfig, params: any) => { - return buildRangeFilter( + return esFilters.buildRangeFilter( aggConfig.params.field, params, aggConfig.getIndexPattern(), diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/terms.test.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/terms.test.ts index b00e939eac8d86..42f8349d5a2a3d 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/terms.test.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/terms.test.ts @@ -16,10 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -import { ExistsFilter, Filter } from '@kbn/es-query'; + import { createFilterTerms } from './terms'; import { AggConfigs } from '../../agg_configs'; import { BUCKET_TYPES } from '../bucket_agg_types'; +import { esFilters } from '../../../../../../plugins/data/public'; jest.mock('ui/new_platform'); @@ -48,7 +49,7 @@ describe('AggConfig Filters', () => { { type: BUCKET_TYPES.TERMS, schema: 'segment', params: { field: 'field' } }, ]); - const filter = createFilterTerms(aggConfigs.aggs[0], 'apache', {}) as Filter; + const filter = createFilterTerms(aggConfigs.aggs[0], 'apache', {}) as esFilters.Filter; expect(filter).toHaveProperty('query'); expect(filter.query).toHaveProperty('match_phrase'); @@ -63,14 +64,14 @@ describe('AggConfig Filters', () => { { type: BUCKET_TYPES.TERMS, schema: 'segment', params: { field: 'field' } }, ]); - const filterFalse = createFilterTerms(aggConfigs.aggs[0], '', {}) as Filter; + const filterFalse = createFilterTerms(aggConfigs.aggs[0], '', {}) as esFilters.Filter; expect(filterFalse).toHaveProperty('query'); expect(filterFalse.query).toHaveProperty('match_phrase'); expect(filterFalse.query.match_phrase).toHaveProperty('field'); expect(filterFalse.query.match_phrase.field).toBeFalsy(); - const filterTrue = createFilterTerms(aggConfigs.aggs[0], '1', {}) as Filter; + const filterTrue = createFilterTerms(aggConfigs.aggs[0], '1', {}) as esFilters.Filter; expect(filterTrue).toHaveProperty('query'); expect(filterTrue.query).toHaveProperty('match_phrase'); @@ -82,7 +83,11 @@ describe('AggConfig Filters', () => { const aggConfigs = getAggConfigs([ { type: BUCKET_TYPES.TERMS, schema: 'segment', params: { field: 'field' } }, ]); - const filter = createFilterTerms(aggConfigs.aggs[0], '__missing__', {}) as ExistsFilter; + const filter = createFilterTerms( + aggConfigs.aggs[0], + '__missing__', + {} + ) as esFilters.ExistsFilter; expect(filter).toHaveProperty('exists'); expect(filter.exists).toHaveProperty('field', 'field'); @@ -98,7 +103,7 @@ describe('AggConfig Filters', () => { const [filter] = createFilterTerms(aggConfigs.aggs[0], '__other__', { terms: ['apache'], - }) as Filter[]; + }) as esFilters.Filter[]; expect(filter).toHaveProperty('query'); expect(filter.query).toHaveProperty('bool'); diff --git a/src/legacy/ui/public/agg_types/buckets/create_filter/terms.ts b/src/legacy/ui/public/agg_types/buckets/create_filter/terms.ts index e5d4406c752c7c..5bd770e672786d 100644 --- a/src/legacy/ui/public/agg_types/buckets/create_filter/terms.ts +++ b/src/legacy/ui/public/agg_types/buckets/create_filter/terms.ts @@ -17,8 +17,8 @@ * under the License. */ -import { Filter, buildPhraseFilter, buildPhrasesFilter, buildExistsFilter } from '@kbn/es-query'; import { IBucketAggConfig } from '../_bucket_agg_type'; +import { esFilters } from '../../../../../../plugins/data/public'; export const createFilterTerms = (aggConfig: IBucketAggConfig, key: string, params: any) => { const field = aggConfig.params.field; @@ -27,20 +27,20 @@ export const createFilterTerms = (aggConfig: IBucketAggConfig, key: string, para if (key === '__other__') { const terms = params.terms; - const phraseFilter = buildPhrasesFilter(field, terms, indexPattern); + const phraseFilter = esFilters.buildPhrasesFilter(field, terms, indexPattern); phraseFilter.meta.negate = true; - const filters: Filter[] = [phraseFilter]; + const filters: esFilters.Filter[] = [phraseFilter]; if (terms.some((term: string) => term === '__missing__')) { - filters.push(buildExistsFilter(field, indexPattern)); + filters.push(esFilters.buildExistsFilter(field, indexPattern)); } return filters; } else if (key === '__missing__') { - const existsFilter = buildExistsFilter(field, indexPattern); + const existsFilter = esFilters.buildExistsFilter(field, indexPattern); existsFilter.meta.negate = true; return existsFilter; } - return buildPhraseFilter(field, key, indexPattern); + return esFilters.buildPhraseFilter(field, key, indexPattern); }; diff --git a/src/legacy/ui/public/agg_types/buckets/date_histogram.ts b/src/legacy/ui/public/agg_types/buckets/date_histogram.ts index 4c2e67f758a7e2..e86d561a1c79bd 100644 --- a/src/legacy/ui/public/agg_types/buckets/date_histogram.ts +++ b/src/legacy/ui/public/agg_types/buckets/date_histogram.ts @@ -32,7 +32,6 @@ import { ScaleMetricsParamEditor } from '../../vis/editors/default/controls/scal import { dateHistogramInterval } from '../../../../core_plugins/data/public'; import { writeParams } from '../agg_params'; import { AggConfigs } from '../agg_configs'; -import { AggConfig } from '../agg_config'; import { isMetricAggType } from '../metrics/metric_agg_type'; import { KBN_FIELD_TYPES } from '../../../../../plugins/data/common'; @@ -189,7 +188,7 @@ export const dateHistogramBucketAgg = new BucketAggType isMetricAggType(a.type)); - const all = _.every(metrics, (a: AggConfig) => { + const all = _.every(metrics, (a: IBucketAggConfig) => { const { type } = a; if (isMetricAggType(type)) { diff --git a/src/legacy/ui/public/agg_types/buckets/date_range.ts b/src/legacy/ui/public/agg_types/buckets/date_range.ts index dd7f0cb972ae22..4de6002e2e3746 100644 --- a/src/legacy/ui/public/agg_types/buckets/date_range.ts +++ b/src/legacy/ui/public/agg_types/buckets/date_range.ts @@ -21,9 +21,8 @@ import moment from 'moment-timezone'; import { i18n } from '@kbn/i18n'; import { npStart } from 'ui/new_platform'; import { BUCKET_TYPES } from './bucket_agg_types'; -import { BucketAggType } from './_bucket_agg_type'; +import { BucketAggType, IBucketAggConfig } from './_bucket_agg_type'; import { createFilterDateRange } from './create_filter/date_range'; -import { AggConfig } from '../agg_config'; import { FieldFormat } from '../../../../../plugins/data/common/field_formats'; import { DateRangesParamEditor } from '../../vis/editors/default/controls/date_ranges'; @@ -64,7 +63,7 @@ export const dateRangeBucketAgg = new BucketAggType({ name: 'field', type: 'field', filterFieldTypes: KBN_FIELD_TYPES.DATE, - default(agg: AggConfig) { + default(agg: IBucketAggConfig) { return agg.getIndexPattern().timeFieldName; }, }, @@ -83,7 +82,7 @@ export const dateRangeBucketAgg = new BucketAggType({ default: undefined, // Implimentation method is the same as that of date_histogram serialize: () => undefined, - write: (agg: AggConfig, output: Record) => { + write: (agg: IBucketAggConfig, output: Record) => { const field = agg.getParam('field'); let tz = agg.getParam('time_zone'); diff --git a/src/legacy/ui/public/agg_types/buckets/geo_hash.ts b/src/legacy/ui/public/agg_types/buckets/geo_hash.ts index 555aa94b636b84..1716891231b838 100644 --- a/src/legacy/ui/public/agg_types/buckets/geo_hash.ts +++ b/src/legacy/ui/public/agg_types/buckets/geo_hash.ts @@ -26,7 +26,6 @@ import { IsFilteredByCollarParamEditor } from '../../vis/editors/default/control import { PrecisionParamEditor } from '../../vis/editors/default/controls/precision'; import { geohashColumns } from '../../utils/decode_geo_hash'; import { AggGroupNames } from '../../vis/editors/default/agg_groups'; -import { AggConfig } from '../agg_config'; import { KBN_FIELD_TYPES } from '../../../../../plugins/data/common'; // @ts-ignore @@ -143,7 +142,7 @@ export const geoHashBucketAgg = new BucketAggType({ }, ], getRequestAggs(agg) { - const aggs: AggConfig[] = []; + const aggs: IBucketAggConfig[] = []; const params = agg.params; if (params.isFilteredByCollar && agg.getField()) { diff --git a/src/legacy/ui/public/agg_types/buckets/histogram.ts b/src/legacy/ui/public/agg_types/buckets/histogram.ts index 74a2da4a0eb674..fba2f47010c34e 100644 --- a/src/legacy/ui/public/agg_types/buckets/histogram.ts +++ b/src/legacy/ui/public/agg_types/buckets/histogram.ts @@ -28,7 +28,6 @@ import { NumberIntervalParamEditor } from '../../vis/editors/default/controls/nu import { MinDocCountParamEditor } from '../../vis/editors/default/controls/min_doc_count'; import { HasExtendedBoundsParamEditor } from '../../vis/editors/default/controls/has_extended_bounds'; import { ExtendedBoundsParamEditor } from '../../vis/editors/default/controls/extended_bounds'; -import { AggConfig } from '../agg_config'; import { KBN_FIELD_TYPES } from '../../../../../plugins/data/common'; import { BUCKET_TYPES } from './bucket_agg_types'; @@ -177,7 +176,7 @@ export const histogramBucketAgg = new BucketAggType({ name: 'min_doc_count', default: false, editorComponent: MinDocCountParamEditor, - write(aggConfig: AggConfig, output: Record) { + write(aggConfig: IBucketAggConfig, output: Record) { if (aggConfig.params.min_doc_count) { output.params.min_doc_count = 0; } else { @@ -198,14 +197,14 @@ export const histogramBucketAgg = new BucketAggType({ max: '', }, editorComponent: ExtendedBoundsParamEditor, - write(aggConfig: AggConfig, output: Record) { + write(aggConfig: IBucketAggConfig, output: Record) { const { min, max } = aggConfig.params.extended_bounds; if (aggConfig.params.has_extended_bounds && (min || min === 0) && (max || max === 0)) { output.params.extended_bounds = { min, max }; } }, - shouldShow: (aggConfig: AggConfig) => aggConfig.params.has_extended_bounds, + shouldShow: (aggConfig: IBucketAggConfig) => aggConfig.params.has_extended_bounds, }, ], }); diff --git a/src/legacy/ui/public/agg_types/buckets/migrate_include_exclude_format.ts b/src/legacy/ui/public/agg_types/buckets/migrate_include_exclude_format.ts index 2bf0930d376845..e4527ff87f48cc 100644 --- a/src/legacy/ui/public/agg_types/buckets/migrate_include_exclude_format.ts +++ b/src/legacy/ui/public/agg_types/buckets/migrate_include_exclude_format.ts @@ -18,7 +18,6 @@ */ import { isString, isObject } from 'lodash'; -import { AggConfig } from 'ui/agg_types'; import { IBucketAggConfig, BucketAggType, BucketAggParam } from './_bucket_agg_type'; export const isType = (type: string) => { @@ -32,12 +31,16 @@ export const isType = (type: string) => { export const isStringType = isType('string'); export const migrateIncludeExcludeFormat = { - serialize(this: BucketAggParam, value: any, agg: AggConfig) { + serialize(this: BucketAggParam, value: any, agg: IBucketAggConfig) { if (this.shouldShow && !this.shouldShow(agg)) return; if (!value || isString(value)) return value; else return value.pattern; }, - write(this: BucketAggType, aggConfig: AggConfig, output: Record) { + write( + this: BucketAggType, + aggConfig: IBucketAggConfig, + output: Record + ) { const value = aggConfig.getParam(this.name); if (isObject(value)) { diff --git a/src/legacy/ui/public/chrome/directives/kbn_chrome.html b/src/legacy/ui/public/chrome/directives/kbn_chrome.html index 541082e68de58d..7738093fe9dea7 100644 --- a/src/legacy/ui/public/chrome/directives/kbn_chrome.html +++ b/src/legacy/ui/public/chrome/directives/kbn_chrome.html @@ -1,4 +1,4 @@ -
+
( }); $rootScope.$on('$routeChangeSuccess', () => { + if (isDummyWrapperRoute($route)) { + return; + } const current = $route.current || {}; if (helpExtensionSetSinceRouteChange || (current.$$route && current.$$route.redirectTo)) { diff --git a/src/legacy/ui/public/vis/editors/default/components/agg_group.tsx b/src/legacy/ui/public/vis/editors/default/components/agg_group.tsx index 6d3b5fc4e63557..528914f4fd006b 100644 --- a/src/legacy/ui/public/vis/editors/default/components/agg_group.tsx +++ b/src/legacy/ui/public/vis/editors/default/components/agg_group.tsx @@ -26,7 +26,9 @@ import { EuiDraggable, EuiSpacer, EuiPanel, + EuiFormErrorText, } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; import { AggConfig } from '../../../../agg_types/agg_config'; import { aggGroupNamesMap, AggGroupNames } from '../agg_groups'; @@ -80,7 +82,15 @@ function DefaultEditorAggGroup({ const [aggsState, setAggsState] = useReducer(aggGroupReducer, group, initAggsState); - const isGroupValid = Object.values(aggsState).every(item => item.valid); + const bucketsError = + lastParentPipelineAggTitle && groupName === AggGroupNames.Buckets && !group.length + ? i18n.translate('common.ui.aggTypes.buckets.mustHaveBucketErrorMessage', { + defaultMessage: 'Add a bucket with "Date Histogram" or "Histogram" aggregation.', + description: 'Date Histogram and Histogram should not be translated', + }) + : undefined; + + const isGroupValid = !bucketsError && Object.values(aggsState).every(item => item.valid); const isAllAggsTouched = isInvalidAggsTouched(aggsState); const isMetricAggregationDisabled = useMemo( () => groupName === AggGroupNames.Metrics && getEnabledMetricAggsCount(group) === 1, @@ -144,6 +154,12 @@ function DefaultEditorAggGroup({

{groupNameLabel}

+ {bucketsError && ( + <> + {bucketsError} + + + )} <> {group.map((agg: AggConfig, index: number) => ( diff --git a/src/legacy/ui/public/vis/vis_filters/brush_event.js b/src/legacy/ui/public/vis/vis_filters/brush_event.js index 90cbaf7c048ee2..17ab302a20cb16 100644 --- a/src/legacy/ui/public/vis/vis_filters/brush_event.js +++ b/src/legacy/ui/public/vis/vis_filters/brush_event.js @@ -19,7 +19,7 @@ import _ from 'lodash'; import moment from 'moment'; -import { buildRangeFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../plugins/data/public'; export function onBrushEvent(event) { const isNumber = event.data.ordered; @@ -56,7 +56,7 @@ export function onBrushEvent(event) { }; } - const newFilter = buildRangeFilter( + const newFilter = esFilters.buildRangeFilter( field, range, indexPattern, diff --git a/src/legacy/ui/public/vis/vis_filters/vis_filters.js b/src/legacy/ui/public/vis/vis_filters/vis_filters.js index 9343585fa9508c..e879d040125f11 100644 --- a/src/legacy/ui/public/vis/vis_filters/vis_filters.js +++ b/src/legacy/ui/public/vis/vis_filters/vis_filters.js @@ -20,8 +20,8 @@ import _ from 'lodash'; import { pushFilterBarFilters } from '../push_filters'; import { onBrushEvent } from './brush_event'; -import { uniqFilters } from '../../../../../plugins/data/public'; -import { toggleFilterNegated } from '@kbn/es-query'; +import { uniqFilters, esFilters } from '../../../../../plugins/data/public'; + /** * For terms aggregations on `__other__` buckets, this assembles a list of applicable filter * terms based on a specific cell in the tabified data. @@ -94,7 +94,7 @@ const createFiltersFromEvent = (event) => { if (filter) { filter.forEach(f => { if (event.negate) { - f = toggleFilterNegated(f); + f = esFilters.toggleFilterNegated(f); } filters.push(f); }); diff --git a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.ts b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.ts index bb9f5832ac4e5f..fb16e095b34187 100644 --- a/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.ts +++ b/src/legacy/ui/public/visualize/loader/embedded_visualize_handler.ts @@ -22,7 +22,6 @@ import { debounce, forEach, get, isEqual } from 'lodash'; import * as Rx from 'rxjs'; import { share } from 'rxjs/operators'; import { i18n } from '@kbn/i18n'; -import { Filter } from '@kbn/es-query'; import { toastNotifications } from 'ui/notify'; // @ts-ignore untyped dependency import { AggConfigs } from 'ui/agg_types/agg_configs'; @@ -44,6 +43,7 @@ import { VisFiltersProvider } from '../../vis/vis_filters'; import { PipelineDataLoader } from './pipeline_data_loader'; import { visualizationLoader } from './visualization_loader'; import { Query } from '../../../../core_plugins/data/public'; +import { esFilters } from '../../../../../plugins/data/public'; import { DataAdapter, RequestAdapter } from '../../inspector/adapters'; @@ -67,7 +67,7 @@ export interface RequestHandlerParams { aggs: AggConfigs; timeRange?: TimeRange; query?: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; forceFetch: boolean; queryFilter: QueryFilter; uiState?: PersistedState; diff --git a/src/legacy/ui/public/visualize/loader/types.ts b/src/legacy/ui/public/visualize/loader/types.ts index 87183d839e6373..525ec35834ecde 100644 --- a/src/legacy/ui/public/visualize/loader/types.ts +++ b/src/legacy/ui/public/visualize/loader/types.ts @@ -17,7 +17,6 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { SavedObject } from 'ui/saved_objects/saved_object'; @@ -26,6 +25,7 @@ import { SearchSource } from '../../courier'; import { PersistedState } from '../../persisted_state'; import { AppState } from '../../state_management/app_state'; import { Vis } from '../../vis'; +import { esFilters } from '../../../../../plugins/data/public'; export interface VisSavedObject extends SavedObject { vis: Vis; @@ -68,7 +68,7 @@ export interface VisualizeLoaderParams { /** * Specifies the filters that should be applied to that visualization. */ - filters?: Filter[]; + filters?: esFilters.Filter[]; /** * The query that should apply to that visualization. */ diff --git a/src/legacy/ui/public/visualize/loader/utils/query_geohash_bounds.ts b/src/legacy/ui/public/visualize/loader/utils/query_geohash_bounds.ts index ec612f7dd03735..9f3aa190917d73 100644 --- a/src/legacy/ui/public/visualize/loader/utils/query_geohash_bounds.ts +++ b/src/legacy/ui/public/visualize/loader/utils/query_geohash_bounds.ts @@ -22,13 +22,13 @@ import { get } from 'lodash'; import { toastNotifications } from 'ui/notify'; import { AggConfig } from 'ui/vis'; -import { Filter } from '@kbn/es-query'; import { Query } from 'src/legacy/core_plugins/data/public'; import { timefilter } from 'ui/timefilter'; import { Vis } from '../../../vis'; +import { esFilters } from '../../../../../../plugins/data/public'; interface QueryGeohashBoundsParams { - filters?: Filter[]; + filters?: esFilters.Filter[]; query?: Query; } @@ -76,7 +76,7 @@ export async function queryGeohashBounds(vis: Vis, params: QueryGeohashBoundsPar const useTimeFilter = !!indexPattern.timeFieldName; if (useTimeFilter) { const filter = timefilter.createFilter(indexPattern); - if (filter) activeFilters.push((filter as any) as Filter); + if (filter) activeFilters.push((filter as any) as esFilters.Filter); } return activeFilters; }); diff --git a/src/plugins/dashboard_embeddable_container/public/embeddable/dashboard_container.tsx b/src/plugins/dashboard_embeddable_container/public/embeddable/dashboard_container.tsx index 36aaecb45ad499..8b258f35584388 100644 --- a/src/plugins/dashboard_embeddable_container/public/embeddable/dashboard_container.tsx +++ b/src/plugins/dashboard_embeddable_container/public/embeddable/dashboard_container.tsx @@ -20,7 +20,6 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; -import { Filter } from '@kbn/es-query'; import { RefreshInterval, TimeRange, Query } from '../../../data/public'; import { CoreStart } from '../../../../core/public'; import { IUiActionsStart } from '../ui_actions_plugin'; @@ -38,6 +37,7 @@ import { createPanelState } from './panel'; import { DashboardPanelState } from './types'; import { DashboardViewport } from './viewport/dashboard_viewport'; import { Start as InspectorStartContract } from '../../../inspector/public'; +import { esFilters } from '../../../../plugins/data/public'; import { KibanaContextProvider, KibanaReactContext, @@ -46,7 +46,7 @@ import { export interface DashboardContainerInput extends ContainerInput { viewMode: ViewMode; - filters: Filter[]; + filters: esFilters.Filter[]; query: Query; timeRange: TimeRange; refreshConfig?: RefreshInterval; @@ -65,7 +65,7 @@ interface IndexSignature { } export interface InheritedChildInput extends IndexSignature { - filters: Filter[]; + filters: esFilters.Filter[]; query: Query; timeRange: TimeRange; refreshConfig?: RefreshInterval; diff --git a/src/plugins/data/common/es_query/__tests__/fields_mock.ts b/src/plugins/data/common/es_query/__tests__/fields_mock.ts new file mode 100644 index 00000000000000..83fdf588af00c8 --- /dev/null +++ b/src/plugins/data/common/es_query/__tests__/fields_mock.ts @@ -0,0 +1,320 @@ +/* + * 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 const fields = [ + { + name: 'bytes', + type: 'number', + esTypes: ['long'], + count: 10, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'ssl', + type: 'boolean', + esTypes: ['boolean'], + count: 20, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: '@timestamp', + type: 'date', + esTypes: ['date'], + count: 30, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'time', + type: 'date', + esTypes: ['date'], + count: 30, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: '@tags', + type: 'string', + esTypes: ['keyword'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'utc_time', + type: 'date', + esTypes: ['date'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'phpmemory', + type: 'number', + esTypes: ['integer'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'ip', + type: 'ip', + esTypes: ['ip'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'request_body', + type: 'attachment', + esTypes: ['attachment'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'point', + type: 'geo_point', + esTypes: ['geo_point'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'area', + type: 'geo_shape', + esTypes: ['geo_shape'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'hashed', + type: 'murmur3', + esTypes: ['murmur3'], + count: 0, + scripted: false, + searchable: true, + aggregatable: false, + readFromDocValues: false, + }, + { + name: 'geo.coordinates', + type: 'geo_point', + esTypes: ['geo_point'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'extension', + type: 'string', + esTypes: ['keyword'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'machine.os', + type: 'string', + esTypes: ['text'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'machine.os.raw', + type: 'string', + esTypes: ['keyword'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + subType: { multi: { parent: 'machine.os' } }, + }, + { + name: 'geo.src', + type: 'string', + esTypes: ['keyword'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: '_id', + type: 'string', + esTypes: ['_id'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: '_type', + type: 'string', + esTypes: ['_type'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: '_source', + type: '_source', + esTypes: ['_source'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'non-filterable', + type: 'string', + esTypes: ['text'], + count: 0, + scripted: false, + searchable: false, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'non-sortable', + type: 'string', + esTypes: ['text'], + count: 0, + scripted: false, + searchable: false, + aggregatable: false, + readFromDocValues: false, + }, + { + name: 'custom_user_field', + type: 'conflict', + esTypes: ['long', 'text'], + count: 0, + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + name: 'script string', + type: 'string', + count: 0, + scripted: true, + script: "'i am a string'", + lang: 'expression', + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'script number', + type: 'number', + count: 0, + scripted: true, + script: '1234', + lang: 'expression', + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'script date', + type: 'date', + count: 0, + scripted: true, + script: '1234', + lang: 'painless', + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'script murmur3', + type: 'murmur3', + count: 0, + scripted: true, + script: '1234', + lang: 'expression', + searchable: true, + aggregatable: true, + readFromDocValues: false, + }, + { + name: 'nestedField.child', + type: 'string', + esTypes: ['text'], + count: 0, + scripted: false, + searchable: true, + aggregatable: false, + readFromDocValues: false, + subType: { nested: { path: 'nestedField' } }, + }, + { + name: 'nestedField.nestedChild.doublyNestedChild', + type: 'string', + esTypes: ['text'], + count: 0, + scripted: false, + searchable: true, + aggregatable: false, + readFromDocValues: false, + subType: { nested: { path: 'nestedField.nestedChild' } }, + }, +]; + +export const getField = (name: string) => fields.find(field => field.name === name); diff --git a/packages/kbn-es-query/src/filters/lib/custom_filter.ts b/src/plugins/data/common/es_query/filters/custom_filter.ts similarity index 100% rename from packages/kbn-es-query/src/filters/lib/custom_filter.ts rename to src/plugins/data/common/es_query/filters/custom_filter.ts diff --git a/packages/kbn-es-query/src/filters/lib/exists_filter.ts b/src/plugins/data/common/es_query/filters/exists_filter.ts similarity index 81% rename from packages/kbn-es-query/src/filters/lib/exists_filter.ts rename to src/plugins/data/common/es_query/filters/exists_filter.ts index 5843c25c43cffd..9125048e5f6cd0 100644 --- a/packages/kbn-es-query/src/filters/lib/exists_filter.ts +++ b/src/plugins/data/common/es_query/filters/exists_filter.ts @@ -18,6 +18,7 @@ */ import { Filter, FilterMeta } from './meta_filter'; +import { IndexPattern, Field } from './types'; export type ExistsFilterMeta = FilterMeta; @@ -31,3 +32,14 @@ export type ExistsFilter = Filter & { }; export const isExistsFilter = (filter: any): filter is ExistsFilter => filter && filter.exists; + +export const buildExistsFilter = (field: Field, indexPattern: IndexPattern) => { + return { + meta: { + index: indexPattern.id, + }, + exists: { + field: field.name, + }, + } as ExistsFilter; +}; diff --git a/packages/kbn-es-query/src/filters/lib/geo_bounding_box_filter.ts b/src/plugins/data/common/es_query/filters/geo_bounding_box_filter.ts similarity index 100% rename from packages/kbn-es-query/src/filters/lib/geo_bounding_box_filter.ts rename to src/plugins/data/common/es_query/filters/geo_bounding_box_filter.ts diff --git a/packages/kbn-es-query/src/filters/lib/geo_polygon_filter.ts b/src/plugins/data/common/es_query/filters/geo_polygon_filter.ts similarity index 100% rename from packages/kbn-es-query/src/filters/lib/geo_polygon_filter.ts rename to src/plugins/data/common/es_query/filters/geo_polygon_filter.ts diff --git a/packages/kbn-es-query/src/filters/lib/phrases_filter.ts b/src/plugins/data/common/es_query/filters/index.ts similarity index 66% rename from packages/kbn-es-query/src/filters/lib/phrases_filter.ts rename to src/plugins/data/common/es_query/filters/index.ts index 213afb409a0a69..e28ce9ba74975c 100644 --- a/packages/kbn-es-query/src/filters/lib/phrases_filter.ts +++ b/src/plugins/data/common/es_query/filters/index.ts @@ -17,16 +17,16 @@ * under the License. */ -import { Filter, FilterMeta } from './meta_filter'; +export * from './custom_filter'; +export * from './exists_filter'; +export * from './geo_bounding_box_filter'; +export * from './geo_polygon_filter'; +export * from './match_all_filter'; +export * from './meta_filter'; +export * from './missing_filter'; +export * from './phrase_filter'; +export * from './phrases_filter'; +export * from './query_string_filter'; +export * from './range_filter'; -export type PhrasesFilterMeta = FilterMeta & { - params: string[]; // The unformatted values - field?: string; -}; - -export type PhrasesFilter = Filter & { - meta: PhrasesFilterMeta; -}; - -export const isPhrasesFilter = (filter: any): filter is PhrasesFilter => - filter && filter.meta.type === 'phrases'; +export * from './types'; diff --git a/packages/kbn-es-query/src/filters/lib/match_all_filter.ts b/src/plugins/data/common/es_query/filters/match_all_filter.ts similarity index 100% rename from packages/kbn-es-query/src/filters/lib/match_all_filter.ts rename to src/plugins/data/common/es_query/filters/match_all_filter.ts diff --git a/packages/kbn-es-query/src/filters/lib/meta_filter.ts b/src/plugins/data/common/es_query/filters/meta_filter.ts similarity index 75% rename from packages/kbn-es-query/src/filters/lib/meta_filter.ts rename to src/plugins/data/common/es_query/filters/meta_filter.ts index 8f6aef782cea24..9adfdc4eedcb33 100644 --- a/packages/kbn-es-query/src/filters/lib/meta_filter.ts +++ b/src/plugins/data/common/es_query/filters/meta_filter.ts @@ -55,7 +55,7 @@ export interface LatLon { lon: number; } -export function buildEmptyFilter(isPinned: boolean, index?: string): Filter { +export const buildEmptyFilter = (isPinned: boolean, index?: string): Filter => { const meta: FilterMeta = { disabled: false, negate: false, @@ -65,43 +65,43 @@ export function buildEmptyFilter(isPinned: boolean, index?: string): Filter { const $state: FilterState = { store: isPinned ? FilterStateStore.GLOBAL_STATE : FilterStateStore.APP_STATE, }; + return { meta, $state }; -} +}; -export function isFilterPinned(filter: Filter) { +export const isFilterPinned = (filter: Filter) => { return filter.$state && filter.$state.store === FilterStateStore.GLOBAL_STATE; -} +}; -export function toggleFilterDisabled(filter: Filter) { +export const toggleFilterDisabled = (filter: Filter) => { const disabled = !filter.meta.disabled; const meta = { ...filter.meta, disabled }; + return { ...filter, meta }; -} +}; -export function toggleFilterNegated(filter: Filter) { +export const toggleFilterNegated = (filter: Filter) => { const negate = !filter.meta.negate; const meta = { ...filter.meta, negate }; + return { ...filter, meta }; -} +}; -export function toggleFilterPinned(filter: Filter) { +export const toggleFilterPinned = (filter: Filter) => { const store = isFilterPinned(filter) ? FilterStateStore.APP_STATE : FilterStateStore.GLOBAL_STATE; const $state = { ...filter.$state, store }; + return { ...filter, $state }; -} +}; -export function enableFilter(filter: Filter) { - return !filter.meta.disabled ? filter : toggleFilterDisabled(filter); -} +export const enableFilter = (filter: Filter) => + !filter.meta.disabled ? filter : toggleFilterDisabled(filter); -export function disableFilter(filter: Filter) { - return filter.meta.disabled ? filter : toggleFilterDisabled(filter); -} +export const disableFilter = (filter: Filter) => + filter.meta.disabled ? filter : toggleFilterDisabled(filter); -export function pinFilter(filter: Filter) { - return isFilterPinned(filter) ? filter : toggleFilterPinned(filter); -} +export const pinFilter = (filter: Filter) => + isFilterPinned(filter) ? filter : toggleFilterPinned(filter); -export function unpinFilter(filter: Filter) { - return !isFilterPinned(filter) ? filter : toggleFilterPinned(filter); -} +export const unpinFilter = (filter: Filter) => + !isFilterPinned(filter) ? filter : toggleFilterPinned(filter); diff --git a/packages/kbn-es-query/src/filters/lib/missing_filter.ts b/src/plugins/data/common/es_query/filters/missing_filter.ts similarity index 100% rename from packages/kbn-es-query/src/filters/lib/missing_filter.ts rename to src/plugins/data/common/es_query/filters/missing_filter.ts diff --git a/src/plugins/data/common/es_query/filters/phrase_filter.test.ts b/src/plugins/data/common/es_query/filters/phrase_filter.test.ts new file mode 100644 index 00000000000000..ec13e28c583d18 --- /dev/null +++ b/src/plugins/data/common/es_query/filters/phrase_filter.test.ts @@ -0,0 +1,97 @@ +/* + * 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 { buildInlineScriptForPhraseFilter, buildPhraseFilter } from './phrase_filter'; +import { IndexPattern } from './types'; +import { getField } from '../__tests__/fields_mock'; + +describe('Phrase filter builder', () => { + let indexPattern: IndexPattern; + + beforeEach(() => { + indexPattern = { + id: 'id', + }; + }); + + it('should be a function', () => { + expect(typeof buildPhraseFilter).toBe('function'); + }); + + it('should return a match query filter when passed a standard field', () => { + const field = getField('bytes'); + + expect(buildPhraseFilter(field, 5, indexPattern)).toEqual({ + meta: { + index: 'id', + }, + query: { + match_phrase: { + bytes: 5, + }, + }, + }); + }); + + it('should return a script filter when passed a scripted field', () => { + const field = getField('script number'); + + expect(buildPhraseFilter(field, 5, indexPattern)).toEqual({ + meta: { + index: 'id', + field: 'script number', + }, + script: { + script: { + lang: 'expression', + params: { + value: 5, + }, + source: '(1234) == value', + }, + }, + }); + }); +}); + +describe('buildInlineScriptForPhraseFilter', () => { + it('should wrap painless scripts in a lambda', () => { + const field = { + lang: 'painless', + script: 'return foo;', + }; + + const expected = + `boolean compare(Supplier s, def v) {return s.get() == v;}` + + `compare(() -> { return foo; }, params.value);`; + + expect(buildInlineScriptForPhraseFilter(field)).toBe(expected); + }); + + it('should create a simple comparison for other langs', () => { + const field = { + lang: 'expression', + script: 'doc[bytes].value', + }; + + const expected = `(doc[bytes].value) == value`; + + expect(buildInlineScriptForPhraseFilter(field)).toBe(expected); + }); +}); diff --git a/src/plugins/data/common/es_query/filters/phrase_filter.ts b/src/plugins/data/common/es_query/filters/phrase_filter.ts new file mode 100644 index 00000000000000..15c5c9d4ad2e6d --- /dev/null +++ b/src/plugins/data/common/es_query/filters/phrase_filter.ts @@ -0,0 +1,144 @@ +/* + * 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 { get, isPlainObject } from 'lodash'; +import { Filter, FilterMeta } from './meta_filter'; +import { IndexPattern, Field } from './types'; + +export type PhraseFilterMeta = FilterMeta & { + params?: { + query: string; // The unformatted value + }; + script?: { + script: { + source?: any; + lang?: string; + params: any; + }; + }; + field?: any; + index?: any; +}; + +export type PhraseFilter = Filter & { + meta: PhraseFilterMeta; +}; + +type PhraseFilterValue = string | number | boolean; + +export const isPhraseFilter = (filter: any): filter is PhraseFilter => { + const isMatchPhraseQuery = filter && filter.query && filter.query.match_phrase; + + const isDeprecatedMatchPhraseQuery = + filter && + filter.query && + filter.query.match && + Object.values(filter.query.match).find((params: any) => params.type === 'phrase'); + + return !!(isMatchPhraseQuery || isDeprecatedMatchPhraseQuery); +}; + +export const isScriptedPhraseFilter = (filter: any): filter is PhraseFilter => + Boolean(get(filter, 'script.script.params.value')); + +export const getPhraseFilterField = (filter: PhraseFilter) => { + const queryConfig = filter.query.match_phrase || filter.query.match; + return Object.keys(queryConfig)[0]; +}; + +export const getPhraseFilterValue = (filter: PhraseFilter): PhraseFilterValue => { + const queryConfig = filter.query.match_phrase || filter.query.match; + const queryValue = Object.values(queryConfig)[0] as any; + return isPlainObject(queryValue) ? queryValue.query : queryValue; +}; + +export const buildPhraseFilter = ( + field: Field, + value: any, + indexPattern: IndexPattern +): PhraseFilter => { + const convertedValue = getConvertedValueForField(field, value); + + if (field.scripted) { + return { + meta: { index: indexPattern.id, field: field.name } as PhraseFilterMeta, + script: getPhraseScript(field, value), + } as PhraseFilter; + } else { + return { + meta: { index: indexPattern.id }, + query: { + match_phrase: { + [field.name]: convertedValue, + }, + }, + } as PhraseFilter; + } +}; + +export const getPhraseScript = (field: Field, value: string) => { + const convertedValue = getConvertedValueForField(field, value); + const script = buildInlineScriptForPhraseFilter(field); + + return { + script: { + source: script, + lang: field.lang, + params: { + value: convertedValue, + }, + }, + }; +}; + +// See https://github.com/elastic/elasticsearch/issues/20941 and https://github.com/elastic/kibana/issues/8677 +// and https://github.com/elastic/elasticsearch/pull/22201 +// for the reason behind this change. Aggs now return boolean buckets with a key of 1 or 0. +export const getConvertedValueForField = (field: Field, value: any) => { + if (typeof value !== 'boolean' && field.type === 'boolean') { + if ([1, 'true'].includes(value)) { + return true; + } else if ([0, 'false'].includes(value)) { + return false; + } else { + throw new Error(`${value} is not a valid boolean value for boolean field ${field.name}`); + } + } + return value; +}; + +/** + * Takes a scripted field and returns an inline script appropriate for use in a script query. + * Handles lucene expression and Painless scripts. Other langs aren't guaranteed to generate valid + * scripts. + * + * @param {object} scriptedField A Field object representing a scripted field + * @returns {string} The inline script string + */ +export const buildInlineScriptForPhraseFilter = (scriptedField: any) => { + // We must wrap painless scripts in a lambda in case they're more than a simple expression + if (scriptedField.lang === 'painless') { + return ( + `boolean compare(Supplier s, def v) {return s.get() == v;}` + + `compare(() -> { ${scriptedField.script} }, params.value);` + ); + } else { + return `(${scriptedField.script}) == value`; + } +}; diff --git a/packages/kbn-es-query/src/filters/phrases.js b/src/plugins/data/common/es_query/filters/phrases_filter.ts similarity index 51% rename from packages/kbn-es-query/src/filters/phrases.js rename to src/plugins/data/common/es_query/filters/phrases_filter.ts index f02b3763f37bb7..e4606695c0f6a5 100644 --- a/packages/kbn-es-query/src/filters/phrases.js +++ b/src/plugins/data/common/es_query/filters/phrases_filter.ts @@ -17,47 +17,54 @@ * under the License. */ -import { getPhraseScript } from './phrase'; +import { Filter, FilterMeta } from './meta_filter'; +import { Field, IndexPattern } from './types'; +import { getPhraseScript } from './phrase_filter'; + +export type PhrasesFilterMeta = FilterMeta & { + params: string[]; // The unformatted values + field?: string; +}; + +export type PhrasesFilter = Filter & { + meta: PhrasesFilterMeta; +}; + +export const isPhrasesFilter = (filter: any): filter is PhrasesFilter => + filter && filter.meta.type === 'phrases'; // Creates a filter where the given field matches one or more of the given values // params should be an array of values -export function buildPhrasesFilter(field, params, indexPattern) { +export const buildPhrasesFilter = (field: Field, params: any, indexPattern: IndexPattern) => { const index = indexPattern.id; const type = 'phrases'; const key = field.name; - const value = params - .map(value => format(field, value)) - .join(', '); - const filter = { - meta: { index, type, key, value, params } - }; + const format = (f: Field, value: any) => + f && f.format && f.format.convert ? f.format.convert(value) : value; + + const value = params.map((v: any) => format(field, v)).join(', '); let should; if (field.scripted) { - should = params.map((value) => ({ - script: getPhraseScript(field, value) + should = params.map((v: any) => ({ + script: getPhraseScript(field, v), })); } else { - should = params.map((value) => ({ + should = params.map((v: any) => ({ match_phrase: { - [field.name]: value - } + [field.name]: v, + }, })); } - filter.query = { - bool: { - should, - minimum_should_match: 1 - } - }; - - return filter; -} - -function format(field, value) { - return field && field.format && field.format.convert - ? field.format.convert(value) - : value; -} + return { + meta: { index, type, key, value, params }, + query: { + bool: { + should, + minimum_should_match: 1, + }, + }, + } as PhrasesFilter; +}; diff --git a/src/plugins/data/common/es_query/filters/query_string_filter.test.ts b/src/plugins/data/common/es_query/filters/query_string_filter.test.ts new file mode 100644 index 00000000000000..839e4f6359257e --- /dev/null +++ b/src/plugins/data/common/es_query/filters/query_string_filter.test.ts @@ -0,0 +1,47 @@ +/* + * 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 { buildQueryFilter } from './query_string_filter'; +import { IndexPattern } from './types'; + +describe('Phrase filter builder', () => { + let indexPattern: IndexPattern; + + beforeEach(() => { + indexPattern = { + id: 'id', + }; + }); + + it('should be a function', () => { + expect(typeof buildQueryFilter).toBe('function'); + }); + + it('should return a query filter when passed a standard field', () => { + expect(buildQueryFilter({ foo: 'bar' }, indexPattern.id, '')).toEqual({ + meta: { + alias: '', + index: 'id', + }, + query: { + foo: 'bar', + }, + }); + }); +}); diff --git a/packages/kbn-es-query/src/filters/lib/query_string_filter.ts b/src/plugins/data/common/es_query/filters/query_string_filter.ts similarity index 78% rename from packages/kbn-es-query/src/filters/lib/query_string_filter.ts rename to src/plugins/data/common/es_query/filters/query_string_filter.ts index 3b3b97fafba9bc..901dc724aa4e49 100644 --- a/packages/kbn-es-query/src/filters/lib/query_string_filter.ts +++ b/src/plugins/data/common/es_query/filters/query_string_filter.ts @@ -18,6 +18,7 @@ */ import { Filter, FilterMeta } from './meta_filter'; +import { IndexPattern } from './types'; export type QueryStringFilterMeta = FilterMeta; @@ -32,3 +33,17 @@ export type QueryStringFilter = Filter & { export const isQueryStringFilter = (filter: any): filter is QueryStringFilter => filter && filter.query && filter.query.query_string; + +// Creates a filter corresponding to a raw Elasticsearch query DSL object +export const buildQueryFilter = ( + query: QueryStringFilter['query'], + index: IndexPattern, + alias: string +) => + ({ + query, + meta: { + index, + alias, + }, + } as QueryStringFilter); diff --git a/src/plugins/data/common/es_query/filters/range_filter.test.ts b/src/plugins/data/common/es_query/filters/range_filter.test.ts new file mode 100644 index 00000000000000..9008dc2a672944 --- /dev/null +++ b/src/plugins/data/common/es_query/filters/range_filter.test.ts @@ -0,0 +1,174 @@ +/* + * 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 { each } from 'lodash'; +import { buildRangeFilter, RangeFilter } from './range_filter'; +import { IndexPattern, Field } from './types'; +import { getField } from '../__tests__/fields_mock'; + +describe('Range filter builder', () => { + let indexPattern: IndexPattern; + + beforeEach(() => { + indexPattern = { + id: 'id', + }; + }); + + it('should be a function', () => { + expect(typeof buildRangeFilter).toBe('function'); + }); + + it('should return a range filter when passed a standard field', () => { + const field = getField('bytes'); + + expect(buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern)).toEqual({ + meta: { + index: 'id', + params: {}, + }, + range: { + bytes: { + gte: 1, + lte: 3, + }, + }, + }); + }); + + it('should return a script filter when passed a scripted field', () => { + const field = getField('script number'); + + expect(buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern)).toEqual({ + meta: { + field: 'script number', + index: 'id', + params: {}, + }, + script: { + script: { + lang: 'expression', + source: '(' + field!.script + ')>=gte && (' + field!.script + ')<=lte', + params: { + value: '>=1 <=3', + gte: 1, + lte: 3, + }, + }, + }, + }); + }); + + it('should wrap painless scripts in comparator lambdas', () => { + const field = getField('script date'); + const expected = + `boolean gte(Supplier s, def v) {return !s.get().toInstant().isBefore(Instant.parse(v))} ` + + `boolean lte(Supplier s, def v) {return !s.get().toInstant().isAfter(Instant.parse(v))}` + + `gte(() -> { ${field!.script} }, params.gte) && ` + + `lte(() -> { ${field!.script} }, params.lte)`; + + const rangeFilter = buildRangeFilter(field, { gte: 1, lte: 3 }, indexPattern); + + expect(rangeFilter.script!.script.source).toBe(expected); + }); + + it('should throw an error when gte and gt, or lte and lt are both passed', () => { + const field = getField('script number'); + + expect(() => { + buildRangeFilter(field, { gte: 1, gt: 3 }, indexPattern); + }).toThrowError(); + + expect(() => { + buildRangeFilter(field, { lte: 1, lt: 3 }, indexPattern); + }).toThrowError(); + }); + + it('to use the right operator for each of gte, gt, lt and lte', () => { + const field = getField('script number'); + + each({ gte: '>=', gt: '>', lte: '<=', lt: '<' }, (operator: string, key: any) => { + const params = { + [key]: 5, + }; + + const filter = buildRangeFilter(field, params, indexPattern); + const script = filter.script!.script; + + expect(script.source).toBe('(' + field!.script + ')' + operator + key); + expect(script.params[key]).toBe(5); + expect(script.params.value).toBe(operator + 5); + }); + }); + + describe('when given params where one side is infinite', () => { + let field: Field; + let filter: RangeFilter; + + beforeEach(() => { + field = getField('script number'); + filter = buildRangeFilter(field, { gte: 0, lt: Infinity }, indexPattern); + }); + + describe('returned filter', () => { + it('is a script filter', () => { + expect(filter).toHaveProperty('script'); + }); + + it('contain a param for the finite side', () => { + expect(filter.script!.script.params).toHaveProperty('gte', 0); + }); + + it('does not contain a param for the infinite side', () => { + expect(filter.script!.script.params).not.toHaveProperty('lt'); + }); + + it('does not contain a script condition for the infinite side', () => { + const script = field!.script; + + expect(filter.script!.script.source).toEqual(`(${script})>=gte`); + }); + }); + }); + + describe('when given params where both sides are infinite', () => { + let field: Field; + let filter: RangeFilter; + + beforeEach(() => { + field = getField('script number'); + filter = buildRangeFilter(field, { gte: -Infinity, lt: Infinity }, indexPattern); + }); + + describe('returned filter', () => { + it('is a match_all filter', () => { + expect(filter).not.toHaveProperty('script'); + expect(filter).toHaveProperty('match_all'); + }); + + it('does not contain params', () => { + expect(filter).not.toHaveProperty('params'); + }); + + it('meta field is set to field name', () => { + expect(filter.meta.field).toEqual('script number'); + }); + }); + }); +}); diff --git a/packages/kbn-es-query/src/filters/range.js b/src/plugins/data/common/es_query/filters/range_filter.ts similarity index 51% rename from packages/kbn-es-query/src/filters/range.js rename to src/plugins/data/common/es_query/filters/range_filter.ts index 357f9209c50de3..d7931f191e52b1 100644 --- a/packages/kbn-es-query/src/filters/range.js +++ b/src/plugins/data/common/es_query/filters/range_filter.ts @@ -16,9 +16,12 @@ * specific language governing permissions and limitations * under the License. */ +import { map, reduce, mapValues, get, keys, pick } from 'lodash'; +import { Filter, FilterMeta } from './meta_filter'; +import { Field, IndexPattern } from './types'; -import _ from 'lodash'; const OPERANDS_IN_RANGE = 2; + const operators = { gt: '>', gte: '>=', @@ -39,33 +42,85 @@ const dateComparators = { lt: 'boolean lt(Supplier s, def v) {return s.get().toInstant().isBefore(Instant.parse(v))}', }; -function formatValue(field, params) { - return _.map(params, (val, key) => operators[key] + format(field, val)).join(' '); +export interface RangeFilterParams { + from?: number | string; + to?: number | string; + gt?: number | string; + lt?: number | string; + gte?: number | string; + lte?: number | string; + format?: string; } +const hasRangeKeys = (params: RangeFilterParams) => + Boolean( + keys(params).find((key: string) => ['gte', 'gt', 'lte', 'lt', 'from', 'to'].includes(key)) + ); + +export type RangeFilterMeta = FilterMeta & { + params: RangeFilterParams; + field?: any; + formattedValue?: string; +}; + +export type RangeFilter = Filter & { + meta: RangeFilterMeta; + script?: { + script: { + params: any; + lang: string; + source: any; + }; + }; + match_all?: any; + range: { [key: string]: RangeFilterParams }; +}; + +export const isRangeFilter = (filter: any): filter is RangeFilter => filter && filter.range; + +export const isScriptedRangeFilter = (filter: any): filter is RangeFilter => { + const params: RangeFilterParams = get(filter, 'script.script.params', {}); + + return hasRangeKeys(params); +}; + +const formatValue = (field: Field, params: any[]) => + map(params, (val: any, key: string) => get(operators, key) + format(field, val)).join(' '); + +const format = (field: Field, value: any) => + field && field.format && field.format.convert ? field.format.convert(value) : value; + // Creates a filter where the value for the given field is in the given range // params should be an object containing `lt`, `lte`, `gt`, and/or `gte` -export function buildRangeFilter(field, params, indexPattern, formattedValue) { - const filter = { meta: { index: indexPattern.id } }; - if (formattedValue) filter.meta.formattedValue = formattedValue; +export const buildRangeFilter = ( + field: Field, + params: RangeFilterParams, + indexPattern: IndexPattern, + formattedValue?: string +): RangeFilter => { + const filter: any = { meta: { index: indexPattern.id, params: {} } }; + + if (formattedValue) { + filter.meta.formattedValue = formattedValue; + } - params = _.mapValues(params, (value) => { - return (field.type === 'number') ? parseFloat(value) : value; - }); + params = mapValues(params, value => (field.type === 'number' ? parseFloat(value) : value)); if ('gte' in params && 'gt' in params) throw new Error('gte and gt are mutually exclusive'); if ('lte' in params && 'lt' in params) throw new Error('lte and lt are mutually exclusive'); - const totalInfinite = ['gt', 'lt'].reduce((totalInfinite, op) => { + const totalInfinite = ['gt', 'lt'].reduce((acc: number, op: any) => { const key = op in params ? op : `${op}e`; - const isInfinite = Math.abs(params[key]) === Infinity; + const isInfinite = Math.abs(get(params, key)) === Infinity; if (isInfinite) { - totalInfinite++; + acc++; + + // @ts-ignore delete params[key]; } - return totalInfinite; + return acc; }, 0); if (totalInfinite === OPERANDS_IN_RANGE) { @@ -81,25 +136,29 @@ export function buildRangeFilter(field, params, indexPattern, formattedValue) { filter.range[field.name] = params; } - return filter; -} + return filter as RangeFilter; +}; -export function getRangeScript(field, params) { - const knownParams = _.pick(params, (val, key) => { - return key in operators; - }); - let script = _.map(knownParams, function (val, key) { - return '(' + field.script + ')' + operators[key] + key; - }).join(' && '); +export const getRangeScript = (field: IndexPattern, params: RangeFilterParams) => { + const knownParams = pick(params, (val, key: any) => key in operators); + let script = map( + knownParams, + (val: any, key: string) => '(' + field.script + ')' + get(operators, key) + key + ).join(' && '); // We must wrap painless scripts in a lambda in case they're more than a simple expression if (field.lang === 'painless') { const comp = field.type === 'date' ? dateComparators : comparators; - const currentComparators = _.reduce(knownParams, (acc, val, key) => acc.concat(comp[key]), []).join(' '); + const currentComparators = reduce( + knownParams, + (acc, val, key) => acc.concat(get(comp, key)), + [] + ).join(' '); - const comparisons = _.map(knownParams, function (val, key) { - return `${key}(() -> { ${field.script} }, params.${key})`; - }).join(' && '); + const comparisons = map( + knownParams, + (val, key) => `${key}(() -> { ${field.script} }, params.${key})` + ).join(' && '); script = `${currentComparators}${comparisons}`; } @@ -108,14 +167,7 @@ export function getRangeScript(field, params) { script: { source: script, params: knownParams, - lang: field.lang - } + lang: field.lang, + }, }; -} - -function format(field, value) { - return field && field.format && field.format.convert - ? field.format.convert(value) - : value; -} - +}; diff --git a/src/plugins/data/common/es_query/filters/types.ts b/src/plugins/data/common/es_query/filters/types.ts new file mode 100644 index 00000000000000..28147350619995 --- /dev/null +++ b/src/plugins/data/common/es_query/filters/types.ts @@ -0,0 +1,57 @@ +/* + * 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 { ExistsFilter } from './exists_filter'; +import { GeoBoundingBoxFilter } from './geo_bounding_box_filter'; +import { GeoPolygonFilter } from './geo_polygon_filter'; +import { PhrasesFilter } from './phrases_filter'; +import { PhraseFilter } from './phrase_filter'; +import { RangeFilter } from './range_filter'; +import { MatchAllFilter } from './match_all_filter'; +import { MissingFilter } from './missing_filter'; + +// Any filter associated with a field (used in the filter bar/editor) +export type FieldFilter = + | ExistsFilter + | GeoBoundingBoxFilter + | GeoPolygonFilter + | PhraseFilter + | PhrasesFilter + | RangeFilter + | MatchAllFilter + | MissingFilter; + +export enum FILTERS { + CUSTOM = 'custom', + PHRASES = 'phrases', + PHRASE = 'phrase', + EXISTS = 'exists', + MATCH_ALL = 'match_all', + MISSING = 'missing', + QUERY_STRING = 'query_string', + RANGE = 'range', + GEO_BOUNDING_BOX = 'geo_bounding_box', + GEO_POLYGON = 'geo_polygon', +} + +// 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 +// will be replaces after Fieds / IndexPattern will be moved into new platform +export type Field = any; +export type IndexPattern = any; diff --git a/packages/kbn-es-query/src/filters/index.js b/src/plugins/data/common/es_query/index.ts similarity index 84% rename from packages/kbn-es-query/src/filters/index.js rename to src/plugins/data/common/es_query/index.ts index d7d092eabd8a2f..88e14a43cfaae2 100644 --- a/packages/kbn-es-query/src/filters/index.js +++ b/src/plugins/data/common/es_query/index.ts @@ -16,10 +16,6 @@ * specific language governing permissions and limitations * under the License. */ +import * as esFilters from './filters'; -export * from './exists'; -export * from './phrase'; -export * from './phrases'; -export * from './query'; -export * from './range'; -export * from './lib'; +export { esFilters }; diff --git a/src/plugins/data/common/index.ts b/src/plugins/data/common/index.ts index dca7897bd2766c..42b5a03fcc926a 100644 --- a/src/plugins/data/common/index.ts +++ b/src/plugins/data/common/index.ts @@ -20,5 +20,6 @@ export * from './query'; export * from './field_formats'; export * from './kbn_field_types'; +export * from './es_query'; export * from './types'; diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 51f26a4bd7f319..32153df69f3678 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -34,5 +34,4 @@ export * from './types'; export { IRequestTypesMap, IResponseTypesMap } from './search'; export * from './search'; - export * from './query'; diff --git a/src/plugins/data/public/query/filter_manager/filter_manager.test.ts b/src/plugins/data/public/query/filter_manager/filter_manager.test.ts index 5092e9e55c2b48..33f9c4ccd795db 100644 --- a/src/plugins/data/public/query/filter_manager/filter_manager.test.ts +++ b/src/plugins/data/public/query/filter_manager/filter_manager.test.ts @@ -21,10 +21,10 @@ import _ from 'lodash'; import sinon from 'sinon'; import { Subscription } from 'rxjs'; -import { Filter, FilterStateStore } from '@kbn/es-query'; import { FilterManager } from './filter_manager'; import { getFilter } from './test_helpers/get_stub_filter'; import { getFiltersArray } from './test_helpers/get_filters_array'; +import { esFilters } from '../../../common/es_query'; import { coreMock } from '../../../../../core/public/mocks'; const setupMock = coreMock.createSetup(); @@ -39,7 +39,7 @@ describe('filter_manager', () => { let updateListener: sinon.SinonSpy; let filterManager: FilterManager; - let readyFilters: Filter[]; + let readyFilters: esFilters.Filter[]; beforeEach(() => { updateListener = sinon.stub(); @@ -82,7 +82,7 @@ describe('filter_manager', () => { test('app state should be set', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); filterManager.setFilters([f1]); expect(filterManager.getAppFilters()).toHaveLength(1); expect(filterManager.getGlobalFilters()).toHaveLength(0); @@ -96,7 +96,7 @@ describe('filter_manager', () => { test('global state should be set', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); filterManager.setFilters([f1]); expect(filterManager.getAppFilters()).toHaveLength(0); expect(filterManager.getGlobalFilters()).toHaveLength(1); @@ -110,8 +110,8 @@ describe('filter_manager', () => { test('both states should be set', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); filterManager.setFilters([f1, f2]); expect(filterManager.getAppFilters()).toHaveLength(1); expect(filterManager.getGlobalFilters()).toHaveLength(1); @@ -128,8 +128,8 @@ describe('filter_manager', () => { test('set state should override previous state', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); filterManager.setFilters([f1]); filterManager.setFilters([f2]); @@ -150,7 +150,7 @@ describe('filter_manager', () => { test('changing a disabled filter should fire only update event', async function() { const updateStub = jest.fn(); const fetchStub = jest.fn(); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, true, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, true, false, 'age', 34); filterManager.setFilters([f1]); @@ -175,7 +175,7 @@ describe('filter_manager', () => { describe('add filters', () => { test('app state should accept a single filter', async function() { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); filterManager.addFilters(f1); const appFilters = filterManager.getAppFilters(); expect(appFilters).toHaveLength(1); @@ -184,20 +184,21 @@ describe('filter_manager', () => { expect(updateListener.callCount).toBe(1); }); - test('app state should accept array', async () => { - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'female'); + test('app state should accept array and preserve order', async () => { + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'female'); + filterManager.addFilters([f1]); filterManager.addFilters([f2]); const appFilters = filterManager.getAppFilters(); expect(appFilters).toHaveLength(2); - expect(appFilters).toEqual([f2, f1]); + expect(appFilters).toEqual([f1, f2]); expect(filterManager.getGlobalFilters()).toHaveLength(0); }); test('global state should accept a single filer', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); filterManager.addFilters(f1); expect(filterManager.getAppFilters()).toHaveLength(0); const globalFilters = filterManager.getGlobalFilters(); @@ -206,20 +207,57 @@ describe('filter_manager', () => { expect(updateListener.callCount).toBe(1); }); - test('global state should be accept array', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'gender', 'female'); + test('global state should be accept array and preserve order', async () => { + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter( + esFilters.FilterStateStore.GLOBAL_STATE, + false, + false, + 'gender', + 'female' + ); + filterManager.addFilters([f1, f2]); expect(filterManager.getAppFilters()).toHaveLength(0); const globalFilters = filterManager.getGlobalFilters(); expect(globalFilters).toHaveLength(2); - expect(globalFilters).toEqual([f2, f1]); + expect(globalFilters).toEqual([f1, f2]); + }); + + test('mixed filters: global filters should stay in the beginning', async () => { + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'female'); + filterManager.addFilters([f1, f2]); + const filters = filterManager.getFilters(); + expect(filters).toHaveLength(2); + expect(filters).toEqual([f1, f2]); + }); + + test('mixed filters: global filters should move to the beginning', async () => { + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); + const f2 = getFilter( + esFilters.FilterStateStore.GLOBAL_STATE, + false, + false, + 'gender', + 'female' + ); + filterManager.addFilters([f1, f2]); + const filters = filterManager.getFilters(); + expect(filters).toHaveLength(2); + expect(filters).toEqual([f2, f1]); }); test('add multiple filters at once', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'gender', 'female'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter( + esFilters.FilterStateStore.GLOBAL_STATE, + false, + false, + 'gender', + 'female' + ); filterManager.addFilters([f1, f2]); expect(filterManager.getAppFilters()).toHaveLength(0); expect(filterManager.getGlobalFilters()).toHaveLength(2); @@ -228,8 +266,8 @@ describe('filter_manager', () => { test('add same filter to global and app', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); filterManager.addFilters([f1, f2]); // FILTER SHOULD BE ADDED ONLY ONCE, TO GLOBAL @@ -240,8 +278,8 @@ describe('filter_manager', () => { test('add same filter with different values to global and app', async () => { updateSubscription = filterManager.getUpdates$().subscribe(updateListener); - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); filterManager.addFilters([f1, f2]); // FILTER SHOULD BE ADDED TWICE @@ -251,7 +289,7 @@ describe('filter_manager', () => { }); test('add filter with no state, and force pin', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); f1.$state = undefined; filterManager.addFilters([f1], true); @@ -260,12 +298,12 @@ describe('filter_manager', () => { const f1Output = filterManager.getFilters()[0]; expect(f1Output.$state).toBeDefined(); if (f1Output.$state) { - expect(f1Output.$state.store).toBe(FilterStateStore.GLOBAL_STATE); + expect(f1Output.$state.store).toBe(esFilters.FilterStateStore.GLOBAL_STATE); } }); test('add filter with no state, and dont force pin', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 38); f1.$state = undefined; filterManager.addFilters([f1], false); @@ -274,7 +312,7 @@ describe('filter_manager', () => { const f1Output = filterManager.getFilters()[0]; expect(f1Output.$state).toBeDefined(); if (f1Output.$state) { - expect(f1Output.$state.store).toBe(FilterStateStore.APP_STATE); + expect(f1Output.$state.store).toBe(esFilters.FilterStateStore.APP_STATE); } }); @@ -286,11 +324,11 @@ describe('filter_manager', () => { // global filters should be listed first let res = filterManager.getFilters(); expect(res).toHaveLength(2); - expect(res[0].$state && res[0].$state.store).toEqual(FilterStateStore.GLOBAL_STATE); + expect(res[0].$state && res[0].$state.store).toEqual(esFilters.FilterStateStore.GLOBAL_STATE); expect(res[0].meta.disabled).toEqual(filters[1].meta.disabled); expect(res[0].query).toEqual(filters[1].query); - expect(res[1].$state && res[1].$state.store).toEqual(FilterStateStore.APP_STATE); + expect(res[1].$state && res[1].$state.store).toEqual(esFilters.FilterStateStore.APP_STATE); expect(res[1].meta.disabled).toEqual(filters[0].meta.disabled); expect(res[1].query).toEqual(filters[0].query); @@ -310,7 +348,7 @@ describe('filter_manager', () => { const res = filterManager.getFilters(); expect(res).toHaveLength(3); _.each(res, function(filter) { - expect(filter.$state && filter.$state.store).toBe(FilterStateStore.GLOBAL_STATE); + expect(filter.$state && filter.$state.store).toBe(esFilters.FilterStateStore.GLOBAL_STATE); }); }); @@ -393,7 +431,7 @@ describe('filter_manager', () => { }); test('should de-dupe global filters being set', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); const f2 = _.cloneDeep(f1); filterManager.setFilters([f1, f2]); expect(filterManager.getAppFilters()).toHaveLength(0); @@ -402,7 +440,7 @@ describe('filter_manager', () => { }); test('should de-dupe app filters being set', async () => { - const f1 = getFilter(FilterStateStore.APP_STATE, false, false, 'age', 34); + const f1 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'age', 34); const f2 = _.cloneDeep(f1); filterManager.setFilters([f1, f2]); expect(filterManager.getAppFilters()).toHaveLength(1); @@ -417,7 +455,7 @@ describe('filter_manager', () => { const appFilter = _.cloneDeep(readyFilters[idx]); appFilter.meta.negate = true; appFilter.$state = { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }; filterManager.addFilters(appFilter); const res = filterManager.getFilters(); @@ -434,7 +472,7 @@ describe('filter_manager', () => { const appFilter = _.cloneDeep(readyFilters[1]); appFilter.meta.negate = true; appFilter.$state = { - store: FilterStateStore.APP_STATE, + store: esFilters.FilterStateStore.APP_STATE, }; filterManager.addFilters(appFilter, false); @@ -443,7 +481,7 @@ describe('filter_manager', () => { expect(res).toHaveLength(3); expect( res.filter(function(filter) { - return filter.$state && filter.$state.store === FilterStateStore.GLOBAL_STATE; + return filter.$state && filter.$state.store === esFilters.FilterStateStore.GLOBAL_STATE; }).length ).toBe(3); }); @@ -496,8 +534,8 @@ describe('filter_manager', () => { }); test('remove on full should clean and fire events', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); filterManager.setFilters([f1, f2]); updateSubscription = filterManager.getUpdates$().subscribe(updateListener); @@ -507,9 +545,9 @@ describe('filter_manager', () => { }); test('remove non existing filter should do nothing and not fire events', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); - const f3 = getFilter(FilterStateStore.APP_STATE, false, false, 'country', 'US'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); + const f3 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'country', 'US'); filterManager.setFilters([f1, f2]); expect(filterManager.getFilters()).toHaveLength(2); @@ -520,9 +558,9 @@ describe('filter_manager', () => { }); test('remove existing filter should remove and fire events', async () => { - const f1 = getFilter(FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); - const f2 = getFilter(FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); - const f3 = getFilter(FilterStateStore.APP_STATE, false, false, 'country', 'US'); + const f1 = getFilter(esFilters.FilterStateStore.GLOBAL_STATE, false, false, 'age', 34); + const f2 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'gender', 'FEMALE'); + const f3 = getFilter(esFilters.FilterStateStore.APP_STATE, false, false, 'country', 'US'); filterManager.setFilters([f1, f2, f3]); expect(filterManager.getFilters()).toHaveLength(3); diff --git a/src/plugins/data/public/query/filter_manager/filter_manager.ts b/src/plugins/data/public/query/filter_manager/filter_manager.ts index 66b65a40926cb7..06e2b77dca238f 100644 --- a/src/plugins/data/public/query/filter_manager/filter_manager.ts +++ b/src/plugins/data/public/query/filter_manager/filter_manager.ts @@ -17,8 +17,6 @@ * under the License. */ -import { Filter, isFilterPinned, FilterStateStore } from '@kbn/es-query'; - import _ from 'lodash'; import { Subject } from 'rxjs'; @@ -29,9 +27,10 @@ import { mapAndFlattenFilters } from './lib/map_and_flatten_filters'; import { uniqFilters } from './lib/uniq_filters'; import { onlyDisabledFiltersChanged } from './lib/only_disabled'; import { PartitionedFilters } from './types'; +import { esFilters } from '../../../common/es_query'; export class FilterManager { - private filters: Filter[] = []; + private filters: esFilters.Filter[] = []; private updated$: Subject = new Subject(); private fetch$: Subject = new Subject(); private uiSettings: UiSettingsClientContract; @@ -40,7 +39,7 @@ export class FilterManager { this.uiSettings = uiSettings; } - private mergeIncomingFilters(partitionedFilters: PartitionedFilters): Filter[] { + private mergeIncomingFilters(partitionedFilters: PartitionedFilters): esFilters.Filter[] { const globalFilters = partitionedFilters.globalFilters; const appFilters = partitionedFilters.appFilters; @@ -61,25 +60,33 @@ export class FilterManager { return FilterManager.mergeFilters(appFilters, globalFilters); } - private static mergeFilters(appFilters: Filter[], globalFilters: Filter[]): Filter[] { + private static mergeFilters( + appFilters: esFilters.Filter[], + globalFilters: esFilters.Filter[] + ): esFilters.Filter[] { return uniqFilters(appFilters.reverse().concat(globalFilters.reverse())).reverse(); } - private static partitionFilters(filters: Filter[]): PartitionedFilters { - const [globalFilters, appFilters] = _.partition(filters, isFilterPinned); + private static partitionFilters(filters: esFilters.Filter[]): PartitionedFilters { + const [globalFilters, appFilters] = _.partition(filters, esFilters.isFilterPinned); return { globalFilters, appFilters, }; } - private handleStateUpdate(newFilters: Filter[]) { + private handleStateUpdate(newFilters: esFilters.Filter[]) { // global filters should always be first - newFilters.sort(({ $state: a }: Filter, { $state: b }: Filter): number => { - return a!.store === FilterStateStore.GLOBAL_STATE && - b!.store !== FilterStateStore.GLOBAL_STATE - ? -1 - : 1; + + newFilters.sort(({ $state: a }: esFilters.Filter, { $state: b }: esFilters.Filter): number => { + if (a!.store === b!.store) { + return 0; + } else { + return a!.store === esFilters.FilterStateStore.GLOBAL_STATE && + b!.store !== esFilters.FilterStateStore.GLOBAL_STATE + ? -1 + : 1; + } }); const filtersUpdated = !_.isEqual(this.filters, newFilters); @@ -124,7 +131,7 @@ export class FilterManager { /* Setters */ - public addFilters(filters: Filter[] | Filter, pinFilterStatus?: boolean) { + public addFilters(filters: esFilters.Filter[] | esFilters.Filter, pinFilterStatus?: boolean) { if (!Array.isArray(filters)) { filters = [filters]; } @@ -139,7 +146,10 @@ export class FilterManager { // Set the store of all filters. For now. // In the future, all filters should come in with filter state store already set. - const store = pinFilterStatus ? FilterStateStore.GLOBAL_STATE : FilterStateStore.APP_STATE; + const store = pinFilterStatus + ? esFilters.FilterStateStore.GLOBAL_STATE + : esFilters.FilterStateStore.APP_STATE; + FilterManager.setFiltersStore(filters, store); const mappedFilters = mapAndFlattenFilters(filters); @@ -154,14 +164,14 @@ export class FilterManager { this.handleStateUpdate(newFilters); } - public setFilters(newFilters: Filter[]) { + public setFilters(newFilters: esFilters.Filter[]) { const mappedFilters = mapAndFlattenFilters(newFilters); const newPartitionedFilters = FilterManager.partitionFilters(mappedFilters); const mergedFilters = this.mergeIncomingFilters(newPartitionedFilters); this.handleStateUpdate(mergedFilters); } - public removeFilter(filter: Filter) { + public removeFilter(filter: esFilters.Filter) { const filterIndex = _.findIndex(this.filters, item => { return _.isEqual(item.meta, filter.meta) && _.isEqual(item.query, filter.query); }); @@ -177,8 +187,8 @@ export class FilterManager { this.setFilters([]); } - public static setFiltersStore(filters: Filter[], store: FilterStateStore) { - _.map(filters, (filter: Filter) => { + public static setFiltersStore(filters: esFilters.Filter[], store: esFilters.FilterStateStore) { + _.map(filters, (filter: esFilters.Filter) => { // Override status only for filters that didn't have state in the first place. if (filter.$state === undefined) { filter.$state = { store }; diff --git a/src/plugins/data/public/query/filter_manager/lib/compare_filters.test.ts b/src/plugins/data/public/query/filter_manager/lib/compare_filters.test.ts index e8244feb988b6e..6bde6b528d07bd 100644 --- a/src/plugins/data/public/query/filter_manager/lib/compare_filters.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/compare_filters.test.ts @@ -16,42 +16,48 @@ * specific language governing permissions and limitations * under the License. */ -import { buildQueryFilter, buildEmptyFilter, FilterStateStore } from '@kbn/es-query'; + import { compareFilters } from './compare_filters'; +import { esFilters } from '../../../../common/es_query'; describe('filter manager utilities', () => { describe('compare filters', () => { test('should compare filters', () => { - const f1 = buildQueryFilter( + const f1 = esFilters.buildQueryFilter( { _type: { match: { query: 'apache', type: 'phrase' } } }, - 'index' + 'index', + '' ); - const f2 = buildEmptyFilter(true); + const f2 = esFilters.buildEmptyFilter(true); expect(compareFilters(f1, f2)).toBeFalsy(); }); test('should compare duplicates', () => { - const f1 = buildQueryFilter( + const f1 = esFilters.buildQueryFilter( { _type: { match: { query: 'apache', type: 'phrase' } } }, - 'index' + 'index', + '' ); - const f2 = buildQueryFilter( + const f2 = esFilters.buildQueryFilter( { _type: { match: { query: 'apache', type: 'phrase' } } }, - 'index' + 'index', + '' ); expect(compareFilters(f1, f2)).toBeTruthy(); }); test('should compare duplicates, ignoring meta attributes', () => { - const f1 = buildQueryFilter( + const f1 = esFilters.buildQueryFilter( { _type: { match: { query: 'apache', type: 'phrase' } } }, - 'index1' + 'index1', + '' ); - const f2 = buildQueryFilter( + const f2 = esFilters.buildQueryFilter( { _type: { match: { query: 'apache', type: 'phrase' } } }, - 'index2' + 'index2', + '' ); expect(compareFilters(f1, f2)).toBeTruthy(); @@ -59,12 +65,20 @@ describe('filter manager utilities', () => { test('should compare duplicates, ignoring $state attributes', () => { const f1 = { - $state: { store: FilterStateStore.APP_STATE }, - ...buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), + $state: { store: esFilters.FilterStateStore.APP_STATE }, + ...esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), }; const f2 = { - $state: { store: FilterStateStore.GLOBAL_STATE }, - ...buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), + $state: { store: esFilters.FilterStateStore.GLOBAL_STATE }, + ...esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), }; expect(compareFilters(f1, f2)).toBeTruthy(); diff --git a/src/plugins/data/public/query/filter_manager/lib/compare_filters.ts b/src/plugins/data/public/query/filter_manager/lib/compare_filters.ts index 44bc333ae2b4fb..2a7cbe6e3303b5 100644 --- a/src/plugins/data/public/query/filter_manager/lib/compare_filters.ts +++ b/src/plugins/data/public/query/filter_manager/lib/compare_filters.ts @@ -17,8 +17,8 @@ * under the License. */ -import { Filter, FilterMeta } from '@kbn/es-query'; import { defaults, isEqual, omit } from 'lodash'; +import { esFilters } from '../../../../common/es_query'; /** * Compare two filters to see if they match @@ -29,10 +29,14 @@ import { defaults, isEqual, omit } from 'lodash'; * * @returns {bool} Filters are the same */ -export const compareFilters = (first: Filter, second: Filter, comparatorOptions: any = {}) => { +export const compareFilters = ( + first: esFilters.Filter, + second: esFilters.Filter, + comparatorOptions: any = {} +) => { let comparators: any = {}; - const mapFilter = (filter: Filter) => { - const cleaned: FilterMeta = omit(filter, excludedAttributes); + const mapFilter = (filter: esFilters.Filter) => { + const cleaned: esFilters.FilterMeta = omit(filter, excludedAttributes); if (comparators.negate) cleaned.negate = filter.meta && Boolean(filter.meta.negate); if (comparators.disabled) cleaned.disabled = filter.meta && Boolean(filter.meta.disabled); diff --git a/src/plugins/data/public/query/filter_manager/lib/dedup_filters.test.ts b/src/plugins/data/public/query/filter_manager/lib/dedup_filters.test.ts index 75bd9d5dfbd81a..9b493add0886c6 100644 --- a/src/plugins/data/public/query/filter_manager/lib/dedup_filters.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/dedup_filters.test.ts @@ -17,19 +17,27 @@ * under the License. */ -import { Filter, buildRangeFilter, FilterStateStore, buildQueryFilter } from '@kbn/es-query'; import { dedupFilters } from './dedup_filters'; +import { esFilters } from '../../../../common/es_query'; describe('filter manager utilities', () => { describe('dedupFilters(existing, filters)', () => { test('should return only filters which are not in the existing', () => { - const existing: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index'), - buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), + const existing: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index', ''), + esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), ]; - const filters: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index'), - buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), + const filters: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index', ''), + esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), ]; const results = dedupFilters(existing, filters); @@ -38,16 +46,24 @@ describe('filter manager utilities', () => { }); test('should ignore the disabled attribute when comparing ', () => { - const existing: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index'), + const existing: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index', ''), { - ...buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), + ...esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index1', + '' + ), meta: { disabled: true, negate: false, alias: null }, }, ]; - const filters: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index'), - buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), + const filters: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index', ''), + esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index1', + '' + ), ]; const results = dedupFilters(existing, filters); @@ -56,18 +72,26 @@ describe('filter manager utilities', () => { }); test('should ignore $state attribute', () => { - const existing: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index'), + const existing: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 0, to: 1024 }, 'index', ''), { - ...buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), - $state: { store: FilterStateStore.APP_STATE }, + ...esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), + $state: { store: esFilters.FilterStateStore.APP_STATE }, }, ]; - const filters: Filter[] = [ - buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index'), + const filters: esFilters.Filter[] = [ + esFilters.buildRangeFilter({ name: 'bytes' }, { from: 1024, to: 2048 }, 'index', ''), { - ...buildQueryFilter({ match: { _term: { query: 'apache', type: 'phrase' } } }, 'index'), - $state: { store: FilterStateStore.GLOBAL_STATE }, + ...esFilters.buildQueryFilter( + { match: { _term: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), + $state: { store: esFilters.FilterStateStore.GLOBAL_STATE }, }, ]; const results = dedupFilters(existing, filters); diff --git a/src/plugins/data/public/query/filter_manager/lib/dedup_filters.ts b/src/plugins/data/public/query/filter_manager/lib/dedup_filters.ts index 9565cbd80b7791..6d6f49cb5e8338 100644 --- a/src/plugins/data/public/query/filter_manager/lib/dedup_filters.ts +++ b/src/plugins/data/public/query/filter_manager/lib/dedup_filters.ts @@ -17,9 +17,9 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { filter, find } from 'lodash'; import { compareFilters } from './compare_filters'; +import { esFilters } from '../../../../../../plugins/data/public'; /** * Combine 2 filter collections, removing duplicates @@ -31,8 +31,8 @@ import { compareFilters } from './compare_filters'; * @returns {object} An array of filters that were not in existing */ export const dedupFilters = ( - existingFilters: Filter[], - filters: Filter[], + existingFilters: esFilters.Filter[], + filters: esFilters.Filter[], comparatorOptions: any = {} ) => { if (!Array.isArray(filters)) { @@ -41,8 +41,8 @@ export const dedupFilters = ( return filter( filters, - (f: Filter) => - !find(existingFilters, (existingFilter: Filter) => + (f: esFilters.Filter) => + !find(existingFilters, (existingFilter: esFilters.Filter) => compareFilters(existingFilter, f, comparatorOptions) ) ); diff --git a/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.test.ts b/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.test.ts index c0c509634aba2c..dfe3a093c66146 100644 --- a/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.test.ts @@ -18,8 +18,8 @@ */ import sinon from 'sinon'; -import { Filter, buildEmptyFilter } from '@kbn/es-query'; import { generateMappingChain } from './generate_mapping_chain'; +import { esFilters } from '../../../../../../plugins/data/public'; describe('filter manager utilities', () => { let mapping: any; @@ -32,7 +32,7 @@ describe('filter manager utilities', () => { describe('generateMappingChain()', () => { test('should create a chaining function which calls the next function if the error is thrown', async () => { - const filter: Filter = buildEmptyFilter(true); + const filter = esFilters.buildEmptyFilter(true); mapping.throws(filter); next.returns('good'); @@ -45,7 +45,7 @@ describe('filter manager utilities', () => { }); test('should create a chaining function which DOES NOT call the next function if the result is returned', async () => { - const filter: Filter = buildEmptyFilter(true); + const filter = esFilters.buildEmptyFilter(true); mapping.returns('good'); next.returns('bad'); @@ -57,7 +57,7 @@ describe('filter manager utilities', () => { }); test('should resolve result for the mapping function', async () => { - const filter: Filter = buildEmptyFilter(true); + const filter = esFilters.buildEmptyFilter(true); mapping.returns({ key: 'test', value: 'example' }); @@ -70,7 +70,7 @@ describe('filter manager utilities', () => { test('should call the mapping function with the argument to the chain', async () => { // @ts-ignore - const filter: Filter = { test: 'example' }; + const filter: esFilters.Filter = { test: 'example' }; mapping.returns({ key: 'test', value: 'example' }); @@ -84,7 +84,7 @@ describe('filter manager utilities', () => { }); test('should resolve result for the next function', async () => { - const filter: Filter = buildEmptyFilter(true); + const filter = esFilters.buildEmptyFilter(true); mapping.throws(filter); next.returns({ key: 'test', value: 'example' }); @@ -98,7 +98,7 @@ describe('filter manager utilities', () => { }); test('should throw an error if no functions match', async done => { - const filter: Filter = buildEmptyFilter(true); + const filter = esFilters.buildEmptyFilter(true); mapping.throws(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.ts b/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.ts index 760270edd7170a..b6764389e0db9f 100644 --- a/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.ts +++ b/src/plugins/data/public/query/filter_manager/lib/generate_mapping_chain.ts @@ -16,14 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; const noop = () => { throw new Error('No mappings have been found for filter.'); }; export const generateMappingChain = (fn: Function, next: Function = noop) => { - return (filter: Filter) => { + return (filter: esFilters.Filter) => { try { return fn(filter); } catch (result) { diff --git a/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.test.ts b/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.test.ts index fce2aa0373ebe8..9a0d0d93698f69 100644 --- a/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.test.ts @@ -17,14 +17,14 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { mapAndFlattenFilters } from './map_and_flatten_filters'; +import { esFilters } from '../../../../../data/public'; describe('filter manager utilities', () => { describe('mapAndFlattenFilters()', () => { let filters: unknown; - function getDisplayName(filter: Filter) { + function getDisplayName(filter: esFilters.Filter) { return typeof filter.meta.value === 'function' ? filter.meta.value() : filter.meta.value; } @@ -45,7 +45,7 @@ describe('filter manager utilities', () => { }); test('should map and flatten the filters', () => { - const results = mapAndFlattenFilters(filters as Filter[]); + const results = mapAndFlattenFilters(filters as esFilters.Filter[]); expect(results).toHaveLength(5); expect(results[0]).toHaveProperty('meta'); diff --git a/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.ts b/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.ts index b350c3957b142d..5326d59f3e32b6 100644 --- a/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.ts +++ b/src/plugins/data/public/query/filter_manager/lib/map_and_flatten_filters.ts @@ -18,9 +18,9 @@ */ import { compact, flatten } from 'lodash'; -import { Filter } from '@kbn/es-query'; import { mapFilter } from './map_filter'; +import { esFilters } from '../../../../../data/public'; -export const mapAndFlattenFilters = (filters: Filter[]) => { - return compact(flatten(filters)).map((item: Filter) => mapFilter(item)); +export const mapAndFlattenFilters = (filters: esFilters.Filter[]) => { + return compact(flatten(filters)).map((item: esFilters.Filter) => mapFilter(item)); }; diff --git a/src/plugins/data/public/query/filter_manager/lib/map_filter.test.ts b/src/plugins/data/public/query/filter_manager/lib/map_filter.test.ts index c1d4ebfd3f7fc8..0d115125451eea 100644 --- a/src/plugins/data/public/query/filter_manager/lib/map_filter.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/map_filter.test.ts @@ -17,11 +17,11 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { mapFilter } from './map_filter'; +import { esFilters } from '../../../../../data/public'; describe('filter manager utilities', () => { - function getDisplayName(filter: Filter) { + function getDisplayName(filter: esFilters.Filter) { return typeof filter.meta.value === 'function' ? filter.meta.value() : filter.meta.value; } @@ -31,7 +31,7 @@ describe('filter manager utilities', () => { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } }, }; - const after = mapFilter(before as Filter); + const after = mapFilter(before as esFilters.Filter); expect(after).toHaveProperty('meta'); expect(after.meta).toHaveProperty('key', '_type'); @@ -43,7 +43,7 @@ describe('filter manager utilities', () => { test('should map exists filters', async () => { const before: any = { meta: { index: 'logstash-*' }, exists: { field: '@timestamp' } }; - const after = mapFilter(before as Filter); + const after = mapFilter(before as esFilters.Filter); expect(after).toHaveProperty('meta'); expect(after.meta).toHaveProperty('key', '@timestamp'); @@ -55,7 +55,7 @@ describe('filter manager utilities', () => { test('should map missing filters', async () => { const before: any = { meta: { index: 'logstash-*' }, missing: { field: '@timestamp' } }; - const after = mapFilter(before as Filter); + const after = mapFilter(before as esFilters.Filter); expect(after).toHaveProperty('meta'); expect(after.meta).toHaveProperty('key', '@timestamp'); @@ -67,7 +67,7 @@ describe('filter manager utilities', () => { test('should map json filter', async () => { const before: any = { meta: { index: 'logstash-*' }, query: { match_all: {} } }; - const after = mapFilter(before as Filter); + const after = mapFilter(before as esFilters.Filter); expect(after).toHaveProperty('meta'); expect(after.meta).toHaveProperty('key', 'query'); @@ -81,7 +81,7 @@ describe('filter manager utilities', () => { const before: any = { meta: { index: 'logstash-*' } }; try { - mapFilter(before as Filter); + mapFilter(before as esFilters.Filter); } catch (e) { expect(e).toBeInstanceOf(Error); expect(e.message).toBe('No mappings have been found for filter.'); diff --git a/src/plugins/data/public/query/filter_manager/lib/map_filter.ts b/src/plugins/data/public/query/filter_manager/lib/map_filter.ts index cda9591e40b33e..2dc855caabfd36 100644 --- a/src/plugins/data/public/query/filter_manager/lib/map_filter.ts +++ b/src/plugins/data/public/query/filter_manager/lib/map_filter.ts @@ -17,7 +17,6 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { reduceRight } from 'lodash'; import { mapMatchAll } from './mappers/map_match_all'; @@ -31,8 +30,9 @@ import { mapGeoBoundingBox } from './mappers/map_geo_bounding_box'; import { mapGeoPolygon } from './mappers/map_geo_polygon'; import { mapDefault } from './mappers/map_default'; import { generateMappingChain } from './generate_mapping_chain'; +import { esFilters } from '../../../../../data/public'; -export function mapFilter(filter: Filter) { +export function mapFilter(filter: esFilters.Filter) { /** Mappers **/ // Each mapper is a simple promise function that test if the mapper can diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.test.ts index acb6e89711033d..f10766901e5b7b 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.test.ts @@ -16,13 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -import { CustomFilter, buildEmptyFilter, buildQueryFilter } from '@kbn/es-query'; + import { mapDefault } from './map_default'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapDefault()', () => { test('should return the key and value for matching filters', async () => { - const filter: CustomFilter = buildQueryFilter({ match_all: {} }, 'index'); + const filter = esFilters.buildQueryFilter({ match_all: {} }, 'index', ''); const result = mapDefault(filter); expect(result).toHaveProperty('key', 'query'); @@ -30,7 +31,7 @@ describe('filter manager utilities', () => { }); test('should return undefined if there is no valid key', async () => { - const filter = buildEmptyFilter(true) as CustomFilter; + const filter = esFilters.buildEmptyFilter(true); try { mapDefault(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.ts index 70c191879c22e9..fd84c5c742589b 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_default.ts @@ -17,15 +17,15 @@ * under the License. */ -import { Filter, FILTERS } from '@kbn/es-query'; import { find, keys, get } from 'lodash'; +import { esFilters } from '../../../../../common/es_query'; -export const mapDefault = (filter: Filter) => { +export const mapDefault = (filter: esFilters.Filter) => { const metaProperty = /(^\$|meta)/; const key = find(keys(filter), item => !item.match(metaProperty)); if (key) { - const type = FILTERS.CUSTOM; + const type = esFilters.FILTERS.CUSTOM; const value = JSON.stringify(get(filter, key, {})); return { type, key, value }; diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.test.ts index c352d3e2b9a734..ff0ed4f4e4d94a 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.test.ts @@ -16,14 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -import { ExistsFilter, buildEmptyFilter, buildExistsFilter } from '@kbn/es-query'; + import { mapExists } from './map_exists'; import { mapQueryString } from './map_query_string'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapExists()', () => { test('should return the key and value for matching filters', async () => { - const filter: ExistsFilter = buildExistsFilter({ name: '_type' }, 'index'); + const filter = esFilters.buildExistsFilter({ name: '_type' }, 'index'); const result = mapExists(filter); expect(result).toHaveProperty('key', '_type'); @@ -31,7 +32,7 @@ describe('filter manager utilities', () => { }); test('should return undefined for none matching', async done => { - const filter = buildEmptyFilter(true) as ExistsFilter; + const filter = esFilters.buildEmptyFilter(true); try { mapQueryString(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.ts index d539219a1ca24e..63665bdd88ccbe 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_exists.ts @@ -17,14 +17,14 @@ * under the License. */ -import { Filter, isExistsFilter, FILTERS } from '@kbn/es-query'; import { get } from 'lodash'; +import { esFilters } from '../../../../../common/es_query'; -export const mapExists = (filter: Filter) => { - if (isExistsFilter(filter)) { +export const mapExists = (filter: esFilters.Filter) => { + if (esFilters.isExistsFilter(filter)) { return { - type: FILTERS.EXISTS, - value: FILTERS.EXISTS, + type: esFilters.FILTERS.EXISTS, + value: esFilters.FILTERS.EXISTS, key: get(filter, 'exists.field'), }; } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.test.ts index c3c99e6f6c4a37..5fca4a652bad88 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.test.ts @@ -18,7 +18,7 @@ */ import { mapGeoBoundingBox } from './map_geo_bounding_box'; -import { Filter, GeoBoundingBoxFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapGeoBoundingBox()', () => { @@ -34,7 +34,7 @@ describe('filter manager utilities', () => { bottom_right: { lat: 15, lon: 20 }, }, }, - } as GeoBoundingBoxFilter; + } as esFilters.GeoBoundingBoxFilter; const result = mapGeoBoundingBox(filter); @@ -63,7 +63,8 @@ describe('filter manager utilities', () => { bottom_right: { lat: 15, lon: 20 }, }, }, - } as GeoBoundingBoxFilter; + } as esFilters.GeoBoundingBoxFilter; + const result = mapGeoBoundingBox(filter); expect(result).toHaveProperty('key', 'point'); @@ -82,7 +83,7 @@ describe('filter manager utilities', () => { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } }, - } as Filter; + } as esFilters.Filter; try { mapGeoBoundingBox(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.ts index 1f9b8cd842509b..091e9a3f34000a 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_bounding_box.ts @@ -16,16 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -import { - GeoBoundingBoxFilter, - Filter, - FILTERS, - isGeoBoundingBoxFilter, - FilterValueFormatter, -} from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; const getFormattedValueFn = (params: any) => { - return (formatter?: FilterValueFormatter) => { + return (formatter?: esFilters.FilterValueFormatter) => { const corners = formatter ? { topLeft: formatter.convert(params.top_left), @@ -40,20 +34,20 @@ const getFormattedValueFn = (params: any) => { }; }; -const getParams = (filter: GeoBoundingBoxFilter) => { +const getParams = (filter: esFilters.GeoBoundingBoxFilter) => { const key = Object.keys(filter.geo_bounding_box).filter(k => k !== 'ignore_unmapped')[0]; const params = filter.geo_bounding_box[key]; return { key, params, - type: FILTERS.GEO_BOUNDING_BOX, + type: esFilters.FILTERS.GEO_BOUNDING_BOX, value: getFormattedValueFn(params), }; }; -export const mapGeoBoundingBox = (filter: Filter) => { - if (!isGeoBoundingBoxFilter(filter)) { +export const mapGeoBoundingBox = (filter: esFilters.Filter) => { + if (!esFilters.isGeoBoundingBoxFilter(filter)) { throw filter; } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.test.ts index ee4f9b295d682d..3afa3891a24bb8 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.test.ts @@ -16,23 +16,28 @@ * specific language governing permissions and limitations * under the License. */ + import { mapGeoPolygon } from './map_geo_polygon'; -import { GeoPolygonFilter, Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { - describe('mapGeoPolygon()', () => { - test('should return the key and value for matching filters with bounds', async () => { - const filter = { - meta: { - index: 'logstash-*', - }, - geo_polygon: { - point: { - points: [{ lat: 5, lon: 10 }, { lat: 15, lon: 20 }], - }, + let filter: esFilters.GeoPolygonFilter; + + beforeEach(() => { + filter = { + meta: { + index: 'logstash-*', + }, + geo_polygon: { + point: { + points: [{ lat: 5, lon: 10 }, { lat: 15, lon: 20 }], }, - } as GeoPolygonFilter; + }, + } as esFilters.GeoPolygonFilter; + }); + describe('mapGeoPolygon()', () => { + test('should return the key and value for matching filters with bounds', async () => { const result = mapGeoPolygon(filter); expect(result).toHaveProperty('key', 'point'); @@ -48,17 +53,6 @@ describe('filter manager utilities', () => { }); test('should return the key and value even when using ignore_unmapped', async () => { - const filter = { - meta: { - index: 'logstash-*', - }, - geo_polygon: { - ignore_unmapped: true, - point: { - points: [{ lat: 5, lon: 10 }, { lat: 15, lon: 20 }], - }, - }, - } as GeoPolygonFilter; const result = mapGeoPolygon(filter); expect(result).toHaveProperty('key', 'point'); @@ -74,15 +68,15 @@ describe('filter manager utilities', () => { }); test('should return undefined for none matching', async done => { - const filter = { + const wrongFilter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } }, - } as Filter; + } as esFilters.Filter; try { - mapGeoPolygon(filter); + mapGeoPolygon(wrongFilter); } catch (e) { - expect(e).toBe(filter); + expect(e).toBe(wrongFilter); done(); } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.ts index 03ce4130d0c972..a7881b4a145a19 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_geo_polygon.ts @@ -16,38 +16,33 @@ * specific language governing permissions and limitations * under the License. */ -import { - GeoPolygonFilter, - Filter, - FILTERS, - isGeoPolygonFilter, - FilterValueFormatter, -} from '@kbn/es-query'; + +import { esFilters } from '../../../../../common/es_query'; const POINTS_SEPARATOR = ', '; const getFormattedValueFn = (points: string[]) => { - return (formatter?: FilterValueFormatter) => { + return (formatter?: esFilters.FilterValueFormatter) => { return points .map((point: string) => (formatter ? formatter.convert(point) : JSON.stringify(point))) .join(POINTS_SEPARATOR); }; }; -function getParams(filter: GeoPolygonFilter) { +function getParams(filter: esFilters.GeoPolygonFilter) { const key = Object.keys(filter.geo_polygon).filter(k => k !== 'ignore_unmapped')[0]; const params = filter.geo_polygon[key]; return { key, params, - type: FILTERS.GEO_POLYGON, + type: esFilters.FILTERS.GEO_POLYGON, value: getFormattedValueFn(params.points || []), }; } -export function mapGeoPolygon(filter: Filter) { - if (!isGeoPolygonFilter(filter)) { +export function mapGeoPolygon(filter: esFilters.Filter) { + if (!esFilters.isGeoPolygonFilter(filter)) { throw filter; } return getParams(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.test.ts index 2f0641598a2ce3..4fc6d0b4924141 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.test.ts @@ -16,12 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -import { MatchAllFilter } from '@kbn/es-query'; + import { mapMatchAll } from './map_match_all'; +import { esFilters } from '../../../../../common/es_query'; describe('filter_manager/lib', () => { describe('mapMatchAll()', () => { - let filter: MatchAllFilter; + let filter: esFilters.MatchAllFilter; beforeEach(() => { filter = { diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.ts index a1387e6dbe4574..4e93b1d41e9a82 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_match_all.ts @@ -16,12 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter, FILTERS, isMatchAllFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; -export const mapMatchAll = (filter: Filter) => { - if (isMatchAllFilter(filter)) { +export const mapMatchAll = (filter: esFilters.Filter) => { + if (esFilters.isMatchAllFilter(filter)) { return { - type: FILTERS.MATCH_ALL, + type: esFilters.FILTERS.MATCH_ALL, key: filter.meta.field, value: filter.meta.formattedValue || 'all', }; diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.test.ts index ca23f25826906b..1847eb37ca42ff 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.test.ts @@ -16,15 +16,16 @@ * specific language governing permissions and limitations * under the License. */ -import { MissingFilter, buildEmptyFilter, ExistsFilter } from '@kbn/es-query'; + import { mapMissing } from './map_missing'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapMissing()', () => { test('should return the key and value for matching filters', async () => { - const filter: MissingFilter = { + const filter: esFilters.MissingFilter = { missing: { field: '_type' }, - ...buildEmptyFilter(true), + ...esFilters.buildEmptyFilter(true), }; const result = mapMissing(filter); @@ -33,7 +34,7 @@ describe('filter manager utilities', () => { }); test('should return undefined for none matching', async done => { - const filter = buildEmptyFilter(true) as ExistsFilter; + const filter = esFilters.buildEmptyFilter(true); try { mapMissing(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.ts index 861a84ed616468..51dee89ad884b2 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_missing.ts @@ -16,13 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter, FILTERS, isMissingFilter } from '@kbn/es-query'; -export const mapMissing = (filter: Filter) => { - if (isMissingFilter(filter)) { +import { esFilters } from '../../../../../common/es_query'; + +export const mapMissing = (filter: esFilters.Filter) => { + if (esFilters.isMissingFilter(filter)) { return { - type: FILTERS.MISSING, - value: FILTERS.MISSING, + type: esFilters.FILTERS.MISSING, + value: esFilters.FILTERS.MISSING, key: filter.missing.field, }; } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.test.ts index c95a2529add149..05372d37264b06 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.test.ts @@ -17,7 +17,7 @@ * under the License. */ import { mapPhrase } from './map_phrase'; -import { PhraseFilter, Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapPhrase()', () => { @@ -25,11 +25,13 @@ describe('filter manager utilities', () => { const filter = { meta: { index: 'logstash-*' }, query: { match: { _type: { query: 'apache', type: 'phrase' } } }, - } as PhraseFilter; + } as esFilters.PhraseFilter; + const result = mapPhrase(filter); expect(result).toHaveProperty('value'); expect(result).toHaveProperty('key', '_type'); + if (result.value) { const displayName = result.value(); expect(displayName).toBe('apache'); @@ -40,7 +42,7 @@ describe('filter manager utilities', () => { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } }, - } as Filter; + } as esFilters.Filter; try { mapPhrase(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.ts index efa348c9ad3206..b6e9c2007db970 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrase.ts @@ -18,45 +18,36 @@ */ import { get } from 'lodash'; -import { - PhraseFilter, - Filter, - FILTERS, - isPhraseFilter, - isScriptedPhraseFilter, - getPhraseFilterField, - getPhraseFilterValue, - FilterValueFormatter, -} from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; -const getScriptedPhraseValue = (filter: PhraseFilter) => +const getScriptedPhraseValue = (filter: esFilters.PhraseFilter) => get(filter, ['script', 'script', 'params', 'value']); const getFormattedValueFn = (value: any) => { - return (formatter?: FilterValueFormatter) => { + return (formatter?: esFilters.FilterValueFormatter) => { return formatter ? formatter.convert(value) : value; }; }; -const getParams = (filter: PhraseFilter) => { +const getParams = (filter: esFilters.PhraseFilter) => { const scriptedPhraseValue = getScriptedPhraseValue(filter); const isScriptedFilter = Boolean(scriptedPhraseValue); - const key = isScriptedFilter ? filter.meta.field || '' : getPhraseFilterField(filter); - const query = scriptedPhraseValue || getPhraseFilterValue(filter); + const key = isScriptedFilter ? filter.meta.field || '' : esFilters.getPhraseFilterField(filter); + const query = scriptedPhraseValue || esFilters.getPhraseFilterValue(filter); const params = { query }; return { key, params, - type: FILTERS.PHRASE, + type: esFilters.FILTERS.PHRASE, value: getFormattedValueFn(query), }; }; -export const isMapPhraseFilter = (filter: any): filter is PhraseFilter => - isPhraseFilter(filter) || isScriptedPhraseFilter(filter); +export const isMapPhraseFilter = (filter: any): filter is esFilters.PhraseFilter => + esFilters.isPhraseFilter(filter) || esFilters.isScriptedPhraseFilter(filter); -export const mapPhrase = (filter: Filter) => { +export const mapPhrase = (filter: esFilters.Filter) => { if (!isMapPhraseFilter(filter)) { throw filter; } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts index c17ff11d49fd4a..7240d87d02b5ab 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_phrases.ts @@ -17,10 +17,10 @@ * under the License. */ -import { Filter, isPhrasesFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; -export const mapPhrases = (filter: Filter) => { - if (!isPhrasesFilter(filter)) { +export const mapPhrases = (filter: esFilters.Filter) => { + if (!esFilters.isPhrasesFilter(filter)) { throw filter; } diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.test.ts index 4b1a5d39c405da..c60e7d3454fe0f 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.test.ts @@ -17,27 +17,28 @@ * under the License. */ -import { QueryStringFilter, buildQueryFilter, buildEmptyFilter } from '@kbn/es-query'; import { mapQueryString } from './map_query_string'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapQueryString()', () => { test('should return the key and value for matching filters', async () => { - const filter: QueryStringFilter = buildQueryFilter( + const filter = esFilters.buildQueryFilter( { query_string: { query: 'foo:bar' } }, - 'index' + 'index', + '' ); - const result = mapQueryString(filter); + const result = mapQueryString(filter as esFilters.Filter); expect(result).toHaveProperty('key', 'query'); expect(result).toHaveProperty('value', 'foo:bar'); }); test('should return undefined for none matching', async done => { - const filter = buildEmptyFilter(true) as QueryStringFilter; + const filter = esFilters.buildEmptyFilter(true); try { - mapQueryString(filter); + mapQueryString(filter as esFilters.Filter); } catch (e) { expect(e).toBe(filter); done(); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.ts index 94da8074edd04d..20c3555639a3ed 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_query_string.ts @@ -16,12 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter, FILTERS, isQueryStringFilter } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; -export const mapQueryString = (filter: Filter) => { - if (isQueryStringFilter(filter)) { +export const mapQueryString = (filter: esFilters.Filter) => { + if (esFilters.isQueryStringFilter(filter)) { return { - type: FILTERS.QUERY_STRING, + type: esFilters.FILTERS.QUERY_STRING, key: 'query', value: filter.query.query_string.query, }; diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.test.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.test.ts index 12d2919e2d47b6..c0d5773d6f2c14 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.test.ts @@ -18,30 +18,15 @@ */ import { mapRange } from './map_range'; -import { RangeFilter, Filter, FilterMeta } from '@kbn/es-query'; +import { esFilters } from '../../../../../common/es_query'; describe('filter manager utilities', () => { describe('mapRange()', () => { test('should return the key and value for matching filters with gt/lt', async () => { const filter = { - meta: { index: 'logstash-*' } as FilterMeta, + meta: { index: 'logstash-*' } as esFilters.FilterMeta, range: { bytes: { lt: 2048, gt: 1024 } }, - } as RangeFilter; - const result = mapRange(filter); - - expect(result).toHaveProperty('key', 'bytes'); - expect(result).toHaveProperty('value'); - if (result.value) { - const displayName = result.value(); - expect(displayName).toBe('1024 to 2048'); - } - }); - - test('should return the key and value for matching filters with gte/lte', async () => { - const filter = { - meta: { index: 'logstash-*' } as FilterMeta, - range: { bytes: { lte: 2048, gte: 1024 } }, - } as RangeFilter; + } as esFilters.RangeFilter; const result = mapRange(filter); expect(result).toHaveProperty('key', 'bytes'); @@ -56,7 +41,7 @@ describe('filter manager utilities', () => { const filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } }, - } as Filter; + } as esFilters.Filter; try { mapRange(filter); diff --git a/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.ts b/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.ts index 76f9d3621e1717..51fb970f5f03ea 100644 --- a/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.ts +++ b/src/plugins/data/public/query/filter_manager/lib/mappers/map_range.ts @@ -17,18 +17,11 @@ * under the License. */ -import { - Filter, - RangeFilter, - FILTERS, - isRangeFilter, - isScriptedRangeFilter, - FilterValueFormatter, -} from '@kbn/es-query'; import { get, has } from 'lodash'; +import { esFilters } from '../../../../../common/es_query'; const getFormattedValueFn = (left: any, right: any) => { - return (formatter?: FilterValueFormatter) => { + return (formatter?: esFilters.FilterValueFormatter) => { let displayValue = `${left} to ${right}`; if (formatter) { const convert = formatter.getConverterFor('text'); @@ -38,11 +31,12 @@ const getFormattedValueFn = (left: any, right: any) => { }; }; -const getFirstRangeKey = (filter: RangeFilter) => filter.range && Object.keys(filter.range)[0]; -const getRangeByKey = (filter: RangeFilter, key: string) => get(filter, ['range', key]); +const getFirstRangeKey = (filter: esFilters.RangeFilter) => + filter.range && Object.keys(filter.range)[0]; +const getRangeByKey = (filter: esFilters.RangeFilter, key: string) => get(filter, ['range', key]); -function getParams(filter: RangeFilter) { - const isScriptedRange = isScriptedRangeFilter(filter); +function getParams(filter: esFilters.RangeFilter) { + const isScriptedRange = esFilters.isScriptedRangeFilter(filter); const key: string = (isScriptedRange ? filter.meta.field : getFirstRangeKey(filter)) || ''; const params: any = isScriptedRange ? get(filter, 'script.script.params') @@ -56,13 +50,13 @@ function getParams(filter: RangeFilter) { const value = getFormattedValueFn(left, right); - return { type: FILTERS.RANGE, key, value, params }; + return { type: esFilters.FILTERS.RANGE, key, value, params }; } -export const isMapRangeFilter = (filter: any): filter is RangeFilter => - isRangeFilter(filter) || isScriptedRangeFilter(filter); +export const isMapRangeFilter = (filter: any): filter is esFilters.RangeFilter => + esFilters.isRangeFilter(filter) || esFilters.isScriptedRangeFilter(filter); -export const mapRange = (filter: Filter) => { +export const mapRange = (filter: esFilters.Filter) => { if (!isMapRangeFilter(filter)) { throw filter; } diff --git a/src/plugins/data/public/query/filter_manager/lib/only_disabled.test.ts b/src/plugins/data/public/query/filter_manager/lib/only_disabled.test.ts index 3fedcf97a625ac..b9731797c9ee36 100644 --- a/src/plugins/data/public/query/filter_manager/lib/only_disabled.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/only_disabled.test.ts @@ -17,8 +17,8 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { onlyDisabledFiltersChanged } from './only_disabled'; +import { esFilters } from '../../../../../data/public'; describe('filter manager utilities', () => { describe('onlyDisabledFiltersChanged()', () => { @@ -27,20 +27,20 @@ describe('filter manager utilities', () => { { meta: { disabled: true } }, { meta: { disabled: true } }, { meta: { disabled: true } }, - ] as Filter[]; - const newFilters = [{ meta: { disabled: true } }] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [{ meta: { disabled: true } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(true); }); test('should return false if there are no old filters', () => { - const newFilters = [{ meta: { disabled: false } }] as Filter[]; + const newFilters = [{ meta: { disabled: false } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, undefined)).toBe(false); }); test('should return false if there are no new filters', () => { - const filters = [{ meta: { disabled: false } }] as Filter[]; + const filters = [{ meta: { disabled: false } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(undefined, filters)).toBe(false); }); @@ -50,8 +50,8 @@ describe('filter manager utilities', () => { { meta: { disabled: false } }, { meta: { disabled: false } }, { meta: { disabled: false } }, - ] as Filter[]; - const newFilters = [{ meta: { disabled: false } }] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [{ meta: { disabled: false } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); @@ -61,8 +61,8 @@ describe('filter manager utilities', () => { { meta: { disabled: true } }, { meta: { disabled: true } }, { meta: { disabled: true } }, - ] as Filter[]; - const newFilters = [{ meta: { disabled: false } }] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [{ meta: { disabled: false } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); @@ -72,8 +72,8 @@ describe('filter manager utilities', () => { { meta: { disabled: false } }, { meta: { disabled: false } }, { meta: { disabled: false } }, - ] as Filter[]; - const newFilters = [{ meta: { disabled: true } }] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [{ meta: { disabled: true } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); @@ -83,8 +83,8 @@ describe('filter manager utilities', () => { { meta: { disabled: true } }, { meta: { disabled: true } }, { meta: { disabled: true } }, - ] as Filter[]; - const newFilters = [] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(true); }); @@ -94,8 +94,8 @@ describe('filter manager utilities', () => { { meta: { disabled: false } }, { meta: { disabled: false } }, { meta: { disabled: false } }, - ] as Filter[]; - const newFilters = [] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); @@ -104,11 +104,11 @@ describe('filter manager utilities', () => { const filters = [ { meta: { disabled: true, negate: false } }, { meta: { disabled: true, negate: false } }, - ] as Filter[]; + ] as esFilters.Filter[]; const newFilters = [ { meta: { disabled: true, negate: true } }, { meta: { disabled: true, negate: true } }, - ] as Filter[]; + ] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(true); }); @@ -118,8 +118,8 @@ describe('filter manager utilities', () => { { meta: { disabled: false } }, { meta: { disabled: false } }, { meta: { disabled: true } }, - ] as Filter[]; - const newFilters = [{ meta: { disabled: false } }] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [{ meta: { disabled: false } }] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); @@ -129,15 +129,15 @@ describe('filter manager utilities', () => { { meta: { disabled: true } }, { meta: { disabled: false } }, { meta: { disabled: true } }, - ] as Filter[]; - const newFilters = [] as Filter[]; + ] as esFilters.Filter[]; + const newFilters = [] as esFilters.Filter[]; expect(onlyDisabledFiltersChanged(newFilters, filters)).toBe(false); }); test('should not throw with null filters', () => { - const filters = [null, { meta: { disabled: true } }] as Filter[]; - const newFilters = [] as Filter[]; + const filters = [null, { meta: { disabled: true } }] as esFilters.Filter[]; + const newFilters = [] as esFilters.Filter[]; expect(() => { onlyDisabledFiltersChanged(newFilters, filters); diff --git a/src/plugins/data/public/query/filter_manager/lib/only_disabled.ts b/src/plugins/data/public/query/filter_manager/lib/only_disabled.ts index 9c0b5f43acb3ef..0fb6894a297a1f 100644 --- a/src/plugins/data/public/query/filter_manager/lib/only_disabled.ts +++ b/src/plugins/data/public/query/filter_manager/lib/only_disabled.ts @@ -17,17 +17,20 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { filter, isEqual } from 'lodash'; +import { esFilters } from '../../../../../../plugins/data/public'; -const isEnabled = (f: Filter) => f && f.meta && !f.meta.disabled; +const isEnabled = (f: esFilters.Filter) => f && f.meta && !f.meta.disabled; /** * Checks to see if only disabled filters have been changed * * @returns {bool} Only disabled filters */ -export const onlyDisabledFiltersChanged = (newFilters?: Filter[], oldFilters?: Filter[]) => { +export const onlyDisabledFiltersChanged = ( + newFilters?: esFilters.Filter[], + oldFilters?: esFilters.Filter[] +) => { // If it's the same - compare only enabled filters const newEnabledFilters = filter(newFilters || [], isEnabled); const oldEnabledFilters = filter(oldFilters || [], isEnabled); diff --git a/src/plugins/data/public/query/filter_manager/lib/uniq_filters.test.ts b/src/plugins/data/public/query/filter_manager/lib/uniq_filters.test.ts index 86f059913cd96a..08eeabc1497e3c 100644 --- a/src/plugins/data/public/query/filter_manager/lib/uniq_filters.test.ts +++ b/src/plugins/data/public/query/filter_manager/lib/uniq_filters.test.ts @@ -16,15 +16,24 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter, buildQueryFilter, FilterStateStore } from '@kbn/es-query'; + import { uniqFilters } from './uniq_filters'; +import { esFilters } from '../../../../../data/public'; describe('filter manager utilities', () => { describe('niqFilter', () => { test('should filter out dups', () => { - const before: Filter[] = [ - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), + const before: esFilters.Filter[] = [ + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), ]; const results = uniqFilters(before); @@ -32,9 +41,17 @@ describe('filter manager utilities', () => { }); test('should filter out duplicates, ignoring meta attributes', () => { - const before: Filter[] = [ - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index1'), - buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index2'), + const before: esFilters.Filter[] = [ + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index1', + '' + ), + esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index2', + '' + ), ]; const results = uniqFilters(before); @@ -42,14 +59,22 @@ describe('filter manager utilities', () => { }); test('should filter out duplicates, ignoring $state attributes', () => { - const before: Filter[] = [ + const before: esFilters.Filter[] = [ { - $state: { store: FilterStateStore.APP_STATE }, - ...buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), + $state: { store: esFilters.FilterStateStore.APP_STATE }, + ...esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), }, { - $state: { store: FilterStateStore.GLOBAL_STATE }, - ...buildQueryFilter({ _type: { match: { query: 'apache', type: 'phrase' } } }, 'index'), + $state: { store: esFilters.FilterStateStore.GLOBAL_STATE }, + ...esFilters.buildQueryFilter( + { _type: { match: { query: 'apache', type: 'phrase' } } }, + 'index', + '' + ), }, ]; const results = uniqFilters(before); diff --git a/src/plugins/data/public/query/filter_manager/lib/uniq_filters.ts b/src/plugins/data/public/query/filter_manager/lib/uniq_filters.ts index 12e793253371e7..e96c52e6db3dec 100644 --- a/src/plugins/data/public/query/filter_manager/lib/uniq_filters.ts +++ b/src/plugins/data/public/query/filter_manager/lib/uniq_filters.ts @@ -16,9 +16,9 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter } from '@kbn/es-query'; import { each, union } from 'lodash'; import { dedupFilters } from './dedup_filters'; +import { esFilters } from '../../../../../data/public'; /** * Remove duplicate filters from an array of filters @@ -28,10 +28,10 @@ import { dedupFilters } from './dedup_filters'; * @returns {object} The original filters array with duplicates removed */ -export const uniqFilters = (filters: Filter[], comparatorOptions: any = {}) => { - let results: Filter[] = []; +export const uniqFilters = (filters: esFilters.Filter[], comparatorOptions: any = {}) => { + let results: esFilters.Filter[] = []; - each(filters, (filter: Filter) => { + each(filters, (filter: esFilters.Filter) => { results = union(results, dedupFilters(results, [filter]), comparatorOptions); }); diff --git a/src/plugins/data/public/query/filter_manager/test_helpers/get_filters_array.ts b/src/plugins/data/public/query/filter_manager/test_helpers/get_filters_array.ts index 27f627b477c359..aa047647c57516 100644 --- a/src/plugins/data/public/query/filter_manager/test_helpers/get_filters_array.ts +++ b/src/plugins/data/public/query/filter_manager/test_helpers/get_filters_array.ts @@ -17,9 +17,9 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; -export function getFiltersArray(): Filter[] { +export function getFiltersArray(): esFilters.Filter[] { return [ { query: { match: { extension: { query: 'jpg', type: 'phrase' } } }, diff --git a/src/plugins/data/public/query/filter_manager/test_helpers/get_stub_filter.ts b/src/plugins/data/public/query/filter_manager/test_helpers/get_stub_filter.ts index 20d9e236f49be8..adc72c961b08bb 100644 --- a/src/plugins/data/public/query/filter_manager/test_helpers/get_stub_filter.ts +++ b/src/plugins/data/public/query/filter_manager/test_helpers/get_stub_filter.ts @@ -17,15 +17,15 @@ * under the License. */ -import { Filter, FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../../../plugins/data/public'; export function getFilter( - store: FilterStateStore, + store: esFilters.FilterStateStore, disabled: boolean, negated: boolean, queryKey: string, queryValue: any -): Filter { +): esFilters.Filter { return { $state: { store, diff --git a/src/plugins/data/public/query/filter_manager/types.ts b/src/plugins/data/public/query/filter_manager/types.ts index e74b48b722cc45..0b3dbca2d6e0a6 100644 --- a/src/plugins/data/public/query/filter_manager/types.ts +++ b/src/plugins/data/public/query/filter_manager/types.ts @@ -17,9 +17,9 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../plugins/data/public'; export interface PartitionedFilters { - globalFilters: Filter[]; - appFilters: Filter[]; + globalFilters: esFilters.Filter[]; + appFilters: esFilters.Filter[]; } diff --git a/src/plugins/data/public/query/index.tsx b/src/plugins/data/public/query/index.tsx index 44b371b6adf192..42647b9d98201d 100644 --- a/src/plugins/data/public/query/index.tsx +++ b/src/plugins/data/public/query/index.tsx @@ -18,5 +18,4 @@ */ export * from './query_service'; - export * from './filter_manager'; diff --git a/src/plugins/data/server/index.ts b/src/plugins/data/server/index.ts index df933167cee255..f0b6117b928cd8 100644 --- a/src/plugins/data/server/index.ts +++ b/src/plugins/data/server/index.ts @@ -25,6 +25,11 @@ export function plugin(initializerContext: PluginInitializerContext) { } export { DataServerPlugin as Plugin }; +export { + IndexPatternsFetcher, + FieldDescriptor, + shouldReadFieldFromDocValues, +} from './index_patterns'; export * from './search'; diff --git a/src/plugins/data/server/index_patterns/fetcher/index.ts b/src/plugins/data/server/index_patterns/fetcher/index.ts new file mode 100644 index 00000000000000..19306696885dbe --- /dev/null +++ b/src/plugins/data/server/index_patterns/fetcher/index.ts @@ -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 * from './index_patterns_fetcher'; +export { shouldReadFieldFromDocValues } from './lib'; diff --git a/src/legacy/server/index_patterns/service/index_patterns_service.ts b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts similarity index 98% rename from src/legacy/server/index_patterns/service/index_patterns_service.ts rename to src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts index 62cf51828a4cc2..5f1493a49ab7de 100644 --- a/src/legacy/server/index_patterns/service/index_patterns_service.ts +++ b/src/plugins/data/server/index_patterns/fetcher/index_patterns_fetcher.ts @@ -36,7 +36,7 @@ interface FieldSubType { nested?: { path: string }; } -export class IndexPatternsService { +export class IndexPatternsFetcher { private _callDataCluster: APICaller; constructor(callDataCluster: APICaller) { diff --git a/src/legacy/server/index_patterns/service/lib/errors.ts b/src/plugins/data/server/index_patterns/fetcher/lib/errors.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/errors.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/errors.ts diff --git a/src/legacy/server/index_patterns/service/lib/es_api.test.js b/src/plugins/data/server/index_patterns/fetcher/lib/es_api.test.js similarity index 100% rename from src/legacy/server/index_patterns/service/lib/es_api.test.js rename to src/plugins/data/server/index_patterns/fetcher/lib/es_api.test.js diff --git a/src/legacy/server/index_patterns/service/lib/es_api.ts b/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts similarity index 99% rename from src/legacy/server/index_patterns/service/lib/es_api.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts index 63c1824784dbdd..92b64fafddd665 100644 --- a/src/legacy/server/index_patterns/service/lib/es_api.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts @@ -18,7 +18,6 @@ */ import { APICaller } from 'src/core/server'; -// @ts-ignore import { convertEsError } from './errors'; import { FieldCapsResponse } from './field_capabilities'; diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/__fixtures__/es_field_caps_response.json b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/__fixtures__/es_field_caps_response.json similarity index 100% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/__fixtures__/es_field_caps_response.json rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/__fixtures__/es_field_caps_response.json diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.test.js b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.test.js similarity index 100% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.test.js rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.test.js diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.ts similarity index 97% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.ts index 05e8d591f13a9e..c275fb714088e9 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_capabilities.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_capabilities.ts @@ -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(arr: T[], value: T) { return arr.includes(value) ? arr : arr.concat(value); diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.test.js b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.test.js similarity index 99% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.test.js rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.test.js index c0a5492aaba360..cf4af615b95779 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.test.js +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.test.js @@ -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'; diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts similarity index 98% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts index 19288014de297a..06eb30db0b24bb 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/field_caps_response.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts @@ -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; diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts similarity index 91% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts index 8ac42255577841..6a541aff564714 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/index.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts @@ -19,3 +19,4 @@ export { getFieldCapabilities } from './field_capabilities'; export { FieldCapsResponse } from './field_caps_response'; +export { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values'; diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/overrides.ts similarity index 95% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/overrides.ts index 6310bf02e4430d..518bfeccac01ab 100644 --- a/src/legacy/server/index_patterns/service/lib/field_capabilities/overrides.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/overrides.ts @@ -18,7 +18,7 @@ */ import { merge } from 'lodash'; -import { FieldDescriptor } from '../../index_patterns_service'; +import { FieldDescriptor } from '../../index_patterns_fetcher'; const OVERRIDES: Record> = { _source: { type: '_source' }, diff --git a/src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/should_read_field_from_doc_values.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/field_capabilities/should_read_field_from_doc_values.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/should_read_field_from_doc_values.ts diff --git a/src/legacy/server/index_patterns/service/lib/index.ts b/src/plugins/data/server/index_patterns/fetcher/lib/index.ts similarity index 90% rename from src/legacy/server/index_patterns/service/lib/index.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/index.ts index cd239b3753aa31..20e74d2b1a579d 100644 --- a/src/legacy/server/index_patterns/service/lib/index.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/index.ts @@ -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'; diff --git a/src/legacy/server/index_patterns/service/lib/resolve_time_pattern.test.js b/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.test.js similarity index 100% rename from src/legacy/server/index_patterns/service/lib/resolve_time_pattern.test.js rename to src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.test.js diff --git a/src/legacy/server/index_patterns/service/lib/resolve_time_pattern.ts b/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/resolve_time_pattern.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts diff --git a/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.ts b/src/plugins/data/server/index_patterns/fetcher/lib/time_pattern_to_wildcard.test.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.test.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/time_pattern_to_wildcard.test.ts diff --git a/src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.ts b/src/plugins/data/server/index_patterns/fetcher/lib/time_pattern_to_wildcard.ts similarity index 100% rename from src/legacy/server/index_patterns/service/lib/time_pattern_to_wildcard.ts rename to src/plugins/data/server/index_patterns/fetcher/lib/time_pattern_to_wildcard.ts diff --git a/src/plugins/data/server/index_patterns/index.ts b/src/plugins/data/server/index_patterns/index.ts new file mode 100644 index 00000000000000..6937fa22c4e5d1 --- /dev/null +++ b/src/plugins/data/server/index_patterns/index.ts @@ -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'; diff --git a/packages/kbn-es-query/src/filters/query.js b/src/plugins/data/server/index_patterns/index_patterns_service.ts similarity index 72% rename from packages/kbn-es-query/src/filters/query.js rename to src/plugins/data/server/index_patterns/index_patterns_service.ts index ded877231bf67e..30d199c0e522e4 100644 --- a/packages/kbn-es-query/src/filters/query.js +++ b/src/plugins/data/server/index_patterns/index_patterns_service.ts @@ -17,18 +17,14 @@ * under the License. */ -// Creates a filter corresponding to a raw Elasticsearch query DSL object -export function buildQueryFilter(query, index, alias) { - const filter = { - query: query, - meta: { - index, - } - }; +import { CoreSetup } from 'kibana/server'; +import { Plugin } from '../../../../core/server'; +import { registerRoutes } from './routes'; - if (alias) { - filter.meta.alias = alias; +export class IndexPatternsService implements Plugin { + public setup({ http, elasticsearch }: CoreSetup) { + registerRoutes(http, elasticsearch); } - return filter; + public start() {} } diff --git a/src/legacy/server/index_patterns/routes.ts b/src/plugins/data/server/index_patterns/routes.ts similarity index 90% rename from src/legacy/server/index_patterns/routes.ts rename to src/plugins/data/server/index_patterns/routes.ts index fb78b94e0f77f4..7975e923e219bd 100644 --- a/src/legacy/server/index_patterns/routes.ts +++ b/src/plugins/data/server/index_patterns/routes.ts @@ -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 => { - const client = await core.elasticsearch.dataClient$.pipe(first()).toPromise(); +export function registerRoutes(http: CoreSetup['http'], elasticsearch: CoreSetup['elasticsearch']) { + const getIndexPatternsService = async (request: KibanaRequest): Promise => { + const client = await elasticsearch.dataClient$.pipe(first()).toPromise(); const callCluster: APICaller = ( endpoint: string, params?: Record, 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[]) => { @@ -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', diff --git a/src/plugins/data/server/plugin.ts b/src/plugins/data/server/plugin.ts index 9cf08b0702e9e0..e81250e653ebd3 100644 --- a/src/plugins/data/server/plugin.ts +++ b/src/plugins/data/server/plugin.ts @@ -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 { 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), }; diff --git a/src/plugins/embeddable/public/lib/actions/apply_filter_action.ts b/src/plugins/embeddable/public/lib/actions/apply_filter_action.ts index 99cfb2ea13d07b..e2592b70397f33 100644 --- a/src/plugins/embeddable/public/lib/actions/apply_filter_action.ts +++ b/src/plugins/embeddable/public/lib/actions/apply_filter_action.ts @@ -18,16 +18,16 @@ */ import { i18n } from '@kbn/i18n'; -import { Filter } from '@kbn/es-query'; import { IAction, createAction, IncompatibleActionError } from '../ui_actions'; import { IEmbeddable, EmbeddableInput } from '../embeddables'; +import { esFilters } from '../../../../../plugins/data/public'; export const APPLY_FILTER_ACTION = 'APPLY_FILTER_ACTION'; -type RootEmbeddable = IEmbeddable; +type RootEmbeddable = IEmbeddable; interface ActionContext { embeddable: IEmbeddable; - filters: Filter[]; + filters: esFilters.Filter[]; } async function isCompatible(context: ActionContext) { diff --git a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/add_panel/add_panel_action.test.tsx b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/add_panel/add_panel_action.test.tsx index 802be5bf1282ec..47113ffc59561c 100644 --- a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/add_panel/add_panel_action.test.tsx +++ b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/add_panel/add_panel_action.test.tsx @@ -20,7 +20,6 @@ import { ViewMode, EmbeddableOutput, isErrorEmbeddable } from '../../../../'; import { AddPanelAction } from './add_panel_action'; import { EmbeddableFactory } from '../../../../embeddables'; -import { Filter, FilterStateStore } from '@kbn/es-query'; import { FILTERABLE_EMBEDDABLE, FilterableEmbeddable, @@ -32,6 +31,7 @@ import { GetEmbeddableFactory } from '../../../../types'; // eslint-disable-next-line import { coreMock } from '../../../../../../../../core/public/mocks'; import { ContactCardEmbeddable } from '../../../../test_samples'; +import { esFilters } from '../../../../../../../../plugins/data/public'; const embeddableFactories = new Map(); embeddableFactories.set(FILTERABLE_EMBEDDABLE, new FilterableEmbeddableFactory()); @@ -51,8 +51,8 @@ beforeEach(async () => { () => null ); - const derivedFilter: Filter = { - $state: { store: FilterStateStore.APP_STATE }, + const derivedFilter: esFilters.Filter = { + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, alias: 'name', negate: false }, query: { match: {} }, }; diff --git a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/inspect_panel_action.test.tsx b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/inspect_panel_action.test.tsx index 550f9706a634b5..8d9beec940acc0 100644 --- a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/inspect_panel_action.test.tsx +++ b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/inspect_panel_action.test.tsx @@ -28,7 +28,6 @@ import { } from '../../../test_samples'; // eslint-disable-next-line import { inspectorPluginMock } from 'src/plugins/inspector/public/mocks'; -import { FilterStateStore } from '@kbn/es-query'; import { EmbeddableFactory, EmbeddableOutput, @@ -37,6 +36,7 @@ import { } from '../../../embeddables'; import { GetEmbeddableFactory } from '../../../types'; import { of } from '../../../../tests/helpers'; +import { esFilters } from '../../../../../../../plugins/data/public'; const setup = async () => { const embeddableFactories = new Map(); @@ -48,7 +48,7 @@ const setup = async () => { panels: {}, filters: [ { - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, alias: 'name', negate: false }, query: { match: {} }, }, diff --git a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/remove_panel_action.test.tsx b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/remove_panel_action.test.tsx index 22e3be89f1ae93..684a8c45a4e890 100644 --- a/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/remove_panel_action.test.tsx +++ b/src/plugins/embeddable/public/lib/panel/panel_header/panel_actions/remove_panel_action.test.tsx @@ -17,7 +17,6 @@ * under the License. */ -import { Filter, FilterStateStore } from '@kbn/es-query'; import { EmbeddableOutput, isErrorEmbeddable } from '../../../'; import { RemovePanelAction } from './remove_panel_action'; import { EmbeddableFactory } from '../../../embeddables'; @@ -30,6 +29,7 @@ import { FilterableEmbeddableFactory } from '../../../test_samples/embeddables/f import { FilterableContainer } from '../../../test_samples/embeddables/filterable_container'; import { GetEmbeddableFactory, ViewMode } from '../../../types'; import { ContactCardEmbeddable } from '../../../test_samples/embeddables/contact_card/contact_card_embeddable'; +import { esFilters } from '../../../../../../../plugins/data/public'; const embeddableFactories = new Map(); embeddableFactories.set(FILTERABLE_EMBEDDABLE, new FilterableEmbeddableFactory()); @@ -39,8 +39,8 @@ let container: FilterableContainer; let embeddable: FilterableEmbeddable; beforeEach(async () => { - const derivedFilter: Filter = { - $state: { store: FilterStateStore.APP_STATE }, + const derivedFilter: esFilters.Filter = { + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, alias: 'name', negate: false }, query: { match: {} }, }; diff --git a/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_container.tsx b/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_container.tsx index eaef8048a6fbff..de708b778c3c7c 100644 --- a/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_container.tsx +++ b/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_container.tsx @@ -16,14 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -import { Filter } from '@kbn/es-query'; + import { Container, ContainerInput } from '../../containers'; import { GetEmbeddableFactory } from '../../types'; +import { esFilters } from '../../../../../data/public'; export const FILTERABLE_CONTAINER = 'FILTERABLE_CONTAINER'; export interface FilterableContainerInput extends ContainerInput { - filters: Filter[]; + filters: esFilters.Filter[]; } /** @@ -33,7 +34,7 @@ export interface FilterableContainerInput extends ContainerInput { */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type InheritedChildrenInput = { - filters: Filter[]; + filters: esFilters.Filter[]; id?: string; }; diff --git a/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_embeddable.tsx b/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_embeddable.tsx index f6885ca25b437a..56aa7688f37a69 100644 --- a/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_embeddable.tsx +++ b/src/plugins/embeddable/public/lib/test_samples/embeddables/filterable_embeddable.tsx @@ -17,14 +17,14 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { IContainer } from '../../containers'; import { EmbeddableOutput, EmbeddableInput, Embeddable } from '../../embeddables'; +import { esFilters } from '../../../../../data/public'; export const FILTERABLE_EMBEDDABLE = 'FILTERABLE_EMBEDDABLE'; export interface FilterableEmbeddableInput extends EmbeddableInput { - filters: Filter[]; + filters: esFilters.Filter[]; } export class FilterableEmbeddable extends Embeddable { diff --git a/src/plugins/embeddable/public/tests/apply_filter_action.test.ts b/src/plugins/embeddable/public/tests/apply_filter_action.test.ts index 52500acc3dc59d..0721acb1a1fba9 100644 --- a/src/plugins/embeddable/public/tests/apply_filter_action.test.ts +++ b/src/plugins/embeddable/public/tests/apply_filter_action.test.ts @@ -32,7 +32,7 @@ import { } from '../lib/test_samples'; // eslint-disable-next-line import { inspectorPluginMock } from 'src/plugins/inspector/public/mocks'; -import { FilterStateStore } from '@kbn/es-query'; +import { esFilters } from '../../../../plugins/data/public'; test('ApplyFilterAction applies the filter to the root of the container tree', async () => { const { doStart } = testPlugin(); @@ -76,7 +76,7 @@ test('ApplyFilterAction applies the filter to the root of the container tree', a } const filter: any = { - $state: { store: FilterStateStore.APP_STATE }, + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, negate: false, diff --git a/src/plugins/embeddable/public/tests/container.test.ts b/src/plugins/embeddable/public/tests/container.test.ts index 3bdbcbad857d65..f97c26a41b901b 100644 --- a/src/plugins/embeddable/public/tests/container.test.ts +++ b/src/plugins/embeddable/public/tests/container.test.ts @@ -26,7 +26,6 @@ import { FILTERABLE_EMBEDDABLE, } from '../lib/test_samples/embeddables/filterable_embeddable'; import { ERROR_EMBEDDABLE_TYPE } from '../lib/embeddables/error_embeddable'; -import { Filter, FilterStateStore } from '@kbn/es-query'; import { FilterableEmbeddableFactory } from '../lib/test_samples/embeddables/filterable_embeddable_factory'; import { CONTACT_CARD_EMBEDDABLE } from '../lib/test_samples/embeddables/contact_card/contact_card_embeddable_factory'; import { SlowContactCardEmbeddableFactory } from '../lib/test_samples/embeddables/contact_card/slow_contact_card_embeddable_factory'; @@ -46,6 +45,7 @@ import { import { coreMock } from '../../../../core/public/mocks'; import { testPlugin } from './test_plugin'; import { of } from './helpers'; +import { esFilters } from '../../../../plugins/data/public'; async function creatHelloWorldContainerAndEmbeddable( containerInput: ContainerInput = { id: 'hello', panels: {} }, @@ -437,8 +437,8 @@ test('Test nested reactions', async done => { test('Explicit embeddable input mapped to undefined will default to inherited', async () => { const { start } = await creatHelloWorldContainerAndEmbeddable(); - const derivedFilter: Filter = { - $state: { store: FilterStateStore.APP_STATE }, + const derivedFilter: esFilters.Filter = { + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, alias: 'name', negate: false }, query: { match: {} }, }; diff --git a/src/plugins/embeddable/public/tests/explicit_input.test.ts b/src/plugins/embeddable/public/tests/explicit_input.test.ts index 6cde7bdc48ba17..47c4b0944cef22 100644 --- a/src/plugins/embeddable/public/tests/explicit_input.test.ts +++ b/src/plugins/embeddable/public/tests/explicit_input.test.ts @@ -18,7 +18,6 @@ */ import { skip } from 'rxjs/operators'; -import { Filter, FilterStateStore } from '@kbn/es-query'; import { testPlugin } from './test_plugin'; import { FILTERABLE_EMBEDDABLE, @@ -34,6 +33,7 @@ import { isErrorEmbeddable } from '../lib'; import { HelloWorldContainer } from '../lib/test_samples/embeddables/hello_world_container'; // eslint-disable-next-line import { coreMock } from '../../../../core/public/mocks'; +import { esFilters } from '../../../../plugins/data/public'; const { setup, doStart, coreStart, uiActions } = testPlugin( coreMock.createSetup(), @@ -50,8 +50,8 @@ setup.registerEmbeddableFactory(CONTACT_CARD_EMBEDDABLE, factory); setup.registerEmbeddableFactory(HELLO_WORLD_EMBEDDABLE_TYPE, new HelloWorldEmbeddableFactory()); test('Explicit embeddable input mapped to undefined will default to inherited', async () => { - const derivedFilter: Filter = { - $state: { store: FilterStateStore.APP_STATE }, + const derivedFilter: esFilters.Filter = { + $state: { store: esFilters.FilterStateStore.APP_STATE }, meta: { disabled: false, alias: 'name', negate: false }, query: { match: {} }, }; diff --git a/src/plugins/expressions/public/expression_types/kibana_context.ts b/src/plugins/expressions/public/expression_types/kibana_context.ts index 174517abc2c053..bcf8e2853dec88 100644 --- a/src/plugins/expressions/public/expression_types/kibana_context.ts +++ b/src/plugins/expressions/public/expression_types/kibana_context.ts @@ -17,8 +17,7 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; -import { TimeRange, Query } from 'src/plugins/data/public'; +import { TimeRange, Query, esFilters } from 'src/plugins/data/public'; const name = 'kibana_context'; export type KIBANA_CONTEXT_NAME = 'kibana_context'; @@ -26,7 +25,7 @@ export type KIBANA_CONTEXT_NAME = 'kibana_context'; export interface KibanaContext { type: typeof name; query?: Query | Query[]; - filters?: Filter[]; + filters?: esFilters.Filter[]; timeRange?: TimeRange; } diff --git a/src/plugins/expressions/public/types/index.ts b/src/plugins/expressions/public/types/index.ts index 2adb0d77941599..2d66216a9770ba 100644 --- a/src/plugins/expressions/public/types/index.ts +++ b/src/plugins/expressions/public/types/index.ts @@ -17,13 +17,13 @@ * under the License. */ -import { Filter } from '@kbn/es-query'; import { ExpressionInterpret } from '../interpreter_provider'; import { TimeRange } from '../../../data/public'; import { Adapters } from '../../../inspector/public'; import { Query } from '../../../data/public'; import { ExpressionAST } from '../../../expressions/public'; import { ExpressionArgAST } from '../../../../plugins/expressions/public'; +import { esFilters } from '../../../../plugins/data/public'; export { ArgumentType } from './arguments'; export { @@ -76,7 +76,7 @@ export type Context = object; export interface SearchContext { type: 'kibana_context'; - filters?: Filter[]; + filters?: esFilters.Filter[]; query?: Query; timeRange?: TimeRange; } diff --git a/test/accessibility/apps/discover.ts b/test/accessibility/apps/discover.ts new file mode 100644 index 00000000000000..e3f73ad4bcaf86 --- /dev/null +++ b/test/accessibility/apps/discover.ts @@ -0,0 +1,46 @@ +/* + * 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 { FtrProviderContext } from '../ftr_provider_context'; + +const FROM_TIME = '2015-09-19 06:31:44.000'; +const TO_TIME = '2015-09-23 18:31:44.000'; + +export default function({ getService, getPageObjects }: FtrProviderContext) { + const PageObjects = getPageObjects(['common', 'timePicker']); + const a11y = getService('a11y'); + const esArchiver = getService('esArchiver'); + const kibanaServer = getService('kibanaServer'); + + describe('Discover', () => { + before(async () => { + await esArchiver.load('discover'); + await esArchiver.loadIfNeeded('logstash_functional'); + await kibanaServer.uiSettings.update({ + defaultIndex: 'logstash-*', + }); + await PageObjects.common.navigateToApp('discover'); + await PageObjects.timePicker.setAbsoluteRange(FROM_TIME, TO_TIME); + }); + + it('main view', async () => { + await a11y.testAppSnapshot(); + }); + }); +} diff --git a/test/accessibility/apps/home.ts b/test/accessibility/apps/home.ts new file mode 100644 index 00000000000000..4df4d5f42c93d8 --- /dev/null +++ b/test/accessibility/apps/home.ts @@ -0,0 +1,35 @@ +/* + * 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 { FtrProviderContext } from '../ftr_provider_context'; + +export default function({ getService, getPageObjects }: FtrProviderContext) { + const PageObjects = getPageObjects(['common']); + const a11y = getService('a11y'); + + describe('Kibana Home', () => { + before(async () => { + await PageObjects.common.navigateToApp('home'); + }); + + it('Kibana Home view', async () => { + await a11y.testAppSnapshot(); + }); + }); +} diff --git a/test/accessibility/apps/management.ts b/test/accessibility/apps/management.ts new file mode 100644 index 00000000000000..842f4ecbafa9ef --- /dev/null +++ b/test/accessibility/apps/management.ts @@ -0,0 +1,55 @@ +/* + * 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 { FtrProviderContext } from '../ftr_provider_context'; + +export default function({ getService, getPageObjects }: FtrProviderContext) { + const PageObjects = getPageObjects(['common', 'settings']); + const a11y = getService('a11y'); + + describe('Management', () => { + before(async () => { + await PageObjects.common.navigateToApp('settings'); + }); + + it('main view', async () => { + await a11y.testAppSnapshot(); + }); + + it('index pattern page', async () => { + await PageObjects.settings.clickKibanaIndexPatterns(); + await a11y.testAppSnapshot(); + }); + + it('Single indexpattern view', async () => { + await PageObjects.settings.clickIndexPatternLogstash(); + await a11y.testAppSnapshot(); + }); + + it('Saved objects view', async () => { + await PageObjects.settings.clickKibanaSavedObjects(); + await a11y.testAppSnapshot(); + }); + + it('Advanced settings', async () => { + await PageObjects.settings.clickKibanaSettings(); + await a11y.testAppSnapshot(); + }); + }); +} diff --git a/test/accessibility/config.ts b/test/accessibility/config.ts new file mode 100644 index 00000000000000..d73a73820a1172 --- /dev/null +++ b/test/accessibility/config.ts @@ -0,0 +1,42 @@ +/* + * 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 { FtrConfigProviderContext } from '@kbn/test/types/ftr'; +import { services } from './services'; +import { pageObjects } from './page_objects'; + +export default async function({ readConfigFile }: FtrConfigProviderContext) { + const functionalConfig = await readConfigFile(require.resolve('../functional/config')); + + return { + ...functionalConfig.getAll(), + + testFiles: [ + require.resolve('./apps/discover'), + require.resolve('./apps/management'), + require.resolve('./apps/home'), + ], + pageObjects, + services, + + junit: { + reportName: 'Accessibility Tests', + }, + }; +} diff --git a/packages/kbn-es-query/src/filters/exists.js b/test/accessibility/ftr_provider_context.d.ts similarity index 76% rename from packages/kbn-es-query/src/filters/exists.js rename to test/accessibility/ftr_provider_context.d.ts index 0c82279fb44176..22df3b16150a43 100644 --- a/packages/kbn-es-query/src/filters/exists.js +++ b/test/accessibility/ftr_provider_context.d.ts @@ -17,14 +17,9 @@ * under the License. */ -// Creates a filter where the given field exists -export function buildExistsFilter(field, indexPattern) { - return { - meta: { - index: indexPattern.id - }, - exists: { - field: field.name - } - }; -} +import { GenericFtrProviderContext } from '@kbn/test/types/ftr'; + +import { pageObjects } from './page_objects'; +import { services } from './services'; + +export type FtrProviderContext = GenericFtrProviderContext; diff --git a/test/accessibility/page_objects.ts b/test/accessibility/page_objects.ts new file mode 100644 index 00000000000000..151b6b34781b84 --- /dev/null +++ b/test/accessibility/page_objects.ts @@ -0,0 +1,20 @@ +/* + * 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 { pageObjects } from '../functional/page_objects'; diff --git a/test/accessibility/services/a11y/a11y.ts b/test/accessibility/services/a11y/a11y.ts new file mode 100644 index 00000000000000..7adfe7ebfcc7d7 --- /dev/null +++ b/test/accessibility/services/a11y/a11y.ts @@ -0,0 +1,130 @@ +/* + * 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 chalk from 'chalk'; +import testSubjectToCss from '@kbn/test-subj-selector'; + +import { FtrProviderContext } from '../../ftr_provider_context'; +import { AxeReport, printResult } from './axe_report'; +// @ts-ignore JS that is run in browser as is +import { analyzeWithAxe, analyzeWithAxeWithClient } from './analyze_with_axe'; + +interface AxeContext { + include?: string[]; + exclude?: string[][]; +} + +interface TestOptions { + excludeTestSubj?: string | string[]; +} + +export const normalizeResult = (report: any) => { + if (report.error) { + throw report.error; + } + + return report.result as false | AxeReport; +}; + +export function A11yProvider({ getService }: FtrProviderContext) { + const browser = getService('browser'); + const Wd = getService('__webdriver__'); + const log = getService('log'); + + /** + * Accessibility testing service using the Axe (https://www.deque.com/axe/) + * toolset to validate a11y rules similar to ESLint. In order to test against + * the rules we must load up the UI and feed a full HTML snapshot into Axe. + */ + return new (class Accessibility { + public async testAppSnapshot(options: TestOptions = {}) { + const context = this.getAxeContext(true, options.excludeTestSubj); + const report = await this.captureAxeReport(context); + await this.testAxeReport(report); + } + + public async testGlobalSnapshot(options: TestOptions = {}) { + const context = this.getAxeContext(false, options.excludeTestSubj); + const report = await this.captureAxeReport(context); + await this.testAxeReport(report); + } + + private getAxeContext(global: boolean, excludeTestSubj?: string | string[]): AxeContext { + return { + include: global ? undefined : [testSubjectToCss('appA11yRoot')], + exclude: ([] as string[]) + .concat(excludeTestSubj || []) + .map(ts => [testSubjectToCss(ts)]) + .concat([['.ace_scrollbar']]), + }; + } + + private testAxeReport(report: AxeReport) { + const errorMsgs = []; + + for (const result of report.incomplete) { + // these items require human review and can't be definitively validated + log.warning(printResult(chalk.yellow('UNABLE TO VALIDATE'), result)); + } + + for (const result of report.violations) { + errorMsgs.push(printResult(chalk.red('VIOLATION'), result)); + } + + if (errorMsgs.length) { + throw new Error(`a11y report:\n${errorMsgs.join('\n')}`); + } + } + + private async captureAxeReport(context: AxeContext): Promise { + const axeOptions = { + reporter: 'v2', + runOnly: ['wcag2a', 'wcag2aa'], + rules: { + 'color-contrast': { + enabled: false, + }, + }, + }; + + await (Wd.driver.manage() as any).setTimeouts({ + ...(await (Wd.driver.manage() as any).getTimeouts()), + script: 600000, + }); + + const report = normalizeResult( + await browser.executeAsync(analyzeWithAxe, context, axeOptions) + ); + + if (report !== false) { + return report; + } + + const withClientReport = normalizeResult( + await browser.executeAsync(analyzeWithAxeWithClient, context, axeOptions) + ); + + if (withClientReport === false) { + throw new Error('Attempted to analyze with axe but failed to load axe client'); + } + + return withClientReport; + } + })(); +} diff --git a/test/accessibility/services/a11y/analyze_with_axe.js b/test/accessibility/services/a11y/analyze_with_axe.js new file mode 100644 index 00000000000000..5cba55a29f8a4d --- /dev/null +++ b/test/accessibility/services/a11y/analyze_with_axe.js @@ -0,0 +1,39 @@ +/* + * 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 { readFileSync } from 'fs'; + +export function analyzeWithAxe(context, options, callback) { + Promise.resolve() + .then(() => { + if (window.axe) { + return window.axe.run(context, options); + } + + // return a false report to trigger analyzeWithAxeWithClient + return false; + }) + .then(result => callback({ result }), error => callback({ error })); +} + +export const analyzeWithAxeWithClient = ` + ${readFileSync(require.resolve('axe-core/axe.js'), 'utf8')} + + return (${analyzeWithAxe.toString()}).apply(null, arguments); +`; diff --git a/test/accessibility/services/a11y/axe_report.ts b/test/accessibility/services/a11y/axe_report.ts new file mode 100644 index 00000000000000..ba1e8c739079da --- /dev/null +++ b/test/accessibility/services/a11y/axe_report.ts @@ -0,0 +1,69 @@ +/* + * 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. + */ + +type AxeImpact = 'minor' | 'moderate' | 'serious' | 'critical'; + +type AxeRelatedNodes = Array<{ + data: any; + id: string; + impact: AxeImpact; + message: string; + relatedNodes: []; +}>; + +export interface AxeResult { + /* Rule description */ + description: string; + /* rule title/error message */ + help: string; + /* documentation url */ + helpUrl: string; + /* rule id */ + id: string; + /* severity level */ + impact?: AxeImpact; + /* tags used to group rules */ + tags: string[]; + /* nodes grouped in this result */ + nodes: Array<{ + all: AxeRelatedNodes; + any: AxeRelatedNodes; + none: AxeRelatedNodes; + + html: string; + impact: AxeImpact; + target: string[]; + }>; +} + +export type AxeResultGroup = AxeResult[]; + +export interface AxeReport { + inapplicable: AxeResultGroup; + passes: AxeResultGroup; + incomplete: AxeResultGroup; + violations: AxeResultGroup; +} + +export const printResult = (title: string, result: AxeResult) => ` +${title} + [${result.id}]: ${result.description} + Help: ${result.helpUrl} + Elements: + - ${result.nodes.map(node => node.target).join('\n - ')}`; diff --git a/src/legacy/server/index_patterns/service/index.ts b/test/accessibility/services/a11y/index.ts similarity index 95% rename from src/legacy/server/index_patterns/service/index.ts rename to test/accessibility/services/a11y/index.ts index 496c4ba7b5b6f1..7baa17c1eafa33 100644 --- a/src/legacy/server/index_patterns/service/index.ts +++ b/test/accessibility/services/a11y/index.ts @@ -17,4 +17,4 @@ * under the License. */ -export * from './index_patterns_service'; +export { A11yProvider } from './a11y'; diff --git a/test/accessibility/services/index.ts b/test/accessibility/services/index.ts new file mode 100644 index 00000000000000..98d1628732f5c8 --- /dev/null +++ b/test/accessibility/services/index.ts @@ -0,0 +1,26 @@ +/* + * 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 { services as kibanaFunctionalServices } from '../../functional/services'; +import { A11yProvider } from './a11y'; + +export const services = { + ...kibanaFunctionalServices, + a11y: A11yProvider, +}; diff --git a/test/api_integration/apis/core/index.js b/test/api_integration/apis/core/index.js index e5da4e4730662f..d617b2ad073511 100644 --- a/test/api_integration/apis/core/index.js +++ b/test/api_integration/apis/core/index.js @@ -16,45 +16,21 @@ * specific language governing permissions and limitations * under the License. */ -import expect from '@kbn/expect'; export default function ({ getService }) { const supertest = getService('supertest'); - describe('core', () => { - describe('request context', () => { - it('provides access to elasticsearch', async () => ( - await supertest - .get('/requestcontext/elasticsearch') - .expect(200, 'Elasticsearch: true') - )); + describe('core request context', () => { + it('provides access to elasticsearch', async () => ( + await supertest + .get('/requestcontext/elasticsearch') + .expect(200, 'Elasticsearch: true') + )); - it('provides access to SavedObjects client', async () => ( - await supertest - .get('/requestcontext/savedobjectsclient') - .expect(200, 'SavedObjects client: {"page":1,"per_page":20,"total":0,"saved_objects":[]}') - )); - }); - - describe('compression', () => { - it(`uses compression when there isn't a referer`, async () => { - await supertest - .get('/app/kibana') - .set('accept-encoding', 'gzip') - .then(response => { - expect(response.headers).to.have.property('content-encoding', 'gzip'); - }); - }); - - it(`doesn't use compression when there is a referer`, async () => { - await supertest - .get('/app/kibana') - .set('accept-encoding', 'gzip') - .set('referer', 'https://www.google.com') - .then(response => { - expect(response.headers).not.to.have.property('content-encoding'); - }); - }); - }); + it('provides access to SavedObjects client', async () => ( + await supertest + .get('/requestcontext/savedobjectsclient') + .expect(200, 'SavedObjects client: {"page":1,"per_page":20,"total":0,"saved_objects":[]}') + )); }); } diff --git a/test/api_integration/apis/index.js b/test/api_integration/apis/index.js index de36ee678b10ed..9f2672959390ce 100644 --- a/test/api_integration/apis/index.js +++ b/test/api_integration/apis/index.js @@ -34,6 +34,5 @@ export default function ({ loadTestFile }) { loadTestFile(require.resolve('./status')); loadTestFile(require.resolve('./stats')); loadTestFile(require.resolve('./ui_metric')); - loadTestFile(require.resolve('./core')); }); } diff --git a/test/api_integration/apis/index_patterns/es_errors/errors.js b/test/api_integration/apis/index_patterns/es_errors/errors.js index 7e04e3f7204d71..4e50b965211c29 100644 --- a/test/api_integration/apis/index_patterns/es_errors/errors.js +++ b/test/api_integration/apis/index_patterns/es_errors/errors.js @@ -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, diff --git a/test/functional/services/browser.ts b/test/functional/services/browser.ts index 4060e3dd7800f2..97e02958f3787f 100644 --- a/test/functional/services/browser.ts +++ b/test/functional/services/browser.ts @@ -461,10 +461,7 @@ export async function BrowserProvider({ getService }: FtrProviderContext) { ); } - public async executeAsync( - fn: string | ((...args: A) => R), - ...args: A - ): Promise { + public async executeAsync(fn: string | ((...args: any[]) => R), ...args: any[]): Promise { return await driver.executeAsyncScript( fn, ...cloneDeep(args, arg => { diff --git a/test/scripts/jenkins_accessibility.sh b/test/scripts/jenkins_accessibility.sh new file mode 100755 index 00000000000000..0b3d8dc3f85c23 --- /dev/null +++ b/test/scripts/jenkins_accessibility.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +set -e + +if [[ -n "$IS_PIPELINE_JOB" ]] ; then + source src/dev/ci_setup/setup_env.sh +fi + +export TEST_BROWSER_HEADLESS=1 + +if [[ -z "$IS_PIPELINE_JOB" ]] ; then + yarn run grunt functionalTests:ensureAllTestsInCiGroup; + node scripts/build --debug --oss; +else + installDir="$(realpath $PARENT_DIR/kibana/build/oss/kibana-*-SNAPSHOT-linux-x86_64)" + destDir=${installDir}-${CI_WORKER_NUMBER} + cp -R "$installDir" "$destDir" + + export KIBANA_INSTALL_DIR="$destDir" +fi + +checks-reporter-with-killswitch "Kibana accessibility tests" \ + node scripts/functional_tests \ + --debug --bail \ + --kibana-install-dir "$installDir" \ + --config test/accessibility/config.ts; diff --git a/test/scripts/jenkins_xpack_accessibility.sh b/test/scripts/jenkins_xpack_accessibility.sh new file mode 100755 index 00000000000000..af813c3c40f841 --- /dev/null +++ b/test/scripts/jenkins_xpack_accessibility.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -e + +if [[ -n "$IS_PIPELINE_JOB" ]] ; then + source src/dev/ci_setup/setup_env.sh +fi + +if [[ -z "$IS_PIPELINE_JOB" ]] ; then + echo " -> building and extracting default Kibana distributable for use in functional tests" + node scripts/build --debug --no-oss + + linuxBuild="$(find "$KIBANA_DIR/target" -name 'kibana-*-linux-x86_64.tar.gz')" + installDir="$PARENT_DIR/install/kibana" + + mkdir -p "$installDir" + tar -xzf "$linuxBuild" -C "$installDir" --strip=1 + + export KIBANA_INSTALL_DIR="$installDir" +else + installDir="$PARENT_DIR/install/kibana" + destDir="${installDir}-${CI_WORKER_NUMBER}" + cp -R "$installDir" "$destDir" + + export KIBANA_INSTALL_DIR="$destDir" +fi + +export TEST_BROWSER_HEADLESS=1 +cd "$XPACK_DIR" + +checks-reporter-with-killswitch "X-Pack accessibility tests" \ + node scripts/functional_tests \ + --debug --bail \ + --kibana-install-dir "$installDir" \ + --config test/accessibility/config.ts; diff --git a/x-pack/.i18nrc.json b/x-pack/.i18nrc.json index 0ee50c0caa3402..6d0da2f0b693d3 100644 --- a/x-pack/.i18nrc.json +++ b/x-pack/.i18nrc.json @@ -34,6 +34,7 @@ "xpack.server": "legacy/server", "xpack.snapshotRestore": "legacy/plugins/snapshot_restore", "xpack.spaces": ["legacy/plugins/spaces", "plugins/spaces"], + "xpack.taskManager": "legacy/plugins/task_manager", "xpack.transform": "legacy/plugins/transform", "xpack.upgradeAssistant": "legacy/plugins/upgrade_assistant", "xpack.uptime": "legacy/plugins/uptime", diff --git a/x-pack/legacy/plugins/actions/server/builtin_action_types/lib/result_type.ts b/x-pack/legacy/plugins/actions/server/builtin_action_types/lib/result_type.ts index c891f3bf218e7d..256463251315d8 100644 --- a/x-pack/legacy/plugins/actions/server/builtin_action_types/lib/result_type.ts +++ b/x-pack/legacy/plugins/actions/server/builtin_action_types/lib/result_type.ts @@ -8,16 +8,15 @@ // Which is basically the Haskel equivalent of Rust/ML/Scala's Result // I'll reach out to other's in Kibana to see if we can merge these into one type -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type Ok = { +export interface Ok { tag: 'ok'; value: T; -}; -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export type Err = { +} + +export interface Err { tag: 'err'; error: E; -}; +} export type Result = Ok | Err; export function asOk(value: T): Ok { diff --git a/x-pack/legacy/plugins/apm/dev_docs/github_commands.md b/x-pack/legacy/plugins/apm/dev_docs/github_commands.md new file mode 100644 index 00000000000000..f2c32bafa7539d --- /dev/null +++ b/x-pack/legacy/plugins/apm/dev_docs/github_commands.md @@ -0,0 +1,6 @@ +### Useful Github Pull Request commands + +The following commands can be executed by writing them as comments on a pull request: + +- `@elasticmachine merge upstream`: Will merge the upstream (eg. master or 7.x) into the branch. This is useful if a bug has been fixed upstream and the fix is necessary to pass CI checks +- `retest` Re-run the tests. This is useful if a flaky test caused the build to fail diff --git a/x-pack/legacy/plugins/apm/dev_docs/vscode_setup.md b/x-pack/legacy/plugins/apm/dev_docs/vscode_setup.md new file mode 100644 index 00000000000000..e1901b3855f735 --- /dev/null +++ b/x-pack/legacy/plugins/apm/dev_docs/vscode_setup.md @@ -0,0 +1,53 @@ +### Visual Studio Code + +When using [Visual Studio Code](https://code.visualstudio.com/) with APM it's best to set up a [multi-root workspace](https://code.visualstudio.com/docs/editor/multi-root-workspaces) and add the `x-pack/legacy/plugins/apm` directory, the `x-pack` directory, and the root of the Kibana repository to the workspace. This makes it so you can navigate and search within APM and use the wider workspace roots when you need to widen your search. + +#### Using the Jest extension + +The [vscode-jest extension](https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest) is a good way to run your Jest tests inside the editor. + +Some of the benefits of using the extension over just running it in a terminal are: + +• It shows the pass/fail of a test inline in the test file +• It shows the error message in the test file if it fails +• You don’t have to have the terminal process running +• It can automatically update your snapshots when they change +• Coverage mapping + +The extension doesn't really work well if you're trying to use it on all of Kibana or all of X-Pack, but it works well if you configure it to run only on the files in APM. + +If you have a workspace configured as described above you should have: + +```json +"jest.disabledWorkspaceFolders": ["kibana", "x-pack"] +``` + +in your Workspace settings, and: + +```json +"jest.pathToJest": "node scripts/jest.js --testPathPattern=legacy/plugins/apm", +"jest.rootPath": "../../.." +``` + +in the settings for the APM folder. + +#### Jest debugging + +To make the [VSCode debugger](https://vscode.readthedocs.io/en/latest/editor/debugging/) work with Jest (you can set breakpoints in the code and tests and use the VSCode debugger) you'll need the [Node Debug extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.node-debug2) installed and can set up a launch configuration like: + +```json +{ + "type": "node", + "name": "APM Jest", + "request": "launch", + "args": ["--runInBand", "--testPathPattern=legacy/plugins/apm"], + "cwd": "${workspaceFolder}/../../..", + "console": "internalConsole", + "internalConsoleOptions": "openOnSessionStart", + "disableOptimisticBPs": true, + "program": "${workspaceFolder}/../../../scripts/jest.js", + "runtimeVersion": "10.15.2" +} +``` + +(you'll want `runtimeVersion` to match what's in the Kibana root .nvmrc. Depending on your setup, you might be able to remove this line.) diff --git a/x-pack/legacy/plugins/apm/readme.md b/x-pack/legacy/plugins/apm/readme.md index 17a72f07470f26..a46b0c2895fcae 100644 --- a/x-pack/legacy/plugins/apm/readme.md +++ b/x-pack/legacy/plugins/apm/readme.md @@ -29,6 +29,13 @@ cd apm-integration-testing/ _Docker Compose is required_ +### Debugging Elasticsearch queries + +All APM api endpoints accept `_debug=true` as a query param that will result in the underlying ES query being outputted in the Kibana backend process. + +Example: +`/api/apm/services/my_service?_debug=true` + ### Unit testing Note: Run the following commands from `kibana/x-pack`. @@ -45,10 +52,6 @@ node scripts/jest.js plugins/apm --watch node scripts/jest.js plugins/apm --updateSnapshot ``` -### Cypress E2E tests - -See the Cypress-specific [readme.md](cypress/README.md) - ### Linting _Note: Run the following commands from `kibana/`._ @@ -65,63 +68,8 @@ yarn prettier "./x-pack/legacy/plugins/apm/**/*.{tsx,ts,js}" --write yarn eslint ./x-pack/legacy/plugins/apm --fix ``` -### Useful Github Pull Request commands - -The following commands can be executed by writing them as comments on a pull request: - - - `@elasticmachine merge upstream`: Will merge the upstream (eg. master or 7.x) into the branch. This is useful if a bug has been fixed upstream and the fix is necessary to pass CI checks - - `retest` Re-run the tests. This is useful if a flaky test caused the build to fail - -### Visual Studio Code - -When using [Visual Studio Code](https://code.visualstudio.com/) with APM it's best to set up a [multi-root workspace](https://code.visualstudio.com/docs/editor/multi-root-workspaces) and add the `x-pack/legacy/plugins/apm` directory, the `x-pack` directory, and the root of the Kibana repository to the workspace. This makes it so you can navigate and search within APM and use the wider workspace roots when you need to widen your search. - -#### Using the Jest extension - -The [vscode-jest extension](https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest) is a good way to run your Jest tests inside the editor. - -Some of the benefits of using the extension over just running it in a terminal are: - -• It shows the pass/fail of a test inline in the test file -• It shows the error message in the test file if it fails -• You don’t have to have the terminal process running -• It can automatically update your snapshots when they change -• Coverage mapping - -The extension doesn't really work well if you're trying to use it on all of Kibana or all of X-Pack, but it works well if you configure it to run only on the files in APM. - -If you have a workspace configured as described above you should have: - -```json -"jest.disabledWorkspaceFolders": ["kibana", "x-pack"] -``` - -in your Workspace settings, and: - -```json -"jest.pathToJest": "node scripts/jest.js --testPathPattern=legacy/plugins/apm", -"jest.rootPath": "../../.." -``` - -in the settings for the APM folder. - -#### Jest debugging - -To make the [VSCode debugger](https://vscode.readthedocs.io/en/latest/editor/debugging/) work with Jest (you can set breakpoints in the code and tests and use the VSCode debugger) you'll need the [Node Debug extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.node-debug2) installed and can set up a launch configuration like: - -```json -{ - "type": "node", - "name": "APM Jest", - "request": "launch", - "args": ["--runInBand", "--testPathPattern=legacy/plugins/apm"], - "cwd": "${workspaceFolder}/../../..", - "console": "internalConsole", - "internalConsoleOptions": "openOnSessionStart", - "disableOptimisticBPs": true, - "program": "${workspaceFolder}/../../../scripts/jest.js", - "runtimeVersion": "10.15.2" -} -``` +#### Further resources -(you'll want `runtimeVersion` to match what's in the Kibana root .nvmrc. Depending on your setup, you might be able to remove this line.) +- [Cypress integration tests](cypress/README.md) +- [VSCode setup instructions](./dev_docs/vscode_setup.md) +- [Github PR commands](./dev_docs/github_commands.md) diff --git a/x-pack/legacy/plugins/apm/server/lib/index_pattern/getKueryBarIndexPattern.ts b/x-pack/legacy/plugins/apm/server/lib/index_pattern/getKueryBarIndexPattern.ts index 33d404009ae204..a5ba4573cfd58a 100644 --- a/x-pack/legacy/plugins/apm/server/lib/index_pattern/getKueryBarIndexPattern.ts +++ b/x-pack/legacy/plugins/apm/server/lib/index_pattern/getKueryBarIndexPattern.ts @@ -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 ({ @@ -21,7 +21,7 @@ export const getKueryBarIndexPattern = async ({ }) => { const { indices } = setup; - const indexPatternsService = new IndexPatternsService( + const indexPatternsFetcher = new IndexPatternsFetcher( (...rest: Parameters) => request.server.plugins.elasticsearch .getCluster('data') @@ -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 }); diff --git a/x-pack/legacy/plugins/beats_management/public/lib/adapters/elasticsearch/rest.ts b/x-pack/legacy/plugins/beats_management/public/lib/adapters/elasticsearch/rest.ts index d4a42ba662db41..526728bd77cac6 100644 --- a/x-pack/legacy/plugins/beats_management/public/lib/adapters/elasticsearch/rest.ts +++ b/x-pack/legacy/plugins/beats_management/public/lib/adapters/elasticsearch/rest.ts @@ -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 { @@ -65,9 +65,9 @@ export class RestElasticsearchAdapter implements ElasticsearchAdapter { if (this.cachedIndexPattern) { return this.cachedIndexPattern; } - const res = await this.api.get( - `/api/index_patterns/_fields_for_wildcard?pattern=${this.indexPatternName}` - ); + const res = await data.indexPatterns.indexPatterns.getFieldsForWildcard({ + pattern: this.indexPatternName, + }); if (isEmpty(res.fields)) { return; } diff --git a/x-pack/legacy/plugins/beats_management/public/lib/compose/kibana.ts b/x-pack/legacy/plugins/beats_management/public/lib/compose/kibana.ts index 3d8ca51e006bf7..2ebda89ba13fd6 100644 --- a/x-pack/legacy/plugins/beats_management/public/lib/compose/kibana.ts +++ b/x-pack/legacy/plugins/beats_management/public/lib/compose/kibana.ts @@ -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), diff --git a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts index 958d9c6a3a6f08..abaa16c4e32710 100644 --- a/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts +++ b/x-pack/legacy/plugins/canvas/canvas_plugin_src/functions/common/saved_map.ts @@ -3,7 +3,6 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Filter as ESFilterType } from '@kbn/es-query'; import { ExpressionFunction } from 'src/legacy/core_plugins/interpreter/public'; import { TimeRange } from 'src/plugins/data/public'; import { EmbeddableInput } from 'src/legacy/core_plugins/embeddable_api/public/np_ready/public'; @@ -15,6 +14,7 @@ import { EmbeddableExpression, } from '../../expression_types'; import { getFunctionHelp } from '../../../i18n'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; interface Arguments { id: string; @@ -29,7 +29,7 @@ interface SavedMapInput extends EmbeddableInput { isPaused: boolean; interval: number; }; - filters: ESFilterType[]; + filters: esFilters.Filter[]; } type Return = EmbeddableExpression; diff --git a/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts b/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts index 8de813255a2301..ca34246531bff5 100644 --- a/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts +++ b/x-pack/legacy/plugins/canvas/server/lib/build_embeddable_filters.ts @@ -4,14 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import { buildQueryFilter, Filter as ESFilterType } from '@kbn/es-query'; import { TimeRange } from 'src/plugins/data/public'; import { Filter } from '../../types'; // @ts-ignore Untyped Local import { buildBoolArray } from './build_bool_array'; +import { esFilters } from '../../../../../../src/plugins/data/common'; export interface EmbeddableFilterInput { - filters: ESFilterType[]; + filters: esFilters.Filter[]; timeRange?: TimeRange; } @@ -30,8 +30,10 @@ function getTimeRangeFromFilters(filters: Filter[]): TimeRange | undefined { : undefined; } -function getQueryFilters(filters: Filter[]): ESFilterType[] { - return buildBoolArray(filters.filter(filter => filter.type !== 'time')).map(buildQueryFilter); +function getQueryFilters(filters: Filter[]): esFilters.Filter[] { + return buildBoolArray(filters.filter(filter => filter.type !== 'time')).map( + esFilters.buildQueryFilter + ); } export function buildEmbeddableFilters(filters: Filter[]): EmbeddableFilterInput { diff --git a/x-pack/legacy/plugins/graph/public/app.js b/x-pack/legacy/plugins/graph/public/app.js index 97ce84c123ec75..b60f6b267ad842 100644 --- a/x-pack/legacy/plugins/graph/public/app.js +++ b/x-pack/legacy/plugins/graph/public/app.js @@ -11,9 +11,7 @@ import React from 'react'; import { Provider } from 'react-redux'; import { isColorDark, hexToRgb } from '@elastic/eui'; -import { KibanaParsedUrl } from 'ui/url/kibana_parsed_url'; import { showSaveModal } from 'ui/saved_objects/show_saved_object_save_modal'; -import { formatAngularHttpError } from 'ui/notify/lib'; import { addAppRedirectMessageToUrl } from 'ui/notify'; import appTemplate from './angular/templates/index.html'; @@ -39,6 +37,7 @@ import { datasourceSelector, hasFieldsSelector } from './state_management'; +import { formatHttpError } from './helpers/format_http_error'; export function initGraphApp(angularModule, deps) { const { @@ -56,7 +55,6 @@ export function initGraphApp(angularModule, deps) { savedObjectRegistry, capabilities, coreStart, - $http, Storage, canEditDrillDownUrls, graphSavePolicy, @@ -208,31 +206,35 @@ export function initGraphApp(angularModule, deps) { async function handleHttpError(error) { checkLicense(kbnBaseUrl); - toastNotifications.addDanger(formatAngularHttpError(error)); + toastNotifications.addDanger(formatHttpError(error)); } // Replacement function for graphClientWorkspace's comms so // that it works with Kibana. function callNodeProxy(indexName, query, responseHandler) { const request = { - index: indexName, - query: query + body: JSON.stringify({ + index: indexName, + query: query + }) }; $scope.loading = true; - return $http.post('../api/graph/graphExplore', request) - .then(function (resp) { - if (resp.data.resp.timed_out) { + return coreStart.http.post('../api/graph/graphExplore', request) + .then(function (data) { + const response = data.resp; + if (response.timed_out) { toastNotifications.addWarning( i18n.translate('xpack.graph.exploreGraph.timedOutWarningText', { defaultMessage: 'Exploration timed out', }) ); } - responseHandler(resp.data.resp); + responseHandler(response); }) .catch(handleHttpError) .finally(() => { $scope.loading = false; + $scope.$digest(); }); } @@ -240,17 +242,21 @@ export function initGraphApp(angularModule, deps) { //Helper function for the graphClientWorkspace to perform a query const callSearchNodeProxy = function (indexName, query, responseHandler) { const request = { - index: indexName, - body: query + body: JSON.stringify({ + index: indexName, + body: query + }) }; $scope.loading = true; - $http.post('../api/graph/searchProxy', request) - .then(function (resp) { - responseHandler(resp.data.resp); + coreStart.http.post('../api/graph/searchProxy', request) + .then(function (data) { + const response = data.resp; + responseHandler(response); }) .catch(handleHttpError) .finally(() => { $scope.loading = false; + $scope.$digest(); }); }; diff --git a/x-pack/legacy/plugins/graph/public/helpers/format_http_error.ts b/x-pack/legacy/plugins/graph/public/helpers/format_http_error.ts new file mode 100644 index 00000000000000..5e0ac1e1ce56f0 --- /dev/null +++ b/x-pack/legacy/plugins/graph/public/helpers/format_http_error.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { i18n } from '@kbn/i18n'; +import { IHttpFetchError } from 'kibana/public'; + +export function formatHttpError(error: IHttpFetchError) { + if (!error.response) { + return i18n.translate('xpack.graph.fatalError.unavailableServerErrorMessage', { + defaultMessage: + 'An HTTP request has failed to connect. ' + + 'Please check if the Kibana server is running and that your browser has a working connection, ' + + 'or contact your system administrator.', + }); + } + return i18n.translate('xpack.graph.fatalError.errorStatusMessage', { + defaultMessage: 'Error {errStatus} {errStatusText}: {errMessage}', + values: { + errStatus: error.body.status, + errStatusText: error.body.statusText, + errMessage: error.body.message, + }, + }); +} diff --git a/x-pack/legacy/plugins/graph/public/index.ts b/x-pack/legacy/plugins/graph/public/index.ts index 5e500367ccdc52..48420d403653f1 100644 --- a/x-pack/legacy/plugins/graph/public/index.ts +++ b/x-pack/legacy/plugins/graph/public/index.ts @@ -36,7 +36,6 @@ async function getAngularInjectedDependencies(): Promise('Private'); return { - $http: injector.get('$http'), savedObjectRegistry: Private(SavedObjectRegistryProvider), kbnBaseUrl: injector.get('kbnBaseUrl'), savedGraphWorkspaces: Private(SavedWorkspacesProvider), diff --git a/x-pack/legacy/plugins/graph/public/render_app.ts b/x-pack/legacy/plugins/graph/public/render_app.ts index 8625e20ab9c52d..a8a86f4d1f850e 100644 --- a/x-pack/legacy/plugins/graph/public/render_app.ts +++ b/x-pack/legacy/plugins/graph/public/render_app.ts @@ -63,10 +63,6 @@ export interface GraphDependencies extends LegacyAngularInjectedDependencies { * These dependencies have to be migrated to their NP counterparts. */ export interface LegacyAngularInjectedDependencies { - /** - * angular $http service - */ - $http: any; /** * Instance of SavedObjectRegistryProvider */ diff --git a/x-pack/legacy/plugins/infra/public/components/inventory/toolbars/toolbar_wrapper.tsx b/x-pack/legacy/plugins/infra/public/components/inventory/toolbars/toolbar_wrapper.tsx index 3c2c3d3bd05d06..7cb86f6e4d0eca 100644 --- a/x-pack/legacy/plugins/infra/public/components/inventory/toolbars/toolbar_wrapper.tsx +++ b/x-pack/legacy/plugins/infra/public/components/inventory/toolbars/toolbar_wrapper.tsx @@ -108,7 +108,7 @@ export const toMetricOpt = (metric: InfraSnapshotMetricType) => { case InfraSnapshotMetricType.tx: return { text: ToolbarTranslations.OutboundTraffic, - value: InfraSnapshotMetricType.rx, + value: InfraSnapshotMetricType.tx, }; case InfraSnapshotMetricType.logRate: return { diff --git a/x-pack/legacy/plugins/infra/public/containers/waffle/with_waffle_view_state.tsx b/x-pack/legacy/plugins/infra/public/containers/waffle/with_waffle_view_state.tsx index cd9ce35a32b687..86beb28a7f3ca1 100644 --- a/x-pack/legacy/plugins/infra/public/containers/waffle/with_waffle_view_state.tsx +++ b/x-pack/legacy/plugins/infra/public/containers/waffle/with_waffle_view_state.tsx @@ -106,6 +106,13 @@ export const withWaffleViewState = connect( ), }) ); + } else { + dispatch( + waffleFilterActions.applyWaffleFilterQuery({ + query: null, + serializedQuery: null, + }) + ); } }, }; diff --git a/x-pack/legacy/plugins/infra/public/store/local/waffle_filter/reducer.ts b/x-pack/legacy/plugins/infra/public/store/local/waffle_filter/reducer.ts index a34cc9659f0b7e..912ad96357334d 100644 --- a/x-pack/legacy/plugins/infra/public/store/local/waffle_filter/reducer.ts +++ b/x-pack/legacy/plugins/infra/public/store/local/waffle_filter/reducer.ts @@ -16,8 +16,8 @@ export interface KueryFilterQuery { export type FilterQuery = KueryFilterQuery; export interface SerializedFilterQuery { - query: FilterQuery; - serializedQuery: string; + query: FilterQuery | null; + serializedQuery: string | null; } export interface WaffleFilterState { diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx index a1710d67b31db4..31d622c7089a86 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx @@ -7,12 +7,12 @@ import React from 'react'; import { ReactWrapper } from 'enzyme'; import { act } from 'react-dom/test-utils'; -import { buildExistsFilter } from '@kbn/es-query'; import { App } from './app'; import { EditorFrameInstance } from '../types'; import { Storage } from '../../../../../../src/plugins/kibana_utils/public'; import { Document, SavedObjectStore } from '../persistence'; import { mount } from 'enzyme'; +import { esFilters } from '../../../../../../src/plugins/data/public'; import { dataPluginMock } from '../../../../../../src/plugins/data/public/mocks'; const dataStartMock = dataPluginMock.createStartContract(); @@ -595,7 +595,7 @@ describe('Lens App', () => { const instance = mount(); args.data.query.filterManager.setFilters([ - buildExistsFilter({ name: 'myfield' }, { id: 'index1' }), + esFilters.buildExistsFilter({ name: 'myfield' }, { id: 'index1' }), ]); instance.update(); @@ -603,7 +603,7 @@ describe('Lens App', () => { expect(frame.mount).toHaveBeenCalledWith( expect.any(Element), expect.objectContaining({ - filters: [buildExistsFilter({ name: 'myfield' }, { id: 'index1' })], + filters: [esFilters.buildExistsFilter({ name: 'myfield' }, { id: 'index1' })], }) ); }); @@ -726,7 +726,7 @@ describe('Lens App', () => { }); args.data.query.filterManager.setFilters([ - buildExistsFilter({ name: 'myfield' }, { id: 'index1' }), + esFilters.buildExistsFilter({ name: 'myfield' }, { id: 'index1' }), ]); instance.update(); diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx index a95e0450f614c8..2815ac9ddda4e5 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx @@ -18,7 +18,6 @@ import { SavedQuery, Query, } from 'src/legacy/core_plugins/data/public'; -import { Filter } from '@kbn/es-query'; import { IStorageWrapper } from 'src/plugins/kibana_utils/public'; import { start as navigation } from '../../../../../../src/legacy/core_plugins/navigation/public/legacy'; import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public'; @@ -26,6 +25,7 @@ import { Document, SavedObjectStore } from '../persistence'; import { EditorFrameInstance } from '../types'; import { NativeRenderer } from '../native_renderer'; import { trackUiEvent } from '../lens_ui_telemetry'; +import { esFilters } from '../../../../../../src/plugins/data/public'; interface State { isLoading: boolean; @@ -40,7 +40,7 @@ interface State { toDate: string; }; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; savedQuery?: SavedQuery; } diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/data_panel_wrapper.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/data_panel_wrapper.tsx index 115e8cbf002c3b..a5509cdc945595 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/data_panel_wrapper.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/data_panel_wrapper.tsx @@ -6,7 +6,6 @@ import React, { useMemo, memo, useContext, useState } from 'react'; import { i18n } from '@kbn/i18n'; -import { Filter } from '@kbn/es-query'; import { EuiPopover, EuiButtonIcon, EuiContextMenuPanel, EuiContextMenuItem } from '@elastic/eui'; import { Query } from 'src/plugins/data/common'; import { DatasourceDataPanelProps, Datasource } from '../../../public'; @@ -14,6 +13,7 @@ import { NativeRenderer } from '../../native_renderer'; import { Action } from './state_management'; import { DragContext } from '../../drag_drop'; import { StateSetter, FramePublicAPI } from '../../types'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; interface DataPanelWrapperProps { datasourceState: unknown; @@ -24,7 +24,7 @@ interface DataPanelWrapperProps { core: DatasourceDataPanelProps['core']; query: Query; dateRange: FramePublicAPI['dateRange']; - filters: Filter[]; + filters: esFilters.Filter[]; } export const DataPanelWrapper = memo((props: DataPanelWrapperProps) => { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx index 8e89d8edc9f230..6d782d119525dc 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx @@ -6,7 +6,6 @@ import React, { useEffect, useReducer } from 'react'; import { CoreSetup, CoreStart } from 'src/core/public'; -import { Filter } from '@kbn/es-query'; import { Query, SavedQuery } from '../../../../../../../src/legacy/core_plugins/data/public'; import { ExpressionRenderer } from '../../../../../../../src/legacy/core_plugins/expressions/public'; import { @@ -26,6 +25,7 @@ import { Document } from '../../persistence/saved_object_store'; import { getSavedObjectFormat } from './save'; import { WorkspacePanelWrapper } from './workspace_panel_wrapper'; import { generateId } from '../../id_generator'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; export interface EditorFrameProps { doc?: Document; @@ -41,7 +41,7 @@ export interface EditorFrameProps { toDate: string; }; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; savedQuery?: SavedQuery; onChange: (arg: { filterableIndexPatterns: DatasourceMetaData['filterableIndexPatterns']; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts index 1ddfc54cc187b1..f03b64295641bd 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts @@ -6,9 +6,9 @@ import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; -import { Filter } from '@kbn/es-query'; import { Ast, fromExpression, ExpressionFunctionAST } from '@kbn/interpreter/common'; import { Visualization, Datasource, FramePublicAPI } from '../../types'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; export function prependDatasourceExpression( visualizationExpression: Ast | string | null, @@ -71,7 +71,7 @@ export function prependKibanaContext( }: { timeRange?: TimeRange; query?: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; } ): Ast { const parsedExpression = typeof expression === 'string' ? fromExpression(expression) : expression; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts index ef8194aa6f924b..f223a9c06a2a75 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts @@ -4,9 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { buildExistsFilter } from '@kbn/es-query'; import { getSavedObjectFormat, Props } from './save'; import { createMockDatasource, createMockVisualization } from '../mocks'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; describe('save editor frame state', () => { const mockVisualization = createMockVisualization(); @@ -37,7 +37,7 @@ describe('save editor frame state', () => { }, query: { query: '', language: 'lucene' }, dateRange: { fromDate: 'now-7d', toDate: 'now' }, - filters: [buildExistsFilter({ name: '@timestamp' }, { id: 'indexpattern' })], + filters: [esFilters.buildExistsFilter({ name: '@timestamp' }, { id: 'indexpattern' })], }, }; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/workspace_panel.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/workspace_panel.test.tsx index 3b79172ea6572d..fd35ecd702d23a 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/workspace_panel.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/workspace_panel.test.tsx @@ -5,8 +5,6 @@ */ import React from 'react'; - -import { buildExistsFilter } from '@kbn/es-query'; import { ExpressionRendererProps } from '../../../../../../../src/legacy/core_plugins/expressions/public'; import { Visualization, FramePublicAPI, TableSuggestion } from '../../types'; import { @@ -22,6 +20,7 @@ import { ReactWrapper } from 'enzyme'; import { DragDrop, ChildDragDropProvider } from '../../drag_drop'; import { Ast } from '@kbn/interpreter/common'; import { coreMock } from 'src/core/public/mocks'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; const waitForPromises = () => new Promise(resolve => setTimeout(resolve)); @@ -382,7 +381,7 @@ describe('workspace_panel', () => { instance.setProps({ framePublicAPI: { ...framePublicAPI, - filters: [buildExistsFilter({ name: 'myfield' }, { id: 'index1' })], + filters: [esFilters.buildExistsFilter({ name: 'myfield' }, { id: 'index1' })], }, }); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx index d728457b7e3a3b..95d39409c57de3 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx @@ -5,10 +5,9 @@ */ import { Embeddable } from './embeddable'; -import { TimeRange } from 'src/plugins/data/public'; +import { TimeRange, esFilters } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { ExpressionRendererProps } from 'src/legacy/core_plugins/expressions/public'; -import { Filter } from '@kbn/es-query'; import { Document } from '../../persistence'; jest.mock('../../../../../../../src/legacy/ui/public/inspector', () => ({ @@ -63,7 +62,9 @@ describe('embeddable', () => { it('should re-render if new input is pushed', () => { const timeRange: TimeRange = { from: 'now-15d', to: 'now' }; const query: Query = { language: 'kquery', query: '' }; - const filters: Filter[] = [{ meta: { alias: 'test', negate: false, disabled: false } }]; + const filters: esFilters.Filter[] = [ + { meta: { alias: 'test', negate: false, disabled: false } }, + ]; const embeddable = new Embeddable( expressionRenderer, @@ -88,7 +89,9 @@ describe('embeddable', () => { it('should pass context to embeddable', () => { const timeRange: TimeRange = { from: 'now-15d', to: 'now' }; const query: Query = { language: 'kquery', query: '' }; - const filters: Filter[] = [{ meta: { alias: 'test', negate: false, disabled: false } }]; + const filters: esFilters.Filter[] = [ + { meta: { alias: 'test', negate: false, disabled: false } }, + ]; const embeddable = new Embeddable( expressionRenderer, @@ -112,7 +115,9 @@ describe('embeddable', () => { it('should not re-render if only change is in disabled filter', () => { const timeRange: TimeRange = { from: 'now-15d', to: 'now' }; const query: Query = { language: 'kquery', query: '' }; - const filters: Filter[] = [{ meta: { alias: 'test', negate: false, disabled: true } }]; + const filters: esFilters.Filter[] = [ + { meta: { alias: 'test', negate: false, disabled: true } }, + ]; const embeddable = new Embeddable( expressionRenderer, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx index e815a1951bdb70..0b5d4909c8e73a 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx @@ -8,10 +8,9 @@ import _ from 'lodash'; import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; -import { TimeRange } from 'src/plugins/data/public'; +import { TimeRange, esFilters } from 'src/plugins/data/public'; import { Query, StaticIndexPattern } from 'src/legacy/core_plugins/data/public'; import { ExpressionRenderer } from 'src/legacy/core_plugins/expressions/public'; -import { Filter } from '@kbn/es-query'; import { Subscription } from 'rxjs'; import { Embeddable as AbstractEmbeddable, @@ -32,7 +31,7 @@ export interface LensEmbeddableConfiguration { export interface LensEmbeddableInput extends EmbeddableInput { timeRange?: TimeRange; query?: Query; - filters?: Filter[]; + filters?: esFilters.Filter[]; } export interface LensEmbeddableOutput extends EmbeddableOutput { @@ -50,7 +49,7 @@ export class Embeddable extends AbstractEmbeddable; visualization: unknown; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; }; } diff --git a/x-pack/legacy/plugins/lens/public/types.ts b/x-pack/legacy/plugins/lens/public/types.ts index 2b97f193a563cb..b66bb7bee8f8ad 100644 --- a/x-pack/legacy/plugins/lens/public/types.ts +++ b/x-pack/legacy/plugins/lens/public/types.ts @@ -6,7 +6,6 @@ import { Ast } from '@kbn/interpreter/common'; import { IconType } from '@elastic/eui/src/components/icon/icon'; -import { Filter } from '@kbn/es-query'; import { CoreSetup } from 'src/core/public'; import { Query } from 'src/plugins/data/common'; import { SavedQuery } from 'src/legacy/core_plugins/data/public'; @@ -14,6 +13,7 @@ import { KibanaDatatable } from '../../../../../src/legacy/core_plugins/interpre import { DragContextState } from './drag_drop'; import { Document } from './persistence'; import { DateRange } from '../common'; +import { esFilters } from '../../../../../src/plugins/data/public'; // eslint-disable-next-line export interface EditorFrameOptions {} @@ -32,7 +32,7 @@ export interface EditorFrameProps { doc?: Document; dateRange: DateRange; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; savedQuery?: SavedQuery; // Frame loader (app or embeddable) is expected to call this when it loads and updates @@ -179,7 +179,7 @@ export interface DatasourceDataPanelProps { core: Pick; query: Query; dateRange: DateRange; - filters: Filter[]; + filters: esFilters.Filter[]; } // The only way a visualization has to restrict the query building @@ -309,7 +309,7 @@ export interface FramePublicAPI { dateRange: DateRange; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; // Adds a new layer. This has a side effect of updating the datasource state addNewLayer: () => string; diff --git a/x-pack/legacy/plugins/lens/server/routes/existing_fields.ts b/x-pack/legacy/plugins/lens/server/routes/existing_fields.ts index b6ff1e6ac757b3..ad1af966983fb3 100644 --- a/x-pack/legacy/plugins/lens/server/routes/existing_fields.ts +++ b/x-pack/legacy/plugins/lens/server/routes/existing_fields.ts @@ -11,10 +11,7 @@ import _ from 'lodash'; import { IScopedClusterClient } from 'src/core/server'; import { CoreSetup } from 'src/core/server'; import { BASE_API_URL } from '../../common'; -import { - FieldDescriptor, - IndexPatternsService, -} from '../../../../../../src/legacy/server/index_patterns/service'; +import { FieldDescriptor, IndexPatternsFetcher } from '../../../../../../src/plugins/data/server'; /** * The number of docs to sample to determine field empty status. @@ -42,11 +39,11 @@ export async function existingFieldsRoute(setup: CoreSetup) { async (context, req, res) => { const { indexPatternTitle } = req.params; const requestClient = context.core.elasticsearch.dataClient; - const indexPatternsService = new IndexPatternsService(requestClient.callAsCurrentUser); + const indexPatternsFetcher = new IndexPatternsFetcher(requestClient.callAsCurrentUser); const { fromDate, toDate, timeFieldName } = req.query; try { - const fields = await indexPatternsService.getFieldsForWildcard({ + const fields = await indexPatternsFetcher.getFieldsForWildcard({ pattern: indexPatternTitle, // TODO: Pull this from kibana advanced settings metaFields: ['_source', '_id', '_type', '_index', '_score'], diff --git a/x-pack/legacy/plugins/maps/public/angular/map_controller.js b/x-pack/legacy/plugins/maps/public/angular/map_controller.js index 594548333cb8c7..41c618d68a68e4 100644 --- a/x-pack/legacy/plugins/maps/public/angular/map_controller.js +++ b/x-pack/legacy/plugins/maps/public/angular/map_controller.js @@ -53,9 +53,9 @@ import { MAP_SAVED_OBJECT_TYPE, MAP_APP_PATH } from '../../common/constants'; -import { FilterStateStore } from '@kbn/es-query'; import { start as data } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { npStart } from 'ui/new_platform'; +import { esFilters } from '../../../../../../src/plugins/data/public'; const { savedQueryService } = data.search.services; @@ -247,7 +247,7 @@ app.controller('GisMapController', ($scope, $route, kbnUrl, localStorage, AppSta function addFilters(newFilters) { newFilters.forEach(filter => { - filter.$state = FilterStateStore.APP_STATE; + filter.$state = esFilters.FilterStateStore.APP_STATE; }); $scope.updateFiltersAndDispatch([...$scope.filters, ...newFilters]); } diff --git a/x-pack/legacy/plugins/maps/public/layers/tooltips/es_tooltip_property.js b/x-pack/legacy/plugins/maps/public/layers/tooltips/es_tooltip_property.js index dae56d5526b0ac..446978a151d648 100644 --- a/x-pack/legacy/plugins/maps/public/layers/tooltips/es_tooltip_property.js +++ b/x-pack/legacy/plugins/maps/public/layers/tooltips/es_tooltip_property.js @@ -4,10 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { buildPhraseFilter } from '@kbn/es-query'; import { TooltipProperty } from './tooltip_property'; import _ from 'lodash'; - +import { esFilters } from '../../../../../../../src/plugins/data/public'; export class ESTooltipProperty extends TooltipProperty { constructor(propertyKey, propertyName, rawValue, indexPattern) { @@ -36,7 +35,7 @@ export class ESTooltipProperty extends TooltipProperty { async getESFilters() { return [ - buildPhraseFilter( + esFilters.buildPhraseFilter( this._indexPattern.fields.getByName(this._propertyName), this._rawValue, this._indexPattern) diff --git a/x-pack/legacy/plugins/ml/public/datavisualizer/index_based/data_loader/data_loader.ts b/x-pack/legacy/plugins/ml/public/datavisualizer/index_based/data_loader/data_loader.ts index 464be8a9157aec..f0bb998a276145 100644 --- a/x-pack/legacy/plugins/ml/public/datavisualizer/index_based/data_loader/data_loader.ts +++ b/x-pack/legacy/plugins/ml/public/datavisualizer/index_based/data_loader/data_loader.ts @@ -4,8 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -// @ts-ignore -import { decorateQuery, luceneStringToDsl } from '@kbn/es-query'; import { i18n } from '@kbn/i18n'; import { toastNotifications } from 'ui/notify'; diff --git a/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js b/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js index e50e49eec9f6eb..f2f27499d113c3 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js +++ b/x-pack/legacy/plugins/monitoring/public/components/no_data/no_data.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; +import React, { Fragment, useState } from 'react'; import PropTypes from 'prop-types'; import { EuiSpacer, @@ -43,7 +43,7 @@ function NoDataMessage(props) { export function NoData(props) { const [isLoading, setIsLoading] = useState(false); - const [useInternalCollection, setUseInternalCollection] = useState(false); + const [useInternalCollection, setUseInternalCollection] = useState(props.isOnCloud); async function startSetup() { setIsLoading(true); @@ -64,15 +64,19 @@ export function NoData(props) { - - setUseInternalCollection(false)}> - - - - + { !props.isOnCloud ? ( + + + setUseInternalCollection(false)}> + + + + + + ) : null } diff --git a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js index 3e7d182f1514c5..7da419719e70c7 100644 --- a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js +++ b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.js @@ -7,6 +7,7 @@ import { ajaxErrorHandlersProvider } from './ajax_error_handler'; import { get, contains } from 'lodash'; import chrome from 'ui/chrome'; +import { i18n } from '@kbn/i18n'; function isOnPage(hash) { return contains(window.location.hash, hash); @@ -80,7 +81,7 @@ export const updateSetupModeData = async (uuid, fetchWithoutClusterUuid = false) const oldData = setupModeState.data; const data = await fetchCollectionData(uuid, fetchWithoutClusterUuid); setupModeState.data = data; - if (get(data, '_meta.isOnCloud', false)) { + if (chrome.getInjected('isOnCloud')) { return toggleSetupMode(false); // eslint-disable-line no-use-before-define } notifySetupModeDataChange(oldData); @@ -139,15 +140,17 @@ export const setSetupModeMenuItem = () => { } const globalState = angularState.injector.get('globalState'); - const navItems = globalState.inSetupMode - ? [] - : [{ + const navItems = []; + if (!globalState.inSetupMode && !chrome.getInjected('isOnCloud')) { + navItems.push({ id: 'enter', - label: 'Enter Setup Mode', - description: 'Enter setup', + label: i18n.translate('xpack.monitoring.setupMode.enter', { + defaultMessage: 'Enter Setup Mode' + }), run: () => toggleSetupMode(true), testId: 'enterSetupMode' - }]; + }); + } angularState.scope.topNavMenu = [...navItems]; // LOL angular diff --git a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js index b5878c7ec51818..4e3a8045048ae6 100644 --- a/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js +++ b/x-pack/legacy/plugins/monitoring/public/lib/setup_mode.test.js @@ -4,13 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - toggleSetupMode, - initSetupModeState, - getSetupModeState, - updateSetupModeData, - setSetupModeMenuItem -} from './setup_mode'; +let toggleSetupMode; +let initSetupModeState; +let getSetupModeState; +let updateSetupModeData; +let setSetupModeMenuItem; jest.mock('./ajax_error_handler', () => ({ ajaxErrorHandlersProvider: err => { @@ -52,16 +50,31 @@ function waitForSetupModeData(action) { process.nextTick(action); } +function setModules() { + jest.resetModules(); + injectorModulesMock.globalState.inSetupMode = false; + + const setupMode = require('./setup_mode'); + toggleSetupMode = setupMode.toggleSetupMode; + initSetupModeState = setupMode.initSetupModeState; + getSetupModeState = setupMode.getSetupModeState; + updateSetupModeData = setupMode.updateSetupModeData; + setSetupModeMenuItem = setupMode.setSetupModeMenuItem; +} + describe('setup_mode', () => { - describe('setup', () => { - afterEach(async () => { - try { - toggleSetupMode(false); - } catch (err) { - // Do nothing... + beforeEach(async () => { + jest.doMock('ui/chrome', () => ({ + getInjected: (key) => { + if (key === 'isOnCloud') { + return false; + } } - }); + })); + setModules(); + }); + describe('setup', () => { it('should require angular state', async () => { let error; try { @@ -99,21 +112,25 @@ describe('setup_mode', () => { describe('in setup mode', () => { afterEach(async () => { data = {}; - toggleSetupMode(false); }); it('should enable it through clicking top nav item', async () => { initSetupModeState(angularStateMock.scope, angularStateMock.injector); + setSetupModeMenuItem(); + expect(injectorModulesMock.globalState.inSetupMode).toBe(false); await angularStateMock.scope.topNavMenu[0].run(); expect(injectorModulesMock.globalState.inSetupMode).toBe(true); }); it('should not fetch data if on cloud', async (done) => { - data = { - _meta: { - isOnCloud: true + jest.doMock('ui/chrome', () => ({ + getInjected: (key) => { + if (key === 'isOnCloud') { + return true; + } } - }; + })); + setModules(); initSetupModeState(angularStateMock.scope, angularStateMock.injector); await toggleSetupMode(true); waitForSetupModeData(() => { diff --git a/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js b/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js index 0ecd6c83265fff..f364b70fd934fc 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/no_data/controller.js @@ -5,6 +5,7 @@ */ import React from 'react'; +import chrome from 'ui/chrome'; import { ClusterSettingsChecker, NodeSettingsChecker, @@ -99,7 +100,12 @@ export class NoDataController extends MonitoringViewBaseController { this.renderReact( - + ); } diff --git a/x-pack/legacy/plugins/monitoring/server/lib/setup/collection/get_collection_status.js b/x-pack/legacy/plugins/monitoring/server/lib/setup/collection/get_collection_status.js index a49da8ba60200a..cd5781ccf53446 100644 --- a/x-pack/legacy/plugins/monitoring/server/lib/setup/collection/get_collection_status.js +++ b/x-pack/legacy/plugins/monitoring/server/lib/setup/collection/get_collection_status.js @@ -547,7 +547,6 @@ export const getCollectionStatus = async (req, indexPatterns, clusterUuid, nodeU status._meta = { secondsAgo: NUMBER_OF_SECONDS_AGO_TO_LOOK, liveClusterUuid, - isOnCloud: get(req.server.plugins, 'cloud.config.isCloudEnabled', false) }; return status; diff --git a/x-pack/legacy/plugins/monitoring/ui_exports.js b/x-pack/legacy/plugins/monitoring/ui_exports.js index a10b83086f738b..0976292be576b1 100644 --- a/x-pack/legacy/plugins/monitoring/ui_exports.js +++ b/x-pack/legacy/plugins/monitoring/ui_exports.js @@ -5,6 +5,7 @@ */ import { i18n } from '@kbn/i18n'; +import { get } from 'lodash'; import { resolve } from 'path'; /** @@ -28,7 +29,8 @@ export const getUiExports = () => ({ injectDefaultVars(server) { const config = server.config(); return { - monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled') + monitoringUiEnabled: config.get('xpack.monitoring.ui.enabled'), + isOnCloud: get(server.plugins, 'cloud.config.isCloudEnabled', false) }; }, hacks: [ 'plugins/monitoring/hacks/toggle_app_link_in_nav' ], diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx index 1c712f874969c2..b9c28105e99d15 100644 --- a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx @@ -5,7 +5,6 @@ */ import { EuiLink, EuiText } from '@elastic/eui'; -import { Filter } from '@kbn/es-query'; import React, { useEffect, useState } from 'react'; import { createPortalNode, InPortal } from 'react-reverse-portal'; import { Query } from 'src/plugins/data/common'; @@ -30,6 +29,7 @@ import { IndexPatternsMissingPrompt } from './index_patterns_missing_prompt'; import { MapToolTip } from './map_tool_tip/map_tool_tip'; import * as i18n from './translations'; import { MapEmbeddable, SetQuery } from './types'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; interface EmbeddableMapProps { maintainRatio?: boolean; @@ -75,7 +75,7 @@ EmbeddableMap.displayName = 'EmbeddableMap'; export interface EmbeddedMapProps { query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; startDate: number; endDate: number; setQuery: SetQuery; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map_helpers.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map_helpers.tsx index 7f514b0e53b71c..a50cfa98fc977a 100644 --- a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map_helpers.tsx +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map_helpers.tsx @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import uuid from 'uuid'; import React from 'react'; import { OutPortal, PortalNode } from 'react-reverse-portal'; @@ -28,6 +27,7 @@ import { getLayerList } from './map_config'; // @ts-ignore Missing type defs as maps moves to Typescript import { MAP_SAVED_OBJECT_TYPE } from '../../../../maps/common/constants'; import * as i18n from './translations'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; /** * Displays an error toast for the provided title and message @@ -86,7 +86,7 @@ export const setupEmbeddablesAPI = (plugins: PluginsStart) => { * @throws Error if EmbeddableFactory does not exist */ export const createEmbeddable = async ( - filters: Filter[], + filters: esFilters.Filter[], indexPatterns: IndexPatternMapping[], query: Query, startDate: number, diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts index b3d930a13ca355..10412ecdb50131 100644 --- a/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter as ESFilterType } from '@kbn/es-query'; import { Query } from 'src/plugins/data/common'; import { TimeRange } from 'src/plugins/data/public'; import { @@ -14,9 +13,10 @@ import { EmbeddableFactory, } from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; import { inputsModel } from '../../store/inputs'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; export interface MapEmbeddableInput extends EmbeddableInput { - filters: ESFilterType[]; + filters: esFilters.Filter[]; query: Query; refreshConfig: { isPaused: boolean; diff --git a/x-pack/legacy/plugins/siem/public/components/events_viewer/events_viewer.tsx b/x-pack/legacy/plugins/siem/public/components/events_viewer/events_viewer.tsx index 13f0666b31212c..6b79a6402586e8 100644 --- a/x-pack/legacy/plugins/siem/public/components/events_viewer/events_viewer.tsx +++ b/x-pack/legacy/plugins/siem/public/components/events_viewer/events_viewer.tsx @@ -5,7 +5,7 @@ */ import { EuiPanel } from '@elastic/eui'; -import { Filter, getEsQueryConfig } from '@kbn/es-query'; +import { getEsQueryConfig } from '@kbn/es-query'; import { getOr, isEmpty, isEqual } from 'lodash/fp'; import React from 'react'; import styled from 'styled-components'; @@ -31,6 +31,7 @@ import { TimelineRefetch } from '../timeline/refetch_timeline'; import { isCompactFooter } from '../timeline/timeline'; import { ManageTimelineContext } from '../timeline/timeline_context'; import * as i18n from './translations'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; const DEFAULT_EVENTS_VIEWER_HEIGHT = 500; @@ -44,7 +45,7 @@ interface Props { columns: ColumnHeader[]; dataProviders: DataProvider[]; end: number; - filters: Filter[]; + filters: esFilters.Filter[]; height?: number; id: string; indexPattern: StaticIndexPattern; diff --git a/x-pack/legacy/plugins/siem/public/components/events_viewer/index.tsx b/x-pack/legacy/plugins/siem/public/components/events_viewer/index.tsx index 9483c60dcc5525..5681588cb44b71 100644 --- a/x-pack/legacy/plugins/siem/public/components/events_viewer/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/events_viewer/index.tsx @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { isEqual } from 'lodash/fp'; import React, { useEffect, useState, useCallback } from 'react'; import { connect } from 'react-redux'; @@ -19,6 +18,7 @@ import { ColumnHeader } from '../timeline/body/column_headers/column_header'; import { DataProvider } from '../timeline/data_providers/data_provider'; import { Sort } from '../timeline/body/sort'; import { OnChangeItemsPerPage } from '../timeline/events'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; import { EventsViewer } from './events_viewer'; import { InputsModelId } from '../../store/inputs/constants'; @@ -33,7 +33,7 @@ interface StateReduxProps { activePage?: number; columns: ColumnHeader[]; dataProviders?: DataProvider[]; - filters: Filter[]; + filters: esFilters.Filter[]; isLive: boolean; itemsPerPage?: number; itemsPerPageOptions?: number[]; diff --git a/x-pack/legacy/plugins/siem/public/components/navigation/helpers.ts b/x-pack/legacy/plugins/siem/public/components/navigation/helpers.ts index 68aa115c965d6d..0c44b8d44c3177 100644 --- a/x-pack/legacy/plugins/siem/public/components/navigation/helpers.ts +++ b/x-pack/legacy/plugins/siem/public/components/navigation/helpers.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { isEmpty } from 'lodash/fp'; import { Location } from 'history'; import { Query } from 'src/plugins/data/common'; @@ -17,6 +16,7 @@ import { replaceStateKeyInQueryString, getQueryStringFromLocation, } from '../url_state/helpers'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; import { TabNavigationProps } from './tab_navigation/types'; import { SearchNavTab } from './types'; @@ -25,7 +25,7 @@ export const getSearch = (tab: SearchNavTab, urlState: TabNavigationProps): stri if (tab && tab.urlKey != null && URL_STATE_KEYS[tab.urlKey] != null) { return URL_STATE_KEYS[tab.urlKey].reduce( (myLocation: Location, urlKey: KeyUrlState) => { - let urlStateToReplace: UrlInputsModel | Query | Filter[] | Timeline | string = ''; + let urlStateToReplace: UrlInputsModel | Query | esFilters.Filter[] | Timeline | string = ''; if (urlKey === CONSTANTS.appQuery && urlState.query != null) { if (urlState.query.query === '') { diff --git a/x-pack/legacy/plugins/siem/public/components/navigation/tab_navigation/types.ts b/x-pack/legacy/plugins/siem/public/components/navigation/tab_navigation/types.ts index 1d5ebf2097974f..856651e6f97dc1 100644 --- a/x-pack/legacy/plugins/siem/public/components/navigation/tab_navigation/types.ts +++ b/x-pack/legacy/plugins/siem/public/components/navigation/tab_navigation/types.ts @@ -4,12 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; -import { Query } from 'src/plugins/data/common'; import { UrlInputsModel } from '../../../store/inputs/model'; import { CONSTANTS } from '../../url_state/constants'; import { Timeline } from '../../url_state/types'; import { HostsTableType } from '../../../store/hosts/model'; +import { esFilters, Query } from '../../../../../../../../src/plugins/data/public'; import { SiemNavigationComponentProps } from '../types'; @@ -18,7 +17,7 @@ export interface TabNavigationProps extends SiemNavigationComponentProps { pageName: string; tabName: HostsTableType | undefined; [CONSTANTS.appQuery]?: Query; - [CONSTANTS.filters]?: Filter[]; + [CONSTANTS.filters]?: esFilters.Filter[]; [CONSTANTS.savedQuery]?: string; [CONSTANTS.timerange]: UrlInputsModel; [CONSTANTS.timeline]: Timeline; diff --git a/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.test.tsx b/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.test.tsx index 34e2bc01a4ea78..ed614d79174f02 100644 --- a/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.test.tsx +++ b/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.test.tsx @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { mount, shallow } from 'enzyme'; import toJson from 'enzyme-to-json'; import * as React from 'react'; @@ -13,9 +12,10 @@ import { apolloClientObservable, mockGlobalState, TestProviders } from '../../.. import { createStore, State } from '../../../store'; import { siemFilterManager } from '../../search_bar'; import { AddFilterToGlobalSearchBar } from '.'; +import { esFilters } from '../../../../../../../../src/plugins/data/public'; interface MockSiemFilterManager { - addFilters: (filters: Filter[]) => void; + addFilters: (filters: esFilters.Filter[]) => void; } const mockSiemFilterManager: MockSiemFilterManager = siemFilterManager as MockSiemFilterManager; jest.mock('../../search_bar', () => ({ diff --git a/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.tsx b/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.tsx index 6f3b56417173c7..6e1e6545e5534c 100644 --- a/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/page/add_filter_to_global_search_bar/index.tsx @@ -5,12 +5,12 @@ */ import { EuiIcon, EuiPanel, EuiToolTip } from '@elastic/eui'; -import { Filter } from '@kbn/es-query'; import React from 'react'; import styled from 'styled-components'; import { WithHoverActions } from '../../with_hover_actions'; import { siemFilterManager } from '../../search_bar'; +import { esFilters } from '../../../../../../../../src/plugins/data/public'; import * as i18n from './translations'; @@ -18,7 +18,7 @@ export * from './helpers'; interface OwnProps { children: JSX.Element; - filter: Filter; + filter: esFilters.Filter; onFilterAdded?: () => void; } diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/index.tsx b/x-pack/legacy/plugins/siem/public/components/page/network/index.tsx index 40460f81fa83c6..1f502635a8de4c 100644 --- a/x-pack/legacy/plugins/siem/public/components/page/network/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/page/network/index.tsx @@ -9,3 +9,4 @@ export { KpiNetworkComponent } from './kpi_network'; export { NetworkDnsTable } from './network_dns_table'; export { NetworkTopCountriesTable } from './network_top_countries_table'; export { NetworkTopNFlowTable } from './network_top_n_flow_table'; +export { NetworkHttpTable } from './network_http_table'; diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/__snapshots__/index.test.tsx.snap b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/__snapshots__/index.test.tsx.snap new file mode 100644 index 00000000000000..dfc1b2cf64e389 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/__snapshots__/index.test.tsx.snap @@ -0,0 +1,102 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`NetworkHttp Table Component rendering it renders the default NetworkHttp table 1`] = ` + +`; diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/columns.tsx b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/columns.tsx new file mode 100644 index 00000000000000..6a47a58c85f319 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/columns.tsx @@ -0,0 +1,113 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import React from 'react'; +import numeral from '@elastic/numeral'; +import { NetworkHttpEdges, NetworkHttpFields, NetworkHttpItem } from '../../../../graphql/types'; +import { escapeDataProviderId } from '../../../drag_and_drop/helpers'; +import { getEmptyTagValue } from '../../../empty_value'; +import { IPDetailsLink } from '../../../links'; +import { Columns } from '../../../paginated_table'; + +import * as i18n from './translations'; +import { getRowItemDraggable, getRowItemDraggables } from '../../../tables/helpers'; +export type NetworkHttpColumns = [ + Columns, + Columns, + Columns, + Columns, + Columns, + Columns, + Columns +]; + +export const getNetworkHttpColumns = (tableId: string): NetworkHttpColumns => [ + { + name: i18n.METHOD, + render: ({ node: { methods, path } }) => { + return Array.isArray(methods) && methods.length > 0 + ? getRowItemDraggables({ + attrName: 'http.request.method', + displayCount: 3, + idPrefix: escapeDataProviderId(`${tableId}-table-methods-${path}`), + rowItems: methods, + }) + : getEmptyTagValue(); + }, + }, + { + name: i18n.DOMAIN, + render: ({ node: { domains, path } }) => + Array.isArray(domains) && domains.length > 0 + ? getRowItemDraggables({ + attrName: 'url.domain', + displayCount: 3, + idPrefix: escapeDataProviderId(`${tableId}-table-domains-${path}`), + rowItems: domains, + }) + : getEmptyTagValue(), + }, + { + field: `node.${NetworkHttpFields.path}`, + name: i18n.PATH, + render: path => + path != null + ? getRowItemDraggable({ + attrName: 'url.path', + idPrefix: escapeDataProviderId(`${tableId}-table-path-${path}`), + rowItem: path, + }) + : getEmptyTagValue(), + }, + { + name: i18n.STATUS, + render: ({ node: { statuses, path } }) => + Array.isArray(statuses) && statuses.length > 0 + ? getRowItemDraggables({ + attrName: 'http.response.status_code', + displayCount: 3, + idPrefix: escapeDataProviderId(`${tableId}-table-statuses-${path}`), + rowItems: statuses, + }) + : getEmptyTagValue(), + }, + { + name: i18n.LAST_HOST, + render: ({ node: { lastHost, path } }) => + lastHost != null + ? getRowItemDraggable({ + attrName: 'host.name', + idPrefix: escapeDataProviderId(`${tableId}-table-lastHost-${path}`), + rowItem: lastHost, + }) + : getEmptyTagValue(), + }, + { + name: i18n.LAST_SOURCE_IP, + render: ({ node: { lastSourceIp, path } }) => + lastSourceIp != null + ? getRowItemDraggable({ + attrName: 'source.ip', + idPrefix: escapeDataProviderId(`${tableId}-table-lastSourceIp-${path}`), + rowItem: lastSourceIp, + render: () => , + }) + : getEmptyTagValue(), + }, + { + align: 'right', + field: `node.${NetworkHttpFields.requestCount}`, + name: i18n.REQUESTS, + sortable: true, + render: requestCount => { + if (requestCount != null) { + return numeral(requestCount).format('0,000'); + } else { + return getEmptyTagValue(); + } + }, + }, +]; diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.test.tsx b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.test.tsx new file mode 100644 index 00000000000000..277e136d776fa6 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.test.tsx @@ -0,0 +1,104 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { mount, shallow } from 'enzyme'; +import toJson from 'enzyme-to-json'; +import { getOr } from 'lodash/fp'; +import * as React from 'react'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { Provider as ReduxStoreProvider } from 'react-redux'; + +import { apolloClientObservable, mockGlobalState, TestProviders } from '../../../../mock'; +import { createStore, networkModel, State } from '../../../../store'; + +import { NetworkHttpTable } from '.'; +import { mockData } from './mock'; + +jest.mock('../../../../lib/settings/use_kibana_ui_setting'); + +describe('NetworkHttp Table Component', () => { + const loadPage = jest.fn(); + const state: State = mockGlobalState; + + let store = createStore(state, apolloClientObservable); + + beforeEach(() => { + store = createStore(state, apolloClientObservable); + }); + + describe('rendering', () => { + test('it renders the default NetworkHttp table', () => { + const wrapper = shallow( + + + + ); + + expect(toJson(wrapper)).toMatchSnapshot(); + }); + }); + + describe('Sorting', () => { + test('when you click on the column header, you should show the sorting icon', () => { + const wrapper = mount( + + + + + + ); + + expect(store.getState().network.page.queries!.http.sort).toEqual({ + direction: 'desc', + }); + + wrapper + .find('.euiTable thead tr th button') + .first() + .simulate('click'); + + wrapper.update(); + + expect(store.getState().network.page.queries!.http.sort).toEqual({ + direction: 'asc', + }); + expect( + wrapper + .find('.euiTable thead tr th button') + .first() + .find('svg') + ).toBeTruthy(); + }); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.tsx b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.tsx new file mode 100644 index 00000000000000..71807280ebcb45 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/index.tsx @@ -0,0 +1,149 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import React from 'react'; +import { connect } from 'react-redux'; +import { compose } from 'redux'; +import { ActionCreator } from 'typescript-fsa'; + +import { networkActions } from '../../../../store/actions'; +import { + NetworkHttpEdges, + NetworkHttpFields, + NetworkHttpSortField, +} from '../../../../graphql/types'; +import { networkModel, networkSelectors, State } from '../../../../store'; +import { Criteria, ItemsPerRow, PaginatedTable } from '../../../paginated_table'; + +import { getNetworkHttpColumns } from './columns'; +import * as i18n from './translations'; + +interface OwnProps { + data: NetworkHttpEdges[]; + fakeTotalCount: number; + id: string; + isInspect: boolean; + loading: boolean; + loadPage: (newActivePage: number) => void; + showMorePagesIndicator: boolean; + totalCount: number; + type: networkModel.NetworkType; +} + +interface NetworkHttpTableReduxProps { + activePage: number; + limit: number; + sort: NetworkHttpSortField; +} + +interface NetworkHttpTableDispatchProps { + updateNetworkTable: ActionCreator<{ + networkType: networkModel.NetworkType; + tableType: networkModel.AllNetworkTables; + updates: networkModel.TableUpdates; + }>; +} + +type NetworkHttpTableProps = OwnProps & NetworkHttpTableReduxProps & NetworkHttpTableDispatchProps; + +const rowItems: ItemsPerRow[] = [ + { + text: i18n.ROWS_5, + numberOfRow: 5, + }, + { + text: i18n.ROWS_10, + numberOfRow: 10, + }, +]; + +const NetworkHttpTableComponent = React.memo( + ({ + activePage, + data, + fakeTotalCount, + id, + isInspect, + limit, + loading, + loadPage, + showMorePagesIndicator, + sort, + totalCount, + type, + updateNetworkTable, + }) => { + const onChange = (criteria: Criteria, tableType: networkModel.HttpTableType) => { + if (criteria.sort != null && criteria.sort.direction !== sort.direction) { + updateNetworkTable({ + networkType: type, + tableType, + updates: { + sort: { + direction: criteria.sort.direction, + }, + }, + }); + } + }; + const tableType = + type === networkModel.NetworkType.page + ? networkModel.NetworkTableType.http + : networkModel.IpDetailsTableType.http; + return ( + loadPage(newActivePage)} + onChange={criteria => onChange(criteria, tableType)} + pageOfItems={data} + showMorePagesIndicator={showMorePagesIndicator} + sorting={{ field: `node.${NetworkHttpFields.requestCount}`, direction: sort.direction }} + totalCount={fakeTotalCount} + updateActivePage={newPage => + updateNetworkTable({ + networkType: type, + tableType, + updates: { activePage: newPage }, + }) + } + updateLimitPagination={newLimit => + updateNetworkTable({ + networkType: type, + tableType, + updates: { limit: newLimit }, + }) + } + /> + ); + } +); + +NetworkHttpTableComponent.displayName = 'NetworkHttpTableComponent'; + +const makeMapStateToProps = () => { + const getNetworkHttpSelector = networkSelectors.httpSelector(); + const mapStateToProps = (state: State, { type }: OwnProps) => getNetworkHttpSelector(state, type); + return mapStateToProps; +}; + +export const NetworkHttpTable = compose>( + connect( + makeMapStateToProps, + { + updateNetworkTable: networkActions.updateNetworkTable, + } + ) +)(NetworkHttpTableComponent); diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/mock.ts b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/mock.ts new file mode 100644 index 00000000000000..ed9b00ba8e49e5 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/mock.ts @@ -0,0 +1,88 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { NetworkHttpData } from '../../../../graphql/types'; + +export const mockData: { NetworkHttp: NetworkHttpData } = { + NetworkHttp: { + edges: [ + { + node: { + _id: '/computeMetadata/v1/instance/virtual-clock/drift-token', + domains: ['metadata.google.internal'], + methods: ['get'], + statuses: [], + lastHost: 'suricata-iowa', + lastSourceIp: '10.128.0.21', + path: '/computeMetadata/v1/instance/virtual-clock/drift-token', + requestCount: 1440, + }, + cursor: { + value: '/computeMetadata/v1/instance/virtual-clock/drift-token', + tiebreaker: null, + }, + }, + { + node: { + _id: '/computeMetadata/v1/', + domains: ['metadata.google.internal'], + methods: ['get'], + statuses: ['200'], + lastHost: 'suricata-iowa', + lastSourceIp: '10.128.0.21', + path: '/computeMetadata/v1/', + requestCount: 1020, + }, + cursor: { + value: '/computeMetadata/v1/', + tiebreaker: null, + }, + }, + { + node: { + _id: '/computeMetadata/v1/instance/network-interfaces/', + domains: ['metadata.google.internal'], + methods: ['get'], + statuses: [], + lastHost: 'suricata-iowa', + lastSourceIp: '10.128.0.21', + path: '/computeMetadata/v1/instance/network-interfaces/', + requestCount: 960, + }, + cursor: { + value: '/computeMetadata/v1/instance/network-interfaces/', + tiebreaker: null, + }, + }, + { + node: { + _id: '/downloads/ca_setup.exe', + domains: ['www.oxid.it'], + methods: ['get'], + statuses: ['200'], + lastHost: 'jessie', + lastSourceIp: '10.0.2.15', + path: '/downloads/ca_setup.exe', + requestCount: 3, + }, + cursor: { + value: '/downloads/ca_setup.exe', + tiebreaker: null, + }, + }, + ], + inspect: { + dsl: [''], + response: [''], + }, + pageInfo: { + activePage: 0, + fakeTotalCount: 4, + showMorePagesIndicator: false, + }, + totalCount: 4, + }, +}; diff --git a/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/translations.ts b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/translations.ts new file mode 100644 index 00000000000000..c891a7c75fc3c1 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/page/network/network_http_table/translations.ts @@ -0,0 +1,57 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { i18n } from '@kbn/i18n'; + +export const HTTP_REQUESTS = i18n.translate('xpack.siem.networkHttpTable.title', { + defaultMessage: 'HTTP Requests', +}); + +export const UNIT = (totalCount: number) => + i18n.translate('xpack.siem.networkHttpTable.unit', { + values: { totalCount }, + defaultMessage: `{totalCount, plural, =1 {request} other {requests}}`, + }); + +export const METHOD = i18n.translate('xpack.siem.networkHttpTable.column.methodTitle', { + defaultMessage: 'Method', +}); +export const DOMAIN = i18n.translate('xpack.siem.networkHttpTable.column.domainTitle', { + defaultMessage: 'Domain', +}); + +export const PATH = i18n.translate('xpack.siem.networkHttpTable.column.pathTitle', { + defaultMessage: 'Path', +}); + +export const STATUS = i18n.translate('xpack.siem.networkHttpTable.column.statusTitle', { + defaultMessage: 'Status', +}); + +export const LAST_HOST = i18n.translate('xpack.siem.networkHttpTable.column.lastHostTitle', { + defaultMessage: 'Last host', +}); + +export const LAST_SOURCE_IP = i18n.translate( + 'xpack.siem.networkHttpTable.column.lastSourceIpTitle', + { + defaultMessage: 'Last source Ip', + } +); + +export const REQUESTS = i18n.translate('xpack.siem.networkHttpTable.column.requestsTitle', { + defaultMessage: 'Requests', +}); + +export const ROWS_5 = i18n.translate('xpack.siem.networkHttpTable.rows', { + values: { numRows: 5 }, + defaultMessage: '{numRows} {numRows, plural, =0 {rows} =1 {row} other {rows}}', +}); + +export const ROWS_10 = i18n.translate('xpack.siem.networkHttpTable.rows', { + values: { numRows: 10 }, + defaultMessage: '{numRows} {numRows, plural, =0 {rows} =1 {row} other {rows}}', +}); diff --git a/x-pack/legacy/plugins/siem/public/components/paginated_table/index.tsx b/x-pack/legacy/plugins/siem/public/components/paginated_table/index.tsx index 4de64c6b32aa96..646d003051e836 100644 --- a/x-pack/legacy/plugins/siem/public/components/paginated_table/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/paginated_table/index.tsx @@ -23,6 +23,7 @@ import { Direction } from '../../graphql/types'; import { AuthTableColumns } from '../page/hosts/authentications_table'; import { HostsTableColumns } from '../page/hosts/hosts_table'; import { NetworkDnsColumns } from '../page/network/network_dns_table/columns'; +import { NetworkHttpColumns } from '../page/network/network_http_table/columns'; import { NetworkTopNFlowColumns, NetworkTopNFlowColumnsIpDetails, @@ -72,6 +73,7 @@ declare type BasicTableColumns = | HostsTableColumns | HostsTableColumnsTest | NetworkDnsColumns + | NetworkHttpColumns | NetworkTopCountriesColumns | NetworkTopCountriesColumnsIpDetails | NetworkTopNFlowColumns diff --git a/x-pack/legacy/plugins/siem/public/components/search_bar/index.tsx b/x-pack/legacy/plugins/siem/public/components/search_bar/index.tsx index e024a4e68492b3..c885d001542e58 100644 --- a/x-pack/legacy/plugins/siem/public/components/search_bar/index.tsx +++ b/x-pack/legacy/plugins/siem/public/components/search_bar/index.tsx @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { getOr, isEqual, set } from 'lodash/fp'; import React, { memo, useEffect, useCallback, useMemo } from 'react'; import { connect } from 'react-redux'; @@ -37,6 +36,7 @@ import { toStrSelector, } from './selectors'; import { timelineActions, hostsActions, networkActions } from '../../store/actions'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; const { ui: { SearchBar }, @@ -67,7 +67,7 @@ interface SiemSearchBarDispatch { id: InputsModelId; savedQuery: SavedQuery | undefined; }) => void; - setSearchBarFilter: ({ id, filters }: { id: InputsModelId; filters: Filter[] }) => void; + setSearchBarFilter: ({ id, filters }: { id: InputsModelId; filters: esFilters.Filter[] }) => void; } interface SiemSearchBarProps { @@ -313,7 +313,7 @@ SearchBarComponent.displayName = 'SiemSearchBar'; interface UpdateReduxSearchBar extends OnTimeChangeProps { id: InputsModelId; - filters?: Filter[]; + filters?: esFilters.Filter[]; query?: Query; savedQuery?: SavedQuery; resetSavedQuery?: boolean; @@ -397,7 +397,7 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({ updateSearch: dispatchUpdateSearch(dispatch), setSavedQuery: ({ id, savedQuery }: { id: InputsModelId; savedQuery: SavedQuery | undefined }) => dispatch(inputsActions.setSavedQuery({ id, savedQuery })), - setSearchBarFilter: ({ id, filters }: { id: InputsModelId; filters: Filter[] }) => + setSearchBarFilter: ({ id, filters }: { id: InputsModelId; filters: esFilters.Filter[] }) => dispatch(inputsActions.setSearchBarFilter({ id, filters })), }); diff --git a/x-pack/legacy/plugins/siem/public/components/tables/__snapshots__/helpers.test.tsx.snap b/x-pack/legacy/plugins/siem/public/components/tables/__snapshots__/helpers.test.tsx.snap index aa8666c247caef..1e6c5eb89352bd 100644 --- a/x-pack/legacy/plugins/siem/public/components/tables/__snapshots__/helpers.test.tsx.snap +++ b/x-pack/legacy/plugins/siem/public/components/tables/__snapshots__/helpers.test.tsx.snap @@ -33,7 +33,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh "and": Array [], "enabled": true, "excluded": false, - "id": "idPrefix-attrName-item1", + "id": "idPrefix-attrName-item1-0", "kqlQuery": "", "name": "item1", "queryMatch": Object { @@ -44,7 +44,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh }, } } - key="idPrefix-attrName-item1" + key="idPrefix-attrName-item1-0" render={[Function]} /> , @@ -55,7 +55,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh "and": Array [], "enabled": true, "excluded": false, - "id": "idPrefix-attrName-item2", + "id": "idPrefix-attrName-item2-1", "kqlQuery": "", "name": "item2", "queryMatch": Object { @@ -66,7 +66,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh }, } } - key="idPrefix-attrName-item2" + key="idPrefix-attrName-item2-1" render={[Function]} /> , @@ -77,7 +77,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh "and": Array [], "enabled": true, "excluded": false, - "id": "idPrefix-attrName-item3", + "id": "idPrefix-attrName-item3-2", "kqlQuery": "", "name": "item3", "queryMatch": Object { @@ -88,7 +88,7 @@ exports[`Table Helpers #getRowItemDraggables it returns correctly against snapsh }, } } - key="idPrefix-attrName-item3" + key="idPrefix-attrName-item3-2" render={[Function]} /> diff --git a/x-pack/legacy/plugins/siem/public/components/tables/helpers.tsx b/x-pack/legacy/plugins/siem/public/components/tables/helpers.tsx index 2557630a603061..b4ee93f9963e43 100644 --- a/x-pack/legacy/plugins/siem/public/components/tables/helpers.tsx +++ b/x-pack/legacy/plugins/siem/public/components/tables/helpers.tsx @@ -89,7 +89,7 @@ export const getRowItemDraggables = ({ }): JSX.Element => { if (rowItems != null && rowItems.length > 0) { const draggables = rowItems.slice(0, displayCount).map((rowItem, index) => { - const id = escapeDataProviderId(`${idPrefix}-${attrName}-${rowItem}`); + const id = escapeDataProviderId(`${idPrefix}-${attrName}-${rowItem}-${index}`); return ( {index !== 0 && ( diff --git a/x-pack/legacy/plugins/siem/public/components/timeline/helpers.tsx b/x-pack/legacy/plugins/siem/public/components/timeline/helpers.tsx index a18e4d7962e955..6182fca6e2e993 100644 --- a/x-pack/legacy/plugins/siem/public/components/timeline/helpers.tsx +++ b/x-pack/legacy/plugins/siem/public/components/timeline/helpers.tsx @@ -4,11 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { isEmpty, isNumber, get } from 'lodash/fp'; import memoizeOne from 'memoize-one'; import { StaticIndexPattern } from 'ui/index_patterns'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { escapeQueryValue, convertToBuildEsQuery, EsQueryConfig } from '../../lib/keury'; @@ -106,7 +105,7 @@ export const combineQueries = ({ dataProviders: DataProvider[]; indexPattern: StaticIndexPattern; browserFields: BrowserFields; - filters: Filter[]; + filters: esFilters.Filter[]; kqlQuery: Query; kqlMode: string; start: number; diff --git a/x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts b/x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts index 9c49d356cd4bca..f7487d7a81a7a3 100644 --- a/x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts +++ b/x-pack/legacy/plugins/siem/public/components/url_state/helpers.ts @@ -4,11 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { decode, encode, RisonValue } from 'rison-node'; import { Location } from 'history'; import { QueryString } from 'ui/utils/query_string'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { inputsSelectors, State, timelineSelectors } from '../../store'; import { SiemPageName } from '../../pages/home/types'; @@ -154,7 +153,7 @@ export const makeMapStateToProps = () => { let searchAttr: { [CONSTANTS.appQuery]?: Query; - [CONSTANTS.filters]?: Filter[]; + [CONSTANTS.filters]?: esFilters.Filter[]; [CONSTANTS.savedQuery]?: string; } = { [CONSTANTS.appQuery]: getGlobalQuerySelector(state), diff --git a/x-pack/legacy/plugins/siem/public/components/url_state/initialize_redux_by_url.tsx b/x-pack/legacy/plugins/siem/public/components/url_state/initialize_redux_by_url.tsx index fa3b2788667042..013983c78a3a51 100644 --- a/x-pack/legacy/plugins/siem/public/components/url_state/initialize_redux_by_url.tsx +++ b/x-pack/legacy/plugins/siem/public/components/url_state/initialize_redux_by_url.tsx @@ -4,11 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { get, isEmpty } from 'lodash/fp'; import { Dispatch } from 'redux'; import { SavedQuery } from 'src/legacy/core_plugins/data/public'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { inputsActions } from '../../store/actions'; import { InputsModelId, TimeRangeKinds } from '../../store/inputs/constants'; @@ -125,7 +124,7 @@ export const dispatchSetInitialStateFromUrl = ( } if (urlKey === CONSTANTS.filters) { - const filters: Filter[] = decodeRisonUrlState(newUrlStateString); + const filters: esFilters.Filter[] = decodeRisonUrlState(newUrlStateString); siemFilterManager.setFilters(filters || []); } diff --git a/x-pack/legacy/plugins/siem/public/components/url_state/types.ts b/x-pack/legacy/plugins/siem/public/components/url_state/types.ts index f858ffc32ddbcb..44c050a1990ce3 100644 --- a/x-pack/legacy/plugins/siem/public/components/url_state/types.ts +++ b/x-pack/legacy/plugins/siem/public/components/url_state/types.ts @@ -4,11 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import ApolloClient from 'apollo-client'; import { ActionCreator } from 'typescript-fsa'; import { StaticIndexPattern } from 'ui/index_patterns'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { UrlInputsModel } from '../../store/inputs/model'; import { RouteSpyState } from '../../utils/route/types'; @@ -60,7 +59,7 @@ export interface Timeline { export interface UrlState { [CONSTANTS.appQuery]?: Query; - [CONSTANTS.filters]?: Filter[]; + [CONSTANTS.filters]?: esFilters.Filter[]; [CONSTANTS.savedQuery]?: string; [CONSTANTS.timerange]: UrlInputsModel; [CONSTANTS.timeline]: Timeline; diff --git a/x-pack/legacy/plugins/siem/public/components/url_state/use_url_state.tsx b/x-pack/legacy/plugins/siem/public/components/url_state/use_url_state.tsx index 5b9511f169744e..f1eeb4e6fbec40 100644 --- a/x-pack/legacy/plugins/siem/public/components/url_state/use_url_state.tsx +++ b/x-pack/legacy/plugins/siem/public/components/url_state/use_url_state.tsx @@ -3,11 +3,11 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; + import { Location } from 'history'; import { isEqual, difference, isEmpty } from 'lodash/fp'; import { useEffect, useRef, useState } from 'react'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { UrlInputsModel } from '../../store/inputs/model'; import { useApolloClient } from '../../utils/apollo_context'; @@ -59,7 +59,7 @@ export const useUrlStateHooks = ({ const prevProps = usePrevious({ pathName, urlState }); const replaceStateInLocation = ( - urlStateToReplace: UrlInputsModel | Query | Filter[] | Timeline | string, + urlStateToReplace: UrlInputsModel | Query | esFilters.Filter[] | Timeline | string, urlStateKey: string, latestLocation: Location = { hash: '', @@ -94,7 +94,10 @@ export const useUrlStateHooks = ({ urlKey ); if (newUrlStateString) { - const queryState: Query | Timeline | Filter[] = decodeRisonUrlState(newUrlStateString); + const queryState: Query | Timeline | esFilters.Filter[] = decodeRisonUrlState( + newUrlStateString + ); + if ( urlKey === CONSTANTS.appQuery && queryState != null && diff --git a/x-pack/legacy/plugins/siem/public/containers/network_http/index.gql_query.ts b/x-pack/legacy/plugins/siem/public/containers/network_http/index.gql_query.ts new file mode 100644 index 00000000000000..bedf13dfa98495 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/network_http/index.gql_query.ts @@ -0,0 +1,57 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import gql from 'graphql-tag'; + +export const networkHttpQuery = gql` + query GetNetworkHttpQuery( + $sourceId: ID! + $ip: String + $filterQuery: String + $pagination: PaginationInputPaginated! + $sort: NetworkHttpSortField! + $timerange: TimerangeInput! + $defaultIndex: [String!]! + $inspect: Boolean! + ) { + source(id: $sourceId) { + id + NetworkHttp( + filterQuery: $filterQuery + ip: $ip + pagination: $pagination + sort: $sort + timerange: $timerange + defaultIndex: $defaultIndex + ) { + totalCount + edges { + node { + domains + lastHost + lastSourceIp + methods + path + requestCount + statuses + } + cursor { + value + } + } + pageInfo { + activePage + fakeTotalCount + showMorePagesIndicator + } + inspect @include(if: $inspect) { + dsl + response + } + } + } + } +`; diff --git a/x-pack/legacy/plugins/siem/public/containers/network_http/index.tsx b/x-pack/legacy/plugins/siem/public/containers/network_http/index.tsx new file mode 100644 index 00000000000000..d76ab53b2de3a9 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/network_http/index.tsx @@ -0,0 +1,154 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { getOr } from 'lodash/fp'; +import React from 'react'; +import { Query } from 'react-apollo'; +import { connect } from 'react-redux'; +import { compose } from 'redux'; +import chrome from 'ui/chrome'; + +import { DEFAULT_INDEX_KEY } from '../../../common/constants'; +import { + GetNetworkHttpQuery, + NetworkHttpEdges, + NetworkHttpSortField, + PageInfoPaginated, +} from '../../graphql/types'; +import { inputsModel, inputsSelectors, networkModel, networkSelectors, State } from '../../store'; +import { generateTablePaginationOptions } from '../../components/paginated_table/helpers'; +import { createFilter, getDefaultFetchPolicy } from '../helpers'; +import { QueryTemplatePaginated, QueryTemplatePaginatedProps } from '../query_template_paginated'; +import { networkHttpQuery } from './index.gql_query'; + +const ID = 'networkHttpQuery'; + +export interface NetworkHttpArgs { + id: string; + ip?: string; + inspect: inputsModel.InspectQuery; + isInspected: boolean; + loading: boolean; + loadPage: (newActivePage: number) => void; + networkHttp: NetworkHttpEdges[]; + pageInfo: PageInfoPaginated; + refetch: inputsModel.Refetch; + totalCount: number; +} + +export interface OwnProps extends QueryTemplatePaginatedProps { + children: (args: NetworkHttpArgs) => React.ReactNode; + ip?: string; + type: networkModel.NetworkType; +} + +export interface NetworkHttpComponentReduxProps { + activePage: number; + isInspected: boolean; + limit: number; + sort: NetworkHttpSortField; +} + +type NetworkHttpProps = OwnProps & NetworkHttpComponentReduxProps; + +class NetworkHttpComponentQuery extends QueryTemplatePaginated< + NetworkHttpProps, + GetNetworkHttpQuery.Query, + GetNetworkHttpQuery.Variables +> { + public render() { + const { + activePage, + children, + endDate, + filterQuery, + id = ID, + ip, + isInspected, + limit, + skip, + sourceId, + sort, + startDate, + } = this.props; + const variables: GetNetworkHttpQuery.Variables = { + defaultIndex: chrome.getUiSettingsClient().get(DEFAULT_INDEX_KEY), + filterQuery: createFilter(filterQuery), + inspect: isInspected, + ip, + pagination: generateTablePaginationOptions(activePage, limit), + sort, + sourceId, + timerange: { + interval: '12h', + from: startDate!, + to: endDate!, + }, + }; + return ( + + fetchPolicy={getDefaultFetchPolicy()} + notifyOnNetworkStatusChange + query={networkHttpQuery} + skip={skip} + variables={variables} + > + {({ data, loading, fetchMore, networkStatus, refetch }) => { + const networkHttp = getOr([], `source.NetworkHttp.edges`, data); + this.setFetchMore(fetchMore); + this.setFetchMoreOptions((newActivePage: number) => ({ + variables: { + pagination: generateTablePaginationOptions(newActivePage, limit), + }, + updateQuery: (prev, { fetchMoreResult }) => { + if (!fetchMoreResult) { + return prev; + } + return { + ...fetchMoreResult, + source: { + ...fetchMoreResult.source, + NetworkHttp: { + ...fetchMoreResult.source.NetworkHttp, + edges: [...fetchMoreResult.source.NetworkHttp.edges], + }, + }, + }; + }, + })); + const isLoading = this.isItAValidLoading(loading, variables, networkStatus); + return children({ + id, + inspect: getOr(null, 'source.NetworkHttp.inspect', data), + isInspected, + loading: isLoading, + loadPage: this.wrappedLoadMore, + networkHttp, + pageInfo: getOr({}, 'source.NetworkHttp.pageInfo', data), + refetch: this.memoizedRefetchQuery(variables, limit, refetch), + totalCount: getOr(-1, 'source.NetworkHttp.totalCount', data), + }); + }} + + ); + } +} + +const makeMapStateToProps = () => { + const getHttpSelector = networkSelectors.httpSelector(); + const getQuery = inputsSelectors.globalQueryByIdSelector(); + return (state: State, { id = ID, type }: OwnProps) => { + const { isInspected } = getQuery(state, id); + return { + ...getHttpSelector(state, type), + isInspected, + }; + }; +}; + +export const NetworkHttpQuery = compose>( + connect(makeMapStateToProps) +)(NetworkHttpComponentQuery); diff --git a/x-pack/legacy/plugins/siem/public/graphql/introspection.json b/x-pack/legacy/plugins/siem/public/graphql/introspection.json index 488a6310db3f27..8732b61e5c0eb0 100644 --- a/x-pack/legacy/plugins/siem/public/graphql/introspection.json +++ b/x-pack/legacy/plugins/siem/public/graphql/introspection.json @@ -1777,6 +1777,93 @@ "isDeprecated": false, "deprecationReason": null }, + { + "name": "NetworkHttp", + "description": "", + "args": [ + { + "name": "id", + "description": "", + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "defaultValue": null + }, + { + "name": "filterQuery", + "description": "", + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "defaultValue": null + }, + { + "name": "ip", + "description": "", + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "defaultValue": null + }, + { + "name": "pagination", + "description": "", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "INPUT_OBJECT", + "name": "PaginationInputPaginated", + "ofType": null + } + }, + "defaultValue": null + }, + { + "name": "sort", + "description": "", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "INPUT_OBJECT", + "name": "NetworkHttpSortField", + "ofType": null + } + }, + "defaultValue": null + }, + { + "name": "timerange", + "description": "", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "INPUT_OBJECT", "name": "TimerangeInput", "ofType": null } + }, + "defaultValue": null + }, + { + "name": "defaultIndex", + "description": "", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + } + } + }, + "defaultValue": null + } + ], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "NetworkHttpData", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "OverviewNetwork", "description": "", @@ -8048,6 +8135,236 @@ "enumValues": null, "possibleTypes": null }, + { + "kind": "INPUT_OBJECT", + "name": "NetworkHttpSortField", + "description": "", + "fields": null, + "inputFields": [ + { + "name": "direction", + "description": "", + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "ENUM", "name": "Direction", "ofType": null } + }, + "defaultValue": null + } + ], + "interfaces": null, + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "NetworkHttpData", + "description": "", + "fields": [ + { + "name": "edges", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "NetworkHttpEdges", "ofType": null } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "totalCount", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "Float", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "pageInfo", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "PageInfoPaginated", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "inspect", + "description": "", + "args": [], + "type": { "kind": "OBJECT", "name": "Inspect", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "NetworkHttpEdges", + "description": "", + "fields": [ + { + "name": "node", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "NetworkHttpItem", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "cursor", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "OBJECT", "name": "CursorType", "ofType": null } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, + { + "kind": "OBJECT", + "name": "NetworkHttpItem", + "description": "", + "fields": [ + { + "name": "_id", + "description": "", + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "domains", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "lastHost", + "description": "", + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "lastSourceIp", + "description": "", + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "methods", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "path", + "description": "", + "args": [], + "type": { "kind": "SCALAR", "name": "String", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "requestCount", + "description": "", + "args": [], + "type": { "kind": "SCALAR", "name": "Float", "ofType": null }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "statuses", + "description": "", + "args": [], + "type": { + "kind": "NON_NULL", + "name": null, + "ofType": { + "kind": "LIST", + "name": null, + "ofType": { + "kind": "NON_NULL", + "name": null, + "ofType": { "kind": "SCALAR", "name": "String", "ofType": null } + } + } + }, + "isDeprecated": false, + "deprecationReason": null + } + ], + "inputFields": null, + "interfaces": [], + "enumValues": null, + "possibleTypes": null + }, { "kind": "OBJECT", "name": "OverviewNetworkData", @@ -11275,6 +11592,54 @@ ], "possibleTypes": null }, + { + "kind": "ENUM", + "name": "NetworkHttpFields", + "description": "", + "fields": null, + "inputFields": null, + "interfaces": null, + "enumValues": [ + { + "name": "domains", + "description": "", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "lastHost", + "description": "", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "lastSourceIp", + "description": "", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "methods", + "description": "", + "isDeprecated": false, + "deprecationReason": null + }, + { "name": "path", "description": "", "isDeprecated": false, "deprecationReason": null }, + { + "name": "requestCount", + "description": "", + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "statuses", + "description": "", + "isDeprecated": false, + "deprecationReason": null + } + ], + "possibleTypes": null + }, { "kind": "ENUM", "name": "FlowDirection", diff --git a/x-pack/legacy/plugins/siem/public/graphql/types.ts b/x-pack/legacy/plugins/siem/public/graphql/types.ts index dbb62d73e5994a..a1b259b876a142 100644 --- a/x-pack/legacy/plugins/siem/public/graphql/types.ts +++ b/x-pack/legacy/plugins/siem/public/graphql/types.ts @@ -85,6 +85,10 @@ export interface NetworkDnsSortField { direction: Direction; } +export interface NetworkHttpSortField { + direction: Direction; +} + export interface TlsSortField { field: TlsFields; @@ -294,6 +298,16 @@ export enum NetworkDirectionEcs { unknown = 'unknown', } +export enum NetworkHttpFields { + domains = 'domains', + lastHost = 'lastHost', + lastSourceIp = 'lastSourceIp', + methods = 'methods', + path = 'path', + requestCount = 'requestCount', + statuses = 'statuses', +} + export enum FlowDirection { uniDirectional = 'uniDirectional', biDirectional = 'biDirectional', @@ -433,6 +447,8 @@ export interface Source { NetworkDns: NetworkDnsData; + NetworkHttp: NetworkHttpData; + OverviewNetwork?: Maybe; OverviewHost?: Maybe; @@ -1588,6 +1604,40 @@ export interface NetworkDnsItem { uniqueDomains?: Maybe; } +export interface NetworkHttpData { + edges: NetworkHttpEdges[]; + + totalCount: number; + + pageInfo: PageInfoPaginated; + + inspect?: Maybe; +} + +export interface NetworkHttpEdges { + node: NetworkHttpItem; + + cursor: CursorType; +} + +export interface NetworkHttpItem { + _id?: Maybe; + + domains: string[]; + + lastHost?: Maybe; + + lastSourceIp?: Maybe; + + methods: string[]; + + path?: Maybe; + + requestCount?: Maybe; + + statuses: string[]; +} + export interface OverviewNetworkData { auditbeatSocket?: Maybe; @@ -2162,6 +2212,21 @@ export interface NetworkDnsSourceArgs { defaultIndex: string[]; } +export interface NetworkHttpSourceArgs { + id?: Maybe; + + filterQuery?: Maybe; + + ip?: Maybe; + + pagination: PaginationInputPaginated; + + sort: NetworkHttpSortField; + + timerange: TimerangeInput; + + defaultIndex: string[]; +} export interface OverviewNetworkSourceArgs { id?: Maybe; @@ -3209,6 +3274,95 @@ export namespace GetNetworkDnsQuery { }; } +export namespace GetNetworkHttpQuery { + export type Variables = { + sourceId: string; + ip?: Maybe; + filterQuery?: Maybe; + pagination: PaginationInputPaginated; + sort: NetworkHttpSortField; + timerange: TimerangeInput; + defaultIndex: string[]; + inspect: boolean; + }; + + export type Query = { + __typename?: 'Query'; + + source: Source; + }; + + export type Source = { + __typename?: 'Source'; + + id: string; + + NetworkHttp: NetworkHttp; + }; + + export type NetworkHttp = { + __typename?: 'NetworkHttpData'; + + totalCount: number; + + edges: Edges[]; + + pageInfo: PageInfo; + + inspect: Maybe; + }; + + export type Edges = { + __typename?: 'NetworkHttpEdges'; + + node: Node; + + cursor: Cursor; + }; + + export type Node = { + __typename?: 'NetworkHttpItem'; + + domains: string[]; + + lastHost: Maybe; + + lastSourceIp: Maybe; + + methods: string[]; + + path: Maybe; + + requestCount: Maybe; + + statuses: string[]; + }; + + export type Cursor = { + __typename?: 'CursorType'; + + value: Maybe; + }; + + export type PageInfo = { + __typename?: 'PageInfoPaginated'; + + activePage: number; + + fakeTotalCount: number; + + showMorePagesIndicator: boolean; + }; + + export type Inspect = { + __typename?: 'Inspect'; + + dsl: string[]; + + response: string[]; + }; +} + export namespace GetNetworkTopCountriesQuery { export type Variables = { sourceId: string; diff --git a/x-pack/legacy/plugins/siem/public/lib/keury/index.ts b/x-pack/legacy/plugins/siem/public/lib/keury/index.ts index 7bd8560a1770a7..bf8726d5ed377f 100644 --- a/x-pack/legacy/plugins/siem/public/lib/keury/index.ts +++ b/x-pack/legacy/plugins/siem/public/lib/keury/index.ts @@ -4,10 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { buildEsQuery, Filter, fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query'; +import { buildEsQuery, fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query'; import { isEmpty, isString, flow } from 'lodash/fp'; import { StaticIndexPattern } from 'ui/index_patterns'; import { Query } from 'src/plugins/data/common'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; import { KueryFilterQuery } from '../../store'; @@ -83,7 +84,7 @@ export const convertToBuildEsQuery = ({ config: EsQueryConfig; indexPattern: StaticIndexPattern; queries: Query[]; - filters: Filter[]; + filters: esFilters.Filter[]; }) => { try { return JSON.stringify( diff --git a/x-pack/legacy/plugins/siem/public/mock/global_state.ts b/x-pack/legacy/plugins/siem/public/mock/global_state.ts index 9be209321085f4..ada34acbc19460 100644 --- a/x-pack/legacy/plugins/siem/public/mock/global_state.ts +++ b/x-pack/legacy/plugins/siem/public/mock/global_state.ts @@ -96,6 +96,11 @@ export const mockGlobalState: State = { limit: 10, sort: { field: TlsFields._id, direction: Direction.desc }, }, + [networkModel.NetworkTableType.http]: { + activePage: 0, + limit: 10, + sort: { direction: Direction.desc }, + }, }, }, details: { @@ -131,6 +136,11 @@ export const mockGlobalState: State = { limit: 10, sort: { field: UsersFields.name, direction: Direction.asc }, }, + [networkModel.IpDetailsTableType.http]: { + activePage: 0, + limit: 10, + sort: { direction: Direction.desc }, + }, }, }, }, diff --git a/x-pack/legacy/plugins/siem/public/pages/hosts/details/index.tsx b/x-pack/legacy/plugins/siem/public/pages/hosts/details/index.tsx index 30744b3e24c4b2..d3a242b41da7b5 100644 --- a/x-pack/legacy/plugins/siem/public/pages/hosts/details/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/hosts/details/index.tsx @@ -12,7 +12,6 @@ import { connect } from 'react-redux'; import { StickyContainer } from 'react-sticky'; import { inputsSelectors, State } from '../../../store'; - import { FiltersGlobal } from '../../../components/filters_global'; import { HeaderPage } from '../../../components/header_page'; import { KpiHostDetailsQuery } from '../../../containers/kpi_host_details'; diff --git a/x-pack/legacy/plugins/siem/public/pages/hosts/details/types.ts b/x-pack/legacy/plugins/siem/public/pages/hosts/details/types.ts index 9df57970176eb4..4f3d34f51fb932 100644 --- a/x-pack/legacy/plugins/siem/public/pages/hosts/details/types.ts +++ b/x-pack/legacy/plugins/siem/public/pages/hosts/details/types.ts @@ -5,9 +5,8 @@ */ import { StaticIndexPattern } from 'ui/index_patterns'; -import { Filter } from '@kbn/es-query'; import { ActionCreator } from 'typescript-fsa'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { InputsModelId } from '../../../store/inputs/constants'; import { HostComponentProps } from '../../../components/link_to/redirect_to_hosts'; @@ -19,7 +18,7 @@ import { hostsModel } from '../../../store'; interface HostDetailsComponentReduxProps { query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; } interface HostBodyComponentDispatchProps { diff --git a/x-pack/legacy/plugins/siem/public/pages/hosts/types.ts b/x-pack/legacy/plugins/siem/public/pages/hosts/types.ts index 980c5535129aa5..afc577244f7e0e 100644 --- a/x-pack/legacy/plugins/siem/public/pages/hosts/types.ts +++ b/x-pack/legacy/plugins/siem/public/pages/hosts/types.ts @@ -6,8 +6,7 @@ import { StaticIndexPattern } from 'ui/index_patterns'; import { ActionCreator } from 'typescript-fsa'; -import { Filter } from '@kbn/es-query'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { SiemPageName } from '../home/types'; import { hostsModel } from '../../store'; @@ -19,7 +18,7 @@ export const hostDetailsPagePath = `${hostsPagePath}/:detailName`; export interface HostsComponentReduxProps { query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; } export interface HostsComponentDispatchProps { diff --git a/x-pack/legacy/plugins/siem/public/pages/network/ip_details/index.tsx b/x-pack/legacy/plugins/siem/public/pages/network/ip_details/index.tsx index 824539cad8d523..0fd4e073ebd13c 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/ip_details/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/network/ip_details/index.tsx @@ -38,6 +38,7 @@ export { getBreadcrumbs } from './utils'; import { TlsQueryTable } from './tls_query_table'; import { UsersQueryTable } from './users_query_table'; import { NetworkTopNFlowQueryTable } from './network_top_n_flow_query_table'; +import { NetworkHttpQueryTable } from './network_http_query_table'; import { NetworkTopCountriesQueryTable } from './network_top_countries_query_table'; import { useKibanaCore } from '../../../lib/compose/kibana_core'; @@ -221,6 +222,18 @@ export const IPDetailsComponent = React.memo( + + + + ( + + {({ + id, + inspect, + isInspected, + loading, + loadPage, + networkHttp, + pageInfo, + refetch, + totalCount, + }) => ( + + )} + +); + +NetworkHttpQueryTable.displayName = 'NetworkHttpQueryTable'; diff --git a/x-pack/legacy/plugins/siem/public/pages/network/ip_details/types.ts b/x-pack/legacy/plugins/siem/public/pages/network/ip_details/types.ts index 008409197f77d4..e0029d8d219ebf 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/ip_details/types.ts +++ b/x-pack/legacy/plugins/siem/public/pages/network/ip_details/types.ts @@ -4,10 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { StaticIndexPattern } from 'ui/index_patterns'; import { ActionCreator } from 'typescript-fsa'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/public'; import { NetworkType } from '../../../store/network/model'; import { ESTermQuery } from '../../../../common/typed_json'; @@ -25,7 +24,7 @@ type SetAbsoluteRangeDatePicker = ActionCreator<{ }>; interface IPDetailsComponentReduxProps { - filters: Filter[]; + filters: esFilters.Filter[]; flowTarget: FlowTarget; query: Query; } @@ -39,7 +38,7 @@ export type IPDetailsComponentProps = IPDetailsComponentReduxProps & IPDetailsComponentDispatchProps & GlobalTimeArgs & { detailName: string }; -interface OwnProps { +export interface OwnProps { type: NetworkType; startDate: number; endDate: number; diff --git a/x-pack/legacy/plugins/siem/public/pages/network/navigation/http_query_tab_body.tsx b/x-pack/legacy/plugins/siem/public/pages/network/navigation/http_query_tab_body.tsx new file mode 100644 index 00000000000000..a20a212623fb84 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/pages/network/navigation/http_query_tab_body.tsx @@ -0,0 +1,63 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import React from 'react'; +import { getOr } from 'lodash/fp'; + +import { NetworkHttpTable } from '../../../components/page/network'; +import { NetworkHttpQuery } from '../../../containers/network_http'; +import { networkModel } from '../../../store'; +import { manageQuery } from '../../../components/page/manage_query'; + +import { HttpQueryTabBodyProps } from './types'; + +const NetworkHttpTableManage = manageQuery(NetworkHttpTable); + +export const HttpQueryTabBody = ({ + to, + filterQuery, + isInitializing, + from, + setQuery, +}: HttpQueryTabBodyProps) => ( + + {({ + id, + inspect, + isInspected, + loading, + loadPage, + networkHttp, + pageInfo, + refetch, + totalCount, + }) => ( + + )} + +); + +HttpQueryTabBody.displayName = 'HttpQueryTabBody'; diff --git a/x-pack/legacy/plugins/siem/public/pages/network/navigation/nav_tabs.tsx b/x-pack/legacy/plugins/siem/public/pages/network/navigation/nav_tabs.tsx index ef097373b3ae7d..fbf137df398729 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/navigation/nav_tabs.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/network/navigation/nav_tabs.tsx @@ -26,6 +26,13 @@ export const navTabsNetwork = (hasMlUserPermissions: boolean): NetworkNavTab => disabled: false, urlKey: 'network', }, + [NetworkRouteType.http]: { + id: NetworkRouteType.http, + name: i18n.NAVIGATION_HTTP_TITLE, + href: getTabsOnNetworkUrl(NetworkRouteType.http), + disabled: false, + urlKey: 'network', + }, [NetworkRouteType.tls]: { id: NetworkRouteType.tls, name: i18n.NAVIGATION_TLS_TITLE, diff --git a/x-pack/legacy/plugins/siem/public/pages/network/navigation/network_routes.tsx b/x-pack/legacy/plugins/siem/public/pages/network/navigation/network_routes.tsx index fff9b1e7a09680..955670b4b098d0 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/navigation/network_routes.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/network/navigation/network_routes.tsx @@ -13,6 +13,7 @@ import { scoreIntervalToDateTime } from '../../../components/ml/score/score_inte import { IPsQueryTabBody } from './ips_query_tab_body'; import { CountriesQueryTabBody } from './countries_query_tab_body'; +import { HttpQueryTabBody } from './http_query_tab_body'; import { AnomaliesQueryTabBody } from './anomalies_query_tab_body'; import { DnsQueryTabBody } from './dns_query_tab_body'; import { ConditionalFlexGroup } from './conditional_flex_group'; @@ -96,6 +97,10 @@ export const NetworkRoutes = ({ )} /> + } + /> } diff --git a/x-pack/legacy/plugins/siem/public/pages/network/navigation/types.ts b/x-pack/legacy/plugins/siem/public/pages/network/navigation/types.ts index a3639da3cc1329..9682495b6f66a6 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/navigation/types.ts +++ b/x-pack/legacy/plugins/siem/public/pages/network/navigation/types.ts @@ -34,6 +34,10 @@ export type TlsQueryTabBodyProps = QueryTabBodyProps & ip?: string; }; +export type HttpQueryTabBodyProps = QueryTabBodyProps & + GlobalTimeArgs & { + ip?: string; + }; export type AnomaliesQueryTabBodyProps = QueryTabBodyProps & Pick & { narrowDateRange: NarrowDateRange; @@ -63,6 +67,7 @@ export enum NetworkRouteType { dns = 'dns', anomalies = 'anomalies', tls = 'tls', + http = 'http', } export type GetNetworkRoutePath = ( diff --git a/x-pack/legacy/plugins/siem/public/pages/network/navigation/utils.ts b/x-pack/legacy/plugins/siem/public/pages/network/navigation/utils.ts index a1cb9d61b9c479..059949bf518373 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/navigation/utils.ts +++ b/x-pack/legacy/plugins/siem/public/pages/network/navigation/utils.ts @@ -12,7 +12,7 @@ export const getNetworkRoutePath: GetNetworkRoutePath = ( hasMlUserPermission ) => { if (capabilitiesFetched && !hasMlUserPermission) { - return `${pagePath}/:tabName(${NetworkRouteType.flows}|${NetworkRouteType.dns}|${NetworkRouteType.tls})`; + return `${pagePath}/:tabName(${NetworkRouteType.flows}|${NetworkRouteType.dns}|${NetworkRouteType.http}|${NetworkRouteType.tls})`; } return ( @@ -20,6 +20,7 @@ export const getNetworkRoutePath: GetNetworkRoutePath = ( `${NetworkRouteType.flows}|` + `${NetworkRouteType.dns}|` + `${NetworkRouteType.anomalies}|` + + `${NetworkRouteType.http}|` + `${NetworkRouteType.tls})` ); }; diff --git a/x-pack/legacy/plugins/siem/public/pages/network/translations.ts b/x-pack/legacy/plugins/siem/public/pages/network/translations.ts index 25c117319248ab..be222bf5f2531f 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/translations.ts +++ b/x-pack/legacy/plugins/siem/public/pages/network/translations.ts @@ -39,6 +39,10 @@ export const NAVIGATION_TLS_TITLE = i18n.translate('xpack.siem.network.navigatio defaultMessage: 'TLS', }); +export const NAVIGATION_HTTP_TITLE = i18n.translate('xpack.siem.network.navigation.httpTitle', { + defaultMessage: 'HTTP', +}); + export const NAVIGATION_ANOMALIES_TITLE = i18n.translate( 'xpack.siem.network.navigation.anomaliesTitle', { diff --git a/x-pack/legacy/plugins/siem/public/pages/network/types.ts b/x-pack/legacy/plugins/siem/public/pages/network/types.ts index 46c868729b8328..e440d0c27e4676 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/types.ts +++ b/x-pack/legacy/plugins/siem/public/pages/network/types.ts @@ -4,10 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { RouteComponentProps } from 'react-router-dom'; import { ActionCreator } from 'typescript-fsa'; -import { Query } from 'src/plugins/data/common'; +import { Query, esFilters } from 'src/plugins/data/common'; import { GlobalTimeArgs } from '../../containers/global_time'; import { InputsModelId } from '../../store/inputs/constants'; @@ -19,7 +18,7 @@ export type SetAbsoluteRangeDatePicker = ActionCreator<{ }>; interface NetworkComponentReduxProps { - filters: Filter[]; + filters: esFilters.Filter[]; query: Query; setAbsoluteRangeDatePicker: SetAbsoluteRangeDatePicker; } diff --git a/x-pack/legacy/plugins/siem/public/store/inputs/actions.ts b/x-pack/legacy/plugins/siem/public/store/inputs/actions.ts index 598b2854b96ebd..aefcd2ea8c696c 100644 --- a/x-pack/legacy/plugins/siem/public/store/inputs/actions.ts +++ b/x-pack/legacy/plugins/siem/public/store/inputs/actions.ts @@ -4,12 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import actionCreatorFactory from 'typescript-fsa'; import { SavedQuery } from 'src/legacy/core_plugins/data/public'; import { InspectQuery, Refetch } from './model'; import { InputsModelId } from './constants'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; const actionCreator = actionCreatorFactory('x-pack/siem/local/inputs'); @@ -83,5 +83,5 @@ export const setSavedQuery = actionCreator<{ export const setSearchBarFilter = actionCreator<{ id: InputsModelId; - filters: Filter[]; + filters: esFilters.Filter[]; }>('SET_SEARCH_BAR_FILTER'); diff --git a/x-pack/legacy/plugins/siem/public/store/inputs/model.ts b/x-pack/legacy/plugins/siem/public/store/inputs/model.ts index a98ea1f5d08121..01cf386311d77f 100644 --- a/x-pack/legacy/plugins/siem/public/store/inputs/model.ts +++ b/x-pack/legacy/plugins/siem/public/store/inputs/model.ts @@ -4,13 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Filter } from '@kbn/es-query'; import { Dispatch } from 'redux'; import { Query } from 'src/plugins/data/common/query'; import { SavedQuery } from 'src/legacy/core_plugins/data/public'; import { Omit } from '../../../common/utility_types'; import { InputsModelId } from './constants'; import { CONSTANTS } from '../../components/url_state/constants'; +import { esFilters } from '../../../../../../../src/plugins/data/public'; export interface AbsoluteTimeRange { kind: 'absolute'; @@ -84,7 +84,7 @@ export interface InputsRange { queries: GlobalQuery[]; linkTo: InputsModelId[]; query: Query; - filters: Filter[]; + filters: esFilters.Filter[]; savedQuery?: SavedQuery; } diff --git a/x-pack/legacy/plugins/siem/public/store/network/helpers.test.ts b/x-pack/legacy/plugins/siem/public/store/network/helpers.test.ts index eb6a5c0c5c631e..a15e187b95e605 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/helpers.test.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/helpers.test.ts @@ -6,11 +6,11 @@ import { Direction, - NetworkTopTablesFields, + FlowTarget, NetworkDnsFields, + NetworkTopTablesFields, TlsFields, UsersFields, - FlowTarget, } from '../../graphql/types'; import { DEFAULT_TABLE_LIMIT } from '../constants'; import { NetworkModel, NetworkTableType, IpDetailsTableType, NetworkType } from './model'; @@ -68,6 +68,11 @@ export const mockNetworkState: NetworkModel = { direction: Direction.desc, }, }, + [NetworkTableType.http]: { + activePage: 0, + limit: DEFAULT_TABLE_LIMIT, + sort: { direction: Direction.desc }, + }, }, }, details: { @@ -120,6 +125,11 @@ export const mockNetworkState: NetworkModel = { direction: Direction.asc, }, }, + [IpDetailsTableType.http]: { + activePage: 0, + limit: DEFAULT_TABLE_LIMIT, + sort: { direction: Direction.desc }, + }, }, flowTarget: FlowTarget.source, }, @@ -145,6 +155,13 @@ describe('Network redux store', () => { sort: { field: 'uniqueDomains', direction: 'desc' }, isPtrIncluded: false, }, + [NetworkTableType.http]: { + activePage: 0, + limit: 10, + sort: { + direction: 'desc', + }, + }, [NetworkTableType.tls]: { activePage: 0, limit: 10, @@ -200,6 +217,13 @@ describe('Network redux store', () => { field: 'bytes_out', }, }, + [IpDetailsTableType.http]: { + activePage: 0, + limit: 10, + sort: { + direction: 'desc', + }, + }, [IpDetailsTableType.tls]: { activePage: 0, limit: 10, diff --git a/x-pack/legacy/plugins/siem/public/store/network/helpers.ts b/x-pack/legacy/plugins/siem/public/store/network/helpers.ts index c438fb6b492cea..0b3a5e65346b8f 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/helpers.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/helpers.ts @@ -40,6 +40,10 @@ export const setNetworkPageQueriesActivePageToZero = (state: NetworkModel): Netw ...state.page.queries[NetworkTableType.tls], activePage: DEFAULT_TABLE_ACTIVE_PAGE, }, + [NetworkTableType.http]: { + ...state.page.queries[NetworkTableType.http], + activePage: DEFAULT_TABLE_ACTIVE_PAGE, + }, }); export const setNetworkDetailsQueriesActivePageToZero = ( @@ -70,6 +74,10 @@ export const setNetworkDetailsQueriesActivePageToZero = ( ...state.details.queries[IpDetailsTableType.users], activePage: DEFAULT_TABLE_ACTIVE_PAGE, }, + [IpDetailsTableType.http]: { + ...state.details.queries[IpDetailsTableType.http], + activePage: DEFAULT_TABLE_ACTIVE_PAGE, + }, }); export const setNetworkQueriesActivePageToZero = ( diff --git a/x-pack/legacy/plugins/siem/public/store/network/model.ts b/x-pack/legacy/plugins/siem/public/store/network/model.ts index 1f82aa8a5319d3..45c49d65988811 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/model.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/model.ts @@ -7,6 +7,7 @@ import { FlowTarget, NetworkDnsSortField, + NetworkHttpSortField, NetworkTopTablesSortField, TlsSortField, UsersSortField, @@ -19,6 +20,7 @@ export enum NetworkType { export enum NetworkTableType { dns = 'dns', + http = 'http', topCountriesDestination = 'topCountriesDestination', topCountriesSource = 'topCountriesSource', topNFlowDestination = 'topNFlowDestination', @@ -40,7 +42,10 @@ export type TopCountriesTableType = export type TopTlsTableType = IpDetailsTableType.tls | NetworkTableType.tls; +export type HttpTableType = IpDetailsTableType.http | NetworkTableType.http; + export enum IpDetailsTableType { + http = 'http', tls = 'tls', topCountriesDestination = 'topCountriesDestination', topCountriesSource = 'topCountriesSource', @@ -74,15 +79,25 @@ export interface TlsQuery extends BasicQueryPaginated { sort: TlsSortField; } +export interface HttpQuery extends BasicQueryPaginated { + sort: NetworkHttpSortField; +} + export interface TableUpdates { activePage?: number; limit?: number; isPtrIncluded?: boolean; - sort?: NetworkDnsSortField | NetworkTopTablesSortField | TlsSortField | UsersSortField; + sort?: + | NetworkDnsSortField + | NetworkHttpSortField + | NetworkTopTablesSortField + | TlsSortField + | UsersSortField; } export interface NetworkQueries { [NetworkTableType.dns]: DnsQuery; + [NetworkTableType.http]: HttpQuery; [NetworkTableType.topCountriesDestination]: TopCountriesQuery; [NetworkTableType.topCountriesSource]: TopCountriesQuery; [NetworkTableType.topNFlowDestination]: TopNFlowQuery; @@ -99,6 +114,7 @@ export interface UsersQuery extends BasicQueryPaginated { } export interface IpOverviewQueries { + [IpDetailsTableType.http]: HttpQuery; [IpDetailsTableType.tls]: TlsQuery; [IpDetailsTableType.topCountriesDestination]: TopCountriesQuery; [IpDetailsTableType.topCountriesSource]: TopCountriesQuery; diff --git a/x-pack/legacy/plugins/siem/public/store/network/reducer.ts b/x-pack/legacy/plugins/siem/public/store/network/reducer.ts index 88adc118d51dca..373bedb63c3e79 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/reducer.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/reducer.ts @@ -58,6 +58,13 @@ export const initialNetworkState: NetworkState = { }, isPtrIncluded: false, }, + [NetworkTableType.http]: { + activePage: DEFAULT_TABLE_ACTIVE_PAGE, + limit: DEFAULT_TABLE_LIMIT, + sort: { + direction: Direction.desc, + }, + }, [NetworkTableType.tls]: { activePage: DEFAULT_TABLE_ACTIVE_PAGE, limit: DEFAULT_TABLE_LIMIT, @@ -86,6 +93,13 @@ export const initialNetworkState: NetworkState = { }, details: { queries: { + [IpDetailsTableType.http]: { + activePage: DEFAULT_TABLE_ACTIVE_PAGE, + limit: DEFAULT_TABLE_LIMIT, + sort: { + direction: Direction.desc, + }, + }, [IpDetailsTableType.topCountriesSource]: { activePage: DEFAULT_TABLE_ACTIVE_PAGE, limit: DEFAULT_TABLE_LIMIT, diff --git a/x-pack/legacy/plugins/siem/public/store/network/selectors.ts b/x-pack/legacy/plugins/siem/public/store/network/selectors.ts index 04cf8f0ea464c4..cf57c0d07c43e0 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/selectors.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/selectors.ts @@ -84,6 +84,21 @@ export const topCountriesSelector = () => topCountriesQueries => topCountriesQueries ); +const selectHttpByType = (state: State, networkType: NetworkType) => { + const httpType = + networkType === NetworkType.page ? NetworkTableType.http : IpDetailsTableType.http; + return ( + get([networkType, 'queries', httpType], state.network) || + get([networkType, 'queries', httpType], initialNetworkState) + ); +}; + +export const httpSelector = () => + createSelector( + selectHttpByType, + httpQueries => httpQueries + ); + // IP Details Selectors export const ipDetailsFlowTargetSelector = () => createSelector( diff --git a/x-pack/legacy/plugins/siem/server/graphql/network/resolvers.ts b/x-pack/legacy/plugins/siem/server/graphql/network/resolvers.ts index 7ce88ba4880be1..db15babc42a722 100644 --- a/x-pack/legacy/plugins/siem/server/graphql/network/resolvers.ts +++ b/x-pack/legacy/plugins/siem/server/graphql/network/resolvers.ts @@ -20,6 +20,11 @@ type QueryNetworkTopNFlowResolver = ChildResolverOf< QuerySourceResolver >; +type QueryNetworkHttpResolver = ChildResolverOf< + AppResolverOf, + QuerySourceResolver +>; + type QueryDnsResolver = ChildResolverOf< AppResolverOf, QuerySourceResolver @@ -33,6 +38,7 @@ export const createNetworkResolvers = ( libs: NetworkResolversDeps ): { Source: { + NetworkHttp: QueryNetworkHttpResolver; NetworkTopCountries: QueryNetworkTopCountriesResolver; NetworkTopNFlow: QueryNetworkTopNFlowResolver; NetworkDns: QueryDnsResolver; @@ -57,6 +63,14 @@ export const createNetworkResolvers = ( }; return libs.network.getNetworkTopNFlow(req, options); }, + async NetworkHttp(source, args, { req }, info) { + const options = { + ...createOptionsPaginated(source, args, info), + networkHttpSort: args.sort, + ip: args.ip, + }; + return libs.network.getNetworkHttp(req, options); + }, async NetworkDns(source, args, { req }, info) { const options = { ...createOptionsPaginated(source, args, info), diff --git a/x-pack/legacy/plugins/siem/server/graphql/network/schema.gql.ts b/x-pack/legacy/plugins/siem/server/graphql/network/schema.gql.ts index 36b57ec9368abf..84f8d004198e93 100644 --- a/x-pack/legacy/plugins/siem/server/graphql/network/schema.gql.ts +++ b/x-pack/legacy/plugins/siem/server/graphql/network/schema.gql.ts @@ -152,6 +152,43 @@ export const networkSchema = gql` inspect: Inspect } + enum NetworkHttpFields { + domains + lastHost + lastSourceIp + methods + path + requestCount + statuses + } + + input NetworkHttpSortField { + direction: Direction! + } + + type NetworkHttpItem { + _id: String + domains: [String!]! + lastHost: String + lastSourceIp: String + methods: [String!]! + path: String + requestCount: Float + statuses: [String!]! + } + + type NetworkHttpEdges { + node: NetworkHttpItem! + cursor: CursorType! + } + + type NetworkHttpData { + edges: [NetworkHttpEdges!]! + totalCount: Float! + pageInfo: PageInfoPaginated! + inspect: Inspect + } + extend type Source { NetworkTopCountries( id: String @@ -182,5 +219,14 @@ export const networkSchema = gql` timerange: TimerangeInput! defaultIndex: [String!]! ): NetworkDnsData! + NetworkHttp( + id: String + filterQuery: String + ip: String + pagination: PaginationInputPaginated! + sort: NetworkHttpSortField! + timerange: TimerangeInput! + defaultIndex: [String!]! + ): NetworkHttpData! } `; diff --git a/x-pack/legacy/plugins/siem/server/graphql/types.ts b/x-pack/legacy/plugins/siem/server/graphql/types.ts index 6de11e06528713..cf7ce3ad02fa18 100644 --- a/x-pack/legacy/plugins/siem/server/graphql/types.ts +++ b/x-pack/legacy/plugins/siem/server/graphql/types.ts @@ -87,6 +87,10 @@ export interface NetworkDnsSortField { direction: Direction; } +export interface NetworkHttpSortField { + direction: Direction; +} + export interface TlsSortField { field: TlsFields; @@ -296,6 +300,16 @@ export enum NetworkDirectionEcs { unknown = 'unknown', } +export enum NetworkHttpFields { + domains = 'domains', + lastHost = 'lastHost', + lastSourceIp = 'lastSourceIp', + methods = 'methods', + path = 'path', + requestCount = 'requestCount', + statuses = 'statuses', +} + export enum FlowDirection { uniDirectional = 'uniDirectional', biDirectional = 'biDirectional', @@ -435,6 +449,8 @@ export interface Source { NetworkDns: NetworkDnsData; + NetworkHttp: NetworkHttpData; + OverviewNetwork?: Maybe; OverviewHost?: Maybe; @@ -1590,6 +1606,40 @@ export interface NetworkDnsItem { uniqueDomains?: Maybe; } +export interface NetworkHttpData { + edges: NetworkHttpEdges[]; + + totalCount: number; + + pageInfo: PageInfoPaginated; + + inspect?: Maybe; +} + +export interface NetworkHttpEdges { + node: NetworkHttpItem; + + cursor: CursorType; +} + +export interface NetworkHttpItem { + _id?: Maybe; + + domains: string[]; + + lastHost?: Maybe; + + lastSourceIp?: Maybe; + + methods: string[]; + + path?: Maybe; + + requestCount?: Maybe; + + statuses: string[]; +} + export interface OverviewNetworkData { auditbeatSocket?: Maybe; @@ -2164,6 +2214,21 @@ export interface NetworkDnsSourceArgs { defaultIndex: string[]; } +export interface NetworkHttpSourceArgs { + id?: Maybe; + + filterQuery?: Maybe; + + ip?: Maybe; + + pagination: PaginationInputPaginated; + + sort: NetworkHttpSortField; + + timerange: TimerangeInput; + + defaultIndex: string[]; +} export interface OverviewNetworkSourceArgs { id?: Maybe; @@ -2643,6 +2708,8 @@ export namespace SourceResolvers { NetworkDns?: NetworkDnsResolver; + NetworkHttp?: NetworkHttpResolver; + OverviewNetwork?: OverviewNetworkResolver, TypeParent, TContext>; OverviewHost?: OverviewHostResolver, TypeParent, TContext>; @@ -2956,6 +3023,27 @@ export namespace SourceResolvers { defaultIndex: string[]; } + export type NetworkHttpResolver< + R = NetworkHttpData, + Parent = Source, + TContext = SiemContext + > = Resolver; + export interface NetworkHttpArgs { + id?: Maybe; + + filterQuery?: Maybe; + + ip?: Maybe; + + pagination: PaginationInputPaginated; + + sort: NetworkHttpSortField; + + timerange: TimerangeInput; + + defaultIndex: string[]; + } + export type OverviewNetworkResolver< R = Maybe, Parent = Source, @@ -6863,6 +6951,119 @@ export namespace NetworkDnsItemResolvers { > = Resolver; } +export namespace NetworkHttpDataResolvers { + export interface Resolvers { + edges?: EdgesResolver; + + totalCount?: TotalCountResolver; + + pageInfo?: PageInfoResolver; + + inspect?: InspectResolver, TypeParent, TContext>; + } + + export type EdgesResolver< + R = NetworkHttpEdges[], + Parent = NetworkHttpData, + TContext = SiemContext + > = Resolver; + export type TotalCountResolver< + R = number, + Parent = NetworkHttpData, + TContext = SiemContext + > = Resolver; + export type PageInfoResolver< + R = PageInfoPaginated, + Parent = NetworkHttpData, + TContext = SiemContext + > = Resolver; + export type InspectResolver< + R = Maybe, + Parent = NetworkHttpData, + TContext = SiemContext + > = Resolver; +} + +export namespace NetworkHttpEdgesResolvers { + export interface Resolvers { + node?: NodeResolver; + + cursor?: CursorResolver; + } + + export type NodeResolver< + R = NetworkHttpItem, + Parent = NetworkHttpEdges, + TContext = SiemContext + > = Resolver; + export type CursorResolver< + R = CursorType, + Parent = NetworkHttpEdges, + TContext = SiemContext + > = Resolver; +} + +export namespace NetworkHttpItemResolvers { + export interface Resolvers { + _id?: _IdResolver, TypeParent, TContext>; + + domains?: DomainsResolver; + + lastHost?: LastHostResolver, TypeParent, TContext>; + + lastSourceIp?: LastSourceIpResolver, TypeParent, TContext>; + + methods?: MethodsResolver; + + path?: PathResolver, TypeParent, TContext>; + + requestCount?: RequestCountResolver, TypeParent, TContext>; + + statuses?: StatusesResolver; + } + + export type _IdResolver< + R = Maybe, + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type DomainsResolver< + R = string[], + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type LastHostResolver< + R = Maybe, + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type LastSourceIpResolver< + R = Maybe, + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type MethodsResolver< + R = string[], + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type PathResolver< + R = Maybe, + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type RequestCountResolver< + R = Maybe, + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; + export type StatusesResolver< + R = string[], + Parent = NetworkHttpItem, + TContext = SiemContext + > = Resolver; +} + export namespace OverviewNetworkDataResolvers { export interface Resolvers { auditbeatSocket?: AuditbeatSocketResolver, TypeParent, TContext>; @@ -8265,6 +8466,9 @@ export type IResolvers = { NetworkDnsData?: NetworkDnsDataResolvers.Resolvers; NetworkDnsEdges?: NetworkDnsEdgesResolvers.Resolvers; NetworkDnsItem?: NetworkDnsItemResolvers.Resolvers; + NetworkHttpData?: NetworkHttpDataResolvers.Resolvers; + NetworkHttpEdges?: NetworkHttpEdgesResolvers.Resolvers; + NetworkHttpItem?: NetworkHttpItemResolvers.Resolvers; OverviewNetworkData?: OverviewNetworkDataResolvers.Resolvers; OverviewHostData?: OverviewHostDataResolvers.Resolvers; TlsData?: TlsDataResolvers.Resolvers; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/alerts/types.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/alerts/types.ts index 7db2db5538dbf3..b8d7af5c453033 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/alerts/types.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/alerts/types.ts @@ -7,7 +7,7 @@ import { get } from 'lodash/fp'; import Hapi from 'hapi'; -import { Filter } from '@kbn/es-query'; +import { esFilters } from '../../../../../../../../src/plugins/data/common'; import { SIGNALS_ID } from '../../../../common/constants'; import { Alert, @@ -19,7 +19,7 @@ import { AlertsClient } from '../../../../../alerting/server/alerts_client'; import { ActionsClient } from '../../../../../actions/server/actions_client'; import { SearchResponse } from '../../types'; -export type PartialFilter = Partial; +export type PartialFilter = Partial; export interface SignalAlertParams { description: string; diff --git a/x-pack/legacy/plugins/siem/server/lib/network/elasticsearch_adapter.ts b/x-pack/legacy/plugins/siem/server/lib/network/elasticsearch_adapter.ts index eff5fba0c54d52..39babc58ee138c 100644 --- a/x-pack/legacy/plugins/siem/server/lib/network/elasticsearch_adapter.ts +++ b/x-pack/legacy/plugins/siem/server/lib/network/elasticsearch_adapter.ts @@ -15,6 +15,8 @@ import { NetworkTopCountriesData, NetworkTopCountriesEdges, NetworkTopNFlowData, + NetworkHttpData, + NetworkHttpEdges, NetworkTopNFlowEdges, } from '../../graphql/types'; import { inspectStringifyObject } from '../../utils/build_query'; @@ -25,15 +27,18 @@ import { DEFAULT_MAX_TABLE_QUERY_SIZE } from '../../../common/constants'; import { NetworkDnsRequestOptions, NetworkTopCountriesRequestOptions, + NetworkHttpRequestOptions, NetworkTopNFlowRequestOptions, } from './index'; import { buildDnsQuery } from './query_dns.dsl'; import { buildTopNFlowQuery, getOppositeField } from './query_top_n_flow.dsl'; +import { buildHttpQuery } from './query_http.dsl'; import { buildTopCountriesQuery } from './query_top_countries.dsl'; import { NetworkAdapter, NetworkDnsBuckets, NetworkTopCountriesBuckets, + NetworkHttpBuckets, NetworkTopNFlowBuckets, } from './types'; @@ -151,6 +156,42 @@ export class ElasticsearchNetworkAdapter implements NetworkAdapter { totalCount, }; } + + public async getNetworkHttp( + request: FrameworkRequest, + options: NetworkHttpRequestOptions + ): Promise { + if (options.pagination && options.pagination.querySize >= DEFAULT_MAX_TABLE_QUERY_SIZE) { + throw new Error(`No query size above ${DEFAULT_MAX_TABLE_QUERY_SIZE}`); + } + const dsl = buildHttpQuery(options); + const response = await this.framework.callWithRequest( + request, + 'search', + dsl + ); + const { activePage, cursorStart, fakePossibleCount, querySize } = options.pagination; + const totalCount = getOr(0, 'aggregations.http_count.value', response); + const networkHttpEdges: NetworkHttpEdges[] = getHttpEdges(response); + const fakeTotalCount = fakePossibleCount <= totalCount ? fakePossibleCount : totalCount; + const edges = networkHttpEdges.splice(cursorStart, querySize - cursorStart); + const inspect = { + dsl: [inspectStringifyObject(dsl)], + response: [inspectStringifyObject(response)], + }; + const showMorePagesIndicator = totalCount > fakeTotalCount; + + return { + edges, + inspect, + pageInfo: { + activePage: activePage ? activePage : 0, + fakeTotalCount, + showMorePagesIndicator, + }, + totalCount, + }; + } } const getTopNFlowEdges = ( @@ -173,6 +214,12 @@ const getTopCountriesEdges = ( ); }; +const getHttpEdges = ( + response: DatabaseSearchResponse +): NetworkHttpEdges[] => { + return formatHttpEdges(getOr([], `aggregations.url.buckets`, response)); +}; + const getFlowTargetFromString = (flowAsString: string) => flowAsString === 'source' ? FlowTargetSourceDest.source : FlowTargetSourceDest.destination; @@ -287,6 +334,24 @@ const formatDnsEdges = (buckets: NetworkDnsBuckets[]): NetworkDnsEdges[] => }, })); +const formatHttpEdges = (buckets: NetworkHttpBuckets[]): NetworkHttpEdges[] => + buckets.map((bucket: NetworkHttpBuckets) => ({ + node: { + _id: bucket.key, + domains: bucket.domains.buckets.map(({ key }) => key), + methods: bucket.methods.buckets.map(({ key }) => key), + statuses: bucket.status.buckets.map(({ key }) => `${key}`), + lastHost: get('source.hits.hits[0]._source.host.name', bucket), + lastSourceIp: get('source.hits.hits[0]._source.source.ip', bucket), + path: bucket.key, + requestCount: bucket.doc_count, + }, + cursor: { + value: bucket.key, + tiebreaker: null, + }, + })); + const getOrNumber = (path: string, bucket: NetworkTopNFlowBuckets | NetworkDnsBuckets) => { const numb = get(path, bucket); if (numb == null) { diff --git a/x-pack/legacy/plugins/siem/server/lib/network/index.ts b/x-pack/legacy/plugins/siem/server/lib/network/index.ts index e391dd922ce31c..0a2de936799b60 100644 --- a/x-pack/legacy/plugins/siem/server/lib/network/index.ts +++ b/x-pack/legacy/plugins/siem/server/lib/network/index.ts @@ -7,7 +7,10 @@ import { FlowTargetSourceDest, Maybe, + NetworkDnsData, NetworkDnsSortField, + NetworkHttpData, + NetworkHttpSortField, NetworkTopCountriesData, NetworkTopNFlowData, NetworkTopTablesSortField, @@ -30,6 +33,11 @@ export interface NetworkTopCountriesRequestOptions extends RequestOptionsPaginat ip?: Maybe; } +export interface NetworkHttpRequestOptions extends RequestOptionsPaginated { + networkHttpSort: NetworkHttpSortField; + ip?: Maybe; +} + export interface NetworkDnsRequestOptions extends RequestOptionsPaginated { isPtrIncluded: boolean; networkDnsSortField: NetworkDnsSortField; @@ -55,7 +63,14 @@ export class Network { public async getNetworkDns( req: FrameworkRequest, options: NetworkDnsRequestOptions - ): Promise { + ): Promise { return this.adapter.getNetworkDns(req, options); } + + public async getNetworkHttp( + req: FrameworkRequest, + options: NetworkHttpRequestOptions + ): Promise { + return this.adapter.getNetworkHttp(req, options); + } } diff --git a/x-pack/legacy/plugins/siem/server/lib/network/query_http.dsl.ts b/x-pack/legacy/plugins/siem/server/lib/network/query_http.dsl.ts new file mode 100644 index 00000000000000..3e33b5af80a855 --- /dev/null +++ b/x-pack/legacy/plugins/siem/server/lib/network/query_http.dsl.ts @@ -0,0 +1,112 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +import { NetworkHttpSortField } from '../../graphql/types'; +import { createQueryFilterClauses } from '../../utils/build_query'; + +import { NetworkHttpRequestOptions } from './index'; + +const getCountAgg = () => ({ + http_count: { + cardinality: { + field: 'url.path', + }, + }, +}); + +export const buildHttpQuery = ({ + defaultIndex, + filterQuery, + networkHttpSort, + pagination: { querySize }, + sourceConfiguration: { + fields: { timestamp }, + }, + timerange: { from, to }, + ip, +}: NetworkHttpRequestOptions) => { + const filter = [ + ...createQueryFilterClauses(filterQuery), + { range: { [timestamp]: { gte: from, lte: to } } }, + { exists: { field: 'http.request.method' } }, + ]; + + const dslQuery = { + allowNoIndices: true, + index: defaultIndex, + ignoreUnavailable: true, + body: { + aggregations: { + ...getCountAgg(), + ...getHttpAggs(networkHttpSort, querySize), + }, + query: { + bool: ip + ? { + filter, + should: [ + { + term: { + 'source.ip': ip, + }, + }, + { + term: { + 'destination.ip': ip, + }, + }, + ], + minimum_should_match: 1, + } + : { + filter, + }, + }, + }, + size: 0, + track_total_hits: false, + }; + return dslQuery; +}; + +const getHttpAggs = (networkHttpSortField: NetworkHttpSortField, querySize: number) => ({ + url: { + terms: { + field: `url.path`, + size: querySize, + order: { + _count: networkHttpSortField.direction, + }, + }, + aggs: { + methods: { + terms: { + field: 'http.request.method', + size: 4, + }, + }, + domains: { + terms: { + field: 'url.domain', + size: 4, + }, + }, + status: { + terms: { + field: 'http.response.status_code', + size: 4, + }, + }, + source: { + top_hits: { + size: 1, + _source: { + includes: ['host.name', 'source.ip'], + }, + }, + }, + }, + }, +}); diff --git a/x-pack/legacy/plugins/siem/server/lib/network/types.ts b/x-pack/legacy/plugins/siem/server/lib/network/types.ts index e2e443fb502591..6afcb211b59961 100644 --- a/x-pack/legacy/plugins/siem/server/lib/network/types.ts +++ b/x-pack/legacy/plugins/siem/server/lib/network/types.ts @@ -4,7 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { NetworkTopCountriesData, NetworkDnsData, NetworkTopNFlowData } from '../../graphql/types'; +import { + NetworkDnsData, + NetworkHttpData, + NetworkTopCountriesData, + NetworkTopNFlowData, +} from '../../graphql/types'; import { FrameworkRequest, RequestOptionsPaginated } from '../framework'; import { TotalValue } from '../types'; @@ -18,6 +23,7 @@ export interface NetworkAdapter { options: RequestOptionsPaginated ): Promise; getNetworkDns(req: FrameworkRequest, options: RequestOptionsPaginated): Promise; + getNetworkHttp(req: FrameworkRequest, options: RequestOptionsPaginated): Promise; } export interface GenericBuckets { @@ -61,6 +67,21 @@ interface AutonomousSystemHit { }; } +interface HttpHit { + hits: { + total: TotalValue | number; + max_score: number | null; + hits: Array<{ + _source: T; + sort?: [number]; + _index?: string; + _type?: string; + _id?: string; + _score?: number | null; + }>; + }; +} + export interface NetworkTopNFlowBuckets { key: string; autonomous_system: AutonomousSystemHit; @@ -106,3 +127,18 @@ export interface NetworkDnsBuckets { value: number; }; } + +export interface NetworkHttpBuckets { + key: string; + doc_count: number; + domains: { + buckets: GenericBuckets[]; + }; + methods: { + buckets: GenericBuckets[]; + }; + source: HttpHit; + status: { + buckets: GenericBuckets[]; + }; +} diff --git a/x-pack/legacy/plugins/task_manager/lib/fill_pool.test.ts b/x-pack/legacy/plugins/task_manager/lib/fill_pool.test.ts index 860a637d581088..d7ac8d227fc4c0 100644 --- a/x-pack/legacy/plugins/task_manager/lib/fill_pool.test.ts +++ b/x-pack/legacy/plugins/task_manager/lib/fill_pool.test.ts @@ -7,13 +7,14 @@ import _ from 'lodash'; import sinon from 'sinon'; import { fillPool } from './fill_pool'; +import { TaskPoolRunResult } from '../task_pool'; describe('fillPool', () => { test('stops filling when there are no more tasks in the store', async () => { const tasks = [[1, 2, 3], [4, 5]]; let index = 0; const fetchAvailableTasks = async () => tasks[index++] || []; - const run = sinon.spy(async () => true); + const run = sinon.spy(async () => TaskPoolRunResult.RunningAllClaimedTasks); const converter = _.identity; await fillPool(run, fetchAvailableTasks, converter); @@ -25,7 +26,7 @@ describe('fillPool', () => { const tasks = [[1, 2, 3], [4, 5]]; let index = 0; const fetchAvailableTasks = async () => tasks[index++] || []; - const run = sinon.spy(async () => false); + const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity); const converter = _.identity; await fillPool(run, fetchAvailableTasks, converter); @@ -37,7 +38,7 @@ describe('fillPool', () => { const tasks = [[1, 2, 3], [4, 5]]; let index = 0; const fetchAvailableTasks = async () => tasks[index++] || []; - const run = sinon.spy(async () => false); + const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity); const converter = (x: number) => x.toString(); await fillPool(run, fetchAvailableTasks, converter); @@ -47,7 +48,7 @@ describe('fillPool', () => { describe('error handling', () => { test('throws exception from fetchAvailableTasks', async () => { - const run = sinon.spy(async () => false); + const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity); const converter = (x: number) => x.toString(); try { @@ -80,7 +81,7 @@ describe('fillPool', () => { const tasks = [[1, 2, 3], [4, 5]]; let index = 0; const fetchAvailableTasks = async () => tasks[index++] || []; - const run = sinon.spy(async () => false); + const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity); const converter = (x: number) => { throw new Error(`can not convert ${x}`); }; diff --git a/x-pack/legacy/plugins/task_manager/lib/fill_pool.ts b/x-pack/legacy/plugins/task_manager/lib/fill_pool.ts index a5970574abaaf4..6fe965e048ea58 100644 --- a/x-pack/legacy/plugins/task_manager/lib/fill_pool.ts +++ b/x-pack/legacy/plugins/task_manager/lib/fill_pool.ts @@ -4,7 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -type BatchRun = (tasks: T[]) => Promise; +import { performance } from 'perf_hooks'; +import { TaskPoolRunResult } from '../task_pool'; + +export enum FillPoolResult { + NoTasksClaimed = 'NoTasksClaimed', + RanOutOfCapacity = 'RanOutOfCapacity', +} + +type BatchRun = (tasks: T[]) => Promise; type Fetcher = () => Promise; type Converter = (t: T1) => T2; @@ -24,18 +32,32 @@ export async function fillPool( run: BatchRun, fetchAvailableTasks: Fetcher, converter: Converter -): Promise { +): Promise { + performance.mark('fillPool.start'); while (true) { const instances = await fetchAvailableTasks(); if (!instances.length) { - return; + performance.mark('fillPool.bailNoTasks'); + performance.measure( + 'fillPool.activityDurationUntilNoTasks', + 'fillPool.start', + 'fillPool.bailNoTasks' + ); + return FillPoolResult.NoTasksClaimed; } const tasks = instances.map(converter); - if (!(await run(tasks))) { - return; + if ((await run(tasks)) === TaskPoolRunResult.RanOutOfCapacity) { + performance.mark('fillPool.bailExhaustedCapacity'); + performance.measure( + 'fillPool.activityDurationUntilExhaustedCapacity', + 'fillPool.start', + 'fillPool.bailExhaustedCapacity' + ); + return FillPoolResult.RanOutOfCapacity; } + performance.mark('fillPool.cycle'); } } diff --git a/x-pack/legacy/plugins/task_manager/lib/middleware.test.ts b/x-pack/legacy/plugins/task_manager/lib/middleware.test.ts index 07afee17974626..699765f16d83e5 100644 --- a/x-pack/legacy/plugins/task_manager/lib/middleware.test.ts +++ b/x-pack/legacy/plugins/task_manager/lib/middleware.test.ts @@ -70,6 +70,7 @@ describe('addMiddlewareToChain', () => { return opts; }, beforeRun: defaultBeforeRun, + beforeMarkRunning: defaultBeforeRun, }; const m2 = { beforeSave: async (opts: BeforeSaveOpts) => { @@ -77,6 +78,7 @@ describe('addMiddlewareToChain', () => { return opts; }, beforeRun: defaultBeforeRun, + beforeMarkRunning: defaultBeforeRun, }; const m3 = { beforeSave: async (opts: BeforeSaveOpts) => { @@ -84,6 +86,7 @@ describe('addMiddlewareToChain', () => { return opts; }, beforeRun: defaultBeforeRun, + beforeMarkRunning: defaultBeforeRun, }; let middlewareChain; @@ -119,6 +122,7 @@ describe('addMiddlewareToChain', () => { m1: true, }; }, + beforeMarkRunning: defaultBeforeRun, }; const m2 = { beforeSave: defaultBeforeSave, @@ -128,6 +132,7 @@ describe('addMiddlewareToChain', () => { m2: true, }; }, + beforeMarkRunning: defaultBeforeRun, }; const m3 = { beforeSave: defaultBeforeSave, @@ -137,6 +142,7 @@ describe('addMiddlewareToChain', () => { m3: true, }; }, + beforeMarkRunning: defaultBeforeRun, }; let middlewareChain; diff --git a/x-pack/legacy/plugins/task_manager/lib/middleware.ts b/x-pack/legacy/plugins/task_manager/lib/middleware.ts index d81b76fda7e50e..d367c8ca56c090 100644 --- a/x-pack/legacy/plugins/task_manager/lib/middleware.ts +++ b/x-pack/legacy/plugins/task_manager/lib/middleware.ts @@ -23,10 +23,12 @@ export type BeforeSaveFunction = ( ) => Promise; export type BeforeRunFunction = (params: RunContext) => Promise; +export type BeforeMarkRunningFunction = (params: RunContext) => Promise; export interface Middleware { beforeSave: BeforeSaveFunction; beforeRun: BeforeRunFunction; + beforeMarkRunning: BeforeMarkRunningFunction; } export function addMiddlewareToChain(prevMiddleware: Middleware, middleware: Middleware) { @@ -39,8 +41,14 @@ export function addMiddlewareToChain(prevMiddleware: Middleware, middleware: Mid ? (params: RunContext) => middleware.beforeRun(params).then(prevMiddleware.beforeRun) : prevMiddleware.beforeRun; + const beforeMarkRunning = middleware.beforeMarkRunning + ? (params: RunContext) => + middleware.beforeMarkRunning(params).then(prevMiddleware.beforeMarkRunning) + : prevMiddleware.beforeMarkRunning; + return { beforeSave, beforeRun, + beforeMarkRunning, }; } diff --git a/x-pack/legacy/plugins/task_manager/task_manager.test.ts b/x-pack/legacy/plugins/task_manager/task_manager.test.ts index 3961dcafffdca6..9ae2f5e1e027b5 100644 --- a/x-pack/legacy/plugins/task_manager/task_manager.test.ts +++ b/x-pack/legacy/plugins/task_manager/task_manager.test.ts @@ -174,6 +174,7 @@ describe('TaskManager', () => { const middleware = { beforeSave: async (saveOpts: any) => saveOpts, beforeRun: async (runOpts: any) => runOpts, + beforeMarkRunning: async (runOpts: any) => runOpts, }; expect(() => client.addMiddleware(middleware)).not.toThrow(); }); @@ -183,6 +184,7 @@ describe('TaskManager', () => { const middleware = { beforeSave: async (saveOpts: any) => saveOpts, beforeRun: async (runOpts: any) => runOpts, + beforeMarkRunning: async (runOpts: any) => runOpts, }; client.start(); @@ -241,7 +243,10 @@ describe('TaskManager', () => { claimAvailableTasks(claim, 10, logger); - sinon.assert.calledWithMatch(logger.warn, /inline scripts/); + expect(logger.warn).toHaveBeenCalledTimes(1); + expect(logger.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"Task Manager cannot operate when inline scripts are disabled in Elasticsearch"` + ); }); }); }); diff --git a/x-pack/legacy/plugins/task_manager/task_manager.ts b/x-pack/legacy/plugins/task_manager/task_manager.ts index 219c525aea23f8..4ddb18c7cfe748 100644 --- a/x-pack/legacy/plugins/task_manager/task_manager.ts +++ b/x-pack/legacy/plugins/task_manager/task_manager.ts @@ -3,10 +3,10 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ - +import { performance } from 'perf_hooks'; import { SavedObjectsClientContract, SavedObjectsSerializer } from 'src/core/server'; import { Logger } from './types'; -import { fillPool } from './lib/fill_pool'; +import { fillPool, FillPoolResult } from './lib/fill_pool'; import { addMiddlewareToChain, BeforeSaveMiddlewareParams, Middleware } from './lib/middleware'; import { sanitizeTaskDefinitions } from './lib/sanitize_task_definitions'; import { intervalFromNow } from './lib/intervals'; @@ -56,13 +56,14 @@ export class TaskManager { private readonly pollerInterval: number; private definitions: TaskDictionary; private store: TaskStore; - private poller: TaskPoller; + private poller: TaskPoller; private logger: Logger; private pool: TaskPool; private startQueue: Array<() => void> = []; private middleware = { beforeSave: async (saveOpts: BeforeSaveMiddlewareParams) => saveOpts, beforeRun: async (runOpts: RunContext) => runOpts, + beforeMarkRunning: async (runOpts: RunContext) => runOpts, }; /** @@ -86,8 +87,6 @@ export class TaskManager { this.logger.info(`TaskManager is identified by the Kibana UUID: ${taskManagerId}`); } - /* Kibana UUID needs to be pulled live (not cached), as it takes a long time - * to initialize, and can change after startup */ const store = new TaskStore({ serializer: opts.serializer, savedObjectsRepository: opts.savedObjectsRepository, @@ -109,13 +108,14 @@ export class TaskManager { store, definitions: this.definitions, beforeRun: this.middleware.beforeRun, + beforeMarkRunning: this.middleware.beforeMarkRunning, }); - const poller = new TaskPoller({ + const poller = new TaskPoller({ logger: this.logger, pollInterval: opts.config.get('xpack.task_manager.poll_interval'), - work: (): Promise => + work: (): Promise => fillPool( - pool.run, + async tasks => await pool.run(tasks), () => claimAvailableTasks( this.store.claimAvailableTasks.bind(this.store), @@ -260,12 +260,24 @@ export async function claimAvailableTasks( logger: Logger ) { if (availableWorkers > 0) { + performance.mark('claimAvailableTasks_start'); + try { const { docs, claimedTasks } = await claim({ size: availableWorkers, claimOwnershipUntil: intervalFromNow('30s')!, }); + if (claimedTasks === 0) { + performance.mark('claimAvailableTasks.noTasks'); + } + performance.mark('claimAvailableTasks_stop'); + performance.measure( + 'claimAvailableTasks', + 'claimAvailableTasks_start', + 'claimAvailableTasks_stop' + ); + if (docs.length !== claimedTasks) { logger.warn( `[Task Ownership error]: (${claimedTasks}) tasks were claimed by Kibana, but (${docs.length}) tasks were fetched` @@ -282,6 +294,7 @@ export async function claimAvailableTasks( } } } else { + performance.mark('claimAvailableTasks.noAvailableWorkers'); logger.info( `[Task Ownership]: Task Manager has skipped Claiming Ownership of available tasks at it has ran out Available Workers. If this happens often, consider adjusting the "xpack.task_manager.max_workers" configuration.` ); diff --git a/x-pack/legacy/plugins/task_manager/task_poller.test.ts b/x-pack/legacy/plugins/task_manager/task_poller.test.ts index 478c1a4dc1b17c..88bcf29ec60849 100644 --- a/x-pack/legacy/plugins/task_manager/task_poller.test.ts +++ b/x-pack/legacy/plugins/task_manager/task_poller.test.ts @@ -73,7 +73,9 @@ describe('TaskPoller', () => { await doneWorking; expect(count).toEqual(2); - sinon.assert.calledWithMatch(logger.error, /Dang it/i); + expect(logger.error.mock.calls[0][0]).toMatchInlineSnapshot( + `"Failed to poll for work: Error: Dang it!"` + ); }); test('is stoppable', async () => { diff --git a/x-pack/legacy/plugins/task_manager/task_poller.ts b/x-pack/legacy/plugins/task_manager/task_poller.ts index ae023f96a40647..0f7b49f17872ad 100644 --- a/x-pack/legacy/plugins/task_manager/task_poller.ts +++ b/x-pack/legacy/plugins/task_manager/task_poller.ts @@ -8,27 +8,28 @@ * This module contains the logic for polling the task manager index for new work. */ +import { performance } from 'perf_hooks'; import { Logger } from './types'; -type WorkFn = () => Promise; +type WorkFn = () => Promise; -interface Opts { +interface Opts { pollInterval: number; logger: Logger; - work: WorkFn; + work: WorkFn; } /** * Performs work on a scheduled interval, logging any errors. This waits for work to complete * (or error) prior to attempting another run. */ -export class TaskPoller { +export class TaskPoller { private isStarted = false; private isWorking = false; private timeout: any; private pollInterval: number; private logger: Logger; - private work: WorkFn; + private work: WorkFn; /** * Constructs a new TaskPoller. @@ -38,7 +39,7 @@ export class TaskPoller { * @prop {Logger} logger - The task manager logger * @prop {WorkFn} work - An empty, asynchronous function that performs the desired work */ - constructor(opts: Opts) { + constructor(opts: Opts) { this.pollInterval = opts.pollInterval; this.logger = opts.logger; this.work = opts.work; @@ -57,8 +58,16 @@ export class TaskPoller { const poll = async () => { await this.attemptWork(); + performance.mark('TaskPoller.sleep'); if (this.isStarted) { - this.timeout = setTimeout(poll, this.pollInterval); + this.timeout = setTimeout( + tryAndLogOnError(() => { + performance.mark('TaskPoller.poll'); + performance.measure('TaskPoller.sleepDuration', 'TaskPoller.sleep', 'TaskPoller.poll'); + poll(); + }, this.logger), + this.pollInterval + ); } }; @@ -94,3 +103,13 @@ export class TaskPoller { } } } + +function tryAndLogOnError(fn: Function, logger: Logger): Function { + return () => { + try { + fn(); + } catch (err) { + logger.error(`Task Poller polling phase failed: ${err}`); + } + }; +} diff --git a/x-pack/legacy/plugins/task_manager/task_pool.test.ts b/x-pack/legacy/plugins/task_manager/task_pool.test.ts index e6a83dd1911bd1..4967f4383294fc 100644 --- a/x-pack/legacy/plugins/task_manager/task_pool.test.ts +++ b/x-pack/legacy/plugins/task_manager/task_pool.test.ts @@ -5,7 +5,7 @@ */ import sinon from 'sinon'; -import { TaskPool } from './task_pool'; +import { TaskPool, TaskPoolRunResult } from './task_pool'; import { mockLogger, resolvable, sleep } from './test_utils'; describe('TaskPool', () => { @@ -17,7 +17,7 @@ describe('TaskPool', () => { const result = await pool.run([{ ...mockTask() }, { ...mockTask() }, { ...mockTask() }]); - expect(result).toBeTruthy(); + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); expect(pool.occupiedWorkers).toEqual(3); }); @@ -29,7 +29,7 @@ describe('TaskPool', () => { const result = await pool.run([{ ...mockTask() }, { ...mockTask() }, { ...mockTask() }]); - expect(result).toBeTruthy(); + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); expect(pool.availableWorkers).toEqual(7); }); @@ -48,10 +48,74 @@ describe('TaskPool', () => { { ...mockTask(), run: shouldNotRun }, ]); - expect(result).toBeFalsy(); + expect(result).toEqual(TaskPoolRunResult.RanOutOfCapacity); expect(pool.availableWorkers).toEqual(0); - sinon.assert.calledTwice(shouldRun); - sinon.assert.notCalled(shouldNotRun); + expect(shouldRun).toHaveBeenCalledTimes(2); + expect(shouldNotRun).not.toHaveBeenCalled(); + }); + + test('should log when marking a Task as running fails', async () => { + const logger = mockLogger(); + const pool = new TaskPool({ + maxWorkers: 2, + logger, + }); + + const taskFailedToMarkAsRunning = mockTask(); + taskFailedToMarkAsRunning.markTaskAsRunning.mockImplementation(async () => { + throw new Error(`Mark Task as running has failed miserably`); + }); + + const result = await pool.run([mockTask(), taskFailedToMarkAsRunning, mockTask()]); + + expect(logger.error.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Failed to mark Task TaskType \\"shooooo\\" as running: Mark Task as running has failed miserably", + ] + `); + + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); + }); + + test('should log when running a Task fails', async () => { + const logger = mockLogger(); + const pool = new TaskPool({ + maxWorkers: 3, + logger, + }); + + const taskFailedToRun = mockTask(); + taskFailedToRun.run.mockImplementation(async () => { + throw new Error(`Run Task has failed miserably`); + }); + + const result = await pool.run([mockTask(), taskFailedToRun, mockTask()]); + + expect(logger.warn.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "Task TaskType \\"shooooo\\" failed in attempt to run: Run Task has failed miserably", + ] + `); + + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); + }); + + test('Running a task which fails still takes up capacity', async () => { + const logger = mockLogger(); + const pool = new TaskPool({ + maxWorkers: 1, + logger, + }); + + const taskFailedToRun = mockTask(); + taskFailedToRun.run.mockImplementation(async () => { + await sleep(0); + throw new Error(`Run Task has failed miserably`); + }); + + const result = await pool.run([taskFailedToRun, mockTask()]); + + expect(result).toEqual(TaskPoolRunResult.RanOutOfCapacity); }); test('clears up capacity when a task completes', async () => { @@ -78,7 +142,7 @@ describe('TaskPool', () => { { ...mockTask(), run: secondRun }, ]); - expect(result).toBeFalsy(); + expect(result).toEqual(TaskPoolRunResult.RanOutOfCapacity); expect(pool.occupiedWorkers).toEqual(1); expect(pool.availableWorkers).toEqual(0); @@ -133,7 +197,7 @@ describe('TaskPool', () => { }, ]); - expect(result).toBeTruthy(); + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); expect(pool.occupiedWorkers).toEqual(2); expect(pool.availableWorkers).toEqual(0); @@ -173,7 +237,7 @@ describe('TaskPool', () => { }, ]); - expect(result).toBeTruthy(); + expect(result).toEqual(TaskPoolRunResult.RunningAllClaimedTasks); await pool.run([]); expect(pool.occupiedWorkers).toEqual(0); @@ -181,11 +245,13 @@ describe('TaskPool', () => { // Allow the task to cancel... await cancelled; - sinon.assert.calledWithMatch(logger.error, /Failed to cancel task "shooooo!"/); + expect(logger.error.mock.calls[0][0]).toMatchInlineSnapshot( + `"Failed to cancel task \\"shooooo!\\": Error: Dern!"` + ); }); function mockRun() { - return sinon.spy(async () => { + return jest.fn(async () => { await sleep(0); return { state: {} }; }); @@ -195,8 +261,9 @@ describe('TaskPool', () => { return { isExpired: false, cancel: async () => undefined, - markTaskAsRunning: async () => true, + markTaskAsRunning: jest.fn(async () => true), run: mockRun(), + toString: () => `TaskType "shooooo"`, }; } }); diff --git a/x-pack/legacy/plugins/task_manager/task_pool.ts b/x-pack/legacy/plugins/task_manager/task_pool.ts index 7afbec65a0d8c6..5828cb0df4a4d0 100644 --- a/x-pack/legacy/plugins/task_manager/task_pool.ts +++ b/x-pack/legacy/plugins/task_manager/task_pool.ts @@ -8,7 +8,7 @@ * This module contains the logic that ensures we don't run too many * tasks at once in a given Kibana instance. */ - +import { performance } from 'perf_hooks'; import { Logger } from './types'; import { TaskRunner } from './task_runner'; @@ -17,6 +17,13 @@ interface Opts { logger: Logger; } +export enum TaskPoolRunResult { + RunningAllClaimedTasks = 'RunningAllClaimedTasks', + RanOutOfCapacity = 'RanOutOfCapacity', +} + +const VERSION_CONFLICT_MESSAGE = 'Task has been claimed by another Kibana service'; + /** * Runs tasks in batches, taking costs into account. */ @@ -66,38 +73,64 @@ export class TaskPool { }; public cancelRunningTasks() { - this.logger.debug(`Cancelling running tasks.`); + this.logger.debug('Cancelling running tasks.'); for (const task of this.running) { this.cancelTask(task); } } - private async attemptToRun(tasks: TaskRunner[]) { - for (const task of tasks) { - if (this.availableWorkers > 0) { - if (await task.markTaskAsRunning()) { - this.running.add(task); - task - .run() - .catch(err => { - this.logger.warn(`Task ${task} failed in attempt to run: ${err.message}`); - }) - .then(() => this.running.delete(task)); - } else { - this.logger.warn(`Failed to mark Task ${task} as running`); - } - } else { - return false; + private async attemptToRun(tasks: TaskRunner[]): Promise { + const [tasksToRun, leftOverTasks] = partitionListByCount(tasks, this.availableWorkers); + if (tasksToRun.length) { + performance.mark('attemptToRun_start'); + await Promise.all( + tasksToRun.map( + async task => + await task + .markTaskAsRunning() + .then((hasTaskBeenMarkAsRunning: boolean) => + hasTaskBeenMarkAsRunning + ? this.handleMarkAsRunning(task) + : this.handleFailureOfMarkAsRunning(task, { + name: 'TaskPoolVersionConflictError', + message: VERSION_CONFLICT_MESSAGE, + }) + ) + .catch(ex => this.handleFailureOfMarkAsRunning(task, ex)) + ) + ); + + performance.mark('attemptToRun_stop'); + performance.measure('taskPool.attemptToRun', 'attemptToRun_start', 'attemptToRun_stop'); + } + + if (leftOverTasks.length) { + if (this.availableWorkers) { + return this.attemptToRun(leftOverTasks); } + return TaskPoolRunResult.RanOutOfCapacity; } + return TaskPoolRunResult.RunningAllClaimedTasks; + } + + private handleMarkAsRunning(task: TaskRunner) { + this.running.add(task); + task + .run() + .catch(err => { + this.logger.warn(`Task ${task.toString()} failed in attempt to run: ${err.message}`); + }) + .then(() => this.running.delete(task)); + } - return true; + private handleFailureOfMarkAsRunning(task: TaskRunner, err: Error) { + this.logger.error(`Failed to mark Task ${task.toString()} as running: ${err.message}`); } private cancelExpiredTasks() { for (const task of this.running) { if (task.isExpired) { - this.logger.debug(`Cancelling expired task ${task}.`); + this.logger.debug(`Cancelling expired task ${task.toString()}.`); this.cancelTask(task); } } @@ -105,11 +138,16 @@ export class TaskPool { private async cancelTask(task: TaskRunner) { try { - this.logger.debug(`Cancelling task ${task}.`); + this.logger.debug(`Cancelling task ${task.toString()}.`); this.running.delete(task); await task.cancel(); } catch (err) { - this.logger.error(`Failed to cancel task ${task}: ${err}`); + this.logger.error(`Failed to cancel task ${task.toString()}: ${err}`); } } } + +function partitionListByCount(list: T[], count: number): [T[], T[]] { + const listInCount = list.splice(0, count); + return [listInCount, list]; +} diff --git a/x-pack/legacy/plugins/task_manager/task_runner.test.ts b/x-pack/legacy/plugins/task_manager/task_runner.test.ts index 49c55279eafe7a..578b86ba0b3f69 100644 --- a/x-pack/legacy/plugins/task_manager/task_runner.test.ts +++ b/x-pack/legacy/plugins/task_manager/task_runner.test.ts @@ -10,6 +10,7 @@ import { minutesFromNow } from './lib/intervals'; import { ConcreteTaskInstance } from './task'; import { TaskManagerRunner } from './task_runner'; import { mockLogger } from './test_utils'; +import { SavedObjectsErrorHelpers } from '../../../../src/core/server/saved_objects/service/lib/errors'; let fakeTimer: sinon.SinonFakeTimers; @@ -202,7 +203,7 @@ describe('TaskManagerRunner', () => { await promise; expect(wasCancelled).toBeTruthy(); - sinon.assert.neverCalledWithMatch(logger.warn, /not cancellable/); + expect(logger.warn).not.toHaveBeenCalled(); }); test('warns if cancel is called on a non-cancellable task', async () => { @@ -220,7 +221,10 @@ describe('TaskManagerRunner', () => { await runner.cancel(); await promise; - sinon.assert.calledWithMatch(logger.warn, /not cancellable/); + expect(logger.warn).toHaveBeenCalledTimes(1); + expect(logger.warn.mock.calls[0][0]).toMatchInlineSnapshot( + `"The task bar \\"foo\\" is not cancellable."` + ); }); test('sets startedAt, status, attempts and retryAt when claiming a task', async () => { @@ -420,6 +424,74 @@ describe('TaskManagerRunner', () => { ); }); + test('it returns false when markTaskAsRunning fails due to VERSION_CONFLICT_STATUS', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = testOpts({ + instance: { + id, + attempts: initialAttempts, + interval: undefined, + }, + definitions: { + bar: { + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); + + store.update = sinon + .stub() + .throws( + SavedObjectsErrorHelpers.decorateConflictError(new Error('repo error')).output.payload + ); + + expect(await runner.markTaskAsRunning()).toEqual(false); + }); + + test('it throw when markTaskAsRunning fails for unexpected reasons', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = testOpts({ + instance: { + id, + attempts: initialAttempts, + interval: undefined, + }, + definitions: { + bar: { + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); + + store.update = sinon + .stub() + .throws(SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id').output.payload); + + return expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot(` + Object { + "error": "Not Found", + "message": "Saved object [type/id] not found", + "statusCode": 404, + } + `); + }); + test('uses getRetry (returning true) to set retryAt when defined', async () => { const id = _.random(1, 20).toString(); const initialAttempts = _.random(1, 3); @@ -601,6 +673,7 @@ describe('TaskManagerRunner', () => { }; const runner = new TaskManagerRunner({ beforeRun: context => Promise.resolve(context), + beforeMarkRunning: context => Promise.resolve(context), logger, store, instance: Object.assign( @@ -655,9 +728,10 @@ describe('TaskManagerRunner', () => { await runner.run(); if (shouldBeValid) { - sinon.assert.notCalled(logger.warn); + expect(logger.warn).not.toHaveBeenCalled(); } else { - sinon.assert.calledWith(logger.warn, sinon.match(/invalid task result/i)); + expect(logger.warn).toHaveBeenCalledTimes(1); + expect(logger.warn.mock.calls[0][0]).toMatch(/invalid task result/i); } } diff --git a/x-pack/legacy/plugins/task_manager/task_runner.ts b/x-pack/legacy/plugins/task_manager/task_runner.ts index a51e0c885b978d..9d1431ed004e32 100644 --- a/x-pack/legacy/plugins/task_manager/task_runner.ts +++ b/x-pack/legacy/plugins/task_manager/task_runner.ts @@ -10,10 +10,11 @@ * rescheduling, middleware application, etc. */ +import { performance } from 'perf_hooks'; import Joi from 'joi'; import { intervalFromDate, intervalFromNow } from './lib/intervals'; import { Logger } from './types'; -import { BeforeRunFunction } from './lib/middleware'; +import { BeforeRunFunction, BeforeMarkRunningFunction } from './lib/middleware'; import { CancelFunction, CancellableTask, @@ -32,7 +33,7 @@ export interface TaskRunner { cancel: CancelFunction; markTaskAsRunning: () => Promise; run: () => Promise; - toString?: () => string; + toString: () => string; } interface Updatable { @@ -47,6 +48,7 @@ interface Opts { instance: ConcreteTaskInstance; store: Updatable; beforeRun: BeforeRunFunction; + beforeMarkRunning: BeforeMarkRunningFunction; } /** @@ -62,8 +64,9 @@ export class TaskManagerRunner implements TaskRunner { private instance: ConcreteTaskInstance; private definitions: TaskDictionary; private logger: Logger; - private store: Updatable; + private bufferedTaskStore: Updatable; private beforeRun: BeforeRunFunction; + private beforeMarkRunning: BeforeMarkRunningFunction; /** * Creates an instance of TaskManagerRunner. @@ -79,8 +82,9 @@ export class TaskManagerRunner implements TaskRunner { this.instance = sanitizeInstance(opts.instance); this.definitions = opts.definitions; this.logger = opts.logger; - this.store = opts.store; + this.bufferedTaskStore = opts.store; this.beforeRun = opts.beforeRun; + this.beforeMarkRunning = opts.beforeMarkRunning; } /** @@ -153,14 +157,20 @@ export class TaskManagerRunner implements TaskRunner { * @returns {Promise} */ public async markTaskAsRunning(): Promise { + performance.mark('markTaskAsRunning_start'); + const VERSION_CONFLICT_STATUS = 409; - const attempts = this.instance.attempts + 1; const now = new Date(); - const ownershipClaimedUntil = this.instance.retryAt; + const { taskInstance } = await this.beforeMarkRunning({ + taskInstance: this.instance, + }); + + const attempts = taskInstance.attempts + 1; + const ownershipClaimedUntil = taskInstance.retryAt; try { - const { id } = this.instance; + const { id } = taskInstance; const timeUntilClaimExpires = howManyMsUntilOwnershipClaimExpires(ownershipClaimedUntil); if (timeUntilClaimExpires < 0) { @@ -171,8 +181,8 @@ export class TaskManagerRunner implements TaskRunner { ); } - this.instance = await this.store.update({ - ...this.instance, + this.instance = await this.bufferedTaskStore.update({ + ...taskInstance, status: 'running', startedAt: now, attempts, @@ -198,13 +208,14 @@ export class TaskManagerRunner implements TaskRunner { ); } + performanceStopMarkingTaskAsRunning(); return true; } catch (error) { + performanceStopMarkingTaskAsRunning(); if (error.statusCode !== VERSION_CONFLICT_STATUS) { throw error; } } - return false; } @@ -263,7 +274,7 @@ export class TaskManagerRunner implements TaskRunner { runAt = intervalFromDate(startedAt, this.instance.interval)!; } - await this.store.update({ + await this.bufferedTaskStore.update({ ...this.instance, runAt, state, @@ -280,7 +291,7 @@ export class TaskManagerRunner implements TaskRunner { private async processResultWhenDone(result: RunResult): Promise { // not a recurring task: clean up by removing the task instance from store try { - await this.store.remove(this.instance.id); + await this.bufferedTaskStore.remove(this.instance.id); } catch (err) { if (err.statusCode === 404) { this.logger.warn(`Task cleanup of ${this} failed in processing. Was remove called twice?`); @@ -306,7 +317,7 @@ export class TaskManagerRunner implements TaskRunner { return 'idle'; } - const maxAttempts = this.definition.maxAttempts || this.store.maxAttempts; + const maxAttempts = this.definition.maxAttempts || this.bufferedTaskStore.maxAttempts; return this.instance.attempts < maxAttempts ? 'idle' : 'failed'; } @@ -352,3 +363,12 @@ function sanitizeInstance(instance: ConcreteTaskInstance): ConcreteTaskInstance function howManyMsUntilOwnershipClaimExpires(ownershipClaimedUntil: Date | null): number { return ownershipClaimedUntil ? ownershipClaimedUntil.getTime() - Date.now() : 0; } + +function performanceStopMarkingTaskAsRunning() { + performance.mark('markTaskAsRunning_stop'); + performance.measure( + 'taskRunner.markTaskAsRunning', + 'markTaskAsRunning_start', + 'markTaskAsRunning_stop' + ); +} diff --git a/x-pack/legacy/plugins/task_manager/task_store.test.ts b/x-pack/legacy/plugins/task_manager/task_store.test.ts index 9779dc5efd28bb..46efc4bb57ba76 100644 --- a/x-pack/legacy/plugins/task_manager/task_store.test.ts +++ b/x-pack/legacy/plugins/task_manager/task_store.test.ts @@ -9,7 +9,6 @@ import sinon from 'sinon'; import uuid from 'uuid'; import { TaskDictionary, TaskDefinition, TaskInstance, TaskStatus } from './task'; import { FetchOpts, StoreOpts, OwnershipClaimingOpts, TaskStore } from './task_store'; -import { mockLogger } from './test_utils'; import { savedObjectsClientMock } from 'src/core/server/mocks'; import { SavedObjectsSerializer, SavedObjectsSchema, SavedObjectAttributes } from 'src/core/server'; @@ -77,6 +76,7 @@ describe('TaskStore', () => { test('serializes the params and state', async () => { const task = { + id: 'id', params: { hello: 'world' }, state: { foo: 'bar' }, taskType: 'report', @@ -99,7 +99,10 @@ describe('TaskStore', () => { taskType: 'report', user: undefined, }, - {} + { + id: 'id', + refresh: false, + } ); expect(result).toEqual({ @@ -326,233 +329,6 @@ describe('TaskStore', () => { }); }); - describe('fetchAvailableTasks', () => { - async function testFetchAvailableTasks({ opts = {}, hits = [] }: any = {}) { - const callCluster = sinon.spy(async (name: string, params?: any) => ({ hits: { hits } })); - const store = new TaskStore({ - callCluster, - logger: mockLogger(), - definitions: taskDefinitions, - maxAttempts: 2, - serializer, - ...opts, - }); - - const result = await store.fetchAvailableTasks(); - - sinon.assert.calledOnce(callCluster); - sinon.assert.calledWith(callCluster, 'search'); - - return { - result, - args: callCluster.args[0][1], - }; - } - - test('it returns normally with no tasks when the index does not exist.', async () => { - const callCluster = sinon.spy(async (name: string, params?: any) => ({ hits: { hits: [] } })); - const store = new TaskStore({ - index: 'tasky', - taskManagerId: '', - serializer, - callCluster, - definitions: taskDefinitions, - maxAttempts: 2, - savedObjectsRepository: savedObjectsClient, - }); - - const result = await store.fetchAvailableTasks(); - - sinon.assert.calledOnce(callCluster); - sinon.assert.calledWithMatch(callCluster, 'search', { ignoreUnavailable: true }); - - expect(result.length).toBe(0); - }); - - test('it filters tasks by supported types, maxAttempts, and runAt', async () => { - const maxAttempts = _.random(2, 43); - const customMaxAttempts = _.random(44, 100); - const { args } = await testFetchAvailableTasks({ - opts: { - maxAttempts, - definitions: { - foo: { - type: 'foo', - title: '', - createTaskRunner: jest.fn(), - }, - bar: { - type: 'bar', - title: '', - maxAttempts: customMaxAttempts, - createTaskRunner: jest.fn(), - }, - }, - }, - }); - expect(args).toMatchObject({ - body: { - query: { - bool: { - must: [ - { term: { type: 'task' } }, - { - bool: { - must: [ - { - bool: { - should: [ - { - bool: { - must: [ - { term: { 'task.status': 'idle' } }, - { range: { 'task.runAt': { lte: 'now' } } }, - ], - }, - }, - { - bool: { - must: [ - { term: { 'task.status': 'running' } }, - { range: { 'task.retryAt': { lte: 'now' } } }, - ], - }, - }, - ], - }, - }, - { - bool: { - should: [ - { exists: { field: 'task.interval' } }, - { - bool: { - must: [ - { term: { 'task.taskType': 'foo' } }, - { - range: { - 'task.attempts': { - lt: maxAttempts, - }, - }, - }, - ], - }, - }, - { - bool: { - must: [ - { term: { 'task.taskType': 'bar' } }, - { - range: { - 'task.attempts': { - lt: customMaxAttempts, - }, - }, - }, - ], - }, - }, - ], - }, - }, - ], - }, - }, - ], - }, - }, - size: 10, - sort: { - _script: { - type: 'number', - order: 'asc', - script: { - lang: 'expression', - source: `doc['task.retryAt'].value || doc['task.runAt'].value`, - }, - }, - }, - seq_no_primary_term: true, - }, - }); - }); - - test('it returns task objects', async () => { - const runAt = new Date(); - const { result } = await testFetchAvailableTasks({ - hits: [ - { - _id: 'aaa', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - interval: undefined, - attempts: 0, - status: 'idle', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - _id: 'bbb', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - interval: '5m', - attempts: 2, - status: 'running', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ], - }); - expect(result).toMatchObject([ - { - attempts: 0, - id: 'aaa', - interval: undefined, - params: { hello: 'world' }, - runAt, - scope: ['reporting'], - state: { baby: 'Henhen' }, - status: 'idle', - taskType: 'foo', - user: 'jimbo', - }, - { - attempts: 2, - id: 'bbb', - interval: '5m', - params: { shazm: 1 }, - runAt, - scope: ['reporting', 'ceo'], - state: { henry: 'The 8th' }, - status: 'running', - taskType: 'bar', - user: 'dabo', - }, - ]); - }); - }); - describe('claimAvailableTasks', () => { async function testClaimAvailableTasks({ opts = {}, @@ -927,7 +703,7 @@ describe('TaskStore', () => { user: undefined, ownerId: null, }, - { version: '123' } + { version: '123', refresh: false } ); expect(result).toEqual({ diff --git a/x-pack/legacy/plugins/task_manager/task_store.ts b/x-pack/legacy/plugins/task_manager/task_store.ts index 9b5253d8be7f95..58bffd2269eb6f 100644 --- a/x-pack/legacy/plugins/task_manager/task_store.ts +++ b/x-pack/legacy/plugins/task_manager/task_store.ts @@ -70,6 +70,11 @@ export interface ClaimOwnershipResult { docs: ConcreteTaskInstance[]; } +export interface BulkUpdateTaskFailureResult { + error: NonNullable; + task: ConcreteTaskInstance; +} + export interface UpdateByQueryResult { updated: number; version_conflicts: number; @@ -107,8 +112,6 @@ export class TaskStore { this.definitions = opts.definitions; this.serializer = opts.serializer; this.savedObjectsRepository = opts.savedObjectsRepository; - - this.fetchAvailableTasks = this.fetchAvailableTasks.bind(this); } /** @@ -128,7 +131,7 @@ export class TaskStore { const savedObject = await this.savedObjectsRepository.create( 'task', taskInstanceToAttributes(taskInstance), - { id: taskInstance.id } + { id: taskInstance.id, refresh: false } ); return savedObjectToConcreteTaskInstance(savedObject); @@ -148,88 +151,6 @@ export class TaskStore { }); } - /** - * Fetches tasks from the index, which are ready to be run. - * - runAt is now or past - * - id is not currently running in this instance of Kibana - * - has a type that is in our task definitions - * - * @param {TaskQuery} query - * @prop {string[]} types - Task types to be queried - * @prop {number} size - The number of task instances to retrieve - * @returns {Promise} - */ - public async fetchAvailableTasks(): Promise { - const { docs } = await this.search({ - query: { - bool: { - must: [ - // Either a task with idle status and runAt <= now or - // status running with a retryAt <= now. - { - bool: { - should: [ - { - bool: { - must: [ - { term: { 'task.status': 'idle' } }, - { range: { 'task.runAt': { lte: 'now' } } }, - ], - }, - }, - { - bool: { - must: [ - { term: { 'task.status': 'running' } }, - { range: { 'task.retryAt': { lte: 'now' } } }, - ], - }, - }, - ], - }, - }, - // Either task has an interval or the attempts < the maximum configured - { - bool: { - should: [ - { exists: { field: 'task.interval' } }, - ...Object.entries(this.definitions).map(([type, definition]) => ({ - bool: { - must: [ - { term: { 'task.taskType': type } }, - { - range: { - 'task.attempts': { - lt: definition.maxAttempts || this.maxAttempts, - }, - }, - }, - ], - }, - })), - ], - }, - }, - ], - }, - }, - size: 10, - sort: { - _script: { - type: 'number', - order: 'asc', - script: { - lang: 'expression', - source: `doc['task.retryAt'].value || doc['task.runAt'].value`, - }, - }, - }, - seq_no_primary_term: true, - }); - - return docs; - } - /** * Claims available tasks from the index, which are ready to be run. * - runAt is now or past @@ -376,6 +297,7 @@ export class TaskStore { return docs; } + /** * Updates the specified doc in the index, returning the doc * with its version up to date. @@ -388,7 +310,10 @@ export class TaskStore { 'task', doc.id, taskInstanceToAttributes(doc), - { version: doc.version } + { + refresh: false, + version: doc.version, + } ); return savedObjectToConcreteTaskInstance(updatedSavedObject); diff --git a/x-pack/legacy/plugins/task_manager/test_utils/index.ts b/x-pack/legacy/plugins/task_manager/test_utils/index.ts index 8ee5cdce064cc2..719ccadbe33dde 100644 --- a/x-pack/legacy/plugins/task_manager/test_utils/index.ts +++ b/x-pack/legacy/plugins/task_manager/test_utils/index.ts @@ -8,8 +8,6 @@ * A handful of helper functions for testing the task manager. */ -import sinon from 'sinon'; - // Caching this here to avoid setTimeout mocking affecting our tests. const nativeTimeout = setTimeout; @@ -18,10 +16,10 @@ const nativeTimeout = setTimeout; */ export function mockLogger() { return { - info: sinon.stub(), - debug: sinon.stub(), - warn: sinon.stub(), - error: sinon.stub(), + info: jest.fn(), + debug: jest.fn(), + warn: jest.fn(), + error: jest.fn(), }; } diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/ping_list.test.tsx.snap b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/ping_list.test.tsx.snap index 165cfe5c370d1e..9c6d2e2f495c11 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/ping_list.test.tsx.snap +++ b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/__snapshots__/ping_list.test.tsx.snap @@ -43,41 +43,30 @@ exports[`PingList component renders sorted list without errors 1`] = ` label="Status" labelType="label" > - @@ -92,37 +81,26 @@ exports[`PingList component renders sorted list without errors 1`] = ` label="Location" labelType="label" > - diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/ping_list.test.tsx b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/ping_list.test.tsx index 64aed656158469..46da7e52333543 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/ping_list.test.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/functional/__tests__/ping_list.test.tsx @@ -7,7 +7,7 @@ import React from 'react'; import { shallowWithIntl } from 'test_utils/enzyme_helpers'; import { PingResults, Ping } from '../../../../common/graphql/types'; -import { PingListComponent, BaseLocationOptions, toggleDetails } from '../ping_list'; +import { PingListComponent, AllLocationOption, toggleDetails } from '../ping_list'; import { EuiComboBoxOptionProps } from '@elastic/eui'; import { ExpandedRowMap } from '../monitor_list/types'; @@ -210,7 +210,7 @@ describe('PingList component', () => { onUpdateApp={jest.fn()} pageSize={30} selectedOption="down" - selectedLocation={BaseLocationOptions} + selectedLocation={AllLocationOption.value} /> ); expect(component).toMatchSnapshot(); diff --git a/x-pack/legacy/plugins/uptime/public/components/functional/ping_list.tsx b/x-pack/legacy/plugins/uptime/public/components/functional/ping_list.tsx index 5b280c5aceb87f..fb7c0a5af1e7f8 100644 --- a/x-pack/legacy/plugins/uptime/public/components/functional/ping_list.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/functional/ping_list.tsx @@ -6,12 +6,11 @@ import { EuiBadge, EuiBasicTable, - EuiComboBox, - EuiComboBoxOptionProps, EuiFlexGroup, EuiFlexItem, EuiHealth, EuiPanel, + EuiSelect, EuiSpacer, EuiText, EuiTitle, @@ -38,13 +37,13 @@ interface PingListQueryResult { } interface PingListProps { - onSelectedStatusChange: (status: string | null) => void; - onSelectedLocationChange: (location: EuiComboBoxOptionProps[]) => void; + onSelectedStatusChange: (status: string | undefined) => void; + onSelectedLocationChange: (location: any) => void; onPageCountChange: (itemCount: number) => void; onUpdateApp: () => void; pageSize: number; selectedOption: string; - selectedLocation: EuiComboBoxOptionProps[]; + selectedLocation: string | undefined; } type Props = UptimeGraphQLQueryProps & PingListProps; @@ -52,7 +51,7 @@ interface ExpandedRowMap { [key: string]: JSX.Element; } -export const BaseLocationOptions = [{ label: 'All', value: 'All' }]; +export const AllLocationOption = { text: 'All', value: '' }; export const toggleDetails = ( ping: Ping, @@ -84,32 +83,32 @@ export const PingListComponent = ({ }: Props) => { const [itemIdToExpandedRowMap, setItemIdToExpandedRowMap] = useState({}); - const statusOptions: EuiComboBoxOptionProps[] = [ + const statusOptions = [ { - label: i18n.translate('xpack.uptime.pingList.statusOptions.allStatusOptionLabel', { + text: i18n.translate('xpack.uptime.pingList.statusOptions.allStatusOptionLabel', { defaultMessage: 'All', }), value: '', }, { - label: i18n.translate('xpack.uptime.pingList.statusOptions.upStatusOptionLabel', { + text: i18n.translate('xpack.uptime.pingList.statusOptions.upStatusOptionLabel', { defaultMessage: 'Up', }), value: 'up', }, { - label: i18n.translate('xpack.uptime.pingList.statusOptions.downStatusOptionLabel', { + text: i18n.translate('xpack.uptime.pingList.statusOptions.downStatusOptionLabel', { defaultMessage: 'Down', }), value: 'down', }, ]; const locations = get(data, 'allPings.locations'); - const locationOptions: EuiComboBoxOptionProps[] = !locations - ? BaseLocationOptions - : BaseLocationOptions.concat( + const locationOptions = !locations + ? [AllLocationOption] + : [AllLocationOption].concat( locations.map(name => { - return { label: name, value: name }; + return { text: name, value: name }; }) ); @@ -253,22 +252,18 @@ export const PingListComponent = ({ defaultMessage: 'Status', })} > - value === selectedOption) || - statusOptions[2], - ]} + { - if (typeof selectedOptions[0].value === 'string') { + value={selectedOption} + onChange={selected => { + if (typeof selected.target.value === 'string') { onSelectedStatusChange( - // @ts-ignore it's definitely a string - selectedOptions[0].value !== '' ? selectedOptions[0].value : null + selected.target && selected.target.value !== '' + ? selected.target.value + : undefined ); } }} @@ -282,16 +277,18 @@ export const PingListComponent = ({ defaultMessage: 'Location', })} > - { - onSelectedLocationChange(selectedOptions); + onChange={selected => { + onSelectedLocationChange( + selected.target && selected.target.value !== '' + ? selected.target.value + : null + ); }} /> diff --git a/x-pack/legacy/plugins/uptime/public/pages/monitor.tsx b/x-pack/legacy/plugins/uptime/public/pages/monitor.tsx index 0ebcf48cd249de..86a0a9e4b0f0b9 100644 --- a/x-pack/legacy/plugins/uptime/public/pages/monitor.tsx +++ b/x-pack/legacy/plugins/uptime/public/pages/monitor.tsx @@ -7,7 +7,6 @@ import { // @ts-ignore No typings for EuiSpacer EuiSpacer, - EuiComboBoxOptionProps, } from '@elastic/eui'; import { ApolloQueryResult, OperationVariables, QueryOptions } from 'apollo-client'; import gql from 'graphql-tag'; @@ -23,7 +22,6 @@ import { UMUpdateBreadcrumbs } from '../lib/lib'; import { UptimeSettingsContext } from '../contexts'; import { useUrlParams } from '../hooks'; import { stringifyUrlParams } from '../lib/helper/stringify_url_params'; -import { BaseLocationOptions } from '../components/functional/ping_list'; import { useTrackPageview } from '../../../infra/public'; import { getTitle } from '../lib/helper/get_title'; @@ -74,16 +72,12 @@ export const MonitorPage = ({ }); }, [params]); - const [selectedLocation, setSelectedLocation] = useState( - BaseLocationOptions - ); - - const selLocationVal = selectedLocation[0].value === 'All' ? null : selectedLocation[0].value; + const [selectedLocation, setSelectedLocation] = useState(undefined); const sharedVariables = { dateRangeStart, dateRangeEnd, - location: selLocationVal, + location: selectedLocation, monitorId, }; @@ -111,7 +105,7 @@ export const MonitorPage = ({ + onSelectedStatusChange={(selectedStatus: string | undefined) => updateUrlParams({ selectedPingStatus: selectedStatus || '' }) } onUpdateApp={refreshApp} diff --git a/x-pack/scripts/functional_tests.js b/x-pack/scripts/functional_tests.js index 2ac8fff6ef8ab7..7b2c2c2b7840f1 100644 --- a/x-pack/scripts/functional_tests.js +++ b/x-pack/scripts/functional_tests.js @@ -13,7 +13,11 @@ require('@kbn/test').runTestsCli([ require.resolve('../test/api_integration/config_security_basic.js'), require.resolve('../test/api_integration/config.js'), require.resolve('../test/alerting_api_integration/spaces_only/config.ts'), - require.resolve('../test/alerting_api_integration/security_and_spaces/config.ts'), + // FLAKY: https://github.com/elastic/kibana/issues/50079 + // FLAKY: https://github.com/elastic/kibana/issues/50074 + // FLAKY: https://github.com/elastic/kibana/issues/48709 + // FLAKY: https://github.com/elastic/kibana/issues/50078 + // require.resolve('../test/alerting_api_integration/security_and_spaces/config.ts'), require.resolve('../test/plugin_api_integration/config.js'), require.resolve('../test/kerberos_api_integration/config'), require.resolve('../test/kerberos_api_integration/anonymous_access.config'), diff --git a/x-pack/scripts/functional_tests_server.js b/x-pack/scripts/functional_tests_server.js index c361f1839c50b7..039da38c8f5479 100644 --- a/x-pack/scripts/functional_tests_server.js +++ b/x-pack/scripts/functional_tests_server.js @@ -4,6 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ +process.env.ALLOW_PERFORMANCE_HOOKS_IN_TASK_MANAGER = true; + require('@kbn/plugin-helpers').babelRegister(); require('@kbn/test').startServersCli( require.resolve('../test/functional/config.js'), diff --git a/x-pack/test/accessibility/apps/login_page.ts b/x-pack/test/accessibility/apps/login_page.ts new file mode 100644 index 00000000000000..bce6a677453777 --- /dev/null +++ b/x-pack/test/accessibility/apps/login_page.ts @@ -0,0 +1,43 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { FtrProviderContext } from '../ftr_provider_context'; + +export default function({ getService, getPageObjects }: FtrProviderContext) { + const esArchiver = getService('esArchiver'); + const a11y = getService('a11y'); + const testSubjects = getService('testSubjects'); + const retry = getService('retry'); + const PageObjects = getPageObjects(['common', 'security']); + + describe('Security', () => { + describe('Login Page', () => { + before(async () => { + await esArchiver.load('empty_kibana'); + await PageObjects.security.logout(); + }); + + after(async () => { + await esArchiver.unload('empty_kibana'); + }); + + afterEach(async () => { + await PageObjects.security.logout(); + }); + + it('meets a11y requirements', async () => { + await PageObjects.common.navigateToApp('login'); + + await retry.waitFor( + 'login page visible', + async () => await testSubjects.exists('loginSubmit') + ); + + await a11y.testAppSnapshot(); + }); + }); + }); +} diff --git a/x-pack/test/accessibility/config.ts b/x-pack/test/accessibility/config.ts new file mode 100644 index 00000000000000..e1a90efccaa807 --- /dev/null +++ b/x-pack/test/accessibility/config.ts @@ -0,0 +1,26 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { FtrConfigProviderContext } from '@kbn/test/types/ftr'; +import { services } from './services'; +import { pageObjects } from './page_objects'; + +export default async function({ readConfigFile }: FtrConfigProviderContext) { + const functionalConfig = await readConfigFile(require.resolve('../functional/config')); + + return { + ...functionalConfig.getAll(), + + testFiles: [require.resolve('./apps/login_page')], + + pageObjects, + services, + + junit: { + reportName: 'X-Pack Accessibility Tests', + }, + }; +} diff --git a/x-pack/test/accessibility/ftr_provider_context.d.ts b/x-pack/test/accessibility/ftr_provider_context.d.ts new file mode 100644 index 00000000000000..bb257cdcbfe1b5 --- /dev/null +++ b/x-pack/test/accessibility/ftr_provider_context.d.ts @@ -0,0 +1,12 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { GenericFtrProviderContext } from '@kbn/test/types/ftr'; + +import { pageObjects } from './page_objects'; +import { services } from './services'; + +export type FtrProviderContext = GenericFtrProviderContext; diff --git a/x-pack/test/accessibility/page_objects.ts b/x-pack/test/accessibility/page_objects.ts new file mode 100644 index 00000000000000..abfa2a700db5f6 --- /dev/null +++ b/x-pack/test/accessibility/page_objects.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export { pageObjects } from '../functional/page_objects'; diff --git a/x-pack/test/accessibility/services.ts b/x-pack/test/accessibility/services.ts new file mode 100644 index 00000000000000..a74b97de36b986 --- /dev/null +++ b/x-pack/test/accessibility/services.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { services as kibanaA11yServices } from '../../../test/accessibility/services'; +import { services as functionalServices } from '../functional/services'; + +export const services = { + ...kibanaA11yServices, + ...functionalServices, +}; diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/index.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/index.ts index a643bd8f9f64fc..af9804473a4489 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/index.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/index.ts @@ -18,7 +18,11 @@ export default function alertingApiIntegrationTests({ const spacesService: SpacesService = getService('spaces'); const esArchiver = getService('esArchiver'); - describe('alerting api integration security and spaces enabled', function() { + // FLAKY: https://github.com/elastic/kibana/issues/50079 + // FLAKY: https://github.com/elastic/kibana/issues/50074 + // FLAKY: https://github.com/elastic/kibana/issues/48709 + // FLAKY: https://github.com/elastic/kibana/issues/50078 + describe.skip('alerting api integration security and spaces enabled', function() { this.tags('ciGroup1'); before(async () => { diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_apm.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_apm.json index a791c2b2b7419d..ddd8d8c9a1de66 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_apm.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_apm.json @@ -51,7 +51,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats.json index 3ce2f20415b5f2..fcde71551a4f37 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats.json @@ -60,7 +60,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats_management.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats_management.json index a64e2f40b33dc9..9186006415759e 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats_management.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_beats_management.json @@ -51,7 +51,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash.json index cc870216d405bd..7311230d108e98 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash.json @@ -51,7 +51,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash_management.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash_management.json index cc870216d405bd..7311230d108e98 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash_management.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/detect_logstash_management.json @@ -51,7 +51,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_exclusive_mb.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_exclusive_mb.json index 4ae753aca52251..69688bc46c27eb 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_exclusive_mb.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_exclusive_mb.json @@ -71,7 +71,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_mb.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_mb.json index 6935060b4d5f7e..8b207b418dae74 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_mb.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/es_and_kibana_mb.json @@ -71,7 +71,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_exclusive_mb.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_exclusive_mb.json index 161ce32e8ff5f3..319844a7e093d9 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_exclusive_mb.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_exclusive_mb.json @@ -71,7 +71,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_mb.json b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_mb.json index b93edacd82b310..f65436ce28616d 100644 --- a/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_mb.json +++ b/x-pack/test/api_integration/apis/monitoring/setup/collection/fixtures/kibana_mb.json @@ -71,7 +71,6 @@ }, "_meta": { "secondsAgo": 30, - "isOnCloud": false, "liveClusterUuid": null } } diff --git a/x-pack/test/api_integration/apis/telemetry/index.js b/x-pack/test/api_integration/apis/telemetry/index.js index d941cda9e3faea..6f794d56ae713a 100644 --- a/x-pack/test/api_integration/apis/telemetry/index.js +++ b/x-pack/test/api_integration/apis/telemetry/index.js @@ -8,5 +8,6 @@ export default function ({ loadTestFile }) { describe('Telemetry', () => { loadTestFile(require.resolve('./telemetry')); loadTestFile(require.resolve('./telemetry_local')); + loadTestFile(require.resolve('./opt_in')); }); } diff --git a/x-pack/test/api_integration/apis/telemetry/opt_in.ts b/x-pack/test/api_integration/apis/telemetry/opt_in.ts new file mode 100644 index 00000000000000..d2ad2d773d6924 --- /dev/null +++ b/x-pack/test/api_integration/apis/telemetry/opt_in.ts @@ -0,0 +1,63 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import expect from '@kbn/expect'; + +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function optInTest({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + const kibanaServer = getService('kibanaServer'); + + describe('/api/telemetry/v2/optIn API', () => { + let kibanaVersion: any; + + before(async () => { + const kibanaVersionAccessor = kibanaServer.version; + kibanaVersion = await kibanaVersionAccessor.get(); + + expect(typeof kibanaVersion).to.eql('string'); + expect(kibanaVersion.length).to.be.greaterThan(0); + }); + + it('should support sending false', async () => { + await postTelemetryV2Optin(supertest, false, 200); + const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest); + expect(enabled).to.be(false); + expect(lastVersionChecked).to.be(kibanaVersion); + }); + + it('should support sending true', async () => { + await postTelemetryV2Optin(supertest, true, 200); + const { enabled, lastVersionChecked } = await getSavedObjectAttributes(supertest); + expect(enabled).to.be(true); + expect(lastVersionChecked).to.be(kibanaVersion); + }); + + it('should not support sending null', async () => { + await postTelemetryV2Optin(supertest, null, 400); + }); + + it('should not support sending junk', async () => { + await postTelemetryV2Optin(supertest, 42, 400); + }); + }); +} + +async function postTelemetryV2Optin(supertest: any, value: any, statusCode: number): Promise { + const { body } = await supertest + .post('/api/telemetry/v2/optIn') + .set('kbn-xsrf', 'xxx') + .send({ enabled: value }) + .expect(statusCode); + + return body; +} + +async function getSavedObjectAttributes(supertest: any): Promise { + const { body } = await supertest.get('/api/saved_objects/telemetry/telemetry').expect(200); + return body.attributes; +} diff --git a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/index.js b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/index.js index 0547069dfb4694..17ae9b26fa3b3b 100644 --- a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/index.js +++ b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/index.js @@ -4,16 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import uuid from 'uuid'; +import _ from 'lodash'; +import stats from 'stats-lite'; +import prettyMilliseconds from 'pretty-ms'; +import { performance, PerformanceObserver } from 'perf_hooks'; import { initRoutes } from './init_routes'; -function avg(items) { - return ( - items.reduce((sum, val) => { - return sum + val; - }, 0) / items.length - ); -} - export default function TaskManagerPerformanceAPI(kibana) { return new kibana.Plugin({ name: 'perfTask', @@ -27,35 +24,46 @@ export default function TaskManagerPerformanceAPI(kibana) { init(server) { const taskManager = server.plugins.task_manager; - const performanceState = { - runningAverageTasks: 0, - averagesTaken: [], - runningAverageLeadTime: -1, - averagesTakenLeadTime: [], - leadTimeQueue: [], - }; + const performanceState = resetPerfState({}); + + let lastFlush = new Date(); + function flushPerfStats() { + setTimeout(flushPerfStats, 5000); + const prevFlush = lastFlush; + lastFlush = new Date(); - setInterval(() => { const tasks = performanceState.leadTimeQueue.length; - console.log(`I have processed ${tasks} tasks in the past 5s`); + const title = `[Perf${performanceState.capturing ? ' (capturing)' : ''}]`; + const seconds = parseInt((lastFlush - prevFlush) / 1000); + console.log( + `${title} I have processed ${tasks} tasks in the past ${seconds}s (${tasks / + seconds} per second)` + ); if (tasks > 0) { - const latestAverage = avg(performanceState.leadTimeQueue.splice(0, tasks)); + const latestAverage = avg(performanceState.leadTimeQueue.splice(0, tasks)).mean; performanceState.averagesTakenLeadTime.push(latestAverage); performanceState.averagesTaken.push(tasks); if (performanceState.averagesTakenLeadTime.length > 1) { - performanceState.runningAverageLeadTime = avg(performanceState.averagesTakenLeadTime); - performanceState.runningAverageTasks = avg(performanceState.averagesTaken); + performanceState.runningAverageLeadTime = avg( + performanceState.averagesTakenLeadTime + ).mean; + performanceState.runningAverageTasksPerSecond = + avg(performanceState.averagesTaken).mean / 5; } else { performanceState.runningAverageLeadTime = latestAverage; - performanceState.runningAverageTasks = tasks; + performanceState.runningAverageTasksPerSecond = tasks / 5; } } - }, 5000); + } + + setTimeout(flushPerfStats, 5000); + + const title = 'Perf Test Task'; taskManager.registerTaskDefinitions({ performanceTestTask: { - title: 'Perf Test Task', + title, description: 'A task for stress testing task_manager.', timeout: '1m', @@ -64,27 +72,41 @@ export default function TaskManagerPerformanceAPI(kibana) { async run() { const { params, state } = taskInstance; - const runAt = millisecondsFromNow(5000); + const counter = state.counter ? state.counter : 1; + const now = Date.now(); const leadTime = now - taskInstance.runAt; performanceState.leadTimeQueue.push(leadTime); - const counter = (state.counter ? 1 + state.counter : 1); + // schedule to run next cycle as soon as possible + const runAt = calRunAt(params, counter); const stateUpdated = { ...state, - counter + counter: counter + 1, }; - if(params.trackExecutionTimeline) { - stateUpdated.timeline = stateUpdated.timeline || []; - stateUpdated.timeline.push({ - owner: taskInstance.owner.split('-')[0], - counter, - leadTime, - ranAt: now - }); + if (params.trackExecutionTimeline && state.perf && state.perf.id) { + performance.mark(`perfTask_run_${state.perf.id}_${counter}`); + performance.measure( + 'perfTask.markUntilRun', + `perfTask_markAsRunning_${state.perf.id}_${counter}`, + `perfTask_run_${state.perf.id}_${counter}` + ); + if (counter === 1) { + performance.measure( + 'perfTask.firstRun', + `perfTask_schedule_${state.perf.id}`, + `perfTask_run_${state.perf.id}_${counter}` + ); + performance.measure( + 'perfTask.firstMarkAsRunningTillRan', + `perfTask_markAsRunning_${state.perf.id}_${counter}`, + `perfTask_run_${state.perf.id}_${counter}` + ); + } } + return { state: stateUpdated, runAt, @@ -95,17 +117,249 @@ export default function TaskManagerPerformanceAPI(kibana) { }, }); - initRoutes(server, performanceState); + taskManager.addMiddleware({ + async beforeSave({ taskInstance, ...opts }) { + const modifiedInstance = { + ...taskInstance, + }; + + if (taskInstance.params && taskInstance.params.trackExecutionTimeline) { + modifiedInstance.state = modifiedInstance.state || {}; + modifiedInstance.state.perf = modifiedInstance.state.perf || {}; + modifiedInstance.state.perf.id = uuid.v4().replace(/-/gi, '_'); + performance.mark(`perfTask_schedule_${modifiedInstance.state.perf.id}`); + } + + return { + ...opts, + taskInstance: modifiedInstance, + }; + }, + + async beforeMarkRunning({ taskInstance, ...opts }) { + const modifiedInstance = { + ...taskInstance, + }; + + if ( + modifiedInstance.state && + modifiedInstance.state.perf && + modifiedInstance.state.perf.id + ) { + const { counter = 1 } = modifiedInstance.state; + performance.mark(`perfTask_markAsRunning_${modifiedInstance.state.perf.id}_${counter}`); + if (counter === 1) { + performance.measure( + 'perfTask.firstMarkAsRunning', + `perfTask_schedule_${modifiedInstance.state.perf.id}`, + `perfTask_markAsRunning_${modifiedInstance.state.perf.id}_${counter}` + ); + } else if (counter > 1) { + performance.measure( + 'perfTask.runUntilNextMarkAsRunning', + `perfTask_run_${modifiedInstance.state.perf.id}_${counter - 1}`, + `perfTask_markAsRunning_${modifiedInstance.state.perf.id}_${counter}` + ); + } + } + + return { + ...opts, + taskInstance: modifiedInstance, + }; + }, + }); + + const perfApi = { + capture() { + resetPerfState(performanceState); + performanceState.capturing = true; + performance.mark('perfTest.start'); + }, + endCapture() { + return new Promise(resolve => { + performanceState.performance.summarize.push([resolve, perfApi.summarize]); + + performance.mark('perfTest.end'); + performance.measure('perfTest.duration', 'perfTest.start', 'perfTest.end'); + }); + }, + summarize(perfTestDuration) { + const { + runningAverageTasksPerSecond, + runningAverageLeadTime, + performance, + } = performanceState; + + const { + numberOfTasksRanOverall, + elasticsearchApiCalls, + activityDuration, + sleepDuration, + cycles, + claimAvailableTasksNoTasks, + claimAvailableTasksNoAvailableWorkers, + taskPoolAttemptToRun, + taskRunnerMarkTaskAsRunning + } = performance; + + const perfRes = { + perfTestDuration: prettyMilliseconds(perfTestDuration), + runningAverageTasksPerSecond, + runningAverageLeadTime, + numberOfTasksRanOverall, + claimAvailableTasksNoTasks, + claimAvailableTasksNoAvailableWorkers, + elasticsearchApiCalls: _.mapValues(elasticsearchApiCalls, avg), + sleepDuration: prettyMilliseconds(stats.sum(sleepDuration)), + activityDuration: prettyMilliseconds(stats.sum(activityDuration)), + cycles, + taskPoolAttemptToRun: avg(taskPoolAttemptToRun), + taskRunnerMarkTaskAsRunning: avg(taskRunnerMarkTaskAsRunning), + }; + + resetPerfState(performanceState); + + return perfRes; + }, + }; + + initRoutes(server, perfApi); }, }); } -function millisecondsFromNow(ms) { - if (!ms) { - return; +function calRunAt(params, counter) { + const runAt = counter === 1 ? new Date(params.startAt) : new Date(); + return runAt.getTime() < params.runUntil ? runAt : undefined; +} + +function avg(items) { + const mode = stats.mode(items); + return { + mean: parseInt(stats.mean(items)), + range: { + min: parseInt(typeof mode === 'number' ? mode : _.min([...mode])), + max: parseInt(typeof mode === 'number' ? mode : _.max([...mode])), + }, + }; +} + +function resetPerfState(target) { + if(target.performanceObserver) { + target.performanceObserver.disconnect(); } - const dt = new Date(); - dt.setTime(dt.getTime() + ms); - return dt; + const performanceState = Object.assign(target, { + capturing: false, + runningAverageTasksPerSecond: 0, + averagesTaken: [], + runningAverageLeadTime: -1, + averagesTakenLeadTime: [], + leadTimeQueue: [], + performance: { + numberOfTasksRanOverall: 0, + cycles: { + fillPoolStarts: 0, + fillPoolCycles: 0, + fillPoolBail: 0, + fillPoolBailNoTasks: 0, + }, + claimAvailableTasksNoTasks: 0, + claimAvailableTasksNoAvailableWorkers: 0, + elasticsearchApiCalls: { + timeUntilFirstRun: [], + timeUntilFirstMarkAsRun: [], + firstMarkAsRunningTillRan: [], + timeFromMarkAsRunTillRun: [], + timeFromRunTillNextMarkAsRun: [], + claimAvailableTasks: [], + }, + activityDuration: [], + sleepDuration: [], + taskPollerActivityDurationPreScheduleComplete: [], + taskPoolAttemptToRun: [], + taskRunnerMarkTaskAsRunning: [], + + summarize: [] + }, + }); + + performanceState.performanceObserver = new PerformanceObserver((list, observer) => { + list.getEntries().forEach(entry => { + const { name, duration } = entry; + switch (name) { + // Elasticsearch Api Calls + case 'perfTask.firstRun': + performanceState.performance.elasticsearchApiCalls.timeUntilFirstRun.push(duration); + break; + case 'perfTask.firstMarkAsRunning': + performanceState.performance.elasticsearchApiCalls.timeUntilFirstMarkAsRun.push(duration); + break; + case 'perfTask.firstMarkAsRunningTillRan': + performanceState.performance.elasticsearchApiCalls.firstMarkAsRunningTillRan.push(duration); + break; + case 'perfTask.markUntilRun': + performanceState.performance.elasticsearchApiCalls.timeFromMarkAsRunTillRun.push(duration); + break; + case 'perfTask.runUntilNextMarkAsRunning': + performanceState.performance.elasticsearchApiCalls.timeFromRunTillNextMarkAsRun.push(duration); + break; + case 'claimAvailableTasks': + performanceState.performance.elasticsearchApiCalls.claimAvailableTasks.push(duration); + break; + case 'TaskPoller.sleepDuration': + performanceState.performance.sleepDuration.push(duration); + break; + case 'fillPool.activityDurationUntilNoTasks': + performanceState.performance.activityDuration.push(duration); + break; + case 'fillPool.activityDurationUntilExhaustedCapacity': + performanceState.performance.activityDuration.push(duration); + break; + case 'fillPool.bailExhaustedCapacity': + performanceState.performance.cycles.fillPoolBail++; + break; + case 'fillPool.bailNoTasks': + performanceState.performance.cycles.fillPoolBail++; + performanceState.performance.cycles.fillPoolBailNoTasks++; + break; + case 'fillPool.start': + performanceState.performance.cycles.fillPoolStarts++; + break; + case 'fillPool.cycle': + performanceState.performance.cycles.fillPoolCycles++; + break; + break; + case 'claimAvailableTasks.noTasks': + performanceState.performance.claimAvailableTasksNoTasks++; + break; + case 'claimAvailableTasks.noAvailableWorkers': + performanceState.performance.claimAvailableTasksNoAvailableWorkers++; + break; + case 'taskPool.attemptToRun': + performanceState.performance.taskPoolAttemptToRun.push(duration); + break; + case 'taskRunner.markTaskAsRunning': + performanceState.performance.taskRunnerMarkTaskAsRunning.push(duration); + break; + case 'perfTest.duration': + observer.disconnect(); + const { summarize } = performanceState.performance; + if(summarize && summarize.length) { + summarize.splice(0, summarize.length).forEach(([resolve, summarize]) => { + resolve(summarize(duration)); + }); + } + break; + default: + if (name.startsWith('perfTask_run_')) { + performanceState.performance.numberOfTasksRanOverall++; + } + } + }); + }); + performanceState.performanceObserver.observe({ entryTypes: ['measure', 'mark'] }); + + return performanceState; } diff --git a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/init_routes.js b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/init_routes.js index b9fe788093d117..ca6d8707f5c58f 100644 --- a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/init_routes.js +++ b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/init_routes.js @@ -5,6 +5,7 @@ */ import Joi from 'joi'; +import { range, chunk } from 'lodash'; const scope = 'perf-testing'; export function initRoutes(server, performanceState) { @@ -18,32 +19,56 @@ export function initRoutes(server, performanceState) { payload: Joi.object({ tasksToSpawn: Joi.number().required(), durationInSeconds: Joi.number().required(), - trackExecutionTimeline: Joi.boolean().default(false).required(), + trackExecutionTimeline: Joi.boolean() + .default(false) + .required(), }), }, }, async handler(request) { - const { tasksToSpawn, durationInSeconds, trackExecutionTimeline } = request.payload; - const tasks = []; + performanceState.capture(); - for (let taskIndex = 0; taskIndex < tasksToSpawn; taskIndex++) { - tasks.push( - await taskManager.schedule( - { - taskType: 'performanceTestTask', - params: { taskIndex, trackExecutionTimeline }, - scope: [scope], - }, - { request } + const { tasksToSpawn, durationInSeconds, trackExecutionTimeline } = request.payload; + const startAt = millisecondsFromNow(5000).getTime(); + await chunk(range(tasksToSpawn), 200) + .map(chunkOfTasksToSpawn => () => + Promise.all( + chunkOfTasksToSpawn.map(taskIndex => + taskManager.schedule( + { + taskType: 'performanceTestTask', + params: { + startAt, + taskIndex, + trackExecutionTimeline, + runUntil: millisecondsFromNow(durationInSeconds * 1000).getTime(), + }, + scope: [scope], + }, + { request } + ) + ) ) - ); - } + ) + .reduce((chain, nextExecutor) => { + return chain.then(() => nextExecutor()); + }, Promise.resolve()); return new Promise(resolve => { setTimeout(() => { - resolve(performanceState); - }, durationInSeconds * 1000); + performanceState.endCapture().then(resolve); + }, durationInSeconds * 1000 + 10000 /* wait extra 10s to drain queue */); }); }, }); } + +function millisecondsFromNow(ms) { + if (!ms) { + return; + } + + const dt = new Date(); + dt.setTime(dt.getTime() + ms); + return dt; +} diff --git a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/package.json b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/package.json index 22ee21852aeee9..7d46d6b0f3cca6 100644 --- a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/package.json +++ b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/package.json @@ -7,6 +7,10 @@ }, "license": "Apache-2.0", "dependencies": { - "joi": "^13.5.2" + "lodash": "^4.17.15", + "uuid": "3.3.2", + "joi": "^13.5.2", + "stats-lite": "2.2.0", + "pretty-ms": "5.0.0" } } diff --git a/x-pack/test/plugin_api_perf/test_suites/task_manager/task_manager_perf_integration.ts b/x-pack/test/plugin_api_perf/test_suites/task_manager/task_manager_perf_integration.ts index e8dbf7c5092878..d5caff5f9d10bf 100644 --- a/x-pack/test/plugin_api_perf/test_suites/task_manager/task_manager_perf_integration.ts +++ b/x-pack/test/plugin_api_perf/test_suites/task_manager/task_manager_perf_integration.ts @@ -10,23 +10,151 @@ export default function({ getService }: { getService: (service: string) => any } const log = getService('log'); const supertest = getService('supertest'); + const params = { tasksToSpawn: 100, trackExecutionTimeline: true, durationInSeconds: 60 }; describe('stressing task manager', () => { - it('should run 10 tasks every second for a minute', async () => { - const { runningAverageTasks, runningAverageLeadTime } = await supertest + it(`should run ${params.tasksToSpawn} tasks over ${params.durationInSeconds} seconds`, async () => { + const { + runningAverageTasksPerSecond, + runningAverageLeadTime, + // how often things happen in Task Manager + cycles: { fillPoolStarts, fillPoolCycles, fillPoolBail, fillPoolBailNoTasks }, + claimAvailableTasksNoTasks, + claimAvailableTasksNoAvailableWorkers, + numberOfTasksRanOverall, + // how long it takes to talk to Elasticsearch + elasticsearchApiCalls: { + timeUntilFirstMarkAsRun, + firstMarkAsRunningTillRan, + timeFromMarkAsRunTillRun, + timeFromRunTillNextMarkAsRun, + claimAvailableTasks, + }, + // durations in Task Manager + perfTestDuration, + taskPoolAttemptToRun, + taskRunnerMarkTaskAsRunning, + sleepDuration, + activityDuration, + } = await supertest .post('/api/perf_tasks') .set('kbn-xsrf', 'xxx') - .send({ tasksToSpawn: 20, trackExecutionTimeline: true, durationInSeconds: 60 }) + .send(params) .expect(200) .then((response: any) => response.body); - log.debug(`Stress Test Result:`); - log.debug(`Average number of tasks executed per second: ${runningAverageTasks}`); - log.debug( - `Average time it took from the moment a task's scheduled time was reached, until Task Manager picked it up: ${runningAverageLeadTime}` + log.info(cyan(`Stress Test Result:`)); + log.info( + `Average number of tasks executed per second: ${bright(runningAverageTasksPerSecond)}` ); + log.info( + `Average time between a task's "runAt" scheduled time and the time it actually ran: ${bright( + runningAverageLeadTime + )}` + ); + + if (params.trackExecutionTimeline) { + log.info( + `Overall number of tasks ran in ${bright(params.durationInSeconds)} seconds: ${bright( + numberOfTasksRanOverall + )}` + ); + log.info(`Average time between stages:`); + log.info( + `Schedule ---[${descMetric( + timeUntilFirstMarkAsRun + )}]--> first markAsRunning ---[${descMetric(firstMarkAsRunningTillRan)}]--> first run` + ); + log.info( + `markAsRunning ---[${descMetric(timeFromMarkAsRunTillRun)}]--> run ---[${descMetric( + timeFromRunTillNextMarkAsRun + )}]---> next markAsRunning` + ); + log.info(`Duration of Perf Test: ${bright(perfTestDuration)}`); + log.info(`Activity within Task Poller: ${bright(activityDuration)}`); + log.info(`Inactivity due to Sleep: ${bright(sleepDuration)}`); + log.info(`Polling Cycles: ${colorizeCycles(fillPoolStarts, fillPoolCycles, fillPoolBail)}`); + if (fillPoolBail > 0) { + log.info(` ⮑ Bailed due to:`); + if (fillPoolBailNoTasks > 0) { + log.info(` ⮑ No Tasks To Process:`); + if (claimAvailableTasksNoTasks > 0) { + log.info(` ⮑ ${claimAvailableTasksNoTasks} Times, due to No Tasks Claimed`); + } + if (claimAvailableTasksNoAvailableWorkers > 0) { + log.info( + ` ⮑ ${claimAvailableTasksNoAvailableWorkers} Times, due to having No Available Worker Capacity` + ); + } + } + if (fillPoolBail - fillPoolBailNoTasks > 0) { + log.info( + ` ⮑ Exhausted Available Workers due to on going Task runs ${fillPoolBail - + fillPoolBailNoTasks}` + ); + } + } + log.info( + `average duration taken to Claim Available Tasks: ${descMetric(claimAvailableTasks)}` + ); + log.info( + `average duration taken to Mark claimed Tasks as Running in Task Pool: ${descMetric( + taskPoolAttemptToRun + )}` + ); + log.info( + `average duration taken to Mark individual Tasks as Running in Task Runner: ${descMetric( + taskRunnerMarkTaskAsRunning + )}` + ); + } - expect(runningAverageTasks).to.be.greaterThan(0); + expect(runningAverageTasksPerSecond).to.be.greaterThan(0); expect(runningAverageLeadTime).to.be.greaterThan(0); }); }); } + +function descMetric(metric: { mean: number; range: { min: number; max: number } }): string { + return `${colorize(metric.mean)}ms ${dim(`(`)}${colorize(metric.range.min)}${dim( + `ms - ` + )}${colorize(metric.range.max)}${dim(`ms)`)}`; +} + +function colorize(avg: number) { + if (!avg) { + return red('?'); + } + return avg < 500 ? green(`${avg}`) : avg < 1000 ? cyan(`${avg}`) : red(`${avg}`); +} + +function colorizeCycles(fillPoolStarts: number, fillPoolCycles: number, fillPoolBail: number) { + const perc = (fillPoolCycles * 100) / fillPoolStarts; + const colorFunc = perc >= 100 ? green : perc >= 50 ? cyan : red; + return ( + `ran ` + + bright(`${fillPoolStarts}`) + + ` cycles, of which ` + + colorFunc(`${fillPoolCycles}`) + + ` were reran before bailing` + ); +} + +function cyan(str: string) { + return `\x1b[36m${str}\x1b[0m`; +} + +function red(str: string) { + return `\x1b[31m${str}\x1b[0m`; +} + +function green(str: string) { + return `\x1b[32m${str}\x1b[0m`; +} + +function dim(str: string) { + return `\x1b[2m${str}\x1b[0m`; +} + +function bright(str: string | number) { + return `\x1b[1m${str}\x1b[0m`; +} diff --git a/yarn.lock b/yarn.lock index 185152e0d0a345..6526c90663b753 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5785,6 +5785,11 @@ aws4@^1.6.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e" integrity sha1-g+9cqGCysy5KDe7e6MdxudtXRx4= +axe-core@^3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-3.3.2.tgz#7baf3c55a5cf1621534a2c38735f5a1bf2f7e1a8" + integrity sha512-lRdxsRt7yNhqpcXQk1ao1BL73OZDzmFCWOG0mC4tGR/r14ohH2payjHwCMQjHGbBKm924eDlmG7utAGHiX/A6g== + axios@^0.18.0, axios@^0.18.1: version "0.18.1" resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.1.tgz#ff3f0de2e7b5d180e757ad98000f1081b87bcea3" @@ -5982,6 +5987,14 @@ babel-plugin-emotion@^9.2.11: source-map "^0.5.7" touch "^2.0.1" +babel-plugin-filter-imports@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/babel-plugin-filter-imports/-/babel-plugin-filter-imports-3.0.0.tgz#a849683837ad29960da17492fb32789ab6b09a11" + integrity sha512-p/chjzVTgCxUqyLM0q/pfWVZS7IJTwGQMwNg0LOvuQpKiTftQgZDtkGB8XvETnUw19rRcL7bJCTopSwibTN2tA== + dependencies: + "@babel/types" "^7.4.0" + lodash "^4.17.11" + babel-plugin-istanbul@^5.1.0: version "5.1.1" resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-5.1.1.tgz#7981590f1956d75d67630ba46f0c22493588c893" @@ -7710,10 +7723,10 @@ chrome-trace-event@^1.0.0, chrome-trace-event@^1.0.2: dependencies: tslib "^1.9.0" -chromedriver@^77.0.0: - version "77.0.0" - resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-77.0.0.tgz#bd916cc87a0ccb7a6e4fb4b43cb2368bc54db6a0" - integrity sha512-mZa1IVx4HD8rDaItWbnS470mmypgiWsDiu98r0NkiT4uLm3qrANl4vOU6no6vtWtLQiW5kt1POcIbjeNpsLbXA== +chromedriver@^78.0.1: + version "78.0.1" + resolved "https://registry.yarnpkg.com/chromedriver/-/chromedriver-78.0.1.tgz#2db3425a2cba6fcaf1a41d9538b16c3d06fa74a8" + integrity sha512-eOsyFk4xb9EECs1VMrDbxO713qN+Bu1XUE8K9AuePc3839TPdAegg72kpXSzkeNqRNZiHbnJUItIVCLFkDqceA== dependencies: del "^4.1.1" extract-zip "^1.6.7"