From 7ad459d75f11ee49a956083cbdb483af21923bab Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 4 Apr 2024 22:25:52 +0000 Subject: [PATCH] Fetching timezone from ui settings for Trigger context formatting (#913) * fetching timezone from ui settings Signed-off-by: Amardeepsingh Siglani * refactored code Signed-off-by: Amardeepsingh Siglani * removed unused code Signed-off-by: Amardeepsingh Siglani --------- Signed-off-by: Amardeepsingh Siglani (cherry picked from commit 0c13bae923cacccb0c9c6b482bb197428c7ad146) Signed-off-by: github-actions[bot] --- .../CreateTrigger/CreateTrigger/CreateTrigger.js | 7 ++++--- .../NotificationConfigDialog.test.js | 5 +++++ public/pages/CreateTrigger/utils/helper.js | 13 ++++++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js b/public/pages/CreateTrigger/containers/CreateTrigger/CreateTrigger/CreateTrigger.js index 8010cfd16..69c3cfde6 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, @@ -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, @@ -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, diff --git a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js index 49fe11d6f..c1585a889 100644 --- a/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js +++ b/public/pages/CreateTrigger/containers/DefineCompositeLevelTrigger/NotificationConfigDialog.test.js @@ -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 = ( {}}> channels.map((channel) => ({ @@ -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, @@ -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; +}