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.
feat(slo): group by instance id (elastic#186131)
- Loading branch information
1 parent
989f80e
commit 9def784
Showing
8 changed files
with
225 additions
and
27 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
76 changes: 76 additions & 0 deletions
76
...ility_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.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,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'); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
...ervability_solution/slo/public/pages/slos/components/grouped_slos/hooks/use_group_name.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,67 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { ALL_VALUE, GroupSummary } from '@kbn/slo-schema'; | ||
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) { | ||
const groupName = group.toLowerCase(); | ||
|
||
switch (groupBy) { | ||
case 'ungrouped': | ||
case 'slo.tags': | ||
case 'status': | ||
return groupName; | ||
case 'slo.instanceId': | ||
if (groupName === ALL_VALUE || !summary) { | ||
return i18n.translate('xpack.slo.group.ungroupedInstanceId', { | ||
defaultMessage: 'Ungrouped', | ||
}); | ||
} | ||
|
||
const groupNames = flattenObject(summary.worst.slo.groupings); | ||
return Object.entries(groupNames) | ||
.map(([key, value]) => `${key}: ${value}`) | ||
.join(', '); | ||
case 'slo.indicator.type': | ||
return SLI_OPTIONS.find((option) => option.value === group)?.text ?? groupName; | ||
case '_index': | ||
if (groupName.includes(':.')) { | ||
const [remoteClusterName] = groupName.split(':.'); | ||
return i18n.translate('xpack.slo.group.remoteCluster', { | ||
defaultMessage: 'Remote Cluster: {remoteClusterName}', | ||
values: { | ||
remoteClusterName, | ||
}, | ||
}); | ||
} | ||
|
||
return i18n.translate('xpack.slo.group.remoteCluster.localKibana', { | ||
defaultMessage: 'Local Kibana', | ||
}); | ||
|
||
default: | ||
assertNever(groupBy); | ||
} | ||
} | ||
|
||
function flattenObject(obj: Record<string, any>, parentKey = '', result: Record<string, any> = {}) { | ||
for (const key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
const newKey = parentKey ? `${parentKey}.${key}` : key; | ||
if (typeof obj[key] === 'object' && obj[key] !== null) { | ||
flattenObject(obj[key], newKey, result); | ||
} else { | ||
result[newKey] = obj[key]; | ||
} | ||
} | ||
} | ||
return result; | ||
} |
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