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 2 #1617

Merged
merged 11 commits into from
Oct 9, 2014
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
70 changes: 27 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,32 @@ 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 +71,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
25 changes: 11 additions & 14 deletions src/kibana/components/courier/data_source/_abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ define(function (require) {
}
}());

this._dynamicState = this._dynamicState || {};

// set internal state values
this._methods.forEach(function (name) {
this[name] = function (val) {
Expand All @@ -50,13 +48,13 @@ define(function (require) {
/**
* Get values from the state
* @param {string} name - The name of the property desired
* @return {any} - the value found
*/
SourceAbstract.prototype.get = function (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;
current = current.getParent();
}
};

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

/**
Expand Down Expand Up @@ -229,13 +227,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 All @@ -250,8 +247,8 @@ define(function (require) {
/**
* Create a filter that can be reversed for filters with negate set
* @param {boolean} reverse This will reverse the filter. If true then
* anything where negate is set will come
* through otherwise it will filter out
* anything where negate is set will come
* through otherwise it will filter out
* @returns {function}
*/
var filterNegate = function (reverse) {
Expand Down
44 changes: 23 additions & 21 deletions src/kibana/components/courier/data_source/_root_search_source.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
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) {
pattern = pattern || undefined;
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 +41,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 +61,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 +73,8 @@ define(function (require) {

return {
get: getAppSource,
set: setAppSource
set: setAppSource,
loadDefault: loadDefaultPattern
};
};
});
Loading