From f703b9449da2f40c0194e727e70809b3f5daa248 Mon Sep 17 00:00:00 2001 From: shahzad Date: Thu, 16 Apr 2020 00:22:42 +0200 Subject: [PATCH] update settings --- .../common/runtime_types/dynamic_settings.ts | 19 +++++- .../components/settings/certificate_form.tsx | 61 +++++++++++-------- .../plugins/uptime/public/pages/settings.tsx | 18 ++++-- .../uptime/server/lib/saved_objects.ts | 16 +++-- 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/x-pack/legacy/plugins/uptime/common/runtime_types/dynamic_settings.ts b/x-pack/legacy/plugins/uptime/common/runtime_types/dynamic_settings.ts index 8dedd4672eeaea..985b51891da99f 100644 --- a/x-pack/legacy/plugins/uptime/common/runtime_types/dynamic_settings.ts +++ b/x-pack/legacy/plugins/uptime/common/runtime_types/dynamic_settings.ts @@ -6,10 +6,20 @@ import * as t from 'io-ts'; -export const DynamicSettingsType = t.type({ - heartbeatIndices: t.string, +export const CertificatesStatesThresholdType = t.interface({ + warningState: t.number, + errorState: t.number, }); +export const DynamicSettingsType = t.intersection([ + t.type({ + heartbeatIndices: t.string, + }), + t.partial({ + certificatesThresholds: CertificatesStatesThresholdType, + }), +]); + export const DynamicSettingsSaveType = t.intersection([ t.type({ success: t.boolean, @@ -21,7 +31,12 @@ export const DynamicSettingsSaveType = t.intersection([ export type DynamicSettings = t.TypeOf; export type DynamicSettingsSaveResponse = t.TypeOf; +export type CertificatesStatesThreshold = t.TypeOf; export const defaultDynamicSettings: DynamicSettings = { heartbeatIndices: 'heartbeat-8*', + certificatesThresholds: { + errorState: 7, + warningState: 30, + }, }; diff --git a/x-pack/legacy/plugins/uptime/public/components/settings/certificate_form.tsx b/x-pack/legacy/plugins/uptime/public/components/settings/certificate_form.tsx index 6f0b58029224fe..6c7660023cf8e6 100644 --- a/x-pack/legacy/plugins/uptime/public/components/settings/certificate_form.tsx +++ b/x-pack/legacy/plugins/uptime/public/components/settings/certificate_form.tsx @@ -44,35 +44,37 @@ export const CertificateExpirationForm: React.FC = ({ title={

} description={ } > {defaultDynamicSettings.heartbeatIndices}, + defaultValue: ( + {defaultDynamicSettings.certificatesThresholds.errorState} + ), }} /> } - isInvalid={!!fieldErrors?.heartbeatIndices} + isInvalid={!!fieldErrors?.certificatesThresholds?.errorState} label={ } @@ -80,36 +82,43 @@ export const CertificateExpirationForm: React.FC = ({ onChange('heartbeatIndices', event.currentTarget.value)} + value={formFields?.certificatesThresholds?.errorState || ''} + onChange={({ currentTarget: { value } }: any) => + onChange( + 'certificatesThresholds.errorState', + value === '' ? undefined : Number(value) + ) + } /> - + {defaultDynamicSettings.heartbeatIndices}, + defaultValue: ( + {defaultDynamicSettings.certificatesThresholds.warningState} + ), }} /> } - isInvalid={!!fieldErrors?.heartbeatIndices} + isInvalid={!!fieldErrors?.certificatesThresholds.warningState} label={ } @@ -117,16 +126,20 @@ export const CertificateExpirationForm: React.FC = ({ onChange('heartbeatIndices', event.currentTarget.value)} + value={formFields?.certificatesThresholds?.warningState || ''} + onChange={(event: any) => + onChange('certificatesThresholds.warningState', Number(event.currentTarget.value)) + } /> - + diff --git a/x-pack/legacy/plugins/uptime/public/pages/settings.tsx b/x-pack/legacy/plugins/uptime/public/pages/settings.tsx index aa52fbf03419ce..41bef871d0bec1 100644 --- a/x-pack/legacy/plugins/uptime/public/pages/settings.tsx +++ b/x-pack/legacy/plugins/uptime/public/pages/settings.tsx @@ -14,16 +14,15 @@ import { EuiForm, EuiPanel, EuiSpacer, - EuiTitle, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { useDispatch, useSelector } from 'react-redux'; -import { isEqual } from 'lodash'; +import { isEqual, set, cloneDeep } from 'lodash'; import { i18n } from '@kbn/i18n'; import { Link } from 'react-router-dom'; import { selectDynamicSettings } from '../state/selectors'; import { getDynamicSettings, setDynamicSettings } from '../state/actions/dynamic_settings'; -import { DynamicSettings } from '../../common/runtime_types'; +import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types'; import { useBreadcrumbs } from '../hooks/use_breadcrumbs'; import { OVERVIEW_ROUTE } from '../../common/constants'; import { useKibana } from '../../../../../../src/plugins/kibana_react/public'; @@ -53,15 +52,22 @@ export const SettingsPage = () => { setFormFields({ ...dss.settings }); } + const blankStr = 'May not be blank'; const fieldErrors = formFields && { - heartbeatIndices: formFields.heartbeatIndices.match(/^\S+$/) ? null : 'May not be blank', + heartbeatIndices: formFields.heartbeatIndices.match(/^\S+$/) ? null : blankStr, + certificatesThresholds: { + errorState: formFields.certificatesThresholds?.errorState ? null : blankStr, + warningState: formFields.certificatesThresholds?.warningState ? null : blankStr, + }, }; + const isFormValid = !(fieldErrors && Object.values(fieldErrors).find(v => !!v)); const onChangeFormField = (field: keyof DynamicSettings, value: any) => { if (formFields) { - formFields[field] = value; - setFormFields({ ...formFields }); + const newFormFields = cloneDeep(formFields); + set(newFormFields, field, value); + setFormFields(cloneDeep(newFormFields)); } }; diff --git a/x-pack/plugins/uptime/server/lib/saved_objects.ts b/x-pack/plugins/uptime/server/lib/saved_objects.ts index 175634ef797ccc..9067fcb9919001 100644 --- a/x-pack/plugins/uptime/server/lib/saved_objects.ts +++ b/x-pack/plugins/uptime/server/lib/saved_objects.ts @@ -7,14 +7,10 @@ import { DynamicSettings, defaultDynamicSettings, -} from '../../../../legacy/plugins/uptime/common/runtime_types/dynamic_settings'; +} from '../../../../legacy/plugins/uptime/common/runtime_types'; import { SavedObjectsType, SavedObjectsErrorHelpers } from '../../../../../src/core/server'; import { UMSavedObjectsQueryFn } from './adapters'; -export interface UMDynamicSettingsType { - heartbeatIndices: string; -} - export interface UMSavedObjectsAdapter { getUptimeDynamicSettings: UMSavedObjectsQueryFn; setUptimeDynamicSettings: UMSavedObjectsQueryFn; @@ -32,6 +28,16 @@ export const umDynamicSettings: SavedObjectsType = { heartbeatIndices: { type: 'keyword', }, + certificatesThresholds: { + properties: { + errorState: { + type: 'long', + }, + warningState: { + type: 'long', + }, + }, + }, }, }, };