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

Use saved object client for saved_object_loader find function #12083

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 });
}

Expand All @@ -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;
});
4 changes: 2 additions & 2 deletions src/core_plugins/timelion/public/services/saved_sheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ savedObjectManagementRegistry.register({
});

// 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 });
};
Expand Down
69 changes: 39 additions & 30 deletions src/ui/public/courier/saved_object/saved_object_loader.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -21,6 +22,8 @@ export class SavedObjectLoader {
noun: StringUtils.upperFirst(this.type),
nouns: `${ this.lowercaseType }s`,
};

this.savedObjectsClient = new SavedObjectsClient($http);
}

/**
Expand Down Expand Up @@ -48,17 +51,27 @@ 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.
* @param hit
* @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) {
Expand All @@ -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.
Expand All @@ -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))
};
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/public/saved_objects/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down