: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(bi): prioritize certified data sources (#117753)
Marius Andrafcab2158b2bf
diff --git a/docs/published/handbook/engineering/data-warehouse.md b/docs/published/handbook/engineering/data-warehouse.mdindex 627a297453ca..88c41ae91b27 100644--- a/docs/published/handbook/engineering/data-warehouse.md+++ b/docs/published/handbook/engineering/data-warehouse.md@@ -32,7 +32,7 @@ Returning to a notebook can reuse completed query results from the last two minu**Business intelligence** lists saved worksheets at `/bi` when the `sql-editor-bi-mode` feature flag is enabled. Search all worksheets or switch to those viewed in the last 30 days. **New worksheet** opens `/bi/new`; selecting a saved worksheet opens `/bi/<short_id>` in edit mode. The project tree's **New → Worksheet** entry opens the same editor. Worksheets retain their insight identities and folder placements, but use their own file subtype and no longer appear in the Product analytics insights list. Legacy `/bi?open_insight=...` links and shared worksheet URLs still open the editor. Business intelligence and SQL keep separate unsaved working copies, and existing SQL editor links with `mode=bi` open in Business intelligence.-Choose a connection in the **Data** panel, then select a table below it. **Run** is the first toolbar action, before **Swap rows and columns**.+Choose a connection in the **Data** panel, then select a table below it. Certified sources have a checkmark beside their table icon and appear first within their folders, and folders containing certified sources are prioritized. Deprecated sources have a warning icon. Hover over either status icon for details. Deprecated and uncertified sources remain selectable; search preserves the same ordering. **Run** is the first toolbar action, before **Swap rows and columns**.**Charts** opens chart types and series controls in the right panel. **Analysis** and the visualization settings button replace that panel with their own controls. The panel starts closed. Close it after editing to return to the chart. The Series/Display settings use the same panel and scroll independently. Row filters appear once above the chart; click a filter to edit it. **AND / OR groups** opens the grouping dialog, and **Result filters** opens a popover. The left pane contains the selected source and its dimensions and measures; **Fields** hides or restores it. The toolbar wraps as the worksheet narrows, and the right panel overlays the chart when there is not enough space to dock it. These layout changes apply only to BI worksheets; SQL and other insights retain their existing settings layout.diff --git a/products/business_intelligence/frontend/BIDataSourcePicker.tsx b/products/business_intelligence/frontend/BIDataSourcePicker.tsxindex f2b091d1f565..73e91fe298eb 100644--- a/products/business_intelligence/frontend/BIDataSourcePicker.tsx+++ b/products/business_intelligence/frontend/BIDataSourcePicker.tsx@@ -10,6 +10,7 @@ import { TreeNodeDisplayIcon } from 'lib/lemon-ui/LemonTree/LemonTreeUtils'import { biDataSourcePickerLogic } from 'products/business_intelligence/frontend/biDataSourcePickerLogic'import { biEditorLogic } from 'products/business_intelligence/frontend/biEditorLogic'import { getBIDataSourceKey } from 'products/business_intelligence/frontend/biEditorTypes'+import { TableCertificationIcon } from 'products/data_warehouse/frontend/shared/components/TableCertificationBadge'export function BIDataSourcePicker(): JSX.Element {const biLogic = useMountedLogic(biEditorLogic)@@ -62,11 +63,14 @@ export function BIDataSourcePicker(): JSX.Element {!!config.source && item.id === getBIDataSourceKey(config.source)}renderItemIcon={(item) => (- <TreeNodeDisplayIcon- item={item}- expandedItemIds={visibleExpandedIds}- defaultNodeIcon={<IconDatabase />}- />+ <span className="flex shrink-0 items-center gap-1">+ <TreeNodeDisplayIcon+ item={item}+ expandedItemIds={visibleExpandedIds}+ defaultNodeIcon={<IconDatabase />}+ />+ <TableCertificationIcon certification={item.record?.certification} />+ </span>)}/></div>diff --git a/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx b/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsxindex c3ff68b368f0..891ad4c8237c 100644--- a/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx+++ b/products/business_intelligence/frontend/BusinessIntelligenceScene.stories.tsx@@ -113,7 +113,24 @@ const meta: Meta = {200,{tables: {+ example_legacy_orders: {+ id: 'example_legacy_orders',+ name: 'example_legacy_orders',+ type: 'data_warehouse',+ fields: {},+ certification: {+ status: 'deprecated',+ notes: 'Use example_orders for new worksheets.',+ },+ },+ example_raw_orders: {+ id: 'example_raw_orders',+ name: 'example_raw_orders',+ type: 'data_warehouse',+ fields: {},+ },example_orders: {+ certification: { status: 'certified', notes: 'Reviewed example order totals.' },id: 'example_orders',name: 'example_orders',type: 'data_warehouse',diff --git a/products/business_intelligence/frontend/biDataSourcePickerLogic.ts b/products/business_intelligence/frontend/biDataSourcePickerLogic.tsindex 2b46d33cf69d..e6576ae52bb9 100644--- a/products/business_intelligence/frontend/biDataSourcePickerLogic.ts+++ b/products/business_intelligence/frontend/biDataSourcePickerLogic.ts@@ -154,7 +154,7 @@ export const biDataSourcePickerLogic: LogicWrapper<biDataSourcePickerLogicType>children.push({id: table.name,name: table.name,- record: { type: table.type === 'view' ? 'view' : 'table' },+ record: { type: table.type === 'view' ? 'view' : 'table', certification: table.certification },})groups.set(group, children)}diff --git a/products/business_intelligence/frontend/biDataSourceTree.test.ts b/products/business_intelligence/frontend/biDataSourceTree.test.tsindex 983f3fd65907..313cdcf49616 100644--- a/products/business_intelligence/frontend/biDataSourceTree.test.ts+++ b/products/business_intelligence/frontend/biDataSourceTree.test.ts@@ -1,6 +1,7 @@import { TreeDataItem } from 'lib/lemon-ui/LemonTree/LemonTree'import { BIDataSource } from '~/queries/schema/schema-business-intelligence'+import { DatabaseSchemaTable } from '~/queries/schema/schema-general'import { buildBIDataSourceTree, searchBIDataSourceTree } from 'products/business_intelligence/frontend/biDataSourceTree'import { getBIDataSourceKey } from 'products/business_intelligence/frontend/biEditorTypes'@@ -39,6 +40,76 @@ const tree: TreeDataItem[] = [const sources: BIDataSource[] = [{ table: 'sales.orders' }, { table: 'sales.customers' }, { table: 'invoices' }]describe('BI data source tree', () => {+ it.each([false, true])('preserves trust metadata and prioritizes certified sources (direct: %s)', (direct) => {+ const certification = { status: 'certified' as const, notes: 'Reviewed example totals' }+ const tables: DatabaseSchemaTable[] = [+ {+ id: 'old',+ name: 'sales.old_orders',+ type: 'posthog',+ fields: {},+ certification: { status: 'deprecated' },+ },+ { id: 'raw', name: 'sales.raw_orders', type: 'posthog', fields: {} },+ { id: 'orders', name: 'sales.orders', type: 'posthog', fields: {}, certification },+ ]+ const connectionId = direct ? 'example-connection' : undefined+ const savedSource = { table: 'sales.orders', connectionId: 'another-connection' }+ const results = buildBIDataSourceTree(+ [+ {+ id: 'tables',+ name: 'Tables',+ children: tables.map((table) => ({+ id: table.id,+ name: table.name,+ record: { type: 'table', certification: table.certification },+ })),+ },+ ],+ [...tables.map(({ name }) => ({ table: name, connectionId })), savedSource],+ direct,+ 'public',+ connectionId+ )+ const leaves = (nodes: TreeDataItem[]): TreeDataItem[] =>+ nodes.flatMap((node) => (node.children ? leaves(node.children) : [node]))+ const matched = leaves(searchBIDataSourceTree(results, 'orders'))+ const current = matched.filter((node) => node.id !== getBIDataSourceKey(savedSource))+ expect(current.map((node) => node.name)).toEqual(['sales.orders', 'sales.raw_orders', 'sales.old_orders'])+ expect(current[0].record?.certification).toEqual(certification)+ expect(+ matched.find((node) => node.id === getBIDataSourceKey(savedSource))?.record?.certification+ ).toBeUndefined()+ })++ it('prioritizes folders by their matching sources after search', () => {+ const results = buildBIDataSourceTree(+ [+ { id: 'raw', name: 'Raw', children: [{ id: 'a', name: 'orders_raw', record: { type: 'table' } }] },+ {+ id: 'views',+ name: 'Views',+ children: [+ {+ id: 'b',+ name: 'customers',+ record: { type: 'view', certification: { status: 'certified' } },+ },+ {+ id: 'c',+ name: 'orders_old',+ record: { type: 'view', certification: { status: 'deprecated' } },+ },+ ],+ },+ ],+ [{ table: 'orders_raw' }, { table: 'customers' }, { table: 'orders_old' }],+ false+ )+ expect(results.map((node) => node.name)).toEqual(['Views', 'Raw'])+ expect(searchBIDataSourceTree(results, 'orders').map((node) => node.name)).toEqual(['Raw', 'Views'])+ })it.each([['orders', ['sales.orders']],[' POSTGRES ', ['sales.orders', 'sales.customers']],@@ -101,7 +172,12 @@ describe('BI data source tree', () => {expect(searchBIDataSourceTree(results, 'Views')).toEqual(results)expect(results).toHaveLength(1)expect(results[0].children).toEqual([- { id: getBIDataSourceKey(endpoint), name: 'report_v1', icon, record: { type: 'endpoint' } },+ {+ id: getBIDataSourceKey(endpoint),+ name: 'report_v1',+ icon,+ record: { type: 'endpoint', tableName: 'report_v1' },+ },])})})diff --git a/products/business_intelligence/frontend/biDataSourceTree.ts b/products/business_intelligence/frontend/biDataSourceTree.tsindex 8c2e49ebf9a0..f6bb209a05c9 100644--- a/products/business_intelligence/frontend/biDataSourceTree.ts+++ b/products/business_intelligence/frontend/biDataSourceTree.ts@@ -5,6 +5,23 @@ import { BIDataSource } from '~/queries/schema/schema-business-intelligence'import { getBIDataSourceKey } from 'products/business_intelligence/frontend/biEditorTypes'import { groupDirectConnectionTableNodesBySchema } from 'products/data_warehouse/frontend/shared/connectionTableTree'+function certificationRank(item: TreeDataItem): number {+ if (item.children?.length) {+ return Math.min(...item.children.map(certificationRank))+ }+ return item.record?.certification?.status === 'certified'+ ? 0+ : item.record?.certification?.status === 'deprecated'+ ? 2+ : 1+}++function prioritizeCertifiedSources(tree: TreeDataItem[]): TreeDataItem[] {+ return tree+ .map((item) => (item.children ? { ...item, children: prioritizeCertifiedSources(item.children) } : item))+ .sort((a, b) => certificationRank(a) - certificationRank(b))+}+export function buildBIDataSourceTree(tree: TreeDataItem[],sources: BIDataSource[],@@ -29,7 +46,7 @@ export function buildBIDataSourceTree(id: getBIDataSourceKey(source),name: source.table,icon: item.icon,- record: { type: item.record?.type },+ record: { ...item.record, type: item.record?.type },}leaves.push(node)return [node]@@ -41,20 +58,24 @@ export function buildBIDataSourceTree(})const grouped = visit(tree)const missing = [...remaining.values()].map((source) => ({ id: getBIDataSourceKey(source), name: source.table }))- return directConnection- ? groupDirectConnectionTableNodesBySchema([...leaves, ...missing], false, defaultSchemaName)- : [...grouped, ...missing]+ return prioritizeCertifiedSources(+ directConnection+ ? groupDirectConnectionTableNodesBySchema([...leaves, ...missing], false, defaultSchemaName)+ : [...grouped, ...missing]+ )}export function searchBIDataSourceTree(tree: TreeDataItem[], search: string): TreeDataItem[] {const term = search.trim().toLocaleLowerCase()- return tree.flatMap((item) => {- if (item.name.toLocaleLowerCase().includes(term)) {- return [item]- }- const children = searchBIDataSourceTree(item.children ?? [], term)- return children.length ? [{ ...item, children }] : []- })+ return prioritizeCertifiedSources(+ tree.flatMap((item) => {+ if (item.name.toLocaleLowerCase().includes(term)) {+ return [item]+ }+ const children = searchBIDataSourceTree(item.children ?? [], term)+ return children.length ? [{ ...item, children }] : []+ })+ )}export function getBIDataSourceFolderIds(tree: TreeDataItem[]): string[] {