Skip to content

Commit

Permalink
Fetching timezone from ui settings for Trigger context formatting (#913)
Browse files Browse the repository at this point in the history
* fetching timezone from ui settings

Signed-off-by: Amardeepsingh Siglani <[email protected]>

* refactored code

Signed-off-by: Amardeepsingh Siglani <[email protected]>

* removed unused code

Signed-off-by: Amardeepsingh Siglani <[email protected]>

---------

Signed-off-by: Amardeepsingh Siglani <[email protected]>
(cherry picked from commit 0c13bae)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Apr 4, 2024
1 parent 2b82447 commit 6f074ee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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, allowedTypes) =>
allowedTypes.map((type) => ({
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;
}

0 comments on commit 6f074ee

Please sign in to comment.