Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Sep 10, 2024
1 parent c226f51 commit 2ae41a5
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { validateFlapping } from './v1';

describe('validateFlapping', () => {
test('should error if look back window exceeds the lower bound', () => {
const result = validateFlapping({
look_back_window: 0,
status_change_threshold: 10,
});

expect(result).toEqual('look back window must be between 2 and 20');
});

test('should error if look back window exceeds the upper bound', () => {
const result = validateFlapping({
look_back_window: 50,
status_change_threshold: 10,
});

expect(result).toEqual('look back window must be between 2 and 20');
});

test('should error if status change threshold exceeds the lower bound', () => {
const result = validateFlapping({
look_back_window: 10,
status_change_threshold: 1,
});

expect(result).toEqual('status change threshold must be between 2 and 20');
});

test('should error if status change threshold exceeds the upper bound', () => {
const result = validateFlapping({
look_back_window: 10,
status_change_threshold: 50,
});

expect(result).toEqual('status change threshold must be between 2 and 20');
});

test('should error if status change threshold is greater than the look back window', () => {
const result = validateFlapping({
look_back_window: 10,
status_change_threshold: 15,
});

expect(result).toEqual(
'lookBackWindow (10) must be equal to or greater than statusChangeThreshold (15)'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const validateCanUpdateFlapping = (
return;
}

// If updated flapping is undefined then don't do anything, it's not being updated
if (updateFlapping === undefined) {
return;
}

// If both versions are falsy, allow it even if its changing between undefined and null
if (!originalFlapping && !updateFlapping) {
return;
Expand Down Expand Up @@ -154,12 +159,6 @@ async function updateWithOCC<Params extends RuleParams = never>(

validateCanUpdateFlapping(isFlappingEnabled, originalFlapping, initialData.flapping);

if (!isFlappingEnabled && !isEqual(originalFlapping, initialData.flapping)) {
throw Boom.badRequest(
`Error updating rule: can not update rule flapping if global flapping is disabled`
);
}

let validationPayload: ValidateScheduleLimitResult = null;
if (enabled && schedule.interval !== data.schedule.interval) {
validationPayload = await validateScheduleLimit({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ describe('transformRuleAttributesToRuleDomain', () => {
updatedBy: 'user',
apiKey: MOCK_API_KEY,
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
};

it('transforms the actions correctly', () => {
Expand All @@ -106,6 +110,13 @@ describe('transformRuleAttributesToRuleDomain', () => {
isSystemAction
);

expect(res.flapping).toMatchInlineSnapshot(`
Object {
"lookBackWindow": 20,
"statusChangeThreshold": 20,
}
`);

expect(res.actions).toMatchInlineSnapshot(`
Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ describe('transformRuleDomainToRule', () => {
updatedBy: 'user',
apiKey: MOCK_API_KEY,
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
};

it('should transform rule domain to rule', () => {
Expand Down Expand Up @@ -100,6 +104,10 @@ describe('transformRuleDomainToRule', () => {
revision: 0,
updatedBy: 'user',
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
});
});

Expand Down Expand Up @@ -135,6 +143,10 @@ describe('transformRuleDomainToRule', () => {
revision: 0,
updatedBy: 'user',
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
});
});

Expand Down Expand Up @@ -172,6 +184,10 @@ describe('transformRuleDomainToRule', () => {
revision: 0,
updatedBy: 'user',
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RuleDomain } from '../types';
import { transformRuleDomainToRuleAttributes } from './transform_rule_domain_to_rule_attributes';

describe('transformRuleDomainToRuleAttributes', () => {
const MOCK_API_KEY = Buffer.from('123:abc').toString('base64');

const defaultAction = {
id: '1',
uuid: 'test-uuid',
group: 'default',
actionTypeId: 'test',
params: {},
};

const rule: RuleDomain<{}> = {
id: 'test',
enabled: false,
name: 'my rule name',
tags: ['foo'],
alertTypeId: 'myType',
consumer: 'myApp',
schedule: { interval: '1m' },
actions: [defaultAction],
params: {},
mapped_params: {},
createdBy: 'user',
createdAt: new Date('2019-02-12T21:01:22.479Z'),
updatedAt: new Date('2019-02-12T21:01:22.479Z'),
legacyId: 'legacyId',
muteAll: false,
mutedInstanceIds: [],
snoozeSchedule: [],
scheduledTaskId: 'task-123',
executionStatus: {
lastExecutionDate: new Date('2019-02-12T21:01:22.479Z'),
status: 'pending' as const,
},
throttle: null,
notifyWhen: null,
revision: 0,
updatedBy: 'user',
apiKey: MOCK_API_KEY,
apiKeyOwner: 'user',
flapping: {
lookBackWindow: 20,
statusChangeThreshold: 20,
},
};

test('should transform rule domain to rule attribute', () => {
const result = transformRuleDomainToRuleAttributes({
rule,
actionsWithRefs: [
{
group: 'default',
actionRef: 'action_0',
actionTypeId: 'test',
uuid: 'test-uuid',
params: {},
},
],
params: {
legacyId: 'test',
paramsWithRefs: {},
},
});

expect(result).toMatchInlineSnapshot(`
Object {
"actions": Array [
Object {
"actionRef": "action_0",
"actionTypeId": "test",
"group": "default",
"params": Object {},
"uuid": "test-uuid",
},
],
"alertTypeId": "myType",
"apiKey": "MTIzOmFiYw==",
"apiKeyOwner": "user",
"consumer": "myApp",
"createdAt": "2019-02-12T21:01:22.479Z",
"createdBy": "user",
"enabled": false,
"executionStatus": Object {
"lastExecutionDate": "2019-02-12T21:01:22.479Z",
"status": "pending",
},
"flapping": Object {
"lookBackWindow": 20,
"statusChangeThreshold": 20,
},
"legacyId": "test",
"muteAll": false,
"mutedInstanceIds": Array [],
"name": "my rule name",
"notifyWhen": null,
"params": Object {},
"revision": 0,
"schedule": Object {
"interval": "1m",
},
"scheduledTaskId": "task-123",
"snoozeSchedule": Array [],
"tags": Array [
"foo",
],
"throttle": null,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "user",
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
await resetRulesSettings(supertest, 'space1');
});

it('should allow flapping to be updated', async () => {
it('should allow flapping to be created', async () => {
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,53 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
},
})
.expect(400);
});

it('should allow rule to be updated when global flapping is off if not updating flapping', async () => {
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule`)
.set('kbn-xsrf', 'foo')
.send(
getTestRuleData({
flapping: {
look_back_window: 5,
status_change_threshold: 5,
},
})
);

objectRemover.add(Spaces.space1.id, response.body.id, 'rule', 'alerting');

await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/internal/alerting/rules/settings/_flapping`)
.set('kbn-xsrf', 'foo')
.send({
enabled: false,
look_back_window: 5,
status_change_threshold: 5,
});

// Can still update when global flapping is off if not updating flapping
await supertest
.put(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule/${response.body.id}`)
.set('kbn-xsrf', 'foo')
.send({
name: 'bcd',
name: 'updated name 1',
tags: ['foo'],
params: {
foo: true,
},
schedule: { interval: '12s' },
actions: [],
throttle: '1m',
notify_when: 'onThrottleInterval',
})
.expect(200);

await supertest
.put(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule/${response.body.id}`)
.set('kbn-xsrf', 'foo')
.send({
name: 'updated name 2',
tags: ['foo'],
params: {
foo: true,
Expand All @@ -309,6 +349,10 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
actions: [],
throttle: '1m',
notify_when: 'onThrottleInterval',
flapping: {
look_back_window: 5,
status_change_threshold: 5,
},
})
.expect(200);
});
Expand Down

0 comments on commit 2ae41a5

Please sign in to comment.