Skip to content

Commit

Permalink
fix for quickrange to use datemath to parse datetime strings (opensea…
Browse files Browse the repository at this point in the history
…rch-project#6782)

provides a formatting util function meant to convert quick range time (such as 'now-15m') to datetimes that can be understood.

Signed-off-by: Paul Sebastian <paulstn@amazon.com>

---------

Signed-off-by: Paul Sebastian <paulstn@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and LDrago27 committed Jun 3, 2024
1 parent acfbdd3 commit 44fcec4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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
*/

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',
});
});
});
});
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 {
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 @@ 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.
*
Expand Down

0 comments on commit 44fcec4

Please sign in to comment.