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.
[Actionable Observability] Refactor data view creation in alerts sear…
…ch bar (elastic#142474) * Refactor creating dataView for alert table * Add test for use_alert_data_view * Use [] as default value for indexPatterns
- Loading branch information
1 parent
3284ba1
commit 73a6965
Showing
8 changed files
with
170 additions
and
78 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/observability/public/config/alert_feature_ids.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,16 @@ | ||
/* | ||
* 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 { AlertConsumers } from '@kbn/rule-data-utils'; | ||
import type { ValidFeatureId } from '@kbn/rule-data-utils'; | ||
|
||
export const observabilityAlertFeatureIds: ValidFeatureId[] = [ | ||
AlertConsumers.APM, | ||
AlertConsumers.INFRASTRUCTURE, | ||
AlertConsumers.LOGS, | ||
AlertConsumers.UPTIME, | ||
]; |
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
100 changes: 100 additions & 0 deletions
100
x-pack/plugins/observability/public/hooks/use_alert_data_view.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,100 @@ | ||
/* | ||
* 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 { DataView } from '@kbn/data-views-plugin/common'; | ||
import type { ValidFeatureId } from '@kbn/rule-data-utils'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { AsyncState } from 'react-use/lib/useAsync'; | ||
import { kibanaStartMock } from '../utils/kibana_react.mock'; | ||
import { observabilityAlertFeatureIds } from '../config'; | ||
import { useAlertDataView } from './use_alert_data_view'; | ||
|
||
const mockUseKibanaReturnValue = kibanaStartMock.startContract(); | ||
|
||
jest.mock('@kbn/kibana-react-plugin/public', () => ({ | ||
__esModule: true, | ||
useKibana: jest.fn(() => mockUseKibanaReturnValue), | ||
})); | ||
|
||
describe('useAlertDataView', () => { | ||
const mockedDataView = 'dataView'; | ||
|
||
beforeEach(() => { | ||
mockUseKibanaReturnValue.services.http.get.mockImplementation(async () => ({ | ||
index_name: [ | ||
'.alerts-observability.uptime.alerts-*', | ||
'.alerts-observability.metrics.alerts-*', | ||
'.alerts-observability.logs.alerts-*', | ||
'.alerts-observability.apm.alerts-*', | ||
], | ||
})); | ||
mockUseKibanaReturnValue.services.data.dataViews.create.mockImplementation( | ||
async () => mockedDataView | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('initially is loading and does not have data', async () => { | ||
await act(async () => { | ||
const mockedAsyncDataView = { | ||
loading: true, | ||
}; | ||
|
||
const { result, waitForNextUpdate } = renderHook<ValidFeatureId[], AsyncState<DataView>>(() => | ||
useAlertDataView(observabilityAlertFeatureIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual(mockedAsyncDataView); | ||
}); | ||
}); | ||
|
||
it('returns dataView for the provided featureIds', async () => { | ||
await act(async () => { | ||
const mockedAsyncDataView = { | ||
loading: false, | ||
value: mockedDataView, | ||
}; | ||
|
||
const { result, waitForNextUpdate } = renderHook<ValidFeatureId[], AsyncState<DataView>>(() => | ||
useAlertDataView(observabilityAlertFeatureIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual(mockedAsyncDataView); | ||
}); | ||
}); | ||
|
||
it('returns error with no data when error happens', async () => { | ||
const error = new Error('http error'); | ||
mockUseKibanaReturnValue.services.http.get.mockImplementation(async () => { | ||
throw error; | ||
}); | ||
|
||
await act(async () => { | ||
const mockedAsyncDataView = { | ||
loading: false, | ||
error, | ||
}; | ||
|
||
const { result, waitForNextUpdate } = renderHook<ValidFeatureId[], AsyncState<DataView>>(() => | ||
useAlertDataView(observabilityAlertFeatureIds) | ||
); | ||
|
||
await waitForNextUpdate(); | ||
await waitForNextUpdate(); | ||
|
||
expect(result.current).toEqual(mockedAsyncDataView); | ||
}); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
x-pack/plugins/observability/public/hooks/use_alert_data_view.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,33 @@ | ||
/* | ||
* 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 { DataView } from '@kbn/data-views-plugin/common'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
import { BASE_RAC_ALERTS_API_PATH } from '@kbn/rule-registry-plugin/common'; | ||
import type { ValidFeatureId } from '@kbn/rule-data-utils'; | ||
import useAsync from 'react-use/lib/useAsync'; | ||
import type { AsyncState } from 'react-use/lib/useAsync'; | ||
|
||
import { ObservabilityAppServices } from '../application/types'; | ||
|
||
export function useAlertDataView(featureIds: ValidFeatureId[]): AsyncState<DataView> { | ||
const { http, data: dataService } = useKibana<ObservabilityAppServices>().services; | ||
const features = featureIds.sort().join(','); | ||
|
||
const dataView = useAsync(async () => { | ||
const { index_name: indexNames } = await http.get<{ index_name: string[] }>( | ||
`${BASE_RAC_ALERTS_API_PATH}/index`, | ||
{ | ||
query: { features }, | ||
} | ||
); | ||
|
||
return dataService.dataViews.create({ title: indexNames.join(',') }); | ||
}, [features]); | ||
|
||
return dataView; | ||
} |
32 changes: 0 additions & 32 deletions
32
x-pack/plugins/observability/public/hooks/use_alert_index_names.ts
This file was deleted.
Oops, something went wrong.
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