Cleanup prefill and edit flow. (#66105)
This commit is contained in:
parent
0802ccc262
commit
6d95b68c72
|
@ -74,6 +74,7 @@ export const Expressions: React.FC<Props> = props => {
|
|||
fetch: alertsContext.http.fetch,
|
||||
toastWarning: alertsContext.toastNotifications.addWarning,
|
||||
});
|
||||
|
||||
const [timeSize, setTimeSize] = useState<number | undefined>(1);
|
||||
const [timeUnit, setTimeUnit] = useState<TimeUnit>('m');
|
||||
const derivedIndexPattern = useMemo(() => createDerivedIndexPattern('metrics'), [
|
||||
|
@ -173,10 +174,9 @@ export const Expressions: React.FC<Props> = props => {
|
|||
[alertParams.criteria, setAlertParams]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const preFillAlertCriteria = useCallback(() => {
|
||||
const md = alertsContext.metadata;
|
||||
if (md) {
|
||||
if (md.currentOptions?.metrics) {
|
||||
if (md && md.currentOptions?.metrics) {
|
||||
setAlertParams(
|
||||
'criteria',
|
||||
md.currentOptions.metrics.map(metric => ({
|
||||
|
@ -191,35 +191,41 @@ export const Expressions: React.FC<Props> = props => {
|
|||
} else {
|
||||
setAlertParams('criteria', [defaultExpression]);
|
||||
}
|
||||
}, [alertsContext.metadata, setAlertParams, timeSize, timeUnit]);
|
||||
|
||||
if (md.currentOptions) {
|
||||
if (md.currentOptions.filterQuery) {
|
||||
const preFillAlertFilter = useCallback(() => {
|
||||
const md = alertsContext.metadata;
|
||||
if (md && md.currentOptions?.filterQuery) {
|
||||
setAlertParams('filterQueryText', md.currentOptions.filterQuery);
|
||||
setAlertParams(
|
||||
'filterQuery',
|
||||
convertKueryToElasticSearchQuery(md.currentOptions.filterQuery, derivedIndexPattern) ||
|
||||
''
|
||||
convertKueryToElasticSearchQuery(md.currentOptions.filterQuery, derivedIndexPattern) || ''
|
||||
);
|
||||
} else if (md.currentOptions.groupBy && md.series) {
|
||||
const filter = `${md.currentOptions.groupBy}: "${md.series.id}"`;
|
||||
} else if (md && md.currentOptions?.groupBy && md.series) {
|
||||
const filter = `${md.currentOptions?.groupBy}: "${md.series.id}"`;
|
||||
setAlertParams('filterQueryText', filter);
|
||||
setAlertParams(
|
||||
'filterQuery',
|
||||
convertKueryToElasticSearchQuery(filter, derivedIndexPattern) || ''
|
||||
);
|
||||
}
|
||||
}, [alertsContext.metadata, derivedIndexPattern, setAlertParams]);
|
||||
|
||||
setAlertParams('groupBy', md.currentOptions.groupBy);
|
||||
}
|
||||
setAlertParams('sourceId', source?.id);
|
||||
useEffect(() => {
|
||||
if (alertParams.criteria && alertParams.criteria.length) {
|
||||
setTimeSize(alertParams.criteria[0].timeSize);
|
||||
setTimeUnit(alertParams.criteria[0].timeUnit);
|
||||
} else {
|
||||
if (!alertParams.criteria) {
|
||||
setAlertParams('criteria', [defaultExpression]);
|
||||
preFillAlertCriteria();
|
||||
}
|
||||
|
||||
if (!alertParams.filterQuery) {
|
||||
preFillAlertFilter();
|
||||
}
|
||||
|
||||
if (!alertParams.sourceId) {
|
||||
setAlertParams('sourceId', source?.id || 'default');
|
||||
}
|
||||
}
|
||||
}, [alertsContext.metadata, defaultExpression, source]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const handleFieldSearchChange = useCallback(
|
||||
|
|
|
@ -13,6 +13,11 @@ export const oneOfLiterals = (arrayOfLiterals: Readonly<string[]>) =>
|
|||
});
|
||||
|
||||
export const validateIsStringElasticsearchJSONFilter = (value: string) => {
|
||||
if (value === '') {
|
||||
// Allow clearing the filter.
|
||||
return;
|
||||
}
|
||||
|
||||
const errorMessage = 'filterQuery must be a valid Elasticsearch filter expressed in JSON';
|
||||
try {
|
||||
const parsedValue = JSON.parse(value);
|
||||
|
|
Loading…
Reference in a new issue