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.
[Cases] Use absolute time ranges when attaching a visualization to a …
…case (elastic#189168) ## Summary Users can add visualizations to cases. If the user used a relative time range it will be added to the case with the relative time range. This can be problematic because, in an investigation of an incident, the visualization should reflect the data at the time of discovery. If time passes the visualization will query different data and the finding will be lost. This PR fixes this issue and uses an absolute time range when adding the visualization to the case. ### Testing 1. Attach a visualization from a dashboard with a relative time range. Verify that the absolute time range persisted. 2. Attach a visualization from a dashboard with an absolute time range. Verify that the absolute time range remains affected. 3. Attach a visualization from a dashboard with a relative time range. Open the visualization and verify that the absolute time range is set. <img width="1726" alt="Screenshot 2024-07-25 at 12 49 28 PM" src="https://github.com/user-attachments/assets/f3057d87-e860-4bc9-8f01-876dbf1078f9"> 4. Attach a visualization from the markdown with a relative time range. Verify that the absolute time range persisted. 5. Attach a visualization from the markdown with an absolute time range. Verify that the absolute time range remains affected. 6. Attach a visualization from the markdown with a relative time range. Edit the visualization and verify that the absolute time range is set. 7. Attach a visualization from the markdown with a relative time range. Edit the visualization, change the time range to a relative one, and save it to the case. Verify that the absolute time range is set to the case. <img width="1082" alt="Screenshot 2024-07-25 at 1 01 22 PM" src="https://github.com/user-attachments/assets/a753d918-391f-40ad-90a4-4d12843bab43"> ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ## Release notes Use absolute time ranges when adding visualizations to a case
- Loading branch information
Showing
7 changed files
with
213 additions
and
49 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
27 changes: 27 additions & 0 deletions
27
...ns/cases/public/components/visualizations/actions/convert_to_absolute_time_range.test.tsx
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,27 @@ | ||
/* | ||
* 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 sinon from 'sinon'; | ||
import { convertToAbsoluteTimeRange } from './convert_to_absolute_time_range'; | ||
|
||
describe('convertToAbsoluteDateRange', () => { | ||
test('should not change absolute time range', () => { | ||
const from = '2024-01-01T00:00:00.000Z'; | ||
const to = '2024-02-01T00:00:00.000Z'; | ||
|
||
expect(convertToAbsoluteTimeRange({ from, to })).toEqual({ from, to }); | ||
}); | ||
|
||
it('converts a relative day correctly', () => { | ||
const from = '2024-01-01T00:00:00.000Z'; | ||
const clock = sinon.useFakeTimers(new Date(from)); | ||
const to = new Date(clock.now).toISOString(); | ||
|
||
expect(convertToAbsoluteTimeRange({ from, to: 'now' })).toEqual({ from, to }); | ||
clock.restore(); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
.../plugins/cases/public/components/visualizations/actions/convert_to_absolute_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,28 @@ | ||
/* | ||
* 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 type { TimeRange } from '@kbn/data-plugin/common'; | ||
import { getAbsoluteTimeRange } from '@kbn/data-plugin/common'; | ||
|
||
export const convertToAbsoluteTimeRange = (timeRange?: TimeRange): TimeRange | undefined => { | ||
if (!timeRange) { | ||
return; | ||
} | ||
|
||
const absRange = getAbsoluteTimeRange( | ||
{ | ||
from: timeRange.from, | ||
to: timeRange.to, | ||
}, | ||
{ forceNow: new Date() } | ||
); | ||
|
||
return { | ||
from: absRange.from, | ||
to: absRange.to, | ||
}; | ||
}; |
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
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
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
Oops, something went wrong.