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.

feat(warehouse-suggestions): suggested models strip and catalog suggested rows (#113447)

Thiago Salvatorebc651e087ac6
diff --git a/common/storybook/.storybook/app-context.ts b/common/storybook/.storybook/app-context.ts
index 1bbf3e681e62..e4539205e44b 100644
--- a/common/storybook/.storybook/app-context.ts
+++ b/common/storybook/.storybook/app-context.ts
@@ -25,6 +25,7 @@ export const getStorybookAppContext = (): AppContext => ({
},
resource_access_control: {
action: 'manager',
+ data_catalog: 'manager',
feature_flag: 'manager',
hog_flow: 'manager',
heatmap: 'manager',
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 7b47b5913c3c..cc3c71898c07 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -16808,6 +16808,66 @@ snapshots:
hash: v1.k794b7964.c17be7ebf593bcdc6ad760d1ae2035be3dadad1e049d39ef6c72b9e093a03067.KVwJMpXcfhchvEMKnnqsNnyBE6EI5d9YIUWIB-08xfY
utils-autocapture-preview-image--tab-relative-image-with-current-url--light:
hash: v1.k794b7964.3e7d7a6fa83916bae8280f36864538241c45bd4122e167b299a046939c802b03.oW1U5X9JYmxSLcn72S6kPmgj_jxgZTKyJa0uZ1kbRJs
+ warehouse-suggestions-suggested-certifications--banner--dark:
+ hash: v1.k794b7964.b3793a018b9fc7d1019402ec5216189d26ae85000cbb9d91ede5b7ac7f6528c3.V-ICrhJc-LkfYh8MsK8Q2AoJxCcRhqLIDNvGQ0uyFug
+ warehouse-suggestions-suggested-certifications--banner--light:
+ hash: v1.k794b7964.4398ae40c8e28494481e73b72c49315fd5294380aaa8f4f579c53b120e95d2f3.qj1-k55d9dyizhho1p59SKxeUPeAKHr6ZRnFAkcZgIA
+ warehouse-suggestions-suggested-certifications--deprecate-dialog--dark:
+ hash: v1.k794b7964.379398a0bc13a5f75b341a3ea524b9ba0a640cbb3a97b0a93e4a79a9dbb980e2.xhvNtFSq3a9qkI9V2wIMnvaBeXCY3h8eXO2De84KhOU
+ warehouse-suggestions-suggested-certifications--deprecate-dialog--light:
+ hash: v1.k794b7964.966ef9d3e4d2e372c4540477ff29f30cf40cfa14fc00f8f20b3598daddb07bc2.Rtpa4Ja7OqI5c5VN5sETaXj1brjK8ejBUvxu1gsqMWQ
+ warehouse-suggestions-suggested-certifications--review-dialog--dark:
+ hash: v1.k794b7964.89802b93fe9b475b2c41505c82c562a9c805a3022fa7b0f31d792ee4e26aa356.12Jokr27QXDdzls30bzHiJBVcmY-YW7jGvpMdjl5qvs
+ warehouse-suggestions-suggested-certifications--review-dialog--light:
+ hash: v1.k794b7964.0f6fb7eb4f579c64837df55cbd4f4df7ed124b75d5d394379c2ac621e65112b1.vEB6Jtzs9i2qSzZpJ1I-oMPSP2YZvLsNeFptGLvDnIw
+ warehouse-suggestions-suggested-certifications--review-dialog-narrow--dark:
+ hash: v1.k794b7964.5f9dc23fe422a76659ed69cdf41b77dfe2270f03d6e0fd86565ba931cb552d95.OJ6su46_HVfPGBbzt_9f_PN91F0Y-4UVu_yGV8xZrFE
+ warehouse-suggestions-suggested-certifications--review-dialog-narrow--light:
+ hash: v1.k794b7964.3b5047b043fe10ce37b2e876db33158911951f7f181f76826df4703d0cf4d9c0.ZMOGZUA32lzC4cpnm9eWc99cU9xXecUWujMH99JdztY
+ warehouse-suggestions-suggested-models-strip--banner--dark:
+ hash: v1.k794b7964.5eab43c7f6811e5ca0651c1dbe3fbe44dd21387f05d24d07de36a6603a24cf8d.ouJOT_N_8rEwDbxsI92aGh8Seefi1ctVoAgMybcFsV0
+ warehouse-suggestions-suggested-models-strip--banner--light:
+ hash: v1.k794b7964.33924e399de75936e19314e3aa0eb7dc1697570862cc893bae3b4cd062e0b360.tZ8DSfGDLOGG7Xc_kXI7uC_CyU5l7vOPkwNKxNu9o6I
+ warehouse-suggestions-suggested-models-strip--banner-narrow--dark:
+ hash: v1.k794b7964.696f466f94d41f530c407436ba73a3eb523005e63e056605b3d457546d0ee045.isZjSiaKcjcu-ztNvw7lQeDodFOk4683ViotSunRT5w
+ warehouse-suggestions-suggested-models-strip--banner-narrow--light:
+ hash: v1.k794b7964.f779330a9ab26a6d5ef120c66ae00f45d956d306abcf213688582cdadf375d61.LVyCJ00Z48VEVXdT1oQwMC6y_NGbdqhXAMxK_-yG448
+ warehouse-suggestions-suggested-models-strip--load-failed--dark:
+ hash: v1.k794b7964.d93ab3575b7bebf7f869bc5b41c0a8e0581679f22bd0251c31f031b0f43ee7c0.iXXukenkQJA0ayAlRoqei_RQx1XW-b9MLaTjdhxfkoM
+ warehouse-suggestions-suggested-models-strip--load-failed--light:
+ hash: v1.k794b7964.a5b60b08e7b0a541c14bae9a0a40118fced381d13bfdd40b8e93342959f24f19.LwTgelFpFnRFgNgu3ueAFF9dCIqGgJGMgoyv53ZJe9g
+ warehouse-suggestions-suggested-models-strip--materialize-dialog-incremental-proposal--dark:
+ hash: v1.k794b7964.74f9d19ad93bb618504d2d403e1725ce7fbe501bf78a121822d8f20ad690203e.bPzGeVdTDg_mPVKZsK_HJ0leK8dC-nlU2Ay61tXAbe4
+ warehouse-suggestions-suggested-models-strip--materialize-dialog-incremental-proposal--light:
+ hash: v1.k794b7964.efe5a247e0cf30df05eb016b063f27bc0c6e7004a7c5346d37cd7001870e150a.n04UaOgI7AGNJC7wRti796r6i9Z4fZ488jp3EdumyaY
+ warehouse-suggestions-suggested-models-strip--materialize-dialog-longer-schedule-unknown-sources--dark:
+ hash: v1.k794b7964.caeb876c239ab68a4910791a582803479d5c395fd32439a2bed416e1ecce46ad.W9QtU1TJIvsJukrw_k6_ZyQMeNxaGfuUYR6wuop5bOQ
+ warehouse-suggestions-suggested-models-strip--materialize-dialog-longer-schedule-unknown-sources--light:
+ hash: v1.k794b7964.5872f82618605205a3fde796088db08326f237543eae5f33711f0a30baf2cb77.lcVZNPKUrhhwlcL8UX95J6FdDkhsNmg0aLPRdTrMXk4
+ warehouse-suggestions-suggested-models-strip--not-eligible--dark:
+ hash: v1.k794b7964.b646d37c91e92fd3d0053e1407559117a50c06c45f8ff7f4bcaaed18bab1cfef.1vguAxzgXrCyuyN1v2jRBSTSGDrmsu62f3nO60GFvGc
+ warehouse-suggestions-suggested-models-strip--not-eligible--light:
+ hash: v1.k794b7964.a66ac5be916b459b6f8b15621e3bdeb12226d34c1e24feb15900940652fc238b.gzxwnKSQkZQfg5wdIMriKJO-aaxIteiXVzfSx1R7w8c
+ warehouse-suggestions-suggested-models-strip--review-dialog--dark:
+ hash: v1.k794b7964.d7a97db72786b603d2b91bc135306d9f209cb826b9a94ea60278e32a5b51c2b4.9SG-h8azE_3Dv6B0fRMypQwublXXHhEAhEtkTHy-ZPY
+ warehouse-suggestions-suggested-models-strip--review-dialog--light:
+ hash: v1.k794b7964.395fbaf1ad8d74c5063132bfeafe6a3f8f9130129e65b08687517c22b7430f79.t1FqX63N7P6Gyob_yJGn4aGFbsk_sMOJaXct-oJe98s
+ warehouse-suggestions-suggested-models-strip--review-dialog-many-suggestions--dark:
+ hash: v1.k794b7964.1fb2c3524d2880a401435332be82e9d6a109699bb684e8d8a632e2c1d7262cce.E6wFW05wsOdqs4abb8a7sOrqAiSlZD_2HGrqLHw8QpA
+ warehouse-suggestions-suggested-models-strip--review-dialog-many-suggestions--light:
+ hash: v1.k794b7964.c441546b521c5286469dc57b0d4a1daedcceb71a1d1b77cc13a985d24a7bc0b2.RZP2_X7ud-iKHkrc4_5kNUPdG_Y7K9g_atrRdloUv9U
+ warehouse-suggestions-suggested-models-strip--review-dialog-viewer-without-edit-access--dark:
+ hash: v1.k794b7964.78818080e410c0639744c301e983042c6c5ce24e451eabe2c716df45d2988472.v_up3Pez-pWEw5UA_54ptbBwA470-3wcjOzaBOkOGk8
+ warehouse-suggestions-suggested-models-strip--review-dialog-viewer-without-edit-access--light:
+ hash: v1.k794b7964.62361ba9d4282173b78fc84a73210eb0b4df37902b6bd49ac341fdd520c3eaba.OPI08CUPtrsmvWyrvyjiojYy0k7xfjxGqLCkwbOP4_A
+ warehouse-suggestions-suggested-models-strip--stop-materializing-dialog--dark:
+ hash: v1.k794b7964.8a797fd358479a7be6614b6d85ebd369a80c839cf2d13f66458757bbb819772e.dG-gaGw-FHj4Gc2e68Y1vaLtxC1cfsC5ViVgLmLPNjk
+ warehouse-suggestions-suggested-models-strip--stop-materializing-dialog--light:
+ hash: v1.k794b7964.3fffc0e8f8756eeeb6073e1915247aa46a925e4cee1c15ddfbf15ceb188bbd8f.Gs-ybZLuvEAOo39xjwrpGnF-c8X-Zrjstj9gmTp3e1w
+ warehouse-suggestions-suggested-models-strip--warming-up--dark:
+ hash: v1.k794b7964.860407091149fedd6c4e056cda79d92213e59dd9e00886e0cedee320fc267c48.x5QcsRUSRT66uOc237oq_n4D-eQWpY2GzsTRTC-SL6w
+ warehouse-suggestions-suggested-models-strip--warming-up--light:
+ hash: v1.k794b7964.86db938002ea2e5c707b2a5732b996bc00a3b62fa007d18be17bd16436a529cd.bd4HNKnDSFxD74paJmB86KW6DhqVz9WPJpCzjbRMt7o
web-analytics-heatmaps-pricing-notice--empty-capture-list--dark:
hash: v1.k794b7964.9a5b4e65070403f615b0f04b60799af6cd4d0ae75810e18bea042c54e084d945.Rc8ysk1u96jJNkCfo1DX-y__WWcmxeHyn8q6l8j3P44
web-analytics-heatmaps-pricing-notice--empty-capture-list--light:
diff --git a/frontend/src/lib/components/ActivityLog/describers.tsx b/frontend/src/lib/components/ActivityLog/describers.tsx
index a1c6942f2459..2491dfaf6579 100644
--- a/frontend/src/lib/components/ActivityLog/describers.tsx
+++ b/frontend/src/lib/components/ActivityLog/describers.tsx
@@ -50,6 +50,7 @@ import { insightActivityDescriber } from 'products/product_analytics/frontend/in
import { signalScoutConfigActivityDescriber } from 'products/signals/frontend/activityDescriber'
import { skillActivityDescriber } from 'products/skills/frontend/activityDescriber'
import { stamphogRepoConfigActivityDescriber } from 'products/stamphog/frontend/activityDescriber'
+import { warehouseSuggestionActivityDescriber } from 'products/warehouse_suggestions/frontend/warehouseSuggestionActivityDescriber'
import { workflowActivityDescriber } from 'products/workflows/frontend/Workflows/misc/workflowActivityDescriber'
export const describerFor = (logItem?: ActivityLogItem): Describer | undefined => {
@@ -119,6 +120,8 @@ export const describerFor = (logItem?: ActivityLogItem): Describer | undefined =
return errorTrackingActivityDescriber
case ActivityScope.DATA_WAREHOUSE_SAVED_QUERY:
return dataWarehouseSavedQueryActivityDescriber
+ case ActivityScope.WAREHOUSE_SUGGESTION:
+ return warehouseSuggestionActivityDescriber
case ActivityScope.REPLAY:
return replayActivityDescriber
case ActivityScope.HEATMAP:
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index e024274afd2c..cecd47501ba9 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -570,6 +570,7 @@ export const FEATURE_FLAGS = {
VISUAL_REVIEW: 'visual-review', // owner: #team-devex
WAREHOUSE_MULTI_DESTINATION: 'warehouse-multi-destination', // owner: #team-warehouse-sources
WAREHOUSE_PERSON_PROPERTIES: 'warehouse-person-properties', // owner: @tomowers #team-warehouse-sources, gates warehouse -> person properties (person-target custom property sources)
+ WAREHOUSE_SUGGESTIONS: 'warehouse-suggestions', // owner: #team-data-modeling
WEB_ANALYTICS_ACHIEVEMENTS: 'web-analytics-achievements', // owner: @jordanm-posthog #team-web-analytics
WEB_ANALYTICS_AGENT_ANALYTICS: 'web-analytics-agent-analytics', // owner: @jordanm-posthog #team-web-analytics
WEB_ANALYTICS_BOT_ANALYSIS: 'web-analytics-bot-analysis', // owner: @lricoy #team-web-analytics
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 957b1e4b8e4a..94d90852b15f 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -318,6 +318,7 @@ export enum AccessControlResourceType {
Action = 'action',
CustomerAnalytics = 'customer_analytics',
CustomerTask = 'customer_task',
+ DataCatalog = 'data_catalog',
DataDeletion = 'data_deletion',
FeatureFlag = 'feature_flag',
Heatmap = 'heatmap',
@@ -6180,6 +6181,7 @@ export enum ActivityScope {
DATA_WAREHOUSE_JOIN = 'DataWarehouseJoin',
ACCESS_CONTROL = 'AccessControl',
DATA_QUALITY_CHECK = 'DataQualityCheck',
+ WAREHOUSE_SUGGESTION = 'WarehouseSuggestion',
TAG = 'Tag',
TAGGED_ITEM = 'TaggedItem',
EVALUATION = 'Evaluation',
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 7cd47f4ff8f4..ebc96d5f90c3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -5504,7 +5504,45 @@ importers:
products/warehouse_sources_queue: {}
- products/warehouse_suggestions: {}
+ products/warehouse_suggestions:
+ dependencies:
+ '@posthog/icons':
+ specifier: 'catalog:'
+ '@types/react':
+ specifier: 18.3.27
+ version: 18.3.27
+ clsx:
+ specifier: '*'
+ version: 2.1.1
+ kea:
+ specifier: 'catalog:'
+ version: 4.0.0-pre.6(patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected])
+ kea-loaders:
+ specifier: 'catalog:'
+ version: 3.1.1([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
+ kea-router:
+ specifier: 'catalog:'
+ version: 3.4.2([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
+ posthog-js:
+ specifier: 'catalog:'
+ version: 1.438.21(@types/[email protected])([email protected])
+ react:
+ specifier: 18.3.1
+ version: 18.3.1
+ devDependencies:
+ '@storybook/react':
+ specifier: 'catalog:'
+ '@testing-library/jest-dom':
+ specifier: ^5.17.0
+ version: 5.17.0
+ '@testing-library/react':
+ specifier: ^14.3.1
+ kea-test-utils:
+ specifier: 'catalog:'
+ version: 0.2.4([email protected](patch_hash=139b8d1f1304f9d9da452a9a1244c94ea679dbcb85687d8999563146879fb6f5)([email protected]))
products/web_analytics:
dependencies:
diff --git a/products/data_catalog/frontend/certificationsLogic.test.ts b/products/data_catalog/frontend/certificationsLogic.test.ts
index 372e2c95b9de..5c627b92006c 100644
--- a/products/data_catalog/frontend/certificationsLogic.test.ts
+++ b/products/data_catalog/frontend/certificationsLogic.test.ts
@@ -1,10 +1,12 @@
+import { router } from 'kea-router'
+
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { databaseTableListLogic } from 'scenes/data-management/database/databaseTableListLogic'
import { initKeaTests } from '~/test/init'
import { expectLogic } from '~/test/keaTestUtils'
-import { certificationsLogic } from './certificationsLogic'
+import { CERTIFICATIONS_TABLE_ID, DEFAULT_CERTIFICATIONS_FILTERS, certificationsLogic } from './certificationsLogic'
import type { CertificationsFilters, NewCertificationForm } from './certificationsLogic'
import {
dataCatalogCertificationsCertifyCreate,
@@ -164,6 +166,33 @@ describe('certificationsLogic', () => {
expect(logic.values.isCreatingCertification).toBe(false)
})
+ it('refreshes the shared schema after a suggestion is accepted so trust badges update', () => {
+ mockLoadDatabase.mockClear()
+
+ logic.actions.suggestionAccepted()
+
+ expect(mockLoadDatabase).toHaveBeenCalledWith({ force: true })
+ })
+
+ it('opens the page that holds a highlighted certification once it loads, with the filters and sort cleared', () => {
+ logic.actions.setFilters({ status: 'proposed', search: 'stripe' })
+ router.actions.replace(router.values.location.pathname, {
+ [`${CERTIFICATIONS_TABLE_ID}_order`]: '-target_name',
+ table: 'stripe',
+ })
+
+ logic.actions.highlightCertification('cert-25')
+ logic.actions.loadCertificationsSuccess(
+ Array.from({ length: 30 }, (_, index) => buildCertification({ id: `cert-${index + 1}` }))
+ )
+
+ expect(logic.values.filters).toEqual(DEFAULT_CERTIFICATIONS_FILTERS)
+ expect(logic.values.highlightedCertificationId).toEqual('cert-25')
+ expect(router.values.searchParams[`${CERTIFICATIONS_TABLE_ID}_page`]).toEqual(2)
+ expect(router.values.searchParams[`${CERTIFICATIONS_TABLE_ID}_order`]).toBeUndefined()
+ expect(router.values.searchParams.table).toBeUndefined()
+ })
+
it('removes the row when revoking', async () => {
;(dataCatalogCertificationsDestroy as jest.Mock).mockResolvedValue(undefined)
diff --git a/products/data_catalog/frontend/certificationsLogic.tsx b/products/data_catalog/frontend/certificationsLogic.tsx
index 1975508ab25c..71afc4192b8f 100644
--- a/products/data_catalog/frontend/certificationsLogic.tsx
+++ b/products/data_catalog/frontend/certificationsLogic.tsx
@@ -1,6 +1,6 @@
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
-import { urlToAction } from 'kea-router'
+import { router, urlToAction } from 'kea-router'
import { ApiConfig, ApiError } from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
@@ -26,6 +26,9 @@ export interface CertificationsFilters {
status: CertificationStatusFilter
}
+export const CERTIFICATIONS_PAGE_SIZE = 20
+export const CERTIFICATIONS_TABLE_ID = 'data-catalog-certifications'
+
export const DEFAULT_CERTIFICATIONS_FILTERS: CertificationsFilters = {
search: '',
status: 'all',
@@ -64,6 +67,8 @@ export interface certificationsLogicValues {
certificationsLoading: boolean
filteredCertifications: DataCatalogCertificationApi[]
filters: CertificationsFilters
+ highlightPending: boolean
+ highlightedCertificationId: string | null
isCreatingCertification: boolean
newCertificationForm: NewCertificationForm
newCertificationModalOpen: boolean
@@ -95,6 +100,12 @@ export interface certificationsLogicActions {
deprecateCertification: (id: string) => {
id: string
}
+ highlightCertification: (id: string) => {
+ id: string
+ }
+ highlightedCertificationShown: () => {
+ value: true
+ }
loadCertifications: () => any
loadCertificationsFailure: (
error: string,
@@ -132,6 +143,9 @@ export interface certificationsLogicActions {
setNewCertificationForm: (form: Partial<NewCertificationForm>) => {
form: Partial<NewCertificationForm>
}
+ suggestionAccepted: () => {
+ value: true
+ }
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -159,6 +173,7 @@ export const certificationsLogic = kea<certificationsLogicType>([
})),
actions({
setFilters: (filters: Partial<CertificationsFilters>) => ({ filters }),
+ suggestionAccepted: true,
openNewCertificationModal: true,
closeNewCertificationModal: true,
setNewCertificationForm: (form: Partial<NewCertificationForm>) => ({ form }),
@@ -168,6 +183,8 @@ export const certificationsLogic = kea<certificationsLogicType>([
deprecateCertification: (id: string) => ({ id }),
revokeCertification: (id: string) => ({ id }),
setActionInFlight: (id: string, inFlight: boolean) => ({ id, inFlight }),
+ highlightCertification: (id: string) => ({ id }),
+ highlightedCertificationShown: true,
}),
loaders(() => ({
certifications: [
@@ -214,6 +231,19 @@ export const certificationsLogic = kea<certificationsLogicType>([
setActionInFlight: (state, { id, inFlight }) => ({ ...state, [id]: inFlight }),
},
],
+ highlightedCertificationId: [
+ null as string | null,
+ {
+ highlightCertification: (_, { id }) => id,
+ },
+ ],
+ highlightPending: [
+ false,
+ {
+ highlightCertification: () => true,
+ highlightedCertificationShown: () => false,
+ },
+ ],
}),
selectors({
filteredCertifications: [
@@ -257,7 +287,37 @@ export const certificationsLogic = kea<certificationsLogicType>([
}
}
+ const showHighlightedPage = (): void => {
+ const index = values.filteredCertifications.findIndex(
+ (certification) => certification.id === values.highlightedCertificationId
+ )
+ if (!values.highlightPending || index < 0) {
+ return
+ }
+ const { location, searchParams, hashParams } = router.values
+ router.actions.replace(
+ location.pathname,
+ {
+ ...searchParams,
+ [`${CERTIFICATIONS_TABLE_ID}_page`]: Math.floor(index / CERTIFICATIONS_PAGE_SIZE) + 1,
+ [`${CERTIFICATIONS_TABLE_ID}_order`]: undefined,
+ table: undefined,
+ },
+ hashParams
+ )
+ actions.highlightedCertificationShown()
+ }
+
return {
+ highlightCertification: () => {
+ actions.setFilters(DEFAULT_CERTIFICATIONS_FILTERS)
+ showHighlightedPage()
+ },
+ loadCertificationsSuccess: () => showHighlightedPage(),
+ suggestionAccepted: () => {
+ actions.loadCertifications()
+ actions.loadDatabase({ force: true })
+ },
openNewCertificationModal: () => actions.loadDatabase(),
createCertification: async () => {
if (values.isCreatingCertification) {
diff --git a/products/data_catalog/frontend/tabs/CertificationsTab.tsx b/products/data_catalog/frontend/tabs/CertificationsTab.tsx
index fb18791a0c1b..9fe2dc2f4885 100644
--- a/products/data_catalog/frontend/tabs/CertificationsTab.tsx
+++ b/products/data_catalog/frontend/tabs/CertificationsTab.tsx
@@ -1,15 +1,25 @@
import { useActions, useValues } from 'kea'
+import { useEffect } from 'react'
import { IconPlusSmall, IconRefresh } from '@posthog/icons'
import { LemonButton, LemonDialog } from '@posthog/lemon-ui'
+import { FEATURE_FLAGS } from 'lib/constants'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { LemonSegmentedButton } from 'lib/lemon-ui/LemonSegmentedButton'
import { LemonTable, LemonTableColumns } from 'lib/lemon-ui/LemonTable'
import { LemonTag } from 'lib/lemon-ui/LemonTag'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
-import { CertificationStatusFilter, certificationsLogic } from '../certificationsLogic'
+import { SuggestedCertificationsTable } from 'products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable'
+
+import {
+ CERTIFICATIONS_PAGE_SIZE,
+ CERTIFICATIONS_TABLE_ID,
+ CertificationStatusFilter,
+ certificationsLogic,
+} from '../certificationsLogic'
import { compareText, reportCatalogTableSorted } from '../common'
import { NewCertificationModal } from '../components/NewCertificationModal'
import type { DataCatalogCertificationApi } from '../generated/api.schemas'
@@ -28,16 +38,35 @@ const STATUS_TAG: Record<string, { label: string; type: 'warning' | 'success' |
}
export function CertificationsTab(): JSX.Element {
- const { filteredCertifications, certificationsLoading, filters, actionsInFlight } = useValues(certificationsLogic)
+ const {
+ filteredCertifications,
+ certificationsLoading,
+ filters,
+ actionsInFlight,
+ highlightedCertificationId,
+ highlightPending,
+ } = useValues(certificationsLogic)
+ const { featureFlags } = useValues(featureFlagLogic)
const {
setFilters,
loadCertifications,
+ suggestionAccepted,
openNewCertificationModal,
certifyCertification,
deprecateCertification,
revokeCertification,
+ highlightCertification,
} = useActions(certificationsLogic)
+ useEffect(() => {
+ if (!highlightedCertificationId || highlightPending) {
+ return
+ }
+ document
+ .querySelector(`[data-row-key="${highlightedCertificationId}"]`)
+ ?.scrollIntoView({ block: 'center', behavior: 'smooth' })
+ }, [highlightedCertificationId, highlightPending])
+
const confirmRevoke = (certification: DataCatalogCertificationApi): void => {
LemonDialog.open({
title: 'Revoke this certification?',
@@ -138,6 +167,17 @@ export function CertificationsTab(): JSX.Element {
return (
<div className="flex flex-col gap-4">
+ {featureFlags[FEATURE_FLAGS.WAREHOUSE_SUGGESTIONS] && (
+ <SuggestedCertificationsTable
+ onAccepted={() => suggestionAccepted()}
+ onViewAccepted={(suggestion) => {
+ const certificationId = suggestion.created_asset?.certification_id
+ if (typeof certificationId === 'string') {
+ highlightCertification(certificationId)
+ }
+ }}
+ />
+ )}
<div className="flex justify-between gap-2 flex-wrap items-center">
<LemonInput
type="search"
@@ -173,13 +213,17 @@ export function CertificationsTab(): JSX.Element {
</div>
</div>
<LemonTable
- id="data-catalog-certifications"
+ key={highlightedCertificationId ?? CERTIFICATIONS_TABLE_ID}
+ id={CERTIFICATIONS_TABLE_ID}
data-attr="data-catalog-certifications-table"
dataSource={filteredCertifications}
rowKey="id"
columns={columns}
loading={certificationsLoading}
- pagination={{ pageSize: 20 }}
+ pagination={{ pageSize: CERTIFICATIONS_PAGE_SIZE }}
+ rowClassName={(certification) =>
+ certification.id === highlightedCertificationId ? 'bg-accent-highlight-secondary' : null
+ }
emptyState="No certifications match your filters."
nouns={['certification', 'certifications']}
onSort={(sorting) => reportCatalogTableSorted('certifications', sorting)}
diff --git a/products/data_modeling/frontend/ViewsTab.tsx b/products/data_modeling/frontend/ViewsTab.tsx
index feced2344b0e..24d7ec2e1b0b 100644
--- a/products/data_modeling/frontend/ViewsTab.tsx
+++ b/products/data_modeling/frontend/ViewsTab.tsx
@@ -19,6 +19,8 @@ import { DataWarehouseSavedQueryOrigin } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType, DataWarehouseSavedQueryRunHistory } from '~/types'
import { TableCertificationTag } from 'products/data_warehouse/frontend/shared/components/TableCertificationBadge'
+import { SuggestedModelsStrip } from 'products/warehouse_suggestions/frontend/components/SuggestedModelsStrip'
+import { WarehouseSuggestionKindEnumApi } from 'products/warehouse_suggestions/frontend/generated/api.schemas'
import { endpointModelUrl, parseEndpointModelName } from './endpointModelName'
import { NodeSuspensionApi } from './generated/api.schemas'
@@ -151,6 +153,7 @@ export function ViewsTab({ getViewUrl, suspensionByViewId }: ViewsTabProps = {})
runMaterialization,
openAccessControlModal,
closeAccessControlModal,
+ suggestionAccepted,
} = useActions(viewsTabLogic)
const warehouseAccessControlEnabled = !!featureFlags[FEATURE_FLAGS.HOGQL_WAREHOUSE_ACCESS_CONTROL]
@@ -304,6 +307,16 @@ export function ViewsTab({ getViewUrl, suspensionByViewId }: ViewsTabProps = {})
description="Control who can query this view. Users without access won't see it and queries referencing it will fail for them."
/>
) : null}
+ {featureFlags[FEATURE_FLAGS.WAREHOUSE_SUGGESTIONS] && (
+ <SuggestedModelsStrip
+ onAccepted={(suggestion) =>
+ suggestionAccepted(
+ suggestion.subject_id,
+ suggestion.kind === WarehouseSuggestionKindEnumApi.Materialize
+ )
+ }
+ />
+ )}
<div className="flex flex-wrap gap-2 items-center">
<LemonInput
type="search"
diff --git a/products/data_modeling/frontend/viewsTabLogic.test.ts b/products/data_modeling/frontend/viewsTabLogic.test.ts
index 67fd48678b7e..00fdc17b52ba 100644
--- a/products/data_modeling/frontend/viewsTabLogic.test.ts
+++ b/products/data_modeling/frontend/viewsTabLogic.test.ts
@@ -80,6 +80,62 @@ describe('viewsTabLogic', () => {
expect(logic.values.filteredViews.map((view) => view.id)).toEqual(['mat-10'])
})
+ it.each([
+ ['a materialize', 'plain-a', true],
+ ['an unmaterialize', 'mat-0', false],
+ ])('shows the new materialization after %s suggestion is accepted', async (_kind, viewId, isMaterialized) => {
+ useMocks({
+ get: {
+ '/api/projects/:team_id/warehouse_saved_queries/': {
+ count: 1,
+ results: [buildView(viewId, isMaterialized, isMaterialized ? 'Completed' : undefined)],
+ },
+ },
+ })
+
+ logic.actions.suggestionAccepted(viewId, isMaterialized)
+ await expectLogic(dataWarehouseViewsLogic).toFinishAllListeners()
+ logic.actions.setTypeFilter(isMaterialized ? 'materialized' : 'view')
+
+ expect(logic.values.filteredViews.map((view) => view.id)).toEqual([viewId])
+ })
+
+ it.each([
+ ['a materialize', 'view' as const, false],
+ ['an unmaterialize', 'materialized' as const, true],
+ ])(
+ 'moves back to the last page when %s empties the filtered page',
+ async (_kind, typeFilter, startsMaterialized) => {
+ const views = Array.from({ length: PAGE_SIZE + 1 }, (_, i) => buildView(`v-${i}`, startsMaterialized))
+ useMocks({
+ get: { '/api/projects/:team_id/warehouse_saved_queries/': { count: views.length, results: views } },
+ })
+ dataWarehouseViewsLogic.actions.loadDataWarehouseSavedQueries()
+ await expectLogic(dataWarehouseViewsLogic).toFinishAllListeners()
+ logic.actions.setTypeFilter(typeFilter)
+ logic.actions.setPage(2)
+ const flipped = views.map((view, i) => (i === PAGE_SIZE ? buildView(`v-${i}`, !startsMaterialized) : view))
+ useMocks({
+ get: {
+ '/api/projects/:team_id/warehouse_saved_queries/': { count: flipped.length, results: flipped },
+ },
+ })
+
+ logic.actions.suggestionAccepted(`v-${PAGE_SIZE}`, !startsMaterialized)
+ await expectLogic(dataWarehouseViewsLogic).toFinishAllListeners()
+
+ expect(logic.values.currentPage).toEqual(1)
+ expect(logic.values.visibleViews).toHaveLength(PAGE_SIZE)
+ }
+ )
+
+ it('keeps polling an accepted materialize until the view reports it is materialized', async () => {
+ logic.actions.suggestionAccepted('plain-a', true)
+ await expectLogic(dataWarehouseViewsLogic).toFinishAllListeners()
+
+ expect(dataWarehouseViewsLogic.values.materializingViewIds).toEqual(['plain-a'])
+ })
+
it('keeps name matches visible while lineage data reloads', () => {
lineageDataLogic.actions.loadNodes()
logic.actions.setSearchTerm('+mat-1')
diff --git a/products/data_modeling/frontend/viewsTabLogic.ts b/products/data_modeling/frontend/viewsTabLogic.ts
index f49d08909cef..73f5a24fe7b4 100644
--- a/products/data_modeling/frontend/viewsTabLogic.ts
+++ b/products/data_modeling/frontend/viewsTabLogic.ts
@@ -67,6 +67,10 @@ export interface viewsTabLogicActions {
fullRefresh: boolean | undefined
viewId: string
} // dataWarehouseViewsLogic
+ loadDataWarehouseSavedQueries: () => any // dataWarehouseViewsLogic
+ addMaterializingViews: (viewIds: string[]) => {
+ viewIds: string[]
+ } // dataWarehouseViewsLogic
loadDataWarehouseSavedQueriesSuccess: (
dataWarehouseSavedQueries: DataWarehouseSavedQuerySummary[],
payload?: any
@@ -127,6 +131,13 @@ export interface viewsTabLogicActions {
setTypeFilter: (typeFilter: ViewTypeFilter) => {
typeFilter: ViewTypeFilter
}
+ suggestionAccepted: (
+ viewId: string,
+ startsMaterializing: boolean
+ ) => {
+ viewId: string
+ startsMaterializing: boolean
+ }
}
export interface viewsTabLogicMeta {
@@ -172,7 +183,13 @@ export const viewsTabLogic = kea<viewsTabLogicType>([
],
actions: [
dataWarehouseViewsLogic,
- ['deleteDataWarehouseSavedQuery', 'runDataWarehouseSavedQuery', 'loadDataWarehouseSavedQueriesSuccess'],
+ [
+ 'deleteDataWarehouseSavedQuery',
+ 'runDataWarehouseSavedQuery',
+ 'loadDataWarehouseSavedQueries',
+ 'addMaterializingViews',
+ 'loadDataWarehouseSavedQueriesSuccess',
+ ],
databaseTableListLogic,
['loadDatabase'],
],
@@ -185,6 +202,7 @@ export const viewsTabLogic = kea<viewsTabLogicType>([
runMaterialization: (viewId: string) => ({ viewId }),
loadRunHistory: (viewIds: string[]) => ({ viewIds }),
loadVisibleData: true,
+ suggestionAccepted: (viewId: string, startsMaterializing: boolean) => ({ viewId, startsMaterializing }),
openAccessControlModal: (view: DataWarehouseSavedQuerySummary) => ({ view }),
closeAccessControlModal: true,
}),
@@ -349,7 +367,18 @@ export const viewsTabLogic = kea<viewsTabLogicType>([
runMaterialization: ({ viewId }) => {
actions.runDataWarehouseSavedQuery(viewId)
},
+ suggestionAccepted: ({ viewId, startsMaterializing }) => {
+ if (startsMaterializing) {
+ actions.addMaterializingViews([viewId])
+ }
+ actions.loadDataWarehouseSavedQueries()
+ },
loadDataWarehouseSavedQueriesSuccess: () => {
+ const lastPage = Math.max(1, Math.ceil(values.filteredViews.length / PAGE_SIZE))
+ if (values.currentPage > lastPage) {
+ actions.setPage(lastPage)
+ return
+ }
actions.loadVisibleData()
},
setPage: () => {
diff --git a/products/warehouse_suggestions/frontend/components/CertificationSuggestionRow.tsx b/products/warehouse_suggestions/frontend/components/CertificationSuggestionRow.tsx
new file mode 100644
index 000000000000..cab722821cd8
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/CertificationSuggestionRow.tsx
@@ -0,0 +1,104 @@
+import { useActions, useValues } from 'kea'
+
+import { IconCheckCircle, IconWarning } from '@posthog/icons'
+import { LemonButton, Link, Tooltip } from '@posthog/lemon-ui'
+
+import { userHasAccess } from 'lib/utils/accessControlUtils'
+
+import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
+import { AccessControlLevel, AccessControlResourceType } from '~/types'
+
+import type { WarehouseSuggestionApi } from '../generated/api.schemas'
+import { subjectUrl } from '../subjectUrl'
+import { daysReadLabel, isDeprecatePayload, noReadsLabel, readSummary, refreshCostLabel } from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { DismissSuggestionMenu } from './DismissSuggestionMenu'
+import { SuggestionRow } from './SuggestionRow'
+import { WhyThisSuggestion } from './WhyThisSuggestion'
+
+const EDIT_ACCESS_REASON = 'You need edit access to this view or table'
+const CATALOG_EDIT_ACCESS_REASON = 'You need edit access to the data catalog'
+
+export interface CertificationSuggestionRowProps {
+ suggestion: WarehouseSuggestionApi
+ windowDays: number
+}
+
+export function CertificationSuggestionRow({ suggestion, windowDays }: CertificationSuggestionRowProps): JSX.Element {
+ const { actionsInFlight, leavingSuggestionIds } = useValues(warehouseSuggestionsLogic)
+ const { acceptSuggestion, openAcceptConfirm } = useActions(warehouseSuggestionsLogic)
+ const { payload } = suggestion
+ const isTable = suggestion.subject_kind === 'table'
+ const leaving = leavingSuggestionIds.includes(suggestion.id)
+ const inFlight = !!actionsInFlight[suggestion.id]
+ const blockedReason = !suggestion.can_act ? EDIT_ACCESS_REASON : leaving ? 'Working' : undefined
+ const acceptBlockedReason =
+ blockedReason ??
+ (userHasAccess(AccessControlResourceType.DataCatalog, AccessControlLevel.Editor)
+ ? undefined
+ : CATALOG_EDIT_ACCESS_REASON)
+ const deprecating = isDeprecatePayload(payload)
+ const confirmsFirst = deprecating && !isTable
+
+ return (
+ <SuggestionRow
+ suggestionId={suggestion.id}
+ leaving={leaving}
+ icon={
+ <Tooltip title={isTable ? 'Table' : 'View'}>
+ <span className="flex" aria-label={isTable ? 'Table' : 'View'}>
+ {iconForType(isTable ? 'data_warehouse' : 'data_modeling')}
+ </span>
+ </Tooltip>
+ }
+ title={
+ <Link to={subjectUrl(suggestion)} subtle className="block min-w-0 truncate text-sm font-semibold">
+ {payload.subject_name}
+ </Link>
+ }
+ meta={
+ deprecating
+ ? [
+ <WhyThisSuggestion
+ key="reads"
+ evidence={suggestion.evidence}
+ windowDays={windowDays}
+ label={noReadsLabel(windowDays)}
+ />,
+ <span key="cost">{refreshCostLabel(payload)}</span>,
+ ]
+ : [
+ <WhyThisSuggestion
+ key="reads"
+ evidence={suggestion.evidence}
+ windowDays={windowDays}
+ label={readSummary(suggestion.evidence)}
+ />,
+ <span key="days">{daysReadLabel(suggestion.evidence, windowDays)}</span>,
+ ]
+ }
+ actions={
+ <>
+ <LemonButton
+ size="small"
+ type="secondary"
+ icon={deprecating ? <IconWarning /> : <IconCheckCircle />}
+ loading={inFlight}
+ disabledReason={acceptBlockedReason}
+ onClick={() =>
+ confirmsFirst ? openAcceptConfirm(suggestion.id) : acceptSuggestion(suggestion.id)
+ }
+ data-attr={`warehouse-suggestions-${suggestion.kind}`}
+ >
+ {deprecating ? 'Mark deprecated' : 'Certify'}
+ </LemonButton>
+ <DismissSuggestionMenu
+ suggestionId={suggestion.id}
+ kind={suggestion.kind}
+ disabledReason={blockedReason ?? (inFlight ? 'Working' : undefined)}
+ />
+ </>
+ }
+ />
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/DataAgeMeta.tsx b/products/warehouse_suggestions/frontend/components/DataAgeMeta.tsx
new file mode 100644
index 000000000000..247ad1872752
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/DataAgeMeta.tsx
@@ -0,0 +1,21 @@
+import { Tooltip } from '@posthog/lemon-ui'
+
+import type { DataAgeLabel } from '../suggestionCopy'
+
+export interface DataAgeMetaProps {
+ age: DataAgeLabel
+}
+
+export function DataAgeMeta({ age }: DataAgeMetaProps): JSX.Element {
+ if (!age.caveat) {
+ return <span translate="no">{age.label}</span>
+ }
+ return (
+ <Tooltip title={age.caveat}>
+ <span tabIndex={0} className="cursor-help underline decoration-dotted underline-offset-2">
+ <span translate="no">{age.label}</span>
+ <span className="sr-only">{`. ${age.caveat}`}</span>
+ </span>
+ </Tooltip>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/DeprecateSuggestionModal.tsx b/products/warehouse_suggestions/frontend/components/DeprecateSuggestionModal.tsx
new file mode 100644
index 000000000000..39910944fc65
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/DeprecateSuggestionModal.tsx
@@ -0,0 +1,81 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonButton, LemonModal } from '@posthog/lemon-ui'
+
+import { LemonRadio, LemonRadioOption } from 'lib/lemon-ui/LemonRadio'
+
+import { isDeprecatePayload, noReadsLabel, refreshCostLabel } from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+
+type RefreshChoice = 'stop' | 'keep'
+
+const REFRESH_CHOICES: LemonRadioOption<RefreshChoice>[] = [
+ {
+ value: 'stop',
+ label: 'Stop refreshing it',
+ description:
+ 'Deletes its stored table and stops its refreshes, if it still has them. PostHog keeps them when another view or a sync reads it. You can materialize it again later.',
+ 'data-attr': 'warehouse-suggestions-deprecate-stop-refreshing',
+ },
+ {
+ value: 'keep',
+ label: 'Keep refreshing it',
+ description: 'It stays materialized, and its refreshes keep their cost.',
+ 'data-attr': 'warehouse-suggestions-deprecate-keep-refreshing',
+ },
+]
+
+export function DeprecateSuggestionModal(): JSX.Element | null {
+ const { acceptConfirmSuggestion, actionsInFlight, keepMaterialized, status } = useValues(warehouseSuggestionsLogic)
+ const { confirmAccept, closeAcceptConfirm, setKeepMaterialized } = useActions(warehouseSuggestionsLogic)
+ const payload = acceptConfirmSuggestion?.payload
+
+ if (!acceptConfirmSuggestion || !payload || !isDeprecatePayload(payload)) {
+ return null
+ }
+ const inFlight = !!actionsInFlight[acceptConfirmSuggestion.id]
+
+ return (
+ <LemonModal
+ isOpen
+ closable={!inFlight}
+ onClose={closeAcceptConfirm}
+ title={`Mark ${payload.subject_name} as deprecated?`}
+ footer={
+ <>
+ <LemonButton
+ type="secondary"
+ onClick={closeAcceptConfirm}
+ disabledReason={inFlight ? 'Working' : undefined}
+ >
+ Cancel
+ </LemonButton>
+ <LemonButton
+ type="primary"
+ loading={inFlight}
+ onClick={() => confirmAccept()}
+ data-attr="warehouse-suggestions-deprecate-confirm"
+ >
+ Mark deprecated
+ </LemonButton>
+ </>
+ }
+ >
+ <div className="flex max-w-xl flex-col gap-4">
+ <p className="m-0 text-sm text-secondary">
+ {`${noReadsLabel(status?.window_days ?? 0)}. ${refreshCostLabel(payload)}.`}
+ </p>
+ <fieldset disabled={inFlight} className="flex flex-col gap-2">
+ <h4 className="m-0 text-sm font-semibold">Its materialization</h4>
+ <LemonRadio
+ aria-label="Its materialization"
+ value={keepMaterialized ? 'keep' : 'stop'}
+ onChange={(choice) => setKeepMaterialized(choice === 'keep')}
+ options={REFRESH_CHOICES}
+ radioPosition="top"
+ />
+ </fieldset>
+ </div>
+ </LemonModal>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/DismissSuggestionMenu.tsx b/products/warehouse_suggestions/frontend/components/DismissSuggestionMenu.tsx
new file mode 100644
index 000000000000..6570efbc3f8f
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/DismissSuggestionMenu.tsx
@@ -0,0 +1,41 @@
+import { useActions } from 'kea'
+
+import { LemonButton, LemonMenu } from '@posthog/lemon-ui'
+
+import { notNowCondition } from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+
+export interface DismissSuggestionMenuProps {
+ suggestionId: string
+ kind: string
+ disabledReason?: string
+}
+
+export function DismissSuggestionMenu({ suggestionId, kind, disabledReason }: DismissSuggestionMenuProps): JSX.Element {
+ const { dismissSuggestion } = useActions(warehouseSuggestionsLogic)
+
+ return (
+ <LemonMenu
+ items={[
+ {
+ label: "Not useful. Won't be suggested again.",
+ onClick: () => dismissSuggestion(suggestionId, 'not_useful'),
+ },
+ {
+ label: `Not now. ${notNowCondition(kind)}`,
+ onClick: () => dismissSuggestion(suggestionId, 'not_now'),
+ },
+ { label: 'Something else', onClick: () => dismissSuggestion(suggestionId, 'other') },
+ ]}
+ >
+ <LemonButton
+ size="small"
+ type="tertiary"
+ disabledReason={disabledReason}
+ data-attr="warehouse-suggestions-dismiss"
+ >
+ Dismiss
+ </LemonButton>
+ </LemonMenu>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/MaterializeSuggestionModal.tsx b/products/warehouse_suggestions/frontend/components/MaterializeSuggestionModal.tsx
new file mode 100644
index 000000000000..bfdff84c3c42
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/MaterializeSuggestionModal.tsx
@@ -0,0 +1,127 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonBanner, LemonButton, LemonModal } from '@posthog/lemon-ui'
+
+import {
+ INCREMENTAL_CHECK_LOADING,
+ IncrementalConfigOptions,
+} from 'scenes/data-warehouse/editor/IncrementalConfigFields'
+import { CADENCE_LABELS, SyncFrequencySelect } from 'scenes/data-warehouse/saved_queries/SyncFrequencySelect'
+
+import { INTERVAL_SECONDS, intervalForSeconds } from '../refreshIntervals'
+import {
+ dataAgeLabel,
+ isMaterializePayload,
+ materializeSaving,
+ refreshModeLabel,
+ savingSentence,
+ shorterScheduleWarning,
+ suggestedScheduleHint,
+ todayAgeLabel,
+} from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { DataAgeMeta } from './DataAgeMeta'
+
+export function MaterializeSuggestionModal(): JSX.Element | null {
+ const {
+ materializeModalSuggestion,
+ materializeError,
+ materializeInterval,
+ materializeIntervalShorterThanSuggested,
+ materializeDisabledReason,
+ actionsInFlight,
+ refreshDraft,
+ incrementalCheck,
+ incrementalCheckLoading,
+ } = useValues(warehouseSuggestionsLogic)
+ const { confirmMaterialize, closeMaterializeModal, setMaterializeInterval, setRefreshDraft } =
+ useActions(warehouseSuggestionsLogic)
+ const payload = materializeModalSuggestion?.payload
+
+ if (!materializeModalSuggestion || !payload || !isMaterializePayload(payload)) {
+ return null
+ }
+ const inFlight = !!actionsInFlight[materializeModalSuggestion.id]
+ const suggestedInterval = intervalForSeconds(payload.refresh_interval_seconds)
+ const suggestedCadence = suggestedInterval ? CADENCE_LABELS[suggestedInterval] : null
+ const pickedSeconds = materializeInterval ? INTERVAL_SECONDS[materializeInterval] : payload.refresh_interval_seconds
+ const shorterWarning =
+ materializeIntervalShorterThanSuggested && suggestedCadence
+ ? shorterScheduleWarning(payload.refresh_interval_basis, suggestedCadence)
+ : null
+
+ return (
+ <LemonModal
+ isOpen
+ closable={!inFlight}
+ onClose={closeMaterializeModal}
+ title={`Materialize ${payload.subject_name}`}
+ footer={
+ <>
+ <LemonButton
+ type="secondary"
+ onClick={closeMaterializeModal}
+ disabledReason={inFlight ? 'Working' : undefined}
+ >
+ Cancel
+ </LemonButton>
+ <LemonButton
+ type="primary"
+ loading={inFlight}
+ disabledReason={materializeDisabledReason ?? undefined}
+ onClick={() => confirmMaterialize()}
+ data-attr="warehouse-suggestions-materialize-confirm"
+ >
+ Materialize
+ </LemonButton>
+ </>
+ }
+ >
+ <div className="flex max-w-xl flex-col gap-4">
+ <p className="m-0 text-sm text-secondary">
+ Stores the result and refreshes it on a schedule. Reads of this view use the stored rows.
+ </p>
+ <div className="flex flex-col gap-1.5">
+ <SyncFrequencySelect
+ value={materializeInterval}
+ onChange={setMaterializeInterval}
+ loading={inFlight}
+ />
+ {suggestedCadence && (
+ <span className="text-xs text-secondary">
+ {suggestedScheduleHint(payload.refresh_interval_basis, suggestedCadence)}
+ </span>
+ )}
+ </div>
+ <fieldset disabled={inFlight} className="flex flex-col">
+ <h4 className="m-0 text-sm font-semibold">Refresh mode</h4>
+ <IncrementalConfigOptions
+ check={incrementalCheckLoading ? INCREMENTAL_CHECK_LOADING : incrementalCheck}
+ draft={refreshDraft}
+ onChange={setRefreshDraft}
+ />
+ </fieldset>
+ <div className="grid grid-cols-1 gap-3 rounded border border-primary px-3 py-2.5">
+ <div className="flex flex-col gap-0.5">
+ <span className="text-xs font-semibold text-secondary">With these settings</span>
+ <span className="text-sm">
+ <DataAgeMeta age={dataAgeLabel(payload, pickedSeconds)} />
+ </span>
+ <span className="text-xs text-secondary">{todayAgeLabel(payload)}</span>
+ <span className="text-xs text-secondary">
+ {refreshModeLabel(refreshDraft.enabled, refreshDraft.incrementalKey)}
+ </span>
+ </div>
+ <div className="flex flex-col gap-0.5">
+ <span className="text-xs font-semibold text-secondary">
+ Estimated at the suggested settings
+ </span>
+ <span className="text-sm">{savingSentence(materializeSaving(payload))}</span>
+ </div>
+ </div>
+ {shorterWarning && <LemonBanner type="warning">{shorterWarning}</LemonBanner>}
+ {materializeError && <LemonBanner type="error">{materializeError}</LemonBanner>}
+ </div>
+ </LemonModal>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/MaterializeSuggestionRow.tsx b/products/warehouse_suggestions/frontend/components/MaterializeSuggestionRow.tsx
new file mode 100644
index 000000000000..53c79c5293ba
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/MaterializeSuggestionRow.tsx
@@ -0,0 +1,88 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonButton, Link } from '@posthog/lemon-ui'
+
+import { urls } from 'scenes/urls'
+
+import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
+
+import type { WarehouseSuggestionApi } from '../generated/api.schemas'
+import { dataAgeLabel, isMaterializePayload, materializeSaving, readSummary, savingHighlight } from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { DataAgeMeta } from './DataAgeMeta'
+import { DismissSuggestionMenu } from './DismissSuggestionMenu'
+import { SuggestionRow } from './SuggestionRow'
+import { WhyThisSuggestion } from './WhyThisSuggestion'
+
+const EDIT_ACCESS_REASON = 'You need edit access to this view'
+
+export interface MaterializeSuggestionRowProps {
+ suggestion: WarehouseSuggestionApi
+ windowDays: number
+}
+
+export function MaterializeSuggestionRow({
+ suggestion,
+ windowDays,
+}: MaterializeSuggestionRowProps): JSX.Element | null {
+ const { actionsInFlight, leavingSuggestionIds } = useValues(warehouseSuggestionsLogic)
+ const { openMaterializeModal } = useActions(warehouseSuggestionsLogic)
+ const { payload } = suggestion
+ if (!isMaterializePayload(payload)) {
+ return null
+ }
+ const leaving = leavingSuggestionIds.includes(suggestion.id)
+ const blockedReason = !suggestion.can_act
+ ? EDIT_ACCESS_REASON
+ : actionsInFlight[suggestion.id] || leaving
+ ? 'Working'
+ : undefined
+
+ return (
+ <SuggestionRow
+ suggestionId={suggestion.id}
+ leaving={leaving}
+ icon={iconForType('data_modeling')}
+ title={
+ <Link
+ to={urls.sqlEditor({ view_id: suggestion.subject_id })}
+ subtle
+ className="block min-w-0 truncate text-sm font-semibold"
+ >
+ {payload.subject_name}
+ </Link>
+ }
+ meta={[
+ <WhyThisSuggestion
+ key="reads"
+ evidence={suggestion.evidence}
+ windowDays={windowDays}
+ label={readSummary(suggestion.evidence)}
+ />,
+ <DataAgeMeta key="age" age={dataAgeLabel(payload, payload.refresh_interval_seconds)} />,
+ ...(payload.refresh_mode === 'incremental'
+ ? [<span key="mode">{`Incremental by ${payload.incremental_key ?? 'new rows'}`}</span>]
+ : []),
+ ]}
+ highlight={savingHighlight(materializeSaving(payload))}
+ actions={
+ <>
+ <LemonButton
+ size="small"
+ type="secondary"
+ disabledReason={blockedReason}
+ onClick={() => openMaterializeModal(suggestion.id)}
+ data-attr="warehouse-suggestions-materialize"
+ >
+ Materialize
+ </LemonButton>
+ <DismissSuggestionMenu
+ suggestionId={suggestion.id}
+ kind={suggestion.kind}
+ disabledReason={blockedReason}
+ />
+ </>
+ }
+ />
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/ReadsBySurfaceBar.tsx b/products/warehouse_suggestions/frontend/components/ReadsBySurfaceBar.tsx
new file mode 100644
index 000000000000..a84af30e9da7
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/ReadsBySurfaceBar.tsx
@@ -0,0 +1,46 @@
+import type { SurfaceSegment } from '../suggestionCopy'
+
+export interface ReadsBySurfaceBarProps {
+ segments: SurfaceSegment[]
+}
+
+function segmentColor(index: number): string {
+ return `var(--data-color-${index + 1})`
+}
+
+export function ReadsBySurfaceBar({ segments }: ReadsBySurfaceBarProps): JSX.Element {
+ const total = segments.reduce((sum, segment) => sum + segment.count, 0)
+ const description = segments.map((segment) => `${segment.label} ${segment.count}`).join(', ')
+
+ return (
+ <div className="flex flex-col gap-1.5">
+ <div
+ role="img"
+ aria-label={`Reads by surface: ${description}`}
+ className="flex h-2 w-full overflow-hidden rounded-full bg-fill-secondary"
+ >
+ {segments.map((segment, index) => (
+ <div
+ key={segment.key}
+ style={{ width: `${(segment.count / total) * 100}%`, backgroundColor: segmentColor(index) }}
+ />
+ ))}
+ </div>
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-1 text-xs">
+ {segments.map((segment, index) => (
+ <span key={segment.key} className="inline-flex items-center gap-1">
+ <span
+ aria-hidden
+ className="inline-block size-2 rounded-sm"
+ style={{ backgroundColor: segmentColor(index) }}
+ />
+ <span>{segment.label}</span>
+ <span translate="no" className="font-semibold tabular-nums">
+ {segment.count}
+ </span>
+ </span>
+ ))}
+ </div>
+ </div>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SavingsBannerMessage.tsx b/products/warehouse_suggestions/frontend/components/SavingsBannerMessage.tsx
new file mode 100644
index 000000000000..70958eeb2824
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SavingsBannerMessage.tsx
@@ -0,0 +1,38 @@
+import { humanizeBytes } from 'lib/utils/numbers'
+
+import { SavingsSummary, countOf, spokenDuration } from '../suggestionCopy'
+
+export interface SavingsBannerMessageProps {
+ summary: SavingsSummary
+}
+
+export function SavingsBannerMessage({ summary }: SavingsBannerMessageProps): JSX.Element {
+ const views = summary.timeViews + summary.bytesViews
+ const time = summary.timeViews > 0 ? `~${spokenDuration(summary.savedSeconds, 1)}` : null
+ const bytes = summary.bytesViews > 0 ? humanizeBytes(summary.savedBytes) : null
+
+ return (
+ <>
+ <strong translate="no">{countOf(views, 'view', 'views')}</strong>
+ <span> could save </span>
+ {time && (
+ <>
+ <strong translate="no" className="text-success">
+ {time}
+ </strong>
+ <span> of query time</span>
+ </>
+ )}
+ {time && bytes && <span> and </span>}
+ {bytes && (
+ <>
+ <strong translate="no" className="text-success">
+ {bytes}
+ </strong>
+ <span> of scanning</span>
+ </>
+ )}
+ <span> a month</span>
+ </>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.stories.tsx b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.stories.tsx
new file mode 100644
index 000000000000..b9d67ee219ca
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.stories.tsx
@@ -0,0 +1,151 @@
+import type { Meta, StoryObj } from '@storybook/react'
+import { useValues } from 'kea'
+import { useEffect } from 'react'
+
+import { useStorybookMocks } from '~/mocks/browser'
+
+import type { WarehouseSuggestionApi, WarehouseSuggestionStatusApi } from '../generated/api.schemas'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { SuggestedCertificationsTable } from './SuggestedCertificationsTable'
+
+const LIST_URL = '/api/projects/:team_id/warehouse_suggestions/'
+const STATUS_URL = '/api/projects/:team_id/warehouse_suggestions/status/'
+
+const status: WarehouseSuggestionStatusApi = {
+ enabled: true,
+ eligible: true,
+ days_with_data: 30,
+ window_days: 30,
+ paused_reason: null,
+ refreshed_at: '2026-10-07T08:30:00Z',
+}
+
+function suggestion(
+ id: string,
+ kind: 'certify' | 'deprecate',
+ subjectKind: 'saved_query' | 'table',
+ name: string,
+ canAct = true
+): WarehouseSuggestionApi {
+ return {
+ id,
+ kind,
+ subject_kind: subjectKind,
+ subject_id: `subject-${id}`,
+ payload:
+ kind === 'certify'
+ ? { subject_name: name }
+ : { subject_name: name, refresh_seconds_per_month: 5400, refresh_bytes_per_month: 2 * 10 ** 12 },
+ payload_version: 1,
+ evidence: {
+ human_requests: kind === 'certify' ? 640 : 0,
+ human_users: kind === 'certify' ? 9 : 0,
+ human_days: kind === 'certify' ? 28 : 0,
+ requests_by_surface: kind === 'certify' ? { dashboard: 400, mcp: 240 } : {},
+ },
+ evidence_window_start: '2026-09-07T00:00:00Z',
+ evidence_window_end: '2026-10-07T00:00:00Z',
+ last_seen_at: '2026-10-07T08:30:00Z',
+ status: 'proposed',
+ surfaced_at: '2026-10-07T08:30:00Z',
+ reviewed_by: null,
+ reviewed_at: null,
+ dismissal_reason: null,
+ dismissal_note: null,
+ created_asset: null,
+ asset_outcome: null,
+ realized_seconds_saved_per_month: null,
+ realized_bytes_saved_per_month: null,
+ can_act: canAct,
+ }
+}
+
+interface TableStoryProps {
+ results: WarehouseSuggestionApi[]
+ width?: 'normal' | 'narrow'
+ reviewOpen?: boolean
+ confirmSuggestionId?: string
+}
+
+function TableStory({ results, width, reviewOpen = false, confirmSuggestionId }: TableStoryProps): JSX.Element {
+ useStorybookMocks({
+ get: {
+ [LIST_URL]: { count: results.length, results },
+ [STATUS_URL]: status,
+ },
+ })
+ const logic = warehouseSuggestionsLogic({ surface: 'catalog' })
+ const { stripState } = useValues(logic)
+ useEffect(() => {
+ if (stripState !== 'active') {
+ return
+ }
+ if (reviewOpen || confirmSuggestionId) {
+ logic.actions.openReview()
+ }
+ if (confirmSuggestionId) {
+ logic.actions.openAcceptConfirm(confirmSuggestionId)
+ }
+ }, [reviewOpen, confirmSuggestionId, stripState, logic])
+ return (
+ <div className={width === 'narrow' ? 'w-[520px] p-4' : 'w-[1000px] p-4'}>
+ <SuggestedCertificationsTable />
+ </div>
+ )
+}
+
+const meta: Meta<typeof TableStory> = {
+ title: 'Warehouse suggestions/Suggested certifications',
+ component: TableStory,
+ parameters: { layout: 'padded', mockDate: '2026-10-07T10:30:00Z' },
+}
+export default meta
+
+type Story = StoryObj<typeof TableStory>
+
+const dialogSnapshot = (width: number): Story['parameters'] => ({
+ layout: 'fullscreen',
+ testOptions: { viewport: { width, height: 900 } },
+})
+
+export const Banner: Story = {
+ args: {
+ results: [
+ suggestion('1', 'certify', 'saved_query', 'customer_monthly_revenue'),
+ suggestion('2', 'certify', 'table', 'stripe_charges'),
+ suggestion('3', 'deprecate', 'saved_query', 'old_marketing_rollup', false),
+ ],
+ },
+}
+
+export const ReviewDialog: Story = {
+ args: {
+ reviewOpen: true,
+ results: [
+ suggestion('1', 'certify', 'saved_query', 'customer_monthly_revenue'),
+ suggestion('2', 'certify', 'table', 'stripe_charges'),
+ suggestion('3', 'deprecate', 'saved_query', 'old_marketing_rollup', false),
+ ],
+ },
+ parameters: dialogSnapshot(1300),
+}
+
+export const ReviewDialogNarrow: Story = {
+ args: {
+ width: 'narrow',
+ reviewOpen: true,
+ results: [
+ suggestion('1', 'certify', 'saved_query', 'customer_monthly_revenue_by_region_and_plan'),
+ suggestion('3', 'deprecate', 'saved_query', 'old_marketing_rollup'),
+ ],
+ },
+ parameters: dialogSnapshot(520),
+}
+
+export const DeprecateDialog: Story = {
+ args: {
+ confirmSuggestionId: '3',
+ results: [suggestion('3', 'deprecate', 'saved_query', 'old_marketing_rollup')],
+ },
+ parameters: dialogSnapshot(1300),
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.tsx b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.tsx
new file mode 100644
index 000000000000..c617f11c2223
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTable.tsx
@@ -0,0 +1,21 @@
+import { BindLogic } from 'kea'
+
+import { WarehouseSuggestionsLogicProps, warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { SuggestedCertificationsTableContent } from './SuggestedCertificationsTableContent'
+
+export interface SuggestedCertificationsTableProps {
+ onAccepted?: WarehouseSuggestionsLogicProps['onAccepted']
+ onViewAccepted?: WarehouseSuggestionsLogicProps['onViewAccepted']
+}
+
+export function SuggestedCertificationsTable({
+ onAccepted,
+ onViewAccepted,
+}: SuggestedCertificationsTableProps): JSX.Element | null {
+ const logicProps: WarehouseSuggestionsLogicProps = { surface: 'catalog', onAccepted, onViewAccepted }
+ return (
+ <BindLogic logic={warehouseSuggestionsLogic} props={logicProps}>
+ <SuggestedCertificationsTableContent />
+ </BindLogic>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTableContent.tsx b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTableContent.tsx
new file mode 100644
index 000000000000..60700082db9e
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedCertificationsTableContent.tsx
@@ -0,0 +1,68 @@
+import { useActions, useValues } from 'kea'
+
+import { IconWarning } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
+
+import { certificationSummaryText, countOf } from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { CertificationSuggestionRow } from './CertificationSuggestionRow'
+import { DeprecateSuggestionModal } from './DeprecateSuggestionModal'
+import { SuggestionsBanner } from './SuggestionsBanner'
+import { SuggestionsReviewModal } from './SuggestionsReviewModal'
+import { SuggestionsStatusNotice } from './SuggestionsStatusNotice'
+
+export function SuggestedCertificationsTableContent(): JSX.Element | null {
+ const { stripState, listedSuggestions, surfaceSuggestions, status } = useValues(warehouseSuggestionsLogic)
+ const { reload } = useActions(warehouseSuggestionsLogic)
+ const windowDays = status?.window_days ?? 0
+
+ if (stripState === 'error') {
+ return (
+ <SuggestionsStatusNotice
+ icon={<IconWarning className="text-warning" />}
+ message="Couldn't load certification suggestions."
+ extra={
+ <LemonButton size="xsmall" type="secondary" onClick={() => reload()}>
+ Retry
+ </LemonButton>
+ }
+ />
+ )
+ }
+ if (stripState !== 'active' && stripState !== 'all_done') {
+ return null
+ }
+
+ return (
+ <>
+ {stripState === 'active' && (
+ <SuggestionsBanner
+ icon={iconForType('data_catalog')}
+ message={
+ <>
+ <strong translate="no">
+ {countOf(
+ surfaceSuggestions.length,
+ 'certification suggestion',
+ 'certification suggestions'
+ )}
+ </strong>
+ <span translate="no">{`: ${certificationSummaryText(surfaceSuggestions)}`}</span>
+ </>
+ }
+ />
+ )}
+ <SuggestionsReviewModal
+ title="Certification suggestions"
+ summary={certificationSummaryText(surfaceSuggestions)}
+ >
+ {listedSuggestions.map((suggestion) => (
+ <CertificationSuggestionRow key={suggestion.id} suggestion={suggestion} windowDays={windowDays} />
+ ))}
+ </SuggestionsReviewModal>
+ <DeprecateSuggestionModal />
+ </>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.stories.tsx b/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.stories.tsx
new file mode 100644
index 000000000000..fee5af17f905
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.stories.tsx
@@ -0,0 +1,234 @@
+import type { Meta, StoryObj } from '@storybook/react'
+import { useValues } from 'kea'
+import { useEffect } from 'react'
+
+import { useStorybookMocks } from '~/mocks/browser'
+import { DataModelingSyncInterval } from '~/types'
+
+import type { WarehouseSuggestionApi, WarehouseSuggestionStatusApi } from '../generated/api.schemas'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { SuggestedModelsStrip } from './SuggestedModelsStrip'
+
+const LIST_URL = '/api/projects/:team_id/warehouse_suggestions/'
+const SAVED_QUERY_URL = '/api/projects/:team_id/warehouse_saved_queries/:id/'
+const CHECK_INCREMENTAL_URL = '/api/projects/:team_id/warehouse_saved_queries/check_incremental/'
+const STATUS_URL = '/api/projects/:team_id/warehouse_suggestions/status/'
+
+const status: WarehouseSuggestionStatusApi = {
+ enabled: true,
+ eligible: true,
+ days_with_data: 30,
+ window_days: 30,
+ paused_reason: null,
+ refreshed_at: '2026-10-07T08:30:00Z',
+}
+
+interface MaterializeOptions {
+ canAct?: boolean
+ unknownSources?: boolean
+ incremental?: boolean
+}
+
+function materialize(
+ id: string,
+ name: string,
+ savedSeconds: number,
+ { canAct = true, unknownSources = false, incremental = false }: MaterializeOptions = {}
+): WarehouseSuggestionApi {
+ return {
+ id,
+ kind: 'materialize',
+ subject_kind: 'saved_query',
+ subject_id: `view-${id}`,
+ payload: {
+ subject_name: name,
+ refresh_interval_seconds: 86400,
+ saves_seconds_per_month: savedSeconds,
+ saves_bytes_per_month: 3 * 10 ** 12,
+ freshness_today_seconds: 3600,
+ freshness_after_seconds: 90000,
+ live_sources: { names: ['events'], hidden_count: 0 },
+ unknown_sources: { names: [], hidden_count: unknownSources ? 1 : 0 },
+ refresh_interval_basis: 'source_sync',
+ ...(incremental
+ ? {
+ refresh_mode: 'incremental',
+ incremental_key: 'day',
+ unique_key: ['day', 'plan'],
+ lookback_seconds: 3600,
+ }
+ : { refresh_mode: 'full' }),
+ },
+ payload_version: 1,
+ evidence: {
+ human_requests: 214,
+ human_users: 6,
+ human_days: 27,
+ requests_by_surface: { sql_editor: 120, dashboard: 60, api: 34 },
+ },
+ evidence_window_start: '2026-09-07T00:00:00Z',
+ evidence_window_end: '2026-10-07T00:00:00Z',
+ last_seen_at: '2026-10-07T08:30:00Z',
+ status: 'proposed',
+ surfaced_at: '2026-10-07T08:30:00Z',
+ reviewed_by: null,
+ reviewed_at: null,
+ dismissal_reason: null,
+ dismissal_note: null,
+ created_asset: null,
+ asset_outcome: null,
+ realized_seconds_saved_per_month: null,
+ realized_bytes_saved_per_month: null,
+ can_act: canAct,
+ }
+}
+
+function unmaterialize(id: string, name: string): WarehouseSuggestionApi {
+ const base = materialize(id, name, 0)
+ return {
+ ...base,
+ kind: 'unmaterialize',
+ payload: {
+ subject_name: name,
+ refresh_seconds_per_month: 5400,
+ refresh_bytes_per_month: 4 * 10 ** 12,
+ live_seconds_per_month: 900,
+ live_bytes_per_month: 10 ** 12,
+ read_seconds: 3.6,
+ },
+ evidence: { human_requests: 9, human_users: 2, human_days: 6, requests_by_surface: { sql_editor: 9 } },
+ }
+}
+
+const suggestions = [
+ materialize('1', 'customer_monthly_revenue', 1080),
+ materialize('2', 'sessions_by_plan_daily', 2400),
+ materialize('3', 'stripe_invoices_enriched', 0, { unknownSources: true }),
+ unmaterialize('4', 'legacy_signup_funnel'),
+]
+
+const manySuggestions = Array.from({ length: 7 }, (_, index) =>
+ materialize(`${index + 1}`, `product_usage_rollup_${index + 1}`, 600 * (index + 1))
+)
+
+interface StripStoryProps {
+ results?: WarehouseSuggestionApi[]
+ statusOverrides?: Partial<WarehouseSuggestionStatusApi>
+ reviewOpen?: boolean
+ failing?: boolean
+ width?: 'normal' | 'narrow'
+ openDialogWithInterval?: DataModelingSyncInterval
+ confirmSuggestionId?: string
+}
+
+function StripStory({
+ results = suggestions,
+ statusOverrides,
+ reviewOpen = false,
+ failing,
+ width,
+ openDialogWithInterval,
+ confirmSuggestionId,
+}: StripStoryProps): JSX.Element {
+ useStorybookMocks({
+ get: {
+ [LIST_URL]: () => (failing ? [500, { detail: 'Server error' }] : { count: results.length, results }),
+ [STATUS_URL]: { ...status, ...statusOverrides },
+ [SAVED_QUERY_URL]: {
+ id: 'view-1',
+ query: { kind: 'HogQLQuery', query: 'SELECT day, plan, count() FROM events GROUP BY day, plan' },
+ },
+ },
+ post: {
+ [CHECK_INCREMENTAL_URL]: {
+ eligible: true,
+ key_candidates: ['day'],
+ unique_key_candidates: ['day', 'plan'],
+ key_candidate_types: { day: 'date', plan: 'string' },
+ blockers: [],
+ warnings: [],
+ },
+ },
+ })
+ const logic = warehouseSuggestionsLogic({ surface: 'models' })
+ const { stripState } = useValues(logic)
+ useEffect(() => {
+ if (stripState !== 'active') {
+ return
+ }
+ if (reviewOpen || openDialogWithInterval || confirmSuggestionId) {
+ logic.actions.openReview()
+ }
+ if (confirmSuggestionId) {
+ logic.actions.openAcceptConfirm(confirmSuggestionId)
+ }
+ if (!openDialogWithInterval) {
+ return
+ }
+ logic.actions.openMaterializeModal(results[0].id)
+ logic.actions.setMaterializeInterval(openDialogWithInterval)
+ }, [reviewOpen, openDialogWithInterval, confirmSuggestionId, stripState, logic, results])
+
+ return (
+ <div className={width === 'narrow' ? 'w-[520px] p-4' : 'w-[1000px] p-4'}>
+ <SuggestedModelsStrip />
+ </div>
+ )
+}
+
+const meta: Meta<typeof StripStory> = {
+ title: 'Warehouse suggestions/Suggested models strip',
+ component: StripStory,
+ parameters: { layout: 'padded', mockDate: '2026-10-07T10:30:00Z' },
+}
+export default meta
+
+type Story = StoryObj<typeof StripStory>
+
+const DIALOG_SNAPSHOT: Story['parameters'] = {
+ layout: 'fullscreen',
+ testOptions: { viewport: { width: 1300, height: 900 } },
+}
+
+export const Banner: Story = {}
+
+export const BannerNarrow: Story = { args: { width: 'narrow' } }
+
+export const ReviewDialog: Story = { args: { reviewOpen: true }, parameters: DIALOG_SNAPSHOT }
+
+export const ReviewDialogViewerWithoutEditAccess: Story = {
+ args: { reviewOpen: true, results: [materialize('1', 'customer_monthly_revenue', 1080, { canAct: false })] },
+ parameters: DIALOG_SNAPSHOT,
+}
+
+export const WarmingUp: Story = { args: { results: [], statusOverrides: { days_with_data: 12 } } }
+
+export const NotEligible: Story = { args: { results: [], statusOverrides: { eligible: false } } }
+
+export const LoadFailed: Story = { args: { failing: true } }
+
+export const ReviewDialogManySuggestions: Story = {
+ args: { reviewOpen: true, results: manySuggestions },
+ parameters: DIALOG_SNAPSHOT,
+}
+
+export const MaterializeDialogLongerScheduleUnknownSources: Story = {
+ args: {
+ results: [materialize('3', 'stripe_invoices_enriched', 1080, { unknownSources: true })],
+ openDialogWithInterval: '7day',
+ },
+ parameters: DIALOG_SNAPSHOT,
+}
+
+export const MaterializeDialogIncrementalProposal: Story = {
+ args: {
+ results: [materialize('1', 'sessions_by_plan_daily', 2400, { incremental: true })],
+ openDialogWithInterval: '24hour',
+ },
+ parameters: DIALOG_SNAPSHOT,
+}
+
+export const StopMaterializingDialog: Story = {
+ args: { confirmSuggestionId: '4' },
+ parameters: DIALOG_SNAPSHOT,
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.tsx b/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.tsx
new file mode 100644
index 000000000000..04e6ae133243
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedModelsStrip.tsx
@@ -0,0 +1,17 @@
+import { BindLogic } from 'kea'
+
+import { WarehouseSuggestionsLogicProps, warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { SuggestedModelsStripContent } from './SuggestedModelsStripContent'
+
+export interface SuggestedModelsStripProps {
+ onAccepted?: WarehouseSuggestionsLogicProps['onAccepted']
+}
+
+export function SuggestedModelsStrip({ onAccepted }: SuggestedModelsStripProps): JSX.Element | null {
+ const logicProps: WarehouseSuggestionsLogicProps = { surface: 'models', onAccepted }
+ return (
+ <BindLogic logic={warehouseSuggestionsLogic} props={logicProps}>
+ <SuggestedModelsStripContent />
+ </BindLogic>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestedModelsStripContent.tsx b/products/warehouse_suggestions/frontend/components/SuggestedModelsStripContent.tsx
new file mode 100644
index 000000000000..9e1160c1bd6d
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestedModelsStripContent.tsx
@@ -0,0 +1,95 @@
+import { useActions, useValues } from 'kea'
+
+import { IconLightBulb, IconWarning } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { LemonProgress } from 'lib/lemon-ui/LemonProgress'
+
+import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
+
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { MaterializeSuggestionModal } from './MaterializeSuggestionModal'
+import { MaterializeSuggestionRow } from './MaterializeSuggestionRow'
+import { SavingsBannerMessage } from './SavingsBannerMessage'
+import { SuggestionsBanner } from './SuggestionsBanner'
+import { SuggestionsReviewModal } from './SuggestionsReviewModal'
+import { SuggestionsStatusNotice } from './SuggestionsStatusNotice'
+import { UnmaterializeSuggestionModal } from './UnmaterializeSuggestionModal'
+import { UnmaterializeSuggestionRow } from './UnmaterializeSuggestionRow'
+
+export function SuggestedModelsStripContent(): JSX.Element | null {
+ const { stripState, listedSuggestions, savingsSummary, status } = useValues(warehouseSuggestionsLogic)
+ const { reload } = useActions(warehouseSuggestionsLogic)
+ const windowDays = status?.window_days ?? 0
+
+ if (stripState === 'error') {
+ return (
+ <SuggestionsStatusNotice
+ icon={<IconWarning className="text-warning" />}
+ message="Couldn't load suggested models."
+ extra={
+ <LemonButton size="xsmall" type="secondary" onClick={() => reload()}>
+ Retry
+ </LemonButton>
+ }
+ />
+ )
+ }
+ if (stripState === 'warming_up') {
+ const daysWithData = status?.days_with_data ?? 0
+ return (
+ <SuggestionsStatusNotice
+ icon={<IconLightBulb />}
+ message="Suggested models get better as read history builds up."
+ extra={
+ <span className="flex shrink-0 items-center gap-2 whitespace-nowrap text-xs text-secondary">
+ <span className="w-24 shrink-0">
+ <LemonProgress percent={windowDays ? (daysWithData / windowDays) * 100 : 0} />
+ </span>
+ <span translate="no" className="tabular-nums">{`${daysWithData} of ${windowDays} days`}</span>
+ </span>
+ }
+ />
+ )
+ }
+ if (stripState === 'not_eligible') {
+ return (
+ <SuggestionsStatusNotice
+ icon={<IconLightBulb />}
+ message="Suggested models start once people in this project read its views regularly."
+ />
+ )
+ }
+ if (stripState !== 'active' && stripState !== 'all_done') {
+ return null
+ }
+
+ return (
+ <>
+ {stripState === 'active' && (
+ <SuggestionsBanner
+ icon={iconForType('data_modeling')}
+ message={<SavingsBannerMessage summary={savingsSummary} />}
+ />
+ )}
+ <SuggestionsReviewModal
+ title="Suggested models"
+ summary={<SavingsBannerMessage summary={savingsSummary} />}
+ >
+ {listedSuggestions.map((suggestion) =>
+ suggestion.kind === 'unmaterialize' ? (
+ <UnmaterializeSuggestionRow
+ key={suggestion.id}
+ suggestion={suggestion}
+ windowDays={windowDays}
+ />
+ ) : (
+ <MaterializeSuggestionRow key={suggestion.id} suggestion={suggestion} windowDays={windowDays} />
+ )
+ )}
+ </SuggestionsReviewModal>
+ <MaterializeSuggestionModal />
+ <UnmaterializeSuggestionModal />
+ </>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestionRow.tsx b/products/warehouse_suggestions/frontend/components/SuggestionRow.tsx
new file mode 100644
index 000000000000..449f43238b8e
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestionRow.tsx
@@ -0,0 +1,83 @@
+import { ReactNode } from 'react'
+
+import { cn } from 'lib/utils/css-classes'
+
+import type { SavingHighlight } from '../suggestionCopy'
+
+export interface SuggestionRowProps {
+ suggestionId: string
+ icon: ReactNode
+ title: ReactNode
+ meta: ReactNode[]
+ highlight?: SavingHighlight
+ actions: ReactNode
+ leaving: boolean
+}
+
+export function SuggestionRow({
+ suggestionId,
+ icon,
+ title,
+ meta,
+ highlight,
+ actions,
+ leaving,
+}: SuggestionRowProps): JSX.Element {
+ return (
+ <div
+ data-suggestion-row={suggestionId}
+ className={cn(
+ 'grid transition-[grid-template-rows,opacity] duration-150 ease-out motion-reduce:transition-none',
+ leaving ? 'grid-rows-[0fr] opacity-0' : 'grid-rows-[1fr] opacity-100'
+ )}
+ >
+ <div className="min-h-0 overflow-hidden">
+ <div
+ className={cn(
+ 'grid items-center gap-x-3 gap-y-1.5 px-3 py-2.5',
+ highlight
+ ? 'grid-cols-[minmax(0,1fr)_auto] @min-[36rem]/suggestions:grid-cols-[minmax(0,1fr)_10rem_minmax(15rem,auto)]'
+ : 'grid-cols-[minmax(0,1fr)_auto]'
+ )}
+ >
+ <div
+ className={cn(
+ 'flex min-w-0 items-start gap-2',
+ highlight && 'col-span-2 @min-[36rem]/suggestions:col-span-1'
+ )}
+ >
+ <span className="group/colorful-product-icons colorful-product-icons-true flex h-5 shrink-0 translate-y-0.5 items-center text-base">
+ {icon}
+ </span>
+ <div className="flex min-w-0 flex-col gap-0.5">
+ <div className="flex min-w-0">{title}</div>
+ <div className="flex flex-wrap items-center gap-x-1.5 gap-y-0.5 text-xs text-secondary">
+ {meta.map((item, index) => (
+ <span key={index} className="inline-flex items-baseline gap-1.5">
+ {index > 0 && (
+ <span aria-hidden className="text-muted">
+ ·
+ </span>
+ )}
+ {item}
+ </span>
+ ))}
+ </div>
+ </div>
+ </div>
+ {highlight && (
+ <div className="flex min-w-0 items-baseline gap-1.5 @min-[36rem]/suggestions:flex-col @min-[36rem]/suggestions:items-end @min-[36rem]/suggestions:gap-0">
+ <span translate="no" className="text-sm font-semibold tabular-nums text-success">
+ {highlight.amount}
+ </span>
+ <span className="text-xs text-secondary">{highlight.label}</span>
+ </div>
+ )}
+ <div data-suggestion-actions className="flex flex-wrap items-center justify-end gap-1">
+ {actions}
+ </div>
+ </div>
+ </div>
+ </div>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestionsBanner.tsx b/products/warehouse_suggestions/frontend/components/SuggestionsBanner.tsx
new file mode 100644
index 000000000000..b36d5845a386
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestionsBanner.tsx
@@ -0,0 +1,35 @@
+import { useActions, useValues } from 'kea'
+import { ReactNode } from 'react'
+
+import { IconChevronRight } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+
+export interface SuggestionsBannerProps {
+ icon: ReactNode
+ message: ReactNode
+}
+
+export function SuggestionsBanner({ icon, message }: SuggestionsBannerProps): JSX.Element {
+ const { surfaceSuggestions } = useValues(warehouseSuggestionsLogic)
+ const { openReview } = useActions(warehouseSuggestionsLogic)
+
+ return (
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-2 rounded border border-accent bg-accent-highlight-secondary px-3 py-2">
+ <span className="group/colorful-product-icons colorful-product-icons-true flex shrink-0 translate-y-px text-lg">
+ {icon}
+ </span>
+ <span className="min-w-0 flex-1 text-sm">{message}</span>
+ <LemonButton
+ size="small"
+ type="primary"
+ sideIcon={<IconChevronRight />}
+ onClick={() => openReview()}
+ data-attr="warehouse-suggestions-review"
+ >
+ {`Review ${surfaceSuggestions.length}`}
+ </LemonButton>
+ </div>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestionsReviewModal.tsx b/products/warehouse_suggestions/frontend/components/SuggestionsReviewModal.tsx
new file mode 100644
index 000000000000..5c27343ab7fe
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestionsReviewModal.tsx
@@ -0,0 +1,96 @@
+import { useActions, useValues } from 'kea'
+import { ReactNode, useEffect, useRef } from 'react'
+
+import { LemonButton, LemonModal } from '@posthog/lemon-ui'
+
+import { TZLabel } from 'lib/components/TZLabel'
+
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+
+export interface SuggestionsReviewModalProps {
+ title: string
+ summary: ReactNode
+ children: ReactNode
+}
+
+export function SuggestionsReviewModal({ title, summary, children }: SuggestionsReviewModalProps): JSX.Element {
+ const { reviewOpen, status, stripState, listedSuggestions } = useValues(warehouseSuggestionsLogic)
+ const { closeReview } = useActions(warehouseSuggestionsLogic)
+ const bodyRef = useRef<HTMLDivElement>(null)
+ const returnFocusRef = useRef<HTMLSpanElement>(null)
+ useFocusFollowsRemovedRow(
+ bodyRef,
+ listedSuggestions.map((suggestion) => suggestion.id)
+ )
+
+ const focusSurfaceIfOpenerLeft = (): void => {
+ if (document.activeElement === null || document.activeElement === document.body) {
+ returnFocusRef.current?.focus()
+ }
+ }
+
+ return (
+ <>
+ <span ref={returnFocusRef} tabIndex={-1} data-suggestions-return-focus className="sr-only" />
+ <LemonModal
+ isOpen={reviewOpen}
+ onClose={closeReview}
+ onAfterClose={focusSurfaceIfOpenerLeft}
+ title={title}
+ description={stripState === 'active' ? summary : undefined}
+ width={820}
+ footer={
+ <div className="flex w-full flex-wrap items-center justify-between gap-2">
+ <span className="min-w-0 flex-1 whitespace-normal text-xs text-muted">
+ <span>Based on read counts from the query log. PostHog never reads your query text.</span>
+ {status?.refreshed_at && (
+ <>
+ <span> Refreshed </span>
+ <TZLabel time={status.refreshed_at} showPopover={false} />
+ <span>.</span>
+ </>
+ )}
+ </span>
+ <LemonButton type="secondary" onClick={closeReview}>
+ Close
+ </LemonButton>
+ </div>
+ }
+ >
+ <div ref={bodyRef} className="@container/suggestions -mx-2">
+ <div className="divide-y divide-primary">{children}</div>
+ {stripState === 'all_done' && (
+ <div
+ tabIndex={-1}
+ data-suggestions-done
+ className="px-3 py-6 text-center text-sm text-secondary"
+ >
+ All caught up. New suggestions appear as usage changes.
+ </div>
+ )}
+ </div>
+ </LemonModal>
+ </>
+ )
+}
+
+function useFocusFollowsRemovedRow(bodyRef: React.RefObject<HTMLDivElement>, rowIds: string[]): void {
+ const previousRowIds = useRef(rowIds)
+
+ useEffect(() => {
+ const previous = previousRowIds.current
+ previousRowIds.current = rowIds
+ const removedIndex = previous.findIndex((id) => !rowIds.includes(id))
+ const focusWasLost = !document.activeElement || document.activeElement === document.body
+ if (removedIndex < 0 || !focusWasLost || !bodyRef.current) {
+ return
+ }
+ const nextId = rowIds[removedIndex] ?? rowIds[removedIndex - 1]
+ const target = nextId
+ ? bodyRef.current.querySelector<HTMLElement>(
+ `[data-suggestion-row="${nextId}"] [data-suggestion-actions] button`
+ )
+ : bodyRef.current.querySelector<HTMLElement>('[data-suggestions-done]')
+ target?.focus()
+ }, [rowIds, bodyRef])
+}
diff --git a/products/warehouse_suggestions/frontend/components/SuggestionsStatusNotice.tsx b/products/warehouse_suggestions/frontend/components/SuggestionsStatusNotice.tsx
new file mode 100644
index 000000000000..71788a375dec
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/SuggestionsStatusNotice.tsx
@@ -0,0 +1,17 @@
+import { ReactNode } from 'react'
+
+export interface SuggestionsStatusNoticeProps {
+ icon: ReactNode
+ message: string
+ extra?: ReactNode
+}
+
+export function SuggestionsStatusNotice({ icon, message, extra }: SuggestionsStatusNoticeProps): JSX.Element {
+ return (
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-2 rounded border border-primary bg-surface-primary px-3 py-2 text-sm">
+ <span className="flex shrink-0 text-base text-secondary">{icon}</span>
+ <span className="min-w-0 flex-1">{message}</span>
+ {extra}
+ </div>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionModal.tsx b/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionModal.tsx
new file mode 100644
index 000000000000..1b67c8945f31
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionModal.tsx
@@ -0,0 +1,77 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonButton, LemonModal } from '@posthog/lemon-ui'
+
+import {
+ isUnmaterializePayload,
+ monthlyCost,
+ readTimeLabel,
+ savesTime,
+ savingSentence,
+ unmaterializeSaving,
+} from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+
+export function UnmaterializeSuggestionModal(): JSX.Element | null {
+ const { acceptConfirmSuggestion, actionsInFlight } = useValues(warehouseSuggestionsLogic)
+ const { confirmAccept, closeAcceptConfirm } = useActions(warehouseSuggestionsLogic)
+ const payload = acceptConfirmSuggestion?.payload
+
+ if (!acceptConfirmSuggestion || !payload || !isUnmaterializePayload(payload)) {
+ return null
+ }
+ const inFlight = !!actionsInFlight[acceptConfirmSuggestion.id]
+ const saving = unmaterializeSaving(payload)
+ const inTime = savesTime(saving)
+
+ return (
+ <LemonModal
+ isOpen
+ closable={!inFlight}
+ onClose={closeAcceptConfirm}
+ title={`Stop materializing ${payload.subject_name}?`}
+ footer={
+ <>
+ <LemonButton
+ type="secondary"
+ onClick={closeAcceptConfirm}
+ disabledReason={inFlight ? 'Working' : undefined}
+ >
+ Cancel
+ </LemonButton>
+ <LemonButton
+ type="primary"
+ loading={inFlight}
+ onClick={() => confirmAccept()}
+ data-attr="warehouse-suggestions-unmaterialize-confirm"
+ >
+ Stop materializing
+ </LemonButton>
+ </>
+ }
+ >
+ <div className="flex max-w-xl flex-col gap-4">
+ <p className="m-0 text-sm text-secondary">
+ Deletes its stored table and stops its refreshes. Each read computes the view from its query
+ instead. You can materialize it again later.
+ </p>
+ <div className="grid grid-cols-1 gap-3 rounded border border-primary px-3 py-2.5">
+ <div className="flex flex-col gap-0.5">
+ <span className="text-xs font-semibold text-secondary">Materialized now</span>
+ <span className="text-sm">
+ {monthlyCost(payload.refresh_seconds_per_month, payload.refresh_bytes_per_month, inTime)}
+ </span>
+ </div>
+ <div className="flex flex-col gap-0.5">
+ <span className="text-xs font-semibold text-secondary">Computed on each read</span>
+ <span className="text-sm">
+ {`${monthlyCost(payload.live_seconds_per_month, payload.live_bytes_per_month, inTime)} (estimate)`}
+ </span>
+ <span className="text-xs text-secondary">{readTimeLabel(payload.read_seconds)}</span>
+ </div>
+ <span className="text-sm font-semibold text-success">{savingSentence(saving)}</span>
+ </div>
+ </div>
+ </LemonModal>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionRow.tsx b/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionRow.tsx
new file mode 100644
index 000000000000..e5f938dbe662
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/UnmaterializeSuggestionRow.tsx
@@ -0,0 +1,90 @@
+import { useActions, useValues } from 'kea'
+
+import { LemonButton, Link } from '@posthog/lemon-ui'
+
+import { urls } from 'scenes/urls'
+
+import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
+
+import type { WarehouseSuggestionApi } from '../generated/api.schemas'
+import {
+ isUnmaterializePayload,
+ readSummary,
+ readTimeLabel,
+ savingHighlight,
+ unmaterializeSaving,
+} from '../suggestionCopy'
+import { warehouseSuggestionsLogic } from '../warehouseSuggestionsLogic'
+import { DismissSuggestionMenu } from './DismissSuggestionMenu'
+import { SuggestionRow } from './SuggestionRow'
+import { WhyThisSuggestion } from './WhyThisSuggestion'
+
+const EDIT_ACCESS_REASON = 'You need edit access to this view'
+
+export interface UnmaterializeSuggestionRowProps {
+ suggestion: WarehouseSuggestionApi
+ windowDays: number
+}
+
+export function UnmaterializeSuggestionRow({
+ suggestion,
+ windowDays,
+}: UnmaterializeSuggestionRowProps): JSX.Element | null {
+ const { actionsInFlight, leavingSuggestionIds } = useValues(warehouseSuggestionsLogic)
+ const { openAcceptConfirm } = useActions(warehouseSuggestionsLogic)
+ const { payload } = suggestion
+ if (!isUnmaterializePayload(payload)) {
+ return null
+ }
+ const leaving = leavingSuggestionIds.includes(suggestion.id)
+ const blockedReason = !suggestion.can_act
+ ? EDIT_ACCESS_REASON
+ : actionsInFlight[suggestion.id] || leaving
+ ? 'Working'
+ : undefined
+
+ return (
+ <SuggestionRow
+ suggestionId={suggestion.id}
+ leaving={leaving}
+ icon={iconForType('data_modeling')}
+ title={
+ <Link
+ to={urls.sqlEditor({ view_id: suggestion.subject_id })}
+ subtle
+ className="block min-w-0 truncate text-sm font-semibold"
+ >
+ {payload.subject_name}
+ </Link>
+ }
+ meta={[
+ <WhyThisSuggestion
+ key="reads"
+ evidence={suggestion.evidence}
+ windowDays={windowDays}
+ label={readSummary(suggestion.evidence)}
+ />,
+ <span key="read-time">{readTimeLabel(payload.read_seconds)}</span>,
+ ]}
+ highlight={savingHighlight(unmaterializeSaving(payload))}
+ actions={
+ <>
+ <LemonButton
+ size="small"
+ type="secondary"
+ disabledReason={blockedReason}
+ onClick={() => openAcceptConfirm(suggestion.id)}
+ data-attr="warehouse-suggestions-unmaterialize"
+ >
+ Stop materializing
+ </LemonButton>
+ <DismissSuggestionMenu
+ suggestionId={suggestion.id}
+ kind={suggestion.kind}
+ disabledReason={blockedReason}
+ />
+ </>
+ }
+ />
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/components/WhyThisSuggestion.tsx b/products/warehouse_suggestions/frontend/components/WhyThisSuggestion.tsx
new file mode 100644
index 000000000000..4a068afa63db
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/components/WhyThisSuggestion.tsx
@@ -0,0 +1,46 @@
+import { Tooltip } from '@posthog/lemon-ui'
+
+import type { WarehouseSuggestionEvidenceApi } from '../generated/api.schemas'
+import { QUERY_LOG_CAVEAT, daysReadLabel, surfaceSegments } from '../suggestionCopy'
+import { ReadsBySurfaceBar } from './ReadsBySurfaceBar'
+
+export interface WhyThisSuggestionProps {
+ evidence: WarehouseSuggestionEvidenceApi
+ windowDays: number
+ label: string
+}
+
+export function WhyThisSuggestion({ evidence, windowDays, label }: WhyThisSuggestionProps): JSX.Element {
+ const segments = surfaceSegments(evidence)
+ const daysRead = (evidence.human_days ?? 0) > 0 ? daysReadLabel(evidence, windowDays) : null
+
+ return (
+ <Tooltip
+ title={
+ <div className="flex w-64 max-w-full flex-col gap-2 py-0.5">
+ {segments.length > 0 && <ReadsBySurfaceBar segments={segments} />}
+ {daysRead && <span>{daysRead}</span>}
+ <span className="opacity-75">{QUERY_LOG_CAVEAT}</span>
+ </div>
+ }
+ >
+ <span
+ tabIndex={0}
+ className="cursor-help underline decoration-dotted underline-offset-2"
+ data-attr="warehouse-suggestions-why-this"
+ >
+ <span translate="no">{label}</span>
+ <span className="sr-only">
+ {[
+ segments.map((segment) => `${segment.label} ${segment.count}`).join(', '),
+ daysRead,
+ QUERY_LOG_CAVEAT,
+ ]
+ .filter(Boolean)
+ .map((part) => `. ${part}`)
+ .join('')}
+ </span>
+ </span>
+ </Tooltip>
+ )
+}
diff --git a/products/warehouse_suggestions/frontend/refreshIntervals.ts b/products/warehouse_suggestions/frontend/refreshIntervals.ts
new file mode 100644
index 000000000000..00d07a7a2780
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/refreshIntervals.ts
@@ -0,0 +1,17 @@
+import { DataModelingSyncInterval } from '~/types'
+
+export const INTERVAL_SECONDS: Record<DataModelingSyncInterval, number> = {
+ '15min': 15 * 60,
+ '30min': 30 * 60,
+ '1hour': 60 * 60,
+ '6hour': 6 * 60 * 60,
+ '12hour': 12 * 60 * 60,
+ '24hour': 24 * 60 * 60,
+ '7day': 7 * 24 * 60 * 60,
+ '30day': 30 * 24 * 60 * 60,
+}
+
+export function intervalForSeconds(seconds: number): DataModelingSyncInterval | null {
+ const match = Object.entries(INTERVAL_SECONDS).find(([, intervalSeconds]) => intervalSeconds === seconds)
+ return match ? (match[0] as DataModelingSyncInterval) : null
+}
diff --git a/products/warehouse_suggestions/frontend/subjectUrl.test.ts b/products/warehouse_suggestions/frontend/subjectUrl.test.ts
new file mode 100644
index 000000000000..632e988d901b
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/subjectUrl.test.ts
@@ -0,0 +1,29 @@
+import type { WarehouseSuggestionApi } from './generated/api.schemas'
+import { subjectUrl } from './subjectUrl'
+
+function tableSuggestion(name: string): WarehouseSuggestionApi {
+ return {
+ kind: 'certify',
+ subject_kind: 'table',
+ subject_id: 'table-1',
+ payload: { subject_name: name },
+ } as WarehouseSuggestionApi
+}
+
+function previewQuery(url: string): string | null {
+ return new URL(url, 'https://example.com').searchParams.get('open_query')
+}
+
+describe('subjectUrl', () => {
+ it.each([
+ ['a plain table name', 'stripe_charges', 'SELECT * FROM stripe_charges LIMIT 100'],
+ ['a dotted source table name', 'demo.orders', 'SELECT * FROM demo.orders LIMIT 100'],
+ [
+ 'a stored name that starts and ends with quotes',
+ '"x" UNION SELECT * FROM persons --"',
+ 'SELECT * FROM `"x" UNION SELECT * FROM persons --"` LIMIT 100',
+ ],
+ ])('previews %s as one table', (_name, tableName, query) => {
+ expect(previewQuery(subjectUrl(tableSuggestion(tableName)))).toEqual(query)
+ })
+})
diff --git a/products/warehouse_suggestions/frontend/subjectUrl.ts b/products/warehouse_suggestions/frontend/subjectUrl.ts
new file mode 100644
index 000000000000..99d9341b79bd
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/subjectUrl.ts
@@ -0,0 +1,19 @@
+import { urls } from 'scenes/urls'
+
+import { escapeRawPropertyAsHogQLIdentifier } from '~/queries/utils'
+
+import type { WarehouseSuggestionApi } from './generated/api.schemas'
+
+const TABLE_PREVIEW_LIMIT = 100
+
+function escapeTableName(name: string): string {
+ return name.split('.').map(escapeRawPropertyAsHogQLIdentifier).join('.')
+}
+
+export function subjectUrl(suggestion: WarehouseSuggestionApi): string {
+ if (suggestion.subject_kind === 'table') {
+ const table = escapeTableName(suggestion.payload.subject_name)
+ return urls.sqlEditor({ query: `SELECT * FROM ${table} LIMIT ${TABLE_PREVIEW_LIMIT}` })
+ }
+ return urls.sqlEditor({ view_id: suggestion.subject_id })
+}
diff --git a/products/warehouse_suggestions/frontend/suggestionCopy.test.ts b/products/warehouse_suggestions/frontend/suggestionCopy.test.ts
new file mode 100644
index 000000000000..d497ea487c2f
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/suggestionCopy.test.ts
@@ -0,0 +1,65 @@
+import type { WarehouseSuggestionMaterializePayloadApi } from './generated/api.schemas'
+import {
+ UNKNOWN_SOURCES_CAVEAT,
+ dataAgeLabel,
+ notNowCondition,
+ suggestedScheduleHint,
+ todayAgeLabel,
+} from './suggestionCopy'
+
+const DAY = 24 * 60 * 60
+const HOUR = 60 * 60
+
+function payload(
+ freshnessTodaySeconds: number | null,
+ unknownSources: WarehouseSuggestionMaterializePayloadApi['unknown_sources'] = { names: [], hidden_count: 0 }
+): WarehouseSuggestionMaterializePayloadApi {
+ return {
+ subject_name: 'orders',
+ refresh_interval_seconds: DAY,
+ saves_seconds_per_month: 1200,
+ saves_bytes_per_month: 0,
+ freshness_today_seconds: freshnessTodaySeconds,
+ freshness_after_seconds: DAY + (freshnessTodaySeconds ?? 0),
+ live_sources: { names: [], hidden_count: 0 },
+ unknown_sources: unknownSources,
+ refresh_mode: 'full',
+ refresh_interval_basis: 'savings',
+ }
+}
+
+describe('suggestionCopy', () => {
+ it.each<[string, WarehouseSuggestionMaterializePayloadApi, string, string | null, string]>([
+ ['known sources', payload(HOUR), 'Data up to 1 day and 1 hour old', null, 'Today: up to 1 hour old'],
+ ['live sources', payload(null), 'Data up to 1 day old', null, 'Today: live'],
+ [
+ 'a source the caller cannot see has no schedule',
+ payload(null, { names: [], hidden_count: 1 }),
+ 'Adds up to 1 day to source age',
+ UNKNOWN_SOURCES_CAVEAT,
+ 'Today: unknown for some sources',
+ ],
+ ])('states the data age honestly when %s', (_name, input, label, caveat, today) => {
+ expect(dataAgeLabel(input, DAY)).toEqual({ label, caveat })
+ expect(todayAgeLabel(input)).toEqual(today)
+ })
+
+ it.each<[Parameters<typeof suggestedScheduleHint>[0], string]>([
+ ['source_sync', "sources don't sync more often"],
+ ['read_rate', "People don't read it more often"],
+ ['fastest_schedule', 'the most often PostHog refreshes a view'],
+ ['savings', 'saves more than it costs'],
+ [null, 'saves more than it costs'],
+ ])('explains a %s interval with its own reason', (basis, reason) => {
+ expect(suggestedScheduleHint(basis, '1 day')).toContain(reason)
+ })
+
+ it.each([
+ ['certify', 'reads grow'],
+ ['deprecate', 'refreshes cost'],
+ ['materialize', 'save a lot more'],
+ ['unmaterialize', 'save a lot more'],
+ ])('says what has to grow before a not-now %s suggestion returns', (kind, condition) => {
+ expect(notNowCondition(kind)).toContain(condition)
+ })
+})
diff --git a/products/warehouse_suggestions/frontend/suggestionCopy.ts b/products/warehouse_suggestions/frontend/suggestionCopy.ts
new file mode 100644
index 000000000000..0dac0c8286ea
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/suggestionCopy.ts
@@ -0,0 +1,260 @@
+import { humanizeBytes } from 'lib/utils/numbers'
+
+import type {
+ WarehouseSuggestionApi,
+ WarehouseSuggestionRefreshIntervalBasisEnumApi,
+ WarehouseSuggestionDeprecatePayloadApi,
+ WarehouseSuggestionEvidenceApi,
+ WarehouseSuggestionMaterializePayloadApi,
+ WarehouseSuggestionUnmaterializePayloadApi,
+} from './generated/api.schemas'
+
+const MIN_SECONDS_WORTH_NAMING = 60
+const SPOKEN_UNITS: [string, number][] = [
+ ['day', 24 * 60 * 60],
+ ['hour', 60 * 60],
+ ['minute', 60],
+]
+const MAX_SPOKEN_UNITS = 2
+const SURFACE_LABELS: Record<string, string> = {
+ mcp: 'MCP',
+ posthog_ai: 'PostHog AI',
+ desktop: 'Desktop app',
+ endpoints: 'Endpoints',
+ dashboard: 'Dashboards',
+ insight: 'Insights',
+ sql_editor: 'SQL editor',
+ notebook: 'Notebooks',
+ api: 'API',
+ app: 'Other pages',
+ unknown: 'Other',
+}
+export const UNKNOWN_SOURCES_CAVEAT = 'Some sources have no sync schedule, so their age is unknown.'
+export const QUERY_LOG_CAVEAT = 'Counts come from the query log. Cached dashboard views are not counted.'
+
+export interface SavingHighlight {
+ amount: string
+ label: string
+}
+
+export interface SurfaceSegment {
+ key: string
+ label: string
+ count: number
+}
+
+export interface SavingsSummary {
+ timeViews: number
+ savedSeconds: number
+ bytesViews: number
+ savedBytes: number
+}
+
+export interface MonthlySaving {
+ seconds: number
+ bytes: number
+}
+
+export interface DataAgeLabel {
+ label: string
+ caveat: string | null
+}
+
+export function isMaterializePayload(
+ payload: WarehouseSuggestionApi['payload']
+): payload is WarehouseSuggestionMaterializePayloadApi {
+ return 'refresh_interval_seconds' in payload
+}
+
+export function isUnmaterializePayload(
+ payload: WarehouseSuggestionApi['payload']
+): payload is WarehouseSuggestionUnmaterializePayloadApi {
+ return 'live_seconds_per_month' in payload
+}
+
+export function isDeprecatePayload(
+ payload: WarehouseSuggestionApi['payload']
+): payload is WarehouseSuggestionDeprecatePayloadApi {
+ return 'refresh_seconds_per_month' in payload && !isUnmaterializePayload(payload)
+}
+
+export function materializeSaving(payload: WarehouseSuggestionMaterializePayloadApi): MonthlySaving {
+ return { seconds: payload.saves_seconds_per_month, bytes: payload.saves_bytes_per_month }
+}
+
+export function unmaterializeSaving(payload: WarehouseSuggestionUnmaterializePayloadApi): MonthlySaving {
+ return {
+ seconds: Math.max(0, payload.refresh_seconds_per_month - payload.live_seconds_per_month),
+ bytes: Math.max(0, payload.refresh_bytes_per_month - payload.live_bytes_per_month),
+ }
+}
+
+export function monthlySaving(payload: WarehouseSuggestionApi['payload']): MonthlySaving | null {
+ if (isMaterializePayload(payload)) {
+ return materializeSaving(payload)
+ }
+ return isUnmaterializePayload(payload) ? unmaterializeSaving(payload) : null
+}
+
+export function savesTime(saving: MonthlySaving): boolean {
+ return saving.seconds >= MIN_SECONDS_WORTH_NAMING
+}
+
+export function suggestedScheduleHint(
+ basis: WarehouseSuggestionRefreshIntervalBasisEnumApi | null,
+ cadence: string
+): string {
+ switch (basis) {
+ case 'source_sync':
+ return `Suggested: every ${cadence}. Its sources don't sync more often than that.`
+ case 'read_rate':
+ return `Suggested: every ${cadence}. People don't read it more often than that.`
+ case 'fastest_schedule':
+ return `Suggested: every ${cadence}, the most often PostHog refreshes a view.`
+ default:
+ return `Suggested: every ${cadence}, the shortest schedule that saves more than it costs.`
+ }
+}
+
+export function shorterScheduleWarning(
+ basis: WarehouseSuggestionRefreshIntervalBasisEnumApi | null,
+ cadence: string
+): string | null {
+ switch (basis) {
+ case 'source_sync':
+ return `Its sources sync every ${cadence} at most, so refreshing more often reads the same data again.`
+ case 'read_rate':
+ return `People don't read it more often than every ${cadence}, so extra refreshes mostly go unread.`
+ case 'fastest_schedule':
+ return null
+ default:
+ return `Refreshing more often than every ${cadence} can cost more than it saves.`
+ }
+}
+
+export function refreshModeLabel(incremental: boolean, incrementalKey: string | null): string {
+ if (!incremental) {
+ return 'Each refresh rebuilds the whole table'
+ }
+ return incrementalKey ? `Each refresh reads only new rows by ${incrementalKey}` : 'Each refresh reads only new rows'
+}
+
+export function savingHighlight(saving: MonthlySaving): SavingHighlight {
+ if (savesTime(saving)) {
+ return { amount: `~${spokenDuration(saving.seconds, 1)}`, label: 'query time saved a month' }
+ }
+ return { amount: `~${humanizeBytes(saving.bytes)}`, label: 'less data scanned a month' }
+}
+
+export function savingSentence(saving: MonthlySaving): string {
+ if (savesTime(saving)) {
+ return `Saves about ${spokenDuration(saving.seconds, 1)} of query time a month`
+ }
+ return `Saves about ${humanizeBytes(saving.bytes)} of scanning a month`
+}
+
+export function monthlyCost(seconds: number, bytes: number, inTime: boolean): string {
+ return inTime
+ ? `about ${spokenDuration(seconds, MAX_SPOKEN_UNITS)} a month`
+ : `about ${humanizeBytes(bytes)} scanned a month`
+}
+
+export function readTimeLabel(seconds: number): string {
+ if (seconds >= MIN_SECONDS_WORTH_NAMING) {
+ return `Each read takes about ${spokenDuration(seconds, MAX_SPOKEN_UNITS)}`
+ }
+ const wholeSeconds = Math.max(1, Math.round(seconds))
+ return `Each read takes about ${countOf(wholeSeconds, 'second', 'seconds')}`
+}
+
+const NOT_NOW_CONDITIONS: Record<string, string> = {
+ certify: 'Suggested again if reads grow a lot.',
+ deprecate: 'Suggested again if its refreshes cost a lot more.',
+ materialize: 'Suggested again if it would save a lot more.',
+ unmaterialize: 'Suggested again if it would save a lot more.',
+}
+
+export function notNowCondition(kind: string): string {
+ return NOT_NOW_CONDITIONS[kind] ?? 'Suggested again if the case for it grows a lot.'
+}
+
+export function certificationSummaryText(suggestions: WarehouseSuggestionApi[]): string {
+ const certify = suggestions.filter((suggestion) => suggestion.kind === 'certify').length
+ const deprecate = suggestions.filter((suggestion) => suggestion.kind === 'deprecate').length
+ return [certify > 0 ? `${certify} to certify` : null, deprecate > 0 ? `${deprecate} to mark deprecated` : null]
+ .filter(Boolean)
+ .join(' · ')
+}
+
+export function readSummary(evidence: WarehouseSuggestionEvidenceApi): string {
+ return `${countOf(evidence.human_requests ?? 0, 'read', 'reads')} · ${countOf(evidence.human_users ?? 0, 'person', 'people')}`
+}
+
+export function daysReadLabel(evidence: WarehouseSuggestionEvidenceApi, windowDays: number): string {
+ return `Read on ${evidence.human_days ?? 0} of the last ${windowDays} days`
+}
+
+export function noReadsLabel(windowDays: number): string {
+ return `No reads recorded in ${windowDays} days`
+}
+
+export function refreshCostLabel(
+ payload: Pick<WarehouseSuggestionDeprecatePayloadApi, 'refresh_seconds_per_month' | 'refresh_bytes_per_month'>
+): string {
+ if (payload.refresh_seconds_per_month >= MIN_SECONDS_WORTH_NAMING) {
+ return `Refreshes use about ${spokenDuration(payload.refresh_seconds_per_month, MAX_SPOKEN_UNITS)} a month`
+ }
+ return `Refreshes scan about ${humanizeBytes(payload.refresh_bytes_per_month)} a month`
+}
+
+function dataAgeAfterSeconds(payload: WarehouseSuggestionMaterializePayloadApi, intervalSeconds: number): number {
+ return intervalSeconds + (payload.freshness_today_seconds ?? 0)
+}
+
+export function hasUnknownSources(payload: WarehouseSuggestionMaterializePayloadApi): boolean {
+ return payload.unknown_sources.names.length + payload.unknown_sources.hidden_count > 0
+}
+
+export function dataAgeLabel(payload: WarehouseSuggestionMaterializePayloadApi, intervalSeconds: number): DataAgeLabel {
+ if (hasUnknownSources(payload)) {
+ return {
+ label: `Adds up to ${spokenDuration(intervalSeconds, MAX_SPOKEN_UNITS)} to source age`,
+ caveat: UNKNOWN_SOURCES_CAVEAT,
+ }
+ }
+ const age = spokenDuration(dataAgeAfterSeconds(payload, intervalSeconds), MAX_SPOKEN_UNITS)
+ return { label: `Data up to ${age} old`, caveat: null }
+}
+
+export function todayAgeLabel(payload: WarehouseSuggestionMaterializePayloadApi): string {
+ if (hasUnknownSources(payload)) {
+ return 'Today: unknown for some sources'
+ }
+ if (payload.freshness_today_seconds === null) {
+ return 'Today: live'
+ }
+ return `Today: up to ${spokenDuration(payload.freshness_today_seconds, MAX_SPOKEN_UNITS)} old`
+}
+
+export function surfaceSegments(evidence: WarehouseSuggestionEvidenceApi): SurfaceSegment[] {
+ return Object.entries(evidence.requests_by_surface ?? {})
+ .sort(([, a], [, b]) => b - a)
+ .map(([key, count]) => ({ key, label: SURFACE_LABELS[key] ?? key, count }))
+}
+
+export function spokenDuration(seconds: number, maxUnits: number): string {
+ const parts: string[] = []
+ let remaining = Math.round(seconds)
+ for (const [unit, unitSeconds] of SPOKEN_UNITS) {
+ const count = Math.floor(remaining / unitSeconds)
+ if (count > 0 && parts.length < maxUnits) {
+ parts.push(`${count} ${unit}${count === 1 ? '' : 's'}`)
+ remaining -= count * unitSeconds
+ }
+ }
+ return parts.length > 0 ? parts.join(' and ') : 'under a minute'
+}
+
+export function countOf(count: number, singular: string, plural: string): string {
+ return `${count} ${count === 1 ? singular : plural}`
+}
diff --git a/products/warehouse_suggestions/frontend/warehouseSuggestionActivityDescriber.tsx b/products/warehouse_suggestions/frontend/warehouseSuggestionActivityDescriber.tsx
new file mode 100644
index 000000000000..46966b5a99a4
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/warehouseSuggestionActivityDescriber.tsx
@@ -0,0 +1,35 @@
+import {
+ ActivityLogItem,
+ ActivityLogUserName,
+ HumanizedChange,
+ activityLogSummary,
+ defaultDescriber,
+} from 'lib/components/ActivityLog/humanizeActivity'
+
+const STATUS_VERBS: Record<string, string> = {
+ accepted: 'accepted',
+ dismissed: 'dismissed',
+ proposed: 'reopened',
+ expired: 'expired',
+ auto_resolved: 'resolved',
+}
+
+export function warehouseSuggestionActivityDescriber(
+ logItem: ActivityLogItem,
+ asNotification?: boolean
+): HumanizedChange {
+ const statusChange = logItem.detail.changes?.find((change) => change.field === 'status')
+ const verb = statusChange ? STATUS_VERBS[String(statusChange.after)] : undefined
+ if (!verb) {
+ return defaultDescriber(logItem, asNotification)
+ }
+ const name = logItem.detail.name ?? 'suggestion'
+ return {
+ summary: activityLogSummary(logItem, verb, name),
+ description: (
+ <>
+ <ActivityLogUserName logItem={logItem} /> {verb} the <b>{name}</b>
+ </>
+ ),
+ }
+}
diff --git a/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.test.ts b/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.test.ts
new file mode 100644
index 000000000000..1d58c0db4256
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.test.ts
@@ -0,0 +1,743 @@
+import { router } from 'kea-router'
+import { expectLogic } from 'kea-test-utils'
+
+import { ApiError } from 'lib/api-error'
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+import { urls } from 'scenes/urls'
+
+import { initKeaTests } from '~/test/init'
+
+import {
+ warehouseSavedQueriesCheckIncrementalCreate,
+ warehouseSavedQueriesRetrieve,
+} from 'products/data_warehouse/frontend/generated/api'
+
+import {
+ warehouseSuggestionsAcceptCreate,
+ warehouseSuggestionsDismissCreate,
+ warehouseSuggestionsList,
+ warehouseSuggestionsResumeCreate,
+ warehouseSuggestionsStatusRetrieve,
+} from './generated/api'
+import type { WarehouseSuggestionApi, WarehouseSuggestionStatusApi } from './generated/api.schemas'
+import { intervalForSeconds } from './refreshIntervals'
+import { ROW_LEAVE_MS, StripState, SuggestionSurface, warehouseSuggestionsLogic } from './warehouseSuggestionsLogic'
+
+jest.mock('./generated/api', () => ({
+ warehouseSuggestionsAcceptCreate: jest.fn(),
+ warehouseSuggestionsDismissCreate: jest.fn(),
+ warehouseSuggestionsList: jest.fn(),
+ warehouseSuggestionsResumeCreate: jest.fn(),
+ warehouseSuggestionsStatusRetrieve: jest.fn(),
+}))
+jest.mock('products/data_warehouse/frontend/generated/api', () => ({
+ warehouseSavedQueriesRetrieve: jest.fn(),
+ warehouseSavedQueriesCheckIncrementalCreate: jest.fn(),
+}))
+
+const mockList = warehouseSuggestionsList as jest.MockedFunction<typeof warehouseSuggestionsList>
+const mockStatus = warehouseSuggestionsStatusRetrieve as jest.MockedFunction<typeof warehouseSuggestionsStatusRetrieve>
+const mockAccept = warehouseSuggestionsAcceptCreate as jest.MockedFunction<typeof warehouseSuggestionsAcceptCreate>
+const mockDismiss = warehouseSuggestionsDismissCreate as jest.MockedFunction<typeof warehouseSuggestionsDismissCreate>
+const mockResume = warehouseSuggestionsResumeCreate as jest.MockedFunction<typeof warehouseSuggestionsResumeCreate>
+const mockSavedQuery = warehouseSavedQueriesRetrieve as jest.MockedFunction<typeof warehouseSavedQueriesRetrieve>
+const mockCheck = warehouseSavedQueriesCheckIncrementalCreate as jest.MockedFunction<
+ typeof warehouseSavedQueriesCheckIncrementalCreate
+>
+
+const ACTIVE_STATUS: WarehouseSuggestionStatusApi = {
+ enabled: true,
+ eligible: true,
+ days_with_data: 30,
+ window_days: 30,
+ paused_reason: null,
+ refreshed_at: '2026-10-07T08:30:00Z',
+}
+
+const MATERIALIZE: WarehouseSuggestionApi = {
+ id: 'suggestion-1',
+ kind: 'materialize',
+ subject_kind: 'saved_query',
+ subject_id: 'view-1',
+ payload: {
+ subject_name: 'orders',
+ refresh_interval_seconds: 86400,
+ saves_seconds_per_month: 1200,
+ saves_bytes_per_month: 0,
+ freshness_today_seconds: null,
+ freshness_after_seconds: 86400,
+ live_sources: { names: [], hidden_count: 0 },
+ unknown_sources: { names: [], hidden_count: 0 },
+ refresh_mode: 'full',
+ refresh_interval_basis: 'savings',
+ },
+ payload_version: 1,
+ evidence: { human_requests: 200, human_users: 6, human_days: 25 },
+ evidence_window_start: '2026-09-07T00:00:00Z',
+ evidence_window_end: '2026-10-07T00:00:00Z',
+ last_seen_at: '2026-10-07T08:30:00Z',
+ status: 'proposed',
+ surfaced_at: '2026-10-07T08:30:00Z',
+ reviewed_by: null,
+ reviewed_at: null,
+ dismissal_reason: null,
+ dismissal_note: null,
+ created_asset: null,
+ asset_outcome: null,
+ realized_seconds_saved_per_month: null,
+ realized_bytes_saved_per_month: null,
+ can_act: true,
+}
+
+function materialize(id: string, savesSeconds: number, savesBytes = 0): WarehouseSuggestionApi {
+ return {
+ ...MATERIALIZE,
+ id,
+ subject_id: `view-${id}`,
+ payload: { ...MATERIALIZE.payload, saves_seconds_per_month: savesSeconds, saves_bytes_per_month: savesBytes },
+ }
+}
+
+const INCREMENTAL: WarehouseSuggestionApi = {
+ ...MATERIALIZE,
+ id: 'suggestion-incremental',
+ payload: {
+ ...MATERIALIZE.payload,
+ refresh_mode: 'incremental',
+ incremental_key: 'day',
+ unique_key: ['day', 'plan'],
+ lookback_seconds: 3600,
+ },
+}
+
+function unmaterialize(id: string, refreshSeconds: number, liveSeconds: number): WarehouseSuggestionApi {
+ return {
+ ...MATERIALIZE,
+ id,
+ kind: 'unmaterialize',
+ subject_id: `view-${id}`,
+ payload: {
+ subject_name: id,
+ refresh_seconds_per_month: refreshSeconds,
+ refresh_bytes_per_month: 0,
+ live_seconds_per_month: liveSeconds,
+ live_bytes_per_month: 0,
+ read_seconds: 4,
+ },
+ }
+}
+
+const DEPRECATE: WarehouseSuggestionApi = {
+ ...MATERIALIZE,
+ id: 'suggestion-deprecate',
+ kind: 'deprecate',
+ payload: { subject_name: 'old_rollup', refresh_seconds_per_month: 5400, refresh_bytes_per_month: 0 },
+}
+
+function certification(id: string, kind: 'certify' | 'deprecate'): WarehouseSuggestionApi {
+ return { ...MATERIALIZE, id, kind, subject_kind: 'table', payload: { subject_name: id } }
+}
+
+function page(results: WarehouseSuggestionApi[]): Awaited<ReturnType<typeof warehouseSuggestionsList>> {
+ return { count: results.length, results } as Awaited<ReturnType<typeof warehouseSuggestionsList>>
+}
+
+describe('warehouseSuggestionsLogic', () => {
+ let logic: ReturnType<typeof warehouseSuggestionsLogic.build>
+
+ beforeEach(() => {
+ initKeaTests()
+ jest.clearAllMocks()
+ mockSavedQuery.mockReset()
+ mockCheck.mockReset()
+ jest.spyOn(lemonToast, 'error').mockImplementation(() => 'toast')
+ jest.spyOn(lemonToast, 'success').mockImplementation(() => 'toast')
+ mockStatus.mockResolvedValue(ACTIVE_STATUS)
+ mockList.mockResolvedValue(page([MATERIALIZE]))
+ })
+
+ afterEach(() => logic?.unmount())
+
+ async function checksRequested(count: number): Promise<void> {
+ while (mockCheck.mock.calls.length < count) {
+ await new Promise((resolve) => setTimeout(resolve, 0))
+ }
+ }
+
+ async function mountLogic(
+ surface: SuggestionSurface = 'models',
+ onViewAccepted?: (suggestion: WarehouseSuggestionApi) => void
+ ): Promise<void> {
+ logic = warehouseSuggestionsLogic({ surface, onViewAccepted })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsSuccess', 'loadStatusSuccess'])
+ }
+
+ it.each<[string, Partial<WarehouseSuggestionStatusApi>, WarehouseSuggestionApi[], StripState]>([
+ ['active with an open suggestion', {}, [MATERIALIZE], 'active'],
+ ['hidden when a surfaced batch is empty', {}, [], 'hidden'],
+ ['hidden while waiting for a slot', {}, [{ ...MATERIALIZE, surfaced_at: null }], 'hidden'],
+ ['warming up before a full window', { days_with_data: 12 }, [], 'warming_up'],
+ ['not eligible', { eligible: false }, [], 'not_eligible'],
+ ['hidden before the first run', { refreshed_at: null, eligible: false }, [], 'hidden'],
+ ])('strip state is %s', async (_name, status, results, expected) => {
+ mockStatus.mockResolvedValue({ ...ACTIVE_STATUS, ...status })
+ mockList.mockResolvedValue(page(results))
+
+ await mountLogic()
+
+ expect(logic.values.stripState).toEqual(expected)
+ })
+
+ it('stays hidden when the backend says the feature is off, even if the other request fails differently', async () => {
+ mockStatus.mockRejectedValue(new ApiError('forbidden', 403, undefined, { detail: 'not enabled' }))
+ mockList.mockRejectedValue(new ApiError('server error', 500, undefined, { detail: 'boom' }))
+ logic = warehouseSuggestionsLogic({ surface: 'models' })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsFailure', 'loadStatusFailure'])
+
+ expect(logic.values.stripState).toEqual('hidden')
+ })
+
+ it('keeps the list and the open dialog when a background reload fails', async () => {
+ await mountLogic()
+ logic.actions.openReview()
+ mockList.mockRejectedValue(new ApiError('server error', 500, undefined, { detail: 'boom' }))
+
+ logic.actions.loadSuggestions()
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsFailure'])
+
+ expect(logic.values.stripState).toEqual('active')
+ expect(logic.values.reviewOpen).toBe(true)
+ })
+
+ it.each(['status', 'list'])(
+ 'shows the error state when the %s request fails while the other succeeds, and retry loads both again',
+ async (failing) => {
+ let finishOther: () => void = () => {}
+ const otherSettles = new Promise<void>((resolve) => (finishOther = resolve))
+ if (failing === 'status') {
+ mockStatus.mockRejectedValueOnce(new Error('down'))
+ mockList.mockImplementationOnce(async () => {
+ await otherSettles
+ return page([MATERIALIZE])
+ })
+ } else {
+ mockList.mockRejectedValueOnce(new Error('down'))
+ mockStatus.mockImplementationOnce(async () => {
+ await otherSettles
+ return ACTIVE_STATUS
+ })
+ }
+ logic = warehouseSuggestionsLogic({ surface: 'models' })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions([
+ failing === 'status' ? 'loadStatusFailure' : 'loadSuggestionsFailure',
+ ])
+ finishOther()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.stripState).toEqual('error')
+
+ logic.actions.reload()
+
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsSuccess', 'loadStatusSuccess'])
+ expect(logic.values.stripState).toEqual('active')
+ }
+ )
+
+ it('keeps a card hidden when the list reloads while its dismiss is still running', async () => {
+ let finish: (value: WarehouseSuggestionApi) => void = () => {}
+ mockDismiss.mockReturnValue(new Promise((resolve) => (finish = resolve)))
+ await mountLogic()
+
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_now')
+ logic.actions.loadSuggestions()
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsSuccess'])
+
+ expect(logic.values.surfaceSuggestions).toEqual([])
+ finish({ ...MATERIALIZE, status: 'dismissed' })
+ })
+
+ it('ignores a second accept while the first is in flight', async () => {
+ let finish: (value: WarehouseSuggestionApi) => void = () => {}
+ mockAccept.mockReturnValue(new Promise((resolve) => (finish = resolve)))
+ await mountLogic()
+
+ logic.actions.acceptSuggestion(MATERIALIZE.id, { refresh_interval_seconds: 86400 })
+ logic.actions.acceptSuggestion(MATERIALIZE.id, { refresh_interval_seconds: 86400 })
+ finish({ ...MATERIALIZE, status: 'accepted' })
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+
+ expect(mockAccept).toHaveBeenCalledTimes(1)
+ expect(logic.values.surfaceSuggestions).toEqual([])
+ })
+
+ it.each([
+ ['someone else decided it first', 409],
+ ['its view or table was deleted', 404],
+ ])('restores a dismissed card and reloads when %s', async (_name, status) => {
+ mockDismiss.mockRejectedValue(new ApiError('stale', status, undefined, { detail: 'stale' }))
+ await mountLogic()
+
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_now')
+
+ await expectLogic(logic).toDispatchActions(['restoreSuggestion', 'loadSuggestions', 'loadSuggestionsSuccess'])
+ expect(logic.values.surfaceSuggestions.map((suggestion) => suggestion.id)).toEqual([MATERIALIZE.id])
+ })
+
+ it.each(['refresh_interval_seconds', 'refresh_mode', 'incremental_key', 'unique_key', 'lookback_seconds'])(
+ 'keeps the materialize dialog open with the error when the backend refuses %s',
+ async (attr) => {
+ mockAccept.mockRejectedValue(new ApiError('refused', 400, undefined, { detail: 'Not allowed.', attr }))
+ await mountLogic()
+ logic.actions.openMaterializeModal(MATERIALIZE.id)
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.confirmMaterialize()
+
+ await expectLogic(logic).toDispatchActions(['setMaterializeError'])
+ expect(logic.values.materializeError).toEqual('Not allowed.')
+ expect(logic.values.materializeModalSuggestion?.id).toEqual(MATERIALIZE.id)
+ }
+ )
+
+ it.each<[string, Parameters<typeof logic.actions.setRefreshDraft>[0], Record<string, unknown>]>([
+ ['keeps every proposed setting', {}, { refresh_interval_seconds: 86400 }],
+ [
+ 'switches to a full refresh without sending keys the backend would refuse',
+ { enabled: false },
+ { refresh_interval_seconds: 86400, refresh_mode: 'full' },
+ ],
+ [
+ 'sends only the incremental key that changed',
+ { incrementalKey: 'created_at' },
+ { refresh_interval_seconds: 86400, incremental_key: 'created_at' },
+ ],
+ [
+ 'sends a new unique key and lookback',
+ { uniqueKey: ['day'], lookbackSeconds: 0 },
+ { refresh_interval_seconds: 86400, unique_key: ['day'], lookback_seconds: 0 },
+ ],
+ ])('materializing an incremental proposal %s', async (_name, change, expectedBody) => {
+ mockList.mockResolvedValue(page([INCREMENTAL]))
+ mockAccept.mockResolvedValue({ ...INCREMENTAL, status: 'accepted' })
+ await mountLogic()
+ logic.actions.openMaterializeModal(INCREMENTAL.id)
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.setRefreshDraft(change)
+
+ logic.actions.confirmMaterialize()
+
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+ expect(mockAccept).toHaveBeenCalledWith(expect.any(String), INCREMENTAL.id, expectedBody)
+ })
+
+ it('materializes with a full refresh when the view can no longer refresh incrementally', async () => {
+ mockList.mockResolvedValue(page([INCREMENTAL]))
+ mockSavedQuery.mockResolvedValue({
+ query: { query: 'SELECT * FROM events' },
+ } as Awaited<ReturnType<typeof warehouseSavedQueriesRetrieve>>)
+ mockCheck.mockResolvedValue({
+ eligible: false,
+ key_candidates: [],
+ unique_key_candidates: [],
+ key_candidate_types: {},
+ blockers: ['The query has no column that grows over time.'],
+ warnings: [],
+ })
+ mockAccept.mockResolvedValue({ ...INCREMENTAL, status: 'accepted' })
+ await mountLogic()
+ logic.actions.openMaterializeModal(INCREMENTAL.id)
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.confirmMaterialize()
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+
+ expect(mockAccept).toHaveBeenCalledWith(expect.any(String), INCREMENTAL.id, {
+ refresh_interval_seconds: 86400,
+ refresh_mode: 'full',
+ })
+ })
+
+ it('switching a full proposal to incremental sends the mode and the keys', async () => {
+ mockAccept.mockResolvedValue({ ...MATERIALIZE, status: 'accepted' })
+ await mountLogic()
+ logic.actions.openMaterializeModal(MATERIALIZE.id)
+ await expectLogic(logic).toFinishAllListeners()
+ logic.actions.setRefreshDraft({ enabled: true, incrementalKey: 'day', uniqueKey: ['day'] })
+
+ logic.actions.confirmMaterialize()
+
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+ expect(mockAccept).toHaveBeenCalledWith(expect.any(String), MATERIALIZE.id, {
+ refresh_interval_seconds: 86400,
+ refresh_mode: 'incremental',
+ incremental_key: 'day',
+ unique_key: ['day'],
+ lookback_seconds: 0,
+ })
+ })
+
+ it.each<[string, Parameters<typeof logic.actions.setRefreshDraft>[0]]>([
+ ['every unique key column is cleared', { uniqueKey: [] }],
+ ['the incremental column is cleared', { incrementalKey: null }],
+ ])('does not materialize an incremental draft when %s', async (_name, change) => {
+ mockList.mockResolvedValue(page([INCREMENTAL]))
+ await mountLogic()
+ logic.actions.openMaterializeModal(INCREMENTAL.id)
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.setRefreshDraft(change)
+ logic.actions.confirmMaterialize()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.materializeDisabledReason).toEqual('Select the incremental column and unique key columns')
+ expect(mockAccept).not.toHaveBeenCalled()
+ })
+
+ it('does not materialize while the incremental check is still running', async () => {
+ let finishCheck: (
+ check: Awaited<ReturnType<typeof warehouseSavedQueriesCheckIncrementalCreate>>
+ ) => void = () => {}
+ mockSavedQuery.mockResolvedValue({
+ query: { query: 'SELECT * FROM events' },
+ } as Awaited<ReturnType<typeof warehouseSavedQueriesRetrieve>>)
+ mockCheck.mockReturnValue(new Promise((resolve) => (finishCheck = resolve)))
+ await mountLogic()
+ logic.actions.openMaterializeModal(MATERIALIZE.id)
+ await checksRequested(1)
+
+ logic.actions.confirmMaterialize()
+
+ expect(logic.values.materializeDisabledReason).toEqual('Checking whether it can refresh incrementally')
+ expect(mockAccept).not.toHaveBeenCalled()
+ finishCheck({
+ eligible: true,
+ key_candidates: ['day'],
+ unique_key_candidates: ['day'],
+ key_candidate_types: {},
+ blockers: [],
+ warnings: [],
+ })
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.materializeDisabledReason).toBeNull()
+ })
+
+ it('keeps the open view check when an earlier view check resolves last', async () => {
+ const first = materialize('first', 600)
+ const second = materialize('second', 1200)
+ mockList.mockResolvedValue(page([first, second]))
+ mockSavedQuery.mockImplementation(
+ async (_project, id) =>
+ ({ query: { query: `SELECT * FROM ${id}` } }) as Awaited<
+ ReturnType<typeof warehouseSavedQueriesRetrieve>
+ >
+ )
+ const resolvers: Record<string, (keys: string[]) => void> = {}
+ mockCheck.mockImplementation(
+ (_project, { query }) =>
+ new Promise((resolve) => {
+ resolvers[query] = (keys) =>
+ resolve({
+ eligible: true,
+ key_candidates: keys,
+ unique_key_candidates: keys,
+ key_candidate_types: {},
+ blockers: [],
+ warnings: [],
+ })
+ })
+ )
+ await mountLogic()
+
+ logic.actions.openMaterializeModal(first.id)
+ logic.actions.closeMaterializeModal()
+ logic.actions.openMaterializeModal(second.id)
+ await checksRequested(2)
+ resolvers[`SELECT * FROM ${second.subject_id}`](['second_day'])
+ resolvers[`SELECT * FROM ${first.subject_id}`](['first_day'])
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.incrementalCheck?.key_candidates).toEqual(['second_day'])
+ expect(logic.values.refreshDraft.incrementalKey).toEqual('second_day')
+ })
+
+ it('checks the view for incremental refresh when the dialog opens', async () => {
+ mockSavedQuery.mockResolvedValue({
+ query: { query: 'SELECT day, plan, count() FROM events GROUP BY day, plan' },
+ } as Awaited<ReturnType<typeof warehouseSavedQueriesRetrieve>>)
+ mockCheck.mockResolvedValue({
+ eligible: true,
+ key_candidates: ['day'],
+ unique_key_candidates: ['day', 'plan'],
+ key_candidate_types: { day: 'date' },
+ blockers: [],
+ warnings: [],
+ })
+ await mountLogic()
+
+ logic.actions.openMaterializeModal(MATERIALIZE.id)
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(mockCheck).toHaveBeenCalledTimes(1)
+ expect(logic.values.incrementalCheck?.key_candidates).toEqual(['day'])
+ })
+
+ it.each<[SuggestionSurface, WarehouseSuggestionApi[], string[]]>([
+ [
+ 'models',
+ [
+ materialize('bytes-big', 0, 5e12),
+ materialize('time-small', 600),
+ unmaterialize('unread-mid', 3000, 1500),
+ materialize('time-big', 2400),
+ ],
+ ['time-big', 'unread-mid', 'time-small', 'bytes-big'],
+ ],
+ ['catalog', [certification('old', 'deprecate'), certification('used', 'certify')], ['used', 'old']],
+ ])('orders %s suggestions by what they are worth', async (surface, results, expectedOrder) => {
+ mockList.mockResolvedValue(page(results))
+
+ await mountLogic(surface)
+
+ expect(logic.values.surfaceSuggestions.map((suggestion) => suggestion.id)).toEqual(expectedOrder)
+ })
+
+ it('counts time and bytes savings separately', async () => {
+ mockList.mockResolvedValue(page([materialize('a', 1800), materialize('b', 1200), materialize('c', 0, 3e12)]))
+
+ await mountLogic()
+
+ expect(logic.values.savingsSummary).toEqual({
+ timeViews: 2,
+ savedSeconds: 3000,
+ bytesViews: 1,
+ savedBytes: 3e12,
+ })
+ })
+
+ it('drops an accepted row from the counts at once and from the list after it leaves', async () => {
+ jest.useFakeTimers()
+ try {
+ mockList.mockResolvedValue(page([materialize('a', 1800), materialize('b', 1200)]))
+ mockAccept.mockResolvedValue({ ...materialize('a', 1800), status: 'accepted' })
+ await mountLogic()
+
+ logic.actions.acceptSuggestion('a', { refresh_interval_seconds: 86400 })
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+
+ expect(logic.values.savingsSummary.savedSeconds).toEqual(1200)
+ expect(logic.values.listedSuggestions.map((suggestion) => suggestion.id)).toEqual(['a', 'b'])
+
+ jest.advanceTimersByTime(ROW_LEAVE_MS)
+
+ expect(logic.values.listedSuggestions.map((suggestion) => suggestion.id)).toEqual(['b'])
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
+ it('stays mounted as all done while the last row leaves and after it is gone', async () => {
+ jest.useFakeTimers()
+ try {
+ mockAccept.mockResolvedValue({ ...MATERIALIZE, status: 'accepted' })
+ await mountLogic()
+
+ logic.actions.acceptSuggestion(MATERIALIZE.id, { refresh_interval_seconds: 86400 })
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+ expect(logic.values.stripState).toEqual('all_done')
+
+ jest.advanceTimersByTime(ROW_LEAVE_MS)
+ expect(logic.values.stripState).toEqual('all_done')
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
+ it('keeps a row whose dismiss failed even after its leave timer would have fired', async () => {
+ jest.useFakeTimers()
+ try {
+ mockDismiss.mockRejectedValue(new Error('network'))
+ await mountLogic()
+
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_useful')
+ await expectLogic(logic).toDispatchActions(['restoreSuggestion'])
+ jest.advanceTimersByTime(ROW_LEAVE_MS)
+
+ expect(logic.values.surfaceSuggestions.map((suggestion) => suggestion.id)).toEqual([MATERIALIZE.id])
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
+ it('gives each dismiss its own toast so every Undo stays reachable', async () => {
+ mockList.mockResolvedValue(page([materialize('a', 600), materialize('b', 600)]))
+ mockDismiss.mockImplementation(async (_project, id) => ({ ...materialize(id, 600), status: 'dismissed' }))
+ await mountLogic()
+
+ logic.actions.dismissSuggestion('a', 'not_useful')
+ logic.actions.dismissSuggestion('b', 'not_useful')
+ await expectLogic(logic).toFinishAllListeners()
+
+ const toastIds = (lemonToast.success as jest.Mock).mock.calls.map(([, options]) => options.toastId)
+ expect(new Set(toastIds).size).toEqual(2)
+ })
+
+ it('brings a dismissed row back through resume even after the list reloaded without it', async () => {
+ mockDismiss.mockResolvedValue({ ...MATERIALIZE, status: 'dismissed' })
+ mockResume.mockResolvedValue({ ...MATERIALIZE, status: 'proposed' })
+ await mountLogic()
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_now')
+ await expectLogic(logic).toFinishAllListeners()
+ mockList.mockResolvedValue(page([]))
+ logic.actions.loadSuggestions()
+ await expectLogic(logic).toDispatchActions(['loadSuggestionsSuccess'])
+
+ logic.actions.undoDismiss(MATERIALIZE.id)
+
+ await expectLogic(logic).toDispatchActions(['suggestionResumed', 'restoreSuggestion'])
+ expect(mockResume).toHaveBeenCalledWith(expect.any(String), MATERIALIZE.id)
+ expect(logic.values.surfaceSuggestions.map((suggestion) => suggestion.id)).toEqual([MATERIALIZE.id])
+ })
+
+ it('brings a dismissed row back through the toast Undo even after the logic unmounted', async () => {
+ mockDismiss.mockResolvedValue({ ...MATERIALIZE, status: 'dismissed' })
+ mockResume.mockResolvedValue({ ...MATERIALIZE, status: 'proposed' })
+ await mountLogic()
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_now')
+ await expectLogic(logic).toFinishAllListeners()
+ const [message, options] = (lemonToast.success as jest.Mock).mock.calls[0]
+ logic.unmount()
+
+ options.button.action()
+ await new Promise((resolve) => setTimeout(resolve, 0))
+
+ expect(message).toEqual('Dismissed. Suggested again if it would save a lot more.')
+ expect(mockResume).toHaveBeenCalledWith(expect.any(String), MATERIALIZE.id)
+ })
+
+ it('keeps the row hidden when undo fails', async () => {
+ mockDismiss.mockResolvedValue({ ...MATERIALIZE, status: 'dismissed' })
+ mockResume.mockRejectedValue(new Error('network'))
+ await mountLogic()
+ logic.actions.dismissSuggestion(MATERIALIZE.id, 'not_now')
+ await expectLogic(logic).toFinishAllListeners()
+
+ logic.actions.undoDismiss(MATERIALIZE.id)
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(lemonToast.error).toHaveBeenCalled()
+ expect(logic.values.surfaceSuggestions).toEqual([])
+ })
+
+ it('offers no catalog View while the certification it would point at does not exist yet', async () => {
+ const accepted = certification('used', 'certify')
+ mockList.mockResolvedValue(page([accepted]))
+ mockAccept.mockResolvedValue({ ...accepted, status: 'accepted', created_asset: null })
+ await mountLogic('catalog', jest.fn())
+
+ logic.actions.acceptSuggestion(accepted.id)
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+
+ expect((lemonToast.success as jest.Mock).mock.calls[0][1].button).toBeUndefined()
+ })
+
+ async function acceptAndClickView(
+ surface: SuggestionSurface,
+ accepted: WarehouseSuggestionApi,
+ onViewAccepted?: (suggestion: WarehouseSuggestionApi) => void
+ ): Promise<void> {
+ mockList.mockResolvedValue(page([accepted]))
+ mockAccept.mockResolvedValue({ ...accepted, status: 'accepted' })
+ await mountLogic(surface, onViewAccepted)
+ logic.actions.openReview()
+ logic.actions.acceptSuggestion(accepted.id)
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+ const [, options] = (lemonToast.success as jest.Mock).mock.calls[0]
+ options.button.action()
+ }
+
+ it.each([MATERIALIZE, unmaterialize('unread', 3000, 1500)])(
+ 'sends the strip toast View for $kind to the view in the SQL editor',
+ async (accepted) => {
+ await acceptAndClickView('models', accepted)
+
+ expect(router.values.location.pathname + router.values.location.search).toContain(
+ urls.sqlEditor({ view_id: accepted.subject_id })
+ )
+ }
+ )
+
+ it.each<[string, boolean, Record<string, unknown> | undefined, string]>([
+ ['stops refreshing by default', false, undefined, 'Marked old_rollup as deprecated and stopped its refreshes.'],
+ ['keeps refreshing when asked', true, { keep_materialized: true }, 'Marked old_rollup as deprecated.'],
+ ])('deprecating a materialized view %s', async (_name, keep, body, toast) => {
+ mockList.mockResolvedValue(page([DEPRECATE]))
+ mockAccept.mockResolvedValue({
+ ...DEPRECATE,
+ status: 'accepted',
+ created_asset: { certification_id: 'cert-1', stopped_materialization: !keep },
+ })
+ await mountLogic('catalog')
+ logic.actions.openAcceptConfirm(DEPRECATE.id)
+ logic.actions.setKeepMaterialized(keep)
+
+ logic.actions.confirmAccept()
+ await expectLogic(logic).toDispatchActions(['suggestionAccepted'])
+
+ expect(mockAccept).toHaveBeenCalledWith(expect.any(String), DEPRECATE.id, body)
+ expect((lemonToast.success as jest.Mock).mock.calls[0][0]).toEqual(toast)
+ expect(logic.values.acceptConfirmSuggestion).toBeNull()
+ })
+
+ it('reloads the list when an accept finds the view or table deleted', async () => {
+ mockAccept.mockRejectedValue(new ApiError('Not found.', 404, undefined, { detail: 'Not found.' }))
+ await mountLogic()
+
+ logic.actions.acceptSuggestion(MATERIALIZE.id, { refresh_interval_seconds: 86400 })
+
+ await expectLogic(logic).toDispatchActions(['loadSuggestions', 'loadSuggestionsSuccess'])
+ expect(lemonToast.error).toHaveBeenCalledWith(
+ 'This suggestion is gone, or its view or table was deleted. The list is up to date now.'
+ )
+ })
+
+ it('shows why the backend refused to stop materializing, closes the dialog and reloads', async () => {
+ const unread = unmaterialize('unread', 3000, 1500)
+ mockList.mockResolvedValue(page([unread]))
+ mockAccept.mockRejectedValue(
+ new ApiError('conflict', 409, undefined, { detail: 'Another view reads this view now.' })
+ )
+ await mountLogic()
+ logic.actions.openAcceptConfirm(unread.id)
+
+ logic.actions.confirmAccept()
+
+ await expectLogic(logic).toDispatchActions(['acceptSuggestionFailed', 'loadSuggestions'])
+ expect(lemonToast.error).toHaveBeenCalledWith('Another view reads this view now.')
+ expect(logic.values.acceptConfirmSuggestion).toBeNull()
+ })
+
+ it('hands the catalog toast View to the host so it can show the new certification', async () => {
+ const onViewAccepted = jest.fn()
+ const accepted = { ...certification('used', 'certify'), created_asset: { certification_id: 'cert-1' } }
+
+ await acceptAndClickView('catalog', accepted, onViewAccepted)
+
+ expect(onViewAccepted).toHaveBeenCalledWith(expect.objectContaining({ id: accepted.id }))
+ expect(logic.values.reviewOpen).toBe(false)
+ })
+
+ it.each<[string, number, boolean]>([
+ ['shorter than suggested', 3600, true],
+ ['the suggested one', 86400, false],
+ ['longer than suggested', 7 * 86400, false],
+ ])('warns about a picked schedule %s only when it is shorter', async (_name, pickedSeconds, warns) => {
+ await mountLogic()
+ logic.actions.openMaterializeModal(MATERIALIZE.id)
+
+ logic.actions.setMaterializeInterval(intervalForSeconds(pickedSeconds))
+
+ expect(logic.values.materializeIntervalShorterThanSuggested).toEqual(warns)
+ })
+})
diff --git a/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.ts b/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.ts
new file mode 100644
index 000000000000..7f54afc17f03
--- /dev/null
+++ b/products/warehouse_suggestions/frontend/warehouseSuggestionsLogic.ts
@@ -0,0 +1,923 @@
+import {
+ LogicWrapper,
+ MakeLogicType,
+ actions,
+ afterMount,
+ kea,
+ key,
+ listeners,
+ path,
+ props,
+ reducers,
+ selectors,
+} from 'kea'
+import { loaders } from 'kea-loaders'
+import { router } from 'kea-router'
+import posthog from 'posthog-js'
+
+import { ApiConfig } from 'lib/api'
+import { ApiError } from 'lib/api-error'
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+import { EMPTY_INCREMENTAL_DRAFT, IncrementalConfigDraft } from 'scenes/data-warehouse/editor/IncrementalConfigFields'
+import { urls } from 'scenes/urls'
+
+import { DataModelingSyncInterval } from '~/types'
+
+import {
+ warehouseSavedQueriesCheckIncrementalCreate,
+ warehouseSavedQueriesRetrieve,
+} from 'products/data_warehouse/frontend/generated/api'
+import type { IncrementalEligibilityApi } from 'products/data_warehouse/frontend/generated/api.schemas'
+
+import {
+ warehouseSuggestionsAcceptCreate,
+ warehouseSuggestionsDismissCreate,
+ warehouseSuggestionsList,
+ warehouseSuggestionsResumeCreate,
+ warehouseSuggestionsStatusRetrieve,
+} from './generated/api'
+import type {
+ AcceptWarehouseSuggestionApi,
+ DismissWarehouseSuggestionApi,
+ WarehouseSuggestionMaterializePayloadApi,
+ WarehouseSuggestionApi,
+ WarehouseSuggestionStatusApi,
+} from './generated/api.schemas'
+import type { WarehouseSuggestionDismissalReasonEnumApi } from './generated/api.schemas'
+import { INTERVAL_SECONDS, intervalForSeconds } from './refreshIntervals'
+import {
+ MonthlySaving,
+ SavingsSummary,
+ isMaterializePayload,
+ monthlySaving,
+ notNowCondition,
+ savesTime,
+} from './suggestionCopy'
+
+export type StripState = 'loading' | 'error' | 'hidden' | 'warming_up' | 'not_eligible' | 'active' | 'all_done'
+export type SuggestionSurface = 'models' | 'catalog'
+
+export interface WarehouseSuggestionsLogicProps {
+ surface: SuggestionSurface
+ onAccepted?: (suggestion: WarehouseSuggestionApi) => void
+ onViewAccepted?: (suggestion: WarehouseSuggestionApi) => void
+}
+
+export const ROW_LEAVE_MS = 150
+const KIND_ORDER: Record<string, number> = { certify: 0, deprecate: 1 }
+
+const SURFACE_KINDS: Record<SuggestionSurface, string[]> = {
+ models: ['materialize', 'unmaterialize'],
+ catalog: ['certify', 'deprecate'],
+}
+const VIEW_IN_SQL_EDITOR_KINDS = ['materialize', 'unmaterialize']
+const CONFLICT_STATUS = 409
+const NOT_FOUND_STATUS = 404
+const FORBIDDEN_STATUS = 403
+const PICK_INTERVAL_REASON = 'Pick how often it refreshes'
+const INCOMPLETE_INCREMENTAL_REASON = 'Select the incremental column and unique key columns'
+const ACCEPTED_MESSAGES: Record<string, (suggestion: WarehouseSuggestionApi) => string> = {
+ materialize: ({ payload }) => `Materializing ${payload.subject_name}. The first refresh is running.`,
+ unmaterialize: ({ payload }) =>
+ `Stopped materializing ${payload.subject_name}. Each read now computes it from its query.`,
+ certify: ({ payload }) => `Certified ${payload.subject_name}.`,
+ deprecate: ({ payload, created_asset }) =>
+ created_asset?.stopped_materialization === true
+ ? `Marked ${payload.subject_name} as deprecated and stopped its refreshes.`
+ : `Marked ${payload.subject_name} as deprecated.`,
+}
+const ACCEPT_OPTION_FIELDS = new Set([
+ 'refresh_interval_seconds',
+ 'refresh_mode',
+ 'incremental_key',
+ 'unique_key',
+ 'lookback_seconds',
+])
+const NOT_SUGGESTED_AGAIN = 'Dismissed. Not suggested again.'
+const CHECKING_INCREMENTAL_REASON = 'Checking whether it can refresh incrementally'
+
+function dismissedMessage(reason: DismissWarehouseSuggestionApi['reason'], kind: string): string {
+ return reason === 'not_now' ? `Dismissed. ${notNowCondition(kind)}` : NOT_SUGGESTED_AGAIN
+}
+
+function suggestionWorth(suggestion: WarehouseSuggestionApi): [number, number] {
+ const saving = monthlySaving(suggestion.payload)
+ if (!saving) {
+ return [KIND_ORDER[suggestion.kind] ?? 0, 0]
+ }
+ return savesTime(saving) ? [0, -saving.seconds] : [1, -saving.bytes]
+}
+
+function compareSuggestions(a: WarehouseSuggestionApi, b: WarehouseSuggestionApi): number {
+ const [groupA, valueA] = suggestionWorth(a)
+ const [groupB, valueB] = suggestionWorth(b)
+ return groupA - groupB || valueA - valueB
+}
+
+function proposedRefreshDraft(payload: WarehouseSuggestionMaterializePayloadApi): IncrementalConfigDraft {
+ return {
+ enabled: payload.refresh_mode === 'incremental',
+ incrementalKey: payload.incremental_key ?? null,
+ uniqueKey: payload.unique_key ?? [],
+ lookbackSeconds: payload.lookback_seconds ?? 0,
+ }
+}
+
+function changedRefreshOptions(
+ payload: WarehouseSuggestionMaterializePayloadApi,
+ draft: IncrementalConfigDraft
+): AcceptWarehouseSuggestionApi {
+ const proposed = proposedRefreshDraft(payload)
+ const options: AcceptWarehouseSuggestionApi = {}
+ if (draft.enabled !== proposed.enabled) {
+ options.refresh_mode = draft.enabled ? 'incremental' : 'full'
+ }
+ if (!draft.enabled) {
+ return options
+ }
+ const switchedToIncremental = !proposed.enabled
+ if (draft.incrementalKey && (switchedToIncremental || draft.incrementalKey !== proposed.incrementalKey)) {
+ options.incremental_key = draft.incrementalKey
+ }
+ if (
+ draft.uniqueKey.length > 0 &&
+ (switchedToIncremental || draft.uniqueKey.join(',') !== proposed.uniqueKey.join(','))
+ ) {
+ options.unique_key = draft.uniqueKey
+ }
+ if (switchedToIncremental || draft.lookbackSeconds !== proposed.lookbackSeconds) {
+ options.lookback_seconds = draft.lookbackSeconds
+ }
+ return options
+}
+
+async function fetchIncrementalCheck(viewId: string): Promise<IncrementalEligibilityApi | null> {
+ try {
+ const view = await warehouseSavedQueriesRetrieve(projectId(), viewId)
+ return await warehouseSavedQueriesCheckIncrementalCreate(projectId(), { query: view.query.query })
+ } catch {
+ return null
+ }
+}
+
+function sum(values: number[]): number {
+ return values.reduce((total, value) => total + value, 0)
+}
+
+function leaveDisposableKey(id: string): string {
+ return `leave-${id}`
+}
+
+function projectId(): string {
+ return String(ApiConfig.getCurrentTeamId())
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface warehouseSuggestionsLogicValues {
+ acceptConfirmSuggestion: WarehouseSuggestionApi | null
+ acceptConfirmSuggestionId: string | null
+ actionsInFlight: Record<string, boolean>
+ featureForbidden: boolean
+ hiddenSuggestionIds: string[]
+ incrementalCheck: IncrementalEligibilityApi | null
+ incrementalCheckLoading: boolean
+ keepMaterialized: boolean
+ leavingSuggestionIds: string[]
+ listedSuggestions: WarehouseSuggestionApi[]
+ loadError: boolean
+ materializeDisabledReason: string | null
+ materializeError: string | null
+ materializeInterval: DataModelingSyncInterval | null
+ materializeIntervalShorterThanSuggested: boolean
+ materializeModalSuggestion: WarehouseSuggestionApi | null
+ materializeModalSuggestionId: string | null
+ refreshDraft: IncrementalConfigDraft
+ reviewOpen: boolean
+ savingsSummary: SavingsSummary
+ status: WarehouseSuggestionStatusApi | null
+ statusLoading: boolean
+ stripState: StripState
+ suggestions: WarehouseSuggestionApi[] | null
+ suggestionsLoading: boolean
+ surfaceSuggestions: WarehouseSuggestionApi[]
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface warehouseSuggestionsLogicActions {
+ acceptSuggestion: (
+ id: string,
+ options?: AcceptWarehouseSuggestionApi
+ ) => {
+ id: string
+ options: AcceptWarehouseSuggestionApi | undefined
+ }
+ acceptSuggestionFailed: (id: string) => {
+ id: string
+ }
+ closeAcceptConfirm: () => {
+ value: true
+ }
+ closeMaterializeModal: () => {
+ value: true
+ }
+ closeMaterializeModalFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ closeMaterializeModalSuccess: (
+ incrementalCheck: null,
+ payload?: {
+ value: true
+ }
+ ) => {
+ incrementalCheck: null
+ payload?: {
+ value: true
+ }
+ }
+ closeReview: () => {
+ value: true
+ }
+ confirmAccept: () => {
+ value: true
+ }
+ confirmMaterialize: () => {
+ value: true
+ }
+ dismissSuggestion: (
+ id: string,
+ reason: DismissWarehouseSuggestionApi['reason'],
+ note?: string
+ ) => {
+ id: string
+ note: string | undefined
+ reason: WarehouseSuggestionDismissalReasonEnumApi
+ }
+ finishHidingSuggestion: (id: string) => {
+ id: string
+ }
+ hideSuggestion: (id: string) => {
+ id: string
+ }
+ loadIncrementalCheck: (viewId: string) => string
+ loadIncrementalCheckFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadIncrementalCheckSuccess: (
+ incrementalCheck: IncrementalEligibilityApi | null,
+ payload?: string
+ ) => {
+ incrementalCheck: IncrementalEligibilityApi | null
+ payload?: string
+ }
+ loadStatus: () => any
+ loadStatusFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadStatusSuccess: (
+ status: WarehouseSuggestionStatusApi,
+ payload?: any
+ ) => {
+ status: WarehouseSuggestionStatusApi
+ payload?: any
+ }
+ loadSuggestions: () => any
+ loadSuggestionsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadSuggestionsSuccess: (
+ suggestions: WarehouseSuggestionApi[],
+ payload?: any
+ ) => {
+ suggestions: WarehouseSuggestionApi[]
+ payload?: any
+ }
+ openAcceptConfirm: (id: string) => {
+ id: string
+ }
+ openMaterializeModal: (id: string) => {
+ id: string
+ }
+ openReview: () => {
+ value: true
+ }
+ reload: () => {
+ value: true
+ }
+ restoreSuggestion: (id: string) => {
+ id: string
+ }
+ setActionInFlight: (
+ id: string,
+ inFlight: boolean
+ ) => {
+ id: string
+ inFlight: boolean
+ }
+ setKeepMaterialized: (keepMaterialized: boolean) => {
+ keepMaterialized: boolean
+ }
+ setMaterializeError: (error: string | null) => {
+ error: string | null
+ }
+ setMaterializeInterval: (interval: DataModelingSyncInterval | null) => {
+ interval: DataModelingSyncInterval | null
+ }
+ setRefreshDraft: (draft: Partial<IncrementalConfigDraft>) => {
+ draft: Partial<IncrementalConfigDraft>
+ }
+ suggestionAccepted: (suggestion: WarehouseSuggestionApi) => {
+ suggestion: WarehouseSuggestionApi
+ }
+ suggestionResumed: (suggestion: WarehouseSuggestionApi) => {
+ suggestion: WarehouseSuggestionApi
+ }
+ undoDismiss: (id: string) => {
+ id: string
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface warehouseSuggestionsLogicMeta {
+ key: SuggestionSurface
+ __keaTypeGenInternalSelectorTypes: {
+ listedSuggestions: (
+ suggestions: WarehouseSuggestionApi[] | null,
+ hiddenSuggestionIds: string[],
+ surface: SuggestionSurface
+ ) => WarehouseSuggestionApi[]
+ surfaceSuggestions: (
+ listedSuggestions: WarehouseSuggestionApi[],
+ leavingSuggestionIds: string[]
+ ) => WarehouseSuggestionApi[]
+ savingsSummary: (surfaceSuggestions: WarehouseSuggestionApi[]) => SavingsSummary
+ materializeModalSuggestion: (
+ suggestions: WarehouseSuggestionApi[] | null,
+ materializeModalSuggestionId: string | null
+ ) => WarehouseSuggestionApi | null
+ acceptConfirmSuggestion: (
+ suggestions: WarehouseSuggestionApi[] | null,
+ acceptConfirmSuggestionId: string | null
+ ) => WarehouseSuggestionApi | null
+ materializeDisabledReason: (
+ materializeInterval: DataModelingSyncInterval | null,
+ refreshDraft: IncrementalConfigDraft,
+ incrementalCheckLoading: boolean
+ ) => string | null
+ materializeIntervalShorterThanSuggested: (
+ materializeModalSuggestion: WarehouseSuggestionApi | null,
+ materializeInterval: DataModelingSyncInterval | null
+ ) => boolean
+ stripState: (
+ suggestions: WarehouseSuggestionApi[] | null,
+ status: WarehouseSuggestionStatusApi | null,
+ loadError: boolean,
+ featureForbidden: boolean,
+ surfaceSuggestions: WarehouseSuggestionApi[],
+ hiddenSuggestionIds: string[],
+ leavingSuggestionIds: string[]
+ ) => StripState
+ }
+}
+
+export type warehouseSuggestionsLogicType = MakeLogicType<
+ warehouseSuggestionsLogicValues,
+ warehouseSuggestionsLogicActions,
+ WarehouseSuggestionsLogicProps,
+ warehouseSuggestionsLogicMeta
+>
+
+export const warehouseSuggestionsLogic: LogicWrapper<warehouseSuggestionsLogicType> =
+ kea<warehouseSuggestionsLogicType>([
+ props({} as WarehouseSuggestionsLogicProps),
+ key((props) => props.surface),
+ path((key) => ['products', 'warehouseSuggestions', 'warehouseSuggestionsLogic', key]),
+ actions({
+ acceptSuggestion: (id: string, options?: AcceptWarehouseSuggestionApi) => ({ id, options }),
+ confirmMaterialize: true,
+ setRefreshDraft: (draft: Partial<IncrementalConfigDraft>) => ({ draft }),
+ dismissSuggestion: (id: string, reason: DismissWarehouseSuggestionApi['reason'], note?: string) => ({
+ id,
+ reason,
+ note,
+ }),
+ setActionInFlight: (id: string, inFlight: boolean) => ({ id, inFlight }),
+ hideSuggestion: (id: string) => ({ id }),
+ finishHidingSuggestion: (id: string) => ({ id }),
+ restoreSuggestion: (id: string) => ({ id }),
+ undoDismiss: (id: string) => ({ id }),
+ suggestionResumed: (suggestion: WarehouseSuggestionApi) => ({ suggestion }),
+ openReview: true,
+ closeReview: true,
+ openMaterializeModal: (id: string) => ({ id }),
+ closeMaterializeModal: true,
+ openAcceptConfirm: (id: string) => ({ id }),
+ closeAcceptConfirm: true,
+ confirmAccept: true,
+ acceptSuggestionFailed: (id: string) => ({ id }),
+ setKeepMaterialized: (keepMaterialized: boolean) => ({ keepMaterialized }),
+ setMaterializeError: (error: string | null) => ({ error }),
+ setMaterializeInterval: (interval: DataModelingSyncInterval | null) => ({ interval }),
+ suggestionAccepted: (suggestion: WarehouseSuggestionApi) => ({ suggestion }),
+ reload: true,
+ }),
+ loaders(({ values }) => ({
+ suggestions: [
+ null as WarehouseSuggestionApi[] | null,
+ {
+ loadSuggestions: async () =>
+ (await warehouseSuggestionsList(projectId(), { status: 'proposed' })).results,
+ },
+ ],
+ status: [
+ null as WarehouseSuggestionStatusApi | null,
+ {
+ loadStatus: async () => await warehouseSuggestionsStatusRetrieve(projectId()),
+ },
+ ],
+ incrementalCheck: [
+ null as IncrementalEligibilityApi | null,
+ {
+ loadIncrementalCheck: async (viewId: string, breakpoint) => {
+ const check = await fetchIncrementalCheck(viewId)
+ breakpoint()
+ return values.materializeModalSuggestion?.subject_id === viewId ? check : null
+ },
+ closeMaterializeModal: () => null,
+ },
+ ],
+ })),
+ reducers(() => ({
+ suggestions: {
+ suggestionResumed: (state: WarehouseSuggestionApi[] | null, { suggestion }) => [
+ ...(state ?? []).filter((existing) => existing.id !== suggestion.id),
+ suggestion,
+ ],
+ },
+ actionsInFlight: [
+ {} as Record<string, boolean>,
+ {
+ setActionInFlight: (state, { id, inFlight }) => ({ ...state, [id]: inFlight }),
+ },
+ ],
+ leavingSuggestionIds: [
+ [] as string[],
+ {
+ hideSuggestion: (state, { id }) => (state.includes(id) ? state : [...state, id]),
+ finishHidingSuggestion: (state, { id }) => state.filter((leavingId) => leavingId !== id),
+ restoreSuggestion: (state, { id }) => state.filter((leavingId) => leavingId !== id),
+ },
+ ],
+ hiddenSuggestionIds: [
+ [] as string[],
+ {
+ finishHidingSuggestion: (state, { id }) => (state.includes(id) ? state : [...state, id]),
+ restoreSuggestion: (state, { id }) => state.filter((hiddenId) => hiddenId !== id),
+ },
+ ],
+ reviewOpen: [
+ false,
+ {
+ openReview: () => true,
+ closeReview: () => false,
+ },
+ ],
+ materializeModalSuggestionId: [
+ null as string | null,
+ {
+ openMaterializeModal: (_, { id }) => id,
+ closeMaterializeModal: () => null,
+ hideSuggestion: (state, { id }) => (state === id ? null : state),
+ },
+ ],
+ acceptConfirmSuggestionId: [
+ null as string | null,
+ {
+ openAcceptConfirm: (_, { id }) => id,
+ closeAcceptConfirm: () => null,
+ hideSuggestion: (state, { id }) => (state === id ? null : state),
+ acceptSuggestionFailed: (state, { id }) => (state === id ? null : state),
+ },
+ ],
+ keepMaterialized: [
+ false,
+ {
+ setKeepMaterialized: (_, { keepMaterialized }) => keepMaterialized,
+ openAcceptConfirm: () => false,
+ closeAcceptConfirm: () => false,
+ },
+ ],
+ materializeInterval: [
+ null as DataModelingSyncInterval | null,
+ {
+ setMaterializeInterval: (_, { interval }) => interval,
+ closeMaterializeModal: () => null,
+ },
+ ],
+ refreshDraft: [
+ EMPTY_INCREMENTAL_DRAFT as IncrementalConfigDraft,
+ {
+ setRefreshDraft: (state, { draft }) => ({ ...state, ...draft }),
+ closeMaterializeModal: () => EMPTY_INCREMENTAL_DRAFT,
+ },
+ ],
+ materializeError: [
+ null as string | null,
+ {
+ setMaterializeError: (_, { error }) => error,
+ setRefreshDraft: () => null,
+ setMaterializeInterval: () => null,
+ openMaterializeModal: () => null,
+ closeMaterializeModal: () => null,
+ },
+ ],
+ loadError: [
+ false,
+ {
+ reload: () => false,
+ loadSuggestionsFailure: (_, { errorObject }) => !isForbidden(errorObject),
+ loadStatusFailure: (_, { errorObject }) => !isForbidden(errorObject),
+ },
+ ],
+ featureForbidden: [
+ false,
+ {
+ reload: () => false,
+ loadSuggestionsFailure: (forbidden, { errorObject }) => forbidden || isForbidden(errorObject),
+ loadStatusFailure: (forbidden, { errorObject }) => forbidden || isForbidden(errorObject),
+ },
+ ],
+ })),
+ selectors({
+ listedSuggestions: [
+ (s, p) => [s.suggestions, s.hiddenSuggestionIds, p.surface],
+ (
+ suggestions: WarehouseSuggestionApi[] | null,
+ hiddenIds: string[],
+ surface: SuggestionSurface
+ ): WarehouseSuggestionApi[] =>
+ (suggestions ?? [])
+ .filter(
+ (suggestion) =>
+ suggestion.surfaced_at !== null &&
+ !hiddenIds.includes(suggestion.id) &&
+ SURFACE_KINDS[surface].includes(suggestion.kind)
+ )
+ .sort(compareSuggestions),
+ ],
+ surfaceSuggestions: [
+ (s) => [s.listedSuggestions, s.leavingSuggestionIds],
+ (listed: WarehouseSuggestionApi[], leavingIds: string[]): WarehouseSuggestionApi[] =>
+ listed.filter((suggestion) => !leavingIds.includes(suggestion.id)),
+ ],
+ savingsSummary: [
+ (s) => [s.surfaceSuggestions],
+ (open: WarehouseSuggestionApi[]): SavingsSummary => {
+ const savings = open
+ .map(({ payload }) => monthlySaving(payload))
+ .filter((saving): saving is MonthlySaving => saving !== null)
+ const timeSavers = savings.filter(savesTime)
+ const bytesSavers = savings.filter((saving) => !savesTime(saving))
+ return {
+ timeViews: timeSavers.length,
+ savedSeconds: sum(timeSavers.map((saving) => saving.seconds)),
+ bytesViews: bytesSavers.length,
+ savedBytes: sum(bytesSavers.map((saving) => saving.bytes)),
+ }
+ },
+ ],
+ materializeModalSuggestion: [
+ (s) => [s.suggestions, s.materializeModalSuggestionId],
+ (suggestions: WarehouseSuggestionApi[] | null, id: string | null): WarehouseSuggestionApi | null =>
+ (suggestions ?? []).find((suggestion) => suggestion.id === id) ?? null,
+ ],
+ acceptConfirmSuggestion: [
+ (s) => [s.suggestions, s.acceptConfirmSuggestionId],
+ (suggestions: WarehouseSuggestionApi[] | null, id: string | null): WarehouseSuggestionApi | null =>
+ (suggestions ?? []).find((suggestion) => suggestion.id === id) ?? null,
+ ],
+ materializeDisabledReason: [
+ (s) => [s.materializeInterval, s.refreshDraft, s.incrementalCheckLoading],
+ (
+ interval: DataModelingSyncInterval | null,
+ draft: IncrementalConfigDraft,
+ checkLoading: boolean
+ ): string | null => {
+ if (!interval) {
+ return PICK_INTERVAL_REASON
+ }
+ if (checkLoading) {
+ return CHECKING_INCREMENTAL_REASON
+ }
+ if (draft.enabled && (!draft.incrementalKey || draft.uniqueKey.length === 0)) {
+ return INCOMPLETE_INCREMENTAL_REASON
+ }
+ return null
+ },
+ ],
+ materializeIntervalShorterThanSuggested: [
+ (s) => [s.materializeModalSuggestion, s.materializeInterval],
+ (suggestion: WarehouseSuggestionApi | null, interval: DataModelingSyncInterval | null): boolean =>
+ !!suggestion &&
+ !!interval &&
+ isMaterializePayload(suggestion.payload) &&
+ INTERVAL_SECONDS[interval] < suggestion.payload.refresh_interval_seconds,
+ ],
+ stripState: [
+ (s) => [
+ s.suggestions,
+ s.status,
+ s.loadError,
+ s.featureForbidden,
+ s.surfaceSuggestions,
+ s.hiddenSuggestionIds,
+ s.leavingSuggestionIds,
+ ],
+ (
+ suggestions: WarehouseSuggestionApi[] | null,
+ status: WarehouseSuggestionStatusApi | null,
+ loadError: boolean,
+ featureForbidden: boolean,
+ surfaceSuggestions: WarehouseSuggestionApi[],
+ hiddenIds: string[],
+ leavingIds: string[]
+ ): StripState => {
+ if (featureForbidden) {
+ return 'hidden'
+ }
+ if (suggestions === null || status === null) {
+ return loadError ? 'error' : 'loading'
+ }
+ if (surfaceSuggestions.length > 0) {
+ return 'active'
+ }
+ if (hiddenIds.length + leavingIds.length > 0) {
+ return 'all_done'
+ }
+ if (!status.enabled || status.refreshed_at === null) {
+ return 'hidden'
+ }
+ if (!status.eligible) {
+ return 'not_eligible'
+ }
+ return status.days_with_data < status.window_days ? 'warming_up' : 'hidden'
+ },
+ ],
+ }),
+ listeners(({ actions, values, props, cache, selectors }) => {
+ const reportViewedOnce = (): void => {
+ if (cache.viewReported || values.stripState !== 'active') {
+ return
+ }
+ cache.viewReported = true
+ posthog.capture('warehouse suggestions viewed', {
+ surface: props.surface,
+ suggestion_count: values.surfaceSuggestions.length,
+ })
+ }
+ const viewAction = (suggestion: WarehouseSuggestionApi): (() => void) | null => {
+ const opensInSqlEditor = VIEW_IN_SQL_EDITOR_KINDS.includes(suggestion.kind)
+ if (!opensInSqlEditor && !suggestion.created_asset?.certification_id) {
+ return null
+ }
+ const onViewAccepted = props.onViewAccepted
+ if (!onViewAccepted && !opensInSqlEditor) {
+ return null
+ }
+ return () => {
+ actions.closeReview()
+ if (onViewAccepted) {
+ onViewAccepted(suggestion)
+ return
+ }
+ router.actions.push(urls.sqlEditor({ view_id: suggestion.subject_id }))
+ }
+ }
+ return {
+ reload: () => {
+ actions.loadSuggestions()
+ actions.loadStatus()
+ },
+ loadSuggestionsSuccess: () => reportViewedOnce(),
+ loadStatusSuccess: () => reportViewedOnce(),
+ closeMaterializeModal: (_, __, ___, previousState) => {
+ posthog.capture('warehouse suggestion materialize cancelled', {
+ suggestion_id: selectors.materializeModalSuggestionId(previousState),
+ })
+ },
+ openAcceptConfirm: ({ id }) => {
+ posthog.capture('warehouse suggestion confirm opened', {
+ suggestion_id: id,
+ kind: values.acceptConfirmSuggestion?.kind,
+ })
+ },
+ closeAcceptConfirm: (_, __, ___, previousState) => {
+ posthog.capture('warehouse suggestion confirm cancelled', {
+ suggestion_id: selectors.acceptConfirmSuggestionId(previousState),
+ })
+ },
+ confirmAccept: () => {
+ const suggestion = values.acceptConfirmSuggestion
+ if (!suggestion) {
+ return
+ }
+ const keepMaterialized = suggestion.kind === 'deprecate' && values.keepMaterialized
+ actions.acceptSuggestion(suggestion.id, keepMaterialized ? { keep_materialized: true } : undefined)
+ },
+ openReview: () => {
+ posthog.capture('warehouse suggestion review opened', {
+ surface: props.surface,
+ suggestion_count: values.surfaceSuggestions.length,
+ })
+ },
+ openMaterializeModal: ({ id }) => {
+ posthog.capture('warehouse suggestion materialize opened', { suggestion_id: id })
+ const payload = values.suggestions?.find((suggestion) => suggestion.id === id)?.payload
+ if (!payload || !isMaterializePayload(payload)) {
+ actions.setMaterializeInterval(null)
+ return
+ }
+ actions.setMaterializeInterval(intervalForSeconds(payload.refresh_interval_seconds))
+ actions.setRefreshDraft(proposedRefreshDraft(payload))
+ const subjectId = values.suggestions?.find((suggestion) => suggestion.id === id)?.subject_id
+ if (subjectId) {
+ actions.loadIncrementalCheck(subjectId)
+ }
+ },
+ hideSuggestion: ({ id }) => {
+ cache.disposables.add(() => {
+ const timer = setTimeout(() => actions.finishHidingSuggestion(id), ROW_LEAVE_MS)
+ return () => clearTimeout(timer)
+ }, leaveDisposableKey(id))
+ },
+ restoreSuggestion: ({ id }) => {
+ cache.disposables.dispose(leaveDisposableKey(id))
+ },
+ suggestionAccepted: ({ suggestion }) => {
+ const view = viewAction(suggestion)
+ lemonToast.success(ACCEPTED_MESSAGES[suggestion.kind]?.(suggestion) ?? 'Accepted.', {
+ toastId: `warehouse-suggestion-accepted-${suggestion.id}`,
+ button: view ? { label: 'View', action: view } : undefined,
+ })
+ },
+ loadIncrementalCheckSuccess: ({ incrementalCheck }) => {
+ if (!incrementalCheck) {
+ return
+ }
+ if (!incrementalCheck.eligible || incrementalCheck.key_candidates.length === 0) {
+ actions.setRefreshDraft({ enabled: false })
+ return
+ }
+ if (values.refreshDraft.incrementalKey) {
+ return
+ }
+ actions.setRefreshDraft({ incrementalKey: incrementalCheck.key_candidates[0] ?? null })
+ },
+ confirmMaterialize: () => {
+ const suggestion = values.materializeModalSuggestion
+ const interval = values.materializeInterval
+ if (
+ !suggestion ||
+ !interval ||
+ values.materializeDisabledReason ||
+ !isMaterializePayload(suggestion.payload)
+ ) {
+ return
+ }
+ actions.acceptSuggestion(suggestion.id, {
+ refresh_interval_seconds: INTERVAL_SECONDS[interval],
+ ...changedRefreshOptions(suggestion.payload, values.refreshDraft),
+ })
+ },
+ acceptSuggestion: async ({ id, options }) => {
+ if (values.actionsInFlight[id]) {
+ return
+ }
+ actions.setActionInFlight(id, true)
+ try {
+ const accepted = await warehouseSuggestionsAcceptCreate(projectId(), id, options)
+ actions.hideSuggestion(id)
+ actions.suggestionAccepted(accepted)
+ props.onAccepted?.(accepted)
+ } catch (error) {
+ if (error instanceof ApiError && error.attr && ACCEPT_OPTION_FIELDS.has(error.attr)) {
+ actions.setMaterializeError(error.detail)
+ } else {
+ reportAcceptFailure(error)
+ actions.acceptSuggestionFailed(id)
+ if (isStale(error)) {
+ actions.loadSuggestions()
+ }
+ }
+ } finally {
+ actions.setActionInFlight(id, false)
+ }
+ },
+ dismissSuggestion: async ({ id, reason, note }) => {
+ if (values.actionsInFlight[id]) {
+ return
+ }
+ const kind = values.suggestions?.find((suggestion) => suggestion.id === id)?.kind ?? ''
+ const project = projectId()
+ actions.setActionInFlight(id, true)
+ actions.hideSuggestion(id)
+ try {
+ await warehouseSuggestionsDismissCreate(project, id, { reason, note })
+ lemonToast.success(dismissedMessage(reason, kind), {
+ toastId: `warehouse-suggestion-dismissed-${id}`,
+ button: {
+ label: 'Undo',
+ action: () => void resumeDismissedSuggestion(props.surface, project, id),
+ },
+ })
+ } catch (error) {
+ actions.restoreSuggestion(id)
+ reportFailure(error, 'Could not dismiss the suggestion.')
+ if (isStale(error)) {
+ actions.loadSuggestions()
+ }
+ } finally {
+ actions.setActionInFlight(id, false)
+ }
+ },
+ undoDismiss: async ({ id }) => {
+ await resumeDismissedSuggestion(props.surface, projectId(), id)
+ },
+ }
+ }),
+ afterMount(({ actions }) => {
+ actions.reload()
+ }),
+ ])
+
+async function resumeDismissedSuggestion(surface: SuggestionSurface, project: string, id: string): Promise<void> {
+ const mounted = (): ReturnType<typeof warehouseSuggestionsLogic.build> | null =>
+ warehouseSuggestionsLogic.findMounted({ surface })
+ if (mounted()?.values.actionsInFlight[id]) {
+ return
+ }
+ mounted()?.actions.setActionInFlight(id, true)
+ try {
+ const resumed = await warehouseSuggestionsResumeCreate(project, id)
+ mounted()?.actions.suggestionResumed(resumed)
+ mounted()?.actions.restoreSuggestion(id)
+ } catch (error) {
+ reportFailure(error, 'Could not bring the suggestion back.')
+ if (isStale(error)) {
+ mounted()?.actions.loadSuggestions()
+ }
+ } finally {
+ mounted()?.actions.setActionInFlight(id, false)
+ }
+}
+
+function isForbidden(error: unknown): boolean {
+ return error instanceof ApiError && error.status === FORBIDDEN_STATUS
+}
+
+function isConflict(error: unknown): boolean {
+ return error instanceof ApiError && error.status === CONFLICT_STATUS
+}
+
+function isNotFound(error: unknown): boolean {
+ return error instanceof ApiError && error.status === NOT_FOUND_STATUS
+}
+
+function isStale(error: unknown): boolean {
+ return isConflict(error) || isNotFound(error)
+}
+
+function reportAcceptFailure(error: unknown): void {
+ if (isConflict(error) && error instanceof ApiError && error.detail) {
+ lemonToast.error(error.detail)
+ return
+ }
+ reportFailure(error, 'Could not accept the suggestion.')
+}
+
+function reportFailure(error: unknown, fallback: string): void {
+ if (isConflict(error)) {
+ lemonToast.error('Someone already decided this suggestion. The list is up to date now.')
+ return
+ }
+ if (isNotFound(error)) {
+ lemonToast.error('This suggestion is gone, or its view or table was deleted. The list is up to date now.')
+ return
+ }
+ lemonToast.error((error instanceof ApiError && error.detail) || fallback)
+}
diff --git a/products/warehouse_suggestions/package.json b/products/warehouse_suggestions/package.json
index 4cf69165c228..12787a35b750 100644
--- a/products/warehouse_suggestions/package.json
+++ b/products/warehouse_suggestions/package.json
@@ -3,5 +3,23 @@
"scripts": {
"backend:test": "pytest -c ../../pytest.ini --rootdir ../.. backend/tests -v --tb=short",
"backend:contract-check": "echo 'Contract files unchanged'"
+ },
+ "dependencies": {
+ "kea": "catalog:",
+ "kea-loaders": "catalog:",
+ "kea-router": "catalog:",
+ "posthog-js": "catalog:"
+ },
+ "devDependencies": {
+ "@storybook/react": "catalog:",
+ "@testing-library/jest-dom": "^5.17.0",
+ "@testing-library/react": "^14.3.1",
+ "kea-test-utils": "catalog:"
+ },
+ "peerDependencies": {
+ "@posthog/icons": "catalog:",
+ "@types/react": "*",
+ "clsx": "*",
+ "react": "*"
}
}