-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor alerts_histogram_panel to reuse logic on alerts_count
- Loading branch information
Showing
30 changed files
with
519 additions
and
815 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
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
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
53 changes: 53 additions & 0 deletions
53
...urity_solution/public/detections/components/alerts_kpis/alerts_count_panel/index.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,53 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { waitFor, act } from '@testing-library/react'; | ||
|
||
import { mount } from 'enzyme'; | ||
import { esQuery } from '../../../../../../../../src/plugins/data/public'; | ||
|
||
import { TestProviders } from '../../../../common/mock'; | ||
|
||
import { AlertsCountPanel } from './index'; | ||
|
||
describe('AlertsCountPanel', () => { | ||
const defaultProps = { | ||
signalIndexName: 'signalIndexName', | ||
}; | ||
|
||
it('renders correctly', async () => { | ||
await act(async () => { | ||
const wrapper = mount( | ||
<TestProviders> | ||
<AlertsCountPanel {...defaultProps} /> | ||
</TestProviders> | ||
); | ||
|
||
expect(wrapper.find('[data-test-subj="alertsCountPanel"]').exists()).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('Query', () => { | ||
it('it render with a illegal KQL', async () => { | ||
const spyOnBuildEsQuery = jest.spyOn(esQuery, 'buildEsQuery'); | ||
spyOnBuildEsQuery.mockImplementation(() => { | ||
throw new Error('Something went wrong'); | ||
}); | ||
const props = { ...defaultProps, query: { query: 'host.name: "', language: 'kql' } }; | ||
const wrapper = mount( | ||
<TestProviders> | ||
<AlertsCountPanel {...props} /> | ||
</TestProviders> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(wrapper.find('[data-test-subj="alertsCountPanel"]').exists()).toBeTruthy(); | ||
}); | ||
}); | ||
}); | ||
}); |
106 changes: 106 additions & 0 deletions
106
...s/security_solution/public/detections/components/alerts_kpis/alerts_count_panel/index.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,106 @@ | ||
/* | ||
* 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 React, { memo, useMemo, useState, useEffect } from 'react'; | ||
import uuid from 'uuid'; | ||
|
||
import { useGlobalTime } from '../../../../common/containers/use_global_time'; | ||
import { HeaderSection } from '../../../../common/components/header_section'; | ||
|
||
import { useQueryAlerts } from '../../../containers/detection_engine/alerts/use_query'; | ||
import { InspectButtonContainer } from '../../../../common/components/inspect'; | ||
|
||
import { getAlertsCountQuery } from './helpers'; | ||
import * as i18n from './translations'; | ||
import { AlertsCount } from './alerts_count'; | ||
import { AlertsCountAggregation } from './types'; | ||
import { DATA_HEIGHT, DEFAULT_STACK_BY_FIELD } from '../common/config'; | ||
import { AlertsStackByField, AlertsStackByOption } from '../common/types'; | ||
import { Filter, esQuery, Query } from '../../../../../../../../src/plugins/data/public'; | ||
import { KpiPanel, StackBySelect } from '../common/components'; | ||
import { useInspectButton } from '../common/hooks'; | ||
|
||
export const DETECTIONS_ALERTS_COUNT_ID = 'detections-alerts-count'; | ||
|
||
interface AlertsCountPanelProps { | ||
defaultStackByOption?: AlertsStackByOption; | ||
filters?: Filter[]; | ||
query?: Query; | ||
signalIndexName: string | null; | ||
} | ||
|
||
export const AlertsCountPanel = memo<AlertsCountPanelProps>( | ||
({ filters, query, signalIndexName }) => { | ||
const { to, from, deleteQuery, setQuery } = useGlobalTime(); | ||
|
||
// create a unique, but stable (across re-renders) query id | ||
const uniqueQueryId = useMemo(() => `${DETECTIONS_ALERTS_COUNT_ID}-${uuid.v4()}`, []); | ||
const [selectedStackByOption, setSelectedStackByOption] = useState<AlertsStackByField>( | ||
DEFAULT_STACK_BY_FIELD | ||
); | ||
|
||
const additionalFilters = useMemo(() => { | ||
try { | ||
return [ | ||
esQuery.buildEsQuery( | ||
undefined, | ||
query != null ? [query] : [], | ||
filters?.filter((f) => f.meta.disabled === false) ?? [] | ||
), | ||
]; | ||
} catch (e) { | ||
return []; | ||
} | ||
}, [query, filters]); | ||
|
||
const { | ||
loading: isLoadingAlerts, | ||
data: alertsData, | ||
setQuery: setAlertsQuery, | ||
response, | ||
request, | ||
refetch, | ||
} = useQueryAlerts<{}, AlertsCountAggregation>({ | ||
query: getAlertsCountQuery(selectedStackByOption, from, to, additionalFilters), | ||
indexName: signalIndexName, | ||
}); | ||
|
||
useEffect(() => { | ||
setAlertsQuery(getAlertsCountQuery(selectedStackByOption, from, to, additionalFilters)); | ||
}, [setAlertsQuery, selectedStackByOption, from, to, additionalFilters]); | ||
|
||
useInspectButton({ | ||
setQuery, | ||
response, | ||
request, | ||
refetch, | ||
uniqueQueryId, | ||
deleteQuery, | ||
loading: isLoadingAlerts, | ||
}); | ||
|
||
return ( | ||
<InspectButtonContainer> | ||
<KpiPanel hasBorder data-test-subj="alertsCountPanel"> | ||
<HeaderSection id={uniqueQueryId} title={i18n.COUNT_TABLE_TITLE} titleSize="s"> | ||
<StackBySelect selected={selectedStackByOption} onSelect={setSelectedStackByOption} /> | ||
</HeaderSection> | ||
<AlertsCount | ||
data={alertsData} | ||
from={from} | ||
loading={isLoadingAlerts} | ||
to={to} | ||
selectedStackByOption={selectedStackByOption} | ||
height={DATA_HEIGHT} | ||
/> | ||
</KpiPanel> | ||
</InspectButtonContainer> | ||
); | ||
} | ||
); | ||
|
||
AlertsCountPanel.displayName = 'AlertsCountPanel'; |
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.