From 2925d5c1bddea26f23479adeff33831fb8b5712e Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Thu, 30 May 2024 13:33:07 -0700 Subject: [PATCH] fix for quickrange to use datemath to parse datetime strings (#6782) (#6868) provides a formatting util function meant to convert quick range time (such as 'now-15m') to datetimes that can be understood. --------- (cherry picked from commit 347639f4ad9e3e856a21bae48b161bfc64434644) Signed-off-by: Paul Sebastian Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/6782.yml | 2 ++ .../data_frames/data_frame_utils.test.ts | 31 +++++++++++++++++++ src/plugins/data/common/data_frames/utils.ts | 23 +++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/6782.yml create mode 100644 src/plugins/data/common/data_frames/data_frame_utils.test.ts diff --git a/changelogs/fragments/6782.yml b/changelogs/fragments/6782.yml new file mode 100644 index 00000000000..19349a8de10 --- /dev/null +++ b/changelogs/fragments/6782.yml @@ -0,0 +1,2 @@ +fix: +- Quickrange selection fix ([#6782](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6782)) \ No newline at end of file diff --git a/src/plugins/data/common/data_frames/data_frame_utils.test.ts b/src/plugins/data/common/data_frames/data_frame_utils.test.ts new file mode 100644 index 00000000000..bb812a29094 --- /dev/null +++ b/src/plugins/data/common/data_frames/data_frame_utils.test.ts @@ -0,0 +1,31 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { TimeRange } from '../types'; +import { formatTimePickerDate } from './utils'; + +describe('Data Frame Utils', () => { + describe('formatTimePickerDate function', () => { + Date.now = jest.fn(() => new Date('2024-05-04T12:30:00.000Z')); + + test('should return a correctly formatted date', () => { + const range = { from: 'now-15m', to: 'now' } as TimeRange; + const formattedDate = formatTimePickerDate(range, 'YYYY-MM-DD HH:mm:ss.SSS'); + expect(formattedDate).toStrictEqual({ + fromDate: '2024-05-04 12:15:00.000', + toDate: '2024-05-04 12:30:00.000', + }); + }); + + test('should indicate invalid when given bad dates', () => { + const range = { from: 'fake', to: 'date' } as TimeRange; + const formattedDate = formatTimePickerDate(range, 'YYYY-MM-DD HH:mm:ss.SSS'); + expect(formattedDate).toStrictEqual({ + fromDate: 'Invalid date', + toDate: 'Invalid date', + }); + }); + }); +}); diff --git a/src/plugins/data/common/data_frames/utils.ts b/src/plugins/data/common/data_frames/utils.ts index c3c55c5f227..6569136b8fa 100644 --- a/src/plugins/data/common/data_frames/utils.ts +++ b/src/plugins/data/common/data_frames/utils.ts @@ -17,7 +17,7 @@ import { import { IFieldType } from './fields'; import { IndexPatternFieldMap, IndexPatternSpec } from '../index_patterns'; import { IOpenSearchDashboardsSearchRequest } from '../search'; -import { GetAggTypeFn, GetDataFrameAggQsFn } from '../types'; +import { GetAggTypeFn, GetDataFrameAggQsFn, TimeRange } from '../types'; /** * Returns the raw data frame from the search request. @@ -290,6 +290,27 @@ export const getTimeField = ( : fields.find((field) => field.type === 'date'); }; +/** + * Parses timepicker datetimes using datemath package. Will attempt to parse strings such as + * "now - 15m" + * + * @param dateRange - of type TimeRange + * @param dateFormat - formatting string (should work with Moment) + * @returns object with `fromDate` and `toDate` strings, both of which will be in utc time and formatted to + * the `dateFormat` parameter + */ +export const formatTimePickerDate = (dateRange: TimeRange, dateFormat: string) => { + const dateMathParse = (date: string) => { + const parsedDate = datemath.parse(date); + return parsedDate ? parsedDate.utc().format(dateFormat) : ''; + }; + + const fromDate = dateMathParse(dateRange.from); + const toDate = dateMathParse(dateRange.to); + + return { fromDate, toDate }; +}; + /** * Checks if the value is a GeoPoint. Expects an object with lat and lon properties. *