UNPKG

kibana-123

Version:

Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic

109 lines (96 loc) 2.65 kB
import _ from 'lodash'; import Scanner from 'ui/utils/scanner'; import { StringUtils } from 'ui/utils/string_utils'; export class SavedObjectLoader { constructor(SavedObjectClass, kbnIndex, esAdmin, kbnUrl) { this.type = SavedObjectClass.type; this.Class = SavedObjectClass; this.lowercaseType = this.type.toLowerCase(); this.kbnIndex = kbnIndex; this.kbnUrl = kbnUrl; this.esAdmin = esAdmin; this.scanner = new Scanner(esAdmin, { index: kbnIndex, type: this.lowercaseType }); this.loaderProperties = { name: `${ this.lowercaseType }s`, noun: StringUtils.upperFirst(this.type), nouns: `${ this.lowercaseType }s`, }; } /** * Retrieve a saved object by id. Returns a promise that completes when the object finishes * initializing. * @param id * @returns {Promise<SavedObject>} */ get(id) { return (new this.Class(id)).init(); } urlFor(id) { return this.kbnUrl.eval(`#/${ this.lowercaseType }/{{id}}`, { id: id }); } delete(ids) { ids = !_.isArray(ids) ? [ids] : ids; const deletions = ids.map(id => { const savedObject = new this.Class(id); return savedObject.delete(); }); return Promise.all(deletions); } /** * 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; } scanAll(queryString, pageSize = 1000) { return this.scanner.scanAndMap(queryString, { pageSize, docCount: Infinity }, (hit) => this.mapHits(hit)); } /** * TODO: Rather than use a hardcoded limit, implement pagination. See * https://github.com/elastic/kibana/issues/8044 for reference. * * @param searchString * @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.type.toLowerCase(), body, size }) .then((resp) => { return { total: resp.hits.total, hits: resp.hits.hits.map((hit) => this.mapHits(hit)) }; }); } }