Skip to content

Commit

Permalink
update settings
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Apr 16, 2020
1 parent 3f3ea12 commit f703b94
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +31,12 @@ export const DynamicSettingsSaveType = t.intersection([

export type DynamicSettings = t.TypeOf<typeof DynamicSettingsType>;
export type DynamicSettingsSaveResponse = t.TypeOf<typeof DynamicSettingsSaveType>;
export type CertificatesStatesThreshold = t.TypeOf<typeof CertificatesStatesThresholdType>;

export const defaultDynamicSettings: DynamicSettings = {
heartbeatIndices: 'heartbeat-8*',
certificatesThresholds: {
errorState: 7,
warningState: 30,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -44,89 +44,102 @@ export const CertificateExpirationForm: React.FC = ({
title={
<h4>
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesTitle"
defaultMessage="Uptime indices"
id="xpack.uptime.sourceConfiguration.stateThresholds"
defaultMessage="Expiration State Thresholds"
/>
</h4>
}
description={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesDescription"
defaultMessage="Index pattern for matching indices that contain Heartbeat data"
id="xpack.uptime.sourceConfiguration.stateThresholdsDescription"
defaultMessage="Set certificate expiration warning/error thresholds"
/>
}
>
<EuiFormRow
describedByIds={['heartbeatIndices']}
error={fieldErrors?.heartbeatIndices}
describedByIds={['errorState']}
error={fieldErrors?.certificatesThresholds?.errorState}
fullWidth
helpText={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesDefaultValue"
id="xpack.uptime.sourceConfiguration.errorStateDefaultValue"
defaultMessage="The default value is {defaultValue}"
values={{
defaultValue: <EuiCode>{defaultDynamicSettings.heartbeatIndices}</EuiCode>,
defaultValue: (
<EuiCode>{defaultDynamicSettings.certificatesThresholds.errorState}</EuiCode>
),
}}
/>
}
isInvalid={!!fieldErrors?.heartbeatIndices}
isInvalid={!!fieldErrors?.certificatesThresholds?.errorState}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesLabel"
id="xpack.uptime.sourceConfiguration.errorStateLabel"
defaultMessage="Error state"
/>
}
>
<EuiFlexGroup>
<EuiFlexItem grow={2}>
<EuiFieldNumber
data-test-subj={`heartbeat-indices-input-${dss.loading ? 'loading' : 'loaded'}`}
data-test-subj={`error-state-threshold-input-${dss.loading ? 'loading' : 'loaded'}`}
fullWidth
disabled={isDisabled}
isLoading={dss.loading}
value={formFields?.heartbeatIndices || ''}
onChange={(event: any) => onChange('heartbeatIndices', event.currentTarget.value)}
value={formFields?.certificatesThresholds?.errorState || ''}
onChange={({ currentTarget: { value } }: any) =>
onChange(
'certificatesThresholds.errorState',
value === '' ? undefined : Number(value)
)
}
/>
</EuiFlexItem>
<EuiFlexItem grow={1}>
<EuiSelect />
<EuiSelect options={[{ value: 'day', text: 'Days' }]} />
</EuiFlexItem>
</EuiFlexGroup>
</EuiFormRow>
<EuiFormRow
describedByIds={['heartbeatIndices']}
error={fieldErrors?.heartbeatIndices}
describedByIds={['warningState']}
error={fieldErrors?.certificatesThresholds?.warningState}
fullWidth
helpText={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesDefaultValue"
id="xpack.uptime.sourceConfiguration.warningStateDefaultValue"
defaultMessage="The default value is {defaultValue}"
values={{
defaultValue: <EuiCode>{defaultDynamicSettings.heartbeatIndices}</EuiCode>,
defaultValue: (
<EuiCode>{defaultDynamicSettings.certificatesThresholds.warningState}</EuiCode>
),
}}
/>
}
isInvalid={!!fieldErrors?.heartbeatIndices}
isInvalid={!!fieldErrors?.certificatesThresholds.warningState}
label={
<FormattedMessage
id="xpack.uptime.sourceConfiguration.heartbeatIndicesLabel"
id="xpack.uptime.sourceConfiguration.warningStateLabel"
defaultMessage="Warning state"
/>
}
>
<EuiFlexGroup>
<EuiFlexItem grow={2}>
<EuiFieldNumber
data-test-subj={`heartbeat-indices-input-${dss.loading ? 'loading' : 'loaded'}`}
data-test-subj={`warning-state-threshold-input-${
dss.loading ? 'loading' : 'loaded'
}`}
fullWidth
disabled={isDisabled}
isLoading={dss.loading}
value={formFields?.heartbeatIndices || ''}
onChange={(event: any) => onChange('heartbeatIndices', event.currentTarget.value)}
value={formFields?.certificatesThresholds?.warningState || ''}
onChange={(event: any) =>
onChange('certificatesThresholds.warningState', Number(event.currentTarget.value))
}
/>
</EuiFlexItem>
<EuiFlexItem grow={1}>
<EuiSelect />
<EuiSelect options={[{ value: 'day', text: 'Days' }]} />
</EuiFlexItem>
</EuiFlexGroup>
</EuiFormRow>
Expand Down
18 changes: 12 additions & 6 deletions x-pack/legacy/plugins/uptime/public/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
};

Expand Down
16 changes: 11 additions & 5 deletions x-pack/plugins/uptime/server/lib/saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DynamicSettings>;
setUptimeDynamicSettings: UMSavedObjectsQueryFn<void, DynamicSettings>;
Expand All @@ -32,6 +28,16 @@ export const umDynamicSettings: SavedObjectsType = {
heartbeatIndices: {
type: 'keyword',
},
certificatesThresholds: {
properties: {
errorState: {
type: 'long',
},
warningState: {
type: 'long',
},
},
},
},
},
};
Expand Down

0 comments on commit f703b94

Please sign in to comment.