Skip to content

Commit

Permalink
use the SavedObjets updated_at instead of a new field in alerting
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Dec 24, 2019
1 parent dcd574e commit 1721ec2
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 58 deletions.
3 changes: 0 additions & 3 deletions x-pack/legacy/plugins/alerting/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
},
"apiKey": {
"type": "binary"
},
Expand Down
21 changes: 5 additions & 16 deletions x-pack/legacy/plugins/alerting/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ describe('create()', () => {
bar: true,
},
createdAt: '2019-02-12T21:01:22.479Z',
updatedAt: null,
actions: [
{
group: 'default',
Expand Down Expand Up @@ -209,7 +208,6 @@ describe('create()', () => {
"foo",
],
"throttle": null,
"updatedAt": null,
"updatedBy": "elastic",
}
`);
Expand Down Expand Up @@ -317,6 +315,7 @@ describe('create()', () => {
params: {
bar: true,
},
createdAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -468,6 +467,7 @@ describe('create()', () => {
params: {
bar: true,
},
createdAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -818,7 +818,6 @@ describe('create()', () => {
createdBy: 'elastic',
createdAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
updatedAt: null,
enabled: true,
schedule: { interval: '10s' },
throttle: null,
Expand Down Expand Up @@ -877,7 +876,6 @@ describe('enable()', () => {
enabled: true,
scheduledTaskId: 'task-123',
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
apiKey: null,
apiKeyOwner: null,
},
Expand Down Expand Up @@ -961,7 +959,6 @@ describe('enable()', () => {
apiKey: Buffer.from('123:abc').toString('base64'),
apiKeyOwner: 'elastic',
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
},
{
version: '123',
Expand Down Expand Up @@ -1011,7 +1008,6 @@ describe('disable()', () => {
enabled: false,
scheduledTaskId: null,
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
},
{
version: '123',
Expand Down Expand Up @@ -1057,7 +1053,6 @@ describe('muteAll()', () => {
muteAll: true,
mutedInstanceIds: [],
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
});
});
});
Expand All @@ -1079,7 +1074,6 @@ describe('unmuteAll()', () => {
muteAll: false,
mutedInstanceIds: [],
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
});
});
});
Expand Down Expand Up @@ -1108,7 +1102,6 @@ describe('muteInstance()', () => {
{
mutedInstanceIds: ['2'],
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
},
{ version: '123' }
);
Expand Down Expand Up @@ -1178,7 +1171,6 @@ describe('unmuteInstance()', () => {
{
mutedInstanceIds: [],
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
},
{ version: '123' }
);
Expand Down Expand Up @@ -1500,8 +1492,8 @@ describe('update()', () => {
],
scheduledTaskId: 'task-123',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
updated_at: new Date().toISOString(),
references: [
{
name: 'action_0',
Expand Down Expand Up @@ -1586,7 +1578,6 @@ describe('update()', () => {
"tags": Array [
"foo",
],
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
`);
Expand Down Expand Up @@ -1653,7 +1644,6 @@ describe('update()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -1682,6 +1672,7 @@ describe('update()', () => {
],
scheduledTaskId: 'task-123',
},
updated_at: new Date().toISOString(),
references: [
{
name: 'action_0',
Expand Down Expand Up @@ -1832,7 +1823,6 @@ describe('update()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand All @@ -1846,6 +1836,7 @@ describe('update()', () => {
apiKey: Buffer.from('123:abc').toString('base64'),
scheduledTaskId: 'task-123',
},
updated_at: new Date().toISOString(),
references: [
{
name: 'action_0',
Expand Down Expand Up @@ -1931,7 +1922,6 @@ describe('update()', () => {
"tags": Array [
"foo",
],
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
`);
Expand Down Expand Up @@ -2249,7 +2239,6 @@ describe('updateApiKey()', () => {
apiKey: Buffer.from('123:abc').toString('base64'),
apiKeyOwner: 'elastic',
updatedBy: 'elastic',
updatedAt: new Date().toISOString(),
},
{ version: '123' }
);
Expand Down
45 changes: 20 additions & 25 deletions x-pack/legacy/plugins/alerting/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Boom from 'boom';
import { omit, isEqual, pick } from 'lodash';
import { omit, isEqual } from 'lodash';
import { i18n } from '@kbn/i18n';
import {
Logger,
Expand Down Expand Up @@ -145,7 +145,6 @@ export class AlertsClient {
createdBy: username,
updatedBy: username,
createdAt: new Date().toISOString(),
updatedAt: null,
params: validatedAlertTypeParams,
muteAll: false,
mutedInstanceIds: [],
Expand Down Expand Up @@ -175,12 +174,17 @@ export class AlertsClient {
});
createdAlert.attributes.scheduledTaskId = scheduledTask.id;
}
return this.getAlertFromRaw(createdAlert.id, createdAlert.attributes, references);
return this.getAlertFromRaw(
createdAlert.id,
createdAlert.attributes,
createdAlert.updated_at,
references
);
}

public async get({ id }: { id: string }) {
const result = await this.savedObjectsClient.get('alert', id);
return this.getAlertFromRaw(result.id, result.attributes, result.references);
return this.getAlertFromRaw(result.id, result.attributes, result.updated_at, result.references);
}

public async find({ options = {} }: FindOptions = {}): Promise<FindResult> {
Expand All @@ -190,7 +194,7 @@ export class AlertsClient {
});

const data = results.saved_objects.map(result =>
this.getAlertFromRaw(result.id, result.attributes, result.references)
this.getAlertFromRaw(result.id, result.attributes, result.updated_at, result.references)
);

return {
Expand Down Expand Up @@ -252,14 +256,18 @@ export class AlertsClient {
params: validatedAlertTypeParams,
actions,
updatedBy: username,
updatedAt: new Date().toISOString(),
},
{
version,
references,
}
);
return this.getAlertFromRaw(id, updatedObject.attributes, updatedObject.references);
return this.getAlertFromRaw(
id,
updatedObject.attributes,
updatedObject.updated_at,
updatedObject.references
);
}

private apiKeyAsAlertAttributes(
Expand Down Expand Up @@ -288,7 +296,6 @@ export class AlertsClient {
...attributes,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
updatedBy: username,
updatedAt: new Date().toISOString(),
},
{ version }
);
Expand All @@ -307,7 +314,7 @@ export class AlertsClient {
enabled: true,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
updatedBy: username,
updatedAt: new Date().toISOString(),

scheduledTaskId: scheduledTask.id,
},
{ version }
Expand All @@ -328,7 +335,6 @@ export class AlertsClient {
apiKey: null,
apiKeyOwner: null,
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
},
{ version }
);
Expand All @@ -341,7 +347,6 @@ export class AlertsClient {
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
});
}

Expand All @@ -350,7 +355,6 @@ export class AlertsClient {
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
});
}

Expand All @@ -371,7 +375,6 @@ export class AlertsClient {
{
mutedInstanceIds,
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
},
{ version }
);
Expand All @@ -393,7 +396,7 @@ export class AlertsClient {
alertId,
{
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),

mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId),
},
{ version }
Expand Down Expand Up @@ -436,6 +439,7 @@ export class AlertsClient {
private getAlertFromRaw(
id: string,
rawAlert: Partial<RawAlert>,
updatedAt: SavedObject['updated_at'],
references: SavedObjectReference[] | undefined
) {
if (!rawAlert.actions) {
Expand All @@ -448,7 +452,8 @@ export class AlertsClient {
return {
id,
...rawAlert,
...parseDates(pick(rawAlert, 'createdAt', 'updatedAt')),
updatedAt: updatedAt ? new Date(updatedAt) : null,
createdAt: new Date(rawAlert.createdAt!),
actions,
};
}
Expand Down Expand Up @@ -508,13 +513,3 @@ export class AlertsClient {
};
}
}

function parseDates({
createdAt,
updatedAt,
}: Pick<RawAlert, 'createdAt' | 'updatedAt'>): Pick<Alert, 'createdAt' | 'updatedAt'> {
return {
createdAt: new Date(createdAt),
updatedAt: updatedAt ? new Date(updatedAt) : null,
};
}
11 changes: 8 additions & 3 deletions x-pack/legacy/plugins/alerting/server/routes/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { omit } from 'lodash';
import { createMockServer } from './_mock_server';
import { createAlertRoute } from './create';

Expand All @@ -19,8 +20,6 @@ const mockedAlert = {
params: {
bar: true,
},
createdAt: new Date(),
updatedAt: null,
actions: [
{
group: 'default',
Expand All @@ -41,8 +40,12 @@ test('creates an alert with proper parameters', async () => {
payload: mockedAlert,
};

const createdAt = new Date();
const updatedAt = null;
alertsClient.create.mockResolvedValueOnce({
...mockedAlert,
createdAt,
updatedAt,
id: '123',
actions: [
{
Expand All @@ -54,7 +57,8 @@ test('creates an alert with proper parameters', async () => {
const { payload, statusCode } = await server.inject(request);
expect(statusCode).toBe(200);
const response = JSON.parse(payload);
expect(response).toMatchInlineSnapshot(`
expect(new Date(response.createdAt)).toEqual(createdAt);
expect(omit(response, 'createdAt')).toMatchInlineSnapshot(`
Object {
"actions": Array [
Object {
Expand All @@ -79,6 +83,7 @@ test('creates an alert with proper parameters', async () => {
"tags": Array [
"foo",
],
"updatedAt": null,
}
`);
expect(alertsClient.create).toHaveBeenCalledTimes(1);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/routes/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ test('calls get with proper parameters', async () => {
alertsClient.get.mockResolvedValueOnce(mockedAlert);
const { payload, statusCode } = await server.inject(request);
expect(statusCode).toBe(200);
const response = JSON.parse(payload);
expect(response).toEqual(mockedAlert);
const { createdAt, ...response } = JSON.parse(payload);
expect({ createdAt: new Date(createdAt), ...response }).toEqual(mockedAlert);
expect(alertsClient.get).toHaveBeenCalledTimes(1);
expect(alertsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Expand Down
Loading

0 comments on commit 1721ec2

Please sign in to comment.