Skip to content

Commit

Permalink
[Discover] use roundUp when converting timestamp for PPL (opensearch-…
Browse files Browse the repository at this point in the history
…project#8935)

Signed-off-by: Joshua Li <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Signed-off-by: Federico Silva <[email protected]>
  • Loading branch information
2 people authored and Federico Silva committed Dec 10, 2024
1 parent e9aec0d commit 4e884c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8935.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Use roundUp when converting timestamp for PPL ([#8935](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8935))
2 changes: 2 additions & 0 deletions packages/opensearch-datemath/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ declare const datemath: {

/**
* Parses a string into a moment object. The string can be something like "now - 15m".
* @param options.roundUp - If true, rounds the parsed date to the end of the
* unit. Only works for string with "/" like "now/d".
* @param options.forceNow If this optional parameter is supplied, "now" will be treated as this
* date, rather than the real "now".
*/
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/data/common/data_frames/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import datemath from '@opensearch/datemath';
import { formatTimePickerDate } from '.';

describe('formatTimePickerDate', () => {
const mockDateFormat = 'YYYY-MM-DD HH:mm:ss';

beforeEach(() => {
jest.clearAllMocks();
});

it('should handle date range with rounding', () => {
jest.spyOn(datemath, 'parse');

const result = formatTimePickerDate({ from: 'now/d', to: 'now/d' }, mockDateFormat);

expect(result.fromDate).not.toEqual(result.toDate);

expect(datemath.parse).toHaveBeenCalledTimes(2);
expect(datemath.parse).toHaveBeenCalledWith('now/d', { roundUp: undefined });
expect(datemath.parse).toHaveBeenCalledWith('now/d', { roundUp: true });
});
});
6 changes: 3 additions & 3 deletions src/plugins/data/common/data_frames/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ export const getTimeField = (
* the `dateFormat` parameter
*/
export const formatTimePickerDate = (dateRange: TimeRange, dateFormat: string) => {
const dateMathParse = (date: string) => {
const parsedDate = datemath.parse(date);
const dateMathParse = (date: string, roundUp?: boolean) => {
const parsedDate = datemath.parse(date, { roundUp });
return parsedDate ? parsedDate.utc().format(dateFormat) : '';
};

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

return { fromDate, toDate };
};
Expand Down

0 comments on commit 4e884c5

Please sign in to comment.