Skip to content

Commit

Permalink
Convert Watch Status and Action Status model classes to stateless fun…
Browse files Browse the repository at this point in the history
…ctions and TS (#138026)
  • Loading branch information
CJ Cenizal authored Aug 10, 2022
1 parent 204bdb6 commit aacb083
Show file tree
Hide file tree
Showing 34 changed files with 1,292 additions and 1,063 deletions.
2 changes: 0 additions & 2 deletions x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -31355,8 +31355,6 @@
"xpack.watcher.models.watchHistoryItem.idPropertyMissingBadRequestMessage": "L'argument JSON doit contenir une propriété {id}",
"xpack.watcher.models.watchHistoryItem.watchHistoryItemJsonPropertyMissingBadRequestMessage": "L'argument JSON doit contenir une propriété {watchHistoryItemJson}",
"xpack.watcher.models.watchHistoryItem.watchIdPropertyMissingBadRequestMessage": "L'argument JSON doit contenir une propriété {watchId}",
"xpack.watcher.models.watchStatus.idPropertyMissingBadRequestMessage": "L'argument JSON doit contenir une propriété {id}",
"xpack.watcher.models.watchStatus.watchStatusJsonPropertyMissingBadRequestMessage": "L'argument JSON doit contenir une propriété {watchStatusJson}",
"xpack.watcher.models.webhookAction.selectMessageText": "Envoyer une requête à un service Web.",
"xpack.watcher.models.webhookAction.simulateButtonLabel": "Envoyer la requête",
"xpack.watcher.models.webhookAction.simulateFailMessage": "Impossible d'envoyer la requête vers {fullPath}.",
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -31431,8 +31431,6 @@
"xpack.watcher.models.watchHistoryItem.idPropertyMissingBadRequestMessage": "json 引数には {id} プロパティが含まれている必要があります",
"xpack.watcher.models.watchHistoryItem.watchHistoryItemJsonPropertyMissingBadRequestMessage": "json 引数には {watchHistoryItemJson} プロパティが含まれている必要があります",
"xpack.watcher.models.watchHistoryItem.watchIdPropertyMissingBadRequestMessage": "json 引数には {watchId} プロパティが含まれている必要があります",
"xpack.watcher.models.watchStatus.idPropertyMissingBadRequestMessage": "json 引数には {id} プロパティが含まれている必要があります",
"xpack.watcher.models.watchStatus.watchStatusJsonPropertyMissingBadRequestMessage": "json 引数には {watchStatusJson} プロパティが含まれている必要があります",
"xpack.watcher.models.webhookAction.selectMessageText": "Web サービスにリクエストを送信してください。",
"xpack.watcher.models.webhookAction.simulateButtonLabel": "リクエストの送信",
"xpack.watcher.models.webhookAction.simulateFailMessage": "{fullPath} へのリクエストの送信に失敗しました。",
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -31460,8 +31460,6 @@
"xpack.watcher.models.watchHistoryItem.idPropertyMissingBadRequestMessage": "JSON 参数必须包含 {id} 属性",
"xpack.watcher.models.watchHistoryItem.watchHistoryItemJsonPropertyMissingBadRequestMessage": "JSON 参数必须包含 {watchHistoryItemJson} 属性",
"xpack.watcher.models.watchHistoryItem.watchIdPropertyMissingBadRequestMessage": "JSON 参数必须包含 {watchId} 属性",
"xpack.watcher.models.watchStatus.idPropertyMissingBadRequestMessage": "JSON 参数必须包含 {id} 属性",
"xpack.watcher.models.watchStatus.watchStatusJsonPropertyMissingBadRequestMessage": "JSON 参数必须包含 {watchStatusJson} 属性",
"xpack.watcher.models.webhookAction.selectMessageText": "将请求发送到 Web 服务。",
"xpack.watcher.models.webhookAction.simulateButtonLabel": "发送请求",
"xpack.watcher.models.webhookAction.simulateFailMessage": "无法将请求发送至 {fullPath}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@ describe('get_moment', () => {
it(`returns a moment object when passed a date`, () => {
const moment = getMoment('2017-03-30T14:53:08.121Z');

expect(moment.constructor.name).toBe('Moment');
expect(moment?.constructor.name).toBe('Moment');
});

it(`returns null when passed falsy`, () => {
const results = [
getMoment(false),
getMoment(0),
getMoment(''),
getMoment(null),
getMoment(undefined),
getMoment(NaN),
];
const results = [getMoment(''), getMoment(null), getMoment(undefined)];

results.forEach((result) => {
expect(result).toBe(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import moment from 'moment';

export function getMoment(date) {
export function getMoment(date?: string | null) {
if (!date) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export { WatchStatusModel } from './watch_status_model';
export { getMoment } from './get_moment';
15 changes: 15 additions & 0 deletions x-pack/plugins/watcher/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

export type {
ActionStatusModelEs,
ServerActionStatusModel,
ClientActionStatusModel,
WatchStatusModelEs,
ServerWatchStatusModel,
ClientWatchStatusModel,
} from './status_types';
84 changes: 84 additions & 0 deletions x-pack/plugins/watcher/common/types/status_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 type { Moment } from 'moment';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';

import { ACTION_STATES, WATCH_STATES, WATCH_STATE_COMMENTS } from '../constants';

export interface ActionStatusModelEs {
id: string;
actionStatusJson: estypes.WatcherActionStatus;
errors?: any; // TODO: Type this more strictly.
lastCheckedRawFormat?: string; // Date e.g. '2017-03-01T20:55:49.679Z'
}

export interface ServerActionStatusModel {
id: string;
actionStatusJson: estypes.WatcherActionStatus;
errors: any; // TODO: Type this more strictly.
lastCheckedRawFormat?: string; // Date e.g. '2017-03-01T20:55:49.679Z'
lastExecutionRawFormat?: string; // Date e.g. '2017-03-01T20:55:49.679Z'
isLastExecutionSuccessful?: boolean;
lastExecutionReason?: string;
lastAcknowledged: Moment | null;
lastExecution: Moment | null;
lastThrottled: Moment | null;
lastSuccessfulExecution: Moment | null;
}

export interface ClientActionStatusModel {
id: string;
lastAcknowledged: Moment | null;
lastThrottled: Moment | null;
lastExecution: Moment | null;
isLastExecutionSuccessful?: boolean;
lastExecutionReason?: string;
lastSuccessfulExecution: Moment | null;
state: keyof typeof ACTION_STATES;
isAckable: boolean;
}

interface SerializedWatchStatus extends estypes.WatcherActivationStatus {
// Inherited from estypes.WatcherActivationStatus:
// - actions: WatcherActions // Record<IndexName, WatcherActionStatus>
// - state: WatcherActivationState // { active, timestamp }
// - version: VersionNumber
last_checked?: string; // Timestamp TODO: Update ES JS client types with this.
last_met_condition?: string; // Timestamp TODO: Update ES JS client types with this.
}

export interface WatchStatusModelEs {
id: string;
watchStatusJson: SerializedWatchStatus;
state?: estypes.WatcherExecutionStatus; // e.g. 'execution_not_needed' or 'failed'
watchErrors?: {
actions?: Record<string, any>; // TODO: Type this more strictly.
};
}

export interface ServerWatchStatusModel {
id: string;
watchState?: estypes.WatcherExecutionStatus; // e.g. 'execution_not_needed' or 'failed'
watchStatusJson: SerializedWatchStatus;
watchErrors?: WatchStatusModelEs['watchErrors'];
isActive: boolean;
lastChecked: Moment | null;
lastMetCondition: Moment | null;
actionStatuses?: ServerActionStatusModel[];
}

export interface ClientWatchStatusModel {
id: string;
isActive: boolean;
lastChecked: Moment | null;
lastMetCondition: Moment | null;
state: keyof typeof WATCH_STATES;
comment: keyof typeof WATCH_STATE_COMMENTS;
lastFired?: Moment | null;
actionStatuses: ClientActionStatusModel[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ActionStatus {
this.lastAcknowledged = getMoment(get(props, 'lastAcknowledged'));
this.lastThrottled = getMoment(get(props, 'lastThrottled'));
this.lastExecution = getMoment(get(props, 'lastExecution'));
this.lastExecutionSuccessful = get(props, 'lastExecutionSuccessful');
this.isLastExecutionSuccessful = get(props, 'isLastExecutionSuccessful');
this.lastExecutionReason = get(props, 'lastExecutionReason');
this.lastSuccessfulExecution = getMoment(get(props, 'lastSuccessfulExecution'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const WatchActionsAccordion: React.FunctionComponent<Props> = ({
(actionItem: ActionType) => actionItem.id === action.id
);

if (actionStatus && actionStatus.lastExecutionSuccessful === false) {
if (actionStatus && actionStatus.isLastExecutionSuccessful === false) {
const message = actionStatus.lastExecutionReason || action.simulateFailMessage;
return toasts.addDanger(message);
}
Expand Down

This file was deleted.

Loading

0 comments on commit aacb083

Please sign in to comment.