diff --git a/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.test.ts b/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.test.ts new file mode 100644 index 00000000000000..fa2b5192665ea7 --- /dev/null +++ b/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.test.ts @@ -0,0 +1,76 @@ +/* + * 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 { SLO_SUMMARY_DESTINATION_INDEX_PATTERN } from '../../../../../../common/constants'; +import { GroupSummary } from '@kbn/slo-schema'; +import { useGroupName } from './use_group_name'; + +describe('useGroupName', () => { + it('returns the group name for ungrouped', () => { + const groupName = useGroupName('ungrouped', 'irrelevant', {} as GroupSummary); + expect(groupName).toBe('irrelevant'); + }); + + it('returns the group name for slo tags', () => { + const groupName = useGroupName('slo.tags', 'some-tag', {} as GroupSummary); + expect(groupName).toBe('some-tag'); + }); + + it('returns the group name for status', () => { + const groupName = useGroupName('status', 'HEALTHY', {} as GroupSummary); + expect(groupName).toBe('healthy'); + }); + + it('returns the group name for slo instanceId', () => { + const summary = { + total: 2, + violated: 0, + healthy: 2, + degrading: 0, + noData: 0, + worst: { + sliValue: 1, + status: 'healthy', + slo: { + id: 'slo-id', + name: 'slo-name', + instanceId: 'domain.com', + groupings: { + host: { + name: 'domain.com', + }, + }, + }, + }, + } as GroupSummary; + const groupName = useGroupName('slo.instanceId', 'domain.com', summary); + expect(groupName).toBe('host.name: domain.com'); + }); + + it('returns the group name for slo indicator type', () => { + const groupName = useGroupName('slo.indicator.type', 'sli.kql.custom', {} as GroupSummary); + expect(groupName).toBe('Custom Query'); + }); + + it('returns the group name for local index', () => { + const groupName = useGroupName( + '_index', + SLO_SUMMARY_DESTINATION_INDEX_PATTERN, + {} as GroupSummary + ); + expect(groupName).toBe('Local Kibana'); + }); + + it('returns the group name for remote index', () => { + const groupName = useGroupName( + '_index', + `my-remote-cluster:${SLO_SUMMARY_DESTINATION_INDEX_PATTERN}`, + {} as GroupSummary + ); + expect(groupName).toBe('Remote Cluster: my-remote-cluster'); + }); +}); diff --git a/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.ts b/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.ts index 45e09337cde99a..ca6ddff88fb19f 100644 --- a/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.ts +++ b/x-pack/plugins/observability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.ts @@ -11,7 +11,7 @@ import { assertNever } from '@kbn/std'; import { SLI_OPTIONS } from '../../../../slo_edit/constants'; import { GroupByField } from '../../slo_list_group_by'; -export function useGroupName(groupBy: GroupByField, group: string, summary: GroupSummary) { +export function useGroupName(groupBy: GroupByField, group: string, summary?: GroupSummary) { const groupName = group.toLowerCase(); switch (groupBy) { @@ -20,7 +20,7 @@ export function useGroupName(groupBy: GroupByField, group: string, summary: Grou case 'status': return groupName; case 'slo.instanceId': - if (groupName === ALL_VALUE) { + if (groupName === ALL_VALUE || !summary) { return i18n.translate('xpack.slo.group.ungroupedInstanceId', { defaultMessage: 'Ungrouped', });