Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetching timezone from ui settings for Trigger context formatting #913

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import React, { Component, Fragment } from 'react';
import _ from 'lodash';
import moment from 'moment';
import moment from 'moment-timezone';
import { Formik, FieldArray } from 'formik';
import {
EuiButton,
Expand Down Expand Up @@ -36,6 +36,7 @@ import DefineBucketLevelTrigger from '../../DefineBucketLevelTrigger';
import { getPathsPerDataType } from '../../../../CreateMonitor/containers/DefineMonitor/utils/mappings';
import { MONITOR_TYPE } from '../../../../../utils/constants';
import { buildClusterMetricsRequest } from '../../../../CreateMonitor/components/ClusterMetricsMonitor/utils/clusterMetricsMonitorHelpers';
import { getTimeZone } from '../../../utils/helper';

export const DEFAULT_CLOSED_STATES = {
WHEN: false,
Expand Down Expand Up @@ -207,8 +208,8 @@ export default class CreateTrigger extends Component {
};

getTriggerContext = (executeResponse, monitor, values) => ({
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).format(),
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).tz(getTimeZone()).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(getTimeZone()).format(),
results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result),
trigger: formikToTrigger(values, _.get(this.props.monitor, 'ui_metadata', {})),
alert: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import { render } from 'enzyme';
import { Formik } from 'formik';
import { FORMIK_INITIAL_VALUES } from '../../../CreateMonitor/containers/CreateMonitor/utils/constants';
import NotificationConfigDialog from './NotificationConfigDialog';
import { uiSettingsServiceMock } from '../../../../../../../src/core/public/mocks';
import { setUISettings } from '../../../../services';

describe('NotificationConfigDialog', () => {
test('renders', () => {
const uiSettingsMock = uiSettingsServiceMock.createStartContract();
uiSettingsMock.get.mockReturnValue('America/Toronto');
setUISettings(uiSettingsMock);
const component = (
<Formik initialValues={FORMIK_INITIAL_VALUES} onSubmit={() => {}}>
<NotificationConfigDialog
Expand Down
13 changes: 10 additions & 3 deletions public/pages/CreateTrigger/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
FORMIK_INITIAL_DOC_LEVEL_SCRIPT,
FORMIK_INITIAL_TRIGGER_VALUES,
} from '../containers/CreateTrigger/utils/constants';
import moment from 'moment';
import moment from 'moment-timezone';
import { formikToTrigger } from '../containers/CreateTrigger/utils/formikToTrigger';
import { getUISettings } from '../../../services';

export const getChannelOptions = (channels) =>
channels.map((channel) => ({
Expand Down Expand Up @@ -55,9 +56,10 @@ export const getDefaultScript = (monitorValues) => {
export const getTriggerContext = (executeResponse, monitor, values, triggerIndex) => {
let trigger = formikToTrigger(values, _.get(monitor, 'ui_metadata', {}));
if (_.isArray(trigger) && triggerIndex >= 0) trigger = trigger[triggerIndex];

return {
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).format(),
periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).tz(getTimeZone()).format(),
periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(getTimeZone()).format(),
results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result),
trigger: trigger,
alert: null,
Expand Down Expand Up @@ -102,3 +104,8 @@ export const conditionToExpressions = (condition = '', monitors) => {

return expressions;
};

export function getTimeZone() {
const detectedTimeZone = getUISettings().get('dateFormat:tz', 'Browser');
return detectedTimeZone === 'Browser' ? (moment.tz.guess() || moment.format('Z')) : detectedTimeZone;
}
Loading