Skip to content

Commit

Permalink
Fix issue with overwritten filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Roes committed Apr 16, 2018
1 parent 33262a7 commit ead1d1e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/ui/public/courier/data_source/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {

this.history = [];
this._requestStartHandlers = [];
this._inheritOptions = {};

this._filterPredicates = [
(filter) => {
Expand Down Expand Up @@ -197,8 +198,9 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
* @param {SearchSource} searchSource - the parent searchSource
* @return {this} - chainable
*/
inherits(parent) {
inherits(parent, options = {}) {
this._parent = parent;
this._inheritOptions = options;
return this;
}

Expand Down Expand Up @@ -458,8 +460,19 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
this.activeFetchCount = (this.activeFetchCount || 0) + 1;
this.history = [request];

const handlers = [...this._requestStartHandlers];
// If callparentStartHandlers has been set to true, we also call all
// handlers of parent search sources.
if (this._inheritOptions.callParentStartHandlers) {
let searchSource = this.getParent();
while (searchSource) {
handlers.push(...searchSource._requestStartHandlers);
searchSource = searchSource.getParent();
}
}

return Promise
.map(this._requestStartHandlers, fn => fn(this, request))
.map(handlers, fn => fn(this, request))
.then(_.noop);
}

Expand Down
29 changes: 25 additions & 4 deletions src/ui/public/vis/request_handlers/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,32 @@ const CourierRequestHandlerProvider = function (Private, courier, timefilter) {
name: 'courier',
handler: function (vis, { appState, queryFilter, searchSource, timeRange }) {

searchSource.filter(() => {
// Create a new search source that inherits the original search source
// but has the propriate timeRange applied via a filter.
// This is a temporary solution until we properly pass down all required
// information for the request to the request handler (https://github.com/elastic/kibana/issues/16641).
// Using callParentStartHandlers: true we make sure, that the parent searchSource
// onSearchRequestStart will be called properly even though we use an inherited
// search source.
const requestSearchSource = new SearchSource().inherits(searchSource, { callParentStartHandlers: true });

// For now we need to mirror the history of the passed search source, since
// the spy panel wouldn't work otherwise.
Object.defineProperty(requestSearchSource, 'history', {
get() {
return requestSearchSource._parent.history;
},
set(history) {
return requestSearchSource._parent.history = history;
}
});

// Add the explicit passed timeRange as a filter to the requestSearchSource.
requestSearchSource.filter(() => {
return timefilter.get(searchSource.index(), timeRange);
});

removeSearchSourceParentTimefilter(searchSource, timeRange);
removeSearchSourceParentTimefilter(requestSearchSource);

if (queryFilter && vis.editorMode) {
searchSource.set('filter', queryFilter.getFilters());
Expand Down Expand Up @@ -75,7 +96,7 @@ const CourierRequestHandlerProvider = function (Private, courier, timefilter) {
return new Promise((resolve, reject) => {
if (shouldQuery()) {
delete vis.reload;
searchSource.onResults().then(resp => {
requestSearchSource.onResults().then(resp => {
searchSource.lastQuery = {
filter: _.cloneDeep(searchSource.get('filter')),
query: _.cloneDeep(searchSource.get('query')),
Expand All @@ -88,7 +109,7 @@ const CourierRequestHandlerProvider = function (Private, courier, timefilter) {
return _.cloneDeep(resp);
}).then(async resp => {
for (const agg of vis.getAggConfig()) {
const nestedSearchSource = new SearchSource().inherits(searchSource);
const nestedSearchSource = new SearchSource().inherits(requestSearchSource);
resp = await agg.type.postFlightRequest(resp, vis.aggs, agg, nestedSearchSource);
}

Expand Down

0 comments on commit ead1d1e

Please sign in to comment.