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

[Discover] use roundUp when converting timestamp for PPL #8935

Merged
merged 4 commits into from
Dec 5, 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/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 @@
* 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 });

Check warning on line 160 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#L159-L160

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

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

Check warning on line 165 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#L165

Added line #L165 was not covered by tests

return { fromDate, toDate };
};
Expand Down
Loading