Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert grouping functionality to the observability alerts page #189958

Merged
merged 24 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a1eb6ce
Add alert grouping functionality to the alerts page
maryam-saeidi Aug 6, 2024
e08a2a6
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 6, 2024
b788b66
Add ungrouped tooltip.
maryam-saeidi Aug 6, 2024
2bba4c9
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Aug 6, 2024
3c368d1
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 6, 2024
d26fe4e
Update ungrouped message
maryam-saeidi Aug 7, 2024
48596ee
Register a separate alerts table for the alerts page with grouping
maryam-saeidi Aug 7, 2024
631a4d8
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 7, 2024
1a57f84
Merge branch 'main' into grouped-alert-table
shahzad31 Aug 9, 2024
288ce39
Fix import
maryam-saeidi Aug 9, 2024
f19cf45
Fix using data view
maryam-saeidi Aug 12, 2024
1af6ae1
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 12, 2024
38ab838
Fix applying alert status filter
maryam-saeidi Aug 13, 2024
6b7b242
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 20, 2024
a4d5dd0
Fix KQL kuery and translation
maryam-saeidi Aug 20, 2024
0a312d6
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 20, 2024
a13dccf
Fix timestamp and type
maryam-saeidi Aug 21, 2024
1263565
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 21, 2024
d83aecb
Fix test
maryam-saeidi Aug 21, 2024
d06e0d9
Fix mismatch in number of alerts without the mapping for the grouped …
maryam-saeidi Aug 21, 2024
4e354b2
Remove badge color
maryam-saeidi Aug 21, 2024
fd2d043
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 21, 2024
e8165da
fix test
maryam-saeidi Aug 22, 2024
e0da06b
Merge branch 'main' into grouped-alert-table
maryam-saeidi Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/kbn-alerts-grouping/src/components/alerts_grouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ const NextLevel = ({
return children(nextGroupingFilters)!;
};

const AlertsGroupingInternal = (props: AlertsGroupingProps) => {
const AlertsGroupingInternal = <T extends Record<string, unknown>>(
props: AlertsGroupingProps<T>
) => {
const {
groupingId,
services,
Expand Down Expand Up @@ -194,7 +196,7 @@ const AlertsGroupingInternal = (props: AlertsGroupingProps) => {
};

return (
<AlertsGroupingLevel
<AlertsGroupingLevel<T>
{...props}
getGrouping={getGrouping}
groupingLevel={level}
Expand Down Expand Up @@ -275,10 +277,13 @@ const AlertsGroupingInternal = (props: AlertsGroupingProps) => {
* );
* ```
*/
export const AlertsGrouping = memo((props: AlertsGroupingProps) => {
return (
<AlertsGroupingContextProvider>
<AlertsGroupingInternal {...props} />
</AlertsGroupingContextProvider>
);
});
const typedMemo: <T>(c: T) => T = memo;
export const AlertsGrouping = typedMemo(
<T extends Record<string, unknown>>(props: AlertsGroupingProps<T>) => {
return (
<AlertsGroupingContextProvider>
<AlertsGroupingInternal {...props} />
</AlertsGroupingContextProvider>
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const DEFAULT_FILTERS: Filter[] = [];
/**
* Renders an alerts grouping level
*/
export const AlertsGroupingLevel = memo(
const typedMemo: <T>(c: T) => T = memo;
export const AlertsGroupingLevel = typedMemo(
<T extends Record<string, unknown> = {}>({
featureIds,
defaultFilters = DEFAULT_FILTERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,29 @@ import {
AlertsTableConfigurationRegistry,
RenderCustomActionsRowArgs,
} from '@kbn/triggers-actions-ui-plugin/public/types';
import { casesFeatureId, observabilityFeatureId } from '../../../../common';
import { DataViewsServicePublic } from '@kbn/data-views-plugin/public/types';
import { HttpSetup } from '@kbn/core-http-browser';
import { NotificationsStart } from '@kbn/core-notifications-browser';
import {
casesFeatureId,
observabilityAlertFeatureIds,
observabilityFeatureId,
} from '../../../../common';
import { AlertActions } from '../../../pages/alerts/components/alert_actions';
import { useGetAlertFlyoutComponents } from '../../alerts_flyout/use_get_alert_flyout_components';
import type { ObservabilityRuleTypeRegistry } from '../../../rules/create_observability_rule_type_registry';
import { ALERTS_PAGE_ALERTS_TABLE_CONFIG_ID } from '../../../constants';
import type { ConfigSchema } from '../../../plugin';
import { getRenderCellValue } from '../common/render_cell_value';
import { getColumns } from '../common/get_columns';
import { getPersistentControlsHook } from './get_peristent_controls';

export const getAlertsPageTableConfiguration = (
observabilityRuleTypeRegistry: ObservabilityRuleTypeRegistry,
config: ConfigSchema
config: ConfigSchema,
dataViews: DataViewsServicePublic,
http: HttpSetup,
notifications: NotificationsStart
): AlertsTableConfigurationRegistry => {
const renderCustomActionsRow = (props: RenderCustomActionsRowArgs) => {
return (
Expand All @@ -34,7 +46,7 @@ export const getAlertsPageTableConfiguration = (
);
};
return {
id: observabilityFeatureId,
id: ALERTS_PAGE_ALERTS_TABLE_CONFIG_ID,
cases: { featureId: casesFeatureId, owner: [observabilityFeatureId] },
columns: getColumns({ showRuleName: true }),
getRenderCellValue,
Expand All @@ -53,6 +65,15 @@ export const getAlertsPageTableConfiguration = (
return { header, body, footer };
},
ruleTypeIds: observabilityRuleTypeRegistry.list(),
usePersistentControls: getPersistentControlsHook({
groupingId: ALERTS_PAGE_ALERTS_TABLE_CONFIG_ID,
featureIds: observabilityAlertFeatureIds,
services: {
dataViews,
http,
notifications,
},
}),
showInspectButton: true,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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, useCallback } from 'react';
import { type AlertsGroupingProps, useAlertsGroupingState } from '@kbn/alerts-grouping';
import { useAlertDataView } from '@kbn/alerts-ui-shared';
import { useGetGroupSelectorStateless } from '@kbn/grouping/src/hooks/use_get_group_selector';
import { AlertConsumers } from '@kbn/rule-data-utils';
import { AlertsByGroupingAgg } from '../types';

interface GetPersistentControlsParams {
groupingId: string;
featureIds: AlertConsumers[];
maxGroupingLevels?: number;
services: Pick<
AlertsGroupingProps<AlertsByGroupingAgg>['services'],
'dataViews' | 'http' | 'notifications'
>;
}

export const getPersistentControlsHook =
({
groupingId,
featureIds,
maxGroupingLevels = 3,
services: { dataViews, http, notifications },
}: GetPersistentControlsParams) =>
() => {
const { grouping, updateGrouping } = useAlertsGroupingState(groupingId);

const onGroupChange = useCallback(
(selectedGroups: string[]) => {
updateGrouping({
activeGroups: grouping.activeGroups.filter((g) => g !== 'none').concat(selectedGroups),
});
},
[grouping, updateGrouping]
);

const { dataViews: alertDataViews } = useAlertDataView({
featureIds,
dataViewsService: dataViews,
http,
toasts: notifications.toasts,
});

const dataView = useMemo(() => alertDataViews?.[0], [alertDataViews]);

const groupSelector = useGetGroupSelectorStateless({
groupingId,
onGroupChange,
fields: dataView?.fields ?? [],
defaultGroupingOptions: grouping.options.filter(
(option) => !grouping.activeGroups.includes(option.key)
),
maxGroupingLevels,
});

return useMemo(() => {
return {
right: groupSelector,
};
}, [groupSelector]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 { SortOrder } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { ALERT_START, AlertConsumers } from '@kbn/rule-data-utils';
import {
AlertsTableConfigurationRegistry,
RenderCustomActionsRowArgs,
} from '@kbn/triggers-actions-ui-plugin/public/types';
import { casesFeatureId, observabilityFeatureId } from '../../../../common';
import { AlertActions } from '../../../pages/alerts/components/alert_actions';
import { useGetAlertFlyoutComponents } from '../../alerts_flyout/use_get_alert_flyout_components';
import type { ObservabilityRuleTypeRegistry } from '../../../rules/create_observability_rule_type_registry';
import type { ConfigSchema } from '../../../plugin';
import { getRenderCellValue } from '../common/render_cell_value';
import { getColumns } from '../common/get_columns';

export const getObservabilityTableConfiguration = (
observabilityRuleTypeRegistry: ObservabilityRuleTypeRegistry,
config: ConfigSchema
): AlertsTableConfigurationRegistry => {
const renderCustomActionsRow = (props: RenderCustomActionsRowArgs) => {
return (
<AlertActions
{...props}
config={config}
observabilityRuleTypeRegistry={observabilityRuleTypeRegistry}
/>
);
};
return {
id: AlertConsumers.OBSERVABILITY,
cases: { featureId: casesFeatureId, owner: [observabilityFeatureId] },
columns: getColumns({ showRuleName: true }),
getRenderCellValue,
sort: [
{
[ALERT_START]: {
order: 'desc' as SortOrder,
},
},
],
useActionsColumn: () => ({
renderCustomActionsRow,
}),
useInternalFlyout: () => {
const { header, body, footer } = useGetAlertFlyoutComponents(observabilityRuleTypeRegistry);
return { header, body, footer };
},
ruleTypeIds: observabilityRuleTypeRegistry.list(),
showInspectButton: true,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@
*/

import { AlertTableConfigRegistry } from '@kbn/triggers-actions-ui-plugin/public/application/alert_table_config_registry';
import type { DataViewsServicePublic } from '@kbn/data-views-plugin/public/types';
import { HttpSetup } from '@kbn/core-http-browser';
import { NotificationsStart } from '@kbn/core-notifications-browser';
import type { ConfigSchema } from '../../plugin';
import { ObservabilityRuleTypeRegistry } from '../..';
import { getAlertsPageTableConfiguration } from './alerts/get_alerts_page_table_configuration';
import { getRuleDetailsTableConfiguration } from './rule_details/get_rule_details_table_configuration';
import { getSloAlertsTableConfiguration } from './slo/get_slo_alerts_table_configuration';
import { getObservabilityTableConfiguration } from './observability/get_alerts_page_table_configuration';

export const registerAlertsTableConfiguration = (
alertTableConfigRegistry: AlertTableConfigRegistry,
observabilityRuleTypeRegistry: ObservabilityRuleTypeRegistry,
config: ConfigSchema
config: ConfigSchema,
dataViews: DataViewsServicePublic,
http: HttpSetup,
notifications: NotificationsStart
) => {
// Alert page
const alertsPageAlertsTableConfig = getAlertsPageTableConfiguration(
// Observability table
const observabilityAlertsTableConfig = getObservabilityTableConfiguration(
Copy link
Member Author

@maryam-saeidi maryam-saeidi Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I registered a new alerts table with an observability ID, which is used on other applications as well. (Basically, this already existed)

observabilityRuleTypeRegistry,
config
);
alertTableConfigRegistry.register(observabilityAlertsTableConfig);

// Alerts page
const alertsPageAlertsTableConfig = getAlertsPageTableConfiguration(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new table with grouping, only for the alerts page for now.

observabilityRuleTypeRegistry,
config,
dataViews,
http,
notifications
);
alertTableConfigRegistry.register(alertsPageAlertsTableConfig);

// Rule details page
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

export interface BucketItem {
key: string;
doc_count: number;
}

export interface AlertsByGroupingAgg extends Record<string, unknown> {
groupByFields: {
doc_count_error_upper_bound: number;
sum_other_doc_count: number;
buckets: BucketItem[];
};
ruleTags: {
doc_count_error_upper_bound: number;
sum_other_doc_count: number;
buckets: BucketItem[];
};
rulesCountAggregation?: {
value: number;
};
sourceCountAggregation?: {
value: number;
};
groupsCount: {
value: number;
};
unitsCount: {
value: number;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,53 @@ import React, { useState } from 'react';
import { EuiBadge, EuiPopover } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

export function Tags({ tags }: { tags: string[] }) {
export function Tags({
tags,
color,
size = 3,
oneLine = false,
}: {
tags: string[];
color?: string;
size?: number;
oneLine?: boolean;
Comment on lines +13 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is same kinda component exported from observability-shared, might be nice to use that

https:/elastic/kibana/blob/main/x-pack/plugins/observability_solution/observability_shared/public/index.ts#L38

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I will check it out!

}) {
const [isMoreTagsOpen, setIsMoreTagsOpen] = useState(false);
const onMoreTagsClick = () => setIsMoreTagsOpen((isPopoverOpen) => !isPopoverOpen);
const onMoreTagsClick = (e: any) => {
e.stopPropagation();
setIsMoreTagsOpen((isPopoverOpen) => !isPopoverOpen);
};
const closePopover = () => setIsMoreTagsOpen(false);
const moreTags = tags.length > 3 && (
const moreTags = tags.length > size && (
<EuiBadge
key="more"
onClick={onMoreTagsClick}
onClickAriaLabel={i18n.translate(
'xpack.observability.alertDetails.alertSummaryField.moreTags.ariaLabel',
{
defaultMessage: 'more tags badge',
}
)}
onClickAriaLabel={i18n.translate('xpack.observability.component.tags.moreTags.ariaLabel', {
defaultMessage: 'more tags badge',
})}
color={color}
>
<FormattedMessage
id="xpack.observability.alertDetails.alertSummaryField.moreTags"
id="xpack.observability.component.tags.moreTags"
defaultMessage="+{number} more"
values={{ number: tags.length - 3 }}
values={{ number: tags.length - size }}
/>
</EuiBadge>
);

return (
<>
{tags.slice(0, 3).map((tag) => (
<EuiBadge key={tag}>{tag}</EuiBadge>
{tags.slice(0, size).map((tag) => (
<EuiBadge key={tag} color={color}>
{tag}
</EuiBadge>
))}
<br />
{oneLine ? ' ' : <br />}
<EuiPopover button={moreTags} isOpen={isMoreTagsOpen} closePopover={closePopover}>
{tags.slice(3).map((tag) => (
<EuiBadge key={tag}>{tag}</EuiBadge>
{tags.slice(size).map((tag) => (
<EuiBadge key={tag} color={color}>
{tag}
</EuiBadge>
))}
</EuiPopover>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
export const DEFAULT_INTERVAL = '60s';
export const DEFAULT_DATE_FORMAT = 'YYYY-MM-DD HH:mm';

export const ALERTS_PAGE_ALERTS_TABLE_CONFIG_ID = `alerts-page-alerts-table`;
export const RULE_DETAILS_ALERTS_TABLE_CONFIG_ID = `rule-details-alerts-table`;
Loading