Skip to content

Commit

Permalink
Fixes per request by PR reviewers
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed May 26, 2017
1 parent 0eb79e2 commit 5cef778
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -59,19 +59,19 @@ 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 });
});
});

it('should return an unique list of objects', () => {
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 },
]);
});
});
Expand Down
21 changes: 9 additions & 12 deletions src/core_plugins/kibana/server/lib/export/collect_dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, []);
});
}

Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}

Expand All @@ -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);
});
});

}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/core_plugins/kibana/server/routes/api/export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down

0 comments on commit 5cef778

Please sign in to comment.