Skip to content

Commit

Permalink
[Monitoring] Fixed logs timezone (#57611)
Browse files Browse the repository at this point in the history
* Fixed logs timezone

* Passing tz as a param

* Fixed tests

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
igoristic and elasticmachine authored Feb 14, 2020
1 parent f87053e commit f8b06e3
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 16 deletions.
12 changes: 7 additions & 5 deletions x-pack/legacy/plugins/monitoring/common/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <em>guessed</em> local time zone.
* Format the {@code date} in the user's expected date/time format using their <em>dateFormat:tz</em> 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');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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',
Expand Down Expand Up @@ -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', {
Expand All @@ -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 (
<EuiMonitoringTable
className="alertsTable"
rows={alertsFlattened}
columns={getColumns(angular.kbnUrl, angular.scope)}
columns={getColumns(angular.kbnUrl, angular.scope, timezone)}
sorting={{
...sorting,
sort: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { merge } from 'lodash';
import { CHART_LINE_COLOR, CHART_TEXT_COLOR } from '../../../common/constants';

export async function getChartOptions(axisOptions) {
const $injector = await chrome.dangerouslyGetActiveInjector();
const $injector = chrome.dangerouslyGetActiveInjector();
const timezone = $injector.get('config').get('dateFormat:tz');
const opts = {
legend: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import React, { Fragment } from 'react';
import moment from 'moment-timezone';
import chrome from '../../../np_imports/ui/chrome';
import { FormattedAlert } from 'plugins/monitoring/components/alerts/formatted_alert';
import { mapSeverity } from 'plugins/monitoring/components/alerts/map_severity';
import { formatTimestampToDuration } from '../../../../common/format_timestamp_to_duration';
Expand Down Expand Up @@ -57,6 +58,9 @@ export function AlertsPanel({ alerts, changeUrl }) {
severityIcon.iconType = 'check';
}

const injector = chrome.dangerouslyGetActiveInjector();
const timezone = injector.get('config').get('dateFormat:tz');

return (
<EuiCallOut
key={`alert-item-${index}`}
Expand All @@ -79,7 +83,7 @@ export function AlertsPanel({ alerts, changeUrl }) {
id="xpack.monitoring.cluster.overview.alertsPanel.lastCheckedTimeText"
defaultMessage="Last checked {updateDateTime} (triggered {duration} ago)"
values={{
updateDateTime: formatDateTimeLocal(item.update_timestamp),
updateDateTime: formatDateTimeLocal(item.update_timestamp, timezone),
duration: formatTimestampToDuration(item.timestamp, CALCULATE_DURATION_SINCE),
}}
/>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import React, { Fragment, PureComponent } from 'react';
import chrome from '../../../np_imports/ui/chrome';
import {
EuiPage,
EuiPageBody,
Expand Down Expand Up @@ -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 (
<EuiAccordion
Expand All @@ -110,7 +113,7 @@ export class CcrShard extends PureComponent {
>
<Fragment>
<EuiTitle size="s">
<h2>{formatDateTimeLocal(timestamp)}</h2>
<h2>{formatDateTimeLocal(timestamp, timezone)}</h2>
</EuiTitle>
<EuiHorizontalRule />
<EuiCodeBlock language="json">{JSON.stringify(stat, null, 2)}</EuiCodeBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CcrShard } from './ccr_shard';
jest.mock('../../../np_imports/ui/chrome', () => {
return {
getBasePath: () => '',
dangerouslyGetActiveInjector: () => ({ get: () => ({ get: () => 'utc' }) }),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions x-pack/legacy/plugins/monitoring/public/components/logs/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down Expand Up @@ -43,7 +49,7 @@ const columns = [
field: 'timestamp',
name: columnTimestampTitle,
width: '12%',
render: timestamp => formatDateTimeLocal(timestamp, true),
render: timestamp => getFormattedDateTimeLocal(timestamp),
},
{
field: 'level',
Expand Down Expand Up @@ -73,7 +79,7 @@ const clusterColumns = [
field: 'timestamp',
name: columnTimestampTitle,
width: '12%',
render: timestamp => formatDateTimeLocal(timestamp, true),
render: timestamp => getFormattedDateTimeLocal(timestamp),
},
{
field: 'level',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8b06e3

Please sign in to comment.