From 3d88ec77bd126cb0ed6d73a113d8ed84d061481e Mon Sep 17 00:00:00 2001 From: ppisljar Date: Mon, 17 Sep 2018 14:41:29 +0200 Subject: [PATCH] removing dashboardContext function --- .../dashboard/__tests__/dashboard_context.js | 150 ------------------ .../public/dashboard/dashboard_context.js | 60 ------- .../public/kbn_vis_types/request_handler.js | 8 +- .../public/vis/timelion_request_handler.js | 10 +- .../vega/public/data_model/es_query_parser.js | 8 +- .../vega/public/data_model/vega_parser.js | 4 +- .../vega/public/vega_request_handler.js | 11 +- src/ui/public/courier/index.js | 1 + src/ui/public/courier/search_source/index.js | 2 +- 9 files changed, 23 insertions(+), 231 deletions(-) delete mode 100644 src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js delete mode 100644 src/core_plugins/kibana/public/dashboard/dashboard_context.js diff --git a/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js deleted file mode 100644 index 75194046dd900..0000000000000 --- a/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js +++ /dev/null @@ -1,150 +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 sinon from 'sinon'; -import { expect } from 'chai'; -import { dashboardContextProvider } from '../dashboard_context'; - -describe('Dashboard Context', () => { - - describe('with query bar', () => { - let Private; - let getAppState; - let getDashboardContext; - beforeEach(() => { - Private = sinon.stub().returns({ - getFilters() { - return []; - } - }); - }); - - it('should return an empty must and must not when there are no filters or queries', () => { - getAppState = sinon.stub().returns({ - query: { - language: 'lucene', - query: null - } - }); - getDashboardContext = dashboardContextProvider(Private, getAppState); - const context = getDashboardContext(); - expect(context).to.eql({ - bool: { - must: [], - must_not: [] - } - }); - }); - - it('should add a valid query to must', () => { - getAppState = sinon.stub().returns({ - query: { - language: 'lucene', - query: '*' - } - }); - getDashboardContext = dashboardContextProvider(Private, getAppState); - const context = getDashboardContext(); - expect(context).to.eql({ - bool: { - must: [ - { - query_string: { - query: '*' - } - } - ], - must_not: [] - } - }); - }); - - }); - - describe('with filter bar', () => { - let Private; - let getAppState; - let getDashboardContext; - beforeEach(() => { - getAppState = sinon.stub().returns({ query: { language: 'something-else' } }); - }); - - afterEach(() => { - getAppState.resetHistory(); - }); - - it('should add a valid filter to must', () => { - Private = sinon.stub().returns({ - getFilters() { - return [ - { meta: { negate: false }, term: { foo: 'bar' } } - ]; - } - }); - getDashboardContext = dashboardContextProvider(Private, getAppState); - const context = getDashboardContext(); - expect(context).to.eql({ - bool: { - must: [{ term: { foo: 'bar' } }], - must_not: [] - } - }); - }); - - it('should add a valid filter to must_not', () => { - Private = sinon.stub().returns({ - getFilters() { - return [ - { meta: { negate: true }, term: { foo: 'bar' } } - ]; - } - }); - getDashboardContext = dashboardContextProvider(Private, getAppState); - const context = getDashboardContext(); - expect(context).to.eql({ - bool: { - must: [], - must_not: [{ term: { foo: 'bar' } }] - } - }); - }); - - it('should not add a disabled filter', () => { - Private = sinon.stub().returns({ - getFilters() { - return [ - { meta: { negate: true, disabled: true }, term: { foo: 'bar' } } - ]; - } - }); - getDashboardContext = dashboardContextProvider(Private, getAppState); - const context = getDashboardContext(); - expect(context).to.eql({ - bool: { - must: [], - must_not: [] - } - }); - }); - - }); - -}); - - diff --git a/src/core_plugins/kibana/public/dashboard/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/dashboard_context.js deleted file mode 100644 index d3b4c64b327f5..0000000000000 --- a/src/core_plugins/kibana/public/dashboard/dashboard_context.js +++ /dev/null @@ -1,60 +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. - */ - -// This file is used by Timelion and TSVB -import _ from 'lodash'; -import { FilterBarQueryFilterProvider } from 'ui/filter_bar/query_filter'; -import 'ui/state_management/app_state'; -import { luceneStringToDsl, migrateFilter } from 'ui/courier'; - -export function dashboardContextProvider(Private, getAppState) { - return () => { - const appState = getAppState(); - const queryFilter = Private(FilterBarQueryFilterProvider); - const bool = { must: [], must_not: [] }; - if (!appState) { return { bool: bool }; } - const filterBarFilters = queryFilter.getFilters(); - const queryBarQuery = appState.query; - - if (queryBarQuery.language === 'lucene') { - // Add the query bar filter, its handled differently. - const query = luceneStringToDsl(queryBarQuery.query); - if (query) { bool.must.push(query); } - } - - // Add each of the filter bar filters - _.each(filterBarFilters, function (filter) { - const esFilter = _.omit(filter, function (val, key) { - if (key === 'meta' || key[0] === '$') { return true; } - return false; - }); - - if (filter.meta.disabled) { return; } - if (filter.meta.negate) { - bool.must_not = bool.must_not || []; - if (esFilter.query || esFilter) { bool.must_not.push(migrateFilter(esFilter.query || esFilter)); } - } else { - bool.must = bool.must || []; - if (esFilter.query || esFilter) { bool.must.push(migrateFilter(esFilter.query || esFilter)); } - } - }); - - return { bool: bool }; - }; -} diff --git a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js index 5889c5a7d7af9..f20c998b845ba 100644 --- a/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js +++ b/src/core_plugins/metrics/public/kbn_vis_types/request_handler.js @@ -18,17 +18,17 @@ */ import { validateInterval } from '../lib/validate_interval'; -import { dashboardContextProvider } from 'plugins/kibana/dashboard/dashboard_context'; import { timezoneProvider } from 'ui/vis/lib/timezone'; import { timefilter } from 'ui/timefilter'; +import { BuildESQueryProvider } from 'ui/courier'; const MetricsRequestHandlerProvider = function (Private, Notifier, config, $http) { - const dashboardContext = Private(dashboardContextProvider); const notify = new Notifier({ location: 'Metrics' }); + const buildEsQuery = Private(BuildESQueryProvider); return { name: 'metrics', - handler: function (vis, { uiState, timeRange }) { + handler: function (vis, { uiState, timeRange, filters, query }) { const timezone = Private(timezoneProvider)(); return new Promise((resolve) => { const panel = vis.params; @@ -39,7 +39,7 @@ const MetricsRequestHandlerProvider = function (Private, Notifier, config, $http if (panel && panel.id) { const params = { timerange: { timezone, ...parsedTimeRange }, - filters: [dashboardContext()], + filters: [buildEsQuery(vis.indexPattern, [query], filters)], panels: [panel], state: uiStateObj }; diff --git a/src/core_plugins/timelion/public/vis/timelion_request_handler.js b/src/core_plugins/timelion/public/vis/timelion_request_handler.js index d7f54566b208a..f79332c561448 100644 --- a/src/core_plugins/timelion/public/vis/timelion_request_handler.js +++ b/src/core_plugins/timelion/public/vis/timelion_request_handler.js @@ -18,12 +18,12 @@ */ import _ from 'lodash'; -import { dashboardContextProvider } from 'plugins/kibana/dashboard/dashboard_context'; - +import { BuildESQueryProvider } from 'ui/courier'; import { timezoneProvider } from 'ui/vis/lib/timezone'; + const TimelionRequestHandlerProvider = function (Private, Notifier, $http) { const timezone = Private(timezoneProvider)(); - const dashboardContext = Private(dashboardContextProvider); + const buildEsQuery = Private(BuildESQueryProvider); const notify = new Notifier({ location: 'Timelion' @@ -31,7 +31,7 @@ const TimelionRequestHandlerProvider = function (Private, Notifier, $http) { return { name: 'timelion', - handler: function (vis, { timeRange }) { + handler: function (vis, { timeRange, filters, query }) { return new Promise((resolve, reject) => { const expression = vis.params.expression; @@ -41,7 +41,7 @@ const TimelionRequestHandlerProvider = function (Private, Notifier, $http) { sheet: [expression], extended: { es: { - filter: dashboardContext() + filter: buildEsQuery(vis.indexPattern, [query], filters) } }, time: _.extend(timeRange, { diff --git a/src/core_plugins/vega/public/data_model/es_query_parser.js b/src/core_plugins/vega/public/data_model/es_query_parser.js index 5d94afd688b24..ebd12df0c618b 100644 --- a/src/core_plugins/vega/public/data_model/es_query_parser.js +++ b/src/core_plugins/vega/public/data_model/es_query_parser.js @@ -35,10 +35,10 @@ const TIMEFIELD = '%timefield%'; */ export class EsQueryParser { - constructor(timeCache, searchCache, dashboardContext, onWarning) { + constructor(timeCache, searchCache, filters, onWarning) { this._timeCache = timeCache; this._searchCache = searchCache; - this._dashboardContext = dashboardContext; + this._filters = filters; this._onWarning = onWarning; } @@ -134,7 +134,7 @@ export class EsQueryParser { if (context) { // Use dashboard context - const newQuery = this._dashboardContext(); + const newQuery = _.cloneDeep(this._filters); if (timefield) { newQuery.bool.must.push(body.query); } @@ -174,7 +174,7 @@ export class EsQueryParser { const item = obj[pos]; if (isQuery && (item === MUST_CLAUSE || item === MUST_NOT_CLAUSE)) { const ctxTag = item === MUST_CLAUSE ? 'must' : 'must_not'; - const ctx = this._dashboardContext(); + const ctx = _.cloneDeep(this._filters); if (ctx && ctx.bool && ctx.bool[ctxTag]) { if (Array.isArray(ctx.bool[ctxTag])) { // replace one value with an array of values diff --git a/src/core_plugins/vega/public/data_model/vega_parser.js b/src/core_plugins/vega/public/data_model/vega_parser.js index 132e84067ab0f..8f5d4fc6d3c4d 100644 --- a/src/core_plugins/vega/public/data_model/vega_parser.js +++ b/src/core_plugins/vega/public/data_model/vega_parser.js @@ -47,7 +47,7 @@ const DEFAULT_PARSER = 'elasticsearch'; export class VegaParser { - constructor(spec, searchCache, timeCache, dashboardContext, serviceSettings) { + constructor(spec, searchCache, timeCache, filters, serviceSettings) { this.spec = spec; this.hideWarnings = false; this.error = undefined; @@ -55,7 +55,7 @@ export class VegaParser { const onWarn = this._onWarning.bind(this); this._urlParsers = { - elasticsearch: new EsQueryParser(timeCache, searchCache, dashboardContext, onWarn), + elasticsearch: new EsQueryParser(timeCache, searchCache, filters, onWarn), emsfile: new EmsFileParser(serviceSettings), url: new UrlParser(onWarn), }; diff --git a/src/core_plugins/vega/public/vega_request_handler.js b/src/core_plugins/vega/public/vega_request_handler.js index 22507d7b67bf4..f65c5b95f4c57 100644 --- a/src/core_plugins/vega/public/vega_request_handler.js +++ b/src/core_plugins/vega/public/vega_request_handler.js @@ -18,24 +18,25 @@ */ import { VegaParser } from './data_model/vega_parser'; -import { dashboardContextProvider } from 'plugins/kibana/dashboard/dashboard_context'; import { SearchCache } from './data_model/search_cache'; import { TimeCache } from './data_model/time_cache'; import { timefilter } from 'ui/timefilter'; +import { BuildESQueryProvider } from 'ui/courier'; export function VegaRequestHandlerProvider(Private, es, serviceSettings) { - - const dashboardContext = Private(dashboardContextProvider); + const buildEsQuery = Private(BuildESQueryProvider); const searchCache = new SearchCache(es, { max: 10, maxAge: 4 * 1000 }); const timeCache = new TimeCache(timefilter, 3 * 1000); + return { name: 'vega', - handler(vis, { timeRange }) { + handler(vis, { timeRange, filters, query }) { timeCache.setTimeRange(timeRange); - const vp = new VegaParser(vis.params.spec, searchCache, timeCache, dashboardContext, serviceSettings); + const filtersDsl = buildEsQuery(vis.indexPattern, [query], filters); + const vp = new VegaParser(vis.params.spec, searchCache, timeCache, filtersDsl, serviceSettings); return vp.parseAsync(); } diff --git a/src/ui/public/courier/index.js b/src/ui/public/courier/index.js index 244042317ebf2..7261ee5a91298 100644 --- a/src/ui/public/courier/index.js +++ b/src/ui/public/courier/index.js @@ -27,6 +27,7 @@ export { decorateQuery, buildQueryFromFilters, luceneStringToDsl, + BuildESQueryProvider } from './search_source'; export { diff --git a/src/ui/public/courier/search_source/index.js b/src/ui/public/courier/search_source/index.js index 3f97b32f9f08a..0714dfabd31d3 100644 --- a/src/ui/public/courier/search_source/index.js +++ b/src/ui/public/courier/search_source/index.js @@ -20,4 +20,4 @@ export { SearchSourceProvider } from './search_source'; export { migrateFilter } from './migrate_filter'; export { decorateQuery } from './decorate_query'; -export { buildQueryFromFilters, luceneStringToDsl } from './build_query'; +export { buildQueryFromFilters, luceneStringToDsl, BuildESQueryProvider } from './build_query';