Skip to content

Commit

Permalink
Revert "[Alerting] Add alert.updatedAt field to represent date of l…
Browse files Browse the repository at this point in the history
…ast user edit (#83578) (#83720)"

This reverts commit 7f2944b.
  • Loading branch information
ymao1 committed Nov 19, 2020
1 parent 516392d commit 8a8e24b
Show file tree
Hide file tree
Showing 22 changed files with 30 additions and 166 deletions.
39 changes: 21 additions & 18 deletions x-pack/plugins/alerts/server/alerts_client/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,14 @@ export class AlertsClient {

this.validateActions(alertType, data.actions);

const createTime = Date.now();
const { references, actions } = await this.denormalizeActions(data.actions);

const rawAlert: RawAlert = {
...data,
...this.apiKeyAsAlertAttributes(createdAPIKey, username),
actions,
createdBy: username,
updatedBy: username,
createdAt: new Date(createTime).toISOString(),
updatedAt: new Date(createTime).toISOString(),
createdAt: new Date().toISOString(),
params: validatedAlertTypeParams as RawAlert['params'],
muteAll: false,
mutedInstanceIds: [],
Expand Down Expand Up @@ -292,7 +289,12 @@ 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 }): Promise<SanitizedAlert> {
Expand All @@ -302,7 +304,7 @@ export class AlertsClient {
result.attributes.consumer,
ReadOperations.Get
);
return this.getAlertFromRaw(result.id, result.attributes, result.references);
return this.getAlertFromRaw(result.id, result.attributes, result.updated_at, result.references);
}

public async getAlertState({ id }: { id: string }): Promise<AlertTaskState | void> {
Expand Down Expand Up @@ -391,11 +393,13 @@ export class AlertsClient {
type: 'alert',
});

const authorizedData = data.map(({ id, attributes, references }) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const authorizedData = data.map(({ id, attributes, updated_at, references }) => {
ensureAlertTypeIsAuthorized(attributes.alertTypeId, attributes.consumer);
return this.getAlertFromRaw(
id,
fields ? (pick(attributes, fields) as RawAlert) : attributes,
updated_at,
references
);
});
Expand Down Expand Up @@ -581,7 +585,6 @@ export class AlertsClient {
params: validatedAlertTypeParams as RawAlert['params'],
actions,
updatedBy: username,
updatedAt: new Date().toISOString(),
});
try {
updatedObject = await this.unsecuredSavedObjectsClient.create<RawAlert>(
Expand All @@ -604,7 +607,12 @@ export class AlertsClient {
throw e;
}

return this.getPartialAlertFromRaw(id, updatedObject.attributes, updatedObject.references);
return this.getPartialAlertFromRaw(
id,
updatedObject.attributes,
updatedObject.updated_at,
updatedObject.references
);
}

private apiKeyAsAlertAttributes(
Expand Down Expand Up @@ -669,7 +677,6 @@ export class AlertsClient {
await this.createAPIKey(this.generateAPIKeyName(attributes.alertTypeId, attributes.name)),
username
),
updatedAt: new Date().toISOString(),
updatedBy: username,
});
try {
Expand Down Expand Up @@ -744,7 +751,6 @@ export class AlertsClient {
username
),
updatedBy: username,
updatedAt: new Date().toISOString(),
});
try {
await this.unsecuredSavedObjectsClient.update('alert', id, updateAttributes, { version });
Expand Down Expand Up @@ -823,7 +829,6 @@ export class AlertsClient {
apiKey: null,
apiKeyOwner: null,
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
}),
{ version }
);
Expand Down Expand Up @@ -870,7 +875,6 @@ export class AlertsClient {
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
});
const updateOptions = { version };

Expand Down Expand Up @@ -909,7 +913,6 @@ export class AlertsClient {
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
});
const updateOptions = { version };

Expand Down Expand Up @@ -954,7 +957,6 @@ export class AlertsClient {
this.updateMeta({
mutedInstanceIds,
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
}),
{ version }
);
Expand Down Expand Up @@ -997,7 +999,6 @@ export class AlertsClient {
alertId,
this.updateMeta({
updatedBy: await this.getUserName(),
updatedAt: new Date().toISOString(),
mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId),
}),
{ version }
Expand Down Expand Up @@ -1049,17 +1050,19 @@ export class AlertsClient {
private getAlertFromRaw(
id: string,
rawAlert: RawAlert,
updatedAt: SavedObject['updated_at'],
references: SavedObjectReference[] | undefined
): Alert {
// In order to support the partial update API of Saved Objects we have to support
// partial updates of an Alert, but when we receive an actual RawAlert, it is safe
// to cast the result to an Alert
return this.getPartialAlertFromRaw(id, rawAlert, references) as Alert;
return this.getPartialAlertFromRaw(id, rawAlert, updatedAt, references) as Alert;
}

private getPartialAlertFromRaw(
id: string,
{ createdAt, updatedAt, meta, scheduledTaskId, ...rawAlert }: Partial<RawAlert>,
{ createdAt, meta, scheduledTaskId, ...rawAlert }: Partial<RawAlert>,
updatedAt: SavedObject['updated_at'] = createdAt,
references: SavedObjectReference[] | undefined
): PartialAlert {
// Not the prettiest code here, but if we want to use most of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ describe('create()', () => {
createdAt: '2019-02-12T21:01:22.479Z',
createdBy: 'elastic',
updatedBy: 'elastic',
updatedAt: '2019-02-12T21:01:22.479Z',
muteAll: false,
mutedInstanceIds: [],
actions: [
Expand Down Expand Up @@ -331,7 +330,6 @@ describe('create()', () => {
"foo",
],
"throttle": null,
"updatedAt": "2019-02-12T21:01:22.479Z",
"updatedBy": "elastic",
}
`);
Expand Down Expand Up @@ -420,7 +418,6 @@ describe('create()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -558,7 +555,6 @@ describe('create()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -635,7 +631,6 @@ describe('create()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down Expand Up @@ -976,7 +971,6 @@ describe('create()', () => {
createdBy: 'elastic',
createdAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
updatedAt: '2019-02-12T21:01:22.479Z',
enabled: true,
meta: {
versionApiKeyLastmodified: 'v7.10.0',
Expand Down Expand Up @@ -1098,7 +1092,6 @@ describe('create()', () => {
createdBy: 'elastic',
createdAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
updatedAt: '2019-02-12T21:01:22.479Z',
enabled: false,
meta: {
versionApiKeyLastmodified: 'v7.10.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/s
import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertsAuthorization } from '../../authorization/alerts_authorization';
import { ActionsAuthorization } from '../../../../actions/server';
import { getBeforeSetup, setGlobalDate } from './lib';
import { getBeforeSetup } from './lib';
import { InvalidatePendingApiKey } from '../../types';

const taskManager = taskManagerMock.createStart();
Expand Down Expand Up @@ -45,8 +45,6 @@ beforeEach(() => {
getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry);
});

setGlobalDate();

describe('disable()', () => {
let alertsClient: AlertsClient;
const existingAlert = {
Expand Down Expand Up @@ -138,7 +136,6 @@ describe('disable()', () => {
scheduledTaskId: null,
apiKey: null,
apiKeyOwner: null,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
actions: [
{
Expand Down Expand Up @@ -193,7 +190,6 @@ describe('disable()', () => {
scheduledTaskId: null,
apiKey: null,
apiKeyOwner: null,
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
actions: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertsAuthorization } from '../../authorization/alerts_authorization';
import { ActionsAuthorization } from '../../../../actions/server';
import { TaskStatus } from '../../../../task_manager/server';
import { getBeforeSetup, setGlobalDate } from './lib';
import { getBeforeSetup } from './lib';
import { InvalidatePendingApiKey } from '../../types';

const taskManager = taskManagerMock.createStart();
Expand Down Expand Up @@ -46,8 +46,6 @@ beforeEach(() => {
getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry);
});

setGlobalDate();

describe('enable()', () => {
let alertsClient: AlertsClient;
const existingAlert = {
Expand Down Expand Up @@ -188,7 +186,6 @@ describe('enable()', () => {
meta: {
versionApiKeyLastmodified: kibanaVersion,
},
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
apiKey: null,
apiKeyOwner: null,
Expand Down Expand Up @@ -295,7 +292,6 @@ describe('enable()', () => {
apiKey: Buffer.from('123:abc').toString('base64'),
apiKeyOwner: 'elastic',
updatedBy: 'elastic',
updatedAt: '2019-02-12T21:01:22.479Z',
actions: [
{
group: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ describe('find()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ describe('get()', () => {
bar: true,
},
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
actions: [
{
group: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ const BaseAlertInstanceSummarySavedObject: SavedObject<RawAlert> = {
createdBy: null,
updatedBy: null,
createdAt: mockedDateString,
updatedAt: mockedDateString,
apiKey: null,
apiKeyOwner: null,
throttle: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/s
import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertsAuthorization } from '../../authorization/alerts_authorization';
import { ActionsAuthorization } from '../../../../actions/server';
import { getBeforeSetup, setGlobalDate } from './lib';
import { getBeforeSetup } from './lib';

const taskManager = taskManagerMock.createStart();
const alertTypeRegistry = alertTypeRegistryMock.create();
Expand Down Expand Up @@ -43,8 +43,6 @@ beforeEach(() => {
getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry);
});

setGlobalDate();

describe('muteAll()', () => {
test('mutes an alert', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
Expand Down Expand Up @@ -76,7 +74,6 @@ describe('muteAll()', () => {
{
muteAll: true,
mutedInstanceIds: [],
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/s
import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertsAuthorization } from '../../authorization/alerts_authorization';
import { ActionsAuthorization } from '../../../../actions/server';
import { getBeforeSetup, setGlobalDate } from './lib';
import { getBeforeSetup } from './lib';

const taskManager = taskManagerMock.createStart();
const alertTypeRegistry = alertTypeRegistryMock.create();
Expand Down Expand Up @@ -44,8 +44,6 @@ beforeEach(() => {
getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry);
});

setGlobalDate();

describe('muteInstance()', () => {
test('mutes an alert instance', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
Expand All @@ -70,7 +68,6 @@ describe('muteInstance()', () => {
'1',
{
mutedInstanceIds: ['2'],
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/s
import { actionsAuthorizationMock } from '../../../../actions/server/mocks';
import { AlertsAuthorization } from '../../authorization/alerts_authorization';
import { ActionsAuthorization } from '../../../../actions/server';
import { getBeforeSetup, setGlobalDate } from './lib';
import { getBeforeSetup } from './lib';

const taskManager = taskManagerMock.createStart();
const alertTypeRegistry = alertTypeRegistryMock.create();
Expand Down Expand Up @@ -44,8 +44,6 @@ beforeEach(() => {
getBeforeSetup(alertsClientParams, taskManager, alertTypeRegistry);
});

setGlobalDate();

describe('unmuteAll()', () => {
test('unmutes an alert', async () => {
const alertsClient = new AlertsClient(alertsClientParams);
Expand Down Expand Up @@ -77,7 +75,6 @@ describe('unmuteAll()', () => {
{
muteAll: false,
mutedInstanceIds: [],
updatedAt: '2019-02-12T21:01:22.479Z',
updatedBy: 'elastic',
},
{
Expand Down
Loading

0 comments on commit 8a8e24b

Please sign in to comment.