Skip to content

Commit

Permalink
[8.x] 🌊 Streams: Centralize stream name helpers (#208148) (#208162)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [🌊 Streams: Centralize stream name helpers
(#208148)](#208148)

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

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

<!--BACKPORT [{"author":{"name":"Joe
Reuter","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-24T10:25:56Z","message":"🌊
Streams: Centralize stream name helpers (#208148)\n\nMost helpers for
dealing with stream names are already centralized in\nthe `hierarchy.ts`
helper, something non-trivial we did in multiple\nplaces is listing out
all ancestors.\n\nThis PR is centralizing that as well - the idea is
that we never mess\nwith `split('.')` and `join('.')` directly
throughout the code base. If\nwe decide later on to give more structure
to stream names (e.g. special\nhandling for DSNS names), it will be much
easier to change
it.","sha":"d75ffa914a2d5035d67f9dd4a77e87bdb38026c8","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:version","v8.18.0","Feature:Streams"],"title":"🌊
Streams: Centralize stream name
helpers","number":208148,"url":"https://github.com/elastic/kibana/pull/208148","mergeCommit":{"message":"🌊
Streams: Centralize stream name helpers (#208148)\n\nMost helpers for
dealing with stream names are already centralized in\nthe `hierarchy.ts`
helper, something non-trivial we did in multiple\nplaces is listing out
all ancestors.\n\nThis PR is centralizing that as well - the idea is
that we never mess\nwith `split('.')` and `join('.')` directly
throughout the code base. If\nwe decide later on to give more structure
to stream names (e.g. special\nhandling for DSNS names), it will be much
easier to change
it.","sha":"d75ffa914a2d5035d67f9dd4a77e87bdb38026c8"}},"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/208148","number":208148,"mergeCommit":{"message":"🌊
Streams: Centralize stream name helpers (#208148)\n\nMost helpers for
dealing with stream names are already centralized in\nthe `hierarchy.ts`
helper, something non-trivial we did in multiple\nplaces is listing out
all ancestors.\n\nThis PR is centralizing that as well - the idea is
that we never mess\nwith `split('.')` and `join('.')` directly
throughout the code base. If\nwe decide later on to give more structure
to stream names (e.g. special\nhandling for DSNS names), it will be much
easier to change
it.","sha":"d75ffa914a2d5035d67f9dd4a77e87bdb38026c8"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Joe Reuter <[email protected]>
  • Loading branch information
kibanamachine and flash1293 authored Jan 24, 2025
1 parent d27ae58 commit 8ef228a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('.');
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { euiThemeVars } from '@kbn/ui-theme';
import { css } from '@emotion/css';
import {
StreamDefinition,
getSegments,
isDescendantOf,
isUnwiredStreamDefinition,
isWiredStreamDefinition,
Expand All @@ -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 (
Expand Down

0 comments on commit 8ef228a

Please sign in to comment.