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

[7.x] [Metrics UI] Fix formatting of values in inventory context.reason (#73155) #73721

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 69 additions & 8 deletions x-pack/plugins/infra/server/lib/alerting/common/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export const stateToAlertMessage = {
}),
};

const toNumber = (value: number | string) =>
typeof value === 'string' ? parseFloat(value) : value;

const comparatorToI18n = (comparator: Comparator, threshold: number[], currentValue: number) => {
const gtText = i18n.translate('xpack.infra.metrics.alerting.threshold.gtComparator', {
defaultMessage: 'greater than',
Expand All @@ -54,10 +57,11 @@ const comparatorToI18n = (comparator: Comparator, threshold: number[], currentVa
case Comparator.LT:
return ltText;
case Comparator.GT_OR_EQ:
case Comparator.LT_OR_EQ:
case Comparator.LT_OR_EQ: {
if (threshold[0] === currentValue) return eqText;
else if (threshold[0] < currentValue) return ltText;
return gtText;
}
}
};

Expand Down Expand Up @@ -88,7 +92,7 @@ const recoveredComparatorToI18n = (
}
};

const thresholdToI18n = ([a, b]: number[]) => {
const thresholdToI18n = ([a, b]: Array<number | string>) => {
if (typeof b === 'undefined') return a;
return i18n.translate('xpack.infra.metrics.alerting.threshold.thresholdRange', {
defaultMessage: '{a} and {b}',
Expand All @@ -99,15 +103,15 @@ const thresholdToI18n = ([a, b]: number[]) => {
export const buildFiredAlertReason: (alertResult: {
metric: string;
comparator: Comparator;
threshold: number[];
currentValue: number;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.firedAlertReason', {
defaultMessage:
'{metric} is {comparator} a threshold of {threshold} (current value is {currentValue})',
values: {
metric,
comparator: comparatorToI18n(comparator, threshold, currentValue),
comparator: comparatorToI18n(comparator, threshold.map(toNumber), toNumber(currentValue)),
threshold: thresholdToI18n(threshold),
currentValue,
},
Expand All @@ -116,15 +120,19 @@ export const buildFiredAlertReason: (alertResult: {
export const buildRecoveredAlertReason: (alertResult: {
metric: string;
comparator: Comparator;
threshold: number[];
currentValue: number;
threshold: Array<number | string>;
currentValue: number | string;
}) => string = ({ metric, comparator, threshold, currentValue }) =>
i18n.translate('xpack.infra.metrics.alerting.threshold.recoveredAlertReason', {
defaultMessage:
'{metric} is now {comparator} a threshold of {threshold} (current value is {currentValue})',
values: {
metric,
comparator: recoveredComparatorToI18n(comparator, threshold, currentValue),
comparator: recoveredComparatorToI18n(
comparator,
threshold.map(toNumber),
toNumber(currentValue)
),
threshold: thresholdToI18n(threshold),
currentValue,
},
Expand All @@ -150,3 +158,56 @@ export const buildErrorAlertReason = (metric: string) =>
metric,
},
});

export const groupActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.groupActionVariableDescription',
{
defaultMessage: 'Name of the group reporting data',
}
);

export const alertStateActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.alertStateActionVariableDescription',
{
defaultMessage: 'Current state of the alert',
}
);

export const reasonActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.reasonActionVariableDescription',
{
defaultMessage:
'A description of why the alert is in this state, including which metrics have crossed which thresholds',
}
);

export const timestampActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.timestampDescription',
{
defaultMessage: 'A timestamp of when the alert was detected.',
}
);

export const valueActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.valueActionVariableDescription',
{
defaultMessage:
'The value of the metric in the specified condition. Usage: (ctx.value.condition0, ctx.value.condition1, etc...).',
}
);

export const metricActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.metricActionVariableDescription',
{
defaultMessage:
'The metric name in the specified condition. Usage: (ctx.metric.condition0, ctx.metric.condition1, etc...).',
}
);

export const thresholdActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.thresholdActionVariableDescription',
{
defaultMessage:
'The threshold value of the metric for the specified condition. Usage: (ctx.threshold.condition0, ctx.threshold.condition1, etc...).',
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const createInventoryMetricThresholdExecutor = (libs: InfraBackendLibs) =
const resultWithVerboseMetricName = {
...result[item],
metric: toMetricOpt(result[item].metric)?.text || result[item].metric,
currentValue: formatMetric(result[item].metric, result[item].currentValue),
};
return buildFiredAlertReason(resultWithVerboseMetricName);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { i18n } from '@kbn/i18n';
import { schema } from '@kbn/config-schema';
import {
createInventoryMetricThresholdExecutor,
Expand All @@ -12,6 +11,15 @@ import {
import { METRIC_INVENTORY_THRESHOLD_ALERT_TYPE_ID, Comparator } from './types';
import { InfraBackendLibs } from '../../infra_types';
import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils';
import {
groupActionVariableDescription,
alertStateActionVariableDescription,
reasonActionVariableDescription,
timestampActionVariableDescription,
valueActionVariableDescription,
metricActionVariableDescription,
thresholdActionVariableDescription,
} from '../common/messages';

const condition = schema.object({
threshold: schema.arrayOf(schema.number()),
Expand Down Expand Up @@ -44,45 +52,13 @@ export const registerMetricInventoryThresholdAlertType = (libs: InfraBackendLibs
executor: createInventoryMetricThresholdExecutor(libs),
actionVariables: {
context: [
{
name: 'group',
description: i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription',
{
defaultMessage: 'Name of the group reporting data',
}
),
},
{
name: 'valueOf',
description: i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.valueOfActionVariableDescription',
{
defaultMessage:
'Record of the current value of the watched metric; grouped by condition, i.e valueOf.condition0, valueOf.condition1, etc.',
}
),
},
{
name: 'thresholdOf',
description: i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription',
{
defaultMessage:
'Record of the alerting threshold; grouped by condition, i.e thresholdOf.condition0, thresholdOf.condition1, etc.',
}
),
},
{
name: 'metricOf',
description: i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription',
{
defaultMessage:
'Record of the watched metric; grouped by condition, i.e metricOf.condition0, metricOf.condition1, etc.',
}
),
},
{ name: 'group', description: groupActionVariableDescription },
{ name: 'alertState', description: alertStateActionVariableDescription },
{ name: 'reason', description: reasonActionVariableDescription },
{ name: 'timestamp', description: timestampActionVariableDescription },
{ name: 'value', description: valueActionVariableDescription },
{ name: 'metric', description: metricActionVariableDescription },
{ name: 'threshold', description: thresholdActionVariableDescription },
],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { i18n } from '@kbn/i18n';
import { schema } from '@kbn/config-schema';
import { METRIC_EXPLORER_AGGREGATIONS } from '../../../../common/http_api/metrics_explorer';
import { createMetricThresholdExecutor, FIRED_ACTIONS } from './metric_threshold_executor';
import { METRIC_THRESHOLD_ALERT_TYPE_ID, Comparator } from './types';
import { InfraBackendLibs } from '../../infra_types';
import { oneOfLiterals, validateIsStringElasticsearchJSONFilter } from '../common/utils';
import {
groupActionVariableDescription,
alertStateActionVariableDescription,
reasonActionVariableDescription,
timestampActionVariableDescription,
valueActionVariableDescription,
metricActionVariableDescription,
thresholdActionVariableDescription,
} from '../common/messages';

export function registerMetricThresholdAlertType(libs: InfraBackendLibs) {
const baseCriterion = {
Expand All @@ -31,59 +39,6 @@ export function registerMetricThresholdAlertType(libs: InfraBackendLibs) {
metric: schema.never(),
});

const groupActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription',
{
defaultMessage: 'Name of the group reporting data',
}
);

const alertStateActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.alertStateActionVariableDescription',
{
defaultMessage: 'Current state of the alert',
}
);

const reasonActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.reasonActionVariableDescription',
{
defaultMessage:
'A description of why the alert is in this state, including which metrics have crossed which thresholds',
}
);

const timestampActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.timestampDescription',
{
defaultMessage: 'A timestamp of when the alert was detected.',
}
);

const valueActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.valueActionVariableDescription',
{
defaultMessage:
'The value of the metric in the specified condition. Usage: (ctx.value.condition0, ctx.value.condition1, etc...).',
}
);

const metricActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.metricActionVariableDescription',
{
defaultMessage:
'The metric name in the specified condition. Usage: (ctx.metric.condition0, ctx.metric.condition1, etc...).',
}
);

const thresholdActionVariableDescription = i18n.translate(
'xpack.infra.metrics.alerting.threshold.alerting.thresholdActionVariableDescription',
{
defaultMessage:
'The threshold value of the metric for the specified condition. Usage: (ctx.threshold.condition0, ctx.threshold.condition1, etc...).',
}
);

return {
id: METRIC_THRESHOLD_ALERT_TYPE_ID,
name: 'Metric threshold',
Expand Down
17 changes: 6 additions & 11 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -8882,17 +8882,12 @@
"xpack.infra.metrics.alertFlyout.weekLabel": "週",
"xpack.infra.metrics.alerting.inventory.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\}は状態\\{\\{context.alertState\\}\\}です\n\n理由:\n\\{\\{context.reason\\}\\}\n",
"xpack.infra.metrics.alerting.inventory.threshold.fired": "実行",
"xpack.infra.metrics.alerting.threshold.aboveRecovery": "より大",
"xpack.infra.metrics.alerting.threshold.alerting.alertStateActionVariableDescription": "現在のアラートの状態",
"xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription": "データを報告するグループの名前",
"xpack.infra.metrics.alerting.threshold.alerting.metricActionVariableDescription": "指定された条件のメトリック名。使用方法:(ctx.metric.condition0、ctx.metric.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription": "監視されたメトリックのレコード。条件でグループ化されます(metricOf.condition0、metricOf.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alerting.reasonActionVariableDescription": "どのメトリックがどのしきい値を超えたのかを含む、アラートがこの状態である理由に関する説明",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdActionVariableDescription": "指定された条件のメトリックのしきい値。使用方法:(ctx.threshold.condition0、ctx.threshold.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription": "アラートしきい値のレコード。条件でグループ化されます(thresholdOf.condition0、thresholdOf.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alerting.timestampDescription": "アラートが検出された時点のタイムスタンプ。",
"xpack.infra.metrics.alerting.threshold.alerting.valueActionVariableDescription": "指定された条件のメトリックの値。使用方法:(ctx.value.condition0、ctx.value.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alerting.valueOfActionVariableDescription": "監視されたメトリックの現在の値のレコード。条件でグループ化されます(valueOf.condition0、valueOf.condition1など)。",
"xpack.infra.metrics.alerting.alertStateActionVariableDescription": "現在のアラートの状態",
"xpack.infra.metrics.alerting.groupActionVariableDescription": "データを報告するグループの名前",
"xpack.infra.metrics.alerting.metricActionVariableDescription": "監視されたメトリックのレコード。条件でグループ化されます(metric.condition0、metric.condition1など)。",
"xpack.infra.metrics.alerting.reasonActionVariableDescription": "どのメトリックがどのしきい値を超えたのかを含む、アラートがこの状態である理由に関する説明",
"xpack.infra.metrics.alerting.thresholdActionVariableDescription": "アラートしきい値のレコード。条件でグループ化されます(threshold.condition0、threshold.condition1など)。",
"xpack.infra.metrics.alerting.valueActionVariableDescription": "監視されたメトリックの現在の値のレコード。条件でグループ化されます(value.condition0、value.condition1など)。",
"xpack.infra.metrics.alerting.threshold.alertState": "アラート",
"xpack.infra.metrics.alerting.threshold.belowRecovery": "より小",
"xpack.infra.metrics.alerting.threshold.betweenComparator": "の間",
Expand Down
17 changes: 6 additions & 11 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -8884,17 +8884,12 @@
"xpack.infra.metrics.alertFlyout.weekLabel": "周",
"xpack.infra.metrics.alerting.inventory.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\} 处于 \\{\\{context.alertState\\}\\} 状态\n\n原因:\n\\{\\{context.reason\\}\\}\n",
"xpack.infra.metrics.alerting.inventory.threshold.fired": "已触发",
"xpack.infra.metrics.alerting.threshold.aboveRecovery": "高于",
"xpack.infra.metrics.alerting.threshold.alerting.alertStateActionVariableDescription": "告警的当前状态",
"xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription": "报告数据的组名称",
"xpack.infra.metrics.alerting.threshold.alerting.metricActionVariableDescription": "指定条件中的指标名称。用法:(ctx.metric.condition0, ctx.metric.condition1, 诸如此类)。",
"xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription": "受监视指标的记录;按条件分组,例如按 metricOf.condition0、metricOf.condition1 等。",
"xpack.infra.metrics.alerting.threshold.alerting.reasonActionVariableDescription": "告警处于此状态的原因描述,包括哪个指标超过哪个阈值",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdActionVariableDescription": "指定条件的指标阈值。用法:(ctx.threshold.condition0, ctx.threshold.condition1, 诸如此类)。",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription": "告警阈值的记录;按条件分组,例如按thresholdOf.condition0、thresholdOf.condition1 等。",
"xpack.infra.metrics.alerting.threshold.alerting.timestampDescription": "检测到告警时的时间戳。",
"xpack.infra.metrics.alerting.threshold.alerting.valueActionVariableDescription": "指定条件中的指标值。用法:(ctx.value.condition0, ctx.value.condition1, 诸如此类)。",
"xpack.infra.metrics.alerting.threshold.alerting.valueOfActionVariableDescription": "受监视指标当前值的记录;按条件分组,例如按 valueOf.condition0、valueOf.condition1 等。",
"xpack.infra.metrics.alerting.alertStateActionVariableDescription": "告警的当前状态",
"xpack.infra.metrics.alerting.groupActionVariableDescription": "报告数据的组名称",
"xpack.infra.metrics.alerting.metricActionVariableDescription": "受监视指标的记录;按条件分组,例如按 metric.condition0、metric.condition1 等。",
"xpack.infra.metrics.alerting.reasonActionVariableDescription": "告警处于此状态的原因描述,包括哪个指标超过哪个阈值",
"xpack.infra.metrics.alerting.thresholdActionVariableDescription": "告警阈值的记录;按条件分组,例如按threshold.condition0、threshold.condition1 等。",
"xpack.infra.metrics.alerting.valueActionVariableDescription": "受监视指标当前值的记录;按条件分组,例如按 value.condition0、value.condition1 等。",
"xpack.infra.metrics.alerting.threshold.alertState": "告警",
"xpack.infra.metrics.alerting.threshold.belowRecovery": "低于",
"xpack.infra.metrics.alerting.threshold.betweenComparator": "介于",
Expand Down