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 all 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))
31 changes: 31 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,31 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
paulstn marked this conversation as resolved.
Show resolved Hide resolved
*/

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'));
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',
});
});

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',
});
});
});
});
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