Skip to content

Commit

Permalink
[8.x] [Inventory][Hosts] Make alert count consistent with the alerts …
Browse files Browse the repository at this point in the history
…page using a filter (elastic#203003) (elastic#203400)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Inventory][Hosts] Make alert count consistent with the alerts page
using a filter (elastic#203003)](elastic#203003)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"jennypavlova","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-12-09T11:48:15Z","message":"[Inventory][Hosts]
Make alert count consistent with the alerts page using a filter
(elastic#203003)\n\nCloses elastic#202979 \r\n\r\n## Summary\r\n\r\nThis PR fixes
inconsistencies between the alerts shown on hosts view and\r\ninventory
and the alerts page. The fix includes using consistent rule\r\ntypes and
filters for both getting the alert count and filters applied\r\non the
alert page\r\n\r\n## How to Test\r\n\r\nService Alert ( Inventory
)\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f3b626da-1a49-42dc-a989-48b13d15ae2c\r\n\r\nHosts
view (+filters) Alerts (Inventory & Custom Threshold
rule)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2a490ad4-e2a4-43b5-b00f-d00ac27f9fd3\r\n\r\nSingle
Host\r\n\r\n\r\nhttps://github.com/user-attachments/assets/7c6a8cf7-f2a2-41f0-9f98-7e7543d4e7d5\r\n\r\ncc:
@roshan-elastic Ping to check the videos of the fix
:)","sha":"62c3aecffe9388a984b4432670ade0ff6f085b75","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services","v8.18.0"],"title":"[Inventory][Hosts]
Make alert count consistent with the alerts page using a
filter","number":203003,"url":"https://github.com/elastic/kibana/pull/203003","mergeCommit":{"message":"[Inventory][Hosts]
Make alert count consistent with the alerts page using a filter
(elastic#203003)\n\nCloses elastic#202979 \r\n\r\n## Summary\r\n\r\nThis PR fixes
inconsistencies between the alerts shown on hosts view and\r\ninventory
and the alerts page. The fix includes using consistent rule\r\ntypes and
filters for both getting the alert count and filters applied\r\non the
alert page\r\n\r\n## How to Test\r\n\r\nService Alert ( Inventory
)\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f3b626da-1a49-42dc-a989-48b13d15ae2c\r\n\r\nHosts
view (+filters) Alerts (Inventory & Custom Threshold
rule)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2a490ad4-e2a4-43b5-b00f-d00ac27f9fd3\r\n\r\nSingle
Host\r\n\r\n\r\nhttps://github.com/user-attachments/assets/7c6a8cf7-f2a2-41f0-9f98-7e7543d4e7d5\r\n\r\ncc:
@roshan-elastic Ping to check the videos of the fix
:)","sha":"62c3aecffe9388a984b4432670ade0ff6f085b75"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/203003","number":203003,"mergeCommit":{"message":"[Inventory][Hosts]
Make alert count consistent with the alerts page using a filter
(elastic#203003)\n\nCloses elastic#202979 \r\n\r\n## Summary\r\n\r\nThis PR fixes
inconsistencies between the alerts shown on hosts view and\r\ninventory
and the alerts page. The fix includes using consistent rule\r\ntypes and
filters for both getting the alert count and filters applied\r\non the
alert page\r\n\r\n## How to Test\r\n\r\nService Alert ( Inventory
)\r\n\r\n\r\nhttps://github.com/user-attachments/assets/f3b626da-1a49-42dc-a989-48b13d15ae2c\r\n\r\nHosts
view (+filters) Alerts (Inventory & Custom Threshold
rule)\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/2a490ad4-e2a4-43b5-b00f-d00ac27f9fd3\r\n\r\nSingle
Host\r\n\r\n\r\nhttps://github.com/user-attachments/assets/7c6a8cf7-f2a2-41f0-9f98-7e7543d4e7d5\r\n\r\ncc:
@roshan-elastic Ping to check the videos of the fix
:)","sha":"62c3aecffe9388a984b4432670ade0ff6f085b75"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: jennypavlova <[email protected]>
  • Loading branch information
kibanamachine and jennypavlova authored Dec 9, 2024
1 parent 54d86c7 commit 859c567
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const AlertsSummaryContent = ({
)}
<EuiFlexItem grow={false}>
<LinkToAlertsPage
kuery={`"${assetId}"`}
kuery={`${assetIdField}:"${assetId}"`}
dateRange={dateRange}
data-test-subj="infraAssetDetailsAlertsTabAlertsShowAllButton"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
*/
import React from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import {
AlertConsumers,
INFRA_RULE_TYPE_IDS,
OBSERVABILITY_RULE_TYPE_IDS,
} from '@kbn/rule-data-utils';
import { AlertConsumers, OBSERVABILITY_RULE_TYPE_IDS } from '@kbn/rule-data-utils';
import { BrushEndListener, type XYBrushEvent } from '@elastic/charts';
import { useSummaryTimeRange } from '@kbn/observability-plugin/public';
import { useBoolean } from '@kbn/react-hooks';
Expand Down Expand Up @@ -47,11 +43,7 @@ export const AlertsTabContent = () => {

const { alertsTableConfigurationRegistry, getAlertsStateTable: AlertsStateTable } =
triggersActionsUi;

const hostsWithAlertsKuery = hostNodes
.filter((host) => host.alertsCount)
.map((host) => `"${host.name}"`)
.join(' OR ');
const hostNamesKuery = hostNodes.map((host) => `host.name: "${host.name}"`).join(' OR ');

return (
<HeightRetainer>
Expand All @@ -73,7 +65,7 @@ export const AlertsTabContent = () => {
<LinkToAlertsPage
dateRange={searchCriteria.dateRange}
data-test-subj="infraHostAlertsTabAlertsShowAllButton"
kuery={`${hostsWithAlertsKuery}`}
kuery={hostNamesKuery}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down Expand Up @@ -146,7 +138,7 @@ const MemoAlertSummaryWidget = React.memo(
return (
<AlertSummaryWidget
chartProps={chartProps}
ruleTypeIds={INFRA_RULE_TYPE_IDS}
ruleTypeIds={OBSERVABILITY_RULE_TYPE_IDS}
consumers={INFRA_ALERT_CONSUMERS}
filter={alertsQuery}
fullSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import React from 'react';
import { EuiIcon, EuiLoadingSpinner, EuiBadge, EuiToolTip } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { OBSERVABILITY_RULE_TYPE_IDS } from '@kbn/rule-data-utils';
import { INFRA_ALERT_CONSUMERS } from '../../../../../../common/constants';
import { INFRA_RULE_TYPE_IDS } from '../../../../../../common/alerting/metrics/types';
import { useAlertsCount } from '../../../../../hooks/use_alerts_count';
import { useAlertsQuery } from '../../hooks/use_alerts_query';

export const AlertsTabBadge = () => {
const { alertsEsQuery } = useAlertsQuery();

const { alertsCount, loading, error } = useAlertsCount({
ruleTypeIds: INFRA_RULE_TYPE_IDS,
ruleTypeIds: OBSERVABILITY_RULE_TYPE_IDS,
consumers: INFRA_ALERT_CONSUMERS,
query: alertsEsQuery,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const useKibanaMock = useKibana as jest.Mock;

const commonEntityFields: Partial<InventoryEntity> = {
entityLastSeenTimestamp: 'foo',
entityId: 'entity1',
entityId: '1',
};

describe('AlertsBadge', () => {
const mockAsKqlFilter = jest.fn();

beforeEach(() => {
jest.clearAllMocks();

Expand All @@ -29,6 +31,11 @@ describe('AlertsBadge', () => {
prepend: (path: string) => path,
},
},
entityManager: {
entityClient: {
asKqlFilter: mockAsKqlFilter,
},
},
},
});
});
Expand All @@ -52,10 +59,11 @@ describe('AlertsBadge', () => {
provider: null,
},
};
mockAsKqlFilter.mockReturnValue('host.name: "foo"');

render(<AlertsBadge entity={entity} />);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.getAttribute('href')).toEqual(
`/app/observability/alerts?_a=(kuery:\"entity1\",status:active)`
`/app/observability/alerts?_a=(kuery:'host.name: "foo"',status:active)`
);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.textContent).toEqual('1');
});
Expand All @@ -78,11 +86,40 @@ describe('AlertsBadge', () => {

alertsCount: 5,
};
mockAsKqlFilter.mockReturnValue('service.name: "bar"');

render(<AlertsBadge entity={entity} />);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.getAttribute('href')).toEqual(
`/app/observability/alerts?_a=(kuery:\"entity1\",status:active)`
`/app/observability/alerts?_a=(kuery:'service.name: "bar"',status:active)`
);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.textContent).toEqual('5');
});
it('render alerts badge for a service entity with multiple identity fields', () => {
const entity: InventoryEntity = {
...(commonEntityFields as InventoryEntity),
entityType: 'service',
entityDisplayName: 'foo',
entityIdentityFields: ['service.name', 'service.environment'],
entityDefinitionId: 'service',
service: {
name: 'bar',
environment: 'prod',
},
agent: {
name: 'node',
},
cloud: {
provider: null,
},
alertsCount: 2,
};

mockAsKqlFilter.mockReturnValue('service.name: "bar" AND service.environment: "prod"');

render(<AlertsBadge entity={entity} />);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.getAttribute('href')).toEqual(
`/app/observability/alerts?_a=(kuery:'service.name: "bar" AND service.environment: "prod"',status:active)`
);
expect(screen.queryByTestId('inventoryAlertsBadgeLink')?.textContent).toEqual('2');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ export function AlertsBadge({ entity }: { entity: InventoryEntity }) {
const {
services: {
http: { basePath },
entityManager,
},
} = useKibana();

const activeAlertsHref = basePath.prepend(
`/app/observability/alerts?_a=${rison.encode({
kuery: `"${entity.entityId}"`,
kuery: entityManager.entityClient.asKqlFilter({
entity: {
identity_fields: entity.entityIdentityFields,
},
...entity,
}),
status: 'active',
})}`
);
Expand Down

0 comments on commit 859c567

Please sign in to comment.