Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Use new API to Generate Reports from Existing Definitions #213

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
addReportDefinitionsTableContent,
removeDuplicatePdfFileFormat,
readStreamToFile,
generateReport,
generateReportFromDefinitionId,
generateReportById,
} from '../main_utils';
import {
Expand Down Expand Up @@ -114,8 +114,8 @@ describe('main_utils tests', () => {
});

test('test generateReport compile', () => {
const metadata = {};
generateReport(metadata, httpClientMock);
const reportDefinitionId = '1';
generateReportFromDefinitionId(reportDefinitionId, httpClientMock);
});

test('test generateReportById compile', () => {
Expand Down
7 changes: 3 additions & 4 deletions kibana-reports/public/components/main/main_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,16 @@ export const readStreamToFile = async (
document.body.removeChild(link);
};

export const generateReport = async (metadata, httpClient) => {
export const generateReportFromDefinitionId = async (reportDefinitionId, httpClient) => {
let status = false;
let permissionsError = false;
await httpClient
.post('../api/reporting/generateReport', {
body: JSON.stringify(metadata),
.post(`../api/reporting/generateReport/${reportDefinitionId}`, {
headers: {
'Content-Type': 'application/json',
},
})
.then(async (response) => {
.then(async (response: any) => {
// for emailing a report, this API response doesn't have response body
if (response) {
const fileFormat = extractFileFormat(response['filename']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
formatEmails,
trimAndRenderAsText,
} from '../report_details/report_details';
import { fileFormatsUpper, generateReport } from '../main_utils';
import { fileFormatsUpper, generateReportFromDefinitionId } from '../main_utils';
import { ReportDefinitionSchemaType } from '../../../../server/model';
import moment from 'moment';
import { converter } from '../../report_definitions/utils';
Expand Down Expand Up @@ -467,17 +467,9 @@ export function ReportDefinitionDetails(props) {
reportDefinitionRawResponse.report_definition.report_params.core_params
.time_duration;
const fromDate = getRelativeStartDate(duration);
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need all these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, good catch. Will remove

Copy link
Member

Choose a reason for hiding this comment

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

Can you verify if it's the same situation for logic in create_report_defintion.ts? I always have an impression that lots of code can be removed with this new API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean create_report_definition.tsx? If so, yes. From the code change in the PR, the only parameter that is created is a reportDefinitionId constant based on the response from the Create API call

let onDemandDownloadMetadata = {
query_url: `${
reportDefinitionDetails.baseUrl
}?_g=(time:(from:'${fromDate.toISOString()}',to:'${moment().toISOString()}'))`,
time_from: fromDate.valueOf(),
time_to: moment().valueOf(),
report_definition: reportDefinitionRawResponse.report_definition,
};
const { httpClient } = props;
let generateReportSuccess = await generateReport(
onDemandDownloadMetadata,
let generateReportSuccess = await generateReportFromDefinitionId(
reportDefinitionId,
httpClient
);
if (generateReportSuccess.status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { ReportSettings } from '../report_settings';
import { ReportDelivery } from '../delivery';
import { ReportTrigger } from '../report_trigger';
import { generateReport } from '../../main/main_utils';
import { generateReportFromDefinitionId } from '../../main/main_utils';
import { isValidCron } from 'cron-validator';
import { converter } from '../utils';
import moment from 'moment';
Expand Down Expand Up @@ -327,15 +327,8 @@ export function CreateReport(props) {
.then(async (resp) => {
//TODO: consider handle the on demand report generation from server side instead
if (metadata.trigger.trigger_type === 'On demand') {
let onDemandDownloadMetadata = {
query_url: `${
metadata.report_params.core_params.base_url
}?_g=(time:(from:'${timeRange.timeFrom.toISOString()}',to:'${timeRange.timeTo.toISOString()}'))`,
time_from: timeRange.timeFrom.valueOf(),
time_to: timeRange.timeTo.valueOf(),
report_definition: metadata,
};
generateReport(onDemandDownloadMetadata, httpClient);
const reportDefinitionId = resp.scheduler_response.reportDefinitionId;
generateReportFromDefinitionId(reportDefinitionId, httpClient);
}
window.location.assign(`opendistro_kibana_reports#/create=success`);
})
Expand Down