Skip to content

Commit

Permalink
don't assume that desiredSize will always be set
Browse files Browse the repository at this point in the history
Fixes #5642
  • Loading branch information
spalger authored and epixa committed Dec 11, 2015
1 parent 4bff792 commit 875d445
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import ngMock from 'ngMock';
import expect from 'expect.js';
import { times } from 'lodash';
import sinon from 'auto-release-sinon';

import HitSortFnProv from 'plugins/kibana/discover/_hit_sort_fn';
import NoDigestPromises from 'testUtils/noDigestPromises';

describe('Segmented Request Size Picking', function () {
let Promise;
let $rootScope;
let SegmentedReq;
let MockSource;
let HitSortFn;

NoDigestPromises.activateForSuite();

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject((Private, $injector) => {
Promise = $injector.get('Promise');
HitSortFn = Private(HitSortFnProv);
$rootScope = $injector.get('$rootScope');
SegmentedReq = Private(require('ui/courier/fetch/request/segmented'));

const StubbedSearchSourceProvider = require('fixtures/stubbed_search_source');
MockSource = class {
constructor() {
return $injector.invoke(StubbedSearchSourceProvider);
}
};
}));

context('without a size', function () {
it('does not set the request size', async function () {
const req = new SegmentedReq(new MockSource());
req._handle.setDirection('desc');
req._handle.setSortFn(new HitSortFn('desc'));
await req.start();

expect((await req.getFetchParams()).body).to.not.have.property('size');
});
});

context('with a size', function () {
it('sets the request size to the entire desired size', async function () {
const req = new SegmentedReq(new MockSource());
req._handle.setDirection('desc');
req._handle.setSize(555);
req._handle.setSortFn(new HitSortFn('desc'));
await req.start();

expect((await req.getFetchParams()).body).to.have.property('size', 555);
});
});
});
23 changes: 14 additions & 9 deletions src/ui/public/courier/fetch/request/segmented.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ define(function (require) {

var indices = self._active = self._queue.splice(0, indexCount);
params.index = _.pluck(indices, 'index');
params.body.size = self._pickSizeForIndices(indices);

if (self._desiredSize) {
params.body.size = self._pickSizeForIndices(indices);
}

if (params.body.size === 0) params.search_type = 'count';

Expand Down Expand Up @@ -235,7 +238,9 @@ define(function (require) {
});
}

mergedHits = self._mergedResp.hits.hits = mergedHits.slice(0, desiredSize);
if (desiredSize) {
mergedHits = self._mergedResp.hits.hits = mergedHits.slice(0, desiredSize);
}
};

SegmentedReq.prototype._mergeSegment = notify.timed('merge response segment', function (seg) {
Expand Down Expand Up @@ -284,9 +289,10 @@ define(function (require) {
SegmentedReq.prototype._detectHitsWindow = function (hits) {
hits = hits || [];
var indexPattern = this.source.get('index');
var desiredSize = this._desiredSize;

var size = _.size(hits);
if (size < this._desiredSize) {
if (!desiredSize || size < desiredSize) {
this._hitWindow = {
size: size,
min: -Infinity,
Expand All @@ -310,20 +316,19 @@ define(function (require) {

SegmentedReq.prototype._pickSizeForIndices = function (indices) {
var hitWindow = this._hitWindow;
var desiredSize = this._desiredSize;

if (!desiredSize) return undefined;
// we don't have any hits yet, get us more info!
if (!hitWindow) return this._desiredSize;

if (!hitWindow) return desiredSize;
// the order of documents isn't important, just get us more
if (!this._sortFn) return Math.max(this._desiredSize - hitWindow.size, 0);

if (!this._sortFn) return Math.max(desiredSize - hitWindow.size, 0);
// if all of the documents in every index fall outside of our current doc set, we can ignore them.
var someOverlap = indices.some(function (index) {
return index.min <= hitWindow.max && hitWindow.min <= index.max;
});

if (someOverlap) return this._desiredSize;
else return 0;
return someOverlap ? desiredSize : 0;
};

return SegmentedReq;
Expand Down

0 comments on commit 875d445

Please sign in to comment.