Ensure we use the right duration for messaging on this alert (#87579)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Chris Roberson 2021-01-11 13:27:32 -05:00 committed by GitHub
parent c2a556bb70
commit d4b3ea9c3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 10 deletions

View file

@ -17,11 +17,13 @@ jest.mock('../lib/alerts/fetch_clusters', () => ({
fetchClusters: jest.fn(), fetchClusters: jest.fn(),
})); }));
jest.mock('moment', () => { jest.mock('moment', () => {
return function () { const moment = function () {
return { return {
format: () => 'THE_DATE', format: () => 'THE_DATE',
}; };
}; };
moment.duration = () => ({ humanize: () => 'HUMANIZED_DURATION' });
return moment;
}); });
jest.mock('../static_globals', () => ({ jest.mock('../static_globals', () => ({
@ -170,11 +172,11 @@ describe('LicenseExpirationAlert', () => {
action: '[Please update your license.](elasticsearch/nodes)', action: '[Please update your license.](elasticsearch/nodes)',
actionPlain: 'Please update your license.', actionPlain: 'Please update your license.',
internalFullMessage: internalFullMessage:
'License expiration alert is firing for testCluster. Your license expires in THE_DATE. [Please update your license.](elasticsearch/nodes)', 'License expiration alert is firing for testCluster. Your license expires in HUMANIZED_DURATION. [Please update your license.](elasticsearch/nodes)',
internalShortMessage: internalShortMessage:
'License expiration alert is firing for testCluster. Your license expires in THE_DATE. Please update your license.', 'License expiration alert is firing for testCluster. Your license expires in HUMANIZED_DURATION. Please update your license.',
clusterName, clusterName,
expiredDate: 'THE_DATE', expiredDate: 'HUMANIZED_DURATION',
state: 'firing', state: 'firing',
}); });
}); });

View file

@ -17,11 +17,7 @@ import {
LegacyAlert, LegacyAlert,
} from '../../common/types/alerts'; } from '../../common/types/alerts';
import { AlertExecutorOptions, AlertInstance } from '../../../alerts/server'; import { AlertExecutorOptions, AlertInstance } from '../../../alerts/server';
import { import { ALERT_LICENSE_EXPIRATION, LEGACY_ALERT_DETAILS } from '../../common/constants';
ALERT_LICENSE_EXPIRATION,
FORMAT_DURATION_TEMPLATE_SHORT,
LEGACY_ALERT_DETAILS,
} from '../../common/constants';
import { AlertMessageTokenType } from '../../common/enums'; import { AlertMessageTokenType } from '../../common/enums';
import { AlertingDefaults } from './alert_helpers'; import { AlertingDefaults } from './alert_helpers';
import { SanitizedAlert } from '../../../alerts/common'; import { SanitizedAlert } from '../../../alerts/common';
@ -113,12 +109,13 @@ export class LicenseExpirationAlert extends BaseAlert {
) { ) {
const legacyAlert = item.meta as LegacyAlert; const legacyAlert = item.meta as LegacyAlert;
const $expiry = moment(legacyAlert.metadata.time); const $expiry = moment(legacyAlert.metadata.time);
const $duration = moment.duration(+new Date() - $expiry.valueOf());
if (alertState.ui.isFiring) { if (alertState.ui.isFiring) {
const actionText = i18n.translate('xpack.monitoring.alerts.licenseExpiration.action', { const actionText = i18n.translate('xpack.monitoring.alerts.licenseExpiration.action', {
defaultMessage: 'Please update your license.', defaultMessage: 'Please update your license.',
}); });
const action = `[${actionText}](elasticsearch/nodes)`; const action = `[${actionText}](elasticsearch/nodes)`;
const expiredDate = $expiry.format(FORMAT_DURATION_TEMPLATE_SHORT).trim(); const expiredDate = $duration.humanize();
instance.scheduleActions('default', { instance.scheduleActions('default', {
internalShortMessage: i18n.translate( internalShortMessage: i18n.translate(
'xpack.monitoring.alerts.licenseExpiration.firing.internalShortMessage', 'xpack.monitoring.alerts.licenseExpiration.firing.internalShortMessage',