Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for quickrange to use datemath to parse datetime strings #6782

Merged
merged 7 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6782.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Quickrange selection fix ([#6782](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6782))
47 changes: 47 additions & 0 deletions src/plugins/data/common/data_frames/data_frame_utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-License-Identifier: Apache-2.0
paulstn marked this conversation as resolved.
Show resolved Hide resolved
*
* 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
paulstn marked this conversation as resolved.
Show resolved Hide resolved
* 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'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kavilla is this fine for mocking the time or is there another preferred option?


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',
});
});
});
});
23 changes: 22 additions & 1 deletion src/plugins/data/common/data_frames/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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.
Expand Down Expand Up @@ -290,6 +290,27 @@
: 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);

Check warning on line 304 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L303-L304

Added lines #L303 - L304 were not covered by tests
return parsedDate ? parsedDate.utc().format(dateFormat) : '';
};

const fromDate = dateMathParse(dateRange.from);
const toDate = dateMathParse(dateRange.to);

Check warning on line 309 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L308-L309

Added lines #L308 - L309 were not covered by tests

return { fromDate, toDate };

Check warning on line 311 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L311

Added line #L311 was not covered by tests
};

/**
* Checks if the value is a GeoPoint. Expects an object with lat and lon properties.
*
Expand Down
Loading