Skip to content

Commit

Permalink
feat: support every 10 and 30 seconds intervals for lightweight monit…
Browse files Browse the repository at this point in the history
…ors (#932)

* support seconds schedule

* update snapshot
  • Loading branch information
shahzad31 authored Jun 13, 2024
1 parent ff303fb commit edaef90
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion __tests__/push/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Run 'npx @elastic/synthetics init' to create project with default settings.
exports[`Push error on invalid schedule 1`] = `
"Aborted. Invalid synthetics project settings.
Set default schedule(12) to one of the allowed values - 1,3,5,10,15,20,30,60,120,240
Set default schedule(12) to one of the allowed values - 1,2,3,5,10,15,20,30,60,120,240
Run 'npx @elastic/synthetics init' to create project with default settings.
"
Expand Down
9 changes: 5 additions & 4 deletions __tests__/push/monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ describe('Monitors', () => {
expect(() => parseSchedule('* * * *')).toThrowError(
`Monitor schedule format(* * * *) not supported: use '@every' syntax instead`
);
expect(parseSchedule('@every 4s')).toBe(1);
expect(parseSchedule('@every 4s')).toBe('10s');
expect(parseSchedule('@every 40s')).toBe('30s');
expect(parseSchedule('@every 70s')).toBe(1);
expect(parseSchedule('@every 121s')).toBe(3);
expect(parseSchedule('@every 121s')).toBe(2);
expect(parseSchedule('@every 1m')).toBe(1);
expect(parseSchedule('@every 2m30s')).toBe(3);
expect(parseSchedule('@every 2m30s')).toBe(2);
expect(parseSchedule('@every 181s')).toBe(3);
expect(parseSchedule('@every 2m10s')).toBe(3);
expect(parseSchedule('@every 2m10s')).toBe(2);
expect(parseSchedule('@every 4m25s')).toBe(5);
expect(parseSchedule('@every 16m')).toBe(15);
expect(parseSchedule('@every 24m')).toBe(20);
Expand Down
2 changes: 1 addition & 1 deletion src/dsl/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const SyntheticsLocations = Object.keys(
LocationsMap
) as SyntheticsLocationsType[];
export const ALLOWED_SCHEDULES = [
1, 3, 5, 10, 15, 20, 30, 60, 120, 240,
1, 2, 3, 5, 10, 15, 20, 30, 60, 120, 240,
] as const;

export interface AlertConfig {
Expand Down
20 changes: 15 additions & 5 deletions src/push/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ export function buildMonitorFromYaml(
...normalizeConfig(config),
retestOnFailure,
privateLocations,
schedule: schedule || options.schedule,
schedule:
(schedule as typeof ALLOWED_SCHEDULES[number]) || options.schedule,
alert: alertConfig,
});

Expand All @@ -285,7 +286,7 @@ export function buildMonitorFromYaml(
return mon;
}

// Deletes unncessary fields from the lightweight monitor config
// Deletes unnecessary fields from the lightweight monitor config
// that is not supported by the Kibana API
function normalizeConfig(config: MonitorConfig) {
delete config['private_locations'];
Expand Down Expand Up @@ -354,14 +355,19 @@ export function parseSchedule(schedule: string) {
// split between non-digit (\D) and a digit (\d)
const durations = duration.split(/(?<=\D)(?=\d)/g);
let minutes = 0;
let seconds = 0;
for (const dur of durations) {
// split between a digit and non-digit
const [value, format] = dur.split(/(?<=\d)(?=\D)/g);
// Calculate based on the duration symbol
const scheduleValue = parseInt(value, 10);
switch (format) {
case 's':
minutes += Math.round(scheduleValue / 60);
if (scheduleValue < 60) {
seconds += scheduleValue;
} else {
minutes += Math.round(scheduleValue / 60);
}
break;
case 'm':
minutes += scheduleValue;
Expand All @@ -374,12 +380,16 @@ export function parseSchedule(schedule: string) {
break;
}
}
return nearestSchedule(minutes);
return nearestSchedule(minutes, seconds);
}

// Find the nearest schedule that is supported by the platform
// from the parsed schedule value
function nearestSchedule(minutes) {
function nearestSchedule(minutes: number, seconds: number) {
if (seconds > 0 && minutes === 0) {
// we allow only 10 and 30 seconds, return the nearest one
return seconds < 20 ? '10s' : '30s';
}
let nearest: typeof ALLOWED_SCHEDULES[number] = ALLOWED_SCHEDULES[0];
let prev = Math.abs(nearest - minutes);
for (let i = 1; i < ALLOWED_SCHEDULES.length; i++) {
Expand Down

0 comments on commit edaef90

Please sign in to comment.