Skip to content

Commit

Permalink
[Feature/Reporting] Export Saved Search CSV as Dashboard Panel Action
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Apr 4, 2019
1 parent 26add27 commit 59034e0
Show file tree
Hide file tree
Showing 46 changed files with 2,974 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class PanelActionsStore {
*/
public initializeFromRegistry(panelActionsRegistry: ContextMenuAction[]) {
panelActionsRegistry.forEach(panelAction => {
this.actions.push(panelAction);
if (!this.actions.includes(panelAction)) {
this.actions.push(panelAction);
}
});
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/legacy/ui/public/kfetch/kfetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ describe('kfetch', () => {
});
});

it('should return response', async () => {
it('should return JSON responses by default', async () => {
fetchMock.get('*', { foo: 'bar' });
const res = await kfetch({ pathname: 'my/path' });
expect(res).toEqual({ foo: 'bar' });
});

it('should not return JSON responses by defaul when `parseJson` is `false`', async () => {
fetchMock.get('*', { foo: 'bar' });
const raw = await kfetch({ pathname: 'my/path' }, { parseJson: false });
const res = await raw.text();
expect(res).toEqual('{"foo":"bar"}');
});

it('should prepend url with basepath by default', async () => {
fetchMock.get('*', {});
await kfetch({ pathname: 'my/path' });
Expand Down
8 changes: 5 additions & 3 deletions src/legacy/ui/public/kfetch/kfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface KFetchOptions extends RequestInit {

export interface KFetchKibanaOptions {
prependBasePath?: boolean;
parseJson?: boolean;
}

export interface Interceptor {
Expand All @@ -50,7 +51,7 @@ export const addInterceptor = (interceptor: Interceptor) => interceptors.push(in

export async function kfetch(
options: KFetchOptions,
{ prependBasePath = true }: KFetchKibanaOptions = {}
{ prependBasePath = true, parseJson = true }: KFetchKibanaOptions = {}
) {
const combinedOptions = withDefaultOptions(options);
const promise = requestInterceptors(combinedOptions).then(
Expand All @@ -61,10 +62,11 @@ export async function kfetch(
});

return window.fetch(fullUrl, restOptions).then(async res => {
const body = await getBodyAsJson(res);
const body = parseJson ? await getBodyAsJson(res) : null;
if (res.ok) {
return body;
return parseJson ? body : res;
}

throw new KFetchError(res, body);
});
}
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/reporting/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ export const PLUGIN_ID = 'reporting';
export const JOB_COMPLETION_NOTIFICATIONS_SESSION_KEY =
'xpack.reporting.jobCompletionNotifications';

export const API_BASE_URL = '/api/reporting';
export const API_BASE_URL = '/api/reporting'; // "Generation URL" from share menu
export const API_BASE_URL_V1 = '/api/reporting/v1'; //

export const CONTENT_TYPE_CSV = 'text/csv';

export const WHITELISTED_JOB_CONTENT_TYPES = [
'application/json',
'application/pdf',
'text/csv',
CONTENT_TYPE_CSV,
'image/png',
];

Expand All @@ -41,4 +44,5 @@ export const KIBANA_REPORTING_TYPE = 'reporting';
export const PDF_JOB_TYPE = 'printable_pdf';
export const PNG_JOB_TYPE = 'PNG';
export const CSV_JOB_TYPE = 'csv';
export const CSV_FROM_SAVEDOBJECT_JOB_TYPE = 'csv_from_savedobject';
export const USES_HEADLESS_JOB_TYPES = [PDF_JOB_TYPE, PNG_JOB_TYPE];
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ beforeEach(() => {
test(`fails if no URL is passed`, async () => {
await expect(
addForceNowQuerystring({
job: {},
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
},
server: mockServer,
})
).rejects.toBeDefined();
Expand All @@ -24,7 +32,17 @@ test(`fails if no URL is passed`, async () => {
test(`adds forceNow to hash's query, if it exists`, async () => {
const forceNow = '2000-01-01T00:00:00.000Z';
const { urls } = await addForceNowQuerystring({
job: { relativeUrl: '/app/kibana#/something', forceNow },
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
relativeUrl: '/app/kibana#/something',
forceNow,
},
server: mockServer,
});

Expand All @@ -38,6 +56,13 @@ test(`appends forceNow to hash's query, if it exists`, async () => {

const { urls } = await addForceNowQuerystring({
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
relativeUrl: '/app/kibana#/something?_g=something',
forceNow,
},
Expand All @@ -52,6 +77,13 @@ test(`appends forceNow to hash's query, if it exists`, async () => {
test(`doesn't append forceNow query to url, if it doesn't exists`, async () => {
const { urls } = await addForceNowQuerystring({
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
relativeUrl: '/app/kibana#/something',
},
server: mockServer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// @ts-ignore
import url from 'url';
import { getAbsoluteUrlFactory } from '../../../common/get_absolute_url';
import { ConditionalHeaders, KbnServer, ReportingJob } from '../../../types';
import { ConditionalHeaders, JobDocPayload, KbnServer } from '../../../types';

function getSavedObjectAbsoluteUrl(job: ReportingJob, relativeUrl: string, server: KbnServer) {
function getSavedObjectAbsoluteUrl(job: JobDocPayload, relativeUrl: string, server: KbnServer) {
const getAbsoluteUrl: any = getAbsoluteUrlFactory(server);

const { pathname: path, hash, search } = url.parse(relativeUrl);
Expand All @@ -21,7 +21,7 @@ export const addForceNowQuerystring = async ({
logo,
server,
}: {
job: ReportingJob;
job: JobDocPayload;
conditionalHeaders?: ConditionalHeaders;
logo?: any;
server: KbnServer;
Expand All @@ -34,7 +34,7 @@ export const addForceNowQuerystring = async ({
job.urls = [getSavedObjectAbsoluteUrl(job, job.relativeUrl, server)];
}

const urls = job.urls.map(jobUrl => {
const urls = job.urls.map((jobUrl: string) => {
if (!job.forceNow) {
return jobUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ describe('headers', () => {
test(`fails if it can't decrypt headers`, async () => {
await expect(
decryptJobHeaders({
job: { relativeUrl: '/app/kibana#/something', timeRange: {} },
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
relativeUrl: '/app/kibana#/something',
timeRange: {},
},
server: mockServer,
})
).rejects.toBeDefined();
Expand All @@ -37,7 +47,17 @@ describe('headers', () => {

const encryptedHeaders = await encryptHeaders(headers);
const { decryptedHeaders } = await decryptJobHeaders({
job: { relativeUrl: '/app/kibana#/something', headers: encryptedHeaders },
job: {
title: 'cool-job-bro',
type: 'csv',
jobParams: {
savedObjectId: 'abc-123',
isImmediate: false,
savedObjectType: 'search',
},
relativeUrl: '/app/kibana#/something',
headers: encryptedHeaders,
},
server: mockServer,
});
expect(decryptedHeaders).toEqual(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
// @ts-ignore
import { cryptoFactory } from '../../../server/lib/crypto';
import { CryptoFactory, KbnServer, ReportingJob } from '../../../types';
import { CryptoFactory, JobDocPayload, KbnServer } from '../../../types';

export const decryptJobHeaders = async ({
job,
server,
}: {
job: ReportingJob;
job: JobDocPayload;
server: KbnServer;
}) => {
const crypto: CryptoFactory = cryptoFactory(server);
Expand Down
Loading

0 comments on commit 59034e0

Please sign in to comment.