diff --git a/src/ui/public/courier/fetch/__tests__/doc.js b/src/ui/public/courier/fetch/__tests__/doc.js index 25c7e78677912..6ce8059c14ba1 100644 --- a/src/ui/public/courier/fetch/__tests__/doc.js +++ b/src/ui/public/courier/fetch/__tests__/doc.js @@ -1,22 +1,22 @@ import sinon from 'auto-release-sinon'; import expect from 'expect.js'; import ngMock from 'ngMock'; -import CourierDataSourceDocSourceProvider from 'ui/courier/data_source/doc_source'; -import CourierFetchRequestDocProvider from 'ui/courier/fetch/request/doc'; -describe('Courier DocFetchRequest class', function () { +import DocSourceProvider from '../../data_source/doc_source'; +import DocRequestProvider from '../request/doc'; - var storage; - var source; - var defer; - var req; +describe('Courier DocFetchRequest class', function () { + let storage; + let source; + let defer; + let req; - var setVersion; + let setVersion; beforeEach(ngMock.module('kibana')); beforeEach(ngMock.inject(function (Private, Promise, $injector) { - var DocSource = Private(CourierDataSourceDocSourceProvider); - var DocFetchRequest = Private(CourierFetchRequestDocProvider); + const DocSource = Private(DocSourceProvider); + const DocFetchRequest = Private(DocRequestProvider); storage = $injector.get('localStorage').store = diff --git a/src/ui/public/courier/fetch/__tests__/fetch.js b/src/ui/public/courier/fetch/__tests__/fetch.js index dad88dd6c93de..1c5c6b107c9a4 100644 --- a/src/ui/public/courier/fetch/__tests__/fetch.js +++ b/src/ui/public/courier/fetch/__tests__/fetch.js @@ -2,11 +2,12 @@ import ngMock from 'ngMock'; import expect from 'expect.js'; import sinon from 'auto-release-sinon'; -import FetchProvider from 'ui/courier/fetch'; import IndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; import searchResp from 'fixtures/search_response'; -import CourierDataSourceDocSourceProvider from 'ui/courier/data_source/doc_source'; -import CourierDataSourceSearchSourceProvider from 'ui/courier/data_source/search_source'; + +import FetchProvider from '../fetch'; +import DocSourceProvider from '../../data_source/doc_source'; +import SearchSourceProvider from '../../data_source/search_source'; describe('Fetch service', function () { require('testUtils/noDigestPromises').activateForSuite(); @@ -24,8 +25,8 @@ describe('Fetch service', function () { Promise = $injector.get('Promise'); fetch = Private(FetchProvider); indexPattern = Private(IndexPatternProvider); - DocSource = Private(CourierDataSourceDocSourceProvider); - SearchSource = Private(CourierDataSourceSearchSourceProvider); + DocSource = Private(DocSourceProvider); + SearchSource = Private(SearchSourceProvider); })); describe('#doc(docSource)', function () { diff --git a/src/ui/public/courier/fetch/__tests__/fetch_these.js b/src/ui/public/courier/fetch/__tests__/fetch_these.js index 57d085394bb02..513373af11b7f 100644 --- a/src/ui/public/courier/fetch/__tests__/fetch_these.js +++ b/src/ui/public/courier/fetch/__tests__/fetch_these.js @@ -1,8 +1,9 @@ -import _ from 'lodash'; import sinon from 'auto-release-sinon'; import expect from 'expect.js'; import ngMock from 'ngMock'; -import CourierFetchFetchTheseProvider from 'ui/courier/fetch/_fetch_these'; + +import FetchTheseProvider from '../fetch_these'; + describe('ui/courier/fetch/_fetch_these', () => { let Promise; @@ -22,15 +23,15 @@ describe('ui/courier/fetch/_fetch_these', () => { return fakeResponses; } - PrivateProvider.swap(require('ui/courier/fetch/_call_client'), FakeResponsesProvider); - PrivateProvider.swap(require('ui/courier/fetch/_call_response_handlers'), FakeResponsesProvider); - PrivateProvider.swap(require('ui/courier/fetch/_continue_incomplete'), FakeResponsesProvider); + PrivateProvider.swap(require('ui/courier/fetch/call_client'), FakeResponsesProvider); + PrivateProvider.swap(require('ui/courier/fetch/call_response_handlers'), FakeResponsesProvider); + PrivateProvider.swap(require('ui/courier/fetch/continue_incomplete'), FakeResponsesProvider); })); beforeEach(ngMock.inject((Private, $injector) => { $rootScope = $injector.get('$rootScope'); Promise = $injector.get('Promise'); - fetchThese = Private(CourierFetchFetchTheseProvider); + fetchThese = Private(FetchTheseProvider); request = mockRequest(); requests = [ request ]; })); diff --git a/src/ui/public/courier/fetch/_is_request.js b/src/ui/public/courier/fetch/_is_request.js deleted file mode 100644 index 5f85fede43456..0000000000000 --- a/src/ui/public/courier/fetch/_is_request.js +++ /dev/null @@ -1,9 +0,0 @@ -import CourierFetchRequestRequestProvider from 'ui/courier/fetch/request/request'; - -export default function CourierFetchIsRequestProvider(Private) { - var AbstractRequest = Private(CourierFetchRequestRequestProvider); - - return function isRequest(obj) { - return obj instanceof AbstractRequest; - }; -}; diff --git a/src/ui/public/courier/fetch/_call_client.js b/src/ui/public/courier/fetch/call_client.js similarity index 77% rename from src/ui/public/courier/fetch/_call_client.js rename to src/ui/public/courier/fetch/call_client.js index db21eaba6cb7b..2157e4420d958 100644 --- a/src/ui/public/courier/fetch/_call_client.js +++ b/src/ui/public/courier/fetch/call_client.js @@ -1,29 +1,31 @@ import _ from 'lodash'; -import CourierFetchIsRequestProvider from 'ui/courier/fetch/_is_request'; -import CourierFetchMergeDuplicateRequestsProvider from 'ui/courier/fetch/_merge_duplicate_requests'; -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; + +import IsRequestProvider from './is_request'; +import MergeDuplicatesRequestProvider from './merge_duplicate_requests'; +import ReqStatusProvider from './req_status'; + export default function CourierFetchCallClient(Private, Promise, es, esShardTimeout, sessionId) { - var isRequest = Private(CourierFetchIsRequestProvider); - var mergeDuplicateRequests = Private(CourierFetchMergeDuplicateRequestsProvider); + const isRequest = Private(IsRequestProvider); + const mergeDuplicateRequests = Private(MergeDuplicatesRequestProvider); - var ABORTED = Private(CourierFetchReqStatusProvider).ABORTED; - var DUPLICATE = Private(CourierFetchReqStatusProvider).DUPLICATE; + const ABORTED = Private(ReqStatusProvider).ABORTED; + const DUPLICATE = Private(ReqStatusProvider).DUPLICATE; function callClient(strategy, requests) { // merging docs can change status to DUPLICATE, capture new statuses - var statuses = mergeDuplicateRequests(requests); + const statuses = mergeDuplicateRequests(requests); // get the actual list of requests that we will be fetching - var executable = statuses.filter(isRequest); - var execCount = executable.length; + const executable = statuses.filter(isRequest); + let execCount = executable.length; // resolved by respond() - var esPromise; - var defer = Promise.defer(); + let esPromise; + const defer = Promise.defer(); // for each respond with either the response or ABORTED - var respond = function (responses) { + const respond = function (responses) { responses = responses || []; return Promise.map(requests, function (req, i) { switch (statuses[i]) { @@ -43,7 +45,7 @@ export default function CourierFetchCallClient(Private, Promise, es, esShardTime // handle a request being aborted while being fetched - var requestWasAborted = Promise.method(function (req, i) { + const requestWasAborted = Promise.method(function (req, i) { if (statuses[i] === ABORTED) { defer.reject(new Error('Request was aborted twice?')); } diff --git a/src/ui/public/courier/fetch/_call_response_handlers.js b/src/ui/public/courier/fetch/call_response_handlers.js similarity index 68% rename from src/ui/public/courier/fetch/_call_response_handlers.js rename to src/ui/public/courier/fetch/call_response_handlers.js index 3dfcd33793729..41b4c2bc20205 100644 --- a/src/ui/public/courier/fetch/_call_response_handlers.js +++ b/src/ui/public/courier/fetch/call_response_handlers.js @@ -1,12 +1,12 @@ -import { SearchTimeout } from 'ui/errors'; -import { RequestFailure } from 'ui/errors'; -import { ShardFailure } from 'ui/errors'; -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; -import CourierFetchNotifierProvider from 'ui/courier/fetch/_notifier'; +import { RequestFailure, SearchTimeout, ShardFailure } from 'ui/errors'; + +import ReqStatusProvider from './req_status'; +import NotifierProvider from './notifier'; + export default function CourierFetchCallResponseHandlers(Private, Promise) { - var ABORTED = Private(CourierFetchReqStatusProvider).ABORTED; - var INCOMPLETE = Private(CourierFetchReqStatusProvider).INCOMPLETE; - var notify = Private(CourierFetchNotifierProvider); + const ABORTED = Private(ReqStatusProvider).ABORTED; + const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE; + const notify = Private(NotifierProvider); function callResponseHandlers(requests, responses) { @@ -15,7 +15,7 @@ export default function CourierFetchCallResponseHandlers(Private, Promise) { return ABORTED; } - var resp = responses[i]; + let resp = responses[i]; if (resp.timed_out) { notify.warning(new SearchTimeout()); diff --git a/src/ui/public/courier/fetch/_continue_incomplete.js b/src/ui/public/courier/fetch/continue_incomplete.js similarity index 79% rename from src/ui/public/courier/fetch/_continue_incomplete.js rename to src/ui/public/courier/fetch/continue_incomplete.js index bff84e5ef4a9b..12433548a32e7 100644 --- a/src/ui/public/courier/fetch/_continue_incomplete.js +++ b/src/ui/public/courier/fetch/continue_incomplete.js @@ -1,10 +1,10 @@ -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; +import ReqStatusProvider from './req_status'; export default function CourierFetchContinueIncompleteRequests(Private) { - var INCOMPLETE = Private(CourierFetchReqStatusProvider).INCOMPLETE; + const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE; function continueIncompleteRequests(strategy, requests, responses, fetchWithStrategy) { - var incomplete = []; + const incomplete = []; responses.forEach(function (resp, i) { if (resp === INCOMPLETE) { diff --git a/src/ui/public/courier/fetch/fetch.js b/src/ui/public/courier/fetch/fetch.js index 4f34135820d42..f6bf27754073f 100644 --- a/src/ui/public/courier/fetch/fetch.js +++ b/src/ui/public/courier/fetch/fetch.js @@ -1,18 +1,20 @@ import _ from 'lodash'; -import CourierRequestQueueProvider from 'ui/courier/_request_queue'; -import CourierFetchFetchTheseProvider from 'ui/courier/fetch/_fetch_these'; -import CourierFetchCallResponseHandlersProvider from 'ui/courier/fetch/_call_response_handlers'; -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; + +import RequestQueueProvider from '../_request_queue'; +import FetchTheseProvider from './fetch_these'; +import CallResponseHandlersProvider from './call_response_handlers'; +import ReqStatusProvider from './req_status'; + export default function fetchService(Private, Promise) { - var requestQueue = Private(CourierRequestQueueProvider); - var fetchThese = Private(CourierFetchFetchTheseProvider); + const requestQueue = Private(RequestQueueProvider); + const fetchThese = Private(FetchTheseProvider); - var callResponseHandlers = Private(CourierFetchCallResponseHandlersProvider); - var INCOMPLETE = Private(CourierFetchReqStatusProvider).INCOMPLETE; + const callResponseHandlers = Private(CallResponseHandlersProvider); + const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE; function fetchQueued(strategy) { - var requests = requestQueue.getStartable(strategy); + const requests = requestQueue.getStartable(strategy); if (!requests.length) return Promise.resolve(); else return fetchThese(requests); } @@ -20,7 +22,7 @@ export default function fetchService(Private, Promise) { this.fetchQueued = fetchQueued; function fetchASource(source, strategy) { - var defer = Promise.defer(); + const defer = Promise.defer(); fetchThese([ source._createRequest(defer) diff --git a/src/ui/public/courier/fetch/_fetch_these.js b/src/ui/public/courier/fetch/fetch_these.js similarity index 58% rename from src/ui/public/courier/fetch/_fetch_these.js rename to src/ui/public/courier/fetch/fetch_these.js index 006f273f7880e..78affdf29ac53 100644 --- a/src/ui/public/courier/fetch/_fetch_these.js +++ b/src/ui/public/courier/fetch/fetch_these.js @@ -1,22 +1,22 @@ -import CourierFetchNotifierProvider from 'ui/courier/fetch/_notifier'; -import CourierFetchForEachStrategyProvider from 'ui/courier/fetch/_for_each_strategy'; -import CourierFetchCallClientProvider from 'ui/courier/fetch/_call_client'; -import CourierFetchCallResponseHandlersProvider from 'ui/courier/fetch/_call_response_handlers'; -import CourierFetchContinueIncompleteProvider from 'ui/courier/fetch/_continue_incomplete'; -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; +import NotifierProvider from './notifier'; +import ForEachStrategyProvider from './for_each_strategy'; +import CallClientProvider from './call_client'; +import CallResponseHandlersProvider from './call_response_handlers'; +import ContinueIncompleteProvider from './continue_incomplete'; +import ReqStatusProvider from './req_status'; export default function FetchTheseProvider(Private, Promise) { - var notify = Private(CourierFetchNotifierProvider); - var forEachStrategy = Private(CourierFetchForEachStrategyProvider); + const notify = Private(NotifierProvider); + const forEachStrategy = Private(ForEachStrategyProvider); // core tasks - var callClient = Private(CourierFetchCallClientProvider); - var callResponseHandlers = Private(CourierFetchCallResponseHandlersProvider); - var continueIncomplete = Private(CourierFetchContinueIncompleteProvider); + const callClient = Private(CallClientProvider); + const callResponseHandlers = Private(CallResponseHandlersProvider); + const continueIncomplete = Private(ContinueIncompleteProvider); - var ABORTED = Private(CourierFetchReqStatusProvider).ABORTED; - var DUPLICATE = Private(CourierFetchReqStatusProvider).DUPLICATE; - var INCOMPLETE = Private(CourierFetchReqStatusProvider).INCOMPLETE; + const ABORTED = Private(ReqStatusProvider).ABORTED; + const DUPLICATE = Private(ReqStatusProvider).DUPLICATE; + const INCOMPLETE = Private(ReqStatusProvider).INCOMPLETE; function fetchThese(requests) { return forEachStrategy(requests, function (strategy, reqsForStrategy) { @@ -66,7 +66,7 @@ export default function FetchTheseProvider(Private, Promise) { } return new Promise(function (resolve) { - var action = req.started ? req.continue : req.start; + const action = req.started ? req.continue : req.start; resolve(action.call(req)); }) .catch(err => req.handleFailure(err)); diff --git a/src/ui/public/courier/fetch/_for_each_strategy.js b/src/ui/public/courier/fetch/for_each_strategy.js similarity index 80% rename from src/ui/public/courier/fetch/_for_each_strategy.js rename to src/ui/public/courier/fetch/for_each_strategy.js index 0bebb1a262b51..8eababd7670a5 100644 --- a/src/ui/public/courier/fetch/_for_each_strategy.js +++ b/src/ui/public/courier/fetch/for_each_strategy.js @@ -1,13 +1,13 @@ import _ from 'lodash'; -export default function FetchForEachRequestStrategy(Private, Promise) { +export default function FetchForEachRequestStrategy(Private, Promise) { function forEachStrategy(requests, block) { block = Promise.method(block); - var sets = []; + const sets = []; requests.forEach(function (req) { - var strategy = req.strategy; - var set = _.find(sets, { 0: strategy }); + const strategy = req.strategy; + const set = _.find(sets, { 0: strategy }); if (set) set[1].push(req); else sets.push([strategy, [req]]); }); diff --git a/src/ui/public/courier/fetch/is_request.js b/src/ui/public/courier/fetch/is_request.js new file mode 100644 index 0000000000000..f64ab1e1ec08d --- /dev/null +++ b/src/ui/public/courier/fetch/is_request.js @@ -0,0 +1,9 @@ +import AbstractRequestProvider from './request'; + +export default function IsRequestProvider(Private) { + const AbstractRequest = Private(AbstractRequestProvider); + + return function isRequest(obj) { + return obj instanceof AbstractRequest; + }; +}; diff --git a/src/ui/public/courier/fetch/_merge_duplicate_requests.js b/src/ui/public/courier/fetch/merge_duplicate_requests.js similarity index 60% rename from src/ui/public/courier/fetch/_merge_duplicate_requests.js rename to src/ui/public/courier/fetch/merge_duplicate_requests.js index 2f1d0c2daffb6..5c698f422834b 100644 --- a/src/ui/public/courier/fetch/_merge_duplicate_requests.js +++ b/src/ui/public/courier/fetch/merge_duplicate_requests.js @@ -1,17 +1,17 @@ -import CourierFetchIsRequestProvider from 'ui/courier/fetch/_is_request'; -import CourierFetchReqStatusProvider from 'ui/courier/fetch/_req_status'; +import IsRequestProvider from './is_request'; +import ReqStatusProvider from './req_status'; export default function FetchMergeDuplicateRequests(Private) { - var isRequest = Private(CourierFetchIsRequestProvider); - var DUPLICATE = Private(CourierFetchReqStatusProvider).DUPLICATE; + const isRequest = Private(IsRequestProvider); + const DUPLICATE = Private(ReqStatusProvider).DUPLICATE; function mergeDuplicateRequests(requests) { // dedupe requests - var index = {}; + const index = {}; return requests.map(function (req) { if (!isRequest(req)) return req; - var iid = req.source._instanceid; + const iid = req.source._instanceid; if (!index[iid]) { // this request is unique so far index[iid] = req; diff --git a/src/ui/public/courier/fetch/_notifier.js b/src/ui/public/courier/fetch/notifier.js similarity index 100% rename from src/ui/public/courier/fetch/_notifier.js rename to src/ui/public/courier/fetch/notifier.js diff --git a/src/ui/public/courier/fetch/_req_status.js b/src/ui/public/courier/fetch/req_status.js similarity index 100% rename from src/ui/public/courier/fetch/_req_status.js rename to src/ui/public/courier/fetch/req_status.js diff --git a/src/ui/public/courier/fetch/request/__tests__/segmented.js b/src/ui/public/courier/fetch/request/__tests__/segmented.js index 9f2a45d5ad8be..cabfae475a296 100644 --- a/src/ui/public/courier/fetch/request/__tests__/segmented.js +++ b/src/ui/public/courier/fetch/request/__tests__/segmented.js @@ -1,10 +1,11 @@ import sinon from 'auto-release-sinon'; import expect from 'expect.js'; import ngMock from 'ngMock'; -import CourierFetchRequestSegmentedProvider from 'ui/courier/fetch/request/segmented'; -import CourierFetchRequestSearchProvider from 'ui/courier/fetch/request/search'; -describe('ui/courier/fetch/request/segmented', () => { +import SegmentedRequestProvider from '../segmented'; +import SearchRequestProvider from '../search'; + +describe('ui/courier/fetch/request/segmented', () => { let Promise; let $rootScope; let SegmentedReq; @@ -16,8 +17,8 @@ describe('ui/courier/fetch/request/segmented', () => { beforeEach(ngMock.inject((Private, $injector) => { Promise = $injector.get('Promise'); $rootScope = $injector.get('$rootScope'); - SegmentedReq = Private(CourierFetchRequestSegmentedProvider); - searchReqStart = sinon.spy(Private(CourierFetchRequestSearchProvider).prototype, 'start'); + SegmentedReq = Private(SegmentedRequestProvider); + searchReqStart = sinon.spy(Private(SearchRequestProvider).prototype, 'start'); })); describe('#start()', () => { diff --git a/src/ui/public/courier/fetch/request/__tests__/segmented_create_queue.js b/src/ui/public/courier/fetch/request/__tests__/segmented_create_queue.js index c63cc5d0db325..e7a20f988090b 100644 --- a/src/ui/public/courier/fetch/request/__tests__/segmented_create_queue.js +++ b/src/ui/public/courier/fetch/request/__tests__/segmented_create_queue.js @@ -1,8 +1,11 @@ import sinon from 'auto-release-sinon'; import expect from 'expect.js'; import ngMock from 'ngMock'; + import StubbedSearchSourceProvider from 'fixtures/stubbed_search_source'; -import CourierFetchRequestSegmentedProvider from 'ui/courier/fetch/request/segmented'; + +import SegmentedRequestProvider from '../segmented'; + describe('ui/courier/fetch/request/segmented/_createQueue', () => { let Promise; @@ -16,7 +19,7 @@ describe('ui/courier/fetch/request/segmented/_createQueue', () => { beforeEach(ngMock.inject((Private, $injector) => { Promise = $injector.get('Promise'); $rootScope = $injector.get('$rootScope'); - SegmentedReq = Private(CourierFetchRequestSegmentedProvider); + SegmentedReq = Private(SegmentedRequestProvider); MockSource = class { constructor() { @@ -29,7 +32,7 @@ describe('ui/courier/fetch/request/segmented/_createQueue', () => { const req = new SegmentedReq(new MockSource()); req._queueCreated = null; - var promise = req._createQueue(); + const promise = req._createQueue(); expect(req._queueCreated).to.be(false); await promise; expect(req._queueCreated).to.be(true); diff --git a/src/ui/public/courier/fetch/request/__tests__/segmented_index_selection.js b/src/ui/public/courier/fetch/request/__tests__/segmented_index_selection.js index 336d7e9570839..89a2c1735a95c 100644 --- a/src/ui/public/courier/fetch/request/__tests__/segmented_index_selection.js +++ b/src/ui/public/courier/fetch/request/__tests__/segmented_index_selection.js @@ -6,7 +6,8 @@ import sinon from 'auto-release-sinon'; import HitSortFnProv from 'plugins/kibana/discover/_hit_sort_fn'; import NoDigestPromises from 'testUtils/noDigestPromises'; import StubbedSearchSourceProvider from 'fixtures/stubbed_search_source'; -import CourierFetchRequestSegmentedProvider from 'ui/courier/fetch/request/segmented'; + +import SegmentedRequestProvider from '../segmented'; describe('Segmented Request Index Selection', function () { let Promise; @@ -22,7 +23,7 @@ describe('Segmented Request Index Selection', function () { Promise = $injector.get('Promise'); HitSortFn = Private(HitSortFnProv); $rootScope = $injector.get('$rootScope'); - SegmentedReq = Private(CourierFetchRequestSegmentedProvider); + SegmentedReq = Private(SegmentedRequestProvider); MockSource = class { constructor() { diff --git a/src/ui/public/courier/fetch/request/__tests__/segmented_size_picking.js b/src/ui/public/courier/fetch/request/__tests__/segmented_size_picking.js index 7e40a44dea777..3339b21d2e925 100644 --- a/src/ui/public/courier/fetch/request/__tests__/segmented_size_picking.js +++ b/src/ui/public/courier/fetch/request/__tests__/segmented_size_picking.js @@ -6,7 +6,8 @@ import sinon from 'auto-release-sinon'; import HitSortFnProv from 'plugins/kibana/discover/_hit_sort_fn'; import NoDigestPromises from 'testUtils/noDigestPromises'; import StubbedSearchSourceProvider from 'fixtures/stubbed_search_source'; -import CourierFetchRequestSegmentedProvider from 'ui/courier/fetch/request/segmented'; + +import SegmentedRequestProvider from '../segmented'; describe('Segmented Request Size Picking', function () { let Promise; @@ -22,7 +23,7 @@ describe('Segmented Request Size Picking', function () { Promise = $injector.get('Promise'); HitSortFn = Private(HitSortFnProv); $rootScope = $injector.get('$rootScope'); - SegmentedReq = Private(CourierFetchRequestSegmentedProvider); + SegmentedReq = Private(SegmentedRequestProvider); MockSource = class { constructor() { diff --git a/src/ui/public/courier/fetch/request/_segmented_handle.js b/src/ui/public/courier/fetch/request/_segmented_handle.js deleted file mode 100644 index 64739964f782b..0000000000000 --- a/src/ui/public/courier/fetch/request/_segmented_handle.js +++ /dev/null @@ -1,29 +0,0 @@ -import _ from 'lodash'; -import EventsProvider from 'ui/events'; -export default function CourierSegmentedReqHandle(Private) { - var Events = Private(EventsProvider); - - - /** - * Simple class for creating an object to send to the - * requester of a SegmentedRequest. Since the SegmentedRequest - * extends AbstractRequest, it wasn't able to be the event - * emitter it was born to be. This provides a channel for - * setting values on the segmented request, and an event - * emitter for the request to speak outwardly - * - * @param {SegmentedRequest} - req - the requst this handle relates to - */ - _.class(SegmentedHandle).inherits(Events); - function SegmentedHandle(req) { - SegmentedHandle.Super.call(this); - - // export a couple methods from the request - this.setDirection = _.bindKey(req, 'setDirection'); - this.setSize = _.bindKey(req, 'setSize'); - this.setMaxSegments = _.bindKey(req, 'setMaxSegments'); - this.setSortFn = _.bindKey(req, 'setSortFn'); - } - - return SegmentedHandle; -}; diff --git a/src/ui/public/courier/fetch/request/doc.js b/src/ui/public/courier/fetch/request/doc.js index 0b822e1fc0c61..bbe0d12e7ee6c 100644 --- a/src/ui/public/courier/fetch/request/doc.js +++ b/src/ui/public/courier/fetch/request/doc.js @@ -1,42 +1,43 @@ -import _ from 'lodash'; -import CourierFetchStrategyDocProvider from 'ui/courier/fetch/strategy/doc'; -import CourierFetchRequestRequestProvider from 'ui/courier/fetch/request/request'; -export default function DocRequestProvider(Private) { +import DocStrategyProvider from '../strategy/doc'; +import AbstractRequestProvider from './request'; - var docStrategy = Private(CourierFetchStrategyDocProvider); - var AbstractRequest = Private(CourierFetchRequestRequestProvider); +export default function DocRequestProvider(Private) { - _.class(DocRequest).inherits(AbstractRequest); - function DocRequest(source, defer) { - DocRequest.Super.call(this, source, defer); + const docStrategy = Private(DocStrategyProvider); + const AbstractRequest = Private(AbstractRequestProvider); - this.type = 'doc'; - this.strategy = docStrategy; - } + class DocRequest extends AbstractRequest { + constructor(...args) { + super(...args); - DocRequest.prototype.canStart = function () { - var parent = DocRequest.Super.prototype.canStart.call(this); - if (!parent) return false; + this.type = 'doc'; + this.strategy = docStrategy; + } - var version = this.source._version; - var storedVersion = this.source._getStoredVersion(); + canStart() { + const parent = super.canStart(); + if (!parent) return false; - // conditions that equal "fetch This DOC!" - var unknown = !version && !storedVersion; - var mismatch = version !== storedVersion; + const version = this.source._version; + const storedVersion = this.source._getStoredVersion(); - return Boolean(mismatch || (unknown && !this.started)); - }; + // conditions that equal "fetch This DOC!" + const unknown = !version && !storedVersion; + const mismatch = version !== storedVersion; - DocRequest.prototype.handleResponse = function (resp) { - if (resp.found) { - this.source._storeVersion(resp._version); - } else { - this.source._clearVersion(); + return Boolean(mismatch || (unknown && !this.started)); } - return DocRequest.Super.prototype.handleResponse.call(this, resp); - }; + handleResponse(resp) { + if (resp.found) { + this.source._storeVersion(resp._version); + } else { + this.source._clearVersion(); + } + + return super.handleResponse(resp); + } + } return DocRequest; }; diff --git a/src/ui/public/courier/fetch/request/_error_handler.js b/src/ui/public/courier/fetch/request/error_handler.js similarity index 76% rename from src/ui/public/courier/fetch/request/_error_handler.js rename to src/ui/public/courier/fetch/request/error_handler.js index 69aede3ee21f4..91e6c0929df39 100644 --- a/src/ui/public/courier/fetch/request/_error_handler.js +++ b/src/ui/public/courier/fetch/request/error_handler.js @@ -1,15 +1,16 @@ -import CourierErrorHandlersProvider from 'ui/courier/_error_handlers'; import Notifier from 'ui/notify/notifier'; +import ErrorHandlersProvider from '../../_error_handlers'; + export default function RequestErrorHandlerFactory(Private) { - var errHandlers = Private(CourierErrorHandlersProvider); + const errHandlers = Private(ErrorHandlersProvider); - var notify = new Notifier({ + const notify = new Notifier({ location: 'Courier Fetch Error' }); function handleError(req, error) { - var myHandlers = []; + const myHandlers = []; errHandlers.splice(0).forEach(function (handler) { (handler.source === req.source ? myHandlers : errHandlers).push(handler); diff --git a/src/ui/public/courier/fetch/request/request.js b/src/ui/public/courier/fetch/request/request.js index 7d828e583adfd..1c7cf35abd318 100644 --- a/src/ui/public/courier/fetch/request/request.js +++ b/src/ui/public/courier/fetch/request/request.js @@ -1,115 +1,110 @@ import _ from 'lodash'; import moment from 'moment'; + import errors from 'ui/errors'; -import CourierRequestQueueProvider from 'ui/courier/_request_queue'; -import CourierFetchRequestErrorHandlerProvider from 'ui/courier/fetch/request/_error_handler'; -export default function AbstractReqProvider(Private, Promise) { - var requestQueue = Private(CourierRequestQueueProvider); - var requestErrorHandler = Private(CourierFetchRequestErrorHandlerProvider); - function AbstractReq(source, defer) { - if (!(this instanceof AbstractReq) || !this.constructor || this.constructor === AbstractReq) { - throw new Error('The AbstractReq class should not be called directly'); - } +import RequestQueueProvider from '../../_request_queue'; +import ErrorHandlerRequestProvider from './error_handler'; - this.source = source; - this.defer = defer || Promise.defer(); +export default function AbstractReqProvider(Private, Promise) { + const requestQueue = Private(RequestQueueProvider); + const requestErrorHandler = Private(ErrorHandlerRequestProvider); - requestQueue.push(this); - } + return class AbstractReq { + constructor(source, defer) { + this.source = source; + this.defer = defer || Promise.defer(); + this._whenAbortedHandlers = []; - AbstractReq.prototype.canStart = function () { - return Boolean(!this.stopped && !this.source._fetchDisabled); - }; + requestQueue.push(this); + } - AbstractReq.prototype.start = function () { - if (this.started) { - throw new TypeError('Unable to start request because it has already started'); + canStart() { + return Boolean(!this.stopped && !this.source._fetchDisabled); } - this.started = true; - this.moment = moment(); + start() { + if (this.started) { + throw new TypeError('Unable to start request because it has already started'); + } - var source = this.source; - if (source.activeFetchCount) { - source.activeFetchCount += 1; - } else { - source.activeFetchCount = 1; - } + this.started = true; + this.moment = moment(); - source.history = [this]; - }; + const source = this.source; + if (source.activeFetchCount) { + source.activeFetchCount += 1; + } else { + source.activeFetchCount = 1; + } - AbstractReq.prototype.getFetchParams = function () { - return this.source._flatten(); - }; + source.history = [this]; + } - AbstractReq.prototype.transformResponse = function (resp) { - return resp; - }; + getFetchParams() { + return this.source._flatten(); + } - AbstractReq.prototype.filterError = function (resp) { - return false; - }; + transformResponse(resp) { + return resp; + } - AbstractReq.prototype.handleResponse = function (resp) { - this.success = true; - this.resp = resp; - }; + filterError(resp) { + return false; + } - AbstractReq.prototype.handleFailure = function (error) { - this.success = false; - this.resp = error && error.resp; - this.retry(); - return requestErrorHandler(this, error); - }; + handleResponse(resp) { + this.success = true; + this.resp = resp; + } - AbstractReq.prototype.isIncomplete = function () { - return false; - }; + handleFailure(error) { + this.success = false; + this.resp = error && error.resp; + this.retry(); + return requestErrorHandler(this, error); + } - AbstractReq.prototype.continue = function () { - throw new Error('Unable to continue ' + this.type + ' request'); - }; + isIncomplete() { + return false; + } - AbstractReq.prototype.retry = function () { - var clone = this.clone(); - this.abort(); - return clone; - }; + continue() { + throw new Error('Unable to continue ' + this.type + ' request'); + } - // don't want people overriding this, so it becomes a natural - // part of .abort() and .complete() - function stop(then) { - return function () { - if (this.stopped) return; + retry() { + const clone = this.clone(); + this.abort(); + return clone; + } + _markStopped() { + if (this.stopped) return; this.stopped = true; this.source.activeFetchCount -= 1; _.pull(requestQueue, this); - then.call(this); - }; - } - - AbstractReq.prototype.abort = stop(function () { - this.defer = null; - this.aborted = true; - if (this._whenAborted) _.callEach(this._whenAborted); - }); - - AbstractReq.prototype.whenAborted = function (cb) { - this._whenAborted = (this._whenAborted || []); - this._whenAborted.push(cb); - }; + } - AbstractReq.prototype.complete = stop(function () { - this.ms = this.moment.diff() * -1; - this.defer.resolve(this.resp); - }); + abort() { + this._markStopped(); + this.defer = null; + this.aborted = true; + _.callEach(this._whenAbortedHandlers); + } - AbstractReq.prototype.clone = function () { - return new this.constructor(this.source, this.defer); - }; + whenAborted(cb) { + this._whenAbortedHandlers.push(cb); + } - return AbstractReq; + complete() { + this._markStopped(); + this.ms = this.moment.diff() * -1; + this.defer.resolve(this.resp); + } + + clone() { + return new this.constructor(this.source, this.defer); + } + }; }; diff --git a/src/ui/public/courier/fetch/request/search.js b/src/ui/public/courier/fetch/request/search.js index 9718124ef0c95..9ae5b8398bc7d 100644 --- a/src/ui/public/courier/fetch/request/search.js +++ b/src/ui/public/courier/fetch/request/search.js @@ -1,19 +1,17 @@ -import _ from 'lodash'; -import CourierFetchStrategySearchProvider from 'ui/courier/fetch/strategy/search'; -import CourierFetchRequestRequestProvider from 'ui/courier/fetch/request/request'; -export default function SearchReqProvider(Private) { +import SearchStrategyProvider from '../strategy/search'; +import AbstractRequestProvider from './request'; - var searchStrategy = Private(CourierFetchStrategySearchProvider); - var AbstractRequest = Private(CourierFetchRequestRequestProvider); +export default function SearchReqProvider(Private) { - _.class(SearchReq).inherits(AbstractRequest); - var Super = SearchReq.Super; - function SearchReq(source, defer) { - Super.call(this, source, defer); + const searchStrategy = Private(SearchStrategyProvider); + const AbstractRequest = Private(AbstractRequestProvider); - this.type = 'search'; - this.strategy = searchStrategy; - } + return class SearchReq extends AbstractRequest { + constructor(...args) { + super(...args); - return SearchReq; + this.type = 'search'; + this.strategy = searchStrategy; + } + }; }; diff --git a/src/ui/public/courier/fetch/request/segmented.js b/src/ui/public/courier/fetch/request/segmented.js index 67013b1caeb28..71ae5e2fa8b8d 100644 --- a/src/ui/public/courier/fetch/request/segmented.js +++ b/src/ui/public/courier/fetch/request/segmented.js @@ -1,334 +1,337 @@ -import { isNumber } from 'lodash'; import _ from 'lodash'; -import CourierFetchRequestSearchProvider from 'ui/courier/fetch/request/search'; -import CourierFetchRequestSegmentedHandleProvider from 'ui/courier/fetch/request/_segmented_handle'; -export default function CourierSegmentedReqProvider(es, Private, Promise, Notifier, timefilter, config) { - var SearchReq = Private(CourierFetchRequestSearchProvider); - var SegmentedHandle = Private(CourierFetchRequestSegmentedHandleProvider); +import { isNumber } from 'lodash'; + +import Notifier from 'ui/notify/notifier'; + +import SearchRequestProvider from './search'; +import SegmentedHandleProvider from './segmented_handle'; + +export default function SegmentedReqProvider(es, Private, Promise, timefilter, config) { + const SearchReq = Private(SearchRequestProvider); + const SegmentedHandle = Private(SegmentedHandleProvider); - var notify = new Notifier({ + const notify = new Notifier({ location: 'Segmented Fetch' }); - _.class(SegmentedReq).inherits(SearchReq); - function SegmentedReq(source, defer, initFn) { - SearchReq.call(this, source, defer); + class SegmentedReq extends SearchReq { + constructor(source, defer, initFn) { + super(source, defer); - this.type = 'segmented'; + this.type = 'segmented'; - // segmented request specific state - this._initFn = initFn; + // segmented request specific state + this._initFn = initFn; - this._desiredSize = null; - this._maxSegments = config.get('courier:maxSegmentCount'); - this._direction = 'desc'; - this._sortFn = null; - this._queueCreated = false; - this._handle = new SegmentedHandle(this); + this._desiredSize = null; + this._maxSegments = config.get('courier:maxSegmentCount'); + this._direction = 'desc'; + this._sortFn = null; + this._queueCreated = false; + this._handle = new SegmentedHandle(this); - this._hitWindow = null; + this._hitWindow = null; - // prevent the source from changing between requests, - // all calls will return the same promise - this._getFlattenedSource = _.once(this._getFlattenedSource); - } + // prevent the source from changing between requests, + // all calls will return the same promise + this._getFlattenedSource = _.once(this._getFlattenedSource); + } - /********* - ** SearchReq overrides - *********/ - - SegmentedReq.prototype.start = function () { - var self = this; - - this._complete = []; - this._active = null; - this._segments = []; - this._all = []; - this._queue = []; - - this._mergedResp = { - took: 0, - hits: { - hits: [], - total: 0, - max_score: 0 - } - }; + /********* + ** SearchReq overrides + *********/ + + start() { + this._complete = []; + this._active = null; + this._segments = []; + this._all = []; + this._queue = []; + + this._mergedResp = { + took: 0, + hits: { + hits: [], + total: 0, + max_score: 0 + } + }; + + // give the request consumer a chance to receive each segment and set + // parameters via the handle + if (_.isFunction(this._initFn)) this._initFn(this._handle); + return this._createQueue().then((queue) => { + this._all = queue.slice(0); - // give the request consumer a chance to receive each segment and set - // parameters via the handle - if (_.isFunction(this._initFn)) this._initFn(this._handle); - return this._createQueue().then(function (queue) { - self._all = queue.slice(0); + // Send the initial fetch status + this._reportStatus(); - // Send the initial fetch status - self._reportStatus(); + return super.start(); + }); + } - return SearchReq.prototype.start.call(self); - }); - }; + continue() { + return this._reportStatus(); + } - SegmentedReq.prototype.continue = function () { - return this._reportStatus(); - }; + getFetchParams() { + return this._getFlattenedSource().then(flatSource => { + const params = _.cloneDeep(flatSource); - SegmentedReq.prototype.getFetchParams = function () { - var self = this; + // calculate the number of indices to fetch in this request in order to prevent + // more than this._maxSegments requests. We use Math.max(1, n) to ensure that each request + // has at least one index pattern, and Math.floor() to make sure that if the + // number of indices does not round out evenly the extra index is tacked onto the last + // request, making sure the first request returns faster. + const remainingSegments = this._maxSegments - this._segments.length; + const indexCount = Math.max(1, Math.floor(this._queue.length / remainingSegments)); - return self._getFlattenedSource().then(function (flatSource) { - var params = _.cloneDeep(flatSource); + const indices = this._active = this._queue.splice(0, indexCount); + params.index = _.pluck(indices, 'index'); - // calculate the number of indices to fetch in this request in order to prevent - // more than self._maxSegments requests. We use Math.max(1, n) to ensure that each request - // has at least one index pattern, and Math.floor() to make sure that if the - // number of indices does not round out evenly the extra index is tacked onto the last - // request, making sure the first request returns faster. - var remainingSegments = self._maxSegments - self._segments.length; - var indexCount = Math.max(1, Math.floor(self._queue.length / remainingSegments)); + if (isNumber(this._desiredSize)) { + params.body.size = this._pickSizeForIndices(indices); + } - var indices = self._active = self._queue.splice(0, indexCount); - params.index = _.pluck(indices, 'index'); + return params; + }); + } - if (isNumber(self._desiredSize)) { - params.body.size = self._pickSizeForIndices(indices); + handleResponse(resp) { + return this._consumeSegment(resp); + } + + filterError(resp) { + if (/ClusterBlockException.*index\sclosed/.test(resp.error)) { + this._consumeSegment(false); + return true; } + } + + isIncomplete() { + const queueNotCreated = !this._queueCreated; + const queueNotEmpty = this._queue.length > 0; + return queueNotCreated || queueNotEmpty; + } + + clone() { + return new SegmentedReq(this.source, this.defer, this._initFn); + } + + complete() { + this._reportStatus(); + this._handle.emit('complete'); + return super.complete(); + } + + /********* + ** SegmentedReq specific methods + *********/ - return params; - }); - }; - SegmentedReq.prototype.handleResponse = function (resp) { - return this._consumeSegment(resp); - }; + /** + * Set the sort total number of segments to emit + * + * @param {number} + */ + setMaxSegments(maxSegments) { + this._maxSegments = Math.max(_.parseInt(maxSegments), 1); + } + + /** + * Set the sort direction for the request. + * + * @param {string} dir - one of 'asc' or 'desc' + */ + setDirection(dir) { + switch (dir) { + case 'asc': + case 'desc': + return (this._direction = dir); + default: + throw new TypeError('unknown sort direction "' + dir + '"'); + } + } - SegmentedReq.prototype.filterError = function (resp) { - if (/ClusterBlockException.*index\sclosed/.test(resp.error)) { - this._consumeSegment(false); - return true; + /** + * Set the function that will be used to sort the rows + * + * @param {fn} + */ + setSortFn(sortFn) { + this._sortFn = sortFn; } - }; - - SegmentedReq.prototype.isIncomplete = function () { - var queueNotCreated = !this._queueCreated; - var queueNotEmpty = this._queue.length > 0; - return queueNotCreated || queueNotEmpty; - }; - - SegmentedReq.prototype.clone = function () { - return new SegmentedReq(this.source, this.defer, this._initFn); - }; - - SegmentedReq.prototype.complete = function () { - this._reportStatus(); - this._handle.emit('complete'); - return SearchReq.prototype.complete.call(this); - }; - - /********* - ** SegmentedReq specific methods - *********/ - - /** - * Set the sort total number of segments to emit - * - * @param {number} - */ - SegmentedReq.prototype.setMaxSegments = function (maxSegments) { - this._maxSegments = Math.max(_.parseInt(maxSegments), 1); - }; - - /** - * Set the sort direction for the request. - * - * @param {string} dir - one of 'asc' or 'desc' - */ - SegmentedReq.prototype.setDirection = function (dir) { - switch (dir) { - case 'asc': - case 'desc': - return (this._direction = dir); - default: - throw new TypeError('unknown sort direction "' + dir + '"'); + + /** + * Set the sort total number of documents to + * emit + * + * Setting to false will not limit the documents, + * if a number is set the size of the request to es + * will be updated on each new request + * + * @param {number|false} + */ + setSize(totalSize) { + this._desiredSize = _.parseInt(totalSize); + if (isNaN(this._desiredSize)) this._desiredSize = null; } - }; - - /** - * Set the function that will be used to sort the rows - * - * @param {fn} - */ - SegmentedReq.prototype.setSortFn = function (sortFn) { - this._sortFn = sortFn; - }; - - /** - * Set the sort total number of documents to - * emit - * - * Setting to false will not limit the documents, - * if a number is set the size of the request to es - * will be updated on each new request - * - * @param {number|false} - */ - SegmentedReq.prototype.setSize = function (totalSize) { - this._desiredSize = _.parseInt(totalSize); - if (isNaN(this._desiredSize)) this._desiredSize = null; - }; - - SegmentedReq.prototype._createQueue = function () { - var self = this; - var timeBounds = timefilter.getBounds(); - var indexPattern = self.source.get('index'); - self._queueCreated = false; - - return indexPattern.toDetailedIndexList(timeBounds.min, timeBounds.max, self._direction) - .then(function (queue) { - if (!_.isArray(queue)) queue = [queue]; - - self._queue = queue; - self._queueCreated = true; - - return queue; - }); - }; - - SegmentedReq.prototype._reportStatus = function () { - return this._handle.emit('status', { - total: this._queueCreated ? this._all.length : NaN, - complete: this._queueCreated ? this._complete.length : NaN, - remaining: this._queueCreated ? this._queue.length : NaN, - hitCount: this._queueCreated ? this._mergedResp.hits.hits.length : NaN - }); - }; - - SegmentedReq.prototype._getFlattenedSource = function () { - return this.source._flatten(); - }; - - SegmentedReq.prototype._consumeSegment = function (seg) { - var index = this._active; - this._complete.push(index); - if (!seg) return; // segment was ignored/filtered, don't store it - - var hadHits = _.get(this._mergedResp, 'hits.hits.length') > 0; - var gotHits = _.get(seg, 'hits.hits.length') > 0; - var firstHits = !hadHits && gotHits; - var haveHits = hadHits || gotHits; - - this._mergeSegment(seg); - this.resp = _.omit(this._mergedResp, '_bucketIndex'); - - if (firstHits) this._handle.emit('first', seg); - if (gotHits) this._handle.emit('segment', seg); - if (haveHits) this._handle.emit('mergedSegment', this.resp); - }; - - SegmentedReq.prototype._mergeHits = function (hits) { - var mergedHits = this._mergedResp.hits.hits; - var desiredSize = this._desiredSize; - var sortFn = this._sortFn; - - _.pushAll(hits, mergedHits); - - if (sortFn) { - notify.event('resort rows', function () { - mergedHits.sort(sortFn); + + _createQueue() { + const timeBounds = timefilter.getBounds(); + const indexPattern = this.source.get('index'); + this._queueCreated = false; + + return indexPattern.toDetailedIndexList(timeBounds.min, timeBounds.max, this._direction) + .then(queue => { + if (!_.isArray(queue)) queue = [queue]; + + this._queue = queue; + this._queueCreated = true; + + return queue; + }); + } + + _reportStatus() { + return this._handle.emit('status', { + total: this._queueCreated ? this._all.length : NaN, + complete: this._queueCreated ? this._complete.length : NaN, + remaining: this._queueCreated ? this._queue.length : NaN, + hitCount: this._queueCreated ? this._mergedResp.hits.hits.length : NaN }); } - if (isNumber(desiredSize)) { - mergedHits = this._mergedResp.hits.hits = mergedHits.slice(0, desiredSize); + _getFlattenedSource() { + return this.source._flatten(); } - }; - SegmentedReq.prototype._mergeSegment = notify.timed('merge response segment', function (seg) { - var merged = this._mergedResp; + _consumeSegment(seg) { + const index = this._active; + this._complete.push(index); + if (!seg) return; // segment was ignored/filtered, don't store it - this._segments.push(seg); + const hadHits = _.get(this._mergedResp, 'hits.hits.length') > 0; + const gotHits = _.get(seg, 'hits.hits.length') > 0; + const firstHits = !hadHits && gotHits; + const haveHits = hadHits || gotHits; - merged.took += seg.took; - merged.hits.total += seg.hits.total; - merged.hits.max_score = Math.max(merged.hits.max_score, seg.hits.max_score); + this._mergeSegment(seg); + this.resp = _.omit(this._mergedResp, '_bucketIndex'); - if (_.size(seg.hits.hits)) { - this._mergeHits(seg.hits.hits); - this._detectHitsWindow(merged.hits.hits); + if (firstHits) this._handle.emit('first', seg); + if (gotHits) this._handle.emit('segment', seg); + if (haveHits) this._handle.emit('mergedSegment', this.resp); } - if (!seg.aggregations) return; + _mergeHits(hits) { + const mergedHits = this._mergedResp.hits.hits; + const desiredSize = this._desiredSize; + const sortFn = this._sortFn; - Object.keys(seg.aggregations).forEach(function (aggKey) { + _.pushAll(hits, mergedHits); - if (!merged.aggregations) { - // start merging aggregations - merged.aggregations = {}; - merged._bucketIndex = {}; + if (sortFn) { + notify.event('resort rows', function () { + mergedHits.sort(sortFn); + }); } - if (!merged.aggregations[aggKey]) { - merged.aggregations[aggKey] = { - buckets: [] - }; + if (isNumber(desiredSize)) { + this._mergedResp.hits.hits = mergedHits.slice(0, desiredSize); + } + } + + _mergeSegment(seg) { + const merged = this._mergedResp; + + this._segments.push(seg); + + merged.took += seg.took; + merged.hits.total += seg.hits.total; + merged.hits.max_score = Math.max(merged.hits.max_score, seg.hits.max_score); + + if (_.size(seg.hits.hits)) { + this._mergeHits(seg.hits.hits); + this._detectHitsWindow(merged.hits.hits); } - seg.aggregations[aggKey].buckets.forEach(function (bucket) { - var mbucket = merged._bucketIndex[bucket.key]; - if (mbucket) { - mbucket.doc_count += bucket.doc_count; - return; + if (!seg.aggregations) return; + + Object.keys(seg.aggregations).forEach(function (aggKey) { + + if (!merged.aggregations) { + // start merging aggregations + merged.aggregations = {}; + merged._bucketIndex = {}; + } + + if (!merged.aggregations[aggKey]) { + merged.aggregations[aggKey] = { + buckets: [] + }; } - mbucket = merged._bucketIndex[bucket.key] = bucket; - merged.aggregations[aggKey].buckets.push(mbucket); + seg.aggregations[aggKey].buckets.forEach(function (bucket) { + let mbucket = merged._bucketIndex[bucket.key]; + if (mbucket) { + mbucket.doc_count += bucket.doc_count; + return; + } + + mbucket = merged._bucketIndex[bucket.key] = bucket; + merged.aggregations[aggKey].buckets.push(mbucket); + }); }); - }); - }); + } - SegmentedReq.prototype._detectHitsWindow = function (hits) { - hits = hits || []; - var indexPattern = this.source.get('index'); - var desiredSize = this._desiredSize; - - var size = _.size(hits); - if (!isNumber(desiredSize) || size < desiredSize) { - this._hitWindow = { - size: size, - min: -Infinity, - max: Infinity - }; - return; + _detectHitsWindow(hits) { + hits = hits || []; + const indexPattern = this.source.get('index'); + const desiredSize = this._desiredSize; + + const size = _.size(hits); + if (!isNumber(desiredSize) || size < desiredSize) { + this._hitWindow = { + size: size, + min: -Infinity, + max: Infinity + }; + return; + } + + let min; + let max; + + hits.forEach(function (deepHit) { + const hit = indexPattern.flattenHit(deepHit); + const time = hit[indexPattern.timeFieldName]; + if (min == null || time < min) min = time; + if (max == null || time > max) max = time; + }); + + this._hitWindow = { size, min, max }; + } + + _pickSizeForIndices(indices) { + const hitWindow = this._hitWindow; + const desiredSize = this._desiredSize; + + if (!isNumber(desiredSize)) return null; + // we don't have any hits yet, get us more info! + if (!hitWindow) return desiredSize; + // the order of documents isn't important, just get us more + 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. + const someOverlap = indices.some(function (index) { + return index.min <= hitWindow.max && hitWindow.min <= index.max; + }); + + return someOverlap ? desiredSize : 0; } + } - let min; - let max; - - hits.forEach(function (deepHit) { - var hit = indexPattern.flattenHit(deepHit); - var time = hit[indexPattern.timeFieldName]; - if (min == null || time < min) min = time; - if (max == null || time > max) max = time; - }); - - this._hitWindow = { size, min, max }; - }; - - SegmentedReq.prototype._pickSizeForIndices = function (indices) { - var hitWindow = this._hitWindow; - var desiredSize = this._desiredSize; - - if (!isNumber(desiredSize)) return null; - // we don't have any hits yet, get us more info! - if (!hitWindow) return desiredSize; - // the order of documents isn't important, just get us more - 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; - }); - - return someOverlap ? desiredSize : 0; - }; + SegmentedReq.prototype.mergedSegment = notify.timed('merge response segment', SegmentedReq.prototype.mergedSegment); return SegmentedReq; }; diff --git a/src/ui/public/courier/fetch/request/segmented_handle.js b/src/ui/public/courier/fetch/request/segmented_handle.js new file mode 100644 index 0000000000000..702378d8392ae --- /dev/null +++ b/src/ui/public/courier/fetch/request/segmented_handle.js @@ -0,0 +1,40 @@ +import EventsProvider from 'ui/events'; + +export default function CourierSegmentedReqHandle(Private) { + const Events = Private(EventsProvider); + + const segmentedRequest = Symbol('Actual Segmented Request'); + + /** + * Simple class for creating an object to send to the + * requester of a SegmentedRequest. Since the SegmentedRequest + * extends AbstractRequest, it wasn't able to be the event + * emitter it was born to be. This provides a channel for + * setting values on the segmented request, and an event + * emitter for the request to speak outwardly + * + * @param {SegmentedRequest} - req - the requst this handle relates to + */ + return class SegmentedHandle extends Events { + constructor(req) { + super(); + this[segmentedRequest] = req; + } + + setDirection(...args) { + this[segmentedRequest].setDirection(...args); + } + + setSize(...args) { + this[segmentedRequest].setSize(...args); + } + + setMaxSegments(...args) { + this[segmentedRequest].setMaxSegments(...args); + } + + setSortFn(...args) { + this[segmentedRequest].setSortFn(...args); + } + }; +}; diff --git a/src/ui/public/courier/fetch/strategy/__tests__/search.js b/src/ui/public/courier/fetch/strategy/__tests__/search.js index e714007912482..21d62a2bbb419 100644 --- a/src/ui/public/courier/fetch/strategy/__tests__/search.js +++ b/src/ui/public/courier/fetch/strategy/__tests__/search.js @@ -2,7 +2,9 @@ import _ from 'lodash'; import sinon from 'auto-release-sinon'; import expect from 'expect.js'; import ngMock from 'ngMock'; -import CourierFetchStrategySearchProvider from 'ui/courier/fetch/strategy/search'; + +import SearchStrategyProvider from '../search'; + describe('ui/courier/fetch/strategy/search', () => { let Promise; @@ -15,7 +17,7 @@ describe('ui/courier/fetch/strategy/search', () => { beforeEach(ngMock.inject((Private, $injector) => { Promise = $injector.get('Promise'); $rootScope = $injector.get('$rootScope'); - search = Private(CourierFetchStrategySearchProvider); + search = Private(SearchStrategyProvider); reqsFetchParams = [ { index: ['logstash-123'], diff --git a/src/ui/public/courier/fetch/strategy/search.js b/src/ui/public/courier/fetch/strategy/search.js index 3c2972e4ec359..2abda799343b6 100644 --- a/src/ui/public/courier/fetch/strategy/search.js +++ b/src/ui/public/courier/fetch/strategy/search.js @@ -1,6 +1,8 @@ -import { toJson } from 'ui/utils/aggressive_parse'; import _ from 'lodash'; import angular from 'angular'; + +import { toJson } from 'ui/utils/aggressive_parse'; + export default function FetchStrategyForSearch(Private, Promise, timefilter) { return { @@ -20,7 +22,7 @@ export default function FetchStrategyForSearch(Private, Promise, timefilter) { return indexList; } - var timeBounds = timefilter.getBounds(); + const timeBounds = timefilter.getBounds(); return indexList.toIndexList(timeBounds.min, timeBounds.max); }) .then(function (indexList) {