Skip to content

Commit

Permalink
[Bug]Support for date range in report generation (#524)
Browse files Browse the repository at this point in the history
* Added parameters for supporting time from and time to in report generation

Signed-off-by: sumukhswamy <[email protected]>

* linter changes

Signed-off-by: sumukhswamy <[email protected]>

* linter changes

Signed-off-by: sumukhswamy <[email protected]>

* Update public/components/report_definitions/create/create_report_definition.tsx

Co-authored-by: Joshua Li <[email protected]>
Signed-off-by: Sumukh Swamy <[email protected]>

* linter suggestion change

Signed-off-by: sumukhswamy <[email protected]>

---------

Signed-off-by: sumukhswamy <[email protected]>
Signed-off-by: Sumukh Swamy <[email protected]>
Co-authored-by: Joshua Li <[email protected]>
  • Loading branch information
sumukhswamy and joshuali925 authored Feb 14, 2025
1 parent 057a619 commit daf3edb
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,35 @@ import {
} 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;
}

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

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;
setBreadcrumbs?: any;
httpClient?: any;
chrome: any;
}) {
const { chrome } = props;

let createReportDefinitionRequest: reportDefinitionParams = {
let createReportDefinitionRequest: ReportDefinitionParams = {
report_params: {
report_name: '',
report_source: '',
Expand All @@ -106,7 +113,7 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
configIds: [],
title: '',
textDescription: '',
htmlDescription: ''
htmlDescription: '',
},
trigger: {
trigger_type: '',
Expand Down Expand Up @@ -192,36 +199,18 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
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));
};

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
Expand All @@ -243,7 +232,7 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
setShowTriggerIntervalNaNError,
timeRange,
setShowTimeRangeError,
setShowCronError,
setShowCronError
).then((response) => {
error = response;
});
Expand All @@ -259,16 +248,20 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
'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
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 } }) => {
console.log('error in creating report definition: ' + error);
if (error.body.statusCode === 403) {
handleErrorOnCreateToast('permissions');
Expand Down Expand Up @@ -304,18 +297,22 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
<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 @@ export function CreateReport(props: { [x: string]: any; setBreadcrumbs?: any; ht
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
2 changes: 2 additions & 0 deletions server/routes/utils/__tests__/savedSearchReportHelper.test.ts
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
4 changes: 4 additions & 0 deletions server/routes/utils/converters/__tests__/backendToUi.test.ts
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

0 comments on commit daf3edb

Please sign in to comment.