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] refactor routes files and helpers #30111

Merged
merged 19 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ export function createGenerateCsv(logger) {
settings
}) {
const escapeValue = createEscapeValue(settings.quoteValues);
const flattenHit = createFlattenHit(fields, metaFields, conflictedTypesFields);
const formatCsvValues = createFormatCsvValues(escapeValue, settings.separator, fields, formatsMap);

const builder = new MaxSizeStringBuilder(settings.maxSizeBytes);

const header = `${fields.map(escapeValue).join(settings.separator)}\n`;
if (!builder.tryAppend(header)) {
return {
Expand All @@ -40,6 +36,8 @@ export function createGenerateCsv(logger) {
const iterator = hitIterator(settings.scroll, callEndpoint, searchRequest, cancellationToken);
let maxSizeReached = false;

const flattenHit = createFlattenHit(fields, metaFields, conflictedTypesFields);
const formatCsvValues = createFormatCsvValues(escapeValue, settings.separator, fields, formatsMap);
try {
while (true) {
const { done, value: hit } = await iterator.next();
Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/reporting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import { resolve } from 'path';
import { UI_SETTINGS_CUSTOM_PDF_LOGO } from './common/constants';
import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status';
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice consolidation here

import { main as mainRoutes } from './server/routes/main';
import { jobs as jobRoutes } from './server/routes/jobs';
import { registerRoutes } from './server/routes';

import { createQueueFactory } from './server/lib/create_queue';
import { config as appConfig } from './server/config/config';
Expand Down Expand Up @@ -174,8 +173,7 @@ export const reporting = (kibana) => {
server.expose('queue', createQueueFactory(server));

// Reporting routes
mainRoutes(server);
jobRoutes(server);
registerRoutes(server);
},

deprecations: function ({ unused }) {
Expand Down
54 changes: 54 additions & 0 deletions x-pack/plugins/reporting/server/routes/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/

import boom from 'boom';
import { Request, ResponseToolkit } from 'hapi';
import rison from 'rison-node';
import { API_BASE_URL } from '../../common/constants';
import { KbnServer } from '../../types';
import { getRouteConfigFactoryReportingPre } from './lib/route_config_factories';
import { HandlerErrorFunction, HandlerFunction } from './types';

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

export function registerGenerate(
server: KbnServer,
handler: HandlerFunction,
handleError: HandlerErrorFunction
) {
const getRouteConfig = getRouteConfigFactoryReportingPre(server);

// generate report
server.route({
path: `${BASE_GENERATE}/{exportType}`,
method: 'POST',
config: getRouteConfig(request => request.params.exportType),
handler: async (request: Request, h: ResponseToolkit) => {
const { exportType } = request.params;
let response;
try {
// @ts-ignore
const jobParams = rison.decode(request.query.jobParams);
response = await handler(exportType, jobParams, request, h);
} catch (err) {
throw handleError(exportType, err);
}
return response;
},
});

// show error about GET method to user
server.route({
path: `${BASE_GENERATE}/{p*}`,
method: 'GET',
config: getRouteConfig(),
handler: () => {
const err = boom.methodNotAllowed('GET is not allowed');
err.output.headers.allow = 'POST';
return err;
},
});
}
57 changes: 57 additions & 0 deletions x-pack/plugins/reporting/server/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.
*/

import boom from 'boom';
import { Request, ResponseToolkit } from 'hapi';
import { API_BASE_URL } from '../../common/constants';
import { KbnServer } from '../../types';
// @ts-ignore
import { enqueueJobFactory } from '../lib/enqueue_job';
import { registerGenerate } from './generate';
import { registerJobs } from './jobs';
import { registerLegacy } from './legacy';

export function registerRoutes(server: KbnServer) {
const config = server.config();
const DOWNLOAD_BASE_URL = config.get('server.basePath') + `${API_BASE_URL}/jobs/download`;
const { errors: esErrors } = server.plugins.elasticsearch.getCluster('admin');
const enqueueJob = enqueueJobFactory(server);

async function handler(exportTypeId: any, jobParams: any, request: Request, h: ResponseToolkit) {
// @ts-ignore
const user = request.pre.user;
const headers = request.headers;

const job = await enqueueJob(exportTypeId, jobParams, user, headers, request);

// return the queue's job information
const jobJson = job.toJSON();

return h
.response({
path: `${DOWNLOAD_BASE_URL}/${jobJson.id}`,
job: jobJson,
})
.type('application/json');
}

function handleError(exportType: any, err: Error) {
if (err instanceof esErrors['401']) {
return boom.unauthorized(`Sorry, you aren't authenticated`);
}
if (err instanceof esErrors['403']) {
return boom.forbidden(`Sorry, you are not authorized to create ${exportType} reports`);
}
if (err instanceof esErrors['404']) {
return boom.boomify(err, { statusCode: 404 });
}
return err;
}

registerGenerate(server, handler, handleError);
registerLegacy(server, handler, handleError);
registerJobs(server);
}
Loading