From a2f5c7e0e223e11317e03426393ecf73a70be809 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Fri, 24 Jan 2025 11:25:56 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=8A=20Streams:=20Centralize=20stream?= =?UTF-8?q?=20name=20helpers=20(#208148)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most helpers for dealing with stream names are already centralized in the `hierarchy.ts` helper, something non-trivial we did in multiple places is listing out all ancestors. This PR is centralizing that as well - the idea is that we never mess with `split('.')` and `join('.')` directly throughout the code base. If we decide later on to give more structure to stream names (e.g. special handling for DSNS names), it will be much easier to change it. (cherry picked from commit d75ffa914a2d5035d67f9dd4a77e87bdb38026c8) --- .../packages/kbn-streams-schema/src/helpers/hierarchy.ts | 8 ++++++++ .../streams/index_templates/generate_index_template.ts | 6 +++--- .../public/components/stream_detail_routing/index.tsx | 4 ++-- .../streams_app/public/components/streams_list/index.tsx | 5 +++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/x-pack/solutions/observability/packages/kbn-streams-schema/src/helpers/hierarchy.ts b/x-pack/solutions/observability/packages/kbn-streams-schema/src/helpers/hierarchy.ts index 3b47ec90507d1..2f00716b583a4 100644 --- a/x-pack/solutions/observability/packages/kbn-streams-schema/src/helpers/hierarchy.ts +++ b/x-pack/solutions/observability/packages/kbn-streams-schema/src/helpers/hierarchy.ts @@ -34,3 +34,11 @@ export function getAncestors(id: string) { const parts = id.split('.'); return parts.slice(0, parts.length - 1).map((_, index) => parts.slice(0, index + 1).join('.')); } + +export function getAncestorsAndSelf(id: string) { + return getAncestors(id).concat(id); +} + +export function getSegments(id: string) { + return id.split('.'); +} diff --git a/x-pack/solutions/observability/plugins/streams/server/lib/streams/index_templates/generate_index_template.ts b/x-pack/solutions/observability/plugins/streams/server/lib/streams/index_templates/generate_index_template.ts index ec4aa4464eeac..56cf48bcece4b 100644 --- a/x-pack/solutions/observability/plugins/streams/server/lib/streams/index_templates/generate_index_template.ts +++ b/x-pack/solutions/observability/plugins/streams/server/lib/streams/index_templates/generate_index_template.ts @@ -5,14 +5,14 @@ * 2.0. */ +import { getAncestorsAndSelf } from '@kbn/streams-schema'; import { ASSET_VERSION } from '../../../../common/constants'; import { getProcessingPipelineName } from '../ingest_pipelines/name'; import { getIndexTemplateName } from './name'; export function generateIndexTemplate(id: string, isServerless: boolean) { - const composedOf = id.split('.').reduce((acc, _, index, array) => { - const parent = array.slice(0, index + 1).join('.'); - return [...acc, `${parent}@stream.layer`]; + const composedOf = getAncestorsAndSelf(id).reduce((acc, ancestorId) => { + return [...acc, `${ancestorId}@stream.layer`]; }, [] as string[]); return { diff --git a/x-pack/solutions/observability/plugins/streams_app/public/components/stream_detail_routing/index.tsx b/x-pack/solutions/observability/plugins/streams_app/public/components/stream_detail_routing/index.tsx index d47e44b171287..9fe6442eb2d81 100644 --- a/x-pack/solutions/observability/plugins/streams_app/public/components/stream_detail_routing/index.tsx +++ b/x-pack/solutions/observability/plugins/streams_app/public/components/stream_detail_routing/index.tsx @@ -41,6 +41,7 @@ import { isDescendantOf, RoutingDefinition, IngestUpsertRequest, + getAncestorsAndSelf, } from '@kbn/streams-schema'; import { useUnsavedChangesPrompt } from '@kbn/unsaved-changes-prompt'; import { AbortableAsyncState } from '@kbn/observability-utils-browser/hooks/use_abortable_async'; @@ -818,8 +819,7 @@ function ChildStreamList({ function CurrentStreamEntry({ definition }: { definition: ReadStreamDefinition }) { const router = useStreamsAppRouter(); - const breadcrumbs: EuiBreadcrumb[] = definition.name.split('.').map((_part, pos, parts) => { - const parentId = parts.slice(0, pos + 1).join('.'); + const breadcrumbs: EuiBreadcrumb[] = getAncestorsAndSelf(definition.name).map((parentId) => { const isBreadcrumbsTail = parentId === definition.name; return { diff --git a/x-pack/solutions/observability/plugins/streams_app/public/components/streams_list/index.tsx b/x-pack/solutions/observability/plugins/streams_app/public/components/streams_list/index.tsx index 4edb5172cd816..dc35e6a47895c 100644 --- a/x-pack/solutions/observability/plugins/streams_app/public/components/streams_list/index.tsx +++ b/x-pack/solutions/observability/plugins/streams_app/public/components/streams_list/index.tsx @@ -22,6 +22,7 @@ import { euiThemeVars } from '@kbn/ui-theme'; import { css } from '@emotion/css'; import { StreamDefinition, + getSegments, isDescendantOf, isUnwiredStreamDefinition, isWiredStreamDefinition, @@ -41,12 +42,12 @@ export interface StreamTree { function asTrees(definitions: StreamDefinition[]) { const trees: StreamTree[] = []; const wiredDefinitions = definitions.filter((definition) => isWiredStreamDefinition(definition)); - wiredDefinitions.sort((a, b) => a.name.split('.').length - b.name.split('.').length); + wiredDefinitions.sort((a, b) => getSegments(a.name).length - getSegments(b.name).length); wiredDefinitions.forEach((definition) => { let currentTree = trees; let existingNode: StreamTree | undefined; - const segments = definition.name.split('.'); + const segments = getSegments(definition.name); // traverse the tree following the prefix of the current id. // once we reach the leaf, the current id is added as child - this works because the ids are sorted by depth while (