Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]Support for date range in report generation #524

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -23,33 +23,35 @@
} from '../../utils/utils';
import { definitionInputValidation } from '../utils/utils';

interface reportParamsType {
interface ReportParamsType {
report_name: string;
report_source: string;
description: string;
core_params: visualReportParams | dataReportParams;
core_params: VisualReportParams | DataReportParams;
}
interface visualReportParams {
interface VisualReportParams {
base_url: string;
report_format: string;
header: string;
footer: string;
time_duration: string;
timeRangeParams: TimeRangeParams;
}

interface dataReportParams {
interface DataReportParams {
saved_search_id: number;
base_url: string;
report_format: string;
time_duration: string;
timeRangeParams: TimeRangeParams;
}
interface triggerType {
interface TriggerType {
trigger_type: string;
trigger_params?: any;

Check warning on line 50 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}

interface deliveryType {
configIds: Array<string>;
interface DeliveryType {
configIds: string[];
title: string;
textDescription: string;
htmlDescription: string;
Expand Down Expand Up @@ -77,21 +79,26 @@
};
}

export interface reportDefinitionParams {
report_params: reportParamsType;
delivery: deliveryType;
trigger: triggerType;
export interface ReportDefinitionParams {
report_params: ReportParamsType;
delivery: DeliveryType;
trigger: TriggerType;
}

export interface timeRangeParams {
export interface TimeRangeParams {
timeFrom: Date;
timeTo: Date;
}

export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; httpClient?: any; chrome: any }) {
export function CreateReport(props: {
[x: string]: any;

Check warning on line 94 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
setBreadcrumbs?: any;

Check warning on line 95 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
httpClient?: any;

Check warning on line 96 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
chrome: any;

Check warning on line 97 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}) {
const { chrome } = props;

let createReportDefinitionRequest: reportDefinitionParams = {
let createReportDefinitionRequest: ReportDefinitionParams = {
report_params: {
report_name: '',
report_source: '',
Expand All @@ -106,7 +113,7 @@
configIds: [],
title: '',
textDescription: '',
htmlDescription: ''
htmlDescription: '',
},
trigger: {
trigger_type: '',
Expand Down Expand Up @@ -192,39 +199,21 @@
addErrorOnCreateToastHandler(errorType);
};

const addInvalidTimeRangeToastHandler = () => {
const errorToast = {
title: i18n.translate(
'opensearch.reports.createReportDefinition.error.invalidTimeRange',
{ defaultMessage: 'Invalid time range selected.' }
),
color: 'danger',
iconType: 'alert',
id: 'timeRangeErrorToast',
};
// @ts-ignore
setToasts(toasts.concat(errorToast));
};

const handleInvalidTimeRangeToast = () => {
addInvalidTimeRangeToastHandler();
};

const removeToast = (removedToast: { id: string; }) => {
const removeToast = (removedToast: { id: string }) => {
setToasts(toasts.filter((toast: any) => toast.id !== removedToast.id));

Check warning on line 203 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
};

let timeRange = {
const NewTimeRange = {
timeFrom: new Date(),
timeTo: new Date(),
};

const createNewReportDefinition = async (
metadata: reportDefinitionParams,
timeRange: timeRangeParams
metadata: ReportDefinitionParams,
timeRange: TimeRangeParams
) => {
const { httpClient } = props;
//TODO: need better handle

Check failure on line 216 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Expected exception block, space or tab after '//' in comment
if (
metadata.trigger.trigger_type === 'On demand' &&
metadata.trigger.trigger_params !== undefined
Expand All @@ -243,7 +232,7 @@
setShowTriggerIntervalNaNError,
timeRange,
setShowTimeRangeError,
setShowCronError,
setShowCronError
).then((response) => {
error = response;
});
Expand All @@ -259,16 +248,20 @@
'Content-Type': 'application/json',
},
})
.then(async (resp: { scheduler_response: { reportDefinitionId: string; }; }) => {
//TODO: consider handle the on demand report generation from server side instead
if (metadata.trigger.trigger_type === 'On demand') {
const reportDefinitionId =
resp.scheduler_response.reportDefinitionId;
generateReportFromDefinitionId(reportDefinitionId, httpClient);
.then(
async (resp: {
scheduler_response: { reportDefinitionId: string };
}) => {
//TODO: consider handle the on demand report generation from server side instead

Check failure on line 255 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

Expected exception block, space or tab after '//' in comment
if (metadata.trigger.trigger_type === 'On demand') {
const reportDefinitionId =
resp.scheduler_response.reportDefinitionId;
generateReportFromDefinitionId(reportDefinitionId, httpClient);
}
window.location.assign(`reports-dashboards#/create=success`);
}
window.location.assign(`reports-dashboards#/create=success`);
})
.catch((error: {body: { statusCode: number; }; }) => {
)
.catch((error: { body: { statusCode: number } }) => {

Check failure on line 264 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

'error' is already declared in the upper scope
console.log('error in creating report definition: ' + error);
if (error.body.statusCode === 403) {
handleErrorOnCreateToast('permissions');
Expand Down Expand Up @@ -297,25 +290,29 @@
href: '#/create',
},
]);
}, []);

Check warning on line 293 in public/components/report_definitions/create/create_report_definition.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect

return (
<div>
<EuiPageBody>
<EuiTitle>
<h1>
{!getNavGroupEnabled && i18n.translate('opensearch.reports.createReportDefinition.title', {
defaultMessage: 'Create report definition',
})}
{!getNavGroupEnabled &&
i18n.translate(
'opensearch.reports.createReportDefinition.title',
{
defaultMessage: 'Create report definition',
}
)}
</h1>
</EuiTitle>
{!getNavGroupEnabled && <EuiSpacer size='s' />}
{!getNavGroupEnabled && <EuiSpacer size="s" />}
<ReportSettings
edit={false}
editDefinitionId={''} // empty string since we are coming from create
reportDefinitionRequest={createReportDefinitionRequest}
httpClientProps={props['httpClient']}
timeRange={timeRange}
httpClientProps={props.httpClient}
timeRange={NewTimeRange}
showSettingsReportNameError={showSettingsReportNameError}
settingsReportNameErrorMessage={settingsReportNameErrorMessage}
showSettingsReportSourceError={showSettingsReportSourceError}
Expand Down Expand Up @@ -344,7 +341,7 @@
onClick={() =>
createNewReportDefinition(
createReportDefinitionRequest,
timeRange
NewTimeRange
)
}
id={'createNewReportDefinition'}
Expand Down
4 changes: 2 additions & 2 deletions public/components/report_definitions/delivery/delivery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
testMessageFailureMessage,
} from './delivery_constants';
import 'react-mde/lib/styles/css/react-mde-all.css';
import { reportDefinitionParams } from '../create/create_report_definition';
import { ReportDefinitionParams } from '../create/create_report_definition';
import ReactMDE from 'react-mde';
import { converter } from '../utils';
import { getAvailableNotificationsChannels } from '../../main/main_utils';
Expand All @@ -41,7 +41,7 @@ export let includeDelivery = false;
export type ReportDeliveryProps = {
edit: boolean;
editDefinitionId: string;
reportDefinitionRequest: reportDefinitionParams;
reportDefinitionRequest: ReportDefinitionParams;
httpClientProps: any;
showDeliveryChannelError: boolean;
deliveryChannelError: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import {
import ReactMde from 'react-mde';
import 'react-mde/lib/styles/css/react-mde-all.css';
import {
reportDefinitionParams,
timeRangeParams,
ReportDefinitionParams,
TimeRangeParams,
} from '../create/create_report_definition';
import {
parseInContextUrl,
Expand All @@ -59,9 +59,9 @@ import { ReportTrigger } from '../report_trigger';
type ReportSettingProps = {
edit: boolean;
editDefinitionId: string;
reportDefinitionRequest: reportDefinitionParams;
reportDefinitionRequest: ReportDefinitionParams;
httpClientProps: any;
timeRange: timeRangeParams;
timeRange: TimeRangeParams;
showSettingsReportNameError: boolean;
settingsReportNameErrorMessage: string;
showSettingsReportSourceError: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
EuiCompressedFieldNumber,
} from '@elastic/eui';
import moment, { Moment } from 'moment';
import { reportDefinitionParams } from '../create/create_report_definition';
import { ReportDefinitionParams } from '../create/create_report_definition';
import {
SCHEDULE_RECURRING_OPTIONS,
INTERVAL_TIME_PERIODS,
Expand All @@ -36,7 +36,7 @@ import { TimezoneSelect } from './timezone';
type ReportTriggerProps = {
edit: boolean;
editDefinitionId: string;
reportDefinitionRequest: reportDefinitionParams;
reportDefinitionRequest: ReportDefinitionParams;
httpClientProps: any;
showTriggerIntervalNaNError: boolean;
showCronError: boolean;
Expand Down
6 changes: 5 additions & 1 deletion public/components/report_definitions/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ export const definitionInputValidation = async (

// if time range is invalid
const nowDate = new Date(moment.now());
if (timeRange.timeFrom > timeRange.timeTo || timeRange.timeTo > nowDate) {
if (timeRange.timeFrom > timeRange.timeTo || timeRange.timeTo > nowDate) {
setShowTimeRangeError(true);
error = true;
}
else{
metadata.report_params.core_params.timeFrom = timeRange.timeFrom;
metadata.report_params.core_params.timeTo = timeRange.timeTo;
}

// if cron based and cron input is invalid
if (
Expand Down
2 changes: 2 additions & 0 deletions server/model/backendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export type BackendReportDefinitionType = {
format: {
duration: string;
fileFormat: BACKEND_REPORT_FORMAT;
timeFrom: string;
timeTo: string;
limit?: number;
header?: string;
footer?: string;
Expand Down
28 changes: 28 additions & 0 deletions server/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ export const dataReportSchema = schema.object({
}
},
}),
timeFrom: schema.string({
validate(value) {
if (isNaN(Date.parse(value))) {
return `invalid timeFrom: ${value}`;
}
},
}),
timeTo: schema.string({
validate(value) {
if (isNaN(Date.parse(value))) {
return `invalid timeTo: ${value}`;
}
},
}),
report_format: schema.oneOf([schema.literal(FORMAT.csv), schema.literal(FORMAT.xlsx)]),
limit: schema.number({ defaultValue: DEFAULT_MAX_SIZE, min: 0 }),
excel: schema.boolean({ defaultValue: true }),
Expand Down Expand Up @@ -73,6 +87,20 @@ export const visualReportSchema = schema.object({
}
},
}),
timeFrom: schema.string({
validate(value) {
if (isNaN(Date.parse(value))) {
return `invalid timeFrom: ${value}`;
}
},
}),
timeTo: schema.string({
validate(value) {
if (isNaN(Date.parse(value))) {
return `invalid timeTo: ${value}`;
}
},
}),
});

export const intervalSchema = schema.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const input = {
limit: 10000,
excel: true,
origin: 'http://localhost:5601',
timeFrom : '2012-07-29T09:23:55.300Z',
timeTo: '2020-07-29T06:43:55.301Z',
},
},
delivery: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const input: BackendReportInstanceType = {
fileFormat: BACKEND_REPORT_FORMAT.pdf,
header: '<p>test header</p>',
footer: '<p>fake footer</p>',
timeFrom: "2020-11-11T09:52:00.000Z",
timeTo: "2020-11-11T10:22:00.000Z"
},
trigger: {
triggerType: BACKEND_TRIGGER_TYPE.cronSchedule,
Expand Down Expand Up @@ -85,6 +87,8 @@ const output = {
origin: 'http://localhost:5601',
window_width: 1600,
window_height: 800,
timeFrom: "2020-11-11T09:52:00.000Z",
timeTo: "2020-11-11T10:22:00.000Z"
},
},
trigger: {
Expand Down
Loading
Loading