Skip to content

Commit

Permalink
[alerts] adds support for index threshold index param string type (#8…
Browse files Browse the repository at this point in the history
…8540)

resolves #68575

The index threshold alert defines an `index` parameter which is
typed as `string | string[]`.  However the UI for this alert has
been typing it as only `string[]`.

This PR changes the UI to work with an incoming string value for
this parameter.  If the parameter is edited in the UI, it will always
be set as an array, even if there is only one element.
  • Loading branch information
pmuellr authored Jan 25, 2021
1 parent 5d68b10 commit 14fa82d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ function isString(value: unknown): value is string {
return typeof value === 'string';
}

// normalize the `index` parameter to be a string array
function indexParamToArray(index: string | string[]): string[] {
if (!index) return [];
return isString(index) ? [index] : index;
}

export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
AlertTypeParamsExpressionProps<IndexThresholdAlertParams>
> = ({ alertParams, alertInterval, setAlertParams, setAlertProperty, errors, charts, data }) => {
Expand All @@ -92,6 +98,7 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
timeWindowUnit,
} = alertParams;

const indexArray = indexParamToArray(index);
const { http } = useKibana<KibanaDeps>().services;

const [indexPopoverOpen, setIndexPopoverOpen] = useState(false);
Expand Down Expand Up @@ -131,8 +138,8 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
threshold: threshold ?? DEFAULT_VALUES.THRESHOLD,
});

if (index && index.length > 0) {
const currentEsFields = await getFields(http, index);
if (indexArray.length > 0) {
const currentEsFields = await getFields(http, indexArray);
const timeFields = getTimeFieldOptions(currentEsFields);

setEsFields(currentEsFields);
Expand Down Expand Up @@ -170,7 +177,7 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
defaultMessage="Indices to query"
/>
}
isInvalid={errors.index.length > 0 && index !== undefined}
isInvalid={errors.index.length > 0 && indexArray.length > 0}
error={errors.index}
helpText={
<FormattedMessage
Expand All @@ -183,11 +190,11 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
fullWidth
async
isLoading={isIndiciesLoading}
isInvalid={errors.index.length > 0 && index !== undefined}
isInvalid={errors.index.length > 0 && indexArray.length > 0}
noSuggestions={!indexOptions.length}
options={indexOptions}
data-test-subj="thresholdIndexesComboBox"
selectedOptions={(index || []).map((anIndex: string) => {
selectedOptions={indexArray.map((anIndex: string) => {
return {
label: anIndex,
value: anIndex,
Expand Down Expand Up @@ -306,12 +313,12 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
description={i18n.translate('xpack.stackAlerts.threshold.ui.alertParams.indexLabel', {
defaultMessage: 'index',
})}
value={index && index.length > 0 ? renderIndices(index) : firstFieldOption.text}
value={indexArray.length > 0 ? renderIndices(indexArray) : firstFieldOption.text}
isActive={indexPopoverOpen}
onClick={() => {
setIndexPopoverOpen(true);
}}
isInvalid={!(index && index.length > 0 && timeField !== '')}
isInvalid={!(indexArray.length > 0 && timeField !== '')}
/>
}
isOpen={indexPopoverOpen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface GroupByType {
}

export interface IndexThresholdAlertParams extends AlertTypeParams {
index: string[];
index: string | string[];
timeField?: string;
aggType: string;
aggField?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('expression params validation', () => {
});
test('if aggField property is invalid should return proper error message', () => {
const initialParams: IndexThresholdAlertParams = {
index: ['test'],
index: 'test',
aggType: 'avg',
threshold: [],
timeWindowSize: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
timeWindowUnit: 'm',
groupBy: 'all',
threshold: [1000, 5000],
index: ['.kibana_1'],
index: '.kibana_1',
timeField: 'alert',
},
actions: [
Expand Down

0 comments on commit 14fa82d

Please sign in to comment.