From 7a18994e28efb7fd83ab978e0050fb18b7dc4aaf Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Wed, 9 Nov 2022 13:46:32 +0100 Subject: [PATCH] feat: Add debug method for graphviz --- .../components/use-hierarchy-parents.tsx | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/configurator/components/use-hierarchy-parents.tsx b/app/configurator/components/use-hierarchy-parents.tsx index 07e94fce4..72fc222aa 100644 --- a/app/configurator/components/use-hierarchy-parents.tsx +++ b/app/configurator/components/use-hierarchy-parents.tsx @@ -61,4 +61,30 @@ const useHierarchyParents = ( }, [dimension.values, hierarchy]); }; +/** + * Can be used for debugging, pass a hierarchy, and copy the output + * to graphviz. + * + * @see https://dreampuf.github.io/GraphvizOnline/ + */ +export const hierarchyToGraphviz = ( + hierarchy: DimensionHierarchyQueryHierarchy +) => { + const lines = [] as string[]; + dfs(hierarchy, (node, { depth, parents }) => { + lines.push(`"${node.value}"[label="${node.label.replace(/"/g, "")}"]`); + if (parents.length > 0) { + const parent = parents[parents.length - 1]; + lines.push(`"${parent.value}" -> "${node.value}"`); + } + }); + return ` + digraph G { + rankdir=LR + + ${lines.join("\n")} + } + `; +}; + export default useHierarchyParents;