Skip to content

Commit

Permalink
Cleanup prefill and edit flow. (#66105)
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipb authored May 12, 2020
1 parent 0802ccc commit 6d95b68
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'), [
Expand Down Expand Up @@ -173,52 +174,57 @@ export const Expressions: React.FC<Props> = props => {
[alertParams.criteria, setAlertParams]
);

useEffect(() => {
const preFillAlertCriteria = useCallback(() => {
const md = alertsContext.metadata;
if (md) {
if (md.currentOptions?.metrics) {
setAlertParams(
'criteria',
md.currentOptions.metrics.map(metric => ({
metric: metric.field,
comparator: Comparator.GT,
threshold: [],
timeSize,
timeUnit,
aggType: metric.aggregation,
}))
);
} else {
setAlertParams('criteria', [defaultExpression]);
}
if (md && md.currentOptions?.metrics) {
setAlertParams(
'criteria',
md.currentOptions.metrics.map(metric => ({
metric: metric.field,
comparator: Comparator.GT,
threshold: [],
timeSize,
timeUnit,
aggType: metric.aggregation,
}))
);
} else {
setAlertParams('criteria', [defaultExpression]);
}
}, [alertsContext.metadata, setAlertParams, timeSize, timeUnit]);

if (md.currentOptions) {
if (md.currentOptions.filterQuery) {
setAlertParams('filterQueryText', md.currentOptions.filterQuery);
setAlertParams(
'filterQuery',
convertKueryToElasticSearchQuery(md.currentOptions.filterQuery, derivedIndexPattern) ||
''
);
} else if (md.currentOptions.groupBy && md.series) {
const filter = `${md.currentOptions.groupBy}: "${md.series.id}"`;
setAlertParams('filterQueryText', filter);
setAlertParams(
'filterQuery',
convertKueryToElasticSearchQuery(filter, derivedIndexPattern) || ''
);
}
const preFillAlertFilter = useCallback(() => {
const md = alertsContext.metadata;
if (md && md.currentOptions?.filterQuery) {
setAlertParams('filterQueryText', md.currentOptions.filterQuery);
setAlertParams(
'filterQuery',
convertKueryToElasticSearchQuery(md.currentOptions.filterQuery, derivedIndexPattern) || ''
);
} 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]);
}
if (!alertParams.sourceId) {
setAlertParams('sourceId', source?.id || 'default');
}
preFillAlertCriteria();
}

if (!alertParams.filterQuery) {
preFillAlertFilter();
}

if (!alertParams.sourceId) {
setAlertParams('sourceId', source?.id || 'default');
}
}, [alertsContext.metadata, defaultExpression, source]); // eslint-disable-line react-hooks/exhaustive-deps

Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/infra/server/lib/alerting/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6d95b68

Please sign in to comment.