Skip to content

Commit

Permalink
Use new API to Generate Reports from Existing Definitions (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcui1225 authored Nov 24, 2020
1 parent 4c3be6a commit 0bbab87
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
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 @@ -463,21 +463,9 @@ export function ReportDefinitionDetails(props) {
};

const generateReportFromDetails = async () => {
let duration =
reportDefinitionRawResponse.report_definition.report_params.core_params
.time_duration;
const fromDate = getRelativeStartDate(duration);
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

0 comments on commit 0bbab87

Please sign in to comment.