Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
benakansara committed Feb 2, 2024
1 parent 8f8fbd4 commit edca4c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,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 All @@ -87,36 +125,6 @@ export default function AlertDetailsAppSection({
const alertEnd = alert.fields[ALERT_END] ? moment(alert.fields[ALERT_END]).valueOf() : undefined;
const groups = alert.fields[ALERT_GROUP];
const tags = alert.fields[TAGS];
const CHART_TITLE_LIMIT = 120;

const generateChartTitle = (criterion: MetricExpression) => {
let equationTitle = '';
const symbolResolver: Record<string, string> = {};
const equation = criterion.equation
? criterion.equation
: criterion.metrics.map((m) => m.name).join(' + ');

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

[...equation].forEach((symbol) =>
symbolResolver[symbol] ? (equationTitle += symbolResolver[symbol]) : (equationTitle += symbol)
);

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

return {
tooltip: `Equation result for ${equationTitle}`,
title: `Equation result for ${chartTitle}`,
};
};

const annotations = [
<AlertAnnotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ export const buildCustomThresholdRule = (
comparator: Comparator.GT,
metrics: [
{
name: 'C',
name: 'CAD',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
{
name: 'D',
name: 'CADE',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
{
name: 'E',
name: 'ADE',
aggType: Aggregators.MIN,
field: 'system.memory.used.pct',
},
Expand Down

0 comments on commit edca4c6

Please sign in to comment.