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

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Zacqary Adam Xeper 2020-07-29 13:04:41 -05:00 committed by GitHub
parent 57dc9c01f7
commit cbfdb36d9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 116 deletions

View file

@ -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',
@ -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;
}
}
};
@ -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}',
@ -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,
},
@ -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,
},
@ -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...).',
}
);

View file

@ -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);
})

View file

@ -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,
@ -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()),
@ -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 },
],
},
});

View file

@ -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 = {
@ -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',

View file

@ -7649,14 +7649,14 @@
"xpack.infra.metrics.alertFlyout.noDataHelpText": "有効にすると、メトリックが想定された期間内にデータを報告しない場合、またはアラートがElasticsearchをクエリできない場合に、アクションをトリガーします",
"xpack.infra.metrics.alertFlyout.outsideRangeLabel": "is not between",
"xpack.infra.metrics.alertFlyout.removeCondition": "条件を削除",
"xpack.infra.metrics.alerting.inventory.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\}\n\n\\{\\{context.metricOf.condition0\\}\\}はしきい値 \\{\\{context.thresholdOf.condition0\\}\\}を超えました\n現在の値は\\{\\{context.valueOf.condition0\\}\\}です\n",
"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.alerting.alertStateActionVariableDescription": "現在のアラートの状態",
"xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription": "データを報告するグループの名前",
"xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription": "監視されたメトリックのレコード。条件でグループ化されますmetricOf.condition0、metricOf.condition1など。",
"xpack.infra.metrics.alerting.threshold.alerting.reasonActionVariableDescription": "どのメトリックがどのしきい値を超えたのかを含む、アラートがこの状態である理由に関する説明",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription": "アラートしきい値のレコード。条件でグループ化されますthresholdOf.condition0、thresholdOf.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.betweenComparator": "の間",
"xpack.infra.metrics.alerting.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\}は状態\\{\\{context.alertState\\}\\}です\n\n理由\n\\{\\{context.reason\\}\\}\n",

View file

@ -7654,14 +7654,14 @@
"xpack.infra.metrics.alertFlyout.noDataHelpText": "启用此选项可在指标在预期的时间段中未报告任何数据时或告警无法查询 Elasticsearch 时触发操作",
"xpack.infra.metrics.alertFlyout.outsideRangeLabel": "不介于",
"xpack.infra.metrics.alertFlyout.removeCondition": "删除条件",
"xpack.infra.metrics.alerting.inventory.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\}\n\n\\{\\{context.metricOf.condition0\\}\\} 已超过阈值 \\{\\{context.thresholdOf.condition0\\}\\}\n当前值为 \\{\\{context.valueOf.condition0\\}\\}\n",
"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.alerting.alertStateActionVariableDescription": "告警的当前状态",
"xpack.infra.metrics.alerting.threshold.alerting.groupActionVariableDescription": "报告数据的组名称",
"xpack.infra.metrics.alerting.threshold.alerting.metricOfActionVariableDescription": "受监视指标的记录;按条件分组,例如按 metricOf.condition0、metricOf.condition1 等。",
"xpack.infra.metrics.alerting.threshold.alerting.reasonActionVariableDescription": "告警处于此状态的原因描述,包括哪个指标超过哪个阈值",
"xpack.infra.metrics.alerting.threshold.alerting.thresholdOfActionVariableDescription": "告警阈值的记录按条件分组例如按thresholdOf.condition0、thresholdOf.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.betweenComparator": "介于",
"xpack.infra.metrics.alerting.threshold.defaultActionMessage": "\\{\\{alertName\\}\\} - \\{\\{context.group\\}\\} 处于 \\{\\{context.alertState\\}\\} 状态\n\n原因\n\\{\\{context.reason\\}\\}\n",