forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra] Fix call to service api (elastic#203451)
fixes [203389](elastic#203389) ## Summary Fix the call to `/api/infra/services` when using a relative date range ![service_api_relative_date_range](https://github.com/user-attachments/assets/772bba2c-07c8-4031-8d8a-61bdc7ab6d70) ### How to test - Navigate to host detail view, and change the data picker to use relative dates - Click on Submit
- Loading branch information
1 parent
13c5722
commit 1a20fda
Showing
4 changed files
with
114 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
x-pack/plugins/observability_solution/infra/public/hooks/use_time_range.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useTimeRange } from './use_time_range'; | ||
import * as datemath from '../utils/datemath'; | ||
|
||
jest.mock('../utils/datemath'); | ||
|
||
describe('useTimeRange', () => { | ||
const mockParseDateRange = datemath.parseDateRange as jest.Mock; | ||
|
||
beforeEach(() => { | ||
Date.now = jest.fn(() => new Date(Date.UTC(2021, 0, 1, 12)).valueOf()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('returns default timestamps when rangeFrom and rangeTo are not provided', () => { | ||
const { result } = renderHook(() => useTimeRange({})); | ||
|
||
const now = Date.now(); | ||
const expectedFrom = new Date(now - 15 * 60000).toISOString(); | ||
const expectedTo = new Date(now).toISOString(); | ||
|
||
expect(result.current.from).toBe(expectedFrom); | ||
expect(result.current.to).toBe(expectedTo); | ||
}); | ||
|
||
it('returns parsed date range when rangeFrom and rangeTo are provided', () => { | ||
const mockFrom = '2021-01-01T00:00:00.000Z'; | ||
const mockTo = '2021-01-01T01:00:00.000Z'; | ||
mockParseDateRange.mockReturnValue({ from: mockFrom, to: mockTo }); | ||
|
||
const { result } = renderHook(() => useTimeRange({ rangeFrom: 'now-15m', rangeTo: 'now' })); | ||
|
||
expect(result.current.from).toBe(mockFrom); | ||
expect(result.current.to).toBe(mockTo); | ||
}); | ||
|
||
it('returns default timestamps when parseDateRange returns undefined values', () => { | ||
mockParseDateRange.mockReturnValue({ from: undefined, to: undefined }); | ||
|
||
const { result } = renderHook(() => useTimeRange({ rangeFrom: 'now-15m', rangeTo: 'now' })); | ||
|
||
const now = Date.now(); | ||
const expectedFrom = new Date(now - 15 * 60000).toISOString(); | ||
const expectedTo = new Date(now).toISOString(); | ||
|
||
expect(result.current.from).toBe(expectedFrom); | ||
expect(result.current.to).toBe(expectedTo); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/observability_solution/infra/public/hooks/use_time_range.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useMemo } from 'react'; | ||
import { parseDateRange } from '../utils/datemath'; | ||
|
||
const DEFAULT_FROM_IN_MILLISECONDS = 15 * 60000; | ||
|
||
const getDefaultTimestamps = () => { | ||
const now = Date.now(); | ||
|
||
return { | ||
from: new Date(now - DEFAULT_FROM_IN_MILLISECONDS).toISOString(), | ||
to: new Date(now).toISOString(), | ||
}; | ||
}; | ||
|
||
export const useTimeRange = ({ rangeFrom, rangeTo }: { rangeFrom?: string; rangeTo?: string }) => { | ||
const parsedDateRange = useMemo(() => { | ||
const defaults = getDefaultTimestamps(); | ||
|
||
if (!rangeFrom || !rangeTo) { | ||
return defaults; | ||
} | ||
|
||
const { from = defaults.from, to = defaults.to } = parseDateRange({ | ||
from: rangeFrom, | ||
to: rangeTo, | ||
}); | ||
|
||
return { from, to }; | ||
}, [rangeFrom, rangeTo]); | ||
|
||
return parsedDateRange; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters