-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add filter field to index threshold rule type #142255
Changes from 8 commits
33820f6
ce0aa9e
c7785d2
6473f80
ca45ea4
1ecf740
eaffaa8
9b50171
95ad5bc
d250284
bd9935e
5a18351
1170fc4
2c36ae8
3704feb
619b657
d669f27
52a9dad
2439bed
8783f3f
1da2bb5
c03041f
1e836d2
2ea592c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,18 @@ | |
* 2.0. | ||
*/ | ||
|
||
import React, { useState, Fragment, useEffect } from 'react'; | ||
import React, { useState, Fragment, useEffect, useCallback, ChangeEvent } from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { EuiSpacer, EuiCallOut, EuiEmptyPrompt, EuiText, EuiTitle } from '@elastic/eui'; | ||
import { | ||
EuiSpacer, | ||
EuiCallOut, | ||
EuiEmptyPrompt, | ||
EuiText, | ||
EuiTitle, | ||
EuiFieldSearch, | ||
EuiFormRow, | ||
} from '@elastic/eui'; | ||
import { HttpSetup } from '@kbn/core/public'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { | ||
|
@@ -23,6 +31,7 @@ import { | |
builtInAggregationTypes, | ||
RuleTypeParamsExpressionProps, | ||
} from '@kbn/triggers-actions-ui-plugin/public'; | ||
import { fromKueryExpression, toElasticsearchQuery } from '@kbn/es-query'; | ||
import { ThresholdVisualization } from './visualization'; | ||
import { IndexThresholdAlertParams } from './types'; | ||
import './expression.scss'; | ||
|
@@ -78,6 +87,7 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent< | |
threshold, | ||
timeWindowSize, | ||
timeWindowUnit, | ||
filterKuery, | ||
} = ruleParams; | ||
|
||
const indexArray = indexParamToArray(index); | ||
|
@@ -133,6 +143,23 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent< | |
setEsFields(currentEsFields); | ||
}; | ||
|
||
const filterQueryHasError = (kuery: string) => { | ||
try { | ||
toElasticsearchQuery(fromKueryExpression(kuery)); | ||
return false; | ||
} catch (e) { | ||
return true; | ||
} | ||
}; | ||
|
||
const handleFilterChange = useCallback( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any way to add a debounce here so not every keystroke is validated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debouncing here just prevents the value to be set. |
||
(e: ChangeEvent<HTMLInputElement>) => { | ||
setRuleParams('filterKuery', e.target.value || undefined); | ||
setRuleParams('filterKueryError', filterQueryHasError(e.target.value) ? true : undefined); | ||
}, | ||
[setRuleParams] | ||
); | ||
|
||
useEffect(() => { | ||
setDefaultExpressionValues(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
|
@@ -261,6 +288,33 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent< | |
} | ||
/> | ||
<EuiSpacer /> | ||
<EuiTitle size="xs"> | ||
<h5> | ||
<FormattedMessage | ||
id="xpack.stackAlerts.threshold.ui.filterTitle" | ||
defaultMessage="Filter (Optional)" | ||
/> | ||
</h5> | ||
</EuiTitle> | ||
<EuiSpacer size="s" /> | ||
<EuiFormRow | ||
helpText={i18n.translate('xpack.stackAlerts.threshold.ui.filterKQLHelpText', { | ||
defaultMessage: 'Use a KQL expression to limit the scope of your alert trigger.', | ||
})} | ||
fullWidth | ||
display="rowCompressed" | ||
isInvalid={errors.filterKuery.length > 0} | ||
error={errors.filterKuery} | ||
> | ||
<EuiFieldSearch | ||
data-test-subj="filterKuery" | ||
onChange={handleFilterChange} | ||
ersin-erdal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value={filterKuery} | ||
fullWidth | ||
isInvalid={errors.filterKuery.length > 0} | ||
/> | ||
</EuiFormRow> | ||
<EuiSpacer /> | ||
<div className="actAlertVisualization__chart"> | ||
{cannotShowVisualization ? ( | ||
<Fragment> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeEditor UI Component doesn't pass the data-test-subj attribute to the final rendered html, so i moved it to the parent row element