From 6b97b827c8cf0b79055efa22756c468cd3d702f7 Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Wed, 8 May 2024 11:49:47 -0700 Subject: [PATCH 1/6] fix for quickrange to use datemath to parse datetime strings Signed-off-by: Paul Sebastian --- src/plugins/data/common/data_frames/utils.ts | 22 +++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugins/data/common/data_frames/utils.ts b/src/plugins/data/common/data_frames/utils.ts index c3c55c5f227..5a4e5a77f03 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,26 @@ 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 + * @returns object with fromDate and toDate, both of which will be in utc time and formatted to + * 'YYYY-MM-DD HH:mm:ss.SSS' + */ +export const formatTimePickerDate = (dateRange: TimeRange) => { + const dateMathParse = (date: string) => { + const parsedDate = datemath.parse(date); + return parsedDate ? parsedDate.utc().format('YYYY-MM-DD HH:mm:ss.SSS') : ''; + }; + + 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. * From 2624ad0181fd97a60f38ac620b06a2df8dff254b Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Thu, 23 May 2024 20:26:25 +0000 Subject: [PATCH 2/6] Changeset file for PR #6782 created/updated --- changelogs/fragments/6782.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6782.yml 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 From 4fb844bb81adc11198957c1456c16df51249d992 Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Thu, 23 May 2024 13:37:52 -0700 Subject: [PATCH 3/6] turn the formatting string into parameter Signed-off-by: Paul Sebastian --- src/plugins/data/common/data_frames/utils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/plugins/data/common/data_frames/utils.ts b/src/plugins/data/common/data_frames/utils.ts index 5a4e5a77f03..6569136b8fa 100644 --- a/src/plugins/data/common/data_frames/utils.ts +++ b/src/plugins/data/common/data_frames/utils.ts @@ -295,13 +295,14 @@ export const getTimeField = ( * "now - 15m" * * @param dateRange - of type TimeRange - * @returns object with fromDate and toDate, both of which will be in utc time and formatted to - * 'YYYY-MM-DD HH:mm:ss.SSS' + * @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) => { +export const formatTimePickerDate = (dateRange: TimeRange, dateFormat: string) => { const dateMathParse = (date: string) => { const parsedDate = datemath.parse(date); - return parsedDate ? parsedDate.utc().format('YYYY-MM-DD HH:mm:ss.SSS') : ''; + return parsedDate ? parsedDate.utc().format(dateFormat) : ''; }; const fromDate = dateMathParse(dateRange.from); From c92f50cbb094642845455d7c22cd3fc262d2dcea Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Thu, 23 May 2024 14:17:48 -0700 Subject: [PATCH 4/6] added testing file Signed-off-by: Paul Sebastian --- .../data_frames/data_frame_utils.test.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/plugins/data/common/data_frames/data_frame_utils.test.ts 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..ee5bb08e0e0 --- /dev/null +++ b/src/plugins/data/common/data_frames/data_frame_utils.test.ts @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Any modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +import { TimeRange } from '../types'; +import { formatTimePickerDate } from './utils'; + +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +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', + }); + }); + }); +}); From 1ec018a686df11f25615b55c27ca7ccc2e97f1ec Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Thu, 23 May 2024 14:25:09 -0700 Subject: [PATCH 5/6] add another test Signed-off-by: Paul Sebastian --- .../data/common/data_frames/data_frame_utils.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) 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 index ee5bb08e0e0..97eae460474 100644 --- a/src/plugins/data/common/data_frames/data_frame_utils.test.ts +++ b/src/plugins/data/common/data_frames/data_frame_utils.test.ts @@ -43,5 +43,14 @@ describe('Data Frame Utils', () => { 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', + }); + }); }); }); From adaf17ef147a929b5d493121b1a6b6acc23e3486 Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Fri, 24 May 2024 12:04:54 -0700 Subject: [PATCH 6/6] update license header Signed-off-by: Paul Sebastian --- .../data_frames/data_frame_utils.test.ts | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) 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 index 97eae460474..bb812a29094 100644 --- a/src/plugins/data/common/data_frames/data_frame_utils.test.ts +++ b/src/plugins/data/common/data_frames/data_frame_utils.test.ts @@ -1,36 +1,11 @@ /* + * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - * - * Any modifications Copyright OpenSearch Contributors. See - * GitHub history for details. */ import { TimeRange } from '../types'; import { formatTimePickerDate } from './utils'; -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - describe('Data Frame Utils', () => { describe('formatTimePickerDate function', () => { Date.now = jest.fn(() => new Date('2024-05-04T12:30:00.000Z'));