Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-modeling): smooth lineage graph loading (#109085)

Jovan Sakovic33328b00da69
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index c510f1d82f5a..a187dd2d09f7 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -6367,9 +6367,9 @@ snapshots:
products-data-catalog-metric-lineage--being-prepared--light:
hash: v1.k794b7964.1d9442bec79157ff586c1e1baefa06a6af93faf873f16f0b1d145aad2f94b330.AkvoCIqxZnaGvJ-bwA5Wxrs6uAmv4WJOugLfPcxxTvo
products-data-catalog-metric-lineage--graph--dark:
- hash: v1.k794b7964.28436b8e236653de8ff4f9ed90b223616a0f74c5fa0768eaa89cb8d2efabbc4c.rU1yb72_zYisx__hS1s9oPU1bvoHE5VRsK_LlGubsy8
+ hash: v1.k794b7964.7e5608dc6debef4164f76bc1f14c81a5697d2e79ab8cc52424a0cadc8c37ba13.TPqOoO2pOBqXnQiy28fq_BaqdAYXnkfWK5RzRTx9vm0
products-data-catalog-metric-lineage--graph--light:
- hash: v1.k794b7964.fd9cb0d75def206101cdbe40b202ce816664338aadc3cedd55d8e0c04988fbb2.pZoiwDKsy0mK_UUcYDxBU4XXhG4zBy30CPbNAREX-0o
+ hash: v1.k794b7964.3c5961c5fdc49fbdf0d005c6430b96724d2b5e4795b90794759a79e398aeec44.FL9fwEsPu_LOsSQG0EnlCubZge2SARjABAy5G8jySdw
products-data-catalog-metric-lineage--lineage-issue--dark:
hash: v1.k794b7964.860d27733af3926c8bb4ebcbc5a687b149ab482f097e67af410450ecc90d21d9.n7Othr89yht04LnSFImmVTHCL4HFXOOY8uZMv8Tw8yw
products-data-catalog-metric-lineage--lineage-issue--light:
@@ -6379,9 +6379,9 @@ snapshots:
products-data-catalog-metric-lineage--load-failed--light:
hash: v1.k794b7964.1b9c3b1720e0444a2f33b4ba6dc972f6a56a114c94be10cc77fa455b8441fbca.LUvQPwM50s6A-ZDh32zDhqhWWm5KAPgEFK2qa8ffmWs
products-data-catalog-metric-lineage--loading--dark:
- hash: v1.k794b7964.d2e01904e4c1ba9e222e434c70d459e1d4354b7cf5ec05f8e54aa17844b91fa3.ldvpgC3VPt5yjE4qd-lwTNdm3tfAhpCoWAGhMun3YWk
+ hash: v1.k794b7964.25be3801d8b9f8224913061df4c656e2184938c1e680b14dc46f200695900471.me_axmlUq8G5xEMqZJEtxbiJoD5RglXi8GTEHrQ7IXc
products-data-catalog-metric-lineage--loading--light:
- hash: v1.k794b7964.fa63fb4b9b33a6c2cc7a7c22e3968c9f015b0e63e82996ea559b87fdea7797a0.RpSFIW0xf3AwRBHwe-Auvg-qR7JQHSMrhro-qOUhAzo
+ hash: v1.k794b7964.825d6b19462e0e4d601f61cdf33f30a4ca7703fe206edb10148007144cdea08f.luaYVjv_mVwBssJe6lMkc8pNj4-YfHX-EXnd0o04E_4
products-data-catalog-metric-lineage--no-definition--dark:
hash: v1.k794b7964.0a67016ff06498b8f16e83dcbd01d03ced850f02935d9cfc555c5d321a6dc21e.SYouGVS4FM4aPQFE51xlJG_1LPN79L4zsp_xwt4mRJI
products-data-catalog-metric-lineage--no-definition--light:
@@ -6391,33 +6391,33 @@ snapshots:
products-data-catalog-metric-lineage--no-warehouse-access--light:
hash: v1.k794b7964.d24fc4898fd69d316912802e328f3a6e2e94cee4cf106d8893597da97dbf73af.UOfef0QXupztuN6lJ1r5vYH5FiYFRHx_eqHRlY-34CQ
products-data-modeling-lineage-graph--canvas--dark:
- hash: v1.k794b7964.c7f0f98e0d7ea29f9bc5abcdd62b882a202d35e5b62af26f363b9c1a1323a99f.EaAJ28EK8wdDcmRGmBf0dkfiuQROQE2QUkFShyE8JgU
+ hash: v1.k794b7964.6ab4d2ea52f93ad1ec3e1184d5d10b323cf922929a4a0d93e0e8b28422c90a48.WxpMQC4ZwLApFccXvRTpN7lNJbV2XMyKZLFizY4gems
products-data-modeling-lineage-graph--canvas--light:
- hash: v1.k794b7964.8123e85693b4e4f1675fb7fba771dbbab268da7247ea3481872ec04653f19dbb.32Hmv5ou0iPAsy1NQubyB_RYDgYWJQWPgL1WFGNn_tQ
+ hash: v1.k794b7964.97977a4c30ab6a8fc5afdecb1ed6e9892d7fab579580cd250a0d3362fbbf6e87.R8w2a2xrAV5cL984OpFZzMY3zelheXpdTkoS2FPKeSs
products-data-modeling-lineage-graph--dark-mode--dark:
- hash: v1.k794b7964.7d91aad4b98dbff40bd8b3a9c5d8f600f69b892dd61b204d3563f94ca72ed4ea.D1YqS2betpNrFP_VLnnNHStCNy03Wi14pmGF3TP4ZoU
+ hash: v1.k794b7964.dbaa1992239d54c653a2132b63857ca77420a178b662c000fb93e7fded648a19.XVks3zJgtqjlm24-mHsgMx3boV8KNZNtXN5dcph4LWw
products-data-modeling-lineage-graph--dark-mode--light:
- hash: v1.k794b7964.62fda4d26e38e85486f11884c2497da55df2180934b54b8e3939226406d5569a.SBCXKXcOB-DwMt8gabA5vzB3qcC4EpPyu4YVYvZwLYw
+ hash: v1.k794b7964.bc8a6b5f4e177299e6b7ca346135a43ecbd3048405692bb1e75a8747f695c12d.Cg73TPpnrS5uZDxIKIWiV4BhszAzKFH_dSHExM_kkJM
products-data-modeling-lineage-graph--empty-state--dark:
hash: v1.k794b7964.9870c50d45ee1583472d6a98917a350892a8c2d8e29c20d17138eb68b8b52d60.yi6jAXaHCs0dh9kUIVgf6aWiVa8Btlb2b9xUQw_q4VE
products-data-modeling-lineage-graph--empty-state--light:
hash: v1.k794b7964.5c6a3999ee44ecbce5db4d57dba01765e599c0c1874eab79eb80e71956d85fd7.AHbnj5yPLIkfMm6oHanSnhhQVEoBO0o5BcRYmK9DSgY
products-data-modeling-lineage-graph--full--dark:
- hash: v1.k794b7964.112213e8e69bef1f6e8aab90994d6e0be0c7fcb922965b6834c1081d3d3805c0.CDKiRu_VCNR1dBVF3-l8EfsSBvtbYuQPHWFYHyXbIoI
+ hash: v1.k794b7964.f643b2ff2c0413c931379a0f9191d90463f82abcd2009884034baa570b94ffd3.YRACGLIM0BQxmoQj7iIWmPWBdf1_gYVGqf6YfhYp0h0
products-data-modeling-lineage-graph--full--light:
- hash: v1.k794b7964.3d2a0a0a6ff2a27f3f8d682857757695b61970b3bfdc3f50e7508c86257aa4ad.S_I2CjkAMi3HiNyo014ZRrZHZbcDaVTQwF4718lZNws
+ hash: v1.k794b7964.e7ba7f94326fd36a1000cc9e226bf688e1f0273aee518eb6aed0d8acba822fa9.QANk7QxBUcwb091vmyWrc_h5hbS3BF2pZ1UYgXbgMIM
products-data-modeling-lineage-graph--loading--dark:
- hash: v1.k794b7964.37e743bd1659c207ae6da2e71d236547305a9956ac742799d8de1016d6136129.v6YYaaPBVxDTr4p6vGmQxUR35EzxaX_KryIe6cPHeHI
+ hash: v1.k794b7964.2f208252322304a23495448d4407dfde8ea39dbeaf917411971d8efc9b06818e.ptAA0uSlzz4xedODyiUojLe-UEUJExEUBj0ENdl5HHw
products-data-modeling-lineage-graph--loading--light:
- hash: v1.k794b7964.799b736f8dfed7d406019308f305b3ce63ff6da574ebdad87d991d3c7bdb654d.H1HIvMH-fmzpbbYKKK3eJ1QhnHzPFV1Rifb2eIqRKUA
+ hash: v1.k794b7964.8692f1b78e81f0be5868de8150588341d0e542de50515816137b3670c63e61e7.pa6XmFtddS8g-ttQePDIy5T0S3VLcutNFZZD1wA16Tk
products-data-modeling-lineage-graph--loading-dark-mode--dark:
- hash: v1.k794b7964.b4b4e7958ad0e3a27afee43b9617d7885fd4e02bc2edff68bc05598297159ef2.0AecY87D9zDsi6MBje5iYOSAbVAGSIkcJ8QZRH6V21Y
+ hash: v1.k794b7964.6cf877fd61e23f3c5b70fdb3961c6a3de97bd8f2e4a302f89f9e758b87f27cee.WJWGv2yGpQf0cdsAHUxQQ8lGUqp7PoMfi37c2Pwqjc0
products-data-modeling-lineage-graph--loading-dark-mode--light:
- hash: v1.k794b7964.9db1ad5713b6cdd0b0410f1c7859bee92d885c48f3400611673b50d0cfbdbe43.kR2brmUL-KkueOC8D8pqWJ9bzuTAVJrhRTxuC4fncJ4
+ hash: v1.k794b7964.77fcb1dee9782e28f7f32f7096a6f9e569b8e8638889e31a66a5a427a0748dcb.KoJDhdPUtBayIhnapYqIxfj4QfakouPi2bCnoEh3JxY
products-data-modeling-lineage-graph--loading-focused--dark:
- hash: v1.k794b7964.5c5253d7f45ff64b2a7de267d7f216b5dfbc6cebf566c64bb2804a15d49f6877.jipTXPSFWSQEENdldM5M1jViiEaBSPmHfbpMf8M_nx8
+ hash: v1.k794b7964.0ca53d995571a069c494fdd50cd135ba8abd657145444fbd7b2f4c97b265a5c2.N8jv7IahZK-BIRi6igR-33Gr2g3TDIZc2wBTBKzSDzk
products-data-modeling-lineage-graph--loading-focused--light:
- hash: v1.k794b7964.fe8f51a186a08326e770d97862c7f3edf4fa0d3bc477a9082be1db3bf6a36d24.1sIg57c84D-amSb_6VRD1ySV2WN-HbaeEToOy-e9YuM
+ hash: v1.k794b7964.ad5f3bb5a4c9a5193cf69e525d90f14a6593bcd863437ae10d7148c798720278.N7js9PPoiXJKVTgurWgbO00-kxtsO-gsKxcFaaZAhPo
products-data-modeling-lineage-graph--search-focus--dark:
hash: v1.k794b7964.50fc53d09a95dbfa156c1bb0234cb4ae94f1edad3c531971a6d29fc6e644792b.CnYgYXb_RjjTDPN13751BGVZ03jgtmKYiwd6IWXzvoE
products-data-modeling-lineage-graph--search-focus--light:
diff --git a/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx b/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
index da59746109ec..12e82ede2d41 100644
--- a/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
@@ -87,6 +87,31 @@ async function expectNodeCentered(canvasElement: HTMLElement, nodeId: string, me
)
}
+async function expectNodesCentered(canvasElement: HTMLElement, message: string): Promise<void> {
+ await waitFor(
+ () => {
+ const graph = canvasElement.querySelector<HTMLElement>('.react-flow')
+ const nodes = [...(graph?.querySelectorAll<HTMLElement>('.react-flow__node') ?? [])]
+ if (!graph || nodes.length < 2) {
+ throw new Error(message)
+ }
+ const nodeBounds = nodes.map((node) => node.getBoundingClientRect())
+ const left = Math.min(...nodeBounds.map((bounds) => bounds.left))
+ const right = Math.max(...nodeBounds.map((bounds) => bounds.right))
+ const top = Math.min(...nodeBounds.map((bounds) => bounds.top))
+ const bottom = Math.max(...nodeBounds.map((bounds) => bounds.bottom))
+ const graphBounds = graph.getBoundingClientRect()
+ if (
+ Math.abs((left + right) / 2 - (graphBounds.left + graphBounds.right) / 2) > 5 ||
+ Math.abs((top + bottom) / 2 - (graphBounds.top + graphBounds.bottom) / 2) > 5
+ ) {
+ throw new Error(message)
+ }
+ },
+ { timeout: VIEWPORT_SETTLE_MS }
+ )
+}
+
type Story = StoryObj<typeof LineageGraph>
const meta: Meta<typeof LineageGraph> = {
title: 'Products/Data modeling/Lineage graph',
@@ -139,6 +164,9 @@ export const Loading: Story = {
panels={<span>Graph tools</span>}
/>
),
+ play: async ({ canvasElement }) => {
+ await expectNodesCentered(canvasElement, 'The loading skeleton must be centered in the lineage viewport')
+ },
}
export const LoadingFocused: Story = {
@@ -155,6 +183,12 @@ export const LoadingFocused: Story = {
panels={<span>Graph tools</span>}
/>
),
+ play: async ({ canvasElement }) => {
+ await expectNodesCentered(
+ canvasElement,
+ 'The focused loading skeleton must be centered in the lineage viewport'
+ )
+ },
}
export const LoadingDarkMode: Story = {
diff --git a/products/data_modeling/frontend/lineage/LineageGraph.tsx b/products/data_modeling/frontend/lineage/LineageGraph.tsx
index 9080f135f691..c1a67a06c754 100644
--- a/products/data_modeling/frontend/lineage/LineageGraph.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraph.tsx
@@ -13,7 +13,7 @@ import {
useReactFlow,
} from '@xyflow/react'
import { useValues } from 'kea'
-import { ReactNode, useEffect } from 'react'
+import { ReactNode, useEffect, useMemo, useRef } from 'react'
import { IconArchive } from '@posthog/icons'
@@ -24,9 +24,13 @@ import { ElkDirection } from './autolayout'
import { LineageGraphLoading } from './LineageGraphLoading'
import { lineageGraphLogic } from './lineageGraphLogic'
import { LINEAGE_NODE_TYPES, LineageNodeCallbacks, LineageNodeState, LineageVariant } from './LineageNode'
+import { useNodesMeasured } from './useNodesMeasured'
export type { LineageVariant, LineageNodeState, LineageNodeCallbacks } from './LineageNode'
+const EMPTY_NODES: DataModelingNode[] = []
+const EMPTY_EDGES: DataModelingEdge[] = []
+
export interface LineageGraphProps {
nodes: DataModelingNode[]
edges: DataModelingEdge[]
@@ -59,19 +63,66 @@ export interface LineageGraphProps {
function LineageGraphContent(props: LineageGraphProps): JSX.Element {
const { fitView, viewportInitialized } = useReactFlow()
+ const nodesMeasured = useNodesMeasured()
const { isDarkModeOn } = useValues(themeLogic)
const { currentNodeId, nodeState, nodeCallbacks, onNodeClick, focusNodeIds, searchFocusRequest } = props
const { layout } = useValues(
lineageGraphLogic({
- nodes: props.nodes,
- edges: props.edges,
+ nodes: props.loading ? EMPTY_NODES : props.nodes,
+ edges: props.loading ? EMPTY_EDGES : props.edges,
variant: props.variant ?? 'full',
direction: props.direction ?? 'RIGHT',
})
)
+ const fittedLayout = useRef<typeof layout>(null)
+
+ // Decorating on every render would hand react-flow new node objects, which drops the sizes it
+ // measured — so the fit below would keep waiting and the edges would keep being redrawn.
+ const decoratedNodes = useMemo(
+ () =>
+ layout?.nodes.map((rfNode) => {
+ const node = rfNode.data.node as DataModelingNode
+ return {
+ ...rfNode,
+ data: {
+ ...rfNode.data,
+ state: { isCurrent: node.id === currentNodeId, ...nodeState?.(node) },
+ callbacks: nodeCallbacks?.(node) ?? {
+ onClick: onNodeClick ? () => onNodeClick(node) : undefined,
+ },
+ },
+ }
+ }) ?? [],
+ [currentNodeId, layout, nodeCallbacks, nodeState, onNodeClick]
+ )
+
+ useEffect(() => {
+ if (!viewportInitialized || !nodesMeasured || !layout || props.loading || fittedLayout.current === layout) {
+ return
+ }
+ fittedLayout.current = layout
+ if ((focusNodeIds !== null && focusNodeIds !== undefined) || searchFocusRequest) {
+ return
+ }
+ void fitView({
+ ...props.fitViewOptions,
+ nodes: layout.nodes,
+ padding: props.fitViewOptions?.padding ?? 0.2,
+ duration: 400,
+ })
+ }, [
+ fitView,
+ focusNodeIds,
+ layout,
+ nodesMeasured,
+ props.fitViewOptions,
+ props.loading,
+ searchFocusRequest,
+ viewportInitialized,
+ ])
useEffect(() => {
- if (!viewportInitialized || !focusNodeIds || !layout) {
+ if (!viewportInitialized || !focusNodeIds || !layout || props.loading) {
return
}
// An empty focusNodeIds means the search was cleared, so fit the whole graph again rather
@@ -85,19 +136,19 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
maxZoom: focusNodeIds.size > 0 ? 2 : undefined,
})
}
- }, [fitView, viewportInitialized, focusNodeIds, layout])
+ }, [fitView, viewportInitialized, focusNodeIds, layout, props.loading])
useEffect(() => {
- if (!viewportInitialized || !searchFocusRequest || !layout) {
+ if (!viewportInitialized || !searchFocusRequest || !layout || props.loading) {
return
}
const node = layout.nodes.find((layoutNode) => layoutNode.id === searchFocusRequest.nodeId)
if (node) {
void fitView({ nodes: [node], padding: 0.2, duration: 400, maxZoom: 2 })
}
- }, [fitView, viewportInitialized, searchFocusRequest, layout])
+ }, [fitView, viewportInitialized, searchFocusRequest, layout, props.loading])
- if (!layout) {
+ if (props.loading || !layout) {
const center = props.loadingCenter ?? props.nodes.find((node) => node.id === currentNodeId)
return (
<LineageGraphLoading
@@ -109,21 +160,6 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
)
}
- // Cheap per-render pass: current-node highlight, state, and callbacks change without relayout
- const decoratedNodes = layout.nodes.map((rfNode) => {
- const node = rfNode.data.node as DataModelingNode
- return {
- ...rfNode,
- data: {
- ...rfNode.data,
- state: { isCurrent: node.id === currentNodeId, ...nodeState?.(node) },
- callbacks: nodeCallbacks?.(node) ?? {
- onClick: onNodeClick ? () => onNodeClick(node) : undefined,
- },
- },
- }
- })
-
return (
<ReactFlow
colorMode={isDarkModeOn ? 'dark' : 'light'}
@@ -135,8 +171,6 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
// The card inside each node is the focus target and carries the key handler. A focusable
// wrapper would add a second tab stop per node that only selects and never navigates.
nodesFocusable={false}
- fitView
- fitViewOptions={props.fitViewOptions}
minZoom={0.1}
maxZoom={2}
zoomOnScroll={props.interactive ?? false}
@@ -162,19 +196,7 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
}
export function LineageGraph(props: LineageGraphProps): JSX.Element {
- if (props.loading) {
- return (
- <ReactFlowProvider>
- <LineageGraphLoading
- center={props.loadingCenter}
- direction={props.direction ?? 'RIGHT'}
- fitViewOptions={props.fitViewOptions}
- variant={props.variant ?? 'full'}
- />
- </ReactFlowProvider>
- )
- }
- if (props.nodes.length === 0) {
+ if (!props.loading && props.nodes.length === 0) {
return (
<div className="flex flex-col w-full h-full items-center justify-center p-4">
<IconArchive className="text-5xl mb-2 text-tertiary" />
diff --git a/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx b/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
index 8ef8d2249a57..0d2e697c1f66 100644
--- a/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
@@ -8,6 +8,7 @@ import { DataModelingEdge, DataModelingNode } from '~/types'
import { ElkDirection } from './autolayout'
import { initialLineageGraphLayout, lineageGraphLogic } from './lineageGraphLogic'
import { LINEAGE_NODE_TYPES, LineageVariant } from './LineageNode'
+import { useNodesMeasured } from './useNodesMeasured'
export interface LineageGraphLoadingProps {
center?: Pick<DataModelingNode, 'name' | 'type'>
@@ -76,6 +77,7 @@ export function LineageGraphLoading({
variant,
}: LineageGraphLoadingProps): JSX.Element {
const { fitView, viewportInitialized } = useReactFlow()
+ const nodesMeasured = useNodesMeasured()
const { isDarkModeOn } = useValues(themeLogic)
const reactId = useId()
const idPrefix = useMemo(() => `lineage-loading-${reactId.replaceAll(':', '')}`, [reactId])
@@ -102,7 +104,7 @@ export function LineageGraphLoading({
)
useEffect(() => {
- if (viewportInitialized) {
+ if (viewportInitialized && nodesMeasured) {
void fitView({
...loadingFitViewOptions,
nodes: displayedLayout.nodes,
@@ -110,22 +112,28 @@ export function LineageGraphLoading({
duration: 0,
})
}
- }, [displayedLayout, fitView, loadingFitViewOptions, viewportInitialized])
+ }, [displayedLayout, fitView, loadingFitViewOptions, nodesMeasured, viewportInitialized])
- const nodes = displayedLayout.nodes.map((node) => ({
- ...node,
- data: {
- ...node.data,
- state: {
- loading: node.id === graph.centerNodeId && center ? ('focus' as const) : ('placeholder' as const),
- },
- callbacks: {},
- },
- }))
- const edges = displayedLayout.edges.map((edge) => ({
- ...edge,
- className: 'opacity-50',
- }))
+ // Rebuilding these arrays on every render hands react-flow new node objects, which drops the
+ // sizes it measured and makes it lay the skeleton out again.
+ const nodes = useMemo(
+ () =>
+ displayedLayout.nodes.map((node) => ({
+ ...node,
+ data: {
+ ...node.data,
+ state: {
+ loading: node.id === graph.centerNodeId ? ('focus' as const) : ('placeholder' as const),
+ },
+ callbacks: {},
+ },
+ })),
+ [displayedLayout.nodes, graph.centerNodeId]
+ )
+ const edges = useMemo(
+ () => displayedLayout.edges.map((edge) => ({ ...edge, className: 'opacity-50' })),
+ [displayedLayout.edges]
+ )
return (
<>
@@ -142,8 +150,6 @@ export function LineageGraphLoading({
nodesConnectable={false}
nodesFocusable={false}
elementsSelectable={false}
- fitView
- fitViewOptions={loadingFitViewOptions}
minZoom={0.1}
maxZoom={1}
zoomOnScroll={false}
diff --git a/products/data_modeling/frontend/lineage/lineageSearch.test.ts b/products/data_modeling/frontend/lineage/lineageSearch.test.ts
index a5a6cd53fdd3..74dc1b4b6900 100644
--- a/products/data_modeling/frontend/lineage/lineageSearch.test.ts
+++ b/products/data_modeling/frontend/lineage/lineageSearch.test.ts
@@ -46,6 +46,23 @@ describe('lineageSearch', () => {
expect(traverseLineage('orders', maps, 'both').has('sessions')).toBe(false)
})
+ it('walks a broad cyclic graph without visiting nodes twice', () => {
+ const branches = Array.from({ length: 100 }, (_, index) => node(`branch-${index}`, `branch-${index}`))
+ const nodes = [node('root', 'root'), ...branches, node('terminal', 'terminal')]
+ const edges = [
+ ...branches.flatMap((branch) => [edge('root', branch.id), edge(branch.id, 'terminal')]),
+ edge('terminal', 'root'),
+ ]
+
+ const reached = traverseLineage('root', buildAdjacencyMaps(edges), 'downstream')
+ expect(reached).toEqual(new Set(nodes.map(({ id }) => id)))
+
+ const ordered = orderedNodesForLineageSearch(nodes, edges, parseLineageSearch('root+'))
+ expect(ordered).toHaveLength(nodes.length)
+ expect(ordered?.[0].id).toEqual('root')
+ expect(ordered?.at(-1)?.id).toEqual('terminal')
+ })
+
it('anchors on the exact name over a longer one that contains it', () => {
const nodes = [node('1', 'orders_daily'), node('2', 'orders'), node('3', 'stripe_orders_raw')]
expect(matchNodesByName(nodes, 'orders')[0].name).toEqual('orders')
diff --git a/products/data_modeling/frontend/lineage/lineageSearch.ts b/products/data_modeling/frontend/lineage/lineageSearch.ts
index 7e98b7c87b89..b139ee350ede 100644
--- a/products/data_modeling/frontend/lineage/lineageSearch.ts
+++ b/products/data_modeling/frontend/lineage/lineageSearch.ts
@@ -40,8 +40,19 @@ export function buildAdjacencyMaps(edges: DataModelingEdge[]): AdjacencyMaps {
const downstream = new Map<string, string[]>()
for (const edge of edges) {
- upstream.set(edge.target_id, [...(upstream.get(edge.target_id) ?? []), edge.source_id])
- downstream.set(edge.source_id, [...(downstream.get(edge.source_id) ?? []), edge.target_id])
+ const upstreamNodes = upstream.get(edge.target_id)
+ if (upstreamNodes) {
+ upstreamNodes.push(edge.source_id)
+ } else {
+ upstream.set(edge.target_id, [edge.source_id])
+ }
+
+ const downstreamNodes = downstream.get(edge.source_id)
+ if (downstreamNodes) {
+ downstreamNodes.push(edge.target_id)
+ } else {
+ downstream.set(edge.source_id, [edge.target_id])
+ }
}
return { upstream, downstream }
@@ -49,8 +60,8 @@ export function buildAdjacencyMaps(edges: DataModelingEdge[]): AdjacencyMaps {
function walk(startId: string, adjacency: Map<string, string[]>, reached: Set<string>): void {
const queue = [startId]
- while (queue.length > 0) {
- const current = queue.shift() as string
+ for (let cursor = 0; cursor < queue.length; cursor++) {
+ const current = queue[cursor]
for (const neighbor of adjacency.get(current) ?? []) {
if (!reached.has(neighbor)) {
reached.add(neighbor)
@@ -62,8 +73,8 @@ function walk(startId: string, adjacency: Map<string, string[]>, reached: Set<st
function walkDistances(startId: string, adjacency: Map<string, string[]>, distances: Map<string, number>): void {
const queue = [startId]
- while (queue.length > 0) {
- const current = queue.shift() as string
+ for (let cursor = 0; cursor < queue.length; cursor++) {
+ const current = queue[cursor]
const distance = distances.get(current) ?? 0
for (const neighbor of adjacency.get(current) ?? []) {
const nextDistance = distance + 1
diff --git a/products/data_modeling/frontend/lineage/useNodesMeasured.ts b/products/data_modeling/frontend/lineage/useNodesMeasured.ts
new file mode 100644
index 000000000000..7246fb442a9c
--- /dev/null
+++ b/products/data_modeling/frontend/lineage/useNodesMeasured.ts
@@ -0,0 +1,16 @@
+import { useStore } from '@xyflow/react'
+
+/**
+ * Whether react-flow has measured every node it currently holds.
+ *
+ * react-flow resolves a queued `fitView` as soon as the first node reports its size, which centers
+ * the viewport on that one node and never corrects itself. Gate a fit on this hook, and keep it in
+ * the effect dependencies so the fit runs again once the whole graph is measured.
+ */
+export function useNodesMeasured(): boolean {
+ return useStore(
+ (state) =>
+ state.nodeLookup.size > 0 &&
+ [...state.nodeLookup.values()].every((node) => node.measured.width && node.measured.height)
+ )
+}
diff --git a/products/data_modeling/frontend/nodeDetail/NodeDetailLineage.tsx b/products/data_modeling/frontend/nodeDetail/NodeDetailLineage.tsx
index 8aae85aa389e..94709b28f348 100644
--- a/products/data_modeling/frontend/nodeDetail/NodeDetailLineage.tsx
+++ b/products/data_modeling/frontend/nodeDetail/NodeDetailLineage.tsx
@@ -47,21 +47,7 @@ export function NodeDetailLineage({ id }: { id: string }): JSX.Element {
router.actions.push(lineageNodeUrl(node, 'lineage'))
}
- if (lineageGraphLoading) {
- return (
- <div className="flex-1 min-h-[400px] max-h-[70vh] w-full border rounded bg-bg-light overflow-hidden">
- <LineageGraph
- nodes={[]}
- edges={[]}
- loading
- loadingCenter={node ? { name: node.name, type: node.type } : undefined}
- variant="full"
- />
- </div>
- )
- }
-
- if (lineageGraphError) {
+ if (!lineageGraphLoading && lineageGraphError) {
return (
<LemonBanner type="error" action={{ children: 'Retry', onClick: loadLineageGraph }}>
Couldn't load lineage.
@@ -69,7 +55,7 @@ export function NodeDetailLineage({ id }: { id: string }): JSX.Element {
)
}
- if (nodes.length <= 1 && !nodes[0]?.lineage_issue) {
+ if (!lineageGraphLoading && nodes.length <= 1 && !nodes[0]?.lineage_issue) {
return (
<div className="flex min-h-64 flex-col items-center justify-center gap-3 rounded border bg-bg-light p-6 text-center">
<div className="max-w-120">
@@ -88,11 +74,13 @@ export function NodeDetailLineage({ id }: { id: string }): JSX.Element {
return (
<>
- <div className="flex-1 min-h-[400px] max-h-[70vh] w-full border rounded bg-bg-light">
+ <div className="flex-1 min-h-[400px] max-h-[70vh] w-full border rounded bg-bg-light overflow-hidden">
<LineageGraph
nodes={nodes}
edges={lineageGraph?.edges ?? []}
currentNodeId={lineageGraph?.currentNodeId}
+ loading={lineageGraphLoading}
+ loadingCenter={lineageGraphLoading && node ? { name: node.name, type: node.type } : undefined}
variant="full"
interactive
showControls