Skip to content

Commit

Permalink
[courier/searchSource] auto add source filter for index pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger authored and scampi committed Jun 2, 2016
1 parent 9d70208 commit 5a97b6b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 8 deletions.
95 changes: 95 additions & 0 deletions src/ui/public/courier/data_source/__tests__/search_source.js
Expand Up @@ -4,17 +4,25 @@ import sinon from 'auto-release-sinon';

import RequestQueueProv from '../../_request_queue';
import SearchSourceProv from '../search_source';
import StubIndexPatternProv from 'testUtils/stub_index_pattern';

describe('SearchSource', function () {
require('test_utils/no_digest_promises').activateForSuite();

let requestQueue;
let SearchSource;
let indexPattern;
let indexPattern2;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
requestQueue = Private(RequestQueueProv);
SearchSource = Private(SearchSourceProv);

const IndexPattern = Private(StubIndexPatternProv);
indexPattern = new IndexPattern('test-*', null, []);
indexPattern2 = new IndexPattern('test2-*', null, []);
expect(indexPattern).to.not.be(indexPattern2);
}));

describe('#onResults()', function () {
Expand Down Expand Up @@ -56,4 +64,91 @@ describe('SearchSource', function () {
expect(requestQueue).to.have.length(0);
});
});

describe('#index()', function () {
describe('auto-sourceFiltering', function () {
context('new index pattern assigned', function () {
it('generates a source filter', function () {
const source = new SearchSource();
expect(source.get('index')).to.be(undefined);
expect(source.get('source')).to.be(undefined);
source.set('index', indexPattern);
expect(source.get('index')).to.be(indexPattern);
expect(source.get('source')).to.be.a('function');
});

it('removes created source filter on removal', function () {
const source = new SearchSource();
source.set('index', indexPattern);
source.set('index', null);
expect(source.get('index')).to.be(undefined);
expect(source.get('source')).to.be(undefined);
});
});

context('new index pattern assigned over another', function () {
it('replaces source filter with new', function () {
const source = new SearchSource();
source.set('index', indexPattern);
const sourceFilter1 = source.get('source');
source.set('index', indexPattern2);
expect(source.get('index')).to.be(indexPattern2);
expect(source.get('source')).to.be.a('function');
expect(source.get('source')).to.not.be(sourceFilter1);
});

it('removes created source filter on removal', function () {
const source = new SearchSource();
source.set('index', indexPattern);
source.set('index', indexPattern2);
source.set('index', null);
expect(source.get('index')).to.be(undefined);
expect(source.get('source')).to.be(undefined);
});
});

context('ip assigned before custom source filter', function () {
it('custom source filter becomes new source', function () {
const source = new SearchSource();
const football = {};
source.set('index', indexPattern);
expect(source.get('source')).to.be.a('function');
source.set('source', football);
expect(source.get('index')).to.be(indexPattern);
expect(source.get('source')).to.be(football);
});

it('custom source stays after removal', function () {
const source = new SearchSource();
const football = {};
source.set('index', indexPattern);
source.set('source', football);
source.set('index', null);
expect(source.get('index')).to.be(undefined);
expect(source.get('source')).to.be(football);
});
});

context('ip assigned after custom source filter', function () {
it('leaves the custom filter in place', function () {
const source = new SearchSource();
const football = {};
source.set('source', football);
source.set('index', indexPattern);
expect(source.get('index')).to.be(indexPattern);
expect(source.get('source')).to.be(football);
});

it('custom source stays after removal', function () {
const source = new SearchSource();
const football = {};
source.set('source', football);
source.set('index', indexPattern);
source.set('index', null);
expect(source.get('index')).to.be(undefined);
expect(source.get('source')).to.be(football);
});
});
});
});
});
32 changes: 28 additions & 4 deletions src/ui/public/courier/data_source/search_source.js
Expand Up @@ -14,6 +14,12 @@ export default function SearchSourceFactory(Promise, Private) {
let searchStrategy = Private(SearchStrategyProvider);
let normalizeSortRequest = Private(NormalizeSortRequestProvider);

let forIp = Symbol('for which index pattern?');

function isIndexPattern(val) {
return Boolean(val && typeof val.toIndexList === 'function');
}

_.class(SearchSource).inherits(SourceAbstract);
function SearchSource(initialState) {
SearchSource.Super.call(this, initialState, searchStrategy);
Expand Down Expand Up @@ -42,13 +48,31 @@ export default function SearchSourceFactory(Promise, Private) {
];

SearchSource.prototype.index = function (indexPattern) {
if (indexPattern === undefined) return this._state.index;
if (indexPattern === null) return delete this._state.index;
if (!indexPattern || typeof indexPattern.toIndexList !== 'function') {
let state = this._state;

let hasSource = state.source;
let sourceCameFromIp = hasSource && state.source.hasOwnProperty(forIp);
let sourceIsForOurIp = sourceCameFromIp && state.source[forIp] === state.index;
if (sourceIsForOurIp) {
delete state.source;
}

if (indexPattern === undefined) return state.index;
if (indexPattern === null) return delete state.index;
if (!isIndexPattern(indexPattern)) {
throw new TypeError('expected indexPattern to be an IndexPattern duck.');
}

this._state.index = indexPattern;
state.index = indexPattern;
if (!state.source) {
// imply source filtering based on the index pattern, but allow overriding
// it by simply setting another value for "source". When index is changed
state.source = function () {
return indexPattern.getSourceFiltering();
};
state.source[forIp] = indexPattern;
}

return this;
};

Expand Down
4 changes: 0 additions & 4 deletions src/ui/public/doc_table/doc_table.js
Expand Up @@ -80,10 +80,6 @@ uiModules.get('kibana')

$scope.searchSource.size(config.get('discover:sampleSize'));
$scope.searchSource.sort(getSort($scope.sorting, $scope.indexPattern));
const sourceFiltering = $scope.indexPattern.getSourceFiltering($scope.columns);
if (sourceFiltering) {
$scope.searchSource.source(sourceFiltering);
}

// Set the watcher after initialization
$scope.$watchCollection('sorting', function (newSort, oldSort) {
Expand Down

0 comments on commit 5a97b6b

Please sign in to comment.