Skip to content

Commit

Permalink
[Synthetics] Code refactor, rename datastream codec to monitor type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Nov 28, 2023
1 parent 11c9328 commit c2d0c48
Show file tree
Hide file tree
Showing 37 changed files with 176 additions and 192 deletions.
20 changes: 10 additions & 10 deletions x-pack/plugins/synthetics/common/constants/monitor_defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
BrowserAdvancedFields,
BrowserSimpleFields,
CommonFields,
DataStream,
MonitorTypeEnum,
FormMonitorType,
HTTPAdvancedFields,
HTTPMethod,
Expand Down Expand Up @@ -129,7 +129,7 @@ export const ALLOWED_SCHEDULES_IN_MINUTES = [
];

export const DEFAULT_COMMON_FIELDS: CommonFields = {
[ConfigKey.MONITOR_TYPE]: DataStream.HTTP,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.HTTP,
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.MULTISTEP,
[ConfigKey.ENABLED]: true,
[ConfigKey.ALERT_CONFIG]: { status: { enabled: true }, tls: { enabled: true } },
Expand Down Expand Up @@ -171,7 +171,7 @@ export const DEFAULT_BROWSER_SIMPLE_FIELDS: BrowserSimpleFields = {
file_name: '',
},
},
[ConfigKey.MONITOR_TYPE]: DataStream.BROWSER,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.BROWSER,
[ConfigKey.PORT]: null,
[ConfigKey.SCHEDULE]: {
unit: ScheduleUnit.MINUTES,
Expand All @@ -192,7 +192,7 @@ export const DEFAULT_HTTP_SIMPLE_FIELDS: HTTPSimpleFields = {
},
[ConfigKey.URLS]: '',
[ConfigKey.MAX_REDIRECTS]: '0',
[ConfigKey.MONITOR_TYPE]: DataStream.HTTP,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.HTTP,
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.HTTP,
[ConfigKey.PORT]: null,
};
Expand Down Expand Up @@ -224,7 +224,7 @@ export const DEFAULT_HTTP_ADVANCED_FIELDS: HTTPAdvancedFields = {
export const DEFAULT_ICMP_SIMPLE_FIELDS: ICMPSimpleFields = {
...DEFAULT_COMMON_FIELDS,
[ConfigKey.HOSTS]: '',
[ConfigKey.MONITOR_TYPE]: DataStream.ICMP,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.ICMP,
[ConfigKey.WAIT]: '1',
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.ICMP,
};
Expand All @@ -236,7 +236,7 @@ export const DEFAULT_TCP_SIMPLE_FIELDS: TCPSimpleFields = {
},
[ConfigKey.HOSTS]: '',
[ConfigKey.URLS]: '',
[ConfigKey.MONITOR_TYPE]: DataStream.TCP,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.TCP,
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.TCP,
[ConfigKey.PORT]: null,
};
Expand Down Expand Up @@ -267,21 +267,21 @@ export const DEFAULT_TLS_FIELDS: TLSFields = {
};

export const DEFAULT_FIELDS: MonitorDefaults = {
[DataStream.HTTP]: {
[MonitorTypeEnum.HTTP]: {
...DEFAULT_HTTP_SIMPLE_FIELDS,
...DEFAULT_HTTP_ADVANCED_FIELDS,
...DEFAULT_TLS_FIELDS,
},
[DataStream.TCP]: {
[MonitorTypeEnum.TCP]: {
...DEFAULT_TCP_SIMPLE_FIELDS,
...DEFAULT_TCP_ADVANCED_FIELDS,
...DEFAULT_TLS_FIELDS,
},
[DataStream.ICMP]: {
[MonitorTypeEnum.ICMP]: {
...DEFAULT_ICMP_SIMPLE_FIELDS,
...DEFAULT_ICMP_ADVANCED_FIELDS,
},
[DataStream.BROWSER]: {
[MonitorTypeEnum.BROWSER]: {
...DEFAULT_BROWSER_SIMPLE_FIELDS,
...DEFAULT_BROWSER_ADVANCED_FIELDS,
...DEFAULT_TLS_FIELDS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
import * as t from 'io-ts';
import { tEnum } from '../../utils/t_enum';

export enum DataStream {
export enum MonitorTypeEnum {
HTTP = 'http',
TCP = 'tcp',
ICMP = 'icmp',
BROWSER = 'browser',
}

export const DataStreamCodec = tEnum<DataStream>('DataStream', DataStream);
export type DataStreamType = t.TypeOf<typeof DataStreamCodec>;
export const MonitorTypeCodec = tEnum<MonitorTypeEnum>('MonitorType', MonitorTypeEnum);

export enum HTTPMethod {
GET = 'GET',
Expand All @@ -26,9 +25,6 @@ export enum HTTPMethod {
HEAD = 'HEAD',
}

export const HTTPMethodCodec = tEnum<HTTPMethod>('HTTPMethod', HTTPMethod);
export type HTTPMethodType = t.TypeOf<typeof HTTPMethodCodec>;

export enum ResponseBodyIndexPolicy {
ALWAYS = 'always',
NEVER = 'never',
Expand All @@ -39,7 +35,6 @@ export const ResponseBodyIndexPolicyCodec = tEnum<ResponseBodyIndexPolicy>(
'ResponseBodyIndexPolicy',
ResponseBodyIndexPolicy
);
export type ResponseBodyIndexPolicyType = t.TypeOf<typeof ResponseBodyIndexPolicyCodec>;

export enum MonacoEditorLangId {
JSON = 'xjson',
Expand All @@ -48,12 +43,6 @@ export enum MonacoEditorLangId {
JAVASCRIPT = 'javascript',
}

export const MonacoEditorLangIdCodec = tEnum<MonacoEditorLangId>(
'MonacoEditorLangId',
MonacoEditorLangId
);
export type MonacoEditorLangIdType = t.TypeOf<typeof MonacoEditorLangIdCodec>;

export enum CodeEditorMode {
FORM = 'form',
JSON = 'json',
Expand All @@ -62,7 +51,6 @@ export enum CodeEditorMode {
}

export const CodeEditorModeCodec = tEnum<CodeEditorMode>('CodeEditorMode', CodeEditorMode);
export type CodeEditorModeType = t.TypeOf<typeof CodeEditorModeCodec>;

export enum ContentType {
JSON = 'application/json',
Expand All @@ -71,16 +59,12 @@ export enum ContentType {
FORM = 'application/x-www-form-urlencoded',
}

export const ContentTypeCodec = tEnum<ContentType>('ContentType', ContentType);
export type ContentTypeType = t.TypeOf<typeof ContentTypeCodec>;

export enum ScheduleUnit {
MINUTES = 'm',
SECONDS = 's',
}

export const ScheduleUnitCodec = tEnum<ScheduleUnit>('ScheduleUnit', ScheduleUnit);
export type ScheduleUnitType = t.TypeOf<typeof ScheduleUnitCodec>;

export enum VerificationMode {
CERTIFICATE = 'certificate',
Expand All @@ -90,7 +74,6 @@ export enum VerificationMode {
}

export const VerificationModeCodec = tEnum<VerificationMode>('VerificationMode', VerificationMode);
export type VerificationModeType = t.TypeOf<typeof VerificationModeCodec>;

export enum TLSVersion {
ONE_ZERO = 'TLSv1.0',
Expand All @@ -100,7 +83,6 @@ export enum TLSVersion {
}

export const TLSVersionCodec = tEnum<TLSVersion>('TLSVersion', TLSVersion);
export type TLSVersionType = t.TypeOf<typeof TLSVersionCodec>;

export enum ScreenshotOption {
ON = 'on',
Expand All @@ -109,7 +91,6 @@ export enum ScreenshotOption {
}

export const ScreenshotOptionCodec = tEnum<ScreenshotOption>('ScreenshotOption', ScreenshotOption);
export type ScreenshotOptionType = t.TypeOf<typeof ScreenshotOptionCodec>;

export enum SourceType {
UI = 'ui',
Expand All @@ -133,7 +114,6 @@ export enum Mode {
ALL = 'all',
}
export const ModeCodec = tEnum<Mode>('Mode', Mode);
export type ModeType = t.TypeOf<typeof ModeCodec>;

export const ResponseCheckJSONCodec = t.interface({
description: t.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { secretKeys } from '../../constants/monitor_management';
import { ConfigKey } from './config_key';
import { MonitorServiceLocationCodec, ServiceLocationErrors } from './locations';
import {
DataStream,
DataStreamCodec,
MonitorTypeEnum,
MonitorTypeCodec,
FormMonitorTypeCodec,
ModeCodec,
ResponseBodyIndexPolicyCodec,
Expand Down Expand Up @@ -55,7 +55,7 @@ export const CommonFieldsCodec = t.intersection([
t.interface({
[ConfigKey.NAME]: t.string,
[ConfigKey.NAMESPACE]: t.string,
[ConfigKey.MONITOR_TYPE]: DataStreamCodec,
[ConfigKey.MONITOR_TYPE]: MonitorTypeCodec,
[ConfigKey.ENABLED]: t.boolean,
[ConfigKey.SCHEDULE]: ScheduleCodec,
[ConfigKey.APM_SERVICE_NAME]: t.string,
Expand Down Expand Up @@ -350,10 +350,10 @@ export const EncryptedSyntheticsSavedMonitorCodec = t.intersection([
]);

export const MonitorDefaultsCodec = t.interface({
[DataStream.HTTP]: HTTPFieldsCodec,
[DataStream.TCP]: TCPFieldsCodec,
[DataStream.ICMP]: ICMPSimpleFieldsCodec,
[DataStream.BROWSER]: BrowserFieldsCodec,
[MonitorTypeEnum.HTTP]: HTTPFieldsCodec,
[MonitorTypeEnum.TCP]: TCPFieldsCodec,
[MonitorTypeEnum.ICMP]: ICMPSimpleFieldsCodec,
[MonitorTypeEnum.BROWSER]: BrowserFieldsCodec,
});

export const MonitorManagementListResultCodec = t.type({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { ClientPluginsStart } from '../../../../../plugin';
import { useSyntheticsSettingsContext } from '../../../contexts';
import { LoadingState } from '../../monitors_page/overview/overview/monitor_detail_flyout';
import { DataStream, MonitorFields } from '../../../../../../common/runtime_types';
import { MonitorTypeEnum, MonitorFields } from '../../../../../../common/runtime_types';
import { inspectMonitorAPI, MonitorInspectResponse } from '../../../state/monitor_management/api';

interface InspectorProps {
Expand Down Expand Up @@ -159,7 +159,7 @@ const formatContent = (result: MonitorInspectResponse) => {

const currentInput = result.privateConfig?.inputs.find((input) => input.enabled);
const compiledConfig = currentInput?.streams.find((stream) =>
Object.values(DataStream).includes(stream.data_stream.dataset as DataStream)
Object.values(MonitorTypeEnum).includes(stream.data_stream.dataset as MonitorTypeEnum)
)?.compiled_stream;

return JSON.stringify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
EncryptedSyntheticsMonitor,
ConfigKey,
FormMonitorType,
DataStream,
MonitorTypeEnum,
} from '../../../../../../common/runtime_types';

export function MonitorTypeBadge({
Expand Down Expand Up @@ -55,7 +55,7 @@ function getMonitorTypeBadgeTitle(monitor: EncryptedSyntheticsMonitor) {
}

switch (monitor?.type) {
case DataStream.BROWSER:
case MonitorTypeEnum.BROWSER:
return 'Journey';
default:
return monitor?.type?.toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createGettingStartedMonitor, UpsertMonitorResponse } from '../../state'
import { DEFAULT_FIELDS } from '../../../../../common/constants/monitor_defaults';
import { ConfigKey } from '../../../../../common/constants/monitor_management';
import {
DataStream,
MonitorTypeEnum,
EncryptedSyntheticsSavedMonitor,
ServiceLocationErrors,
} from '../../../../../common/runtime_types';
Expand Down Expand Up @@ -47,7 +47,7 @@ export const useSimpleMonitor = ({ monitorData }: { monitorData?: SimpleFormData
'source.inline.script': `step('Go to ${urls}', async () => {
await page.goto('${urls}');
});`,
[ConfigKey.MONITOR_TYPE]: DataStream.BROWSER,
[ConfigKey.MONITOR_TYPE]: MonitorTypeEnum.BROWSER,
[ConfigKey.NAME]: urls,
[ConfigKey.LOCATIONS]: locations,
[ConfigKey.URLS]: urls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { ConfigKey, DataStream, FormMonitorType, SyntheticsMonitor } from '../types';
import { ConfigKey, MonitorTypeEnum, FormMonitorType, SyntheticsMonitor } from '../types';
import { DEFAULT_FIELDS, PROFILE_VALUES_ENUM, PROFILES_MAP } from '../constants';
import { formatDefaultFormValues } from './defaults';

Expand Down Expand Up @@ -128,13 +128,13 @@ describe('defaults', () => {
});

it.each([
[DataStream.HTTP, true],
[DataStream.HTTP, false],
[DataStream.TCP, true],
[DataStream.TCP, false],
[MonitorTypeEnum.HTTP, true],
[MonitorTypeEnum.HTTP, false],
[MonitorTypeEnum.TCP, true],
[MonitorTypeEnum.TCP, false],
])('correctly formats isTLSEnabled', (formType, isTLSEnabled) => {
const monitor = {
...DEFAULT_FIELDS[formType as DataStream],
...DEFAULT_FIELDS[formType as MonitorTypeEnum],
[ConfigKey.FORM_MONITOR_TYPE]: formType as unknown as FormMonitorType,
[ConfigKey.TLS_CERTIFICATE_AUTHORITIES]: 'mockCA',
[ConfigKey.METADATA]: {
Expand All @@ -149,10 +149,10 @@ describe('defaults', () => {
});

it.each([
[DataStream.HTTP, FormMonitorType.HTTP],
[DataStream.TCP, FormMonitorType.TCP],
[DataStream.ICMP, FormMonitorType.ICMP],
[DataStream.BROWSER, FormMonitorType.MULTISTEP],
[MonitorTypeEnum.HTTP, FormMonitorType.HTTP],
[MonitorTypeEnum.TCP, FormMonitorType.TCP],
[MonitorTypeEnum.ICMP, FormMonitorType.ICMP],
[MonitorTypeEnum.BROWSER, FormMonitorType.MULTISTEP],
])(
'correctly formats legacy uptime monitors to include ConfigKey.FORM_MONITOR_TYPE',
(dataStream, formType) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DEFAULT_FIELDS } from '../constants';

import {
ConfigKey,
DataStream,
MonitorTypeEnum,
FormMonitorType,
SyntheticsMonitor,
BrowserFields,
Expand All @@ -22,7 +22,7 @@ export const getDefaultFormFields = (
const kibanaNamespace = formatKibanaNamespace(spaceId);
return {
[FormMonitorType.MULTISTEP]: {
...DEFAULT_FIELDS[DataStream.BROWSER],
...DEFAULT_FIELDS[MonitorTypeEnum.BROWSER],
'source.inline': {
type: 'recorder',
script: '',
Expand All @@ -32,24 +32,24 @@ export const getDefaultFormFields = (
[ConfigKey.NAMESPACE]: kibanaNamespace,
},
[FormMonitorType.SINGLE]: {
...DEFAULT_FIELDS[DataStream.BROWSER],
...DEFAULT_FIELDS[MonitorTypeEnum.BROWSER],
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.SINGLE,
[ConfigKey.NAMESPACE]: kibanaNamespace,
},
[FormMonitorType.HTTP]: {
...DEFAULT_FIELDS[DataStream.HTTP],
...DEFAULT_FIELDS[MonitorTypeEnum.HTTP],
isTLSEnabled: false,
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.HTTP,
[ConfigKey.NAMESPACE]: kibanaNamespace,
},
[FormMonitorType.TCP]: {
...DEFAULT_FIELDS[DataStream.TCP],
...DEFAULT_FIELDS[MonitorTypeEnum.TCP],
isTLSEnabled: false,
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.TCP,
[ConfigKey.NAMESPACE]: kibanaNamespace,
},
[FormMonitorType.ICMP]: {
...DEFAULT_FIELDS[DataStream.ICMP],
...DEFAULT_FIELDS[MonitorTypeEnum.ICMP],
[ConfigKey.FORM_MONITOR_TYPE]: FormMonitorType.ICMP,
[ConfigKey.NAMESPACE]: kibanaNamespace,
},
Expand All @@ -68,9 +68,9 @@ export const formatDefaultFormValues = (monitor?: SyntheticsMonitor) => {
// handle default monitor types from Uptime, which don't contain `ConfigKey.FORM_MONITOR_TYPE`
if (!formMonitorType) {
formMonitorType =
monitorType === DataStream.BROWSER
monitorType === MonitorTypeEnum.BROWSER
? FormMonitorType.MULTISTEP
: (monitorType as Omit<DataStream, DataStream.BROWSER> as FormMonitorType);
: (monitorType as Omit<MonitorTypeEnum, MonitorTypeEnum.BROWSER> as FormMonitorType);
monitorWithFormMonitorType[ConfigKey.FORM_MONITOR_TYPE] = formMonitorType;
}

Expand Down
Loading

0 comments on commit c2d0c48

Please sign in to comment.