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

[Alert details page] [Custom threshold] Change title of condition charts #175865

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('AlertDetailsAppSection', () => {
it('should render rule and alert data', async () => {
const result = renderComponent();

expect((await result.findByTestId('thresholdAlertOverviewSection')).children.length).toBe(3);
expect((await result.findByTestId('thresholdAlertOverviewSection')).children.length).toBe(6);
expect(result.getByTestId('thresholdRule-2000-2500')).toBeTruthy();
});

Expand Down Expand Up @@ -145,7 +145,30 @@ describe('AlertDetailsAppSection', () => {
(ExpressionChart as jest.Mock).mockImplementation(mockedExpressionChart);
const alertDetailsAppSectionComponent = renderComponent();

expect(alertDetailsAppSectionComponent.getAllByTestId('ExpressionChart').length).toBe(3);
expect(alertDetailsAppSectionComponent.getAllByTestId('ExpressionChart').length).toBe(6);
expect(mockedExpressionChart.mock.calls[0]).toMatchSnapshot();
});

it('should render title on condition charts', async () => {
benakansara marked this conversation as resolved.
Show resolved Hide resolved
const result = renderComponent();

expect(result.getByTestId('chartTitle-0').textContent).toBe(
'Equation result for count (all documents)'
);
expect(result.getByTestId('chartTitle-1').textContent).toBe(
'Equation result for max (system.cpu.user.pct)'
);
expect(result.getByTestId('chartTitle-2').textContent).toBe(
'Equation result for min (system.memory.used.pct)'
);
expect(result.getByTestId('chartTitle-3').textContent).toBe(
'Equation result for min (system.memory.used.pct) + min (system.memory.used.pct) + min (system.memory.used.pct) + min (system.memory.used.pct...'
);
expect(result.getByTestId('chartTitle-4').textContent).toBe(
'Equation result for min (system.memory.used.pct) + min (system.memory.used.pct)'
);
expect(result.getByTestId('chartTitle-5').textContent).toBe(
'Equation result for min (system.memory.used.pct) + min (system.memory.used.pct) + min (system.memory.used.pct)'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EuiSpacer,
EuiText,
EuiTitle,
EuiToolTip,
useEuiTheme,
} from '@elastic/eui';
import { Rule, RuleTypeParams } from '@kbn/alerting-plugin/common';
Expand All @@ -40,6 +41,7 @@ import {
AlertParams,
CustomThresholdAlertFields,
CustomThresholdRuleTypeParams,
MetricExpression,
} from '../../types';
import { ExpressionChart } from '../expression_chart';
import { TIME_LABELS } from '../criterion_preview_chart/criterion_preview_chart';
Expand All @@ -63,6 +65,44 @@ interface AppSectionProps {
setAlertSummaryFields: React.Dispatch<React.SetStateAction<AlertSummaryField[] | undefined>>;
}

const CHART_TITLE_LIMIT = 120;

const equationResultText = i18n.translate('xpack.observability.customThreshold.alertChartTitle', {
defaultMessage: 'Equation result for ',
});

const generateChartTitle = (criterion: MetricExpression) => {
const metricNameResolver: Record<string, string> = {};

criterion.metrics.forEach(
(metric) =>
(metricNameResolver[metric.name] = `${metric.aggType} (${
metric.field ? metric.field : metric.filter ? metric.filter : 'all documents'
})`)
);

let equation = criterion.equation
? criterion.equation
: criterion.metrics.map((m) => m.name).join(' + ');

Object.keys(metricNameResolver)
.sort()
.reverse()
.forEach((metricName) => {
equation = equation.replaceAll(metricName, metricNameResolver[metricName]);
});

const chartTitle =
equation.length > CHART_TITLE_LIMIT
? `${equation.substring(0, CHART_TITLE_LIMIT)}...`
: equation;

return {
tooltip: `${equationResultText}${equation}`,
title: `${equationResultText}${chartTitle}`,
};
};

// eslint-disable-next-line import/no-default-export
export default function AlertDetailsAppSection({
alert,
Expand Down Expand Up @@ -172,9 +212,13 @@ export default function AlertDetailsAppSection({
{ruleParams.criteria.map((criterion, index) => (
<EuiFlexItem key={`criterion-${index}`}>
<EuiPanel hasBorder hasShadow={false}>
<EuiTitle size="xs">
<h4>{criterion.label || 'CUSTOM'} </h4>
</EuiTitle>
<EuiToolTip content={generateChartTitle(criterion).tooltip}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can save the return value of generateChartTitle(criterion) once and then use tooltip and title from that variable. The current implementation goes through generating the title twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 63e1b56

<EuiTitle size="xs">
<h4 data-test-subj={`chartTitle-${index}`}>
{generateChartTitle(criterion).title}
</h4>
</EuiTitle>
</EuiToolTip>
<EuiText size="s" color="subdued">
<FormattedMessage
id="xpack.observability.customThreshold.rule.alertDetailsAppSection.criterion.subtitle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,62 @@ export const buildCustomThresholdRule = (
timeSize: 15,
timeUnit: 'm',
},
{
comparator: Comparator.GT,
metrics: [
{
name: 'A',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
],
threshold: [0.8],
timeSize: 15,
timeUnit: 'm',
equation:
'A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A + A',
},
{
comparator: Comparator.GT,
metrics: [
{
name: 'C',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
{
name: 'D',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
],
threshold: [0.8],
timeSize: 15,
timeUnit: 'm',
},
{
comparator: Comparator.GT,
metrics: [
{
name: 'CAD',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
{
name: 'CADE',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
{
name: 'ADE',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
],
threshold: [0.8],
timeSize: 15,
timeUnit: 'm',
},
],
searchConfiguration: {
query: {
Expand Down