Skip to content

Commit a801a63

Browse files
committed
Simplify parseFloat transform
1 parent eede01c commit a801a63

File tree

1 file changed

+15
-20
lines changed
  • x-pack/plugins/infra/server/lib/alerting/common

1 file changed

+15
-20
lines changed

x-pack/plugins/infra/server/lib/alerting/common/messages.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ export const stateToAlertMessage = {
2929
}),
3030
};
3131

32-
const comparatorToI18n = (
33-
comparator: Comparator,
34-
threshold: Array<number | string>,
35-
currentValue: number | string
36-
) => {
32+
const toNumber = (value: number | string) =>
33+
typeof value === 'string' ? parseFloat(value) : value;
34+
35+
const comparatorToI18n = (comparator: Comparator, threshold: number[], currentValue: number) => {
3736
const gtText = i18n.translate('xpack.infra.metrics.alerting.threshold.gtComparator', {
3837
defaultMessage: 'greater than',
3938
});
@@ -59,35 +58,27 @@ const comparatorToI18n = (
5958
return ltText;
6059
case Comparator.GT_OR_EQ:
6160
case Comparator.LT_OR_EQ: {
62-
const currentValueAsNumber =
63-
typeof currentValue === 'string' ? parseFloat(currentValue) : currentValue;
64-
const threshold0AsNumber =
65-
typeof threshold[0] === 'string' ? parseFloat(threshold[0]) : threshold[0];
66-
if (threshold0AsNumber === currentValueAsNumber) return eqText;
67-
else if (threshold0AsNumber < currentValueAsNumber) return ltText;
61+
if (threshold[0] === currentValue) return eqText;
62+
else if (threshold[0] < currentValue) return ltText;
6863
return gtText;
6964
}
7065
}
7166
};
7267

7368
const recoveredComparatorToI18n = (
7469
comparator: Comparator,
75-
threshold: Array<number | string>,
76-
currentValue: number | string
70+
threshold: number[],
71+
currentValue: number
7772
) => {
7873
const belowText = i18n.translate('xpack.infra.metrics.alerting.threshold.belowRecovery', {
7974
defaultMessage: 'below',
8075
});
8176
const aboveText = i18n.translate('xpack.infra.metrics.alerting.threshold.aboveRecovery', {
8277
defaultMessage: 'above',
8378
});
84-
const currentValueAsNumber =
85-
typeof currentValue === 'string' ? parseFloat(currentValue) : currentValue;
86-
const threshold0AsNumber =
87-
typeof threshold[0] === 'string' ? parseFloat(threshold[0]) : threshold[0];
8879
switch (comparator) {
8980
case Comparator.BETWEEN:
90-
return currentValueAsNumber < threshold0AsNumber ? belowText : aboveText;
81+
return currentValue < threshold[0] ? belowText : aboveText;
9182
case Comparator.OUTSIDE_RANGE:
9283
return i18n.translate('xpack.infra.metrics.alerting.threshold.betweenRecovery', {
9384
defaultMessage: 'between',
@@ -120,7 +111,7 @@ export const buildFiredAlertReason: (alertResult: {
120111
'{metric} is {comparator} a threshold of {threshold} (current value is {currentValue})',
121112
values: {
122113
metric,
123-
comparator: comparatorToI18n(comparator, threshold, currentValue),
114+
comparator: comparatorToI18n(comparator, threshold.map(toNumber), toNumber(currentValue)),
124115
threshold: thresholdToI18n(threshold),
125116
currentValue,
126117
},
@@ -137,7 +128,11 @@ export const buildRecoveredAlertReason: (alertResult: {
137128
'{metric} is now {comparator} a threshold of {threshold} (current value is {currentValue})',
138129
values: {
139130
metric,
140-
comparator: recoveredComparatorToI18n(comparator, threshold, currentValue),
131+
comparator: recoveredComparatorToI18n(
132+
comparator,
133+
threshold.map(toNumber),
134+
toNumber(currentValue)
135+
),
141136
threshold: thresholdToI18n(threshold),
142137
currentValue,
143138
},

0 commit comments

Comments
 (0)