Make a scan util

This commit is contained in:
Jonathan Budzenski 2015-12-08 15:58:49 -06:00
parent b53d48b49a
commit 7acd69563c
4 changed files with 103 additions and 114 deletions

View file

@ -1,6 +1,8 @@
define(function (require) {
var module = require('ui/modules').get('app/dashboard');
var _ = require('lodash');
var Scanner = require('ui/utils/scanner');
// bring in the factory
require('plugins/kibana/dashboard/services/_saved_dashboard');
@ -14,6 +16,11 @@ define(function (require) {
// This is the only thing that gets injected into controllers
module.service('savedDashboards', function (Promise, SavedDashboard, kbnIndex, es, kbnUrl) {
var scanner = new Scanner(es, {
index: kbnIndex,
type: 'dashboard'
});
this.type = SavedDashboard.type;
this.Class = SavedDashboard;
@ -42,44 +49,11 @@ define(function (require) {
});
};
this.scan = function (pageSize = 100, docCount = 1000) {
var allResults = {
hits: [],
total: 0
};
var self = this;
return new Promise(function (resolve, reject) {
es.search({
index: kbnIndex,
type: 'dashboard',
size: pageSize,
body: { query: {match_all: {}}},
searchType: 'scan',
scroll: '1m'
}, function getMoreUntilDone(error, response) {
var scanAllResults = docCount === Number.POSITIVE_INFINITY;
allResults.total = scanAllResults ? response.hits.total : Math.min(response.hits.total, docCount);
var hits = response.hits.hits
.slice(0, allResults.total - allResults.hits.length)
.map(self.mapHits.bind(self));
allResults.hits = allResults.hits.concat(hits);
var collectedAllResults = allResults.total === allResults.hits.length;
if (collectedAllResults) {
resolve(allResults);
} else {
es.scroll({
scrollId: response._scroll_id,
}, getMoreUntilDone);
}
});
});
};
this.scanAll = function (queryString, pageSize = 100) {
return this.scan(pageSize, Number.POSITIVE_INFINITY);
this.scanAll = function (queryString, pageSize = 1000) {
return scanner.scanAndMap(queryString, {
pageSize,
docCount: Number.POSITIVE_INFINITY
}, this.mapHits.bind(this));
};
this.mapHits = function (hit) {

View file

@ -1,5 +1,6 @@
define(function (require) {
var _ = require('lodash');
var Scanner = require('ui/utils/scanner');
require('plugins/kibana/discover/saved_searches/_saved_search');
require('ui/notify');
@ -16,7 +17,10 @@ define(function (require) {
});
module.service('savedSearches', function (Promise, config, kbnIndex, es, createNotifier, SavedSearch, kbnUrl) {
var scanner = new Scanner(es, {
index: kbnIndex,
type: 'search'
});
var notify = createNotifier({
location: 'Saved Searches'
@ -31,44 +35,12 @@ define(function (require) {
nouns: 'saved searches'
};
this.scan = function (pageSize = 100, docCount = 1000) {
var allResults = {
hits: [],
total: 0
};
var self = this;
return new Promise(function (resolve, reject) {
es.search({
index: kbnIndex,
type: 'search',
size: pageSize,
body: { query: {match_all: {}}},
searchType: 'scan',
scroll: '1m'
}, function getMoreUntilDone(error, response) {
var scanAllResults = docCount === Number.POSITIVE_INFINITY;
allResults.total = scanAllResults ? response.hits.total : Math.min(response.hits.total, docCount);
var hits = response.hits.hits
.slice(0, allResults.total - allResults.hits.length)
.map(self.mapHits.bind(self));
allResults.hits = allResults.hits.concat(hits);
var collectedAllResults = allResults.total === allResults.hits.length;
if (collectedAllResults) {
resolve(allResults);
} else {
es.scroll({
scrollId: response._scroll_id,
}, getMoreUntilDone);
}
});
});
};
this.scanAll = function (queryString, pageSize = 100) {
return this.scan(pageSize, Number.POSITIVE_INFINITY);
this.scanAll = function (queryString, pageSize = 1000) {
return scanner.scanAndMap(queryString, {
pageSize,
docCount: Number.POSITIVE_INFINITY
}, this.mapHits.bind(this));
};

View file

@ -1,6 +1,7 @@
define(function (require) {
var app = require('ui/modules').get('app/visualize');
var _ = require('lodash');
var Scanner = require('ui/utils/scanner');
require('plugins/kibana/visualize/saved_visualizations/_saved_vis');
@ -13,6 +14,12 @@ define(function (require) {
app.service('savedVisualizations', function (Promise, es, kbnIndex, SavedVis, Private, Notifier, kbnUrl) {
var visTypes = Private(require('ui/registry/vis_types'));
var scanner = new Scanner(es, {
index: kbnIndex,
type: 'visualization'
});
var notify = new Notifier({
location: 'Saved Visualization Service'
});
@ -41,44 +48,11 @@ define(function (require) {
});
};
this.scan = function (pageSize = 100, docCount = 1000) {
var allResults = {
hits: [],
total: 0
};
var self = this;
return new Promise(function (resolve, reject) {
es.search({
index: kbnIndex,
type: 'visualization',
size: pageSize,
body: { query: {match_all: {}}},
searchType: 'scan',
scroll: '1m'
}, function getMoreUntilDone(error, response) {
var scanAllResults = docCount === Number.POSITIVE_INFINITY;
allResults.total = scanAllResults ? response.hits.total : Math.min(response.hits.total, docCount);
var hits = response.hits.hits
.slice(0, allResults.total - allResults.hits.length)
.map(self.mapHits.bind(self));
allResults.hits = allResults.hits.concat(hits);
var collectedAllResults = allResults.total === allResults.hits.length;
if (collectedAllResults) {
resolve(allResults);
} else {
es.scroll({
scrollId: response._scroll_id,
}, getMoreUntilDone);
}
});
});
};
this.scanAll = function (queryString, pageSize = 100) {
return this.scan(pageSize, Number.POSITIVE_INFINITY);
this.scanAll = function (queryString, pageSize = 1000) {
return scanner.scanAndMap(queryString, {
pageSize,
docCount: Number.POSITIVE_INFINITY
}, this.mapHits.bind(this));
};
this.mapHits = function (hit) {

View file

@ -0,0 +1,69 @@
var _ = require('lodash');
var Scanner = function (client, {index, type}) {
if (!index) throw new Error('Expected index');
if (!type) throw new Error('Expected type');
if (!client) throw new Error('Expected client');
this.client = client;
this.index = index;
this.type = type;
};
Scanner.prototype.scanAndMap = function (searchString, options, mapFn) {
const opts = _.defaults({
pageSize: 100,
docCount: 1000
}, options);
var allResults = {
hits: [],
total: 0
};
var body;
if (searchString) {
body = {
query: {
simple_query_string: {
query: searchString + '*',
fields: ['title^3', 'description'],
default_operator: 'AND'
}
}
};
} else {
body = { query: {match_all: {}}};
}
return new Promise((resolve, reject) => {
this.client.search({
index: this.index,
type: this.type,
size: opts.pageSize,
body,
searchType: 'scan',
scroll: '1m'
}, function getMoreUntilDone(error, response) {
var scanAllResults = opts.docCount === Number.POSITIVE_INFINITY;
allResults.total = scanAllResults ? response.hits.total : Math.min(response.hits.total, opts.docCount);
var hits = response.hits.hits
.slice(0, allResults.total - allResults.hits.length);
if (mapFn) hits = hits.map(mapFn);
allResults.hits = allResults.hits.concat(hits);
var collectedAllResults = allResults.total === allResults.hits.length;
if (collectedAllResults) {
resolve(allResults);
} else {
this.client.scroll({
scrollId: response._scroll_id,
}, getMoreUntilDone);
}
});
});
};
export default Scanner;