Skip to content
Merged
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 @@ -25,6 +25,7 @@ import {
transformResponse,
scheduledQueryFactory,
CreatedAtSearchResponse,
transformSingleResponse,
} from './scheduled_query';
import { ReportingCore } from '../../..';
import { ScheduledReportType } from '../../../types';
Expand Down Expand Up @@ -1216,6 +1217,81 @@ describe('transformResponse', () => {
});
});

it('should correctly transform a response with rrule.dtstart is in the future', () => {
jest.useFakeTimers();
jest.setSystemTime(new Date('2025-05-06T21:10:17.137Z'));

// current time is 2025-05-06T21:10:17.137Z which is a Tuesday
// schedule is set to run every Friday at 17:00 UTC
// start time is set to 2025-05-11T12:00:00.000Z (which is a Sunday)
// next run should be the next Friday after 2025-05-11T12:00:00.000Z which is 2025-05-16T17:00:00.000Z
// not the actual next friday which would be 2025-05-09T17:00:00.000Z
const dtstart = '2025-05-11T12:00:00.000Z';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation! 😅

expect(
transformSingleResponse(mockLogger, {
type: 'scheduled_report',
id: 'aa8b6fb3-cf61-4903-bce3-eec9ddc823ca',
namespaces: ['a-space'],
attributes: {
createdAt: '2025-05-06T21:10:17.137Z',
createdBy: 'elastic',
enabled: true,
jobType: 'printable_pdf_v2',
meta: {
isDeprecated: false,
layout: 'preserve_layout',
objectType: 'dashboard',
},
migrationVersion: '9.1.0',
title: '[Logs] Web Traffic',
payload,
schedule: {
rrule: {
dtstart,
freq: 3,
interval: 1,
byhour: [17],
byminute: [0],
byweekday: ['FR'],
tzid: 'UTC',
},
},
},
references: [],
managed: false,
updated_at: '2025-05-06T21:10:17.137Z',
created_at: '2025-05-06T21:10:17.137Z',
version: 'WzEsMV0=',
coreMigrationVersion: '8.8.0',
typeMigrationVersion: '10.1.0',
score: 0,
})
).toEqual({
id: 'aa8b6fb3-cf61-4903-bce3-eec9ddc823ca',
created_at: '2025-05-06T21:10:17.137Z',
created_by: 'elastic',
enabled: true,
jobtype: 'printable_pdf_v2',
next_run: '2025-05-16T17:00:00.000Z',
payload: jsonPayload,
schedule: {
rrule: {
dtstart,
freq: 3,
interval: 1,
byhour: [17],
byminute: [0],
byweekday: ['FR'],
tzid: 'UTC',
},
},
space_id: 'a-space',
title: '[Logs] Web Traffic',
});

jest.useRealTimers();
});

it('handles malformed payload', () => {
const malformedSo = {
...savedObjects[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,25 @@ export function transformSingleResponse(
);

const schedule = so.attributes.schedule;
const _rrule = new RRule({
...schedule.rrule,
dtstart: new Date(),
});

// get start date
let dtstart = new Date();
const rruleStart = schedule.rrule.dtstart;
if (rruleStart) {
try {
// if start date is provided and in the future, use it, otherwise use current time
const startDateValue = new Date(rruleStart).valueOf();
const now = Date.now();
if (startDateValue > now) {
dtstart = new Date(startDateValue + 60000); // add 1 minute to ensure it's in the future
}
} catch (e) {
logger.debug(
`Failed to parse rrule.dtstart for scheduled report next run calculation - default to now ${id}: ${e.message}`
);
}
}
const _rrule = new RRule({ ...schedule.rrule, dtstart });

let payload: ReportApiJSON['payload'] | undefined;
try {
Expand Down