Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index Pattern Loading #1601

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/kibana/apps/visualize/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,23 @@ define(function (require) {
};

$scope.unlink = function () {
return searchSource.getParent(true)
.then(function (parent) {
var parent = searchSource.getParent(true);
var parentsParent = parent.getParent(true);

return parent.getParent(true)
.then(function (parentsParent) {
// parentsParent can be undefined
// display unlinking for 2 seconds, unless it is double clicked
$scope.unlinking = $timeout($scope.doneUnlinking, 2000);
delete savedVis.savedSearchId;
var q = searchSource.get('query');
$state.query = q;

var searchState = parent.toJSON();

// display unlinking for 2 seconds, unless it is double clicked
$scope.unlinking = $timeout($scope.doneUnlinking, 2000);
delete savedVis.savedSearchId;
var q = searchSource.get('query');
$state.query = q;

var searchState = parent.toJSON();

// copy over all state except "aggs"
_(searchState).omit('aggs').forOwn(function (val, key) {
searchSource.set(key, val);
});
// copy over all state except "aggs"
_(searchState).omit('aggs').forOwn(function (val, key) {
searchSource.set(key, val);
});

searchSource.inherits(parentsParent);
courier.setRootSearchSource(searchSource);
});
}).catch(notify.fatal);
searchSource.inherits(parentsParent);
};

$scope.doneUnlinking = function () {
Expand Down
71 changes: 28 additions & 43 deletions src/kibana/apps/visualize/saved_visualizations/_saved_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ define(function (require) {
title: 'string',
visState: 'json',
description: 'string',
savedSearchId: 'string',
indexPattern: 'string'
savedSearchId: 'string'
},

defaults: {
Expand All @@ -37,57 +36,33 @@ define(function (require) {
return def;
}()),
description: '',
savedSearchId: opts.savedSearchId,
indexPattern: opts.indexPattern
savedSearchId: opts.savedSearchId
},

searchSource: true,
indexPattern: opts.indexPattern,

afterESResp: this._afterEsResp
});
}

SavedVis.prototype._afterEsResp = function () {
var self = this;
var relatedSearch = self.savedSearchId;
var relatedPattern = !relatedSearch && self.indexPattern;
var linkedSearch = self.savedSearchId;

var promisedParent = (function () {
if (relatedSearch) {
// returns a promise
return savedSearches.get(self.savedSearchId);
}

var fakeSavedSearch = {
searchSource: courier.createSource('search')
};

if (relatedPattern) {
return courier.indexPatterns.get(relatedPattern)
.then(function (indexPattern) {
fakeSavedSearch.searchSource.index(indexPattern);
return fakeSavedSearch;
});
}

return Promise.resolve(fakeSavedSearch);
}());

return promisedParent
return Promise.resolve(linkedSearch && savedSearches.get(linkedSearch))
.then(function (parent) {
self.savedSearch = parent;

self.searchSource
.inherits(parent.searchSource)
.size(0);

if (!self.vis) {
self.vis = self._createVis();
} else {
self.vis.indexPattern = self.searchSource.get('index');
self.vis.setState(self.visState);
if (parent) {
self.savedSearch = parent;
self.searchSource
.inherits(parent.searchSource);
}

self.searchSource.size(0);

return self.vis ? self._updateVis() : self._createVis();
})
.then(function (vis) {
self.searchSource.aggs(function () {
return self.vis.aggs.toDsl();
});
Expand All @@ -97,13 +72,23 @@ define(function (require) {
};

SavedVis.prototype._createVis = function () {
var indexPattern = this.searchSource.get('index');
var self = this;

if (this.stateJSON) {
this.visState = Vis.convertOldState(this.typeName, JSON.parse(this.stateJSON));
if (self.stateJSON) {
self.visState = Vis.convertOldState(self.typeName, JSON.parse(self.stateJSON));
}

return new Vis(indexPattern, this.visState);
return self.vis = new Vis(
self.searchSource.get('index'),
self.visState
);
};

SavedVis.prototype._updateVis = function () {
var self = this;

self.vis.indexPattern = self.searchSource.get('index');
self.vis.setState(self.visState);
};

return SavedVis;
Expand Down
2 changes: 1 addition & 1 deletion src/kibana/components/config/_delayed_updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ define(function (require) {

var defer = Promise.defer();
queue.push(defer);
notify.log('config change: ' + key + ': ' + vals[key] + ' -> ' + val);
notify.log('config change: ' + key + ': ' + oldVal + ' -> ' + newVal);
$rootScope.$broadcast('change:config.' + key, newVal, oldVal);

// reset the fire timer
Expand Down
42 changes: 0 additions & 42 deletions src/kibana/components/courier/_get_root_search.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/kibana/components/courier/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ define(function (require) {
courier.indexPatterns = indexPatterns;
courier.redirectWhenMissing = Private(require('components/courier/_redirect_when_missing'));

courier.DocSource = DocSource;
courier.SearchSource = SearchSource;

var HastyRefresh = errors.HastyRefresh;
var Abort = errors.Abort;

Expand Down
29 changes: 18 additions & 11 deletions src/kibana/components/courier/data_source/_abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ define(function (require) {
/**
* Get values from the state
* @param {string} name - The name of the property desired
* @param {boolean} deep - Load the value from this search source, or traverse
* the "globally" inheritted sources and look for values there.
* @return {any|Promise<any>} - when deep, a promise is returned, otherwise the value found
*/
SourceAbstract.prototype.get = function (name) {
function read(source) {
if (source._state[name] !== void 0) return source._state[name];
if (source._dynamicState[name] !== void 0) return source._dynamicState[name]();
}

var current = this;
while (current) {
if (current._state[name] !== void 0) return current._state[name];
if (current._dynamicState[name] !== void 0) return current._dynamicState[name]();
current = current._parent;
var val = read(current);
if (val !== void 0) return val;
current = current.getParent();
}
};

Expand Down Expand Up @@ -116,7 +124,7 @@ define(function (require) {
* Noop
*/
SourceAbstract.prototype.getParent = function () {
return Promise.resolve(undefined);
return this._parent;
};

/**
Expand Down Expand Up @@ -229,13 +237,12 @@ define(function (require) {
}))
.then(function () {
// move to this sources parent
return current.getParent().then(function (parent) {
// keep calling until we reach the top parent
if (parent) {
current = parent;
return ittr();
}
});
var parent = current.getParent();
// keep calling until we reach the top parent
if (parent) {
current = parent;
return ittr();
}
});
}())
.then(function () {
Expand Down
45 changes: 23 additions & 22 deletions src/kibana/components/courier/data_source/_root_search_source.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
define(function (require) {
return function RootSearchSource(Private, $rootScope, config, Promise, indexPatterns, timefilter) {
return function RootSearchSource(Private, $rootScope, config, Promise, indexPatterns, timefilter, Notifier) {
var _ = require('lodash');
var SearchSource = Private(require('components/courier/data_source/search_source'));

var notify = new Notifier({ location: 'Root Search Source' });

var globalSource = new SearchSource();
globalSource.inherits(false); // this is the final source, it has no parents
globalSource.filter(function (globalSource) {
// dynamic time filter will be called in the _flatten phase of things
return timefilter.get(globalSource.get('index'));
});

var ensureDefaultLoaded = _.once(__loadDefaultPattern__);
var appSource; // set in setAppSource()
resetAppSource();

// when the default index changes, or the config is intialized, connect the defaultIndex to the globalSource
$rootScope.$on('change:config.defaultIndex', ensureDefaultLoaded);
$rootScope.$on('init:config', ensureDefaultLoaded);
/**
* Get the default index from the config, and hook it up to the globalSource.
*
* @return {Promise}
*/
function loadDefaultPattern() {
return notify.event('loading default index pattern', function () {
var defId = config.get('defaultIndex');

return Promise.cast(defId && indexPatterns.get(defId))
.then(function (pattern) {
globalSource.set('index', pattern);
notify.log('index pattern set to', defId);
});
});
}

// when the route changes, clear the appSource
$rootScope.$on('$routeChangeStart', resetAppSource);
Expand All @@ -26,9 +40,7 @@ define(function (require) {
* @return {Promise} - resolved with the current AppSource
*/
function getAppSource() {
return ensureDefaultLoaded().then(function () {
return appSource;
});
return appSource;
}

/**
Expand All @@ -48,19 +60,7 @@ define(function (require) {
literalRoot.inherits(globalSource);
}

/**
* Get the default index from the config, and hook it up to the globalSource. Broken out
* so that it can be called on config change.
*
* @return {Promise}
*/
function __loadDefaultPattern__() {
var defId = config.get('defaultIndex');

return Promise.cast(defId && indexPatterns.get(defId)).then(function (pattern) {
globalSource.set('index', pattern);
});
}


/**
* Sets the appSource to be a new, empty, SearchSource
Expand All @@ -72,7 +72,8 @@ define(function (require) {

return {
get: getAppSource,
set: setAppSource
set: setAppSource,
loadDefault: loadDefaultPattern
};
};
});
20 changes: 12 additions & 8 deletions src/kibana/components/courier/data_source/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ define(function (require) {
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;
Expand All @@ -18,6 +15,15 @@ define(function (require) {
}
inherits(SearchSource, SourceAbstract);

// expose a ready state for the route setup to read
var rootSearchSource;
SearchSource.ready = new Promise(function (resolve) {
require(['components/courier/data_source/_root_search_source'], function (PromiseModule) {
rootSearchSource = Private(PromiseModule);
resolve();
});
});

/*****
* PUBLIC API
*****/
Expand Down Expand Up @@ -70,11 +76,9 @@ define(function (require) {
*/
SearchSource.prototype.getParent = function (onlyHardLinked) {
var self = this;
return getRootSourcePromise.then(function (rootSearchSource) {
if (self._parent === false) return false;
if (self._parent) return self._parent;
return onlyHardLinked ? undefined : rootSearchSource.get();
});
if (self._parent === false) return false;
if (self._parent) return self._parent;
return onlyHardLinked ? undefined : rootSearchSource.get();
};

/**
Expand Down
Loading