From a48aeb910f2e88030a4e190a186b2aab0cde4eb0 Mon Sep 17 00:00:00 2001 From: Stacey Gammon Date: Tue, 30 May 2017 16:49:41 -0400 Subject: [PATCH] Use saved object client for saved_object_loader find function --- .../saved_dashboard/saved_dashboards.js | 4 +- .../discover/saved_searches/saved_searches.js | 4 +- .../saved_visualizations.js | 17 +++-- .../timelion/public/services/saved_sheets.js | 4 +- .../saved_object/saved_object_loader.js | 69 +++++++++++-------- .../saved_objects/saved_objects_client.js | 3 +- 6 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboards.js b/src/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboards.js index 5eb6b53a67c7c..76607a88b2949 100644 --- a/src/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboards.js +++ b/src/core_plugins/kibana/public/dashboard/saved_dashboard/saved_dashboards.js @@ -13,6 +13,6 @@ savedObjectManagementRegistry.register({ }); // This is the only thing that gets injected into controllers -module.service('savedDashboards', function (SavedDashboard, kbnIndex, esAdmin, kbnUrl) { - return new SavedObjectLoader(SavedDashboard, kbnIndex, esAdmin, kbnUrl); +module.service('savedDashboards', function (SavedDashboard, kbnIndex, esAdmin, kbnUrl, $http) { + return new SavedObjectLoader(SavedDashboard, kbnIndex, esAdmin, kbnUrl, $http); }); diff --git a/src/core_plugins/kibana/public/discover/saved_searches/saved_searches.js b/src/core_plugins/kibana/public/discover/saved_searches/saved_searches.js index 1076ed06b161e..51bb21b6ef5d5 100644 --- a/src/core_plugins/kibana/public/discover/saved_searches/saved_searches.js +++ b/src/core_plugins/kibana/public/discover/saved_searches/saved_searches.js @@ -14,8 +14,8 @@ savedObjectManagementRegistry.register({ title: 'searches' }); -module.service('savedSearches', function (Promise, config, kbnIndex, esAdmin, createNotifier, SavedSearch, kbnUrl) { - const savedSearchLoader = new SavedObjectLoader(SavedSearch, kbnIndex, esAdmin, kbnUrl); +module.service('savedSearches', function (Promise, config, kbnIndex, esAdmin, createNotifier, SavedSearch, kbnUrl, $http) { + const savedSearchLoader = new SavedObjectLoader(SavedSearch, kbnIndex, esAdmin, kbnUrl, $http); // Customize loader properties since adding an 's' on type doesn't work for type 'search' . savedSearchLoader.loaderProperties = { name: 'searches', diff --git a/src/core_plugins/kibana/public/visualize/saved_visualizations/saved_visualizations.js b/src/core_plugins/kibana/public/visualize/saved_visualizations/saved_visualizations.js index e09e70350ebde..dbb0960e1aaeb 100644 --- a/src/core_plugins/kibana/public/visualize/saved_visualizations/saved_visualizations.js +++ b/src/core_plugins/kibana/public/visualize/saved_visualizations/saved_visualizations.js @@ -13,17 +13,17 @@ savedObjectManagementRegistry.register({ title: 'visualizations' }); -app.service('savedVisualizations', function (Promise, esAdmin, kbnIndex, SavedVis, Private, Notifier, kbnUrl) { +app.service('savedVisualizations', function (Promise, esAdmin, kbnIndex, SavedVis, Private, Notifier, kbnUrl, $http) { const visTypes = Private(VisTypesRegistryProvider); const notify = new Notifier({ location: 'Saved Visualization Service' }); - const saveVisualizationLoader = new SavedObjectLoader(SavedVis, kbnIndex, esAdmin, kbnUrl); - saveVisualizationLoader.mapHits = function (hit) { - const source = hit._source; - source.id = hit._id; - source.url = this.urlFor(hit._id); + const saveVisualizationLoader = new SavedObjectLoader(SavedVis, kbnIndex, esAdmin, kbnUrl, $http); + + saveVisualizationLoader.mapHitSource = function (source, id) { + source.id = id; + source.url = this.urlFor(id); let typeName = source.typeName; if (source.visState) { @@ -32,8 +32,8 @@ app.service('savedVisualizations', function (Promise, esAdmin, kbnIndex, SavedVi } if (!typeName || !visTypes.byName[typeName]) { - if (!typeName) notify.error('Visualization type is missing. Please add a type to this visualization.', hit); - else notify.error('Visualization type of "' + typeName + '" is invalid. Please change to a valid type.', hit); + if (!typeName) notify.error('Visualization type is missing. Please add a type to this visualization.', source); + else notify.error('Visualization type of "' + typeName + '" is invalid. Please change to a valid type.', source); return kbnUrl.redirect('/management/kibana/objects/savedVisualizations/{{id}}', { id: source.id }); } @@ -45,6 +45,5 @@ app.service('savedVisualizations', function (Promise, esAdmin, kbnIndex, SavedVi saveVisualizationLoader.urlFor = function (id) { return kbnUrl.eval('#/visualize/edit/{{id}}', { id: id }); }; - return saveVisualizationLoader; }); diff --git a/src/core_plugins/timelion/public/services/saved_sheets.js b/src/core_plugins/timelion/public/services/saved_sheets.js index 9e9441e233ad8..97965e04bae2a 100644 --- a/src/core_plugins/timelion/public/services/saved_sheets.js +++ b/src/core_plugins/timelion/public/services/saved_sheets.js @@ -15,8 +15,8 @@ define(function (require) { }); // This is the only thing that gets injected into controllers - module.service('savedSheets', function (Promise, SavedSheet, kbnIndex, esAdmin, kbnUrl) { - const savedSheetLoader = new SavedObjectLoader(SavedSheet, kbnIndex, esAdmin, kbnUrl); + module.service('savedSheets', function (Promise, SavedSheet, kbnIndex, esAdmin, kbnUrl, $http) { + const savedSheetLoader = new SavedObjectLoader(SavedSheet, kbnIndex, esAdmin, kbnUrl, $http); savedSheetLoader.urlFor = function (id) { return kbnUrl.eval('#/{{id}}', { id: id }); }; diff --git a/src/ui/public/courier/saved_object/saved_object_loader.js b/src/ui/public/courier/saved_object/saved_object_loader.js index a6b9e2794ac4f..df054eb2e9adf 100644 --- a/src/ui/public/courier/saved_object/saved_object_loader.js +++ b/src/ui/public/courier/saved_object/saved_object_loader.js @@ -1,9 +1,10 @@ import _ from 'lodash'; import { Scanner } from 'ui/utils/scanner'; import { StringUtils } from 'ui/utils/string_utils'; +import { SavedObjectsClient } from 'ui/saved_objects'; export class SavedObjectLoader { - constructor(SavedObjectClass, kbnIndex, esAdmin, kbnUrl) { + constructor(SavedObjectClass, kbnIndex, esAdmin, kbnUrl, $http) { this.type = SavedObjectClass.type; this.Class = SavedObjectClass; this.lowercaseType = this.type.toLowerCase(); @@ -21,6 +22,8 @@ export class SavedObjectLoader { noun: StringUtils.upperFirst(this.type), nouns: `${ this.lowercaseType }s`, }; + + this.savedObjectsClient = new SavedObjectsClient($http); } /** @@ -48,6 +51,19 @@ export class SavedObjectLoader { return Promise.all(deletions); } + /** + * Updates source to contain an id and url field, and returns the updated + * source object. + * @param source + * @param id + * @returns {source} The modified source object, with an id and url field. + */ + mapHitSource(source, id) { + source.id = id; + source.url = this.urlFor(id); + return source; + } + /** * Updates hit._source to contain an id and url field, and returns the updated * source object. @@ -55,10 +71,7 @@ export class SavedObjectLoader { * @returns {hit._source} The modified hit._source object, with an id and url field. */ mapHits(hit) { - const source = hit._source; - source.id = hit._id; - source.url = this.urlFor(hit._id); - return source; + return this.mapHitSource(hit._source, hit._id); } scanAll(queryString, pageSize = 1000) { @@ -68,6 +81,16 @@ export class SavedObjectLoader { }, (hit) => this.mapHits(hit)); } + /** + * Updates hit._attributes to contain an id and url field, and returns the updated + * attributes object. + * @param hit + * @returns {hit._attributes} The modified hit._attributes object, with an id and url field. + */ + mapSavedObjectApiHits(hit) { + return this.mapHitSource(hit._attributes, hit.id); + } + /** * TODO: Rather than use a hardcoded limit, implement pagination. See * https://github.com/elastic/kibana/issues/8044 for reference. @@ -76,32 +99,18 @@ export class SavedObjectLoader { * @param size * @returns {Promise} */ - find(searchString, size = 100) { - let body; - if (searchString) { - body = { - query: { - simple_query_string: { - query: searchString + '*', - fields: ['title^3', 'description'], - default_operator: 'AND' - } - } - }; - } else { - body = { query: { match_all: {} } }; - } - - return this.esAdmin.search({ - index: this.kbnIndex, - type: this.lowercaseType, - body, - size - }) - .then((resp) => { + find(search, size = 100) { + return this.savedObjectsClient.find( + { + type: this.lowercaseType, + search, + perPage: size, + page: 1, + searchFields: ['title^3', 'description'] + }).then((resp) => { return { - total: resp.hits.total, - hits: resp.hits.hits.map((hit) => this.mapHits(hit)) + total: resp.total, + hits: resp.savedObjects.map((savedObject) => this.mapSavedObjectApiHits(savedObject)) }; }); } diff --git a/src/ui/public/saved_objects/saved_objects_client.js b/src/ui/public/saved_objects/saved_objects_client.js index 04aef92a0b678..264c7d51bcb74 100644 --- a/src/ui/public/saved_objects/saved_objects_client.js +++ b/src/ui/public/saved_objects/saved_objects_client.js @@ -3,13 +3,14 @@ import { pick, get } from 'lodash'; import { keysToSnakeCaseShallow, keysToCamelCaseShallow } from '../../../utils/case_conversion'; import { SavedObject } from './saved_object'; +import chrome from 'ui/chrome'; const join = (...uriComponents) => ( uriComponents.filter(Boolean).map(encodeURIComponent).join('/') ); export class SavedObjectsClient { - constructor($http, basePath, PromiseCtor = Promise) { + constructor($http, basePath = chrome.getBasePath(), PromiseCtor = Promise) { this._$http = $http; this._apiBaseUrl = `${basePath}/api/saved_objects/`; this._PromiseCtor = PromiseCtor;