Skip to content

Commit

Permalink
Remove lazy loading and reference to lazy resource from validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed Jul 28, 2020
1 parent f68e77f commit f5e2705
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { initMonitorStatusAlertType } from '../monitor_status';
import { validateMonitorStatusParams as validate } from '../lazy_wrapper/validate_monitor_status';
import { initMonitorStatusAlertType, validate } from '../monitor_status';

describe('monitor status alert type', () => {
describe('validate', () => {
Expand Down

This file was deleted.

63 changes: 50 additions & 13 deletions x-pack/plugins/uptime/public/lib/alert_types/monitor_status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,64 @@

import React from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { AlertTypeModel, ValidationResult } from '../../../../triggers_actions_ui/public';
import { isRight } from 'fp-ts/lib/Either';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { AlertTypeModel } from '../../../../triggers_actions_ui/public';
import { AlertTypeInitializer } from '.';

import { CLIENT_ALERT_TYPES } from '../../../common/constants/alerts';
import { MonitorStatusTranslations } from './translations';
import {
AtomicStatusCheckParamsType,
StatusCheckParamsType,
MonitorAvailabilityType,
} from '../../../common/runtime_types';

const { defaultActionMessage } = MonitorStatusTranslations;

const MonitorStatusAlert = React.lazy(() => import('./lazy_wrapper/monitor_status'));

let validateFunc: (alertParams: any) => ValidationResult;
export const validate = (alertParams: any) => {
const errors: Record<string, any> = {};
const decoded = AtomicStatusCheckParamsType.decode(alertParams);
const oldDecoded = StatusCheckParamsType.decode(alertParams);
const availabilityDecoded = MonitorAvailabilityType.decode(alertParams);

if (!isRight(decoded) && !isRight(oldDecoded) && !isRight(availabilityDecoded)) {
return {
errors: {
typeCheckFailure: 'Provided parameters do not conform to the expected type.',
typeCheckParsingMessage: PathReporter.report(decoded),
},
};
}

if (
!(alertParams.shouldCheckAvailability ?? false) &&
!(alertParams.shouldCheckStatus ?? false)
) {
return {
errors: {
noAlertSelected: 'Alert must check for monitor status or monitor availability.',
},
};
}

if (isRight(decoded) && decoded.right.shouldCheckStatus) {
const { numTimes, timerangeCount } = decoded.right;
if (numTimes < 1) {
errors.invalidNumTimes = 'Number of alert check down times must be an integer greater than 0';
}
if (isNaN(timerangeCount)) {
errors.timeRangeStartValueNaN = 'Specified time range value must be a number';
}
if (timerangeCount <= 0) {
errors.invalidTimeRangeValue = 'Time range value must be greater than 0';
}
}

return { errors };
};

export const initMonitorStatusAlertType: AlertTypeInitializer = ({
core,
Expand All @@ -33,17 +80,7 @@ export const initMonitorStatusAlertType: AlertTypeInitializer = ({
alertParamsExpression: (params: any) => (
<MonitorStatusAlert core={core} plugins={plugins} params={params} />
),
validate: async (alertParams: any) => {
if (!validateFunc) {
await (async function loadValidate() {
const { validateMonitorStatusParams } = await import(
'./lazy_wrapper/validate_monitor_status'
);
validateFunc = validateMonitorStatusParams;
})();
}
return validateFunc && validateFunc(alertParams);
},
validate,
defaultActionMessage,
requiresAppContext: false,
});

0 comments on commit f5e2705

Please sign in to comment.