Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Fix Generating Reports with long jobParams RISON #45603

Merged
merged 21 commits into from Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,7 +6,6 @@

import rison from 'rison-node';
import chrome from 'ui/chrome';
import { QueryString } from 'ui/utils/query_string';
// @ts-ignore Untyped local.
import { fetch } from '../../../../common/lib/fetch';
import { CanvasWorkpad } from '../../../../types';
Expand Down Expand Up @@ -54,13 +53,15 @@ export function getPdfUrl(
title,
};

return `${reportingEntry}/printablePdf?${QueryString.param(
'jobParams',
rison.encode(jobParams)
)}`;
return {
createPdfUri: `${reportingEntry}/printablePdf`,
createPdfPayload: {
jobParams: rison.encode(jobParams),
},
};
}

export function createPdf(...args: Arguments) {
const createPdfUri = getPdfUrl(...args);
return fetch.post(createPdfUri);
const { createPdfUri, createPdfPayload } = getPdfUrl(...args);
return fetch.post(createPdfUri, createPdfPayload);
}
12 changes: 8 additions & 4 deletions x-pack/legacy/plugins/reporting/public/lib/reporting_client.ts
Expand Up @@ -27,10 +27,14 @@ class ReportingClient {
};

public createReportingJob = async (exportType: string, jobParams: any) => {
const query = {
jobParams: rison.encode(jobParams),
};
const resp = await kfetch({ method: 'POST', pathname: `${API_BASE_URL}/${exportType}`, query });
const jobParamsRison = rison.encode(jobParams);
const resp = await kfetch({
method: 'POST',
pathname: `${API_BASE_URL}/${exportType}`,
body: JSON.stringify({
jobParams: jobParamsRison,
}),
});
jobCompletionNotifications.add(resp.job.id);
return resp;
};
Expand Down
Expand Up @@ -5,6 +5,7 @@
*/

import boom from 'boom';
import Joi from 'joi';
import { Request, ResponseToolkit } from 'hapi';
import rison from 'rison-node';
import { API_BASE_URL } from '../../common/constants';
Expand All @@ -14,7 +15,7 @@ import { HandlerErrorFunction, HandlerFunction } from './types';

const BASE_GENERATE = `${API_BASE_URL}/generate`;

export function registerGenerate(
export function registerGenerateFromJobParams(
server: KbnServer,
handler: HandlerFunction,
handleError: HandlerErrorFunction
Expand All @@ -25,13 +26,45 @@ export function registerGenerate(
server.route({
path: `${BASE_GENERATE}/{exportType}`,
method: 'POST',
config: getRouteConfig(request => request.params.exportType),
config: {
...getRouteConfig(request => request.params.exportType),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I like this method better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an unrelated small fix I can make to validate the exportType param is registered in the exportTypesRegistry as an ID of one of the types

validate: {
params: Joi.object({
exportType: Joi.string().required(),
}).required(),
payload: Joi.object({
jobParams: Joi.string()
.optional()
.default(null),
}).allow(null), // allow optional payload
query: Joi.object({
jobParams: Joi.string().default(null),
}).default(),
},
},
handler: async (request: Request, h: ResponseToolkit) => {
let jobParamsRison: string | null;

if (request.payload) {
const { jobParams: jobParamsPayload } = request.payload as { jobParams: string };
jobParamsRison = jobParamsPayload;
} else {
const { jobParams: queryJobParams } = request.query as { jobParams: string };
if (queryJobParams) {
jobParamsRison = queryJobParams;
} else {
jobParamsRison = null;
}
}

if (!jobParamsRison) {
throw boom.badRequest('A jobParams RISON string is required');
}

const { exportType } = request.params;
let response;
try {
// @ts-ignore
const jobParams = rison.decode(request.query.jobParams);
const jobParams = rison.decode(jobParamsRison);
response = await handler(exportType, jobParams, request, h);
} catch (err) {
throw handleError(exportType, err);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/reporting/server/routes/index.ts
Expand Up @@ -9,7 +9,7 @@ import { Request, ResponseToolkit } from 'hapi';
import { API_BASE_URL } from '../../common/constants';
import { KbnServer, Logger } from '../../types';
import { enqueueJobFactory } from '../lib/enqueue_job';
import { registerGenerate } from './generate';
import { registerGenerateFromJobParams } from './generate_from_jobparams';
import { registerGenerateCsvFromSavedObject } from './generate_from_savedobject';
import { registerGenerateCsvFromSavedObjectImmediate } from './generate_from_savedobject_immediate';
import { registerJobs } from './jobs';
Expand Down Expand Up @@ -60,7 +60,7 @@ export function registerRoutes(server: KbnServer, logger: Logger) {
return err;
}

registerGenerate(server, handler, handleError);
registerGenerateFromJobParams(server, handler, handleError);
registerLegacy(server, handler, handleError);

// Register beta panel-action download-related API's
Expand Down
80 changes: 80 additions & 0 deletions x-pack/test/reporting/api/generate/csv_job_params.ts
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙇

import expect from '@kbn/expect';
import supertest from 'supertest';

// eslint-disable-next-line import/no-default-export
export default function({ getService }: { getService: any }) {
const esArchiver = getService('esArchiver');
const supertestSvc = getService('supertest');
const generateAPI = {
getCsvFromParamsInPayload: async (jobParams: object = {}) => {
return await supertestSvc
.post(`/api/reporting/generate/csv`)
.set('kbn-xsrf', 'xxx')
.send(jobParams);
},
getCsvFromParamsInQueryString: async (jobParams: string = '') => {
return await supertestSvc
.post(`/api/reporting/generate/csv${jobParams}`)
.set('kbn-xsrf', 'xxx')
.send(jobParams);
},
};

describe('Generation from Job Params', () => {
it('Rejects bogus jobParams', async () => {
// load test data that contains a saved search and documents
await esArchiver.load('reporting/logs');

const { status: resStatus, text: resText } = (await generateAPI.getCsvFromParamsInPayload({
jobParams: 0,
})) as supertest.Response;

expect(resStatus).to.eql(400);
expect(resText).to.match(/\\\"jobParams\\\" must be a string/);

await esArchiver.unload('reporting/logs');
});

it('Rejects empty jobParams', async () => {
// load test data that contains a saved search and documents
await esArchiver.load('reporting/logs');

const {
status: resStatus,
text: resText,
} = (await generateAPI.getCsvFromParamsInPayload()) as supertest.Response;

expect(resStatus).to.eql(400);
expect(resText).to.match(/jobParams RISON string is required/);

await esArchiver.unload('reporting/logs');
});

it('Accepts jobParams in POST payload', async () => {
// load test data that contains a saved search and documents
await esArchiver.load('reporting/logs');
const { status: resStatus } = (await generateAPI.getCsvFromParamsInPayload({
jobParams:
"(conflictedTypesFields:!(),fields:!('@date',_id,_index,_score,_type,country,metric,name),indexPatternId:b6a10720-ce76-11e9-aa85-47efb3fa905c,metaFields:!(_source,_id,_type,_index,_score),searchRequest:(body:(_source:(excludes:!()),docvalue_fields:!((field:'@date',format:date_time)),query:(bool:(filter:!((match_all:()),(range:('@date':(format:strict_date_optional_time,gte:'2004-01-01T07:00:00.000Z',lte:'2019-09-13T00:43:32.540Z')))),must:!(),must_not:!(),should:!())),script_fields:(),sort:!(('@date':(order:desc,unmapped_type:boolean))),stored_fields:!('*'),version:!t),index:tests),title:'New Saved Search 4',type:search)",
})) as supertest.Response;
expect(resStatus).to.eql(200);
await esArchiver.unload('reporting/logs');
});

it('Accepts jobParams in query string', async () => {
// load test data that contains a saved search and documents
await esArchiver.load('reporting/logs');
const { status: resStatus } = (await generateAPI.getCsvFromParamsInQueryString(
'?jobParams=(conflictedTypesFields:!(),fields:!(%27@date%27,_id,_index,_score,_type,country,metric,name),indexPatternId:b6a10720-ce76-11e9-aa85-47efb3fa905c,metaFields:!(_source,_id,_type,_index,_score),searchRequest:(body:(_source:(excludes:!()),docvalue_fields:!((field:%27@date%27,format:date_time)),query:(bool:(filter:!((match_all:()),(range:(%27@date%27:(format:strict_date_optional_time,gte:%272004-01-01T07:00:00.000Z%27,lte:%272019-09-13T00:43:32.540Z%27)))),must:!(),must_not:!(),should:!())),script_fields:(),sort:!((%27@date%27:(order:desc,unmapped_type:boolean))),stored_fields:!(%27*%27),version:!t),index:tests),title:%27New%20Saved%20Search%204%27,type:search)'
)) as supertest.Response;
expect(resStatus).to.eql(200);
await esArchiver.unload('reporting/logs');
});
});
}
1 change: 1 addition & 0 deletions x-pack/test/reporting/api/generate/index.js
Expand Up @@ -8,5 +8,6 @@ export default function ({ loadTestFile }) {
describe('CSV', function () {
this.tags('ciGroup2');
loadTestFile(require.resolve('./csv_saved_search'));
loadTestFile(require.resolve('./csv_job_params'));
});
}
2 changes: 1 addition & 1 deletion x-pack/test/reporting/functional/reporting.js
Expand Up @@ -73,7 +73,7 @@ export default function ({ getService, getPageObjects }) {
await expectDisabledGenerateReportButton();
});

it('becomes available when saved', async () => {
it.skip('becomes available when saved', async () => {
await PageObjects.dashboard.saveDashboard('mypdfdash');
await PageObjects.reporting.openPdfReportingPanel();
await expectEnabledGenerateReportButton();
Expand Down