From 7ff55bfe88d31ed8df5ba5cbe1fa03f7f8e569e3 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Thu, 24 Jul 2014 16:14:30 -0700 Subject: [PATCH] Changing the inheritance chain so it's resolved at merge time --- src/kibana/apps/dashboard/index.js | 42 +++++++--------- .../dashboard/services/_saved_dashboard.js | 6 ++- src/kibana/apps/discover/index.html | 2 +- .../saved_visualizations/_adhoc_vis.js | 10 ++-- .../saved_visualizations/_saved_vis.js | 32 +++++------- .../components/courier/_get_root_search.js | 17 ++++--- src/kibana/components/courier/courier.js | 5 +- .../courier/data_source/_abstract.js | 20 ++++++-- .../data_source/_root_search_source.js | 49 +++++++++++++++++++ .../courier/data_source/search_source.js | 23 ++++++--- .../courier/saved_object/saved_object.js | 10 +--- src/kibana/controllers/kibana.js | 11 ----- 12 files changed, 134 insertions(+), 93 deletions(-) create mode 100644 src/kibana/components/courier/data_source/_root_search_source.js diff --git a/src/kibana/apps/dashboard/index.js b/src/kibana/apps/dashboard/index.js index 1fe643cfeaa0e..ea2437fb62338 100644 --- a/src/kibana/apps/dashboard/index.js +++ b/src/kibana/apps/dashboard/index.js @@ -71,41 +71,35 @@ define(function (require) { $scope.openAdd = _.partial($scope.configTemplate.toggle, 'pickVis'); $scope.refresh = _.bindKey(courier, 'fetch'); - timefilter.enabled(true); $scope.timefilter = timefilter; $scope.$watchCollection('globalState.time', $scope.refresh); + courier.setRootSearchSource(dash.searchSource); + function init() { - updateQueryOnRootSource() - .then(function () { - $scope.$broadcast('application.load'); - }); + updateQueryOnRootSource(); + $scope.$broadcast('application.load'); } function updateQueryOnRootSource() { - return courier.getRootSearch() - .then(function (rootSource) { - if ($state.query) { - rootSource.set('filter', { - query: { - query_string: { - query: $state.query - } + if ($state.query) { + dash.searchSource.set('filter', { + query: { + query_string: { + query: $state.query } - }); - } else { - rootSource.set('query', null); - } - }); + } + }); + } else { + dash.searchSource.set('filter', null); + } } $scope.filterResults = function () { - updateQueryOnRootSource() - .then(function () { - $state.commit(); - courier.fetch(); - }); + updateQueryOnRootSource(); + $state.commit(); + courier.fetch(); }; $scope.save = function () { @@ -162,4 +156,4 @@ define(function (require) { } }; }); -}); \ No newline at end of file +}); diff --git a/src/kibana/apps/dashboard/services/_saved_dashboard.js b/src/kibana/apps/dashboard/services/_saved_dashboard.js index 748a9a9e91c12..88d3a0ad58f96 100644 --- a/src/kibana/apps/dashboard/services/_saved_dashboard.js +++ b/src/kibana/apps/dashboard/services/_saved_dashboard.js @@ -32,7 +32,9 @@ define(function (require) { hits: 0, description: '', panelsJSON: '[]' - } + }, + + searchSource: true }); } @@ -41,4 +43,4 @@ define(function (require) { return SavedDashboard; }); -}); \ No newline at end of file +}); diff --git a/src/kibana/apps/discover/index.html b/src/kibana/apps/discover/index.html index 619a6839dd9ee..5b68be96dcee6 100644 --- a/src/kibana/apps/discover/index.html +++ b/src/kibana/apps/discover/index.html @@ -88,4 +88,4 @@

Searching

- \ No newline at end of file + diff --git a/src/kibana/apps/visualize/saved_visualizations/_adhoc_vis.js b/src/kibana/apps/visualize/saved_visualizations/_adhoc_vis.js index 1a4766520865b..71275842ea7a7 100644 --- a/src/kibana/apps/visualize/saved_visualizations/_adhoc_vis.js +++ b/src/kibana/apps/visualize/saved_visualizations/_adhoc_vis.js @@ -51,12 +51,8 @@ define(function (require) { return opts.searchSource; } - return courier.getRootSearch() - .then(function (rootSearch) { - var searchSource = courier.createSource('search'); - searchSource.inherits(rootSearch); - return searchSource; - }); + return courier.createSource('search'); + }())) .then(function (searchSource) { // TODO: Should we abtract out the agg building stuff? @@ -132,4 +128,4 @@ define(function (require) { return AdhocVis; }); -}); \ No newline at end of file +}); diff --git a/src/kibana/apps/visualize/saved_visualizations/_saved_vis.js b/src/kibana/apps/visualize/saved_visualizations/_saved_vis.js index 16f9686ec1885..ed9ad44368899 100644 --- a/src/kibana/apps/visualize/saved_visualizations/_saved_vis.js +++ b/src/kibana/apps/visualize/saved_visualizations/_saved_vis.js @@ -73,26 +73,20 @@ define(function (require) { return savedSearches.get(vis.savedSearchId); } - return courier.getRootSearch() - .then(function (rootSearch) { - - if (relatedPattern) { - return indexPatterns.get(relatedPattern) - .then(function (indexPattern) { - // create a new search source that inherits from the parent and uses the indexPattern - return { - searchSource: rootSearch.extend().index(indexPattern) - }; - }); - } + var fakeSavedSearch = { + searchSource: courier.createSource('search') + }; + + if (relatedPattern) { + return indexPatterns.get(relatedPattern) + .then(function (indexPattern) { + fakeSavedSearch.searchSource.index(indexPattern); + return fakeSavedSearch; + }); + } - // default parent is the rootSearch, can be overridden (like in discover) - // but we mimic the searchSource prop from saved objects here - return { - searchSource: rootSearch - }; + return Promise.resolve(fakeSavedSearch); - }); }()); return promisedParent @@ -223,4 +217,4 @@ define(function (require) { return SavedVis; }); -}); \ No newline at end of file +}); diff --git a/src/kibana/components/courier/_get_root_search.js b/src/kibana/components/courier/_get_root_search.js index 79d50f924a95c..d40f2ab15d12a 100644 --- a/src/kibana/components/courier/_get_root_search.js +++ b/src/kibana/components/courier/_get_root_search.js @@ -15,13 +15,16 @@ define(function (require) { }); }; - var init = function () { - globalSource = new SearchSource(); + globalSource = new SearchSource(); - globalSource.filter(function (globalSource) { - // dynamic time filter will be called in the _flatten phase of things - return timefilter.get(globalSource.get('index')); - }); + // searchSourceManager.registerGlobal(globalSource); + + globalSource.filter(function (globalSource) { + // dynamic time filter will be called in the _flatten phase of things + return timefilter.get(globalSource.get('index')); + }); + + var init = function () { appSource = new SearchSource(); appSource.inherits(globalSource); @@ -36,4 +39,4 @@ define(function (require) { return prom || init(); }; }; -}); \ No newline at end of file +}); diff --git a/src/kibana/components/courier/courier.js b/src/kibana/components/courier/courier.js index cc4bb4a6418d8..e78b6128a7c17 100644 --- a/src/kibana/components/courier/courier.js +++ b/src/kibana/components/courier/courier.js @@ -18,7 +18,8 @@ define(function (require) { var docLooper = Private(require('components/courier/looper/doc')); // expose some internal modules - courier.getRootSearch = Private(require('components/courier/_get_root_search')); + courier.setRootSearchSource = Private(require('components/courier/data_source/_root_search_source')).set; + courier.SavedObject = Private(require('components/courier/saved_object/saved_object')); courier.indexPatterns = indexPatterns; courier.redirectWhenMissing = Private(require('components/courier/_redirect_when_missing')); @@ -117,4 +118,4 @@ define(function (require) { return new Courier(); }); -}); \ No newline at end of file +}); diff --git a/src/kibana/components/courier/data_source/_abstract.js b/src/kibana/components/courier/data_source/_abstract.js index af1184846b141..891d247a105b2 100644 --- a/src/kibana/components/courier/data_source/_abstract.js +++ b/src/kibana/components/courier/data_source/_abstract.js @@ -111,6 +111,13 @@ define(function (require) { }, handler); }; + /** + * Noop + */ + SourceAbstract.prototype.getParent = function () { + return Promise.resolve(false); + }; + /** * similar to onResults, but allows a seperate loopy code path * for error handling. @@ -221,10 +228,13 @@ define(function (require) { })) .then(function () { // move to this sources parent - current = current._parent; - - // keep calling until we reach the top parent - if (current) return ittr(); + return current.getParent().then(function (parent) { + // keep calling until we reach the top parent + if (parent) { + current = parent; + return ittr(); + } + }); }); }()) .then(function () { @@ -260,4 +270,4 @@ define(function (require) { return SourceAbstract; }; -}); \ No newline at end of file +}); diff --git a/src/kibana/components/courier/data_source/_root_search_source.js b/src/kibana/components/courier/data_source/_root_search_source.js new file mode 100644 index 0000000000000..6419d27617b38 --- /dev/null +++ b/src/kibana/components/courier/data_source/_root_search_source.js @@ -0,0 +1,49 @@ +define(function (require) { + return function RootSearchSource(Private, $rootScope, config, Promise, indexPatterns, timefilter) { + var prom; // promise that must be resolved before the source is acurrate (updated by loadDefaultPattern) + var appSource; + var SearchSource = Private(require('components/courier/data_source/search_source')); + + var get = function () { + return prom || loadDefaultPattern(); + }; + + var set = function (source) { + source.inherits(globalSource); + appSource = source; + }; + + var loadDefaultPattern = function () { + var defId = config.get('defaultIndex'); + + return prom = Promise.cast(defId && indexPatterns.get(defId)).then(function (pattern) { + globalSource.set('index', pattern); + return appSource; + }); + }; + + var globalSource = new SearchSource(); + globalSource.inherits(false); + set(new SearchSource()); + + globalSource.filter(function (globalSource) { + // dynamic time filter will be called in the _flatten phase of things + return timefilter.get(globalSource.get('index')); + }); + + $rootScope.$on('change:config.defaultIndex', loadDefaultPattern); + $rootScope.$on('init:config', loadDefaultPattern); + + function onRouteChangeStart() { + appSource = new SearchSource(); + } + + $rootScope.$on('$routeChangeStart', onRouteChangeStart); + $rootScope.$on('$routeUpdate', onRouteChangeStart); + + return { + get: get, + set: set + }; + }; +}); diff --git a/src/kibana/components/courier/data_source/search_source.js b/src/kibana/components/courier/data_source/search_source.js index ff93dd97f5441..c3b9a106f36da 100644 --- a/src/kibana/components/courier/data_source/search_source.js +++ b/src/kibana/components/courier/data_source/search_source.js @@ -1,11 +1,15 @@ define(function (require) { - var inherits = require('utils/inherits'); - var _ = require('lodash'); - var errors = require('errors'); return function SearchSourceFactory(Promise, Private) { + var inherits = require('utils/inherits'); + var _ = require('lodash'); + var errors = require('errors'); var SourceAbstract = Private(require('components/courier/data_source/_abstract')); + var getRootSourcePromise = new Promise(function (resolve) { + require(['components/courier/data_source/_root_search_source'], _.compose(resolve, Private)); + }); + var FetchFailure = errors.FetchFailure; var RequestFailure = errors.RequestFailure; @@ -62,10 +66,15 @@ define(function (require) { /** * Get the parent of this SearchSource - * @return {SearchSource} + * @return {Promise} */ - SearchSource.prototype.parent = function () { - return this._parent; + SearchSource.prototype.getParent = function () { + var self = this; + return getRootSourcePromise.then(function (rootSearchSource) { + if (self._parent === false) return false; + if (self._parent) return self._parent; + return rootSearchSource.get(); + }); }; /** @@ -144,4 +153,4 @@ define(function (require) { return SearchSource; }; -}); \ No newline at end of file +}); diff --git a/src/kibana/components/courier/saved_object/saved_object.js b/src/kibana/components/courier/saved_object/saved_object.js index f669634ffef84..656772557b5f7 100644 --- a/src/kibana/components/courier/saved_object/saved_object.js +++ b/src/kibana/components/courier/saved_object/saved_object.js @@ -61,14 +61,8 @@ define(function (require) { .type(type) .id(obj.id); - // by default, the search source should inherit from the rootSearch - return Promise.cast(obj.searchSource && getRootSearch()) - .then(function (rootSearch) { - if (rootSearch) obj.searchSource.inherits(rootSearch); - - // check that the mapping for this type is defined - return mappingSetup.isDefined(type); - }) + // check that the mapping for this type is defined + return mappingSetup.isDefined(type) .then(function (defined) { // if it is already defined skip this step if (defined) return true; diff --git a/src/kibana/controllers/kibana.js b/src/kibana/controllers/kibana.js index 341a1019f7dd8..02990605ea10b 100644 --- a/src/kibana/controllers/kibana.js +++ b/src/kibana/controllers/kibana.js @@ -110,17 +110,6 @@ define(function (require) { $rootScope.$on('$routeChangeSuccess', onRouteChange); $rootScope.$on('$routeUpdate', onRouteChange); - function onRouteChangeStart() { - // Reset appSource - courier.getRootSearch() - .then(function (rootSearch) { - rootSearch.set({}); - }); - } - - $rootScope.$on('$routeChangeStart', onRouteChangeStart); - $rootScope.$on('$routeUpdate', onRouteChangeStart); - var writeGlobalStateToLastPaths = function () { var currentUrl = $location.url(); var _g = rison.encode(globalState);