Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 25, 2024
1 parent 5d8ffbf commit a55fe17
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions x-pack/plugins/streams/server/routes/streams/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,15 @@ export interface StreamTree {
children: StreamTree[];
}

<<<<<<< HEAD
function asTrees(definitions: Array<{ id: string[] }>) {
const trees: ListStreamDefinition[] = [];
const ids = definitions.map((definition) => definition.id[0]);
function asTrees(definitions: Array<{ id: string }>) {
const trees: StreamTree[] = [];
const ids = definitions.map((definition) => definition.id);

ids.sort((a, b) => a.split('.').length - b.split('.').length);

ids.forEach((id) => {
let currentTree = trees;
let existingNode: ListStreamDefinition | undefined;
let existingNode: StreamTree | undefined;
// 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 ((existingNode = currentTree.find((node) => id.startsWith(node.id)))) {
Expand All @@ -70,34 +69,8 @@ function asTrees(definitions: Array<{ id: string[] }>) {
if (!existingNode) {
const newNode = { id, children: [] };
currentTree.push(newNode);
=======
function asTrees(definitions: StreamDefinition[]): StreamTree[] {
const nodes = new Map<string, StreamTree>();

const rootNodes = new Set<StreamTree>();

function getNode(id: string) {
let node = nodes.get(id);
if (!node) {
node = { id, children: [] };
nodes.set(id, node);
}
return node;
}

definitions.forEach((definition) => {
const path = definition.id.split('.');
const parentId = path.slice(0, path.length - 1).join('.');
const parentNode = parentId.length ? getNode(parentId) : undefined;
const selfNode = getNode(definition.id);

if (parentNode) {
parentNode.children.push(selfNode);
} else {
rootNodes.add(selfNode);
>>>>>>> upstream/main
}
});

return Array.from(rootNodes.values());
return trees;
}

0 comments on commit a55fe17

Please sign in to comment.