-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
saved_object_loader.js
108 lines (96 loc) · 2.58 KB
/
saved_object_loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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.lowercaseType,
body,
size
})
.then((resp) => {
return {
total: resp.hits.total,
hits: resp.hits.hits.map((hit) => this.mapHits(hit))
};
});
}
}