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.

perf(data-modeling): make the lineage graph load on large projects (#124837)

Jovan Sakovicdfc685d5737b
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index dfaf8562f8b7..462969506c66 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -7321,13 +7321,13 @@ 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.51013454b87a130b110bc70f120eaf5a33ffe6df8417526e1335f199a3eeb7e2.mOH07Ta25FyqcfK_wO-AmP5pf_nfmckzujvkgf4KzOI
+ hash: v1.k794b7964.b74a362a0713b4d1692f86312dab09f01e98354adede1a4fca1a74a6245384d6.xpWo73Tv3ESz6-t_P9UCF-hTrK51stKqQJ2qwPGSA8Y
products-data-catalog-metric-lineage--graph--light:
- hash: v1.k794b7964.ef1ca2740a03cf952ae4f93882a20d78c643176aad74904e359ae3f608f7a4f9.pXp--BDgcGDpx67Dg8FfZ-p46ltHivB4bVIGtZGbI_c
+ hash: v1.k794b7964.319295338cd4bb76a2a064d87707100c4d8db95cc3ef6a52dbcbb3f80c403a3b.F2RbmJKwtVcGgJyxvpnNsVDzU_IdcrJ5g7s-ovypsxw
products-data-catalog-metric-lineage--lineage-issue--dark:
- hash: v1.k794b7964.656696196efedb0fd7093c25d810981b4ac7fcf3e44ebdc1e7c6f585e92a4791.Axds5QVAA2WSMtEov4q_cQFozEmzGVStnlS8R26Seig
+ hash: v1.k794b7964.982f070b001e8e5572f7f3fd1a64083be18624c1fbd3f563e63a36f590c20140.ASPOaTkSDhewfiCRRTUFKlTo8APhRiftTtwxOwUYVyM
products-data-catalog-metric-lineage--lineage-issue--light:
- hash: v1.k794b7964.cdbb822d7a19b6b84e41aa130a1b876153996391677919dce8e83d4ac81c00a0.A9HF3dyrwj1jB6fak5kLLkIprCiiROYA8-uGze0CWsE
+ hash: v1.k794b7964.29f8cbb269f177eb06b8d8516790545c6a05d92aa5609c2ad1f986c2e852e821.IgBKSqvm6uJBOSJlOc3nnfkVkyk_wDiS4eYw0_3Ioos
products-data-catalog-metric-lineage--load-failed--dark:
hash: v1.k794b7964.dbaf9c756de48bf854da4200cd90a2a6f9eb1c9dd579c41313ea106fc952d014.EJZNZnR7SS9VqjxWGF2ewZNdCXKG7n3iCC_muZiwZng
products-data-catalog-metric-lineage--load-failed--light:
@@ -7353,25 +7353,25 @@ snapshots:
products-data-catalog-new-metric--sql-with-result--light:
hash: v1.k794b7964.a6b6357d6d7ca719f4d9508f35e647fb64c6749213f763b876d24ee1b2ab7fff.BjYXpwtfAhhCU2yo-a17U0kP6SkEF2gi5uAj69hUcsI
products-data-modeling-lineage-graph--canvas--dark:
- hash: v1.k794b7964.dfc9cecd5fe04ca15b3030156b8a22b1e7e856ca02ba62a45acb9f18e8e4363f.OMoHNsARLj6RjX4bCuo2KF33IMbCanui8ZqWzGhIczk
+ hash: v1.k794b7964.f6519f944bba37bef7c018d3a33af183a9aefde60a21c0782e673994cab453e1.Dm_ZU_SOYwAWL4tpYPOdcyyezUUdltDIUJ0VKyMqWx0
products-data-modeling-lineage-graph--canvas--light:
- hash: v1.k794b7964.abbc2578ede8d828962ca36072f7f3192b2f595879d2df310d2b8304da15c9dc.8xbTQ8AJrMZhoqk4vMGuUxKW1IBiljeLpuubCXYKCkM
+ hash: v1.k794b7964.8dab9c7abc4c1434a1a211e89722ae0b13fbb8f5a0defef0ba1b7c7c2ce2b509.riBhtQ01wnlr-Zqbb6IG3i9kciJte-2zi9yfv9xXkSE
products-data-modeling-lineage-graph--dark-mode--dark:
- hash: v1.k794b7964.0bba727c1ca42bcb0a8790ab3bb1c109959965d16796e53db4069776e4fc39bc.vAO2RugmtkrEI-DUd14pzqt_rxrjKWNFKT6K47DG_a0
+ hash: v1.k794b7964.573b0ac86b29a10eb291756426a8d37ba76ba1228c55133d1cea25136722ae92.RZqWyCsWNWsAH76B2lSVVawWllDfhJM4jT7lNGIcP_0
products-data-modeling-lineage-graph--dark-mode--light:
- hash: v1.k794b7964.facc72132f63867ab73701992d6db307415345b73a0c22f226bb44046a328535.slTtGwJP220QDkrOFSZ5qeYFZzTEWTqDGxYwS1bgumw
+ hash: v1.k794b7964.54c7ceccc8b68c86c0a4b7f16a2e9edfdc65a95069be7776c49958b1c69d954f.b05xAJyLfrSSziJPyUhiz8l1KuJmVWHaSO4aO3KHxiU
products-data-modeling-lineage-graph--draggable-nodes--dark:
- hash: v1.k794b7964.07a5ee7f200d2b6b0f19c1c2295c0351810959cb4782ffdf368616f76bcea019.pM9SXd5270zePRVh34lXqpalGz5Wz_WDdO_5uEi232g
+ hash: v1.k794b7964.ce3d029a56f1ea40bfacd236a50545288256a2fbea23d37bda700bf5646e367c.9f5Poh-3IW6WLSp2Z0S80er1AT2RRSutgbjuCVKFH-8
products-data-modeling-lineage-graph--draggable-nodes--light:
- hash: v1.k794b7964.423039b18ad74e8b8a0f512bed923afebb082d520175c27a37852ae961d79f0f.nrCG91Mj_M54zDZb4ZL1DLh8Gedgm_DOmhPKh_zGEVQ
+ hash: v1.k794b7964.ea45692f093239731b24ae7fd73a475496767c30f4e2768d32486cdd5fa9c208.kM0uLv1-1stRk_F5w3PmNROxd4t25ez8l6sRL-0ObLE
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.c0687eeeb97ce418ef48b89f1a639aeb0996b6cfcc170e0ca91e7791136d47a9.cDYR7Z4N1Ee0qlWdoMriZEp70Z8sE0hHWniBQ2S_ycA
+ hash: v1.k794b7964.108d63e29073bc99117a6ea3d89ccefd287b3c6cc202591b5655c9e223cde734.hiNs3nTXs3ir0zT1DSxG_wIPNH-n9_KUSSlRYUSSns0
products-data-modeling-lineage-graph--full--light:
- hash: v1.k794b7964.31d0e40635d2dc6f8f417ebc5385ab28d68573f95da6eca9f44dc98ea3fb330b.AJfVSDKAq2TPHuiejnhVXMf8sGu_K6Bgdm9HNzRIMXo
+ hash: v1.k794b7964.89f63325605426975447eb5c49f37c2373d2a10992449917cfda6372e06957d3.B-XUikFmmQLn-LgIQMFAiw0dxyMlPsj8bzRTflNqU7s
products-data-modeling-lineage-graph--loading--dark:
hash: v1.k794b7964.0e48330c3af8b4788b41e6938d6957d7db3f2fac227302133d4482dde3f4e84f.1_ZYFp5H2cK3OTMZ8UrEjcgHn36LPpbKeEo0Zy1BRPk
products-data-modeling-lineage-graph--loading--light:
@@ -7385,21 +7385,21 @@ snapshots:
products-data-modeling-lineage-graph--loading-focused--light:
hash: v1.k794b7964.ad5f3bb5a4c9a5193cf69e525d90f14a6593bcd863437ae10d7148c798720278.N7js9PPoiXJKVTgurWgbO00-kxtsO-gsKxcFaaZAhPo
products-data-modeling-lineage-graph--moved-node-focus--dark:
- hash: v1.k794b7964.3c88300ce8234129cbe1809432f3545a596ea655166149faaf5fce6f23ce4c18.pyjkoADBv8Js6lTGxe2HYEa_N2j2cAiq0g9qbwTkH_4
+ hash: v1.k794b7964.e0ffdf99e79ee8a8e4b7f1dd8d5566eaed610885557356d95c46fca59181840e.8YO8W0f-IZNO6MlqQlHzjVhgfIni5J8rGqF6o3KnIYM
products-data-modeling-lineage-graph--moved-node-focus--light:
- hash: v1.k794b7964.cd86fa8aae566ba968e6f427aac7244c098b983627b71bbaa977ead163dea928.b99WA-4QaTu5AO5y2aOxScBgDW9s_DWoU0VXbJeEd8M
+ hash: v1.k794b7964.ac1a4b5645d6afae4a4820c409a0aa29bb59a90811186015f1c784646b3fdcad.pYmlMwj-I84NVUKV2f_muXNdueK3flFodsJzkfONbao
products-data-modeling-lineage-graph--narrow-canvas--dark:
- hash: v1.k794b7964.1c8595f58c615df292fb49f20c6a5baa270fb748fa352e00b61a22f198bef927.RO7E7WU6C-rcB3X_ehW13eY8II9DroOk63RrtEUZCwQ
+ hash: v1.k794b7964.f1dd1ce4fc09acb6c1bd4264aac2c7f8f13c4774d2394433de9439ab8f61c8af.Cki8jM5ZEU38rXd0df9yCwSgYyAAOo52gcSEXy9cfWs
products-data-modeling-lineage-graph--narrow-canvas--light:
- hash: v1.k794b7964.607c0429769d365d295ac605cdbadd234773b26cf48cb9e6349dea953baed856._YpaiX9ffz0Cx-r5WxQLV6PLKzNhsu34nbsXN3hVOIM
+ hash: v1.k794b7964.744988e52b3ff9ab953c1476787542513bd8741b417c13824651012403bb99f7.BGghsLpReeKPhkQzTWRdmeYnOrg2AjuBpJWq-4xNYjg
products-data-modeling-lineage-graph--reset-moved-nodes--dark:
- hash: v1.k794b7964.bf54d50c1f529389bb45a9d7d21aa6e735e73773952fea70f08b0ea6803396cc.oeEbgWmB_ekBNdk2Ng5-9L4blVGhsovaAPYrwUzI3K4
+ hash: v1.k794b7964.0d71ea8b1d85a93a8859b1f5b327071bf7c1395cfda54c090133aaff3bc26b60.aImUaSWAVX5YoG0oDT9bFR3SyuU7kVUBbXFEV_ywzO4
products-data-modeling-lineage-graph--reset-moved-nodes--light:
- hash: v1.k794b7964.58aa75c5200337a71da0b9e4eb6455df23e16c91592982406f1934d9fcab4173.uvZ46-W9atDhcT0R_h9rtD0s4Z4UItW3nnL-9DDi6Lg
+ hash: v1.k794b7964.d118ca4970c2cdf0615e0bf9dfc12a724e25983abea393590aac5583b7e9c138.B0fYzA2JlPZwsB2OfjJMUfC0rAPwOX8k7Ot6CDa-0us
products-data-modeling-lineage-graph--search-focus--dark:
- hash: v1.k794b7964.e620ed1b4d970eff0b93c3c99ec1180a8a2c8a8968ff9452c0eddf7980579fb0.K9rW9ZrdutsowcVxTOesI1rffbsms45utafU_NNUW4k
+ hash: v1.k794b7964.0ff7eaebe44c86e8f76c8809793ae83770d641b9c6aa2ab249bbace3658e737f.td5QFAw0LUIsaGwt2yglPKiZdQyY-Ilkv4bHKwDQYGs
products-data-modeling-lineage-graph--search-focus--light:
- hash: v1.k794b7964.f0abd3dcb5beca11fa88e53156122110da3c91a9c284fedb3a7da6be35d2c86a.GsNJz21eT7RcVVcv23KuLnS1AiVIcfbDI50so0ZuYTY
+ hash: v1.k794b7964.73d95e3115f93ade6e94c817ec88a906ad51813e95a041c30b1dab6012aa8af4.fTWqZEMJ70AVGo_CJiHrDCV5Jg3KAZ3vksruXab2Mac
products-data-modeling-lineage-graph--selectable--dark:
hash: v1.k794b7964.6715d1a72146914d134deef3cc269fa35d5c1d2de75e863c43ffd4dfa13928b6.GObyY80pnRagOt_eHG5SjVjTUjGavbHm6QinFyLCoMg
products-data-modeling-lineage-graph--selectable--light:
diff --git a/frontend/src/lib/elk.ts b/frontend/src/lib/elk.ts
index 08fb5cd32400..44a368ae60cf 100644
--- a/frontend/src/lib/elk.ts
+++ b/frontend/src/lib/elk.ts
@@ -1,10 +1,50 @@
import { type ELK } from 'elkjs'
+// Falls back to the main thread without a Worker (jsdom) or when the bundle isn't served (Vite dev)
+const WORKER_URL = '/static/elkWorker.js'
+const WORKER_PROBE_TIMEOUT_MS = 5000
+
let elkPromise: Promise<ELK> | null = null
+async function loadBundledElk(): Promise<ELK> {
+ const { default: ELKConstructor } = await import('elkjs/lib/elk.bundled.js')
+ return new ELKConstructor()
+}
+
+async function loadWorkerElk(): Promise<ELK> {
+ if (typeof Worker === 'undefined') {
+ throw new Error('Worker is not available')
+ }
+ const { default: ELKConstructor } = await import('elkjs/lib/elk-api.js')
+ let worker: Worker | null = null
+ const elk = new ELKConstructor({
+ workerFactory: () => {
+ worker = new Worker(WORKER_URL, { type: 'module' })
+ return worker
+ },
+ })
+ // elk-api never rejects when the worker script fails to load, so a layout would hang forever, so check the worker answers first
+ let timer: ReturnType<typeof setTimeout> | undefined
+ try {
+ await Promise.race([
+ elk.layout({ id: 'probe', children: [] }),
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(() => reject(new Error('ELK worker did not answer')), WORKER_PROBE_TIMEOUT_MS)
+ worker?.addEventListener('error', () => reject(new Error('ELK worker failed to load')))
+ }),
+ ])
+ } catch (error) {
+ elk.terminateWorker()
+ throw error
+ } finally {
+ clearTimeout(timer)
+ }
+ return elk
+}
+
export function getElk(): Promise<ELK> {
if (!elkPromise) {
- elkPromise = import('elkjs/lib/elk.bundled.js').then(({ default: ELK }) => new ELK())
+ elkPromise = loadWorkerElk().catch(() => loadBundledElk())
}
return elkPromise
}
diff --git a/frontend/src/lib/elkWorker.ts b/frontend/src/lib/elkWorker.ts
new file mode 100644
index 000000000000..d8ee81c401b1
--- /dev/null
+++ b/frontend/src/lib/elkWorker.ts
@@ -0,0 +1,2 @@
+// The elkjs worker build registers its own message handler when it runs without a document
+import 'elkjs/lib/elk-worker.min.js'
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index d4b810e4efab..ea7afee8399b 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -6344,9 +6344,6 @@ export interface DataModelingEdge {
id: string
source_id: string
target_id: string
- properties: Record<string, unknown>
- created_at: string
- updated_at: string
}
export type DataModelingSyncInterval = '15min' | '30min' | '1hour' | '6hour' | '12hour' | '24hour' | '7day' | '30day'
diff --git a/frontend/workers.config.mjs b/frontend/workers.config.mjs
index 6cbe3fa6eca3..a8fc93aea98e 100644
--- a/frontend/workers.config.mjs
+++ b/frontend/workers.config.mjs
@@ -20,6 +20,11 @@ export const WORKER_ENTRIES = [
entryPoint: 'src/scenes/data-warehouse/editor/hogqlParserWorker.ts',
outfileName: 'hogqlParserWorker.js',
},
+ {
+ name: 'ELK Layout Worker',
+ entryPoint: 'src/lib/elkWorker.ts',
+ outfileName: 'elkWorker.js',
+ },
{
name: 'Monaco Editor Worker',
entryPoint: 'src/lib/monaco/workers/editor.worker.ts',
diff --git a/products/data_catalog/frontend/MetricLineagePanel.stories.tsx b/products/data_catalog/frontend/MetricLineagePanel.stories.tsx
index 018799f1c7d1..2ba21d53ef62 100644
--- a/products/data_catalog/frontend/MetricLineagePanel.stories.tsx
+++ b/products/data_catalog/frontend/MetricLineagePanel.stories.tsx
@@ -41,8 +41,8 @@ const NODES: DataModelingNode[] = [
node({ id: '3', name: 'weekly_active_accounts', type: 'metric', metric_id: 'metric-1' }),
]
const EDGES: DataModelingEdge[] = [
- { id: 'e1', source_id: '1', target_id: '2', properties: {}, created_at: '', updated_at: '' },
- { id: 'e2', source_id: '2', target_id: '3', properties: {}, created_at: '', updated_at: '' },
+ { id: 'e1', source_id: '1', target_id: '2' },
+ { id: 'e2', source_id: '2', target_id: '3' },
]
const BASE: MetricLineagePanelProps = {
diff --git a/products/data_modeling/backend/presentation/views/edge.py b/products/data_modeling/backend/presentation/views/edge.py
index 809f74906b21..71b682e7917f 100644
--- a/products/data_modeling/backend/presentation/views/edge.py
+++ b/products/data_modeling/backend/presentation/views/edge.py
@@ -47,6 +47,16 @@ class EdgeSerializer(serializers.ModelSerializer):
return attrs
+class EdgeSummarySerializer(serializers.ModelSerializer):
+ source_id = serializers.UUIDField(read_only=True, help_text="ID of the upstream node.")
+ target_id = serializers.UUIDField(read_only=True, help_text="ID of the downstream node.")
+
+ class Meta:
+ model = Edge
+ fields = ["id", "source_id", "target_id"]
+ read_only_fields = fields
+
+
class EdgePagination(PageNumberPagination):
page_size = 5000
@@ -69,6 +79,9 @@ class EdgeViewSet(
def get_serializer_context(self) -> dict[str, Any]:
return super().get_serializer_context()
+ def get_serializer_class(self) -> type[serializers.Serializer]:
+ return EdgeSummarySerializer if self.action == "list" else EdgeSerializer
+
def perform_destroy(self, instance: Edge) -> None:
self._require_warehouse_access(level="editor", message=DELETE_DENIED)
if instance.target.belongs_to_managed_viewset:
@@ -79,4 +92,6 @@ class EdgeViewSet(
delete_orphaned_table_nodes(instance.team_id, [instance.source_id])
def safely_get_queryset(self, queryset):
+ if self.action == "list":
+ queryset = queryset.select_related(None)
return self._exclude_hidden_edges(queryset.filter(team_id=self.team_id)).order_by(self.ordering)
diff --git a/products/data_modeling/backend/tests/api/test_node_api.py b/products/data_modeling/backend/tests/api/test_node_api.py
index 516c61ff1d17..cddcf65a4783 100644
--- a/products/data_modeling/backend/tests/api/test_node_api.py
+++ b/products/data_modeling/backend/tests/api/test_node_api.py
@@ -938,8 +938,13 @@ class TestMetricNodeVisibility(WarehouseAccessControlTestMixin):
response = self.client.get(f"/api/environments/{self.team.id}/data_modeling_edges/")
- edge_ids = {edge["id"] for edge in response.json()["results"]}
- self.assertEqual(str(self.edge.id) in edge_ids, expected_visible)
+ edges = {edge["id"]: edge for edge in response.json()["results"]}
+ self.assertEqual(str(self.edge.id) in edges, expected_visible)
+ if expected_visible:
+ self.assertEqual(
+ edges[str(self.edge.id)],
+ {"id": str(self.edge.id), "source_id": str(self.edge.source_id), "target_id": str(self.edge.target_id)},
+ )
@parameterized.expand(
[
diff --git a/products/data_modeling/frontend/generated/api.schemas.ts b/products/data_modeling/frontend/generated/api.schemas.ts
index f3905643805f..1bfe3a848a83 100644
--- a/products/data_modeling/frontend/generated/api.schemas.ts
+++ b/products/data_modeling/frontend/generated/api.schemas.ts
@@ -7,25 +7,33 @@
* PostHog API - generated
* OpenAPI spec version: 1.0.0
*/
-export interface EdgeApi {
+export interface EdgeSummaryApi {
readonly id: string
/** ID of the upstream node. */
readonly source_id: string
/** ID of the downstream node. */
readonly target_id: string
- properties?: unknown
- readonly created_at: string
- /** @nullable */
- readonly updated_at: string | null
}
-export interface PaginatedEdgeListApi {
+export interface PaginatedEdgeSummaryListApi {
count: number
/** @nullable */
next?: string | null
/** @nullable */
previous?: string | null
- results: EdgeApi[]
+ results: EdgeSummaryApi[]
+}
+
+export interface EdgeApi {
+ readonly id: string
+ /** ID of the upstream node. */
+ readonly source_id: string
+ /** ID of the downstream node. */
+ readonly target_id: string
+ properties?: unknown
+ readonly created_at: string
+ /** @nullable */
+ readonly updated_at: string | null
}
export interface PatchedEdgeApi {
diff --git a/products/data_modeling/frontend/generated/api.ts b/products/data_modeling/frontend/generated/api.ts
index 0750a445e1dd..214e3ab418e6 100644
--- a/products/data_modeling/frontend/generated/api.ts
+++ b/products/data_modeling/frontend/generated/api.ts
@@ -21,7 +21,7 @@ import type {
NodeResumeApi,
NodeRunApi,
NodeRunResponseApi,
- PaginatedEdgeListApi,
+ PaginatedEdgeSummaryListApi,
PaginatedNodeListApi,
PatchedEdgeApi,
PatchedNodeApi,
@@ -64,8 +64,8 @@ export const dataModelingEdgesList = async (
projectId: string,
params?: DataModelingEdgesListParams,
options?: RequestInit
-): Promise<PaginatedEdgeListApi> => {
- return apiMutator<PaginatedEdgeListApi>(getDataModelingEdgesListUrl(projectId, params), {
+): Promise<PaginatedEdgeSummaryListApi> => {
+ return apiMutator<PaginatedEdgeSummaryListApi>(getDataModelingEdgesListUrl(projectId, params), {
...options,
method: 'GET',
})
diff --git a/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx b/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
index f77841f13e58..baee0da47063 100644
--- a/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraph.stories.tsx
@@ -26,9 +26,6 @@ function mockEdge(id: string, sourceId: string, targetId: string): DataModelingE
id,
source_id: sourceId,
target_id: targetId,
- properties: {},
- created_at: '2024-01-01T00:00:00Z',
- updated_at: '2024-01-01T00:00:00Z',
}
}
diff --git a/products/data_modeling/frontend/lineage/LineageGraph.tsx b/products/data_modeling/frontend/lineage/LineageGraph.tsx
index 825b71f9266c..7c8c6a9e13bb 100644
--- a/products/data_modeling/frontend/lineage/LineageGraph.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraph.tsx
@@ -7,6 +7,7 @@ import {
Controls,
FitViewOptions,
MiniMap,
+ type Node,
Panel,
ReactFlow,
ReactFlowProvider,
@@ -16,14 +17,24 @@ import {
} from '@xyflow/react'
import clsx from 'clsx'
import { useValues } from 'kea'
-import { type KeyboardEvent, type MouseEvent, ReactNode, useEffect, useMemo, useRef, useState } from 'react'
+import {
+ type KeyboardEvent,
+ type MouseEvent,
+ ReactNode,
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react'
-import { IconArchive, IconRefresh } from '@posthog/icons'
+import { IconArchive, IconExpand45, IconRefresh } from '@posthog/icons'
import { themeLogic } from '~/layout/navigation-3000/themeLogic'
import { DataModelingEdge, DataModelingNode } from '~/types'
import { ElkDirection } from './autolayout'
+import { LINEAGE_MIN_ZOOM, lineageFitTarget } from './lineageFit'
import { LineageGraphLoading } from './LineageGraphLoading'
import { lineageGraphLogic } from './lineageGraphLogic'
import { LINEAGE_NODE_TYPES, LineageNodeCallbacks, LineageNodeState, LineageVariant } from './LineageNode'
@@ -74,7 +85,9 @@ export interface LineageGraphProps {
}
function LineageGraphContent(props: LineageGraphProps): JSX.Element {
- const { fitView, setNodes, viewportInitialized } = useReactFlow()
+ const { fitView, getNodesBounds, setNodes, viewportInitialized } = useReactFlow()
+ const viewportWidth = useStore((state) => state.width)
+ const viewportHeight = useStore((state) => state.height)
const nodesMeasured = useNodesMeasured()
const { isDarkModeOn } = useValues(themeLogic)
const [selectedNodeId, setSelectedNodeId] = useState<string | null>(null)
@@ -96,6 +109,16 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
const fittedSearchRequest = useRef<{ requestId: number; layout: typeof layout } | null>(null)
const lastNodeDrag = useRef<{ nodeId: string; stoppedAt: number } | null>(null)
const resetRequested = useRef(false)
+
+ const fitNodes = useCallback(
+ (nodes: Node[], options: FitViewOptions = {}): void => {
+ const padding = typeof options.padding === 'number' ? options.padding : 0.2
+ const viewport = { width: viewportWidth, height: viewportHeight }
+ const target = lineageFitTarget(nodes, getNodesBounds(nodes), viewport, padding)
+ void fitView({ ...options, padding, nodes: target.nodes, maxZoom: target.maxZoom ?? options.maxZoom })
+ },
+ [fitView, getNodesBounds, viewportWidth, viewportHeight]
+ )
const selectedLineage = useMemo(
() => (props.selectable && selectedNodeId ? lineageCone(props.edges, selectedNodeId) : null),
[props.edges, props.selectable, selectedNodeId]
@@ -215,8 +238,8 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
return
}
resetRequested.current = false
- void fitView({ nodes: decoratedNodes, padding: props.fitViewOptions?.padding ?? 0.2, duration: 400 })
- }, [decoratedNodes, fitView, props.fitViewOptions?.padding, props.nodePositions, resetPositionsApplied])
+ fitNodes(decoratedNodes, { padding: props.fitViewOptions?.padding ?? 0.2, duration: 400 })
+ }, [decoratedNodes, fitNodes, props.fitViewOptions?.padding, props.nodePositions, resetPositionsApplied])
useEffect(() => {
if (!viewportInitialized || !nodesMeasured || !layout || props.loading || fittedLayout.current === layout) {
@@ -226,14 +249,13 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
if (focusNodeIds || searchFocusRequest) {
return
}
- void fitView({
+ fitNodes(decoratedNodes, {
...props.fitViewOptions,
- nodes: decoratedNodes,
padding: props.fitViewOptions?.padding ?? 0.2,
duration: 400,
})
}, [
- fitView,
+ fitNodes,
focusNodeIds,
decoratedNodes,
layout,
@@ -262,8 +284,7 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
if (nodes.length > 0) {
fittedFocus.current = { focusNodeIds, layout }
const focusedNodesMaxZoom = props.focusNodeIds ? FOCUSED_NODES_MAX_ZOOM : CURRENT_NODE_MAX_ZOOM
- void fitView({
- nodes,
+ fitNodes(nodes, {
padding: 0.2,
duration: 400,
maxZoom: focusNodeIds.size > 0 ? focusedNodesMaxZoom : undefined,
@@ -271,7 +292,7 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
}
}, [
decoratedNodes,
- fitView,
+ fitNodes,
viewportInitialized,
nodesMeasured,
focusNodeIds,
@@ -331,7 +352,7 @@ 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}
- minZoom={0.1}
+ minZoom={LINEAGE_MIN_ZOOM}
maxZoom={2}
zoomOnScroll={props.interactive ?? false}
panOnScroll={props.interactive ?? false}
@@ -341,8 +362,14 @@ function LineageGraphContent(props: LineageGraphProps): JSX.Element {
>
<Background variant={BackgroundVariant.Dots} gap={20} size={1} />
{props.showControls && (
- // The button runs its own fitView, so it needs the caller's options to respect the same zoom cap
- <Controls showInteractive={false} position="bottom-left" fitViewOptions={props.fitViewOptions}>
+ <Controls showInteractive={false} showFitView={false} position="bottom-left">
+ <ControlButton
+ aria-label="Fit view"
+ title="Fit view"
+ onClick={() => fitNodes(decoratedNodes, props.fitViewOptions)}
+ >
+ <IconExpand45 aria-hidden="true" />
+ </ControlButton>
{props.nodesDraggable && props.onResetNodePositions && (
<ControlButton
aria-label="Reset layout"
diff --git a/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx b/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
index 1f62f9bdea8c..fa8dd904f18d 100644
--- a/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
+++ b/products/data_modeling/frontend/lineage/LineageGraphLoading.tsx
@@ -38,9 +38,6 @@ function loadingGraph(
id,
source_id,
target_id,
- properties: {},
- created_at: '',
- updated_at: '',
})
const upstreamId = `${idPrefix}-upstream`
const centerNodeId = `${idPrefix}-center`
diff --git a/products/data_modeling/frontend/lineage/lineageFit.test.ts b/products/data_modeling/frontend/lineage/lineageFit.test.ts
new file mode 100644
index 000000000000..ecf173236b87
--- /dev/null
+++ b/products/data_modeling/frontend/lineage/lineageFit.test.ts
@@ -0,0 +1,29 @@
+import { LINEAGE_MIN_ZOOM, lineageFitTarget } from './lineageFit'
+
+const node = (id: string, x: number, y: number): { id: string; position: { x: number; y: number } } => ({
+ id,
+ position: { x, y },
+})
+
+describe('lineageFitTarget', () => {
+ const viewport = { width: 1000, height: 800 }
+
+ test.each([
+ {
+ name: 'fits every node when they fit above the zoom floor',
+ nodes: [node('a', 0, 0), node('b', 400, 0), node('c', 400, 300)],
+ bounds: { x: 0, y: 0, width: 600, height: 400 },
+ expected: { ids: ['a', 'b', 'c'], maxZoom: undefined },
+ },
+ {
+ name: 'centers on the node nearest the centroid when the graph needs a zoom below the floor',
+ nodes: [node('a', 0, 0), node('b', 200, 0), node('c', 0, 200), node('d', 200, 200), node('far', 0, 100000)],
+ bounds: { x: 0, y: 0, width: 400, height: 100200 },
+ expected: { ids: ['c'], maxZoom: LINEAGE_MIN_ZOOM },
+ },
+ ])('$name', ({ nodes, bounds, expected }) => {
+ const target = lineageFitTarget(nodes, bounds, viewport, 0.2)
+
+ expect({ ids: target.nodes.map((n) => n.id), maxZoom: target.maxZoom }).toEqual(expected)
+ })
+})
diff --git a/products/data_modeling/frontend/lineage/lineageFit.ts b/products/data_modeling/frontend/lineage/lineageFit.ts
new file mode 100644
index 000000000000..8a04e2e1f2b3
--- /dev/null
+++ b/products/data_modeling/frontend/lineage/lineageFit.ts
@@ -0,0 +1,41 @@
+import { type Rect, type XYPosition, getViewportForBounds } from '@xyflow/react'
+
+export const LINEAGE_MIN_ZOOM = 0.1
+
+interface FittableNode {
+ position: XYPosition
+}
+
+export interface FitTarget<T> {
+ nodes: T[]
+ maxZoom?: number
+}
+
+// Below the zoom floor fitView stays on the bounding-box midpoint, which a sparse layout can leave empty
+export function lineageFitTarget<T extends FittableNode>(
+ nodes: T[],
+ bounds: Rect,
+ viewport: { width: number; height: number },
+ padding: number
+): FitTarget<T> {
+ if (nodes.length < 2 || viewport.width <= 0 || viewport.height <= 0) {
+ return { nodes }
+ }
+ const { zoom } = getViewportForBounds(bounds, viewport.width, viewport.height, 0, Infinity, padding)
+ if (zoom >= LINEAGE_MIN_ZOOM) {
+ return { nodes }
+ }
+
+ const centroidX = nodes.reduce((sum, node) => sum + node.position.x, 0) / nodes.length
+ const centroidY = nodes.reduce((sum, node) => sum + node.position.y, 0) / nodes.length
+ let nearest = nodes[0]
+ let nearestDistance = Infinity
+ for (const node of nodes) {
+ const distance = (node.position.x - centroidX) ** 2 + (node.position.y - centroidY) ** 2
+ if (distance < nearestDistance) {
+ nearest = node
+ nearestDistance = distance
+ }
+ }
+ return { nodes: [nearest], maxZoom: LINEAGE_MIN_ZOOM }
+}
diff --git a/products/data_modeling/frontend/modelsSceneLogic.ts b/products/data_modeling/frontend/modelsSceneLogic.ts
index 7c3e94f2da2a..3feac6766fa8 100644
--- a/products/data_modeling/frontend/modelsSceneLogic.ts
+++ b/products/data_modeling/frontend/modelsSceneLogic.ts
@@ -271,7 +271,10 @@ export const modelsSceneLogic = kea<modelsSceneLogicType>([
})),
afterMount(({ actions, cache, values }) => {
posthog.capture('models tab viewed', { tab: values.activeTab })
- actions.loadDataWarehouseSavedQueries()
+ // Mounting dataWarehouseViewsLogic already starts this load, so only refresh when it was mounted earlier
+ if (!values.dataWarehouseSavedQueriesLoading) {
+ actions.loadDataWarehouseSavedQueries()
+ }
actions.setNow(Date.now())
cache.disposables.add(() => {
const intervalId = window.setInterval(() => actions.setNow(Date.now()), 60_000)
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 2a11741e4680..a147892d6fbc 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -40700,6 +40700,14 @@ export namespace Schemas {
readonly updated_at: string | null;
}
+ export interface EdgeSummary {
+ readonly id: string;
+ /** ID of the upstream node. */
+ readonly source_id: string;
+ /** ID of the downstream node. */
+ readonly target_id: string;
+ }
+
export interface ReviewerState {
/**
* Current reviewer artefact ID, or null before the first list.
@@ -76877,13 +76885,13 @@ export namespace Schemas {
results: EarlyAccessFeature[];
}
- export interface PaginatedEdgeList {
+ export interface PaginatedEdgeSummaryList {
count: number;
/** @nullable */
next?: string | null;
/** @nullable */
previous?: string | null;
- results: Edge[];
+ results: EdgeSummary[];
}
export interface PaginatedElementList {