From 9037839c641f2aa0b0cbe4ac4f09ae0d495a66f1 Mon Sep 17 00:00:00 2001 From: Jean-Louis Leysens Date: Thu, 24 Feb 2022 11:25:56 +0100 Subject: [PATCH] [Reporting] Move error_code to output (#126044) * move error_code to output * first version of api integration test for error_code * clean up old error_code field and make exisint one optional * use correct mapping format * fix api integration test Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../plugins/reporting/common/types/index.ts | 2 +- .../reporting/server/lib/store/mapping.ts | 2 +- .../reporting/server/lib/store/store.ts | 1 - .../server/lib/tasks/execute_report.ts | 2 +- .../reporting_and_security/error_codes.ts | 43 +++++++++++++++++++ .../reporting_and_security/index.ts | 1 + .../services/scenarios.ts | 18 ++++++++ .../services/usage.ts | 7 +-- 8 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 x-pack/test/reporting_api_integration/reporting_and_security/error_codes.ts diff --git a/x-pack/plugins/reporting/common/types/index.ts b/x-pack/plugins/reporting/common/types/index.ts index f8fefd790aa3b1..42845297e204e8 100644 --- a/x-pack/plugins/reporting/common/types/index.ts +++ b/x-pack/plugins/reporting/common/types/index.ts @@ -32,6 +32,7 @@ export interface ReportDocumentHead { export interface ReportOutput extends TaskRunResult { content: string | null; + error_code?: string; size: number; } @@ -71,7 +72,6 @@ export interface ReportSource { */ jobtype: string; // refers to `ExportTypeDefinition.jobType` created_by: string | false; // username or `false` if security is disabled. Used for ensuring users can only access the reports they've created. - error_code?: string; payload: BasePayload; meta: { // for telemetry diff --git a/x-pack/plugins/reporting/server/lib/store/mapping.ts b/x-pack/plugins/reporting/server/lib/store/mapping.ts index db088aa99f1e12..9accfd0c751846 100644 --- a/x-pack/plugins/reporting/server/lib/store/mapping.ts +++ b/x-pack/plugins/reporting/server/lib/store/mapping.ts @@ -44,7 +44,6 @@ export const mapping = { created_at: { type: 'date' }, started_at: { type: 'date' }, completed_at: { type: 'date' }, - error_code: { type: 'keyword' }, attempts: { type: 'short' }, max_attempts: { type: 'short' }, kibana_name: { type: 'keyword' }, @@ -54,6 +53,7 @@ export const mapping = { output: { type: 'object', properties: { + error_code: { type: 'keyword' }, chunk: { type: 'long' }, content_type: { type: 'keyword' }, size: { type: 'long' }, diff --git a/x-pack/plugins/reporting/server/lib/store/store.ts b/x-pack/plugins/reporting/server/lib/store/store.ts index 29f61d8b2a364b..41fdd9580c996c 100644 --- a/x-pack/plugins/reporting/server/lib/store/store.ts +++ b/x-pack/plugins/reporting/server/lib/store/store.ts @@ -34,7 +34,6 @@ export type ReportProcessingFields = Required<{ export type ReportFailedFields = Required<{ completed_at: Report['completed_at']; output: ReportOutput | null; - error_code: undefined | string; }>; export type ReportCompletedFields = Required<{ diff --git a/x-pack/plugins/reporting/server/lib/tasks/execute_report.ts b/x-pack/plugins/reporting/server/lib/tasks/execute_report.ts index 7b380bc5ab353f..aeeb79a85ce981 100644 --- a/x-pack/plugins/reporting/server/lib/tasks/execute_report.ts +++ b/x-pack/plugins/reporting/server/lib/tasks/execute_report.ts @@ -211,7 +211,6 @@ export class ExecuteReportTask implements ReportingTask { const doc: ReportFailedFields = { completed_at: completedTime, output: docOutput ?? null, - error_code: error?.code, }; return await store.setReportFailed(report, doc); @@ -233,6 +232,7 @@ export class ExecuteReportTask implements ReportingTask { docOutput.content = output.toString() || defaultOutput; docOutput.content_type = unknownMime; docOutput.warnings = [output.details ?? output.toString()]; + docOutput.error_code = output.code; } return docOutput; diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/error_codes.ts b/x-pack/test/reporting_api_integration/reporting_and_security/error_codes.ts new file mode 100644 index 00000000000000..2ddeeb711600ed --- /dev/null +++ b/x-pack/test/reporting_api_integration/reporting_and_security/error_codes.ts @@ -0,0 +1,43 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import expect from '@kbn/expect'; +import { ReportApiJSON } from '../../../plugins/reporting/common/types'; +import { FtrProviderContext } from '../ftr_provider_context'; + +// eslint-disable-next-line import/no-default-export +export default function ({ getService }: FtrProviderContext) { + const reportingAPI = getService('reportingAPI'); + + describe('Reporting error codes', () => { + it('places error_code in report output', async () => { + await reportingAPI.initEcommerce(); + + const { body: reportApiJson, status } = await reportingAPI.generateCsv({ + title: 'CSV Report', + browserTimezone: 'UTC', + objectType: 'search', + version: '7.15.0', + searchSource: null, // Invalid searchSource that should cause job to throw at execute phase... + } as any); + expect(status).to.be(200); + + const { job: report, path: downloadPath } = reportApiJson as { + job: ReportApiJSON; + path: string; + }; + + // wait for the the pending job to complete + await reportingAPI.waitForJobToFinish(downloadPath, true); + + expect(await reportingAPI.getJobErrorCode(report.id)).to.be('unknown_error'); + + await reportingAPI.teardownEcommerce(); + await reportingAPI.deleteAllReports(); + }); + }); +} diff --git a/x-pack/test/reporting_api_integration/reporting_and_security/index.ts b/x-pack/test/reporting_api_integration/reporting_and_security/index.ts index 02a2915fffd604..4cff15dc9f4443 100644 --- a/x-pack/test/reporting_api_integration/reporting_and_security/index.ts +++ b/x-pack/test/reporting_api_integration/reporting_and_security/index.ts @@ -29,5 +29,6 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) { loadTestFile(require.resolve('./spaces')); loadTestFile(require.resolve('./usage')); loadTestFile(require.resolve('./ilm_migration_apis')); + loadTestFile(require.resolve('./error_codes')); }); } diff --git a/x-pack/test/reporting_api_integration/services/scenarios.ts b/x-pack/test/reporting_api_integration/services/scenarios.ts index 20099eb2eb1e1a..be72233a26ffc3 100644 --- a/x-pack/test/reporting_api_integration/services/scenarios.ts +++ b/x-pack/test/reporting_api_integration/services/scenarios.ts @@ -164,6 +164,7 @@ export function createScenarios({ getService }: Pick { const jobParams = rison.encode(job as object as RisonValue); + return await supertestWithoutAuth .post(`/api/reporting/generate/csv_searchsource`) .auth(username, password) @@ -191,6 +192,22 @@ export function createScenarios({ getService }: Pick => { + const { + body: [job], + } = await supertestWithoutAuth + .get(`/api/reporting/jobs/list?page=0&ids=${id}`) + .auth(username, password) + .set('kbn-xsrf', 'xxx') + .send() + .expect(200); + return job?.output?.error_code; + }; + const deleteAllReports = async () => { log.debug('ReportingAPI.deleteAllReports'); @@ -271,5 +288,6 @@ export function createScenarios({ getService }: Pick