From 8999916e4d9e5c62a51239fc3a8f60c0248963de Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Tue, 2 Apr 2024 18:08:23 -0700 Subject: [PATCH 1/3] fetching timezone from ui settings Signed-off-by: Amardeepsingh Siglani --- .../CreateTrigger/CreateTrigger.js | 24 +++++++++++-------- .../NotificationConfigDialog.test.js | 4 ++++ public/pages/CreateTrigger/utils/helper.js | 9 ++++--- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js index 8010cfd16..587b09eb7 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js @@ -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, @@ -206,15 +206,19 @@ export default class CreateTrigger extends Component { else this.onCreate(trigger, triggerMetadata, formikBag); }; - getTriggerContext = (executeResponse, monitor, values) => ({ - periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).format(), - periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).format(), - results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result), - trigger: formikToTrigger(values, _.get(this.props.monitor, 'ui_metadata', {})), - alert: null, - error: null, - monitor: monitor, - }); + getTriggerContext = (executeResponse, monitor, values) => { + const userTimeZone = getUISettings().get('dateFormat:tz', moment.tz.guess()) || moment().format('Z'); + + return { + periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).tz(userTimeZone).format(), + periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(userTimeZone).format(), + results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result), + trigger: formikToTrigger(values, _.get(this.props.monitor, 'ui_metadata', {})), + alert: null, + error: null, + monitor: monitor, + } + }; openExpression = (expression) => { this.setState({ diff --git a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js index 49fe11d6f..7907a227e 100644 --- a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js +++ b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js @@ -9,9 +9,13 @@ 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(); + setUISettings(uiSettingsMock); const component = ( {}}> channels.map((channel) => ({ @@ -55,9 +56,11 @@ 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]; + const userTimeZone = getUISettings().get('dateFormat:tz', moment.tz.guess()) || moment().format('Z'); + 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(userTimeZone).format(), + periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(userTimeZone).format(), results: [_.get(executeResponse, 'input_results.results[0]')].filter((result) => !!result), trigger: trigger, alert: null, From c96de81be935b30c236457d0c9650c581689e13a Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Tue, 2 Apr 2024 18:24:45 -0700 Subject: [PATCH 2/3] refactored code Signed-off-by: Amardeepsingh Siglani --- .../CreateTrigger/CreateTrigger/CreateTrigger.js | 5 +++-- .../NotificationConfigDialog.test.js | 1 + public/pages/CreateTrigger/utils/helper.js | 10 +++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js index 587b09eb7..6e3259576 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js @@ -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, @@ -210,8 +211,8 @@ export default class CreateTrigger extends Component { const userTimeZone = getUISettings().get('dateFormat:tz', moment.tz.guess()) || moment().format('Z'); return { - periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).tz(userTimeZone).format(), - periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(userTimeZone).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, diff --git a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js index 7907a227e..c1585a889 100644 --- a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js +++ b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js @@ -15,6 +15,7 @@ import { setUISettings } from '../../../../services'; describe('NotificationConfigDialog', () => { test('renders', () => { const uiSettingsMock = uiSettingsServiceMock.createStartContract(); + uiSettingsMock.get.mockReturnValue('America/Toronto'); setUISettings(uiSettingsMock); const component = ( {}}> diff --git a/public/pages/CreateTrigger/utils/helper.js b/public/pages/CreateTrigger/utils/helper.js index efbfcee32..b8c91bdc6 100644 --- a/public/pages/CreateTrigger/utils/helper.js +++ b/public/pages/CreateTrigger/utils/helper.js @@ -56,11 +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]; - const userTimeZone = getUISettings().get('dateFormat:tz', moment.tz.guess()) || moment().format('Z'); return { - periodStart: moment.utc(_.get(executeResponse, 'period_start', Date.now())).tz(userTimeZone).format(), - periodEnd: moment.utc(_.get(executeResponse, 'period_end', Date.now())).tz(userTimeZone).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, @@ -105,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; +} From c286faaf6c4d04a46f3ce432f71e11f03ff5b09b Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Wed, 3 Apr 2024 13:05:12 -0700 Subject: [PATCH 3/3] removed unused code Signed-off-by: Amardeepsingh Siglani --- .../CreateTrigger/CreateTrigger.js | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js index 6e3259576..69c3cfde6 100644 --- a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js +++ b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js @@ -207,19 +207,15 @@ export default class CreateTrigger extends Component { else this.onCreate(trigger, triggerMetadata, formikBag); }; - getTriggerContext = (executeResponse, monitor, values) => { - const userTimeZone = getUISettings().get('dateFormat:tz', moment.tz.guess()) || moment().format('Z'); - - return { - 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, - error: null, - monitor: monitor, - } - }; + getTriggerContext = (executeResponse, monitor, values) => ({ + 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, + error: null, + monitor: monitor, + }); openExpression = (expression) => { this.setState({