From f8b06e3726ba54a52415dfa7cfb5e14471235f6a Mon Sep 17 00:00:00 2001 From: igoristic Date: Fri, 14 Feb 2020 06:29:49 -0500 Subject: [PATCH] [Monitoring] Fixed logs timezone (#57611) * Fixed logs timezone * Passing tz as a param * Fixed tests Co-authored-by: Elastic Machine --- .../legacy/plugins/monitoring/common/formatting.js | 12 +++++++----- .../monitoring/public/components/alerts/alerts.js | 10 +++++++--- .../public/components/chart/get_chart_options.js | 2 +- .../components/cluster/overview/alerts_panel.js | 6 +++++- .../ccr_shard/__snapshots__/ccr_shard.test.js.snap | 2 +- .../components/elasticsearch/ccr_shard/ccr_shard.js | 5 ++++- .../elasticsearch/ccr_shard/ccr_shard.test.js | 1 + .../elasticsearch/shard_activity/parse_props.js | 5 ++++- .../monitoring/public/components/logs/logs.js | 10 ++++++++-- .../monitoring/public/views/license/controller.js | 4 +++- 10 files changed, 41 insertions(+), 16 deletions(-) diff --git a/x-pack/legacy/plugins/monitoring/common/formatting.js b/x-pack/legacy/plugins/monitoring/common/formatting.js index a3b3ce07c8c76..ed5d68f942dfd 100644 --- a/x-pack/legacy/plugins/monitoring/common/formatting.js +++ b/x-pack/legacy/plugins/monitoring/common/formatting.js @@ -13,14 +13,16 @@ export const SMALL_BYTES = '0.0 b'; export const LARGE_ABBREVIATED = '0,0.[0]a'; /** - * Format the {@code date} in the user's expected date/time format using their guessed local time zone. + * Format the {@code date} in the user's expected date/time format using their dateFormat:tz defined time zone. * @param date Either a numeric Unix timestamp or a {@code Date} object * @returns The date formatted using 'LL LTS' */ -export function formatDateTimeLocal(date, useUTC = false) { - return useUTC - ? moment.utc(date).format('LL LTS') - : moment.tz(date, moment.tz.guess()).format('LL LTS'); +export function formatDateTimeLocal(date, timezone) { + if (timezone === 'Browser') { + timezone = moment.tz.guess() || 'utc'; + } + + return moment.tz(date, timezone).format('LL LTS'); } /** diff --git a/x-pack/legacy/plugins/monitoring/public/components/alerts/alerts.js b/x-pack/legacy/plugins/monitoring/public/components/alerts/alerts.js index 4c2f3b027bc8a..11fcef73a4b97 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/alerts/alerts.js +++ b/x-pack/legacy/plugins/monitoring/public/components/alerts/alerts.js @@ -5,6 +5,7 @@ */ import React from 'react'; +import chrome from '../../np_imports/ui/chrome'; import { capitalize } from 'lodash'; import { formatDateTimeLocal } from '../../../common/formatting'; import { formatTimestampToDuration } from '../../../common'; @@ -21,7 +22,7 @@ const linkToCategories = { 'kibana/instances': 'Kibana Instances', 'logstash/instances': 'Logstash Nodes', }; -const getColumns = (kbnUrl, scope) => [ +const getColumns = (kbnUrl, scope, timezone) => [ { name: i18n.translate('xpack.monitoring.alerts.statusColumnTitle', { defaultMessage: 'Status', @@ -126,7 +127,7 @@ const getColumns = (kbnUrl, scope) => [ }), field: 'update_timestamp', sortable: true, - render: timestamp => formatDateTimeLocal(timestamp), + render: timestamp => formatDateTimeLocal(timestamp, timezone), }, { name: i18n.translate('xpack.monitoring.alerts.triggeredColumnTitle', { @@ -151,11 +152,14 @@ export const Alerts = ({ alerts, angular, sorting, pagination, onTableChange }) category: alert.metadata.link, })); + const injector = chrome.dangerouslyGetActiveInjector(); + const timezone = injector.get('config').get('dateFormat:tz'); + return ( diff --git a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/__snapshots__/ccr_shard.test.js.snap b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/__snapshots__/ccr_shard.test.js.snap index 366c23135cc76..e55f9c84b51fe 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/__snapshots__/ccr_shard.test.js.snap +++ b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/__snapshots__/ccr_shard.test.js.snap @@ -147,7 +147,7 @@ exports[`CcrShard that it renders normally 1`] = ` size="s" >

- September 27, 2018 9:32:09 AM + September 27, 2018 1:32:09 PM

diff --git a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.js b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.js index 68c87b386da49..af0ff323b7ba8 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.js +++ b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.js @@ -5,6 +5,7 @@ */ import React, { Fragment, PureComponent } from 'react'; +import chrome from '../../../np_imports/ui/chrome'; import { EuiPage, EuiPageBody, @@ -92,6 +93,8 @@ export class CcrShard extends PureComponent { renderLatestStat() { const { stat, timestamp } = this.props; + const injector = chrome.dangerouslyGetActiveInjector(); + const timezone = injector.get('config').get('dateFormat:tz'); return ( -

{formatDateTimeLocal(timestamp)}

+

{formatDateTimeLocal(timestamp, timezone)}

{JSON.stringify(stat, null, 2)} diff --git a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.test.js b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.test.js index 17caa8429a275..b950c2ca0a6d2 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.test.js +++ b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/ccr_shard/ccr_shard.test.js @@ -11,6 +11,7 @@ import { CcrShard } from './ccr_shard'; jest.mock('../../../np_imports/ui/chrome', () => { return { getBasePath: () => '', + dangerouslyGetActiveInjector: () => ({ get: () => ({ get: () => 'utc' }) }), }; }); diff --git a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/shard_activity/parse_props.js b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/shard_activity/parse_props.js index 692025631f3b8..133b520947b1b 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/shard_activity/parse_props.js +++ b/x-pack/legacy/plugins/monitoring/public/components/elasticsearch/shard_activity/parse_props.js @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ +import chrome from '../../../np_imports/ui/chrome'; import { capitalize } from 'lodash'; import { formatMetric } from 'plugins/monitoring/lib/format_number'; import { formatDateTimeLocal } from '../../../../common/formatting'; @@ -38,13 +39,15 @@ export const parseProps = props => { } = props; const { files, size } = index; + const injector = chrome.dangerouslyGetActiveInjector(); + const timezone = injector.get('config').get('dateFormat:tz'); return { name: indexName || index.name, shard: `${id} / ${isPrimary ? 'Primary' : 'Replica'}`, relocationType: type === 'PRIMARY_RELOCATION' ? 'Primary Relocation' : normalizeString(type), stage: normalizeString(stage), - startTime: formatDateTimeLocal(startTimeInMillis), + startTime: formatDateTimeLocal(startTimeInMillis, timezone), totalTime: formatMetric(Math.floor(totalTimeInMillis / 1000), '00:00:00'), isCopiedFromPrimary: !isPrimary || type === 'PRIMARY_RELOCATION', sourceName: source.name === undefined ? 'n/a' : source.name, diff --git a/x-pack/legacy/plugins/monitoring/public/components/logs/logs.js b/x-pack/legacy/plugins/monitoring/public/components/logs/logs.js index 926f5cdda26a7..744ebb5a7ceb4 100644 --- a/x-pack/legacy/plugins/monitoring/public/components/logs/logs.js +++ b/x-pack/legacy/plugins/monitoring/public/components/logs/logs.js @@ -14,6 +14,12 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { Reason } from './reason'; import { capabilities } from '../../np_imports/ui/capabilities'; +const getFormattedDateTimeLocal = timestamp => { + const injector = chrome.dangerouslyGetActiveInjector(); + const timezone = injector.get('config').get('dateFormat:tz'); + return formatDateTimeLocal(timestamp, timezone); +}; + const columnTimestampTitle = i18n.translate('xpack.monitoring.logs.listing.timestampTitle', { defaultMessage: 'Timestamp', }); @@ -43,7 +49,7 @@ const columns = [ field: 'timestamp', name: columnTimestampTitle, width: '12%', - render: timestamp => formatDateTimeLocal(timestamp, true), + render: timestamp => getFormattedDateTimeLocal(timestamp), }, { field: 'level', @@ -73,7 +79,7 @@ const clusterColumns = [ field: 'timestamp', name: columnTimestampTitle, width: '12%', - render: timestamp => formatDateTimeLocal(timestamp, true), + render: timestamp => getFormattedDateTimeLocal(timestamp), }, { field: 'level', diff --git a/x-pack/legacy/plugins/monitoring/public/views/license/controller.js b/x-pack/legacy/plugins/monitoring/public/views/license/controller.js index dcd3ca76ceffd..ce6e9c8fb74cd 100644 --- a/x-pack/legacy/plugins/monitoring/public/views/license/controller.js +++ b/x-pack/legacy/plugins/monitoring/public/views/license/controller.js @@ -54,11 +54,13 @@ export class LicenseViewController { } renderReact($scope) { + const injector = chrome.dangerouslyGetActiveInjector(); + const timezone = injector.get('config').get('dateFormat:tz'); $scope.$evalAsync(() => { const { isPrimaryCluster, license, isExpired, uploadLicensePath } = this; let expiryDate = license.expiry_date_in_millis; if (license.expiry_date_in_millis !== undefined) { - expiryDate = formatDateTimeLocal(license.expiry_date_in_millis); + expiryDate = formatDateTimeLocal(license.expiry_date_in_millis, timezone); } // Mount the React component to the template