diff --git a/kibana-reports/public/components/main/main_utils.tsx b/kibana-reports/public/components/main/main_utils.tsx index 3e2d8f21..cd5019d1 100644 --- a/kibana-reports/public/components/main/main_utils.tsx +++ b/kibana-reports/public/components/main/main_utils.tsx @@ -179,7 +179,7 @@ export const generateReportById = async ( handlePermissionsMissingToast ) => { await httpClient - .post(`../api/reporting/generateReport/${reportId}`) + .get(`../api/reporting/generateReport/${reportId}`) .then(async (response) => { //TODO: duplicate code, extract to be a function that can reuse. e.g. handleResponse(response) const fileFormat = extractFileFormat(response['filename']); diff --git a/kibana-reports/server/backend/opendistro-es-reports-plugin.ts b/kibana-reports/server/backend/opendistro-es-reports-plugin.ts index c11c9b52..69c684e1 100644 --- a/kibana-reports/server/backend/opendistro-es-reports-plugin.ts +++ b/kibana-reports/server/backend/opendistro-es-reports-plugin.ts @@ -32,6 +32,20 @@ export default function (Client: any, config: any, components: any) { needBody: true, }); + esReports.createReportFromDefinition = clientAction({ + url: { + fmt: `${ES_REPORTS_API.ON_DEMAND_REPORT}/<%=reportDefinitionId%>`, + req: { + reportDefinitionId: { + type: 'string', + required: true, + }, + }, + }, + method: 'POST', + needBody: true, + }); + esReports.updateReportInstanceStatus = clientAction({ url: { fmt: `${ES_REPORTS_API.REPORT_INSTANCE}/<%=reportInstanceId%>`, diff --git a/kibana-reports/server/plugin.ts b/kibana-reports/server/plugin.ts index 69cc3646..e0cd6e76 100644 --- a/kibana-reports/server/plugin.ts +++ b/kibana-reports/server/plugin.ts @@ -115,16 +115,16 @@ export class OpendistroKibanaReportsPlugin Polling at at a 5 min fixed interval TODO: need further optimization polling with a mix approach of - random delay and dynamic delay based on the amount of jobs + random delay and dynamic delay based on the amount of jobs. */ - setIntervalAsync( - pollAndExecuteJob, - POLL_INTERVAL, - esReportsClient, - notificationClient, - esClient, - this.logger - ); + // setIntervalAsync( + // pollAndExecuteJob, + // POLL_INTERVAL, + // esReportsClient, + // notificationClient, + // esClient, + // this.logger + // ); return {}; } diff --git a/kibana-reports/server/routes/lib/createReport.ts b/kibana-reports/server/routes/lib/createReport.ts index 6016cc2d..bf051e24 100644 --- a/kibana-reports/server/routes/lib/createReport.ts +++ b/kibana-reports/server/routes/lib/createReport.ts @@ -109,9 +109,10 @@ export const createReport = async ( ); } // update report state to "created" - if (!savedReportId) { - await updateReportState(reportId, esReportsClient, REPORT_STATE.created); - } + // TODO: temporarily remove the following + // if (!savedReportId) { + // await updateReportState(reportId, esReportsClient, REPORT_STATE.created); + // } // deliver report if (!savedReportId && deliveryType == DELIVERY_TYPE.channel) { @@ -125,10 +126,11 @@ export const createReport = async ( } } catch (error) { // update report instance with "error" state - //TODO: save error detail and display on UI - if (!savedReportId) { - await updateReportState(reportId, esReportsClient, REPORT_STATE.error); - } + // TODO: save error detail and display on UI + // TODO: temporarily disable the following, will add back + // if (!savedReportId) { + // await updateReportState(reportId, esReportsClient, REPORT_STATE.error); + // } throw error; } diff --git a/kibana-reports/server/routes/lib/deliverReport.ts b/kibana-reports/server/routes/lib/deliverReport.ts index 0b019272..7b38dc43 100644 --- a/kibana-reports/server/routes/lib/deliverReport.ts +++ b/kibana-reports/server/routes/lib/deliverReport.ts @@ -117,5 +117,6 @@ export const deliverReport = async ( } // update report state - await updateReportState(reportId, esReportsClient, REPORT_STATE.shared); + // TODO: temporarily disable the following, will add back + // await updateReportState(reportId, esReportsClient, REPORT_STATE.shared); }; diff --git a/kibana-reports/server/routes/lib/saveReport.ts b/kibana-reports/server/routes/lib/saveReport.ts index 0315838d..ff403f02 100644 --- a/kibana-reports/server/routes/lib/saveReport.ts +++ b/kibana-reports/server/routes/lib/saveReport.ts @@ -45,7 +45,9 @@ export const saveReport = async ( }, }, }, - status: BACKEND_REPORT_STATE.executing, // download from in-context menu should always pass executing state to backend + // download from in-context menu should always pass executing state to backend + // TODO: set to success, since update report status API in temporarily unavailable, need change back to pending later + status: BACKEND_REPORT_STATE.success, inContextDownloadUrlPath: queryUrl, }; diff --git a/kibana-reports/server/routes/report.ts b/kibana-reports/server/routes/report.ts index 2d8c8cbf..178d4eb4 100644 --- a/kibana-reports/server/routes/report.ts +++ b/kibana-reports/server/routes/report.ts @@ -32,7 +32,7 @@ import { } from './utils/converters/backendToUi'; export default function (router: IRouter) { - // generate report + // generate report (with provided metadata) router.post( { path: `${API_PREFIX}/generateReport`, @@ -85,8 +85,8 @@ export default function (router: IRouter) { } ); - // generate report from id - router.post( + // generate report from report id + router.get( { path: `${API_PREFIX}/generateReport/{reportId}`, validate: { @@ -140,6 +140,66 @@ export default function (router: IRouter) { } ); + // create report from existing report definition + router.post( + { + path: `${API_PREFIX}/generateReport/{reportDefinitionId}`, + validate: { + params: schema.object({ + reportDefinitionId: schema.string(), + }), + }, + }, + async ( + context, + request, + response + ): Promise> => { + //@ts-ignore + const logger: Logger = context.reporting_plugin.logger; + const reportDefinitionId = request.params.reportDefinitionId; + try { + // @ts-ignore + const esReportsClient: ILegacyScopedClusterClient = context.reporting_plugin.esReportsClient.asScoped( + request + ); + // call ES API to create report from definition + const esResp = await esReportsClient.callAsCurrentUser( + 'es_reports.createReportFromDefinition', + { + reportDefinitionId: reportDefinitionId, + body: { + reportDefinitionId: reportDefinitionId, + }, + } + ); + const reportId = esResp.reportInstance.id; + // convert report to use UI model + const report = backendToUiReport(esResp.reportInstance); + // generate report + const reportData = await createReport( + request, + context, + report, + reportId + ); + + return response.ok({ + body: { + data: reportData.dataUrl, + filename: reportData.fileName, + }, + }); + } catch (error) { + logger.error( + `Failed to generate report from reportDefinition id ${reportDefinitionId} : ${error}` + ); + logger.error(error); + return errorResponse(response, error); + } + } + ); + // get all reports details router.get( {