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

Commit

Permalink
remove create report logic for background job (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu authored Nov 13, 2020
1 parent e980a2d commit 1e6ba4b
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 260 deletions.
65 changes: 34 additions & 31 deletions kibana-reports/server/executor/createScheduledReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
REPORT_TYPE,
REPORT_STATE,
LOCAL_HOST,
DELIVERY_TYPE,
} from '../routes/utils/constants';
import { ILegacyClusterClient, Logger } from '../../../../src/core/server';
import { createSavedSearchReport } from '../routes/utils/savedSearchReportHelper';
Expand All @@ -33,33 +34,36 @@ export const createScheduledReport = async (
esReportsClient: ILegacyClusterClient,
notificationClient: ILegacyClusterClient,
logger: Logger
): Promise<CreateReportResultType> => {
) => {
const isScheduledTask = true;
let createReportResult: CreateReportResultType;

const {
report_definition: { report_params: reportParams },
report_definition: {
report_params: reportParams,
delivery: { delivery_type: deliveryType },
},
} = report;
const { report_source: reportSource } = reportParams;

// compose url with localhost
const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`;
try {
// generate report
if (reportSource === REPORT_TYPE.savedSearch) {
createReportResult = await createSavedSearchReport(
report,
esClient,
isScheduledTask
);
} else {
// report source can only be one of [saved search, visualization, dashboard]
createReportResult = await createVisualReport(
reportParams,
completeQueryUrl,
logger
);
}
// TODO: generate report logic will be added back once we have the user impersonation
// if (reportSource === REPORT_TYPE.savedSearch) {
// createReportResult = await createSavedSearchReport(
// report,
// esClient,
// isScheduledTask
// );
// } else {
// // report source can only be one of [saved search, visualization, dashboard]
// // compose url with localhost
// const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`;
// createReportResult = await createVisualReport(
// reportParams,
// completeQueryUrl,
// logger
// );
// }

await updateReportState(
isScheduledTask,
Expand All @@ -69,26 +73,25 @@ export const createScheduledReport = async (
);

// deliver report
createReportResult = await deliverReport(
report,
createReportResult,
notificationClient,
esReportsClient,
reportId,
isScheduledTask,
logger
);
if (deliveryType == DELIVERY_TYPE.channel) {
await deliverReport(
report,
notificationClient,
esReportsClient,
reportId,
isScheduledTask,
logger
);
}
} catch (error) {
// update report instance with "error" state
//TODO: save error detail and display on UI
logger.error(`Failed to create scheduled report ${error}`);
await updateReportState(
isScheduledTask,
reportId,
esReportsClient,
REPORT_STATE.error
);
throw error;
}

return createReportResult;
};
9 changes: 5 additions & 4 deletions kibana-reports/server/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ async function pollAndExecuteJob(
`scheduled job sent from scheduler with report id: ${reportId}`
);

const reportData = await createScheduledReport(
await createScheduledReport(
reportId,
reportMetadata,
esClient,
esReportsClient,
notificationClient,
logger
);
logger.info('done executing job');
} else {
// 204 no content is returned, 204 doesn't have response body
logger.info(`no available job in queue ${JSON.stringify(esResp)}`);
logger.info(`No scheduled job to execute ${JSON.stringify(esResp)}`);
}
} catch (error) {
logger.error(`${error.statusCode} ${parseEsErrorResponse(error)}`);
logger.error(
`Failed to poll job ${error.statusCode} ${parseEsErrorResponse(error)}`
);
}
}

Expand Down
19 changes: 11 additions & 8 deletions kibana-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
REPORT_STATE,
LOCAL_HOST,
SECURITY_AUTH_COOKIE_NAME,
DELIVERY_TYPE,
} from '../utils/constants';

import {
Expand Down Expand Up @@ -66,12 +67,13 @@ export const createReport = async (
}

const {
report_definition: { report_params: reportParams },
report_definition: {
report_params: reportParams,
delivery: { delivery_type: deliveryType },
},
} = report;
const { report_source: reportSource } = reportParams;

// compose url
const queryUrl = `${LOCAL_HOST}${report.query_url}`;
try {
// generate report
if (reportSource === REPORT_TYPE.savedSearch) {
Expand All @@ -82,6 +84,8 @@ export const createReport = async (
);
} else {
// report source can only be one of [saved search, visualization, dashboard]
// compose url
const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`;
// Check if security is enabled. TODO: is there a better way to check?
let cookieObject: SetCookie | undefined;
if (request.headers.cookie) {
Expand All @@ -92,15 +96,15 @@ export const createReport = async (
cookieObject = {
name: cookie[0],
value: cookie[1],
url: queryUrl,
url: completeQueryUrl,
};
}
});
}

createReportResult = await createVisualReport(
reportParams,
queryUrl,
completeQueryUrl,
logger,
cookieObject
);
Expand All @@ -116,10 +120,9 @@ export const createReport = async (
}

// deliver report
if (!savedReportId) {
createReportResult = await deliverReport(
if (!savedReportId && deliveryType == DELIVERY_TYPE.channel) {
await deliverReport(
report,
createReportResult,
notificationClient,
esReportsClient,
reportId,
Expand Down
14 changes: 2 additions & 12 deletions kibana-reports/server/routes/lib/deliverReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import {
import { DELIVERY_TYPE, REPORT_STATE } from '../utils/constants';
import { composeEmbeddedHtml } from '../utils/notification/deliveryContentHelper';
import { callCluster } from '../utils/helpers';
import { CreateReportResultType } from '../utils/types';
import { updateReportState } from './updateReportState';

export const deliverReport = async (
report: ReportSchemaType,
reportData: CreateReportResultType,
notificationClient: ILegacyScopedClusterClient | ILegacyClusterClient,
esReportsClient: ILegacyClusterClient | ILegacyScopedClusterClient,
reportId: string,
Expand Down Expand Up @@ -117,21 +115,13 @@ export const deliverReport = async (
);
}
});
} else {
// empty kibana recipients array
//TODO: tmp solution
// @ts-ignore
if (!deliveryParams.kibana_recipients.length) {
return reportData;
}
}
// update report document

// update report state
await updateReportState(
isScheduledTask,
reportId,
esReportsClient,
REPORT_STATE.shared
);

return reportData;
};
Loading

0 comments on commit 1e6ba4b

Please sign in to comment.