From 24b7060b65cd83acec05bc5535341aa1b9fdeab7 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 28 Jul 2015 11:52:41 -0700 Subject: [PATCH 1/3] Change importer to use saved object class, fix broken select all --- .../courier/saved_object/saved_object.js | 73 ++++++++++--------- .../settings/sections/objects/_objects.js | 39 ++++------ 2 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/kibana/components/courier/saved_object/saved_object.js b/src/kibana/components/courier/saved_object/saved_object.js index 00f71d2ec4728..275d124aa07d7 100644 --- a/src/kibana/components/courier/saved_object/saved_object.js +++ b/src/kibana/components/courier/saved_object/saved_object.js @@ -92,54 +92,55 @@ define(function (require) { // fetch the object from ES return docSource.fetch() - .then(function applyESResp(resp) { + .then(self.applyESResp); + }) + .then(function () { + return customInit.call(self); + }) + .then(function () { + // return our obj as the result of init() + return self; + }); + }); - self._source = _.cloneDeep(resp._source); + self.applyESResp = function (resp) { + self._source = _.cloneDeep(resp._source); - if (!resp.found) throw new errors.SavedObjectNotFound(type, self.id); + if (resp.found != null && !resp.found) throw new errors.SavedObjectNotFound(type, self.id); - var meta = resp._source.kibanaSavedObjectMeta || {}; - delete resp._source.kibanaSavedObjectMeta; + var meta = resp._source.kibanaSavedObjectMeta || {}; + delete resp._source.kibanaSavedObjectMeta; - if (!config.indexPattern && self._source.indexPattern) { - config.indexPattern = self._source.indexPattern; - delete self._source.indexPattern; - } + if (!config.indexPattern && self._source.indexPattern) { + config.indexPattern = self._source.indexPattern; + delete self._source.indexPattern; + } - // assign the defaults to the response - _.defaults(self._source, defaults); + // assign the defaults to the response + _.defaults(self._source, defaults); - // transform the source using _deserializers - _.forOwn(mapping, function ittr(fieldMapping, fieldName) { - if (fieldMapping._deserialize) { - self._source[fieldName] = fieldMapping._deserialize(self._source[fieldName], resp, fieldName, fieldMapping); - } - }); + // transform the source using _deserializers + _.forOwn(mapping, function ittr(fieldMapping, fieldName) { + if (fieldMapping._deserialize) { + self._source[fieldName] = fieldMapping._deserialize(self._source[fieldName], resp, fieldName, fieldMapping); + } + }); - // Give obj all of the values in _source.fields - _.assign(self, self._source); - - return Promise.try(function () { - parseSearchSource(meta.searchSourceJSON); - }) - .then(hydrateIndexPattern) - .then(function () { - return Promise.cast(afterESResp.call(self, resp)); - }) - .then(function () { - // Any time obj is updated, re-call applyESResp - docSource.onUpdate().then(applyESResp, notify.fatal); - }); - }); + // Give obj all of the values in _source.fields + _.assign(self, self._source); + + return Promise.try(function () { + parseSearchSource(meta.searchSourceJSON); }) + .then(hydrateIndexPattern) .then(function () { - return customInit.call(self); + return Promise.cast(afterESResp.call(self, resp)); }) .then(function () { - // return our obj as the result of init() - return self; + // Any time obj is updated, re-call applyESResp + docSource.onUpdate().then(self.applyESResp, notify.fatal); }); - }); + }; function parseSearchSource(searchSourceJson) { if (!self.searchSource) return; diff --git a/src/kibana/plugins/settings/sections/objects/_objects.js b/src/kibana/plugins/settings/sections/objects/_objects.js index f212daab63eb6..e8ea5ff50fd2a 100644 --- a/src/kibana/plugins/settings/sections/objects/_objects.js +++ b/src/kibana/plugins/settings/sections/objects/_objects.js @@ -13,7 +13,7 @@ define(function (require) { }); require('modules').get('apps/settings') - .directive('kbnSettingsObjects', function (config, Notifier, Private, kbnUrl) { + .directive('kbnSettingsObjects', function (config, Notifier, Private, kbnUrl, Promise) { return { restrict: 'E', controller: function ($scope, $injector, $q, AppState, es) { @@ -41,7 +41,7 @@ define(function (require) { $q.all(services).then(function (data) { $scope.services = _.sortBy(data, 'title'); var tab = $scope.services[0]; - if ($state.tab) tab = _.find($scope.services, {title: $state.tab}); + if ($state.tab) $scope.currentTab = tab = _.find($scope.services, {title: $state.tab}); $scope.$watch('state.tab', function (tab) { if (!tab) $scope.changeTab($scope.services[0]); @@ -124,32 +124,19 @@ define(function (require) { notify.error('The file could not be processed.'); } - return es.mget({ - index: config.file.kibana_index, - body: {docs: docs.map(_.partialRight(_.pick, '_id', '_type'))} - }) - .then(function (response) { - var existingDocs = _.where(response.docs, {found: true}); - var confirmMessage = 'The following objects will be overwritten:\n\n'; - if (existingDocs.length === 0 || window.confirm(confirmMessage + _.pluck(existingDocs, '_id').join('\n'))) { - return es.bulk({ - index: config.file.kibana_index, - body: _.flattenDeep(docs.map(transformToBulk)) - }) - .then(refreshIndex) - .then(refreshData, notify.error); - } - }); + return Promise.all(docs.map(function (doc) { + var service = _.find($scope.services, {type: doc._type}).service; + return service.get().then(function (obj) { + obj.id = doc._id; + return obj.applyESResp(doc).then(function () { + return obj.save(); + }); + }); + })) + .then(refreshIndex) + .then(refreshData, notify.error); }; - // Takes a doc and returns the associated two entries for an index bulk API request - function transformToBulk(doc) { - return [ - {index: _.pick(doc, '_id', '_type')}, - doc._source - ]; - } - function refreshIndex() { return es.indices.refresh({ index: config.file.kibana_index From b82f5b5f0c10f58b3e5447250b6f8d01dd2cb8f9 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 28 Jul 2015 13:23:22 -0700 Subject: [PATCH 2/3] Use Promise.map instead of Promise.all --- src/kibana/plugins/settings/sections/objects/_objects.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kibana/plugins/settings/sections/objects/_objects.js b/src/kibana/plugins/settings/sections/objects/_objects.js index e8ea5ff50fd2a..67590cbf3b0a1 100644 --- a/src/kibana/plugins/settings/sections/objects/_objects.js +++ b/src/kibana/plugins/settings/sections/objects/_objects.js @@ -124,7 +124,7 @@ define(function (require) { notify.error('The file could not be processed.'); } - return Promise.all(docs.map(function (doc) { + return Promise.map(docs, function (doc) { var service = _.find($scope.services, {type: doc._type}).service; return service.get().then(function (obj) { obj.id = doc._id; @@ -132,7 +132,7 @@ define(function (require) { return obj.save(); }); }); - })) + }) .then(refreshIndex) .then(refreshData, notify.error); }; From 796e64ba41c66cf83ddad6a6217ff299e78baf1a Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 28 Jul 2015 14:25:56 -0700 Subject: [PATCH 3/3] Reset file upload on click so that onchange fires if same file is uploaded multiple times --- src/kibana/directives/file_upload.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kibana/directives/file_upload.js b/src/kibana/directives/file_upload.js index bccdd50e2a4f7..4c281fb2c35e5 100644 --- a/src/kibana/directives/file_upload.js +++ b/src/kibana/directives/file_upload.js @@ -24,6 +24,7 @@ define(function (require) { }); $elem.on('click', function (e) { + $fileInput.val(null); $fileInput.trigger('click'); }); }