Skip to content

Commit

Permalink
[Metrics UI] Debounce onChange in alert creation UI (#65167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary authored May 5, 2020
1 parent 76d8ffe commit a467fe5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { debounce } from 'lodash';
import React, { ChangeEvent, useCallback, useMemo, useEffect, useState } from 'react';
import {
EuiSpacer,
Expand Down Expand Up @@ -40,6 +41,8 @@ import { ExpressionRow } from './expression_row';
import { AlertContextMeta, TimeUnit, MetricExpression } from '../types';
import { ExpressionChart } from './expression_chart';

const FILTER_TYPING_DEBOUNCE_MS = 500;

interface Props {
errors: IErrorObject[];
alertParams: {
Expand Down Expand Up @@ -125,6 +128,10 @@ export const Expressions: React.FC<Props> = props => {
[setAlertParams, derivedIndexPattern]
);

const debouncedOnFilterChange = useCallback(debounce(onFilterChange, FILTER_TYPING_DEBOUNCE_MS), [
onFilterChange,
]);

const onGroupByChange = useCallback(
(group: string | null) => {
setAlertParams('groupBy', group || '');
Expand Down Expand Up @@ -320,7 +327,7 @@ export const Expressions: React.FC<Props> = props => {
{(alertsContext.metadata && (
<MetricsExplorerKueryBar
derivedIndexPattern={derivedIndexPattern}
onChange={onFilterChange}
onChange={debouncedOnFilterChange}
onSubmit={onFilterChange}
value={alertParams.filterQueryText}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { debounce } from 'lodash';
import React, { useCallback, useMemo, useEffect, useState, ChangeEvent } from 'react';
import {
EuiFlexGroup,
Expand Down Expand Up @@ -51,6 +52,8 @@ import { NodeTypeExpression } from './node_type';
import { InfraWaffleMapOptions } from '../../../lib/lib';
import { convertKueryToElasticSearchQuery } from '../../../utils/kuery';

const FILTER_TYPING_DEBOUNCE_MS = 500;

interface AlertContextMeta {
options?: Partial<InfraWaffleMapOptions>;
nodeType?: InventoryItemType;
Expand Down Expand Up @@ -134,6 +137,10 @@ export const Expressions: React.FC<Props> = props => {
[derivedIndexPattern, setAlertParams]
);

const debouncedOnFilterChange = useCallback(debounce(onFilterChange, FILTER_TYPING_DEBOUNCE_MS), [
onFilterChange,
]);

const emptyError = useMemo(() => {
return {
aggField: [],
Expand Down Expand Up @@ -291,7 +298,7 @@ export const Expressions: React.FC<Props> = props => {
<MetricsExplorerKueryBar
derivedIndexPattern={derivedIndexPattern}
onSubmit={onFilterChange}
onChange={onFilterChange}
onChange={debouncedOnFilterChange}
value={alertParams.filterQueryText}
/>
)) || (
Expand Down

0 comments on commit a467fe5

Please sign in to comment.