[Synthetics] Support project lightweight 10 and 30 seconds schedules (#185920)

## Summary

Companion PR to https://github.com/elastic/synthetics/pull/932
This commit is contained in:
Shahzad 2024-06-20 19:17:38 +02:00 committed by GitHub
parent f4f927b3d0
commit e3a85ddb39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 70 additions and 6 deletions

View file

@ -6,9 +6,23 @@
*/
import * as t from 'io-ts';
import { Either } from 'fp-ts/Either';
import { AlertConfigsCodec } from './alert_config';
import { ScreenshotOptionCodec } from './monitor_configs';
const ProjectMonitorSchedule = new t.Type<string | number, string | number, unknown>(
'ProjectMonitorSchedule',
t.string.is,
(input, context): Either<t.Errors, string | number> => {
if (typeof input === 'number' || input === '10s' || input === '30s') {
return t.success(input);
} else {
return t.failure(input, context);
}
},
t.identity
);
export const ProjectMonitorThrottlingConfigCodec = t.union([
t.interface({
download: t.number,
@ -23,7 +37,7 @@ export const ProjectMonitorCodec = t.intersection([
type: t.string,
id: t.string,
name: t.string,
schedule: t.number,
schedule: ProjectMonitorSchedule,
}),
t.partial({
content: t.string,

View file

@ -75,10 +75,7 @@ export const getNormalizeCommonFields = ({
[ConfigKey.JOURNEY_ID]: monitor.id || defaultFields[ConfigKey.JOURNEY_ID],
[ConfigKey.MONITOR_SOURCE_TYPE]: SourceType.PROJECT,
[ConfigKey.NAME]: monitor.name || '',
[ConfigKey.SCHEDULE]: {
number: `${monitor.schedule}`,
unit: ScheduleUnit.MINUTES,
},
[ConfigKey.SCHEDULE]: getMonitorSchedule(monitor.schedule, defaultFields[ConfigKey.SCHEDULE]),
[ConfigKey.PROJECT_ID]: projectId,
[ConfigKey.LOCATIONS]: monLocations,
[ConfigKey.TAGS]: getOptionalListField(monitor.tags) || defaultFields[ConfigKey.TAGS],
@ -145,8 +142,27 @@ export const getCustomHeartbeatId = (
return `${monitor.id}-${projectId}-${namespace}`;
};
export const getMonitorSchedule = (schedule: number | string | MonitorFields['schedule']) => {
export const getMonitorSchedule = (
schedule: number | string | MonitorFields['schedule'],
defaultValue?: MonitorFields['schedule']
) => {
if (!schedule && defaultValue) {
return defaultValue;
}
if (typeof schedule === 'number' || typeof schedule === 'string') {
if (typeof schedule === 'number') {
return {
number: `${schedule}`,
unit: ScheduleUnit.MINUTES,
};
}
if (schedule.includes('s')) {
return {
number: schedule.replace('s', ''),
unit: ScheduleUnit.SECONDS,
};
}
return {
number: `${schedule}`,
unit: ScheduleUnit.MINUTES,

View file

@ -194,6 +194,40 @@ describe('ProjectMonitorFormatter', () => {
updatedMonitors: [],
});
});
it('should return invalid schedule error', async () => {
const invalidMonitor = {
...testMonitors[0],
schedule: '3m',
};
const pushMonitorFormatter = new ProjectMonitorFormatter({
projectId: 'test-project',
spaceId: 'default',
routeContext,
encryptedSavedObjectsClient,
monitors: [invalidMonitor],
});
pushMonitorFormatter.getProjectMonitorsForProject = jest.fn().mockResolvedValue([]);
await pushMonitorFormatter.configureAllProjectMonitors();
expect({
createdMonitors: pushMonitorFormatter.createdMonitors,
updatedMonitors: pushMonitorFormatter.updatedMonitors,
failedMonitors: pushMonitorFormatter.failedMonitors,
}).toStrictEqual({
createdMonitors: [],
failedMonitors: [
{
details: 'Invalid value "3m" supplied to "schedule"',
id: 'check if title is present 10 0',
payload: invalidMonitor,
reason: "Couldn't save or update monitor because of an invalid configuration.",
},
],
updatedMonitors: [],
});
});
it('catches errors from bulk edit method', async () => {
soClient.bulkCreate.mockImplementation(async () => {