Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
fkanout committed Dec 16, 2022
1 parent e50602f commit ca4db3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import { formatAlertEvaluationValue } from './format_alert_evaluation_value';

describe('formatAlertEvaluationValue', () => {
it('it returns - when there is no evaluationValue passed', () => {
it('returns - when there is no evaluationValue passed', () => {
expect(formatAlertEvaluationValue('apm.transaction_error_rate', undefined)).toBe('-');
});
it('it returns the evaluation value when ruleTypeId im unknown aka unformatted', () => {
it('returns the evaluation value when ruleTypeId in unknown aka unformatted', () => {
expect(formatAlertEvaluationValue('unknown.rule.type', 2000)).toBe(2000);
});
it('it returns the evaluation value formatted as percent when the alert rule type is "apm.transaction_error_rate" ', () => {
it('returns the evaluation value formatted as percent when the alert rule type is "apm.transaction_error_rate" ', () => {
expect(formatAlertEvaluationValue('apm.transaction_error_rate', 20)).toBe('20%');
});
it('it returns the evaluation value formatted as duration in ms when the alert rule type is "apm.transaction_duration" ', () => {
it('returns the evaluation value formatted as duration in ms when the alert rule type is "apm.transaction_duration" ', () => {
expect(formatAlertEvaluationValue('apm.transaction_duration', 140000)).toBe('140 ms');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

import { asMillisecondDuration, asPercent } from '../../common/utils/formatters';
import {
AlertEvaluationUnitType,
ALERT_EVALUATION_UNIT_TYPE,
getAlertEvaluationUnitTypeByRuleTypeId,
} from './get_alert_evaluation_unit_type_by_rule_type_id';

export const formatAlertEvaluationValue = (ruleTypeId?: string, evaluationValue?: number) => {
if (!evaluationValue || !ruleTypeId) return '-';
const unitType = getAlertEvaluationUnitTypeByRuleTypeId(ruleTypeId);
switch (unitType) {
case AlertEvaluationUnitType.Duration:
case ALERT_EVALUATION_UNIT_TYPE.DURATION:
return asMillisecondDuration(evaluationValue);
case AlertEvaluationUnitType.Percent:
case ALERT_EVALUATION_UNIT_TYPE.PERCENT:
return asPercent(evaluationValue, 100);
default:
return evaluationValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export enum AlertEvaluationUnitType {
Duration,
Percent,
Number,
}

export const ALERT_EVALUATION_UNIT_TYPE = {
DURATION: 'DURATION',
PERCENT: 'PERCENT',
NUMBER: 'NUMBER',
} as const;

type ObjectValues<T> = T[keyof T];
type AlertEvaluationUnitType = ObjectValues<typeof ALERT_EVALUATION_UNIT_TYPE>;

export const getAlertEvaluationUnitTypeByRuleTypeId = (
ruleTypeId: string
): AlertEvaluationUnitType => {
switch (ruleTypeId) {
case 'apm.transaction_duration':
return AlertEvaluationUnitType.Duration;
return ALERT_EVALUATION_UNIT_TYPE.DURATION;
case 'apm.transaction_error_rate':
return AlertEvaluationUnitType.Percent;
return ALERT_EVALUATION_UNIT_TYPE.PERCENT;
default:
return AlertEvaluationUnitType.Number;
return ALERT_EVALUATION_UNIT_TYPE.NUMBER;
}
};

0 comments on commit ca4db3a

Please sign in to comment.