Skip to content
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

Remove unused code for redundant groupBy field in custom threshold and metric threshold rules #184787

Merged
merged 9 commits into from
Jul 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
EuiFieldSearch,
EuiFormRow,
EuiIconTip,
EuiLink,
EuiPanel,
EuiSpacer,
EuiText,
Expand All @@ -35,7 +34,6 @@ import {
useSourceContext,
withSourceProvider,
} from '../../../containers/metrics_source';
import { useKibanaContextForPlugin } from '../../../hooks/use_kibana';
import { MetricsExplorerGroupBy } from '../../../pages/metrics/metrics_explorer/components/group_by';
import { MetricsExplorerKueryBar } from '../../../pages/metrics/metrics_explorer/components/kuery_bar';
import { MetricsExplorerOptions } from '../../../pages/metrics/metrics_explorer/hooks/use_metrics_explorer_options';
Expand Down Expand Up @@ -66,7 +64,6 @@ export { defaultExpression };

export const Expressions: React.FC<Props> = (props) => {
const { setRuleParams, ruleParams, errors, metadata } = props;
const { docLinks } = useKibanaContextForPlugin().services;
const { source } = useSourceContext();
const { metricsView } = useMetricsDataViewContext();
const [timeSize, setTimeSize] = useState<number | undefined>(1);
Expand Down Expand Up @@ -267,30 +264,6 @@ export const Expressions: React.FC<Props> = (props) => {
[ruleParams.criteria]
);

// Test to see if any of the group fields in groupBy are already filtered down to a single
// group by the filterQuery. If this is the case, then a groupBy is unnecessary, as it would only
// ever produce one group instance
const groupByFilterTestPatterns = useMemo(() => {
if (!ruleParams.groupBy) return null;
const groups = !Array.isArray(ruleParams.groupBy) ? [ruleParams.groupBy] : ruleParams.groupBy;
return groups.map((group: string) => ({
groupName: group,
pattern: new RegExp(`{"match(_phrase)?":{"${group}":"(.*?)"}}`),
}));
}, [ruleParams.groupBy]);

const redundantFilterGroupBy = useMemo(() => {
const { filterQuery } = ruleParams;
if (typeof filterQuery !== 'string' || !groupByFilterTestPatterns) return [];
return groupByFilterTestPatterns
.map(({ groupName, pattern }) => {
if (pattern.test(filterQuery)) {
return groupName;
}
})
.filter((g) => typeof g === 'string') as string[];
}, [ruleParams, groupByFilterTestPatterns]);

return (
<>
<EuiSpacer size="m" />
Expand Down Expand Up @@ -465,35 +438,8 @@ export const Expressions: React.FC<Props> = (props) => {
...options,
groupBy: ruleParams.groupBy || undefined,
}}
errorOptions={redundantFilterGroupBy}
/>
</EuiFormRow>
{redundantFilterGroupBy.length > 0 && (
<>
<EuiSpacer size="s" />
<EuiText size="xs" color="danger">
<FormattedMessage
id="xpack.infra.metrics.alertFlyout.alertPerRedundantFilterError"
defaultMessage="This rule may alert on {matchedGroups} less than expected, because the filter query contains a match for {groupCount, plural, one {this field} other {these fields}}. For more information, refer to {filteringAndGroupingLink}."
values={{
matchedGroups: <strong>{redundantFilterGroupBy.join(', ')}</strong>,
groupCount: redundantFilterGroupBy.length,
filteringAndGroupingLink: (
<EuiLink
data-test-subj="infraExpressionsTheDocsLink"
href={`${docLinks.links.observability.metricsThreshold}#filtering-and-grouping`}
>
{i18n.translate(
'xpack.infra.metrics.alertFlyout.alertPerRedundantFilterError.docsLink',
{ defaultMessage: 'the docs' }
)}
</EuiLink>
),
}}
/>
</EuiText>
</>
)}
<EuiSpacer size="s" />
<EuiCheckbox
id="metrics-alert-group-disappear-toggle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { MetricsExplorerOptions } from '../hooks/use_metrics_explorer_options';
interface Props {
options: MetricsExplorerOptions;
onChange: (groupBy: string | null | string[]) => void;
errorOptions?: string[];
}

export const MetricsExplorerGroupBy = ({ options, onChange, errorOptions }: Props) => {
export const MetricsExplorerGroupBy = ({ options, onChange }: Props) => {
const { metricsView } = useMetricsDataViewContext();
const handleChange = useCallback(
(selectedOptions: Array<{ label: string }>) => {
Expand All @@ -28,17 +27,9 @@ export const MetricsExplorerGroupBy = ({ options, onChange, errorOptions }: Prop
);

const selectedOptions = Array.isArray(options.groupBy)
? options.groupBy.map((field) => ({
label: field,
color: errorOptions?.includes(field) ? 'danger' : undefined,
}))
? options.groupBy.map((field) => ({ label: field }))
: options.groupBy
? [
{
label: options.groupBy,
color: errorOptions?.includes(options.groupBy) ? 'danger' : undefined,
},
]
? [{ label: options.groupBy }]
: [];

const comboOptions = (metricsView?.fields ?? [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ interface Props {
options: GroupByOptions;
onChange: (groupBy: GroupBy) => void;
fields: MetricsExplorerFields;
errorOptions?: string[];
}

export function GroupBy({ options, onChange, fields, errorOptions, ...rest }: Props) {
export function GroupBy({ options, onChange, fields, ...rest }: Props) {
const handleChange = useCallback(
(selectedOptions: Array<{ label: string }>) => {
const groupBy = selectedOptions.map((option) => option.label);
Expand All @@ -35,17 +34,9 @@ export function GroupBy({ options, onChange, fields, errorOptions, ...rest }: Pr
);

const selectedOptions = Array.isArray(options.groupBy)
? options.groupBy.map((field) => ({
label: field,
color: errorOptions?.includes(field) ? 'danger' : undefined,
}))
? options.groupBy.map((field) => ({ label: field }))
: options.groupBy
? [
{
label: options.groupBy,
color: errorOptions?.includes(options.groupBy) ? 'danger' : undefined,
},
]
? [{ label: options.groupBy }]
: [];

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import {
EuiFormRow,
EuiHorizontalRule,
EuiIconTip,
EuiLink,
EuiLoadingSpinner,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import { ISearchSource, Query } from '@kbn/data-plugin/common';
Expand Down Expand Up @@ -72,7 +70,6 @@ export default function Expressions(props: Props) {
data,
dataViews,
dataViewEditor,
docLinks,
unifiedSearch: {
ui: { SearchBar },
},
Expand Down Expand Up @@ -331,30 +328,6 @@ export default function Expressions(props: Props) {
[ruleParams.groupBy]
);

// Test to see if any of the group fields in groupBy are already filtered down to a single
// group by the filterQuery. If this is the case, then a groupBy is unnecessary, as it would only
// ever produce one group instance
const groupByFilterTestPatterns = useMemo(() => {
if (!ruleParams.groupBy) return null;
const groups = !Array.isArray(ruleParams.groupBy) ? [ruleParams.groupBy] : ruleParams.groupBy;
return groups.map((group: string) => ({
groupName: group,
pattern: new RegExp(`{"match(_phrase)?":{"${group}":"(.*?)"}}`),
}));
}, [ruleParams.groupBy]);

const redundantFilterGroupBy = useMemo(() => {
const { filterQuery } = ruleParams;
if (typeof filterQuery !== 'string' || !groupByFilterTestPatterns) return [];
return groupByFilterTestPatterns
.map(({ groupName, pattern }) => {
if (pattern.test(filterQuery)) {
return groupName;
}
})
.filter((g) => typeof g === 'string') as string[];
}, [ruleParams, groupByFilterTestPatterns]);

if (paramsError) {
return (
<>
Expand Down Expand Up @@ -562,35 +535,8 @@ export default function Expressions(props: Props) {
options={{
groupBy: ruleParams.groupBy || null,
}}
errorOptions={redundantFilterGroupBy}
/>
</EuiFormRow>
{redundantFilterGroupBy.length > 0 && (
<>
<EuiSpacer size="s" />
<EuiText size="xs" color="danger">
<FormattedMessage
id="xpack.observability.customThreshold.rule.alertFlyout.alertPerRedundantFilterError"
defaultMessage="This rule may alert on {matchedGroups} less than expected, because the filter query contains a match for {groupCount, plural, one {this field} other {these fields}}. For more information, refer to {filteringAndGroupingLink}."
values={{
matchedGroups: <strong>{redundantFilterGroupBy.join(', ')}</strong>,
groupCount: redundantFilterGroupBy.length,
filteringAndGroupingLink: (
<EuiLink
data-test-subj="thresholdRuleExpressionsTheDocsLink"
href={`${docLinks.links.observability.metricsThreshold}#filtering-and-grouping`}
>
{i18n.translate(
'xpack.observability.customThreshold.rule.alertFlyout.alertPerRedundantFilterError.docsLink',
{ defaultMessage: 'the docs' }
)}
</EuiLink>
),
}}
/>
</EuiText>
</>
)}
<EuiSpacer size="s" />
<EuiCheckbox
id="metrics-alert-group-disappear-toggle"
Expand Down
Loading