Skip to content

Commit 14fa82d

Browse files
authored
[alerts] adds support for index threshold index param string type (#88540)
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.
1 parent 5d68b10 commit 14fa82d

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

x-pack/plugins/stack_alerts/public/alert_types/threshold/expression.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ function isString(value: unknown): value is string {
7575
return typeof value === 'string';
7676
}
7777

78+
// normalize the `index` parameter to be a string array
79+
function indexParamToArray(index: string | string[]): string[] {
80+
if (!index) return [];
81+
return isString(index) ? [index] : index;
82+
}
83+
7884
export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
7985
AlertTypeParamsExpressionProps<IndexThresholdAlertParams>
8086
> = ({ alertParams, alertInterval, setAlertParams, setAlertProperty, errors, charts, data }) => {
@@ -92,6 +98,7 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
9298
timeWindowUnit,
9399
} = alertParams;
94100

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

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

134-
if (index && index.length > 0) {
135-
const currentEsFields = await getFields(http, index);
141+
if (indexArray.length > 0) {
142+
const currentEsFields = await getFields(http, indexArray);
136143
const timeFields = getTimeFieldOptions(currentEsFields);
137144

138145
setEsFields(currentEsFields);
@@ -170,7 +177,7 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
170177
defaultMessage="Indices to query"
171178
/>
172179
}
173-
isInvalid={errors.index.length > 0 && index !== undefined}
180+
isInvalid={errors.index.length > 0 && indexArray.length > 0}
174181
error={errors.index}
175182
helpText={
176183
<FormattedMessage
@@ -183,11 +190,11 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
183190
fullWidth
184191
async
185192
isLoading={isIndiciesLoading}
186-
isInvalid={errors.index.length > 0 && index !== undefined}
193+
isInvalid={errors.index.length > 0 && indexArray.length > 0}
187194
noSuggestions={!indexOptions.length}
188195
options={indexOptions}
189196
data-test-subj="thresholdIndexesComboBox"
190-
selectedOptions={(index || []).map((anIndex: string) => {
197+
selectedOptions={indexArray.map((anIndex: string) => {
191198
return {
192199
label: anIndex,
193200
value: anIndex,
@@ -306,12 +313,12 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<
306313
description={i18n.translate('xpack.stackAlerts.threshold.ui.alertParams.indexLabel', {
307314
defaultMessage: 'index',
308315
})}
309-
value={index && index.length > 0 ? renderIndices(index) : firstFieldOption.text}
316+
value={indexArray.length > 0 ? renderIndices(indexArray) : firstFieldOption.text}
310317
isActive={indexPopoverOpen}
311318
onClick={() => {
312319
setIndexPopoverOpen(true);
313320
}}
314-
isInvalid={!(index && index.length > 0 && timeField !== '')}
321+
isInvalid={!(indexArray.length > 0 && timeField !== '')}
315322
/>
316323
}
317324
isOpen={indexPopoverOpen}

x-pack/plugins/stack_alerts/public/alert_types/threshold/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface GroupByType {
2727
}
2828

2929
export interface IndexThresholdAlertParams extends AlertTypeParams {
30-
index: string[];
30+
index: string | string[];
3131
timeField?: string;
3232
aggType: string;
3333
aggField?: string;

x-pack/plugins/stack_alerts/public/alert_types/threshold/validation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('expression params validation', () => {
3232
});
3333
test('if aggField property is invalid should return proper error message', () => {
3434
const initialParams: IndexThresholdAlertParams = {
35-
index: ['test'],
35+
index: 'test',
3636
aggType: 'avg',
3737
threshold: [],
3838
timeWindowSize: 1,

x-pack/test/functional_with_es_ssl/apps/triggers_actions_ui/details.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
228228
timeWindowUnit: 'm',
229229
groupBy: 'all',
230230
threshold: [1000, 5000],
231-
index: ['.kibana_1'],
231+
index: '.kibana_1',
232232
timeField: 'alert',
233233
},
234234
actions: [

0 commit comments

Comments
 (0)