Skip to content

Commit

Permalink
fix: handle nested alert keys (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Jul 4, 2023
1 parent 1bdb16d commit a21cf7f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 79 deletions.
150 changes: 98 additions & 52 deletions __tests__/push/monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,58 +132,6 @@ describe('Monitors', () => {
expect(parseSchedule('@every 1d')).toBe(240);
});

it('parse alert config option', async () => {
expect(parseAlertConfig({})).toBe(undefined);
expect(parseAlertConfig({ 'alert.status.enabled': true } as any)).toEqual({
status: { enabled: true },
});
expect(parseAlertConfig({ alert: { status: { enabled: true } } })).toEqual({
status: { enabled: true },
});

expect(
parseAlertConfig(
{ alert: { status: { enabled: true } } },
{
status: { enabled: false },
tls: { enabled: true },
}
)
).toEqual({
status: { enabled: true },
tls: {
enabled: true,
},
});
});

it('parse tls alert config option', async () => {
expect(parseAlertConfig({})).toBe(undefined);
expect(
parseAlertConfig({
'alert.status.enabled': true,
'alert.tls.enabled': true,
} as any)
).toEqual({
status: { enabled: true },
tls: { enabled: true },
});
expect(parseAlertConfig({ 'alert.tls.enabled': true } as any)).toEqual({
tls: { enabled: true },
});
expect(
parseAlertConfig(
{ alert: { tls: { enabled: true } } },
{
status: { enabled: false },
}
)
).toEqual({
tls: { enabled: true },
status: { enabled: false },
});
});

describe('Lightweight monitors', () => {
const PROJECT_DIR = generateTempPath();
const HB_SOURCE = join(PROJECT_DIR, 'heartbeat.yml');
Expand Down Expand Up @@ -406,4 +354,102 @@ heartbeat.monitors:
});
});
});

describe('parseAlertConfig', () => {
it('parse alert config option', async () => {
expect(parseAlertConfig({})).toBe(undefined);
expect(parseAlertConfig({ 'alert.status.enabled': true } as any)).toEqual(
{
status: { enabled: true },
}
);
expect(
parseAlertConfig({ alert: { status: { enabled: true } } })
).toEqual({
status: { enabled: true },
});
});

it('parse alert config option when global config is also provided', async () => {
expect(
parseAlertConfig(
{ alert: { 'status.enabled': false, 'tls.enabled': true } } as any,
{
status: { enabled: false },
tls: { enabled: false },
}
)
).toEqual({
status: { enabled: false },
tls: {
enabled: true,
},
});
expect(
parseAlertConfig(
{ alert: { status: { enabled: true } } },
{
status: { enabled: false },
tls: { enabled: true },
}
)
).toEqual({
status: { enabled: true },
tls: {
enabled: true,
},
});
});

it('parse tls alert config option', async () => {
expect(parseAlertConfig({})).toBe(undefined);
expect(
parseAlertConfig({
'alert.status.enabled': true,
'alert.tls.enabled': true,
} as any)
).toEqual({
status: { enabled: true },
tls: { enabled: true },
});
expect(parseAlertConfig({ 'alert.tls.enabled': true } as any)).toEqual({
tls: { enabled: true },
});
expect(
parseAlertConfig(
{ alert: { tls: { enabled: true } } },
{
status: { enabled: false },
}
)
).toEqual({
tls: { enabled: true },
status: { enabled: false },
});
});

it('deletes parsed keys from config', async () => {
let config: any = {
'alert.status.enabled': true,
'alert.tls.enabled': true,
};
expect(parseAlertConfig(config)).toEqual({
status: { enabled: true },
tls: { enabled: true },
});
expect(config).toEqual({});

config = {
alert: {
'status.enabled': true,
'tls.enabled': true,
},
};
expect(parseAlertConfig(config)).toEqual({
status: { enabled: true },
tls: { enabled: true },
});
expect(config).toEqual({});
});
});
});
66 changes: 39 additions & 27 deletions src/push/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,42 +260,54 @@ export function buildMonitorFromYaml(

export const parseAlertConfig = (
config: MonitorConfig,
globalAlertConfig?: AlertConfig
gConfig?: AlertConfig
) => {
const alertConfig: AlertConfig = {};

if (config['alert.status.enabled'] !== undefined) {
const value = config['alert.status.enabled'];
delete config['alert.status.enabled'];
alertConfig['status'] = {
enabled: value,
};
// If the user has provided a global alert config, merge it with the monitor alert config
const status = getAlertKeyValue('status', config, gConfig);
const tls = getAlertKeyValue('tls', config, gConfig);
const result = {};
if (status) {
result['status'] = status;
}
if (tls) {
result['tls'] = tls;
}
if (config['alert.tls.enabled'] !== undefined) {
const value = config['alert.tls.enabled'];
delete config['alert.tls.enabled'];
alertConfig['tls'] = {
enabled: value,
return Object.keys(result).length > 0 ? result : undefined;
};

export function getAlertKeyValue(
key: 'status' | 'tls',
config: MonitorConfig,
alertConfig?: AlertConfig
): { enabled: boolean } {
const value = config.alert;
if (value?.[key]?.enabled !== undefined) {
return {
enabled: value[key].enabled,
};
}
if (config?.alert?.status?.enabled !== undefined) {
alertConfig.status = {
enabled: config.alert.status.enabled,

if (value?.[`${key}.enabled`] !== undefined) {
const val = value?.[`${key}.enabled`];
delete value?.[`${key}.enabled`];
if (Object.keys(value).length === 0) {
delete config.alert;
}
return {
enabled: val,
};
}
if (config?.alert?.tls?.enabled !== undefined) {
alertConfig.tls = {
enabled: config.alert.tls.enabled,
const rootKey = `alert.${key}.enabled`;
if (config[rootKey] !== undefined) {
const enabled = config[rootKey];
delete config[rootKey];
return {
enabled,
};
}

// If the user has provided a global alert config, merge it with the monitor alert config
const result = {
...(globalAlertConfig ?? {}),
...alertConfig,
};
return Object.keys(result).length > 0 ? result : undefined;
};
return alertConfig?.[key];
}

export function parseSchedule(schedule: string) {
const EVERY_SYNTAX = '@every';
Expand Down

0 comments on commit a21cf7f

Please sign in to comment.