From 5cef778a52305be9af4166f79e3cc44b95939d58 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 26 May 2017 10:27:42 -0700 Subject: [PATCH] Fixes per request by PR reviewers --- .../lib/export/__test__/collect_dashboards.js | 28 +++++++++---------- .../server/lib/export/collect_dashboards.js | 21 ++++++-------- .../lib/export/collect_index_patterns.js | 4 +-- .../lib/export/collect_search_sources.js | 18 ++++++------ .../server/lib/import/import_dashboards.js | 6 ++-- .../kibana/server/routes/api/export/index.js | 5 ++-- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/core_plugins/kibana/server/lib/export/__test__/collect_dashboards.js b/src/core_plugins/kibana/server/lib/export/__test__/collect_dashboards.js index 0ef284199e434..21121506337bb 100644 --- a/src/core_plugins/kibana/server/lib/export/__test__/collect_dashboards.js +++ b/src/core_plugins/kibana/server/lib/export/__test__/collect_dashboards.js @@ -2,26 +2,26 @@ import sinon from 'sinon'; import collectDashboards, { deps } from '../collect_dashboards'; import { expect } from 'chai'; -describe('collectDashbaords(req, ids)', () => { +describe('collectDashboards(req, ids)', () => { let req; let requestStub; let collectPanelsStub; beforeEach(() => { requestStub = sinon.stub().returns(Promise.resolve({ - docs: [ { _id: 'dashboard-01' }, { _id: 'dashboard-02' } ] + docs: [ { _id: 'dashboard-01', found: true }, { _id: 'dashboard-02', found: true } ] })); collectPanelsStub = sinon.stub(deps, 'collectPanels'); collectPanelsStub.onFirstCall().returns(Promise.resolve([ - { _id: 'dashboard-01' }, - { _id: 'panel-01' }, - { _id: 'index-*' } + { _id: 'dashboard-01', found: true }, + { _id: 'panel-01', found: true }, + { _id: 'index-*', found: true } ])); collectPanelsStub.onSecondCall().returns(Promise.resolve([ - { _id: 'dashboard-02' }, - { _id: 'panel-01' }, - { _id: 'index-*' } + { _id: 'dashboard-02', found: true }, + { _id: 'panel-01', found: true }, + { _id: 'index-*', found: true } ])); req = { @@ -59,8 +59,8 @@ describe('collectDashbaords(req, ids)', () => { return collectDashboards(req, ['dashboard-01', 'dashboard-02']) .then(() => { expect(collectPanelsStub.calledTwice).to.equal(true); - expect(collectPanelsStub.args[0][1]).to.eql({ _id: 'dashboard-01' }); - expect(collectPanelsStub.args[1][1]).to.eql({ _id: 'dashboard-02' }); + expect(collectPanelsStub.args[0][1]).to.eql({ _id: 'dashboard-01', found: true }); + expect(collectPanelsStub.args[1][1]).to.eql({ _id: 'dashboard-02', found: true }); }); }); @@ -68,10 +68,10 @@ describe('collectDashbaords(req, ids)', () => { return collectDashboards(req, ['dashboard-01', 'dashboard-02']) .then(results => { expect(results).to.eql([ - { _id: 'dashboard-01' }, - { _id: 'panel-01' }, - { _id: 'index-*' }, - { _id: 'dashboard-02' }, + { _id: 'dashboard-01', found: true }, + { _id: 'panel-01', found: true }, + { _id: 'index-*', found: true }, + { _id: 'dashboard-02', found: true }, ]); }); }); diff --git a/src/core_plugins/kibana/server/lib/export/collect_dashboards.js b/src/core_plugins/kibana/server/lib/export/collect_dashboards.js index 271da9a0820b7..4a1f6028e248b 100644 --- a/src/core_plugins/kibana/server/lib/export/collect_dashboards.js +++ b/src/core_plugins/kibana/server/lib/export/collect_dashboards.js @@ -13,18 +13,15 @@ export default function collectDashboards(req, ids) { body: { ids } }; return callWithRequest(req, 'mget', params) - .then(resp => { - return Promise.all(resp.docs.map(d => deps.collectPanels(req, d))) - .then(results => { - return results.reduce((acc, result) => { - return acc.concat(result); - }, []).reduce((acc, obj) => { - if (!acc.find(o => o._id === obj._id)) { - acc.push(obj); - } - return acc; - }, []); - }); + .then(resp => resp.docs.filter(d => d.found)) + .then(docs => Promise.all(docs.map(d => deps.collectPanels(req, d)))) + .then(results => { + return results + .reduce((acc, result) => acc.concat(result), []) + .reduce((acc, obj) => { + if (!acc.find(o => o._id === obj._id)) acc.push(obj); + return acc; + }, []); }); } diff --git a/src/core_plugins/kibana/server/lib/export/collect_index_patterns.js b/src/core_plugins/kibana/server/lib/export/collect_index_patterns.js index 04fad640e319a..2a445dfb30aa3 100644 --- a/src/core_plugins/kibana/server/lib/export/collect_index_patterns.js +++ b/src/core_plugins/kibana/server/lib/export/collect_index_patterns.js @@ -11,9 +11,9 @@ export default function collectIndexPatterns(req, panels) { } } return acc; - }, []).filter(id => id); + }, []); - if (!ids.length) { + if (ids.length === 0) { return Promise.resolve([]); } diff --git a/src/core_plugins/kibana/server/lib/export/collect_search_sources.js b/src/core_plugins/kibana/server/lib/export/collect_search_sources.js index 44af599716e86..920a61a03f712 100644 --- a/src/core_plugins/kibana/server/lib/export/collect_search_sources.js +++ b/src/core_plugins/kibana/server/lib/export/collect_search_sources.js @@ -12,9 +12,9 @@ export default function collectSearchSources(req, panels) { } } return acc; - }, []).filter(id => id); + }, []); - if (!ids.length) { + if (ids.length === 0) { return Promise.resolve([]); } @@ -25,11 +25,13 @@ export default function collectSearchSources(req, panels) { }; - return callWithRequest(req, 'mget', params).then(resp => resp.docs).then(savedSearches => { - return deps.collectIndexPatterns(req, savedSearches) - .then(resp => { - return savedSearches.concat(resp); - }); - }); + return callWithRequest(req, 'mget', params) + .then(resp => resp.docs) + .then(savedSearches => { + return deps.collectIndexPatterns(req, savedSearches) + .then(resp => { + return savedSearches.concat(resp); + }); + }); } diff --git a/src/core_plugins/kibana/server/lib/import/import_dashboards.js b/src/core_plugins/kibana/server/lib/import/import_dashboards.js index 5b893d45107ca..9b325b448c0d1 100644 --- a/src/core_plugins/kibana/server/lib/import/import_dashboards.js +++ b/src/core_plugins/kibana/server/lib/import/import_dashboards.js @@ -12,7 +12,7 @@ export default function importDashboards(req) { const config = req.server.config(); const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin'); const index = config.get('kibana.index'); - const force = req.query.force && _.includes([1, true, 'true', '1'], req.query.force); + const force = 'force' in req.query && req.query.force !== 'false'; const action = force ? 'index' : 'create'; const exclude = _.flatten([req.query.exclude]); @@ -22,9 +22,9 @@ export default function importDashboards(req) { } const body = payload.objects - .filter(item => !_.includes(exclude, item._type)) + .filter(item => !exclude.includes(item._type)) .reduce((acc, item) => { - if (_.includes(acceptableTypes, item._type)) { + if (acceptableTypes.includes(item._type)) { acc.push({ [action]: { _index: index, _type: item._type, _id: item._id } }); acc.push(item._source); } diff --git a/src/core_plugins/kibana/server/routes/api/export/index.js b/src/core_plugins/kibana/server/routes/api/export/index.js index 090410959fb76..abbe4115bc388 100644 --- a/src/core_plugins/kibana/server/routes/api/export/index.js +++ b/src/core_plugins/kibana/server/routes/api/export/index.js @@ -9,9 +9,10 @@ export default function exportApi(server) { const currentDate = moment.utc(); return exportDashboards(req) .then(resp => { - const json = JSON.stringify(resp, null, ' '); + const json = JSON.stringify(resp, null, ' '); + const filename = `kibana-dashboards.${currentDate.format('YYYY-MM-DD-HH-mm-ss')}.json`; reply(json) - .header('Content-Disposition', `attachment; filename="kibana-dashboards.${currentDate.format('YYYY-MM-DD-HH-mm-ss')}.json"`) + .header('Content-Disposition', `attachment; filename="${filename}"`) .header('Content-Type', 'application/json') .header('Content-Length', json.length); })