The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Content Optimizer (#76328)
Kamil Mielnik14d62380fb16
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 77d56c24e24..ea8780f83c2 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -916,6 +916,8 @@query-permissionsquery-processorsearch+ settings+ stalenessutilwarehouse-schema-overlay}:model-exports #{:model/Document}@@ -2893,11 +2895,14 @@:model-imports #{:model/AiUsageLog:model/Card:model/Dashboard+ :model/Document:model/MetabotConversation:model/MetabotMessage:model/PermissionsGroup:model/PermissionsGroupMembership:model/SecurityAdvisory+ :model/Transform+ :model/TransformRun:model/User}}tiles@@ -2976,6 +2981,7 @@run-trackingsearchsettings+ stalenesstasktask.historytracing@@ -3720,6 +3726,35 @@:api #{}:uses #{premium-features}}+ enterprise/content-diagnostics+ {:team "Gadget"+ :api #{metabase-enterprise.content-diagnostics.api+ metabase-enterprise.content-diagnostics.init}+ :uses #{analytics.interface+ api+ app-db+ collections+ documents+ events+ lib.schema+ models+ permissions+ premium-features+ queries+ request+ settings+ task+ task.history+ tracing+ util+ enterprise/staleness}+ :model-imports #{:model/Card+ :model/Collection+ :model/Dashboard+ :model/Document+ :model/TaskHistory+ :model/Transform+ :model/User}}enterprise/content-translation{:team "UX West":api #{metabase-enterprise.content-translation.routes}@@ -4457,8 +4492,7 @@enterprise/staleness{:team "UX West":ns-prefix "metabase-enterprise.stale"- :api #{metabase-enterprise.stale.api- metabase-enterprise.stale.init}+ :api #{metabase-enterprise.stale.api metabase-enterprise.stale.impl metabase-enterprise.stale.init}:uses #{analyticsapicollectionsdiff --git a/e2e/support/helpers/api/archiveDocument.ts b/e2e/support/helpers/api/archiveDocument.tsnew file mode 100644index 00000000000..07fad455290--- /dev/null+++ b/e2e/support/helpers/api/archiveDocument.ts@@ -0,0 +1,11 @@+import type { Document, DocumentId } from "metabase-types/api";++export const archiveDocument = (+ id: DocumentId,+): Cypress.Chainable<Cypress.Response<Document>> => {+ cy.log(`Archiving a document with id: ${id}`);++ return cy.request<Document>("PUT", `/api/document/${id}`, {+ archived: true,+ });+};diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex a6df914f948..c6e25c84b46 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -2,6 +2,7 @@ export { addOrUpdateDashboardCard } from "./addOrUpdateDashboardCard";export { addQuestionToDashboard } from "./addQuestionToDashboard";export { archiveCollection } from "./archiveCollection";export { archiveDashboard } from "./archiveDashboard";+export { archiveDocument } from "./archiveDocument";export { archiveQuestion } from "./archiveQuestion";export { createApiKey } from "./createApiKey";export { createCard } from "./createCard";diff --git a/e2e/support/helpers/e2e-content-diagnostics-helpers.ts b/e2e/support/helpers/e2e-content-diagnostics-helpers.tsnew file mode 100644index 00000000000..51e73c5a0e0--- /dev/null+++ b/e2e/support/helpers/e2e-content-diagnostics-helpers.ts@@ -0,0 +1,40 @@+export type ContentDiagnosticsTab =+ | "stale"+ | "slow"+ | "duplicated"+ | "empty"+ | "sparse"+ | "crowded";++const FINDINGS_ALIAS = "findings";++// Empty, sparse and crowded are views over the same endpoint.+const TAB_ENDPOINTS: Record<ContentDiagnosticsTab, string> = {+ stale: "/api/ee/content-diagnostics/stale",+ slow: "/api/ee/content-diagnostics/slow",+ duplicated: "/api/ee/content-diagnostics/duplicated",+ empty: "/api/ee/content-diagnostics/imbalanced",+ sparse: "/api/ee/content-diagnostics/imbalanced",+ crowded: "/api/ee/content-diagnostics/imbalanced",+};++export function runContentDiagnosticsScan() {+ return cy.request("POST", "/api/testing/content-diagnostics/scan");+}++export function visitContentDiagnosticsTab(tab: ContentDiagnosticsTab) {+ cy.intercept("GET", `${TAB_ENDPOINTS[tab]}*`).as(FINDINGS_ALIAS);+ cy.visit(`/monitor/content-diagnostics/${tab}`);+ cy.wait(`@${FINDINGS_ALIAS}`);+}++export function findImbalancedContentFindingRow(name: string) {+ return cy+ .findByTestId("imbalanced-content-list")+ .contains('[role="row"]', name);+}++export function searchFindings(term: string) {+ cy.H.main().findByLabelText("Search").type(term);+ cy.wait(`@${FINDINGS_ALIAS}`);+}diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex 6c3b5d2f048..66381e21bbe 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -11,6 +11,7 @@ export * from "./e2e-codemirror-helpers";export * from "./e2e-collection-helpers";export * from "./e2e-command-palette-helpers";export * from "./e2e-comment-helpers";+export * from "./e2e-content-diagnostics-helpers";export * from "./e2e-custom-column-helpers";export * from "./e2e-custom-viz-helpers";export * from "./data-app-test-env";diff --git a/e2e/test/scenarios/monitor/content-diagnostics/archived.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/archived.cy.spec.tsnew file mode 100644index 00000000000..0c91ae9b421--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/archived.cy.spec.ts@@ -0,0 +1,74 @@+const { H } = cy;++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "archiving";++const LIVE_COLLECTION_NAME = "E2E archiving live collection";+const LIVE_DASHBOARD_NAME = "E2E archiving live dashboard";+const LIVE_DOCUMENT_NAME = "E2E archiving live document";++const TRASHED_COLLECTION_NAME = "E2E archiving trashed collection";+const TRASHED_DASHBOARD_NAME = "E2E archiving trashed dashboard";+const TRASHED_DOCUMENT_NAME = "E2E archiving trashed document";++const LIVE = [LIVE_COLLECTION_NAME, LIVE_DASHBOARD_NAME, LIVE_DOCUMENT_NAME];+const TRASHED = [+ TRASHED_COLLECTION_NAME,+ TRASHED_DASHBOARD_NAME,+ TRASHED_DOCUMENT_NAME,+];++const EMPTY_DOCUMENT = { type: "doc", content: [] };++function createContent() {+ H.createCollection({ name: LIVE_COLLECTION_NAME });+ H.createDashboard({ name: LIVE_DASHBOARD_NAME });+ H.createDocument({ name: LIVE_DOCUMENT_NAME, document: EMPTY_DOCUMENT });++ H.createCollection({ name: TRASHED_COLLECTION_NAME }).then(+ ({ body: collection }) => H.archiveCollection(collection.id),+ );+ H.createDashboard({ name: TRASHED_DASHBOARD_NAME }).then(+ ({ body: dashboard }) => H.archiveDashboard(dashboard.id),+ );+ H.createDocument({+ name: TRASHED_DOCUMENT_NAME,+ document: EMPTY_DOCUMENT,+ }).then(({ body: document }) => H.archiveDocument(document.id));+}++describe("scenarios > monitor > content diagnostics > archived", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports live content and passes over its archived counterparts", () => {+ createContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.log("every live entity is reported");+ cy.findByTestId("imbalanced-content-list").within(() => {+ LIVE.forEach((name) => {+ cy.findByText(name).should("be.visible");+ });+ });++ cy.log("and none of the archived ones are");+ TRASHED.forEach((name) => {+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ name,+ );+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/bulk-dismiss.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/bulk-dismiss.cy.spec.tsnew file mode 100644index 00000000000..698cfc37e72--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/bulk-dismiss.cy.spec.ts@@ -0,0 +1,72 @@+const { H } = cy;++import type { Dashboard, DashboardId } from "metabase-types/api";++const SEARCH_TERM = "dismissal";+const FIRST_DASHBOARD_NAME = "E2E dismissal first dashboard";+const SECOND_DASHBOARD_NAME = "E2E dismissal second dashboard";++describe("scenarios > monitor > content diagnostics > bulk dismissal", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("dismisses selected findings without trashing their content", () => {+ H.createDashboard(+ { name: FIRST_DASHBOARD_NAME },+ { wrapId: true, idAlias: "firstDashboardId" },+ );+ H.createDashboard(+ { name: SECOND_DASHBOARD_NAME },+ { wrapId: true, idAlias: "secondDashboardId" },+ );+ H.runContentDiagnosticsScan();++ H.visitContentDiagnosticsTab("empty");+ H.searchFindings(SEARCH_TERM);++ cy.intercept("POST", "/api/ee/content-diagnostics/invalidate").as(+ "dismissFindings",+ );+ cy.intercept({+ method: "GET",+ pathname: "/api/ee/content-diagnostics/imbalanced",+ query: { query: SEARCH_TERM },+ }).as("refreshedFindings");++ H.findImbalancedContentFindingRow(FIRST_DASHBOARD_NAME)+ .findByRole("checkbox")+ .click();+ H.findImbalancedContentFindingRow(SECOND_DASHBOARD_NAME)+ .findByRole("checkbox")+ .click();++ cy.findByTestId("content-diagnostics-bulk-actions").within(() => {+ cy.findByText("2 items selected").should("be.visible");+ cy.findByRole("button", { name: "Dismiss findings" }).click();+ });+ H.modal().within(() => {+ cy.findByText("Dismiss 2 findings?").should("be.visible");+ cy.findByRole("button", { name: "Dismiss findings" }).click();+ });++ cy.wait("@dismissFindings").its("response.statusCode").should("eq", 200);+ H.undoToast().findByText("Dismissed 2 findings").should("be.visible");+ cy.wait("@refreshedFindings").its("response.body.total").should("eq", 0);+ H.main().findByText("No empty content found").should("be.visible");+ cy.findByTestId("content-diagnostics-bulk-actions").should("not.exist");++ cy.log("Both dashboards should still exist");+ cy.wrap(["@firstDashboardId", "@secondDashboardId"]).each(+ (alias: string) => {+ cy.get<DashboardId>(alias).then((id) => {+ cy.request<Dashboard>("GET", `/api/dashboard/${id}`)+ .its("body.archived")+ .should("eq", false);+ });+ },+ );+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/bulk-trash.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/bulk-trash.cy.spec.tsnew file mode 100644index 00000000000..a73144cc52b--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/bulk-trash.cy.spec.ts@@ -0,0 +1,100 @@+const { H } = cy;++import {+ ALL_USERS_GROUP_ID,+ COLLECTION_GROUP_ID,+ DATA_GROUP_ID,+ NORMAL_USER_ID,+} from "e2e/support/cypress_sample_instance_data";++const SEARCH_TERM = "trashing";+const FIRST_DASHBOARD_NAME = "E2E trashing first dashboard";+const SECOND_DASHBOARD_NAME = "E2E trashing second dashboard";++const WRITABLE_DASHBOARD_NAME = "E2E trashing writable dashboard";+const READABLE_DASHBOARD_NAME = "E2E trashing readable dashboard";+const READABLE_COLLECTION_NAME = "E2E trashing readable collection";++const LIST = "imbalanced-content-list";++describe("scenarios > monitor > content diagnostics > bulk trash", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("moves the selected findings to the trash and drops them from the list", () => {+ H.createDashboard({ name: FIRST_DASHBOARD_NAME });+ H.createDashboard({ name: SECOND_DASHBOARD_NAME });+ H.runContentDiagnosticsScan();++ H.visitContentDiagnosticsTab("empty");+ H.searchFindings(SEARCH_TERM);++ H.findImbalancedContentFindingRow(FIRST_DASHBOARD_NAME)+ .findByRole("checkbox")+ .click();+ H.findImbalancedContentFindingRow(SECOND_DASHBOARD_NAME)+ .findByRole("checkbox")+ .click();++ cy.findByTestId("toast-card").within(() => {+ cy.findByText("2 items selected").should("be.visible");+ cy.findByRole("button", { name: "Move to trash" }).click();+ });++ H.modal().within(() => {+ cy.findByText("Move 2 items to trash?").should("be.visible");+ cy.findByRole("button", { name: "Move to trash" }).click();+ });++ H.undoToast().findByText("Moved 2 items to the trash").should("be.visible");++ cy.log(+ "trashing the entities invalidates their findings, so the list empties",+ );+ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);+ cy.findByTestId(LIST).should("not.contain.text", SECOND_DASHBOARD_NAME);+ });++ it("disables trash when an analyst selects findings for read-only entities", () => {+ H.createDashboard({ name: WRITABLE_DASHBOARD_NAME });+ H.createCollection({ name: READABLE_COLLECTION_NAME }).then(+ ({ body: collection }) => {+ H.createDashboard({+ name: READABLE_DASHBOARD_NAME,+ collection_id: collection.id,+ });+ cy.updateCollectionGraph({+ [ALL_USERS_GROUP_ID]: { [collection.id]: "read" as const },+ [COLLECTION_GROUP_ID]: { [collection.id]: "read" as const },+ [DATA_GROUP_ID]: { [collection.id]: "read" as const },+ });+ },+ );+ H.runContentDiagnosticsScan();++ H.setUserAsAnalyst(NORMAL_USER_ID);+ cy.signInAsNormalUser();++ H.visitContentDiagnosticsTab("empty");+ H.searchFindings(SEARCH_TERM);++ H.findImbalancedContentFindingRow(WRITABLE_DASHBOARD_NAME)+ .findByRole("checkbox")+ .should("be.enabled")+ .click();+ H.findImbalancedContentFindingRow(READABLE_DASHBOARD_NAME)+ .findByRole("checkbox")+ .should("be.enabled")+ .click();++ cy.findByTestId("content-diagnostics-bulk-actions").within(() => {+ cy.findByRole("button", { name: "Move to trash" }).should("be.disabled");+ cy.findByRole("button", { name: "Dismiss findings" }).should(+ "be.enabled",+ );+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/crowded.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/crowded.cy.spec.tsnew file mode 100644index 00000000000..5f53bc37b41--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/crowded.cy.spec.ts@@ -0,0 +1,128 @@+const { H } = cy;++import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";+import { ORDERS_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";++import {+ documentEmbedding,+ runContentDiagnosticsScan,+ searchFindings,+ setThreshold,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const { ORDERS_ID } = SAMPLE_DATABASE;++const SEARCH_TERM = "crowding";+const COLLECTION_NAME = "E2E crowding collection";+const TABBED_DASHBOARD_NAME = "E2E crowding tabbed dashboard";+const PACKED_DASHBOARD_NAME = "E2E crowding packed dashboard";+const DOCUMENT_NAME = "E2E crowding document";++const CHILD_QUESTION_NAMES = [+ "E2E child question A",+ "E2E child question B",+ "E2E child question C",+];++const COLLECTION_ITEM_LIMIT = 2;+const DASHBOARD_TAB_LIMIT = 1;+const DASHCARDS_PER_TAB_LIMIT = 1;+const DOCUMENT_CARD_LIMIT = 1;++const TABS = [+ { id: 1, name: "First tab" },+ { id: 2, name: "Second tab" },+];++const CROWDED = [+ { name: COLLECTION_NAME, count: "3 items" },+ { name: PACKED_DASHBOARD_NAME, count: "2 dashcards on one tab" },+ { name: TABBED_DASHBOARD_NAME, count: "2 tabs" },+ { name: DOCUMENT_NAME, count: "2 cards" },+];++function lowerThresholds() {+ setThreshold(+ "content-diagnostics-crowded-collection-threshold-items",+ COLLECTION_ITEM_LIMIT,+ );+ setThreshold(+ "content-diagnostics-crowded-dashboard-threshold-tabs",+ DASHBOARD_TAB_LIMIT,+ );+ setThreshold(+ "content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab",+ DASHCARDS_PER_TAB_LIMIT,+ );+ setThreshold(+ "content-diagnostics-crowded-document-threshold-cards",+ DOCUMENT_CARD_LIMIT,+ );+}++function createCrowdedContent() {+ H.createCollection({ name: COLLECTION_NAME }).then(({ body: collection }) => {+ CHILD_QUESTION_NAMES.forEach((name) => {+ H.createQuestion({+ name,+ collection_id: collection.id,+ query: { "source-table": ORDERS_ID },+ });+ });+ });++ H.createDashboard({ name: PACKED_DASHBOARD_NAME }).then(+ ({ body: dashboard }) => {+ H.addQuestionToDashboard({+ dashboardId: dashboard.id,+ cardId: ORDERS_QUESTION_ID,+ });+ H.addQuestionToDashboard({+ dashboardId: dashboard.id,+ cardId: ORDERS_QUESTION_ID,+ });+ },+ );++ H.createDashboardWithTabs({ name: TABBED_DASHBOARD_NAME, tabs: TABS });++ H.createDocument({+ name: DOCUMENT_NAME,+ document: documentEmbedding([ORDERS_QUESTION_ID, ORDERS_QUESTION_ID]),+ });+}++describe("scenarios > monitor > content diagnostics > crowded", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports every type that holds too much, on the axis it crossed", () => {+ lowerThresholds();+ createCrowdedContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("crowded");+ searchFindings(SEARCH_TERM);++ cy.log("each subject is reported, having crossed a bound of its own");+ cy.findByTestId("imbalanced-content-list").within(() => {+ CROWDED.forEach(({ name }) => {+ cy.findByText(name).should("be.visible");+ });+ });++ cy.log("the sidebar counts each one in the unit of the bound it crossed");+ CROWDED.forEach(({ name, count }) => {+ cy.findByTestId("imbalanced-content-list").findByText(name).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Content count").should("be.visible");+ cy.findByText(count).should("be.visible");+ });+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/duplicated.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/duplicated.cy.spec.tsnew file mode 100644index 00000000000..db792fb90ed--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/duplicated.cy.spec.ts@@ -0,0 +1,135 @@+const { H } = cy;++import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const { ORDERS_ID } = SAMPLE_DATABASE;++const SEARCH_TERM = "duplicated";++const NAME = "E2E duplicated question";+const NAME_VARIANT = "e2e DUPLICATED question";+const BOTH_NAMES = /e2e\s+duplicated\s+question/i;++const COLLECTION_NAME = "E2E duplicated collection";+const DASHBOARD_NAME = "E2E duplicated dashboard";+const DOCUMENT_NAME = "E2E duplicated document";++const CROSS_KIND_NAME = "E2E duplicated hybrid";++const QUERY = { "source-table": ORDERS_ID };+const EMPTY_DOCUMENT = { type: "doc", content: [] };++function createDuplicatePair() {+ H.createQuestion({ name: NAME, query: QUERY });+ H.createQuestion({ name: NAME_VARIANT, query: QUERY });+}++function createDuplicatesOfEveryType() {+ H.createCollection({ name: COLLECTION_NAME });+ H.createCollection({ name: COLLECTION_NAME });++ H.createDashboard({ name: DASHBOARD_NAME });+ H.createDashboard({ name: DASHBOARD_NAME });++ H.createDocument({ name: DOCUMENT_NAME, document: EMPTY_DOCUMENT });+ H.createDocument({ name: DOCUMENT_NAME, document: EMPTY_DOCUMENT });+}++describe("scenarios > monitor > content diagnostics > duplicated", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("lists both sides of a name cluster, each pointing at the other", () => {+ createDuplicatePair();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("duplicated");+ searchFindings(SEARCH_TERM);++ cy.log(+ "both questions are reported, despite differing in case and spacing",+ );+ cy.findByTestId("duplicated-content-list")+ .findAllByText(BOTH_NAMES)+ .should("have.length", 2);++ cy.log("opening one shows the other as its duplicate");+ cy.findByTestId("duplicated-content-list").findByText(NAME).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duplicates (1)").should("be.visible");+ cy.findByRole("region", { name: "Duplicates" }).should(+ "contain.text",+ NAME_VARIANT,+ );+ });+ });++ it("clusters collections, dashboards and documents, not only questions", () => {+ createDuplicatesOfEveryType();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("duplicated");+ searchFindings(SEARCH_TERM);++ cy.log("every type the checker covers reports both sides of its cluster");+ [COLLECTION_NAME, DASHBOARD_NAME, DOCUMENT_NAME].forEach((name) => {+ cy.findByTestId("duplicated-content-list")+ .findAllByText(name)+ .should("have.length", 2);+ });++ cy.log("and each side names the other as its duplicate");+ cy.findByTestId("duplicated-content-list")+ .findAllByText(COLLECTION_NAME)+ .first()+ .click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duplicates (1)").should("be.visible");+ cy.findByRole("region", { name: "Duplicates" }).should(+ "contain.text",+ COLLECTION_NAME,+ );+ });+ });++ it("treats a question and a model sharing a name as duplicates", () => {+ H.createQuestion({ name: CROSS_KIND_NAME, query: QUERY });+ H.createQuestion({ name: CROSS_KIND_NAME, type: "model", query: QUERY });+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("duplicated");+ searchFindings(SEARCH_TERM);++ cy.log("both sub-kinds are reported, with their own type icons");+ cy.findByTestId("duplicated-content-list").within(() => {+ cy.findAllByText(CROSS_KIND_NAME).should("have.length", 2);+ cy.findByRole("img", { name: "Question" }).should("be.visible");+ cy.findByRole("img", { name: "Model" }).should("be.visible");+ });++ cy.log("the question counts the model as its duplicate");+ cy.findByTestId("duplicated-content-list")+ .findAllByText(CROSS_KIND_NAME)+ .first()+ .click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duplicates (1)").should("be.visible");+ cy.findByRole("region", { name: "Duplicates" }).should(+ "contain.text",+ CROSS_KIND_NAME,+ );+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/empty.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/empty.cy.spec.tsnew file mode 100644index 00000000000..e541f374013--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/empty.cy.spec.ts@@ -0,0 +1,69 @@+const { H } = cy;++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "emptiness";+const COLLECTION_NAME = "E2E emptiness collection";+const DASHBOARD_NAME = "E2E emptiness dashboard";+const DOCUMENT_NAME = "E2E emptiness document";+const QUESTION_NAME = "E2E emptiness question";++const EMPTY = [+ { name: COLLECTION_NAME, count: "0 items" },+ { name: DASHBOARD_NAME, count: "0 dashcards" },+ { name: DOCUMENT_NAME, count: "0 cards" },+ { name: QUESTION_NAME, count: "0 rows" },+];++function createEmptyContent() {+ H.createCollection({ name: COLLECTION_NAME });+ H.createDashboard({ name: DASHBOARD_NAME });+ H.createDocument({+ name: DOCUMENT_NAME,+ document: { type: "doc", content: [] },+ });++ H.createNativeQuestion({+ name: QUESTION_NAME,+ native: { query: "SELECT * FROM ORDERS WHERE ID < 0" },+ }).then(({ body: card }) => {+ cy.request("POST", `/api/card/${card.id}/query`);+ });+}++describe("scenarios > monitor > content diagnostics > empty", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports every type of content that has nothing in it", () => {+ createEmptyContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.log("all four entity types are reported by the one checker");+ cy.findByTestId("imbalanced-content-list").within(() => {+ EMPTY.forEach(({ name }) => {+ cy.findByText(name).should("be.visible");+ });+ });++ cy.log("the sidebar counts each type in the unit that suits it");+ EMPTY.forEach(({ name, count }) => {+ cy.findByTestId("imbalanced-content-list").findByText(name).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Content count").should("be.visible");+ cy.findByText(count).should("be.visible");+ });+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/helpers/content-diagnostics-helpers.ts b/e2e/test/scenarios/monitor/content-diagnostics/helpers/content-diagnostics-helpers.tsnew file mode 100644index 00000000000..c4f5b8e96e5--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/helpers/content-diagnostics-helpers.ts@@ -0,0 +1,68 @@+import type {+ CardId,+ ContentDiagnosticsEntityId,+ DocumentContent,+} from "metabase-types/api";++export {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "e2e/support/helpers/e2e-content-diagnostics-helpers";++export type StaleModel = "card" | "dashboard" | "document" | "transform";++export type ThresholdSetting =+ | "content-diagnostics-stale-threshold-days"+ | "content-diagnostics-slow-card-threshold-seconds"+ | "content-diagnostics-slow-transform-threshold-seconds"+ | "content-diagnostics-crowded-collection-threshold-items"+ | "content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab"+ | "content-diagnostics-crowded-dashboard-threshold-tabs"+ | "content-diagnostics-crowded-document-threshold-cards"+ | "content-diagnostics-sparse-collection-threshold-items"+ | "content-diagnostics-sparse-dashboard-threshold-dashcards";++// `H.updateSetting` / `H.updateEnterpriseSetting` accepts admin-visible settings, content+// diagnostics thresholds aren't exposed in that type, so we call API directly.+export function setThreshold(setting: ThresholdSetting, value: number) {+ return cy.request("PUT", `/api/setting/${setting}`, { value });+}++// `dateStr` is a "YYYY-MM-DD" date; the endpoint backdates by seven months when it is omitted.+export function markStale(+ model: StaleModel,+ id: ContentDiagnosticsEntityId,+ dateStr?: string,+) {+ return cy.request("POST", "/api/testing/mark-stale", {+ id,+ model,+ ...(dateStr ? { "date-str": dateStr } : {}),+ });+}++export function documentEmbedding(cardIds: CardId[]): DocumentContent {+ return {+ type: "doc",+ content: cardIds.map((id, index) => ({+ type: "resizeNode",+ attrs: { height: 400, minHeight: 280 },+ content: [+ {+ type: "cardEmbed",+ attrs: { id, name: null, _id: String(index + 1) },+ },+ ],+ })),+ };+}++// Creating a document clones any card its embeds point at into a document-owned copy and rewrites the embed, so the stored ids are not the ones it was created with.+export function embeddedCardIds(document: DocumentContent): CardId[] {+ return (document.content ?? []).flatMap((block) =>+ (block.content ?? [])+ .filter((node) => node.type === "cardEmbed")+ .map((node) => node.attrs?.id),+ );+}diff --git a/e2e/test/scenarios/monitor/content-diagnostics/overlapping.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/overlapping.cy.spec.tsnew file mode 100644index 00000000000..9dabefb17c9--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/overlapping.cy.spec.ts@@ -0,0 +1,58 @@+const { H } = cy;++import {+ runContentDiagnosticsScan,+ searchFindings,+ setThreshold,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "overlapping";+const DASHBOARD_NAME = "E2E overlapping dashboard";++const DASHBOARD_TAB_LIMIT = 1;++const TABS = [+ { id: 1, name: "First tab" },+ { id: 2, name: "Second tab" },+];++describe("scenarios > monitor > content diagnostics > overlapping findings", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ // The checkers run independently, so one entity can cross two unrelated bounds at once.+ it("reports a dashboard that is crowded and empty at the same time", () => {+ setThreshold(+ "content-diagnostics-crowded-dashboard-threshold-tabs",+ DASHBOARD_TAB_LIMIT,+ );+ H.createDashboardWithTabs({ name: DASHBOARD_NAME, tabs: TABS });+ runContentDiagnosticsScan();++ cy.log("its tab count puts it over the crowded bound");+ visitContentDiagnosticsTab("crowded");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("imbalanced-content-list")+ .findByText(DASHBOARD_NAME)+ .click();+ cy.findByTestId("content-diagnostics-sidebar")+ .findByText("2 tabs")+ .should("be.visible");++ cy.log("and holding no cards at all makes it empty on the same scan");+ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("imbalanced-content-list")+ .findByText(DASHBOARD_NAME)+ .click();+ cy.findByTestId("content-diagnostics-sidebar")+ .findByText("0 dashcards")+ .should("be.visible");+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/permissions.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/permissions.cy.spec.tsnew file mode 100644index 00000000000..50db225dd9d--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/permissions.cy.spec.ts@@ -0,0 +1,89 @@+const { H } = cy;++import {+ ALL_USERS_GROUP_ID,+ COLLECTION_GROUP_ID,+ DATA_GROUP_ID,+ NORMAL_USER_ID,+} from "e2e/support/cypress_sample_instance_data";++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "permissioned";+const READABLE_DASHBOARD_NAME = "E2E permissioned readable dashboard";+const HIDDEN_DASHBOARD_NAME = "E2E permissioned hidden dashboard";+const HIDDEN_COLLECTION_NAME = "E2E permissioned hidden collection";++const CONTENT_DIAGNOSTICS_PATH = "/monitor/content-diagnostics/stale";+const UPSELL_TITLE = "Find and clean up stale content without hunting it down";++describe("scenarios > monitor > content diagnostics > permissions", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ });++ it("shows the upsell and asks for no findings without the token", () => {+ cy.intercept("GET", "/api/ee/content-diagnostics/**").as("findings");++ cy.visit(CONTENT_DIAGNOSTICS_PATH);+ H.main().findByText(UPSELL_TITLE).should("be.visible");++ cy.get("@findings.all").should("have.length", 0);+ });++ describe("with the token", () => {+ beforeEach(() => {+ H.activateToken("bleeding-edge");+ });++ it("shows a data analyst only the findings they can read", () => {+ H.createDashboard({ name: READABLE_DASHBOARD_NAME });+ H.createCollection({ name: HIDDEN_COLLECTION_NAME }).then(+ ({ body: collection }) => {+ H.createDashboard({+ name: HIDDEN_DASHBOARD_NAME,+ collection_id: collection.id,+ });+ cy.updateCollectionGraph({+ [ALL_USERS_GROUP_ID]: { [collection.id]: "none" as const },+ [COLLECTION_GROUP_ID]: { [collection.id]: "none" as const },+ [DATA_GROUP_ID]: { [collection.id]: "none" as const },+ });+ },+ );+ runContentDiagnosticsScan();++ H.setUserAsAnalyst(NORMAL_USER_ID);+ cy.signInAsNormalUser();++ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("imbalanced-content-list")+ .findByText(READABLE_DASHBOARD_NAME)+ .should("be.visible");+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ HIDDEN_DASHBOARD_NAME,+ );+ });++ it("lets in a monitoring user who is not an analyst", () => {+ H.updateAdvancedPermissionsGraph({+ [ALL_USERS_GROUP_ID]: { monitoring: "yes" },+ });+ cy.signInAsNormalUser();++ cy.visit("/monitor");+ cy.findByRole("link", { name: /Content diagnostics/ }).should("exist");++ visitContentDiagnosticsTab("stale");+ cy.location("pathname").should("eq", CONTENT_DIAGNOSTICS_PATH);+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/remembered-filters.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/remembered-filters.cy.spec.tsnew file mode 100644index 00000000000..c26fd09435e--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/remembered-filters.cy.spec.ts@@ -0,0 +1,61 @@+const { H } = cy;++import { visitContentDiagnosticsTab } from "./helpers/content-diagnostics-helpers";++const STALE_INDEX_PATH = "/monitor/content-diagnostics/stale";++function openFilterPicker() {+ cy.findByTestId("content-diagnostics-filter-button").click();+}++function closeFilterPicker() {+ cy.get("body").type("{esc}");+}++function dropDashboardsFromTheFilter() {+ openFilterPicker();+ H.popover().findByLabelText("Dashboards").click();+ cy.wait("@findings");+ closeFilterPicker();+}++describe("scenarios > monitor > content diagnostics > remembered filters", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("brings back the filters last used on the tab", () => {+ visitContentDiagnosticsTab("stale");+ dropDashboardsFromTheFilter();+ cy.location("search").should("contain", "entity-types");++ cy.log("leave the tab and come back to it with a bare url");+ visitContentDiagnosticsTab("slow");+ visitContentDiagnosticsTab("stale");++ cy.log("the url is rewritten with what was last used here");+ cy.location("search").should("contain", "entity-types");++ openFilterPicker();+ H.popover().findByLabelText("Dashboards").should("not.be.checked");+ H.popover().findByLabelText("Questions").should("be.checked");+ });++ it("gives way to filters that came in on the url", () => {+ visitContentDiagnosticsTab("stale");+ dropDashboardsFromTheFilter();++ cy.log("the url asks for the opposite of what was remembered");+ cy.intercept("GET", "/api/ee/content-diagnostics/stale*").as("findings");+ cy.visit(`${STALE_INDEX_PATH}?entity-types=dashboard`);+ cy.wait("@findings");++ cy.location("search").should("contain", "entity-types=dashboard");++ openFilterPicker();+ H.popover().findByLabelText("Dashboards").should("be.checked");+ H.popover().findByLabelText("Questions").should("not.be.checked");+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/rescan.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/rescan.cy.spec.tsnew file mode 100644index 00000000000..4774ca32faf--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/rescan.cy.spec.ts@@ -0,0 +1,70 @@+const { H } = cy;++import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const { ORDERS_ID } = SAMPLE_DATABASE;++const SEARCH_TERM = "rescanning";+const COLLECTION_NAME = "E2E rescanning collection";+const DASHBOARD_NAME = "E2E rescanning dashboard";+const CHILD_QUESTION_NAME = "E2E child question";++describe("scenarios > monitor > content diagnostics > rescanning", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("drops a finding once the problem is fixed or the entity is gone", () => {+ H.createCollection({ name: COLLECTION_NAME }).then(({ body: collection }) =>+ cy.wrap(collection.id).as("collectionId"),+ );+ H.createDashboard({ name: DASHBOARD_NAME }).then(({ body: dashboard }) =>+ cy.wrap(dashboard.id).as("dashboardId"),+ );+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.log("both start out empty, and are reported as such");+ cy.findByTestId("imbalanced-content-list").within(() => {+ cy.findByText(COLLECTION_NAME).should("be.visible");+ cy.findByText(DASHBOARD_NAME).should("be.visible");+ });++ cy.get("@collectionId").then((collectionId) => {+ H.createQuestion({+ name: CHILD_QUESTION_NAME,+ collection_id: Number(collectionId),+ query: { "source-table": ORDERS_ID },+ });+ });+ cy.get("@dashboardId").then((dashboardId) => {+ cy.request("DELETE", `/api/dashboard/${dashboardId}`);+ });+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.log("the collection is no longer empty, so its finding is gone");+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ COLLECTION_NAME,+ );++ cy.log("and the deleted dashboard takes its finding with it");+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ DASHBOARD_NAME,+ );+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/slow.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/slow.cy.spec.tsnew file mode 100644index 00000000000..8e4530a5bc4--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/slow.cy.spec.ts@@ -0,0 +1,104 @@+const { H } = cy;++import { WRITABLE_DB_ID } from "e2e/support/cypress_data";++import {+ documentEmbedding,+ embeddedCardIds,+ runContentDiagnosticsScan,+ searchFindings,+ setThreshold,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "slowness";+const CARD_NAME = "E2E slowness question";+const EMBEDDED_CARD_NAME = "E2E slowness embedded question";+const DASHBOARD_NAME = "E2E slowness dashboard";+const DOCUMENT_NAME = "E2E slowness document";++const SLEEP_SECONDS = 2;+const THRESHOLD_SECONDS = 1;+const SLOW_QUERY = `SELECT pg_sleep(${SLEEP_SECONDS})`;++const SECONDS_DURATION = /^\d+\.\ds$/;++// EMBEDDED_CARD_NAME is absent by design: embedding copies the card, and a document's own copies+// are not finding subjects. Only the copy is ever executed below, so the name reaches the slow+// list through the document roll-up alone.+const SLOW = [CARD_NAME, DASHBOARD_NAME, DOCUMENT_NAME];++function createSlowQuestion(name: string) {+ return H.createNativeQuestion({+ name,+ database: WRITABLE_DB_ID,+ native: { query: SLOW_QUERY },+ });+}++function createSlowContent() {+ createSlowQuestion(CARD_NAME).then(({ body: card }) => {+ cy.request("POST", `/api/card/${card.id}/query`);++ H.createDashboard({ name: DASHBOARD_NAME }).then(({ body: dashboard }) => {+ H.addQuestionToDashboard({ dashboardId: dashboard.id, cardId: card.id });+ });+ });++ createSlowQuestion(EMBEDDED_CARD_NAME).then(({ body: card }) => {+ H.createDocument({+ name: DOCUMENT_NAME,+ document: documentEmbedding([card.id]),+ }).then(({ body: document }) => {+ embeddedCardIds(document.document).forEach((id) => {+ cy.request("POST", `/api/card/${id}/query`);+ });+ });+ });+}++describe(+ "scenarios > monitor > content diagnostics > slow",+ { tags: "@external" },+ () => {+ beforeEach(() => {+ H.restore("postgres-12");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports slow cards, and the dashboard and document that embed them", () => {+ setThreshold(+ "content-diagnostics-slow-card-threshold-seconds",+ THRESHOLD_SECONDS,+ );+ createSlowContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("slow");+ searchFindings(SEARCH_TERM);++ cy.log("the measured cards are reported, and so are both roll-ups");+ cy.findByTestId("slow-content-list").within(() => {+ SLOW.forEach((name) => {+ cy.findByText(name).should("be.visible");+ });+ });++ cy.log("the document's own copy of its card is not a finding of its own");+ cy.findByTestId("slow-content-list")+ .findByText(EMBEDDED_CARD_NAME)+ .should("not.exist");++ cy.log("each one reports a duration on the scale of the query itself");+ SLOW.forEach((name) => {+ cy.findByTestId("slow-content-list").findByText(name).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duration").should("be.visible");+ cy.findByText(SECONDS_DURATION).should("be.visible");+ });+ });+ });+ },+);diff --git a/e2e/test/scenarios/monitor/content-diagnostics/sparse.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/sparse.cy.spec.tsnew file mode 100644index 00000000000..1f0f59fa37f--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/sparse.cy.spec.ts@@ -0,0 +1,102 @@+const { H } = cy;++import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";+import { ORDERS_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const { ORDERS_ID } = SAMPLE_DATABASE;++const SEARCH_TERM = "sparsity";+const SPARSE_COLLECTION_NAME = "E2E sparsity thin collection";+const SPARSE_DASHBOARD_NAME = "E2E sparsity thin dashboard";+const EMPTY_COLLECTION_NAME = "E2E sparsity bare collection";++const CHILD_QUESTION_NAMES = ["E2E child question A", "E2E child question B"];++const SPARSE = [+ { name: SPARSE_COLLECTION_NAME, count: "2 items" },+ { name: SPARSE_DASHBOARD_NAME, count: "1 dashcard" },+];++function createContent() {+ H.createCollection({ name: EMPTY_COLLECTION_NAME });++ H.createCollection({ name: SPARSE_COLLECTION_NAME }).then(+ ({ body: collection }) => {+ CHILD_QUESTION_NAMES.forEach((name) => {+ H.createQuestion({+ name,+ collection_id: collection.id,+ query: { "source-table": ORDERS_ID },+ });+ });+ },+ );++ H.createDashboard({ name: SPARSE_DASHBOARD_NAME }).then(+ ({ body: dashboard }) => {+ H.addQuestionToDashboard({+ dashboardId: dashboard.id,+ cardId: ORDERS_QUESTION_ID,+ });+ },+ );+}++describe("scenarios > monitor > content diagnostics > sparse", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports thinly filled content, leaving bare content to the empty tab", () => {+ createContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("sparse");+ searchFindings(SEARCH_TERM);++ cy.log("both types holding a little content are reported as sparse");+ cy.findByTestId("imbalanced-content-list").within(() => {+ SPARSE.forEach(({ name }) => {+ cy.findByText(name).should("be.visible");+ });+ });++ cy.log(+ "sparseness floors at one item, so the bare collection is not sparse",+ );+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ EMPTY_COLLECTION_NAME,+ );++ cy.log("the sidebar counts each type in the unit that suits it");+ SPARSE.forEach(({ name, count }) => {+ cy.findByTestId("imbalanced-content-list").findByText(name).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Content count").should("be.visible");+ cy.findByText(count).should("be.visible");+ });+ });++ cy.log("the bare collection is reported by the empty tab instead");+ visitContentDiagnosticsTab("empty");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("imbalanced-content-list")+ .findByText(EMPTY_COLLECTION_NAME)+ .should("be.visible");+ cy.findByTestId("imbalanced-content-list").should(+ "not.contain.text",+ SPARSE_COLLECTION_NAME,+ );+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/stale.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/stale.cy.spec.tsnew file mode 100644index 00000000000..516b009c061--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/stale.cy.spec.ts@@ -0,0 +1,90 @@+const { H } = cy;++import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";++import {+ markStale,+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const { ORDERS_ID } = SAMPLE_DATABASE;++const SEARCH_TERM = "staleness";+const QUESTION_NAME = "E2E staleness backdated question";+const DASHBOARD_NAME = "E2E staleness backdated dashboard";+const DOCUMENT_NAME = "E2E staleness backdated document";+const FRESH_NAME = "E2E staleness fresh question";++const LAST_USED_ON = "2020-06-15";+const LAST_USED_DISPLAY = "June 15, 2020";++const BACKDATED = [+ { name: QUESTION_NAME, activityLabel: "Last used" },+ { name: DASHBOARD_NAME, activityLabel: "Last viewed" },+ { name: DOCUMENT_NAME, activityLabel: "Last viewed" },+];++function createContent() {+ H.createQuestion({ name: FRESH_NAME, query: { "source-table": ORDERS_ID } });++ H.createQuestion({+ name: QUESTION_NAME,+ query: { "source-table": ORDERS_ID },+ }).then(({ body: card }) => markStale("card", card.id, LAST_USED_ON));++ H.createDashboard({ name: DASHBOARD_NAME }).then(({ body: dashboard }) =>+ markStale("dashboard", dashboard.id, LAST_USED_ON),+ );++ H.createDocument({+ name: DOCUMENT_NAME,+ document: { type: "doc", content: [] },+ }).then(({ body: document }) =>+ markStale("document", document.id, LAST_USED_ON),+ );+}++describe("scenarios > monitor > content diagnostics > stale", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ it("reports backdated content of every type, and leaves recently used content out", () => {+ createContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("stale");+ searchFindings(SEARCH_TERM);++ cy.log("each type is reported, carrying the date it was last active");+ cy.findByTestId("stale-content-list").within(() => {+ BACKDATED.forEach(({ name }) => {+ cy.findByText(name).should("be.visible");+ });+ cy.findAllByText(LAST_USED_DISPLAY).should(+ "have.length",+ BACKDATED.length,+ );+ });++ cy.log("the question that has not gone stale is left out");+ cy.findByTestId("stale-content-list").should(+ "not.contain.text",+ FRESH_NAME,+ );++ cy.log("the sidebar names each type's own kind of activity");+ BACKDATED.forEach(({ name, activityLabel }) => {+ cy.findByTestId("stale-content-list").findByText(name).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText(activityLabel).should("be.visible");+ cy.findByText(LAST_USED_DISPLAY).should("be.visible");+ });+ });+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/table-operations.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/table-operations.cy.spec.tsnew file mode 100644index 00000000000..01957c0d30a--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/table-operations.cy.spec.ts@@ -0,0 +1,168 @@+const { H } = cy;++import { ADMIN_PERSONAL_COLLECTION_ID } from "e2e/support/cypress_sample_instance_data";++import {+ runContentDiagnosticsScan,+ searchFindings,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "tableops";+const COLLECTION_NAME = "E2E tableops collection";+const PERSONAL_DASHBOARD_NAME = "E2E tableops personal dashboard";+const EXCLUDED_TERM = "zzdecoy";+const EXCLUDED_DASHBOARD_NAME = "E2E zzdecoy dashboard";++const DASHBOARD_COUNT = 26;+const dashboardName = (index: number) =>+ `E2E tableops dashboard ${String(index).padStart(2, "0")}`;++const FIRST_DASHBOARD_NAME = dashboardName(1);+const LAST_DASHBOARD_NAME = dashboardName(DASHBOARD_COUNT);++const TOTAL_FINDINGS = DASHBOARD_COUNT + 2;++const LIST = "imbalanced-content-list";++function seedEmptyContent() {+ H.createCollection({ name: COLLECTION_NAME });+ H.createDashboard({ name: EXCLUDED_DASHBOARD_NAME });+ H.createDashboard({+ name: PERSONAL_DASHBOARD_NAME,+ collection_id: ADMIN_PERSONAL_COLLECTION_ID,+ });++ Array.from({ length: DASHBOARD_COUNT }, (_, index) =>+ H.createDashboard({ name: dashboardName(index + 1) }),+ );+}++function searchFor(term: string) {+ H.main().findByLabelText("Search").clear().type(term);+ cy.wait("@findings");+}++function openFilterPicker() {+ cy.findByTestId("content-diagnostics-filter-button").click();+}++function closeFilterPicker() {+ cy.get("body").type("{esc}");+}++function sortByName() {+ cy.findByTestId(LIST).findByText("Name").click();+ cy.wait("@findings");+}++describe("scenarios > monitor > content diagnostics > table operations", () => {+ beforeEach(() => {+ H.restore();+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");++ seedEmptyContent();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("empty");+ });++ it("narrows the table by search, entity type and personal collections", () => {+ cy.log("searching the decoy's term finds the decoy and nothing else");+ searchFor(EXCLUDED_TERM);+ cy.findByTestId(LIST).should("contain.text", EXCLUDED_DASHBOARD_NAME);+ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);++ cy.log("searching the seeded term finds everything except the decoy");+ searchFor(SEARCH_TERM);+ cy.findByTestId("pagination-total").should("have.text", TOTAL_FINDINGS);+ cy.findByTestId(LIST).should("not.contain.text", EXCLUDED_DASHBOARD_NAME);++ cy.log("unchecking Dashboards leaves only the collection");+ openFilterPicker();+ H.popover().findByLabelText("Dashboards").click();+ cy.wait("@findings");+ closeFilterPicker();+ cy.findByTestId(LIST).findByText(COLLECTION_NAME).should("be.visible");+ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);++ // No request to wait on here: putting dashboards back restores the default filter, so the original+ // query is served from cache rather than fetched again.+ cy.log("re-checking Dashboards brings them back");+ openFilterPicker();+ H.popover().findByLabelText("Dashboards").click();+ closeFilterPicker();+ cy.findByTestId(LIST).should("contain.text", FIRST_DASHBOARD_NAME);++ cy.log("unchecking personal collections drops the personal dashboard");+ openFilterPicker();+ H.popover()+ .findByLabelText("Include items in personal collections")+ .click();+ cy.wait("@findings");+ closeFilterPicker();++ cy.findByTestId("pagination-total").should("have.text", TOTAL_FINDINGS - 1);+ });++ it("sorts the table by the column that was clicked", () => {+ searchFindings(SEARCH_TERM);+ sortByName();+ cy.findByTestId(LIST).findByText(FIRST_DASHBOARD_NAME).should("be.visible");+ cy.findByTestId(LIST).should("not.contain.text", PERSONAL_DASHBOARD_NAME);++ cy.log("clicking the header again reverses the order");+ sortByName();+ cy.findByTestId(LIST)+ .findByText(PERSONAL_DASHBOARD_NAME)+ .should("be.visible");+ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);+ });++ it("loads the next page of results", () => {+ searchFindings(SEARCH_TERM);+ sortByName();+ cy.findByTestId(LIST).findByText(FIRST_DASHBOARD_NAME).should("be.visible");+ cy.findByTestId(LIST).should("not.contain.text", LAST_DASHBOARD_NAME);++ cy.findByTestId("next-page-btn").click();+ cy.wait("@findings");++ cy.findByTestId(LIST).findByText(LAST_DASHBOARD_NAME).should("be.visible");+ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);+ });++ it("opens a sidebar of details for the finding that was clicked", () => {+ searchFindings(SEARCH_TERM);++ cy.findByTestId("pagination-total").should("have.text", TOTAL_FINDINGS);+ cy.findByTestId(LIST).findByText(COLLECTION_NAME).click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText(COLLECTION_NAME).should("be.visible");+ cy.findByText("Content count").should("be.visible");+ cy.findByText("0 items").should("be.visible");+ });+ });++ it("starts a neighbouring tab clean, with no search, filters or sidebar", () => {+ searchFindings(SEARCH_TERM);+ openFilterPicker();+ H.popover().findByLabelText("Dashboards").click();+ cy.wait("@findings");+ closeFilterPicker();++ cy.findByTestId(LIST).should("not.contain.text", FIRST_DASHBOARD_NAME);+ cy.findByTestId(LIST).findByText(COLLECTION_NAME).click();+ cy.findByTestId("content-diagnostics-sidebar").should("be.visible");++ cy.log("switching tabs clears the search, the filters and the sidebar");+ cy.findByRole("link", { name: "Sparse" }).click();+ cy.wait("@findings");++ cy.findByTestId("content-diagnostics-sidebar").should("not.exist");+ H.main().findByLabelText("Search").should("have.value", "");+ cy.findByTestId("content-diagnostics-filter-button").should("be.visible");+ });+});diff --git a/e2e/test/scenarios/monitor/content-diagnostics/transforms.cy.spec.ts b/e2e/test/scenarios/monitor/content-diagnostics/transforms.cy.spec.tsnew file mode 100644index 00000000000..0005b996a8f--- /dev/null+++ b/e2e/test/scenarios/monitor/content-diagnostics/transforms.cy.spec.ts@@ -0,0 +1,300 @@+const { H } = cy;++import { WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { dayjs } from "metabase/dayjs";++import {+ markStale,+ runContentDiagnosticsScan,+ searchFindings,+ setThreshold,+ visitContentDiagnosticsTab,+} from "./helpers/content-diagnostics-helpers";++const SEARCH_TERM = "transforming";++const ARCHIVED_FOLDER_NAME = "E2E transforming archived folder";+const ARCHIVED_TRANSFORM_NAME = "E2E transforming archived-folder transform";+const SLOW_TRANSFORM_NAME = "E2E transforming slow transform";+const STALE_TRANSFORM_NAME = "E2E transforming stale transform";+const SCHEDULED_TRANSFORM_NAME = "E2E transforming scheduled transform";+const UNSCHEDULED_TRANSFORM_NAME = "E2E transforming unscheduled transform";+const DUPLICATED_TRANSFORM_NAME = "E2E transforming duplicated transform";+const DELETED_TRANSFORM_NAME = "E2E transforming deleted transform";++const TARGET_SCHEMA = "public";++const SLEEP_SECONDS = 2;+const SLOW_THRESHOLD_SECONDS = 1;+const SECONDS_DURATION = /^\d+\.\ds$/;++const LAST_RUN_DAYS_AGO = 120;+const lastRunOn = () =>+ dayjs().subtract(LAST_RUN_DAYS_AGO, "day").format("YYYY-MM-DD");++const yearlyCronAwayFromNow = () =>+ `0 0 0 1 ${dayjs().add(6, "month").month() + 1} ?`;++function createTransformInArchivedFolder() {+ H.createTransformCollection({ name: ARCHIVED_FOLDER_NAME }).then(+ ({ body: collection }) => {+ H.createTransform({+ name: ARCHIVED_TRANSFORM_NAME,+ collection_id: collection.id,+ source: {+ type: "query",+ query: {+ database: WRITABLE_DB_ID,+ type: "native",+ native: { query: "SELECT 1 AS id" },+ },+ },+ target: {+ type: "table",+ database: WRITABLE_DB_ID,+ name: "e2e_transforming_archived",+ schema: TARGET_SCHEMA,+ },+ }).then(({ body: transform }) => markStale("transform", transform.id));++ H.archiveCollection(collection.id);+ },+ );+}++function findingRow(name: string) {+ return cy.findByTestId("stale-content-list").contains('[role="row"]', name);+}++function createTransformNamed(name: string, targetTable: string) {+ return H.createTransform({+ name,+ source: {+ type: "query",+ query: {+ database: WRITABLE_DB_ID,+ type: "native",+ native: { query: "SELECT 1 AS id" },+ },+ },+ target: {+ type: "table",+ database: WRITABLE_DB_ID,+ name: targetTable,+ schema: TARGET_SCHEMA,+ },+ });+}++describe(+ "scenarios > monitor > content diagnostics > transforms",+ { tags: "@external" },+ () => {+ beforeEach(() => {+ H.restore("postgres-writable");+ cy.signInAsAdmin();+ H.activateToken("bleeding-edge");+ });++ // Archiving a folder takes its cards, dashboards and documents out of scope, but a transform runs on+ // its schedule regardless of the folder it sits in, so it stays in scope.+ it("still reports a stale transform whose folder has been archived", () => {+ createTransformInArchivedFolder();+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("stale");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("stale-content-list")+ .findByText(ARCHIVED_TRANSFORM_NAME)+ .should("be.visible");++ cy.findByTestId("stale-content-list")+ .findByText(ARCHIVED_TRANSFORM_NAME)+ .click();+ cy.findByTestId("content-diagnostics-sidebar")+ .findByText("Last run")+ .should("be.visible");+ });++ it("reports a transform whose run exceeded the threshold", () => {+ setThreshold(+ "content-diagnostics-slow-transform-threshold-seconds",+ SLOW_THRESHOLD_SECONDS,+ );+ H.createAndRunSqlTransform({+ name: SLOW_TRANSFORM_NAME,+ sourceQuery: `SELECT pg_sleep(${SLEEP_SECONDS}) IS NULL AS napped`,+ targetTable: "e2e_transforming_slow",+ targetSchema: TARGET_SCHEMA,+ });+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("slow");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("slow-content-list")+ .findByText(SLOW_TRANSFORM_NAME)+ .should("be.visible");++ cy.findByTestId("slow-content-list")+ .findByText(SLOW_TRANSFORM_NAME)+ .click();+ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duration").should("be.visible");+ cy.findByText(SECONDS_DURATION).should("be.visible");+ });+ });++ it("reports a transform whose last run predates the threshold", () => {+ H.createAndRunSqlTransform({+ name: STALE_TRANSFORM_NAME,+ sourceQuery: "SELECT 1 AS id",+ targetTable: "e2e_transforming_stale",+ targetSchema: TARGET_SCHEMA,+ }).then(({ transformId }) =>+ markStale("transform", transformId, lastRunOn()),+ );+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("stale");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("stale-content-list")+ .findByText(STALE_TRANSFORM_NAME)+ .should("be.visible");+ });++ it("clusters transforms that share a name", () => {+ createTransformNamed(+ DUPLICATED_TRANSFORM_NAME,+ "e2e_transforming_duplicated_a",+ );+ createTransformNamed(+ DUPLICATED_TRANSFORM_NAME,+ "e2e_transforming_duplicated_b",+ );+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("duplicated");+ searchFindings(SEARCH_TERM);++ cy.findByTestId("duplicated-content-list")+ .findAllByText(DUPLICATED_TRANSFORM_NAME)+ .should("have.length", 2);++ cy.log("and each side names the other as its duplicate");+ cy.findByTestId("duplicated-content-list")+ .findAllByText(DUPLICATED_TRANSFORM_NAME)+ .first()+ .click();++ cy.findByTestId("content-diagnostics-sidebar").within(() => {+ cy.findByText("Duplicates (1)").should("be.visible");+ cy.findByRole("region", { name: "Duplicates" }).should(+ "contain.text",+ DUPLICATED_TRANSFORM_NAME,+ );+ });+ });++ // A transform has no archived state, so bulk removal deletes it outright, and the bar+ // says so instead of offering the trash.+ it("permanently deletes a selected transform instead of trashing it", () => {+ let transformId = 0;++ H.createAndRunSqlTransform({+ name: DELETED_TRANSFORM_NAME,+ sourceQuery: "SELECT 1 AS id",+ targetTable: "e2e_transforming_deleted",+ targetSchema: TARGET_SCHEMA,+ }).then((transform) => {+ transformId = transform.transformId;+ markStale("transform", transformId, lastRunOn());+ });+ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("stale");+ searchFindings(SEARCH_TERM);++ findingRow(DELETED_TRANSFORM_NAME).findByRole("checkbox").click();++ cy.findByTestId("toast-card").within(() => {+ cy.findByText("1 item selected").should("be.visible");+ cy.findByRole("button", { name: "Delete" }).click();+ });++ cy.log("the confirmation drops the trash wording for a permanent delete");+ H.modal().within(() => {+ cy.findByText("Delete 1 transform?").should("be.visible");+ cy.findByText(+ "1 transform will be permanently deleted and cannot be restored.",+ ).should("be.visible");+ cy.findByRole("button", { name: "Delete" }).click();+ });++ H.undoToast().findByText("Deleted 1 item").should("be.visible");++ cy.log("the finding is invalidated and the transform itself is gone");+ cy.findByTestId("stale-content-list").should(+ "not.contain.text",+ DELETED_TRANSFORM_NAME,+ );+ cy.then(() => {+ cy.request({+ url: `/api/transform/${transformId}`,+ failOnStatusCode: false,+ })+ .its("status")+ .should("eq", 404);+ });+ });++ it("leaves a transform between fires of its schedule out of stale", () => {+ H.createTransformTag({ name: "E2E transforming tag" }).then(+ ({ body: tag }) => {+ H.createTransformJob({+ name: "E2E transforming job",+ schedule: yearlyCronAwayFromNow(),+ tag_ids: [tag.id],+ });++ H.createAndRunSqlTransform({+ name: SCHEDULED_TRANSFORM_NAME,+ sourceQuery: "SELECT 1 AS id",+ targetTable: "e2e_transforming_scheduled",+ targetSchema: TARGET_SCHEMA,+ tagIds: [tag.id],+ }).then(({ transformId }) =>+ markStale("transform", transformId, lastRunOn()),+ );+ },+ );++ H.createAndRunSqlTransform({+ name: UNSCHEDULED_TRANSFORM_NAME,+ sourceQuery: "SELECT 1 AS id",+ targetTable: "e2e_transforming_unscheduled",+ targetSchema: TARGET_SCHEMA,+ }).then(({ transformId }) =>+ markStale("transform", transformId, lastRunOn()),+ );++ runContentDiagnosticsScan();++ visitContentDiagnosticsTab("stale");+ searchFindings(SEARCH_TERM);++ cy.log("the unscheduled one is overdue and reported");+ cy.findByTestId("stale-content-list")+ .findByText(UNSCHEDULED_TRANSFORM_NAME)+ .should("be.visible");++ cy.log("the scheduled one is merely between fires, so it is left out");+ cy.findByTestId("stale-content-list").should(+ "not.contain.text",+ SCHEDULED_TRANSFORM_NAME,+ );+ });+ },+);diff --git a/e2e/test/scenarios/permissions/application-permissions.cy.spec.js b/e2e/test/scenarios/permissions/application-permissions.cy.spec.jsindex a9005f4e8f7..dd46aba98a1 100644--- a/e2e/test/scenarios/permissions/application-permissions.cy.spec.js+++ b/e2e/test/scenarios/permissions/application-permissions.cy.spec.js@@ -127,7 +127,7 @@ describe("scenarios > admin > permissions > application", () => {H.popover().findByText("Monitor").click();cy.log("Monitor tools smoke test");- cy.location("pathname").should("contain", "/monitor/tasks");+ cy.location("pathname").should("eq", "/monitor/tasks/list");cy.findByRole("heading", {name: "Background tasks",});diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex 3aa3acf8d28..84e10babe35 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -13,6 +13,7 @@[metabase-enterprise.billing.api.routes][metabase-enterprise.cloud-add-ons.api][metabase-enterprise.cloud-proxy.api]+ [metabase-enterprise.content-diagnostics.api][metabase-enterprise.content-translation.routes][metabase-enterprise.content-verification.api.routes][metabase-enterprise.custom-viz-plugin.api]@@ -61,6 +62,7 @@:attached-dwh (deferred-tru "Attached DWH"):audit-app (deferred-tru "Audit app"):collection-cleanup (deferred-tru "Collection Cleanup")+ :content-diagnostics (deferred-tru "Content Diagnostics"):content-translation (deferred-tru "Content translation"):custom-viz (deferred-tru "Custom Visualizations"):data-apps (deferred-tru "Data Apps")@@ -115,6 +117,7 @@"/ai-controls" (premium-handler metabase-enterprise.metabot.api.routes/routes :ai-controls)"/audit-app" (premium-handler metabase-enterprise.audit-app.api.routes/routes :audit-app)"/billing" metabase-enterprise.billing.api.routes/routes+ "/content-diagnostics" (premium-handler metabase-enterprise.content-diagnostics.api/routes :content-diagnostics)"/content-translation" (premium-handler metabase-enterprise.content-translation.routes/routes :content-translation);; The sandbox donor GET can be accessed without auth (an iframe src cannot carry session auth), so it lives in;; its own namespace, tested before the rest of the session-authed custom-viz routes. Both stay behinddiff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/api.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api.cljnew file mode 100644index 00000000000..f6fc28b0d98--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api.clj@@ -0,0 +1,531 @@+(ns metabase-enterprise.content-diagnostics.api+ "Content Diagnostics API - paginated, batch-hydrated latest-per-entity finding lists, mounted behind+ `premium-handler … :content-diagnostics` (`+auth` + feature gate). The namespace is audience-gated to+ superuser/data-analyst/`:monitoring` ([[+check-diagnostics-access]]). Endpoints only: each composes the+ shared read/hydration layer in `api.common` and pins its own param + response schema. The scan runs on a+ Quartz job.++ Response shape: a flat identity (`id, finding_type, entity_type, entity_kind, card_type?, entity_id,+ detected_at, entity_display_name, collection_name`) plus a nested typed `details` merging the stored+ verdict with live-hydrated `collection`, `description`, `owner`, `creator`, and `view_count` (the+ entity's usage counter, present for card/dashboard/document; not collection or transform)."+ (:require+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.api.common :as api.common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase-enterprise.content-diagnostics.schema :as cd.schema]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.api.routes.common :as routes.common :refer [+auth]]+ [metabase.permissions.core :as perms]+ [metabase.request.core :as request]+ [metabase.util :as u]+ [metabase.util.malli.schema :as ms]))++(set! *warn-on-reflection* true)++;;; -------------------------------------------- response schema ----------------------------------------+;;; The HTTP response contract. Distinct from a checker's internal finding map (kebab keys, pre-hydration)+;;; - these describe the hydrated snake_case shape `api.common/hydrate-findings` produces.++(def ^:private NormalizedUser+ "A finding's `owner`: a Metabase user `{id,name,email,type:user}`, or - for an external transform owner -+ `{email,type:external}`, or nil. Keys optional to admit both variants."+ [:maybe [:map+ [:id {:optional true} [:maybe :int]]+ [:name {:optional true} [:maybe :string]]+ [:email {:optional true} [:maybe :string]]+ [:type :keyword]]])++(def ^:private Creator+ "A finding's `creator`: a Metabase user `{id,name,type:user}` (denormalized from+ `entity_creator_id`/`entity_creator_name`), or nil. No `email` (not denormalized); `type` is always `:user`."+ [:maybe [:map {:closed true}+ [:id :int]+ [:name [:maybe :string]]+ [:type [:= :user]]]])++(def ^:private FindingBase+ "The flat identity every finding response shares. Each finding `:merge`s its own top-level column+ (`last_active_at` / `duration_ms` / `duplicate_count` / `content_count`) and its typed `details` onto+ this."+ [:map+ [:id :int]+ [:finding_type :keyword]+ [:entity_type :keyword]+ ;; card findings only - report_card.type denormalized at scan time; the column `entity-types` filters+ ;; on when given a card sub-kind (question/model/metric);+ ;; nullable (rows can predate the column, and a card deleted mid-scan stamps nil)+ [:card_type {:optional true} [:maybe :keyword]]+ ;; Live visualization display, only for card findings.+ [:display {:optional true} [:maybe :keyword]]+ [:entity_kind :keyword]+ [:entity_id :int]+ [:detected_at ms/TemporalInstant]+ [:entity_display_name [:maybe :string]]+ ;; entity's created_at, denormalized at scan time (immutable ⇒ equals live)+ [:created_at [:maybe ms/TemporalInstant]]+ ;; whether the caller can trash the entity (curate its collection, or delete a transform)+ [:can_write :boolean]+ ;; scan-time parent-collection name (the collection sort key); root rows carry the site-locale root+ ;; label. nil when the caller cannot read the scan-time parent or the row predates the migration.+ ;; details.collection stays the live, permission-scoped breadcrumb for navigation.+ [:collection_name [:maybe :string]]])++(def ^:private BreadcrumbId+ "A breadcrumb collection id: a real collection id, or the literal \"root\" of the root sentinel."+ [:or :int [:= "root"]])++(def ^:private BreadcrumbName+ "A breadcrumb collection name. Admits a localized string because validation runs on the raw response,+ where a root sentinel's label is still a `tru` instance."+ [:or :string ms/LocalizedString])++(def ^:private CollectionBreadcrumb+ "A finding's `collection`: the entity's parent-collection breadcrumb, or nil (unreadable parent /+ entity deleted post-scan). `namespace` is the collection tree's namespace - nil for the default tree;+ `transforms` and `shared-tenant-collection` are the namespaced trees findings can live in."+ [:maybe [:map {:closed true}+ [:id BreadcrumbId]+ [:name BreadcrumbName]+ [:namespace [:maybe :keyword]]+ [:effective_ancestors [:sequential+ [:map {:closed true}+ [:id BreadcrumbId]+ [:name BreadcrumbName]]]]]])++(def ^:private FindingDetailsBase+ "The display fields every finding's `details` carries: the collection breadcrumb, live `description`, the+ hydrated `owner`/`creator`, and the entity's `view_count` (present for every type but transform). Each+ finding type `:merge`s its own detail extras onto this."+ [:map+ [:collection CollectionBreadcrumb]+ [:description [:maybe :string]]+ [:owner NormalizedUser]+ [:creator Creator]+ [:view_count {:optional true} :int]])++(def ^:private StaleDetails+ "`stale` details: the shared core plus the frozen `threshold_days` verdict."+ [:merge FindingDetailsBase+ [:map+ [:threshold_days {:optional true} :int]]])++(def ^:private StaleFinding+ "Response item for a `stale` finding: flat identity + a top-level `last_active_at` + nested typed `details`."+ [:merge FindingBase+ [:map+ ;; frozen scan-time activity anchor; nil ⇒ never used/ran (top-level, SQL-filterable by threshold-days)+ [:last_active_at [:maybe ms/TemporalInstant]]+ [:details StaleDetails]]])++(def ^:private SlowEntity+ "A hydrated culprit of a container roll-up: an embedded slow **card** of a dashboard/document finding.+ Always a card - a container embeds cards (a dashboard via its dashcards, a document via the cards embedded+ in its body) and is flagged slow when one of those cards' queries is slow; it never embeds a transform, and+ a slow transform is its own leaf finding, not a member of another entity.+ `{id, name, entity_type, card_type?, view_count}`."+ [:map+ [:id :int]+ [:name [:maybe :string]]+ [:entity_type :keyword]+ [:card_type {:optional true} [:maybe :keyword]]+ [:view_count :int]])++(def ^:private SlowDetails+ "`slow` details: the shared core plus a leaf's frozen `threshold_ms`, or a container's hydrated+ `slow_entities` culprit cards."+ [:merge FindingDetailsBase+ [:map+ [:threshold_ms {:optional true} :int]+ [:slow_entities {:optional true} [:sequential SlowEntity]]]])++(def ^:private SlowFinding+ "Response item for a `slow` finding: flat identity + a top-level `duration_ms` + nested typed `details`.+ One open map covering both variants: a **leaf** (card/transform) freezes `details.threshold_ms`; a+ **container** (dashboard/document) carries `details.slow_entities` (hydrated culprit cards). Every slow+ row stamps `duration_ms` (leaf median / container's slowest culprit), so it is never null in this result."+ [:merge FindingBase+ [:map+ ;; measured magnitude (top-level, SQL-filterable/sortable); always present on slow findings+ [:duration_ms :int]+ [:details SlowDetails]]])++(def ^:private DuplicatedEntity+ "A hydrated peer of a duplicated finding: another entity **of the same type** sharing the flagged+ entity's normalized name. `{id, name, entity_type, card_type?, display?, namespace?, view_count?}` -+ `card_type` (question/model/metric) and `display` only on card peers, `namespace` only on collection peers.+ Card/dashboard/document peers carry their live+ `view_count` for judging which duplicate is the abandoned one; transforms have no view concept, so+ transform peers carry no usage signal."+ [:map+ [:id :int]+ [:name [:maybe :string]]+ [:entity_type :keyword]+ [:card_type {:optional true} [:maybe :keyword]]+ [:display {:optional true} [:maybe :keyword]]+ [:namespace {:optional true} [:maybe :keyword]]+ [:view_count {:optional true} :int]])++(def ^:private DuplicatedDetails+ "`duplicated` details: the shared core plus the collided `normalized_name` and the hydrated same-type+ `duplicate_entities` peers."+ [:merge FindingDetailsBase+ [:map+ [:normalized_name :string]+ [:duplicate_entities [:sequential DuplicatedEntity]]]])++(def ^:private DuplicatedFinding+ "Response item for a duplicated finding: flat identity + a top-level `duplicate_count` + nested typed+ `details`. `duplicate_count` is the peer count (cluster size minus 1) and is never null on duplicated+ findings. `details.view_count` is the flagged entity's own live usage counter (present for every type+ but transform). `details.normalized_name` is the normalized name the cluster collided on;+ `details.duplicate_entities` are the hydrated peers the caller can see - permission and+ personal-collection filtering can leave it shorter than `duplicate_count` (down to empty, but the key+ is always present)."+ [:merge FindingBase+ [:map+ ;; peer count (top-level, SQL-filterable/sortable); always present on duplicated findings+ [:duplicate_count :int]+ [:details DuplicatedDetails]]])++(def ^:private ImbalancedDetails+ "`imbalanced` details. `as_of` lives in the JSON `details` blob, so it round-trips as a string, not a+ temporal instant."+ [:merge FindingDetailsBase+ [:map+ [:threshold :int]+ [:unit :string]+ [:as_of {:optional true} ms/TemporalString]]])++(def ^:private ImbalancedFinding+ "Response item for the `/imbalanced` endpoint. `empty`/`sparse`/`crowded` share one count-vs-threshold+ shape, discriminated by the top-level `finding_type`: `content_count` is the measured amount (0 for+ `empty`), `details.threshold` the bound it crossed, `details.unit` what was counted."+ [:merge FindingBase+ [:map+ ;; measured magnitude (top-level, SQL-filterable/sortable); always present on imbalanced findings+ [:content_count :int]+ [:details ImbalancedDetails]]])++(def ^:private stale-sort-column->field+ "Sortable stale-list params → their native `content_diagnostics_finding` column. The shared base plus+ the stale-specific `last-active-at` magnitude column."+ (assoc api.common/base-sort-column->field :last-active-at :last_active_at))++(def ^:private slow-sort-column->field+ "Sortable slow-list params → their native `content_diagnostics_finding` column. The shared base plus+ the slow-specific `duration-ms` magnitude column."+ (assoc api.common/base-sort-column->field :duration-ms :duration_ms))++(def ^:private imbalanced-sort-column->field+ "Sortable imbalanced-list params → their native `content_diagnostics_finding` column: the shared base+ plus `content-count` (always set on an imbalanced finding) and `finding-type`."+ (assoc api.common/base-sort-column->field+ :content-count :content_count+ :finding-type :finding_type))++(def ^:private imbalanced-finding-types+ "The finding types the `/imbalanced` endpoint spans."+ #{:empty :sparse :crowded})++(def ^:private imbalanced-entity-types+ "Entity types the `/imbalanced` filter accepts. The full vocabulary rather than+ `non-collection-entity-types`: `collection` sits outside the stale/slow set, `card` emits only+ `empty`, and `transform` emits nothing - the filter vocabulary is shared across+ `empty`/`sparse`/`crowded`, so it stays a superset of what they emit."+ cd.schema/entity-types)++(def ^:private duplicated-finding-types+ "The finding types the `/duplicated` endpoint spans."+ #{:duplicate_name})++(def ^:private duplicated-sort-column->field+ "Sortable duplicated-list params → their native `content_diagnostics_finding` column. The shared base+ plus the duplicated-specific `duplicate-count` magnitude column."+ (assoc api.common/base-sort-column->field :duplicate-count :duplicate_count))++(def ^:private duplicated-entity-types+ "Entity types the duplicated finding types can emit - all of them, since the checker walks every entity+ type in `common/entity-type->model`."+ cd.schema/entity-types)++(defn- entity-types-param+ "Param schema for `entity-types` - the flat vocabulary `api.common/filter-types` builds from the+ endpoint's `entity-types`."+ [entity-types]+ (let [enum (ms/enum-decode-keyword (api.common/filter-types entity-types))]+ [:or enum [:sequential enum]]))++(defn- stale-where-clause+ "The shared finding-list WHERE plus the stale-specific `threshold-days` filter - keeps findings whose+ `last_active_at` is on or before `today - threshold-days` (never-used always pass)."+ [{:keys [threshold-days] :as params}]+ (api.common/findings-where+ "stale" params+ ;; "less stale than threshold-days" = active more recently than the cutoff → excluded. Never-used+ ;; (`last_active_at` nil) is maximally stale, so it always passes. Mirrors the scan-time cutoff.+ (when threshold-days+ (let [cutoff (t/minus (t/local-date) (t/days threshold-days))]+ [:or [:= :last_active_at nil] [:<= :last_active_at cutoff]]))))++(defn- slow-where-clause+ "The shared finding-list WHERE plus the slow-specific `min-duration-ms` floor on the native `duration_ms`+ (containers filter naturally, since they stamp a representative duration)."+ [{:keys [min-duration-ms] :as params}]+ (api.common/findings-where+ "slow" params+ (when min-duration-ms [:>= :duration_ms min-duration-ms])))++(defn- imbalanced-where-clause+ "The shared finding-list WHERE over the `empty`/`sparse`/`crowded` finding types, narrowed to the+ `finding-types` param when it is given."+ [{:keys [finding-types] :as params}]+ (let [types (or (not-empty (u/one-or-many finding-types)) imbalanced-finding-types)]+ (api.common/findings-where (mapv name types) params)))++(defn- duplicated-where-clause+ "The shared finding-list WHERE plus the duplicated-specific `min-duplicate-count` floor on the native+ `duplicate_count` (the peer count - e.g. names shared by 3+ entities = `min-duplicate-count` 2)."+ [{:keys [min-duplicate-count] :as params}]+ (api.common/findings-where+ (mapv name duplicated-finding-types) params+ (when min-duplicate-count [:>= :duplicate_count min-duplicate-count])))++(defn- findings-response+ "The shared list-endpoint pipeline: select the sorted, paginated page for `where`, hydrate it+ (`exclude-personal?` gates the culprit hydration; the per-finding-type tail - hoisted columns and+ any details rewrite - is dispatched inside `api.common/hydrate-findings`), and wrap it in the+ `{:data :total :limit :offset :last_scan_at}` envelope every finding list returns."+ [where sort-column->field sort-column sort-direction exclude-personal?]+ (let [page (cd.db/findings-page where+ [[(sort-column->field sort-column) sort-direction]+ [:id sort-direction]]+ (request/limit)+ (request/offset))]+ {:data (api.common/hydrate-findings page exclude-personal?)+ :total (cd.db/finding-count where)+ :limit (request/limit)+ :offset (request/offset)+ :last_scan_at (api.common/last-scan-at)}))++;;; ------------------------------------------------ endpoints ------------------------------------------++(defn- check-diagnostics-access+ "403 unless the caller is a superuser, an entitled data analyst, or holds `:monitoring` - the same union+ as the FE `canAccessContentDiagnostics` guard. Without `:advanced-permissions` only superusers pass.+ This only gates who can call the endpoints; `api.common/visible-findings-clause` still decides which+ findings they get back."+ []+ (api/check-403 (or (api/entitled-data-analyst?)+ (perms/current-user-has-application-permissions? :monitoring))))++(def ^:private ^{:arglists '([handler])} +check-diagnostics-access+ "Applies [[check-diagnostics-access]] to every endpoint in the namespace, so a new one is gated+ automatically."+ (routes.common/wrap-middleware-for-open-api-spec-generation+ (fn [handler]+ (fn [request respond raise]+ (check-diagnostics-access)+ (handler request respond raise)))))++(api.macros/defendpoint :get "/stale"+ :- [:map+ [:data [:sequential StaleFinding]]+ [:total :int]+ [:limit [:maybe :int]]+ [:offset [:maybe :int]]+ [:last_scan_at [:maybe ms/TemporalInstant]]]+ "List **stale** findings - the latest valid `stale` finding per entity, permission-filtered+ for the current user. Each item is a flat identity + a nested `details` (collection, `description`, `owner`,+ `creator`, `threshold_days`). Card items also carry a top-level `card_type`+ (`question`|`model`|`metric`) - card findings only, denormalized at scan time. Paginated via+ `limit`/`offset`; `total` is the full valid count.++ Params: `include-personal-collections` (default false) - when false, entities currently in a personal+ collection are excluded. `entity-types` (repeatable, omitted = all) narrows by type; the card sub-kinds+ are valid values, and `card` means any card type.+ `threshold-days` (positive int) keeps findings with `last_active_at` on or before `today -+ threshold-days` (never-used always pass). `query` case-insensitively substring-matches the entity name.+ `sort-column` (`detected-at`|`entity-type`|`name`|`created-at`|`created-by`|`collection-name`|+ `last-active-at`, default `detected-at`) + `sort-direction` (`asc`|`desc`, default `asc`); `id` is+ the stable tiebreak."+ [_route-params+ {:keys [include-personal-collections sort-column sort-direction entity-types threshold-days query]+ :or {include-personal-collections false+ sort-column :detected-at+ sort-direction :asc}}+ :- [:map {:closed true}+ [:include-personal-collections {:optional true} :boolean]+ [:sort-column {:optional true} (ms/enum-decode-keyword (keys stale-sort-column->field))]+ [:sort-direction {:optional true} (ms/enum-decode-keyword api.common/sort-directions)]+ [:entity-types {:optional true} (entity-types-param cd.schema/non-collection-entity-types)]+ [:threshold-days {:optional true} ms/PositiveInt]+ [:query {:optional true} :string]]]+ (let [exclude-personal? (not include-personal-collections)]+ (findings-response (stale-where-clause {:exclude-personal? exclude-personal?+ :entity-types entity-types+ :threshold-days threshold-days+ :query query})+ stale-sort-column->field sort-column sort-direction+ exclude-personal?)))++(api.macros/defendpoint :get "/slow"+ :- [:map+ [:data [:sequential SlowFinding]]+ [:total :int]+ [:limit [:maybe :int]]+ [:offset [:maybe :int]]+ [:last_scan_at [:maybe ms/TemporalInstant]]]+ "List **slow** findings - the latest valid `slow` finding per entity, permission-filtered for the+ current user. Each item is a flat identity + a top-level `duration_ms` + a nested `details`. `details`+ varies by `entity_type`: leaves (card/transform) freeze `threshold_ms`; containers (dashboard/document)+ carry the hydrated `slow_entities` culprit cards. Card items also carry a top-level `card_type`+ (`question`|`model`|`metric`) - card findings only, denormalized at scan time. Paginated via+ `limit`/`offset`; `total` is the full valid count.++ Params: `include-personal-collections` (default false) - when false, entities currently in a personal+ collection are excluded and personal-collection culprit cards are omitted from `slow_entities`.+ `entity-types` (repeatable, omitted = all) narrows the flagged entity by type; the card sub-kinds are+ valid values, and `card` means any card type. Containers rolled up from slow cards are matched on+ their own type.+ `min-duration-ms` (positive int) keeps findings whose `duration_ms` is at least that (containers+ filter on their representative duration). `query` case-insensitively substring-matches the entity name.+ `sort-column` (`detected-at`|`entity-type`|`name`|`created-at`|`created-by`|`collection-name`|+ `duration-ms`, default `detected-at`) + `sort-direction` (`asc`|`desc`, default `asc`); `id` is the+ stable tiebreak."+ [_route-params+ {:keys [include-personal-collections sort-column sort-direction entity-types min-duration-ms query]+ :or {include-personal-collections false+ sort-column :detected-at+ sort-direction :asc}}+ :- [:map {:closed true}+ [:include-personal-collections {:optional true} :boolean]+ [:sort-column {:optional true} (ms/enum-decode-keyword (keys slow-sort-column->field))]+ [:sort-direction {:optional true} (ms/enum-decode-keyword api.common/sort-directions)]+ [:entity-types {:optional true} (entity-types-param cd.schema/non-collection-entity-types)]+ [:min-duration-ms {:optional true} ms/PositiveInt]+ [:query {:optional true} :string]]]+ (let [exclude-personal? (not include-personal-collections)]+ (findings-response (slow-where-clause {:exclude-personal? exclude-personal?+ :entity-types entity-types+ :min-duration-ms min-duration-ms+ :query query})+ slow-sort-column->field sort-column sort-direction+ exclude-personal?)))++(api.macros/defendpoint :get "/imbalanced"+ :- [:map+ [:data [:sequential ImbalancedFinding]]+ [:total :int]+ [:limit [:maybe :int]]+ [:offset [:maybe :int]]+ [:last_scan_at [:maybe ms/TemporalInstant]]]+ "List **imbalanced** findings - the latest valid finding per (entity, finding-type) across the+ `empty`/`sparse`/`crowded` checkers, permission-filtered for the current user. The checkers run+ independently, so one entity can appear once per finding type (a collection whose items are all empty+ is both `crowded` and `empty`) - rows are findings, not entities, and `total` counts findings. Each+ item is a flat identity plus a top-level `content_count` and a nested `details` (collection breadcrumb,+ description, owner, creator, the `threshold` crossed, and its `unit`). Card items (card only ever emits+ `empty`) also carry a top-level `card_type`, denormalized at scan time. Paginated via `limit`/`offset`.++ Params: `include-personal-collections` (default false) excludes entities in personal collections.+ `entity-types` and `finding-types` (both repeatable) narrow the results; the card sub-kinds are valid+ `entity-types` values, and `card` means any card type. `query` substring-matches the entity name.+ `sort-column` (default `detected-at`) + `sort-direction` (default `asc`); `id` breaks ties."+ [_route-params+ {:keys [include-personal-collections sort-column sort-direction entity-types finding-types query]+ :or {include-personal-collections false+ sort-column :detected-at+ sort-direction :asc}}+ :- [:map {:closed true}+ [:include-personal-collections {:optional true} :boolean]+ [:sort-column {:optional true} (ms/enum-decode-keyword (keys imbalanced-sort-column->field))]+ [:sort-direction {:optional true} (ms/enum-decode-keyword api.common/sort-directions)]+ [:entity-types {:optional true} (entity-types-param imbalanced-entity-types)]+ [:finding-types {:optional true} [:or+ (ms/enum-decode-keyword imbalanced-finding-types)+ [:sequential (ms/enum-decode-keyword imbalanced-finding-types)]]]+ [:query {:optional true} :string]]]+ (let [exclude-personal? (not include-personal-collections)]+ (findings-response (imbalanced-where-clause {:exclude-personal? exclude-personal?+ :entity-types entity-types+ :finding-types finding-types+ :query query})+ imbalanced-sort-column->field sort-column sort-direction+ exclude-personal?)))+(api.macros/defendpoint :get "/duplicated"+ :- [:map+ [:data [:sequential DuplicatedFinding]]+ [:total :int]+ [:limit [:maybe :int]]+ [:offset [:maybe :int]]+ [:last_scan_at [:maybe ms/TemporalInstant]]]+ "List **duplicated** findings - the latest valid finding per (entity, finding-type) across the+ duplicated finding types (currently just `duplicate_name`), permission-filtered for the current user.+ Each item is a flat identity + a top-level `duplicate_count` (the number of other same-type entities+ sharing the normalized name) + a nested `details` (collection, `description`, `owner`, `creator`,+ `normalized_name`, and the hydrated same-type `duplicate_entities` peers). Card items also carry a+ top-level `card_type` (`question`|`model`|`metric`) - card findings only, denormalized at scan time.+ Paginated via `limit`/`offset`; `total` is the full valid count.++ Params: `include-personal-collections` (default false) - when false, entities currently in a personal+ collection are excluded and personal-collection peers are omitted from `duplicate_entities`.+ `entity-types` (repeatable, omitted = all) narrows the flagged entity by type; the card sub-kinds are+ valid values, `card` means any card type, and `collection` finds same-named collections instance-wide,+ regardless of parent. Peers are not filtered - cards cluster across sub-kinds, so a model's peers can+ be questions.+ `min-duplicate-count` (positive int) keeps findings with at least that many peers. `query`+ case-insensitively substring-matches the entity name. `sort-column`+ (`detected-at`|`entity-type`|`name`|`created-at`|`created-by`|`collection-name`|`duplicate-count`,+ default `detected-at`) + `sort-direction` (`asc`|`desc`, default `asc`); `id` is the stable+ tiebreak."+ [_route-params+ {:keys [include-personal-collections sort-column sort-direction entity-types+ min-duplicate-count query]+ :or {include-personal-collections false+ sort-column :detected-at+ sort-direction :asc}}+ :- [:map {:closed true}+ [:include-personal-collections {:optional true} :boolean]+ [:sort-column {:optional true} (ms/enum-decode-keyword (keys duplicated-sort-column->field))]+ [:sort-direction {:optional true} (ms/enum-decode-keyword api.common/sort-directions)]+ [:entity-types {:optional true} (entity-types-param duplicated-entity-types)]+ [:min-duplicate-count {:optional true} ms/PositiveInt]+ [:query {:optional true} :string]]]+ (let [exclude-personal? (not include-personal-collections)]+ (findings-response (duplicated-where-clause {:exclude-personal? exclude-personal?+ :entity-types entity-types+ :min-duplicate-count min-duplicate-count+ :query query})+ duplicated-sort-column->field sort-column sort-direction+ exclude-personal?)))++(api.macros/defendpoint :post "/invalidate"+ :- [:map {:closed true}+ [:invalidated [:sequential :int]]+ [:skipped [:sequential :int]]]+ "Dismiss the findings with `ids`. Invalidation is soft and global: the row is kept, but the finding leaves+ every caller's lists. If the write fails, nothing is dismissed.++ `invalidated` lists the ids this call found active and dismissed (a racing call may list the same id);+ `skipped` lists every other requested id - nonexistent, already invalidated, superseded by a newer+ finding for the same entity, or not visible to the caller - without saying which. Both keep the request+ order, de-duplicated."+ [_route-params+ _query-params+ {:keys [ids]} :- [:map {:closed true}+ [:ids [:sequential {:min 1, :max 500} ms/PositiveInt]]]]+ (let [unique-ids (distinct ids)+ invalidated (finding/invalidate-by-ids! unique-ids [:and+ (api.common/latest-findings-clause)+ (api.common/visible-findings-clause)])]+ {:invalidated (filterv invalidated unique-ids)+ :skipped (filterv (complement invalidated) unique-ids)}))++(def ^{:arglists '([request respond raise])} routes+ "Ring routes for the Content Diagnostics API."+ ;; Middleware is applied left-to-right, so the last one ends up outermost: `+auth` runs first and an+ ;; unauthenticated request still gets a 401 rather than the audience gate's 403.+ (api.macros/ns-handler *ns* +check-diagnostics-access +auth))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.cljnew file mode 100644index 00000000000..3b0ab744636--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/api/common.clj@@ -0,0 +1,574 @@+(ns metabase-enterprise.content-diagnostics.api.common+ "Shared read-path helpers the thin `api` endpoints compose: the read-time WHERE fragments+ (validity, per-caller collection visibility, personal-collection exclusion, name search), the batched+ display hydration, and the schema/sort fragments every per-finding-type endpoint composes.++ All per-caller concerns resolve **live at read time** against each finding's *current* collection (never+ the scan-time `scope_collection_id`). Display attrs (name/created_at/creator/card_type/entity_kind/+ collection_name) are denormalized at scan time; description, the collection breadcrumb, the transform+ owner, and slow roll-up culprits hydrate live."+ (:require+ [clojure.string :as str]+ [medley.core :as m]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.api.common :as api]+ [metabase.collections.models.collection :as collection]+ [metabase.models.interface :as mi]+ [metabase.premium-features.core :as premium-features]+ [metabase.queries.schema :as queries.schema]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn filter-types+ "The `entity-types` filter vocabulary for an endpoint whose findings span `entity-types`: one flat enum+ of the entity types plus the card sub-kinds. `card` stays valid and means any card type."+ [entity-types]+ (into entity-types queries.schema/card-types))++(defn latest-findings-clause+ "Keep only the latest finding per entity and finding type, invalidated or not. A scan inserts before it+ supersedes, so an older row can stay active behind a newer one."+ []+ ;; latest finding per entity = MAX(id) per (entity_type, entity_id, finding_type). id is the recency+ ;; key (monotonic; scan_id is a random UUID). Latest-per-entity, not newest-scan-only, so an entity a+ ;; partial scan hasn't re-written yet still shows its last finding.+ [:in :id ^:allow-subquery {:select [[[:max :id] :id]]+ :from [(t2/table-name :model/ContentDiagnosticsFinding)]+ :group-by [:entity_type :entity_id :finding_type]}])++(defn valid-clause+ "Result set for one **or many** `finding-types` (an umbrella endpoint spans several): the latest+ finding per entity, excluding entities whose latest row is invalidated (an older valid row does not+ resurface)."+ [finding-types]+ [:and+ [:= :invalidated_at nil]+ [:in :finding_type (u/one-or-many finding-types)]+ (latest-findings-clause)])++;;; ------------------------------ per-caller read-time filters (shared) --------------------------------+;;; Resolved live at read time against each entity's *current* collection (not scan-time+;;; `scope_collection_id`): visibility (always) + personal-collection exclusion (param-gated).++(def ^:private archived-inclusive-visibility+ "Visibility config that includes archived collections; permission filtering is unchanged. Used by the+ transform-serving clauses - see [[visible-findings-clause]] for why."+ {:include-archived-items :all})++(defn- and-not-archived-clause+ "Narrow a collection-visibility `clause` to non-archived rows. Only for the `::common/collection-item`+ types (card/dashboard/document): a collection subject's own archived state is already handled by the+ visibility config, and a transform has no `archived` column at all."+ [clause]+ [:and clause [:= :archived false]])++(defn visible-findings-clause+ "Keep only findings whose entity is in a collection the current user can read (a collection subject must+ itself be readable). Fail-closed: an entity-type with no collection model is dropped.++ Transform findings need more, because transform readability is not collection-based: they reach only an+ entitled data analyst, and only while the transforms feature is enabled. The rest of `mi/can-read?` -+ whether the caller may read the transform's source tables - is checked when hydrating a peer or culprit+ ([[read-entity-rows]] `:transform`) but not here, so a finding can name a transform whose sources the+ caller cannot read. Transforms also survive an archived folder, since archiving a folder neither archives+ nor stops them.++ Archived cards, dashboards and documents are excluded here rather than left to the archive events: some+ archive paths publish no per-entity event - archiving a dashboard archives its dashboard questions in one+ bulk update - so their findings would otherwise linger until the next scan."+ []+ (into [:or]+ (common/entity-collection-clauses+ (cond->> (keys common/entity-type->model)+ (not (and (api/entitled-data-analyst?) (premium-features/any-transforms-enabled?)))+ (remove #{:transform}))+ (fn [etype coll-col]+ (cond-> (collection/visible-collection-filter-clause+ coll-col+ (if (= etype :transform) archived-inclusive-visibility {}))+ (isa? common/hierarchy etype ::common/collection-item)+ (and-not-archived-clause))))))++(def ^:private personal-root-prefixes+ "Subquery: the `location` prefix each personal-collection root imposes on its descendants - `/<id>/`.+ Personal collections only ever live at the root, so a collection is nested under one exactly when the+ first segment of its `location` names a personal root."+ ^:allow-subquery {:select [[[:concat "/" :id "/"] :prefix]]+ :from [(t2/table-name :model/Collection)]+ :where [:not= :personal_owner_id nil]})++(def ^:private first-location-segment+ "`/<first segment>/` of a collection's `location`: `/3/` for `/3/14/`, and `/` for a root-level collection,+ which no personal prefix can match (a personal root is caught by its own `personal_owner_id` instead).++ Standard SQL, because H2, Postgres and MySQL agree on `SUBSTRING`, `POSITION` and `CONCAT` but not on+ `SPLIT_PART`, `LOCATE`, `STRPOS` or `CAST(… AS INTEGER)`. `POSITION` needs `:raw` since Honey SQL emits the+ comma form, which Postgres rejects; only that constant is spliced and Honey SQL still quotes the column.+ Comparing strings rather than ints avoids a dialect-specific cast."+ [:substring :location [:inline 1]+ [:+ ^:allow-raw-sql [:raw ["POSITION('/' IN " [:substring :location [:inline 2]] ")"]]+ [:inline 1]]])++(defn- personal-collection-clause+ "WHERE fragment: the collection `coll-col` points at is, or is nested under, a personal collection.++ A non-correlated subquery, so its only bind parameters are the two `/` literals in+ [[personal-root-prefixes]] however many personal collections exist, and the planner evaluates it once.+ Matching the first `location` segment by equality keeps it a hash semi-join; a correlated+ `location LIKE '/' || pc.id || '/%'` could not use an index, because a pattern that is not constant at+ plan time yields no range bounds."+ [coll-col]+ [:in coll-col ^:allow-subquery+ {:select [:id]+ :from [(t2/table-name :model/Collection)]+ :where [:or+ [:not= :personal_owner_id nil]+ [:in first-location-segment personal-root-prefixes]]}])++(defn name-search-clause+ "Case-insensitive substring match on the denormalized `entity_name`. Nil for a blank/absent query.+ `%`/`_` are not escaped - they act as LIKE wildcards, matching the app's fuzzy name search."+ [query]+ (when-let [q (some-> query str/trim not-empty u/lower-case-en)]+ [:like [:lower :entity_name] (str "%" q "%")]))++(defn entity-types-clause+ "WHERE fragment for the flat `entity-types` vocabulary (see [[filter-types]]); nil when nothing was+ requested. `card` means any card type, so it expands to itself (the deleted-entity fallback) plus+ the card sub-kinds. Kept a positive IN so `idx_cd_finding_ftype_entity_kind` can serve it."+ [entity-types]+ (when-let [types (not-empty (set (u/one-or-many entity-types)))]+ (let [kinds (into #{}+ (mapcat #(if (= % :card) (cons :card queries.schema/card-types) [%]))+ types)]+ [:in :entity_kind (mapv name kinds)])))++(defn exclude-personal-collections-clause+ "WHERE fragment dropping findings whose entity currently lives in a personal collection - or, for a+ collection subject, *is* one (see [[personal-collection-clause]], which covers descendants). Root and+ regular-collection entities are kept. Nil when `exclude-personal?` is false."+ [exclude-personal?]+ (when exclude-personal?+ (into [:and]+ (map (fn [clause] [:not clause]))+ (common/entity-collection-clauses+ (keys common/entity-type->model)+ (fn [_etype coll-col] (personal-collection-clause coll-col))))))++(defn findings-where+ "Base WHERE for one endpoint's finding list (one finding-type, or an umbrella's several): the valid ++ caller-visible base narrowed by the filters every endpoint shares - personal-collection exclusion+ (when `:exclude-personal?`; see [[exclude-personal-collections-clause]]),+ `entity-types` (see [[entity-types-clause]]), and `query` name search - plus any finding-type-specific+ `extra-filters`. Each filter is precomputed so a nil (no-op) is skipped, not conjoined as a null+ AND-term."+ [finding-types {:keys [exclude-personal? entity-types query]} & extra-filters]+ (let [personal-filter (exclude-personal-collections-clause exclude-personal?)+ entity-type-filter (entity-types-clause entity-types)+ name-search-filter (name-search-clause query)]+ (into (cond-> [:and (valid-clause finding-types) (visible-findings-clause)]+ personal-filter (conj personal-filter)+ entity-type-filter (conj entity-type-filter)+ name-search-filter (conj name-search-filter))+ (filter some?)+ extra-filters)))++;;; ----------------------------------- display hydration (shared layer) --------------------------------+;;; name/created_at/creator are denormalized (frozen at scan time); description, the collection+;;; breadcrumb, the transform owner, and slow roll-up culprits are live-hydrated, batched per entity-type.++(defmulti ^:private hydrate-owner+ "Batch-hydrate the per-type `owner` onto already-selected rows. card/dashboard/document have no owner, so+ `::collection-item` returns rows unchanged; transform hydrates its `:owner` (a user row, or an `{:email …}`+ external stand-in). Collection sets its owner in [[collection-context]], not here."+ {:arglists '([entity-type rows])}+ (fn [entity-type _rows] entity-type)+ :hierarchy #'common/hierarchy)++(defmethod hydrate-owner ::common/collection-item [_ rows] rows)+(defmethod hydrate-owner :transform [_ rows] (t2/hydrate rows :owner))++(defn- context-rows+ "Build `{entity-id → row}` for a column-resident type: select `common/context-cols` plus `id`/`collection_id`,+ hydrate the owner (`hydrate-owner`), index by id. Empty `ids` → nil (skips a degenerate `IN ()`; callers use+ `get-in`, so nil is fine)."+ [entity-type ids]+ (when (seq ids)+ (->> (cd.db/entity-context-rows (common/entity-type->model entity-type)+ (common/context-cols entity-type)+ (set ids))+ (hydrate-owner entity-type)+ (m/index-by :id))))++(defmulti ^:private entity-context+ "For one entity-type's id set → `{entity-id → row}` of the live display fields (description, collection_id,+ view_count, transform owner). card/dashboard/document (`::collection-item`) and transform share the+ column-based [[context-rows]]; collection is not column-resident, so it derives its breadcrumb anchor from+ `location` via [[collection-context]]. An unregistered type throws - fail-closed, no `:default`."+ {:arglists '([entity-type ids])}+ (fn [entity-type _ids] entity-type)+ :hierarchy #'common/hierarchy)++(defmethod entity-context ::common/collection-item [entity-type ids] (context-rows entity-type ids))+(defmethod entity-context :transform [entity-type ids] (context-rows entity-type ids))++(defn- collection-context+ "The `entity-context` arm for `:collection` subjects, which have no `collection_id`/`creator_id`+ columns: `collection_id` (the breadcrumb anchor) is the **parent** parsed from `location` - consistent+ \"where it lives\" semantics; the subject itself is already the finding's identity - and `owner` is the+ owning user when the collection is personal (api-design: collection carries `owner` only when personal,+ `creator` always null). Nil at root / for regular collections respectively. `namespace` rides along so+ a root-resident subject's breadcrumb can name its own tree's root. Empty `ids` → nil (skips a+ degenerate `IN ()`; callers use `get-in`, so nil is fine)."+ [ids]+ (when (seq ids)+ (let [rows (cd.db/collection-context-rows (set ids))+ owners (when-let [owner-ids (not-empty (into #{} (keep :personal_owner_id) rows))]+ (cd.db/user-contacts-by-id owner-ids))]+ (m/index-by :id+ (for [{:keys [location personal_owner_id] :as row} rows]+ (assoc row+ :collection_id (collection/location-path->parent-id location)+ ;; same {:id :common_name :email} shape the transform :owner hydrate returns,+ ;; so normalized-owner serves both+ :owner (get owners personal_owner_id)))))))++(defmethod entity-context :collection [_ ids] (collection-context ids))++(defn- collection-breadcrumbs+ "For a set of collection ids → `{collection-id → {:id :name :namespace :effective_ancestors [{:id :name} …]}}`.+ Hydrates the permission-filtered `:effective_ancestors` breadcrumb. Selects the full row (the hydrate+ needs `:location`). No entry for root/nil collections. `:namespace` (the tree's namespace: nil for the+ default tree, `transforms` / `shared-tenant-collection` for the namespaced trees findings can live in)+ rides once at the top level - a subtree is namespace-uniform, so it covers the ancestors too - letting+ the FE build the namespace-specific collection URL.++ A breadcrumb can be a collection the primary gate never checked (a `:collection` subject's breadcrumb+ is its **parent**), so caller visibility is re-applied here - an unreadable breadcrumb collection gets+ no entry and the finding's `collection` degrades to null, same as root. Archived collections get an+ entry, so a transform finding can name the archived folder it lives in. Other types' findings are+ dropped by the primary gate while their folder is archived, and any archived parent that still shows+ up here names a folder the caller may read."+ [coll-ids]+ (when (seq coll-ids)+ (let [colls (t2/hydrate (cd.db/collections [:and+ [:in :id (set coll-ids)]+ (collection/visible-collection-filter-clause+ :id archived-inclusive-visibility)])+ :effective_ancestors)]+ (into {}+ (map (fn [c]+ [(:id c)+ {:id (:id c)+ :name (:name c)+ :namespace (:namespace c)+ :effective_ancestors (mapv #(select-keys % [:id :name]) (:effective_ancestors c))}]))+ colls))))++(defn- root-breadcrumb+ "The root-collection sentinel used as a root-resident entity's `collection` breadcrumb, normalized to the+ `{:id :name :namespace :effective_ancestors}` shape the nested-collection breadcrumbs use. `:id` is the+ literal \"root\" (the app-wide root id - the FE detects root by id, never by the localized name); `:name`+ is `collection-namespace`'s root label (e.g. \"Transforms\" for the transforms namespace); `:namespace`+ echoes `collection-namespace`, disambiguating which tree's root this is (nil for the default tree - the+ sentinel shares `:id` \"root\" across namespaces). Root has no ancestors. Mirrors how the rest of the app+ links root as a breadcrumb (`collection/hydrate-root-collection` / the root head of+ `:effective_ancestors`)."+ [collection-namespace]+ (let [root (collection/root-collection-with-ui-details collection-namespace)]+ {:id (:id root) :name (:name root) :namespace (:namespace root) :effective_ancestors []}))++(defn- entity-breadcrumb+ "One finding's `collection` breadcrumb from its hydrated `entity` context row: the parent-collection+ object when the entity lives in a readable collection; the namespaced root sentinel when it is+ root-resident (nil parent - every covered subject is placeable, so a nil parent means \"at the root of its+ tree\", never \"no collection concept\"); nil when the parent is unreadable (degrades leak-safe, visually+ like root) or the entity was deleted post-scan (absent from `entity`)."+ [entity-type entity breadcrumbs]+ (when entity+ (if-let [parent-id (:collection_id entity)]+ (get breadcrumbs parent-id)+ (root-breadcrumb (common/entity-root-namespace entity-type (:namespace entity))))))++(defn- readable-entities-where+ "HoneySQL WHERE keeping only the rows in `ids` the caller may read at hydration time: caller visibility+ (the same gate as `visible-findings-clause`) always, the archived exclusion for the types that have the+ column, plus the personal-collection exclusion when `exclude-personal?`. Shared by the culprit/peer+ hydrators so the read-time gate lives in one place - a perms change lands once, not per hydrator.++ `opts` carries the two type-dependent parts: `:visibility-config` for the collection clause and+ `:exclude-archived?` for the archived filter. Only the transform hydrator turns them off - a transform+ outlives its folder's archiving and has no `archived` column - so for everything else a trashed peer or+ culprit drops out of `duplicate_entities`/`slow_entities` at once instead of waiting for the next scan."+ ([ids exclude-personal?]+ (readable-entities-where ids exclude-personal? nil))+ ([ids exclude-personal? {:keys [visibility-config exclude-archived?]+ :or {visibility-config {} exclude-archived? true}}]+ [:and+ [:in :id ids]+ (collection/visible-collection-filter-clause :collection_id visibility-config)+ (when exclude-archived? [:= :archived false])+ ;; root-collection entities (nil collection_id) must survive the personal-collection exclusion.+ (when exclude-personal?+ [:or+ [:= :collection_id nil]+ [:not (personal-collection-clause :collection_id)]])]))++(defn- hydrate-slow-entities+ "Card-id set → `{card-id → {:id :name :entity_type :card :card_type <kw> :view_count <int>}}`. The+ read-time hydration of a `slow` roll-up's stored culprit ids (`slow_entity_ids`) into objects.+ `card_type` is the `report_card.type` enum (question/model/metric) that drives the FE per-member+ link; `view_count` is the card's live usage counter. Batched.++ Culprit cards can live outside their container's collection, so the per-caller read-time filters are+ re-applied here via [[readable-entities-where]]: caller visibility always, and the personal-collection+ exclusion when `exclude-personal?`. A filtered-out culprit drops out of `slow_entities`+ exactly like a deleted one."+ [card-ids exclude-personal?]+ (when (seq card-ids)+ (cd.db/card-summaries-by-id (readable-entities-where (set card-ids) exclude-personal?))))++(defmulti ^:private read-entity-rows+ "Permission-filtered rows for hydrating a type's duplicate ids, read-gated by [[readable-entities-where]].+ For card/dashboard/document (`::collection-item`) that collection clause IS the read permission (they derive+ `:perms/use-parent-collection-perms`), with projection cols from `common/peer-select-cols`; transform+ readability isn't collection-based, so it selects full rows and additionally filters by `mi/can-read?`."+ {:arglists '([entity-type ids exclude-personal?])}+ (fn [entity-type _ids _excluded] entity-type)+ :hierarchy #'common/hierarchy)++(defmethod read-entity-rows ::common/collection-item+ [entity-type ids exclude-personal?]+ (cd.db/name-rows (common/entity-type->model entity-type)+ (common/peer-select-cols entity-type)+ (readable-entities-where ids exclude-personal?)))++(defmethod read-entity-rows :collection+ [_ ids exclude-personal?]+ ;; a `:collection` subject *is* the read-permission unit, so it gates on its own `:id` rather than a+ ;; parent `:collection_id`, and has no root row to preserve. The visibility clause already drops+ ;; archived collections, so no separate archived filter here.+ (cd.db/name-rows :model/Collection [:namespace]+ [:and+ [:in :id ids]+ (collection/visible-collection-filter-clause :id)+ (when exclude-personal? [:not (personal-collection-clause :id)])]))++(defmethod read-entity-rows :transform+ [_ ids exclude-personal?]+ ;; mi/can-read? on a transform = source-type feature gate + (superuser, or data-analyst with readable+ ;; source tables) - the collection clause alone would leak transform names to collection-granted+ ;; non-analysts. It reads :source, so select full rows; peer sets are page-bounded, so the per-row check+ ;; is cheap.+ (filter mi/can-read? (cd.db/transforms+ (readable-entities-where ids exclude-personal?+ {:visibility-config archived-inclusive-visibility+ :exclude-archived? false}))))++(defn- hydrate-duplicate-entities+ "The findings' stored `duplicate_entity_ids` → `{[entity-type id] → {:id :name :entity_type <etype>+ :card_type <kw> :display <kw> :view_count <int> :namespace <kw>}}`. `card_type` and `display` are present+ only on card peers; `view_count` is present on card/dashboard/document peers; `namespace` is present+ only on collection peers. `display` is the card's live visualization type (e.g. table/bar/line), used+ to choose the question icon in the duplicates sidebar. Peers share the finding's own entity type,+ so each type's ids resolve from that type's own model via [[read-entity-rows]]+ (which applies the per-type read gate); a filtered-out peer drops out of `duplicate_entities` like a+ deleted one."+ [findings exclude-personal?]+ (into {}+ (for [[etype rows] (group-by :entity_type findings)+ :let [model (common/entity-type->model etype)+ ids (into #{} (mapcat (comp :duplicate_entity_ids :details)) rows)]+ :when (and model (seq ids))+ row (read-entity-rows etype ids exclude-personal?)]+ [[etype (:id row)]+ (cond-> {:id (:id row) :name (:name row) :entity_type etype}+ ;; served only where the peer select fetched it (transform + collection have none)+ (some #{:view_count} (common/peer-select-cols etype)) (assoc :view_count (:view_count row))+ (= etype :card) (assoc :card_type (:type row)+ :display (:display row))+ (= etype :collection) (assoc :namespace (:namespace row)))])))++(defn- normalized-owner+ "Normalized `owner` from the transform `:owner` hydrate or a personal collection's owning user:+ `{id,name,email,type:user}` or, for an external email-only transform owner, `{email,type:external}`.+ Nil for entity types with no owner (card/dashboard/document, non-personal collections)."+ [{:keys [owner]}]+ (when owner+ (let [{:keys [id common_name email]} owner]+ (if id+ {:id id :name common_name :email email :type :user}+ {:email email :type :external}))))++(defn- rewrite-ids->entities+ "Replace `details.<ids-key>` with hydrated `details.<entities-key>`, each id looked up via `id->entity`+ (misses dropped). A no-op when `ids-key` is absent."+ [details ids-key entities-key id->entity]+ (if (contains? details ids-key)+ (-> details+ (dissoc ids-key)+ (assoc entities-key (into [] (keep id->entity) (ids-key details))))+ details))++(defn- with-slow-culprits+ "Replace `details.slow_entity_ids` with hydrated `details.slow_entities` from `culprits`. A no-op for a+ slow leaf (card/transform), which rolls up no culprits and so carries no `slow_entity_ids`."+ [details culprits]+ (rewrite-ids->entities details :slow_entity_ids :slow_entities culprits))++(defn- with-duplicate-peers+ "Replace `details.duplicate_entity_ids` with hydrated same-type `details.duplicate_entities` from+ `entities` (keyed `[entity-type id]`). The raw stored ids are not permission-filtered, so the hydrated+ list is the served form; a filtered-out peer drops out like a deleted one. A no-op when the finding+ carries no `duplicate_entity_ids`."+ [details entity-type entities]+ (rewrite-ids->entities details :duplicate_entity_ids :duplicate_entities #(get entities [entity-type %])))++(defmulti ^:private finalize-finding+ "Apply the finding-type-specific tail to one assembled finding `base`: hoist the type's native top-level+ column(s) from `row`, and rewrite `details` from the batch-hydrated `ctx` (`{:culprits _ :entities _}`).+ Dispatches per row on `finding_type`, so a page may mix finding types (an umbrella endpoint; the imbalanced+ umbrella spans three); an unregistered type throws - fail-closed, no `:default`."+ {:arglists '([finding-type base row ctx])}+ (fn [finding-type _base _row _ctx] finding-type))++(defmethod finalize-finding :stale [_ base row _ctx]+ (merge base (select-keys row [:last_active_at])))++(defmethod finalize-finding :slow [_ base row {:keys [culprits]}]+ (-> (merge base (select-keys row [:duration_ms]))+ (update :details with-slow-culprits culprits)))++(defmethod finalize-finding :duplicate_name [_ base row {:keys [entities]}]+ (-> (merge base (select-keys row [:duplicate_count]))+ (update :details with-duplicate-peers (:entity_type row) entities)))++;; the imbalanced umbrella - all three types hoist the same measured magnitude, no details rewrite+(defmethod finalize-finding :empty [_ base row _ctx] (merge base (select-keys row [:content_count])))+(defmethod finalize-finding :sparse [_ base row _ctx] (merge base (select-keys row [:content_count])))+(defmethod finalize-finding :crowded [_ base row _ctx] (merge base (select-keys row [:content_count])))++(defn- can-write-by-entity+ "`{[entity-type entity-id] → can_write bool}` for the page's findings, each entity hydrated with its own+ model's `:can_write` - collection curate for card/dashboard/document/collection, DB-transforms permission+ for transform. Collection items select only `collection_id`; collection and transform read many columns,+ so they stay whole-row."+ [findings]+ (into {}+ (for [[etype rows] (group-by :entity_type findings)+ :let [model (common/entity-type->model etype)+ ids (into #{} (map :entity_id) rows)+ selectable (cond+ ;; Include document_id so Card's write-permission check does not fetch the full+ ;; Card (and parse unrelated legacy result_metadata) to resolve its parent Document.+ (= etype :card)+ [model :id :collection_id :document_id :card_schema]++ (isa? common/hierarchy etype ::common/collection-item)+ [model :id :collection_id]++ :else+ model)]+ :when (and model (seq ids))+ row (t2/hydrate (cd.db/entity-rows selectable ids) :can_write)]+ [[etype (:id row)] (boolean (:can_write row))])))++(defn hydrate-findings+ "Project stored findings into the response shape: flat identity + denormalized display fields, plus a+ nested `details` = stored verdict + {collection, description, owner, creator, view_count?}. `view_count`+ is the entity's live usage counter, present only for types that have the column (all but transform).+ A card finding also carries a top-level `card_type` (question/model/metric) - served from the stored+ column, not hydrated live. Its `display` is the live visualization type (e.g. table/bar/line), used to+ choose the question icon in the table and sidebar header without waiting for a new scan.+ Batched, page-size-independent.++ The finding-type-specific tail - the hoisted native column(s) and any `details` rewrite (slow culprits /+ duplicated peers) - is dispatched per row on each finding's `finding_type` via [[finalize-finding]], so a+ page may mix finding types (an umbrella endpoint; the imbalanced umbrella spans three).+ `exclude-personal?` (the request's `include-personal-collections` param, negated) gates the culprit/peer+ hydration so it matches the findings filter."+ [findings exclude-personal?]+ (let [ctx-by-type (into {} (for [[etype rows] (group-by :entity_type findings)]+ [etype (entity-context etype (map :entity_id rows))]))+ coll-ids (into #{} (keep (fn [{:keys [entity_type entity_id]}]+ (get-in ctx-by-type [entity_type entity_id :collection_id])))+ findings)+ ;; scan-time parents ride along so the collection_name gate below can check their readability -+ ;; an entity may have moved since the scan, so they can differ from the live coll-ids+ breadcrumbs (collection-breadcrumbs (into coll-ids (keep :scope_collection_id) findings))+ can-write (can-write-by-entity findings)+ ;; Batch-prep runs over whatever the page carries - an absent finding type contributes no ids, so+ ;; its hydrator issues no query.+ culprits (hydrate-slow-entities (into #{} (mapcat (comp :slow_entity_ids :details)) findings)+ exclude-personal?)+ entities (hydrate-duplicate-entities findings exclude-personal?)+ ctx {:culprits culprits :entities entities}]+ (mapv (fn [{:keys [id finding_type entity_type entity_id detected_at entity_created_at+ entity_name entity_creator_id entity_creator_name card_type entity_kind+ entity_collection_name scope_collection_id details] :as row}]+ (let [entity (get-in ctx-by-type [entity_type entity_id])+ breadcrumb (entity-breadcrumb entity_type entity breadcrumbs)+ details* (merge details+ {:collection breadcrumb+ :description (:description entity)+ ;; only transforms have owner columns; null for the rest.+ :owner (normalized-owner entity)+ ;; creator denormalized (id + common_name) - no live :creator hydrate.+ :creator (when entity_creator_id+ {:id entity_creator_id :name entity_creator_name :type :user})}+ (when-some [view-count (:view_count entity)]+ {:view_count view-count}))+ base (cond-> {:id id+ :finding_type finding_type+ :entity_type entity_type+ :entity_id entity_id+ :detected_at detected_at+ :entity_display_name entity_name+ :created_at entity_created_at+ :details details*+ :can_write (get can-write [entity_type entity_id] false)+ ;; additive flat kind; coalesce pre-migration rows+ :entity_kind (or entity_kind card_type entity_type)+ ;; scan-time display name for the collection sort column (root rows+ ;; carry the stored root label), gated on the scan-time parent's+ ;; readability - the live gates only cover the entity's current+ ;; parent. Rows with no scan-time parent fall back to the breadcrumb.+ :collection_name (when (if scope_collection_id+ (get breadcrumbs scope_collection_id)+ breadcrumb)+ entity_collection_name)}+ ;; keyed on entity type so a card row with NULL card_type still serves+ ;; the key, as null+ (= entity_type :card) (assoc :card_type card_type :display (:display entity)))]+ (finalize-finding finding_type base row ctx)))+ findings)))++(defn last-scan-at+ "`detected_at` of the most recent finding overall (≈ the latest scan's time), or nil if none."+ []+ (cd.db/last-detected-at))++;;; ---------------------------------------------- sort config -----------------------------------------++(def sort-directions+ "Valid sort directions for the finding lists."+ #{:asc :desc})++(def base-sort-column->field+ "Sortable params common to every finding list → their native `content_diagnostics_finding` column.+ Entity attributes are denormalized at scan time, so sorting is a plain `ORDER BY` with no join. Each+ endpoint `assoc`s its per-finding-type magnitude column (stale `:last-active-at`, slow `:duration-ms`).+ `entity-type` sorts by the flat `entity_kind` (card sub-kinds order as peers, not clustered under+ `card`); every name-ish sort is case-insensitive (and collation-stable) via lower() - `created-by`+ included, now that `entity_creator_name` carries no index for a raw sort to have used.+ collection-name orders by the scan-time stored parent name even when the caller cannot read it - the+ name itself is gated at serve time, and the ordering position is the accepted, marginal exposure."+ {:detected-at :detected_at+ :entity-type :entity_kind+ :name [:lower :entity_name]+ :created-at :entity_created_at+ :created-by [:lower :entity_creator_name]+ :collection-name [:lower :entity_collection_name]})diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/duplicated.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/duplicated.cljnew file mode 100644index 00000000000..c85640b74ea--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/duplicated.clj@@ -0,0 +1,93 @@+(ns metabase-enterprise.content-diagnostics.checkers.duplicated+ "The `duplicated` Content Diagnostics checker - match mode `name`: two or more non-archived entities of+ the same type whose normalized names are equal and non-blank form a duplicate cluster. Every member of+ a cluster of size k gets one `:duplicate_name` finding whose peers are the other k-1 members, with+ `:duplicate-count` = the peer count (→ the native `duplicate_count` column).++ `details` carries `normalized_name` (the normalized name the cluster collided on) and+ `duplicate_entity_ids` (the flagged entity's peer ids - what the serve layer hydrates). Clustering is by+ normalized name within an entity type; cards cluster across all sub-kinds (`type` -+ question/model/metric), so a question and a model sharing a name are duplicates. One lightweight+ (id, name) load per entity type, grouped in memory - no per-entity loop, app-db only."+ (:require+ [clojure.string :as str]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.util :as u]))++(set! *warn-on-reflection* true)++(defn- normalize-name+ "Normalize a name for duplicate detection: remove diacritical marks, lowercase, remove invisible and collapse whitespace"+ [s]+ (-> (or (u/remove-diacritical-marks s) "") ; remove-diacritical-marks returns nil on empty input+ u/lower-case-en+ (str/replace #"(?U)\p{Cf}" "") ; strip Unicode format chars (zero-width joiners, bidi marks, soft hyphens)+ (str/replace #"(?U)\s+" " ") ; collapse any run of Unicode whitespace to a single space+ str/trim))++(defmulti ^:private candidate-rows+ "Lightweight non-archived `(id, name)` rows for one entity type, for name clustering. card/dashboard/document+ (`::collection-item`) filter on their `archived` column, keep only rows in eligible containers+ (`common/eligible-container-clause`), and add any `common/candidate-cols` (card carries `:card_schema`,+ required by its after-select hook); cards also drop the ones a document owns; transform has no+ `archived` column (hard-deleted), so every row counts; collection narrows to the shared+ collection-subject set. Scan-time and instance-wide - deliberately un-permissioned, unlike the serve+ layer's `read-entity-rows`."+ {:arglists '([entity-type])}+ identity+ :hierarchy #'common/hierarchy)++(defmethod candidate-rows ::common/collection-item+ [entity-type]+ ;; :card_schema (a candidate-col for cards) is required on any Card select - its after-select hook reads it.+ (cd.db/name-rows (common/entity-type->model entity-type)+ (common/candidate-cols entity-type)+ [:and+ [:= :archived false]+ ;; peers come from this row set, so a document-owned card has to go before+ ;; clustering, not just from the emitted findings+ ;; (`common/remove-document-internal-card-findings-xf`)+ (when (= entity-type :card) [:= :document_id nil])+ (common/eligible-container-clause :collection_id)]))++(defmethod candidate-rows :transform+ [_]+ (cd.db/name-rows :model/Transform nil nil))++(defmethod candidate-rows :collection+ [_]+ ;; the shared collection-subject definition, so no two checkers can scan divergent collection sets+ (cd.db/name-rows :model/Collection nil common/eligible-collection-where))++(defn- cluster-findings+ "One `:duplicate_name` finding per member of a name cluster; peers are the other members (symmetric: in+ {A,B,C}, A's finding lists {B,C}, B's {A,C}, C's {A,B})."+ [entity-type normalized-name rows]+ (let [ids (mapv :id rows)]+ (for [id ids+ :let [peer-ids (filterv #(not= % id) ids)]]+ {:entity-type entity-type+ :entity-id id+ :finding-type :duplicate_name+ :duplicate-count (count peer-ids)+ :details {:normalized_name normalized-name+ :duplicate_entity_ids peer-ids}})))++(defn- findings-for-type+ "Group one entity type's rows by normalized name and emit findings for every cluster of ≥ 2 (cards cluster+ across all sub-kinds - grouping is by name alone). Clusters with a blank normalized name are skipped:+ `name` is NOT NULL on every covered model but can be whitespace-only, and unknown is not duplicate."+ [entity-type]+ (for [[norm-name rows] (group-by (comp normalize-name :name) (candidate-rows entity-type))+ :when (and (not (str/blank? norm-name)) (>= (count rows) 2))+ finding (cluster-findings entity-type norm-name rows)]+ finding))++(defn checker+ "Instance-wide `:duplicate_name` finding maps across all covered entity types. The denormalized display+ attrs are stamped by `common/attach-entity-attrs`; `:duration-ms`/`:last-active-at` are left unset+ (those columns stay NULL on duplicated findings)."+ []+ (common/attach-entity-attrs+ (into [] (mapcat findings-for-type) (keys common/entity-type->model))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/common.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/common.cljnew file mode 100644index 00000000000..5148a0a9912--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/common.clj@@ -0,0 +1,84 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.common+ "Shared helpers for the three imbalanced checkers (`empty`/`sparse`/`crowded`, one per sibling+ namespace): the finding constructor and the app-db count/row helpers more than one of them needs -+ notably `eligible-collections`, the checkers' view over the module-wide collection-subject definition+ (`common/eligible-collection-where`), so the checkers never scan different collection sets. Each+ checker re-runs only the helpers it needs; these are cheap app-db aggregates, so we favor independence+ over threading shared results.++ A collection's direct items are exactly its non-archived child collections plus its cards, dashboards,+ documents, and transforms (a card inside a dashboard or document lives in that container, not the+ collection). Checker-specific probes - the card-run window, document parsing, per-tab dashcard+ grouping - live in the checker namespaces."+ (:require+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.collections.models.collection :as collection]+ [metabase.util :as u]))++(set! *warn-on-reflection* true)++(defn finding+ "The shared imbalanced finding shape: the measured amount in `:content-count`, the crossed limit and+ its unit in `:details`."+ [entity-type entity-id finding-type content-count details]+ {:entity-type entity-type+ :entity-id entity-id+ :finding-type finding-type+ :content-count content-count+ :details details})++(defn collection-item-cards+ "Non-archived cards in eligible containers that count as direct collection items, as+ `{:id :collection_id}` rows - dashboard/document-internal cards live inside their container, not the+ collection."+ []+ (cd.db/collection-item-card-rows (common/eligible-container-clause :collection_id)))++(defn active-dashboards+ "Non-archived dashboards in eligible containers as `{:id :collection_id}` rows."+ []+ (cd.db/active-dashboard-rows (common/eligible-container-clause :collection_id)))++(defn document-items+ "Non-archived documents in eligible containers as `{:id :collection_id}` rows - the light form for+ collection counting (no AST fetch)."+ []+ (cd.db/document-rows (common/eligible-container-clause :collection_id)))++(defn active-documents+ "Non-archived documents in eligible containers with their AST - for the document verdicts, which parse+ `:document`."+ []+ (cd.db/active-document-rows (common/eligible-container-clause :collection_id)))++(defn transform-items+ "Transforms as `{:id :collection_id}` rows - transforms are hard-deleted (no archived column), so every+ row counts."+ []+ ;; no container clause: a transform's only possible containers are transforms-namespace collections+ ;; (`allowed-namespaces :model/Transform`), and both consumers ignore ineligible collections' counts+ (cd.db/transform-rows))++(defn dashboard-dashcard-totals+ "`{dashboard-id -> primary dashcard count across all tabs}`; no row = 0. Counts primary dashcards+ only - a series card layers onto another dashcard without taking a layout slot of its own."+ []+ (u/index-by :dashboard_id :cnt (cd.db/dashboard-dashcard-counts)))++(defn eligible-collections+ "The collections the imbalanced checkers scan: the shared collection-subject set+ (`common/eligible-collection-where`)."+ []+ (cd.db/collection-locations common/eligible-collection-where))++(defn direct-item-counts+ "`{collection-id -> raw direct item count}` over `collections`: child collections plus the+ card/dashboard/document/transform items. Empty items still count; all three checkers read this+ same count."+ [collections]+ (merge-with ++ (frequencies (keep (comp collection/location-path->parent-id :location) collections))+ (frequencies (keep :collection_id+ (concat (collection-item-cards) (active-dashboards) (document-items)+ (transform-items))))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded.cljnew file mode 100644index 00000000000..8ba0454eb1f--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded.clj@@ -0,0 +1,62 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.crowded+ "The `crowded` imbalanced checker: too much content, across collections, dashboards, and documents.+ Runs independently of `empty`/`sparse`, so an entity can be flagged by more than one (a many-tab+ dashboard with 0 dashcards is both `crowded` and `empty`).++ What counts as crowded:+ - Collection: more direct items than the limit.+ - Dashboard: too many dashcards on a single tab, or - only if that passes - too many tabs. Checking+ dashcards-per-tab first means a dashboard gets at most one `crowded` finding; a tabless dashboard+ counts as one tab. (Per-tab counting is crowded-only, so this checker runs its own grouped query+ rather than reusing the shared across-tabs total.)+ - Document: more embedded cards than the limit.++ Each finding records the measured amount and the limit it crossed. Set-based, reads only the app DB."+ (:require+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.common :as shared]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase.documents.prose-mirror :as prose-mirror]+ [metabase.util :as u]))++(set! *warn-on-reflection* true)++(defn checker+ "Instance-wide `crowded` findings across collections, dashboards, and documents. A dashboard is checked+ dashcards-per-tab first, then tab count, so it gets at most one `crowded` finding; a tabless dashboard+ counts as one tab."+ []+ (let [crowded-collection-items (cd.settings/content-diagnostics-crowded-collection-threshold-items)+ crowded-dashcards-per-tab (cd.settings/content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab)+ crowded-tabs (cd.settings/content-diagnostics-crowded-dashboard-threshold-tabs)+ crowded-document-cards (cd.settings/content-diagnostics-crowded-document-threshold-cards)+ dashcard-groups (group-by :dashboard_id (cd.db/dashboard-tab-dashcard-counts))+ tab-counts (u/index-by :dashboard_id :cnt (cd.db/dashboard-tab-counts))]+ (common/attach-entity-attrs+ (concat+ (let [collections (shared/eligible-collections)+ counts (shared/direct-item-counts collections)]+ (for [{:keys [id]} collections+ :let [n (long (get counts id 0))]+ :when (> n crowded-collection-items)]+ (shared/finding :collection id :crowded n {:threshold crowded-collection-items :unit "items"})))+ (for [{:keys [id]} (shared/active-dashboards)+ :let [tab-rows (get dashcard-groups id)+ max-per-tab (transduce (map :cnt) max 0 tab-rows)+ tabs (max 1 (long (get tab-counts id 0)))+ verdict (cond+ (> max-per-tab crowded-dashcards-per-tab)+ (shared/finding :dashboard id :crowded max-per-tab+ {:threshold crowded-dashcards-per-tab :unit "dashcards"})++ (> tabs crowded-tabs)+ (shared/finding :dashboard id :crowded tabs+ {:threshold crowded-tabs :unit "tabs"}))]+ :when verdict]+ verdict)+ (for [doc (shared/active-documents)+ :when (= (:content_type doc) prose-mirror/prose-mirror-content-type)+ :let [n (count (prose-mirror/card-ids doc))]+ :when (> n crowded-document-cards)]+ (shared/finding :document (:id doc) :crowded n {:threshold crowded-document-cards :unit "cards"}))))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty.cljnew file mode 100644index 00000000000..b68b08e331b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty.clj@@ -0,0 +1,100 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.empty+ "The `empty` imbalanced checker: content with nothing in it, across collections, cards, dashboards, and+ documents. Runs independently of `sparse`/`crowded`, so an entity can be flagged by more than one (a+ many-tab dashboard with 0 dashcards is both `crowded` and `empty`).++ What counts as empty:+ - Collection: no direct items, the same count `sparse`/`crowded` use. Items are exactly the covered+ kinds: child collections, cards, dashboards, documents, transforms; empty items still count (a+ folder of only-empty dashboards is not `empty` - the dashboards are).+ - Card: its latest clean run (no parameters, sandbox, cache, or error) within+ `content-diagnostics-empty-card-lookback-days` returned 0 rows; a card with no clean run in that window+ is left alone. `as_of` is that run's start.+ - Dashboard: no dashcards.+ - Document: no text and no embedded content.++ Transforms are never flagged - the app DB has no reliable row count for a transform's target table.++ Every finding records a count of 0 (there is no threshold - 0 is definitionally empty). Set-based,+ reads only the app DB."+ (:require+ [clojure.string :as str]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.common :as shared]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase.documents.prose-mirror :as prose-mirror]+ [metabase.util :as u]))++(set! *warn-on-reflection* true)++(defn- lookback-cutoff+ "The earliest clean run the card arm will draw a verdict from, per+ `content-diagnostics-empty-card-lookback-days`. A card with no clean run since then is left alone rather+ than called empty."+ []+ (t/minus (t/offset-date-time) (t/days (cd.settings/content-diagnostics-empty-card-lookback-days))))++(defn- empty-card-id->as-of+ "`{card-id -> started_at}` for every non-archived card in an eligible container whose latest clean run+ within the [[lookback-cutoff]] window returned 0 rows. Clean means unparameterized, unsandboxed, not a+ cache hit, and error-free - anything else is not instance-wide evidence of emptiness (a sandbox filters+ rows per user, and an errored run means broken, not empty)."+ []+ (u/index-by :card_id :started_at+ (cd.db/cards-with-empty-latest-run (lookback-cutoff)+ (common/eligible-container-clause :c.collection_id))))++(def ^:private structural-node-types+ "Prose-mirror node types that are pure structure or layout - a document built only from these, with no+ non-blank text or reference label, has no content. `text` is here because a text node's substance is+ its `:text`, checked separately. The list is intentionally not exhaustive: an unknown node type (e.g.+ an image) counts as content, so the check never wrongly calls a document empty."+ #{"doc" "paragraph" "heading" "text" "bulletList" "orderedList" "listItem" "blockquote"+ "codeBlock" "flexContainer" "resizeNode" "hardBreak"})++(defn- document-empty?+ "True when a prose-mirror document has **no content of any kind**: no non-blank text, no non-blank+ reference label (smart links, mentions), and no node outside [[structural-node-types]] (card embeds+ and unknown future node types count as content)."+ [doc]+ (empty? (prose-mirror/collect-ast+ doc+ (fn [{:keys [type text] :as node}]+ (when (or (not (str/blank? text))+ (not (str/blank? (get-in node [:attrs :label])))+ (and (some? type) (not (structural-node-types type))))+ node)))))++(defn checker+ "Instance-wide `empty` findings across collections, cards, dashboards, and documents. Collections are+ flagged on their direct-item count alone - the per-item verdicts never roll up."+ []+ (let [empty-card-as-of (empty-card-id->as-of)+ dashboards (shared/active-dashboards)+ dashcard-totals (shared/dashboard-dashcard-totals)+ documents (shared/active-documents)+ collections (shared/eligible-collections)+ item-counts (shared/direct-item-counts collections)+ empty-dashboards (into #{}+ (keep #(when (zero? (long (get dashcard-totals (:id %) 0))) (:id %)))+ dashboards)+ ;; only a parseable (prose-mirror) document can be judged empty - any other content type is+ ;; unknown, so it is never flagged+ empty-documents (into #{}+ (keep #(when (and (= (:content_type %) prose-mirror/prose-mirror-content-type)+ (document-empty? %))+ (:id %)))+ documents)]+ (common/attach-entity-attrs+ (concat+ (for [{:keys [id]} collections+ :when (zero? (long (get item-counts id 0)))]+ (shared/finding :collection id :empty 0 {:threshold 0 :unit "items"}))+ (for [[card-id as-of] empty-card-as-of]+ (shared/finding :card card-id :empty 0 {:threshold 0 :unit "rows" :as_of as-of}))+ (for [id empty-dashboards]+ (shared/finding :dashboard id :empty 0 {:threshold 0 :unit "dashcards"}))+ (for [id empty-documents]+ (shared/finding :document id :empty 0 {:threshold 0 :unit "cards"}))))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse.cljnew file mode 100644index 00000000000..0f77fab9d20--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse.clj@@ -0,0 +1,39 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.sparse+ "The `sparse` imbalanced checker: a little content, but not none, across collections and dashboards.+ Runs independently of `empty`/`crowded`. The rule floors at 1 (`0 < n < limit`), so a zero-count+ subject belongs to `empty` alone - though a sparse entity can still be `crowded` on another axis (a+ 6-tab dashboard with 2 dashcards is both).++ What counts as sparse:+ - Collection: between 1 and the limit direct items (empty items still count). Items are exactly the+ covered kinds: child collections, cards, dashboards, documents, transforms.+ - Dashboard: between 1 and the limit dashcards total across tabs.++ Each finding records the measured count and the limit. Set-based, reads only the app DB."+ (:require+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.common :as shared]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]))++(set! *warn-on-reflection* true)++(defn checker+ "Instance-wide `sparse` findings across collections and dashboards. Sparse means a little content, not+ none: the rule floors at 1, so a zero-count subject belongs to `empty` alone."+ []+ (let [sparse-collection-items (cd.settings/content-diagnostics-sparse-collection-threshold-items)+ sparse-dashboard-dashcards (cd.settings/content-diagnostics-sparse-dashboard-threshold-dashcards)+ dashcard-totals (shared/dashboard-dashcard-totals)]+ (common/attach-entity-attrs+ (concat+ (let [collections (shared/eligible-collections)+ counts (shared/direct-item-counts collections)]+ (for [{:keys [id]} collections+ :let [n (long (get counts id 0))]+ :when (< 0 n sparse-collection-items)]+ (shared/finding :collection id :sparse n {:threshold sparse-collection-items :unit "items"})))+ (for [{:keys [id]} (shared/active-dashboards)+ :let [total (long (get dashcard-totals id 0))]+ :when (< 0 total sparse-dashboard-dashcards)]+ (shared/finding :dashboard id :sparse total+ {:threshold sparse-dashboard-dashcards :unit "dashcards"}))))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/slow.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/slow.cljnew file mode 100644index 00000000000..90fae40a3c9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/slow.clj@@ -0,0 +1,178 @@+(ns metabase-enterprise.content-diagnostics.checkers.slow+ "The `slow` Content Diagnostics checker - flags content whose execution time exceeds a configurable+ threshold by reading the precomputed signals the product already caches (no re-execution):++ - **Card** (leaf): median `query_execution.running_time` with cache hits excluded, over+ `slow-card-threshold-seconds`.+ - **Dashboard / Document** (container): a *roll-up* - flagged when they embed ≥1 slow card. The+ culprit card ids are frozen in `details` (`slow_entity_ids`) and hydrated to objects at read time;+ the container's own `:duration-ms` is the slowest culprit's median, a representative magnitude so+ containers sort/filter by duration alongside leaves.+ - **Transform** (leaf): wall-clock duration (`end_time - start_time`) of its latest **finished** run+ (succeeded/failed/timeout - not canceled) over `slow-transform-threshold-seconds`.++ Leaves freeze their `threshold_ms` in `details` at scan time; every finding carries its measured+ magnitude in the top-level `:duration-ms` (→ the native `duration_ms` column). Every detector is+ **set-based** (a fixed handful of grouped queries, no per-entity loops), reads only the app-db, never the warehouse,+ and only considers activity within the last [[lookback-days]] days.+ The denormalized display attrs are stamped by `common/attach-entity-attrs`; `:last-active-at` is left+ unset (the column stays NULL on slow findings)."+ (:require+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase.documents.prose-mirror :as prose-mirror]+ [metabase.util :as u]))++(set! *warn-on-reflection* true)++(def ^:private lookback-days+ "Only activity within this window counts toward slowness - executions or runs older than this say nothing+ about how the content performs today. It decides which runs are averaged, so narrowing it can change a+ median and flip a verdict; a card's served `duration_ms` is the median over this window. Not a setting for+ that reason. The `empty` checker's window answers a different question - whether there is recent enough+ evidence to hold a verdict - and has its own setting, `content-diagnostics-empty-card-lookback-days`."+ 30)++(defn- lookback-cutoff+ "The earliest activity timestamp the current scan considers."+ []+ (t/minus (t/offset-date-time) (t/days lookback-days)))++;;; -------------------------------------------------- cards --------------------------------------------------++(defn- slow-card-id->median-ms+ "`{card-id → median running_time (ms, rounded)}` for every **non-archived** card in an eligible+ container whose median over its last [[lookback-days]] days of non-cache-hit executions exceeds+ `threshold-ms`. One grouped query, no per-card loop."+ [threshold-ms]+ (into {}+ ;; AVG comes back as BigDecimal - round to a Long for the native bigint column.+ (map (juxt :card_id #(Math/round (double (:median_ms %)))))+ (cd.db/card-run-medians threshold-ms+ (lookback-cutoff)+ (common/eligible-container-clause :c.collection_id))))++(defn- card-findings+ "Leaf card findings - one per slow card, carrying the measured median (`:duration-ms`) and freezing the+ threshold."+ [card->median-ms threshold-ms]+ (for [[card-id median-ms] card->median-ms]+ {:entity-type :card+ :entity-id card-id+ :finding-type :slow+ :duration-ms median-ms+ :details {:threshold_ms threshold-ms}}))++;;; ----------------------------------------------- container roll-ups ----------------------------------------+;;; A dashboard/document is "slow" when it embeds at least one slow card. We store the culprit ids+;;; (`slow_entity_ids`) loose and let the read layer hydrate them into objects; the container's own+;;; `:duration-ms` is the slowest culprit's median, so it sorts/filters by duration like a leaf.++(defn- representative-duration-ms+ "A container's stand-in magnitude - the slowest culprit card's median."+ [card->median-ms culprit-ids]+ (apply max (map card->median-ms culprit-ids)))++(defn- container-finding+ "The shared roll-up finding for a container (dashboard/document): its culprit slow cards frozen in+ `slow_entity_ids`, and its own `:duration-ms` set to the slowest culprit's median so it sorts/filters+ by duration like a leaf. The two containers differ only in how they derive `culprit-ids`."+ [entity-type entity-id card->median-ms culprit-ids]+ {:entity-type entity-type+ :entity-id entity-id+ :finding-type :slow+ :duration-ms (representative-duration-ms card->median-ms culprit-ids)+ :details {:slow_entity_ids culprit-ids}})++(defn- dashboard-culprit-pairs+ "`{:dashboard_id … :card_id …}` rows for every way a **non-archived** dashboard in an eligible+ container runs a card in `slow-card-ids` **when it renders**: a dashcard's primary card, and a+ combined-**series** card (extra+ cards layered onto one dashcard's visualization). Both execute the card's real query on dashboard load.++ We deliberately exclude the third dashboard→card reference dependency tracking counts — a filter's **card+ value-source** (`parameter_card`, the \"From another model or question\" filter option) — because it does+ not reflect this card's slowness: the dropdown values are fetched **on demand** (only when the filter is+ opened), are **cached**, and run a *different, limited distinct-values query* (see+ `parameters.custom-values/values-from-card-query`), not the card's chart query that `duration_ms` measures."+ [slow-card-ids]+ (let [eligible [:and+ [:= :d.archived false]+ ;; the container clause is re-applied to the dashboard itself - a slow (eligible) card+ ;; can be embedded by a dashboard living in an ineligible container+ (common/eligible-container-clause :d.collection_id)]]+ (concat+ ;; primary dashcard cards (a card can appear on several tabs — deduped by the caller)+ (cd.db/dashboard-primary-card-pairs eligible slow-card-ids)+ ;; combined-series cards (extra cards layered onto one dashcard's visualization)+ (cd.db/dashboard-series-card-pairs eligible slow-card-ids))))++(defn- dashboard-findings+ "Container findings for **non-archived** dashboards that render ≥1 of `slow-card-ids`. `slow_entity_ids`+ is the de-duplicated set of slow cards the dashboard runs on load — primary or series (see+ `dashboard-culprit-pairs`)."+ [card->median-ms slow-card-ids]+ (when (seq slow-card-ids)+ (for [[dash-id rows] (group-by :dashboard_id (dashboard-culprit-pairs slow-card-ids))+ :let [culprit-ids (vec (distinct (map :card_id rows)))]]+ (container-finding :dashboard dash-id card->median-ms culprit-ids))))++(defn- document-findings+ "Container findings for **non-archived** prose-mirror documents in eligible containers embedding ≥1+ of `slow-card-ids`.+ Card ids are parsed from each document's prose-mirror body (`prose-mirror/card-ids`); only documents+ with the prose-mirror content type are scanned (others would assert-throw and embed no cards anyway)."+ [card->median-ms slow-card-ids]+ (when (seq slow-card-ids)+ (let [slow? (set slow-card-ids)]+ ;; the container clause is re-applied to the document itself - a slow (eligible) card can be+ ;; embedded by a document living in an ineligible container+ (for [doc (cd.db/documents-of-content-type prose-mirror/prose-mirror-content-type+ (common/eligible-container-clause :collection_id))+ :let [culprits (filterv slow? (distinct (prose-mirror/card-ids doc)))]+ :when (seq culprits)]+ (container-finding :document (:id doc) card->median-ms culprits)))))++;;; ------------------------------------------------ transforms -----------------------------------------------++(defn- run-duration-ms+ "Wall-clock milliseconds between two run timestamps."+ [start end]+ (.toMillis ^java.time.Duration (t/duration start end)))++(defn- transform-findings+ "Leaf transform findings - a transform is slow when the wall-clock duration of its latest **finished**+ run (succeeded, failed, or timed out) exceeds `threshold-ms`. Canceled runs don't count: their+ duration measures when someone hit cancel, not the transform. Only runs started within the last+ [[lookback-days]] days are considered."+ [threshold-ms]+ (for [{:keys [transform_id start_time end_time]}+ (cd.db/finished-transform-run-spans (lookback-cutoff))+ :when (and start_time end_time)+ :let [duration-ms (run-duration-ms start_time end_time)]+ :when (> duration-ms threshold-ms)]+ {:entity-type :transform+ :entity-id transform_id+ :finding-type :slow+ :duration-ms duration-ms+ :details {:threshold_ms threshold-ms}}))++;;; ------------------------------------------------- checker -------------------------------------------------++(defn checker+ "Instance-wide `slow` finding maps across card (leaf), dashboard/document (container roll-up of slow+ cards), and transform (leaf). The slow-card set is computed once and reused by both roll-ups; the+ denormalized display attrs are stamped by `common/attach-entity-attrs`."+ []+ (let [card-threshold-ms (u/seconds->ms (cd.settings/content-diagnostics-slow-card-threshold-seconds))+ transform-threshold-ms (u/seconds->ms (cd.settings/content-diagnostics-slow-transform-threshold-seconds))+ card->median-ms (slow-card-id->median-ms card-threshold-ms)+ slow-card-ids (vec (keys card->median-ms))]+ (common/attach-entity-attrs+ (concat+ (card-findings card->median-ms card-threshold-ms)+ (dashboard-findings card->median-ms slow-card-ids)+ (document-findings card->median-ms slow-card-ids)+ (transform-findings transform-threshold-ms)))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/stale.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/stale.cljnew file mode 100644index 00000000000..0625a109489--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/checkers/stale.clj@@ -0,0 +1,61 @@+(ns metabase-enterprise.content-diagnostics.checkers.stale+ "The `stale` Content Diagnostics checker - instance-wide inactive content across every covered entity+ type, sourced from the stale module's staleness-rule entry point and narrowed to eligible containers+ here (the stale query arms are shared with the standalone stale tool). Freezes the staleness threshold+ and the per-entity activity anchor at scan time (drift between scans is acceptable)."+ (:require+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.schema :as cd.schema]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ ;; sanctioned export: find-candidates is the stale module's public staleness-rule entry point+ ;; (see enterprise/stale :api in .clj-kondo/config/modules/config.edn).+ [metabase-enterprise.stale.impl :as stale.impl]))++(set! *warn-on-reflection* true)++(defn checker+ "Instance-wide stale candidates for every covered entity type as finding maps, dropping+ card/dashboard/document candidates in ineligible containers (transforms are exempt - they run+ regardless of their folder's state). `:entity-name` and the activity anchor come from the stale query;+ the remaining denormalized attrs are filled by `common/attach-entity-attrs`."+ []+ (let [threshold (cd.settings/content-diagnostics-stale-threshold-days)+ cutoff (t/minus (t/local-date) (t/days threshold))+ {:keys [rows]} (stale.impl/find-candidates+ {:collection-ids :all+ ;; NOT (vals common/entity-type->model): find-candidates throws on models+ ;; with no find-stale-query method, and :model/Collection has none+ :models (into #{} (map common/entity-type->model)+ cd.schema/non-collection-entity-types)+ ;; name + recency come from the stale query - the per-model recency source+ ;; stays single-sourced in the `find-stale-query` arms; collection_id feeds+ ;; the container post-filter below.+ :include-columns #{:name :last_used_at :collection_id}+ :cutoff-date cutoff+ :limit nil+ :offset nil+ :sort-column :name+ :sort-direction :asc})+ ;; post-filter rather than a WHERE in the shared arms; safe because :limit is nil (no page to+ ;; backfill). Also closes the arms' sample-content gap (they never check is_sample).+ eligible-container-ids (cd.db/collection-ids common/eligible-collection-where)]+ (common/attach-entity-attrs+ (for [{:keys [id model collection_id last_used_at] entity-name :name} rows+ :let [entity-type (common/model->entity-type model)]+ ;; transforms are container-exempt: they execute regardless of their folder's state, so a+ ;; stale transform in an archived folder is a retirement candidate, not noise+ :when (and entity-type+ (or (= entity-type :transform)+ (nil? collection_id)+ (contains? eligible-container-ids collection_id)))]+ {:entity-type entity-type+ :entity-id id+ :finding-type :stale+ ;; scan-time activity anchor (the stale query aliases each model's recency column to+ ;; `last_used_at`); nil ⇒ never used/ran+ :last-active-at last_used_at+ ;; denormalized at scan time - the sort/display name; drift between scans is acceptable+ :entity-name entity-name+ :details {:threshold_days threshold}}))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/common.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/common.cljnew file mode 100644index 00000000000..9b18a3565aa--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/common.clj@@ -0,0 +1,211 @@+(ns metabase-enterprise.content-diagnostics.common+ "Shared building blocks for the Content Diagnostics module: the entity-type ↔ model mapping every+ checker and the serve layer key off, plus the scan-time denormalization helper the checkers share.+ Requires nothing module-internal, so both `checkers/*` and `serve` can depend on it acyclically."+ (:require+ [clojure.set :as set]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.collections.models.collection :as collection]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(def entity-type->model+ "Content Diagnostics entity-types → their Toucan models. Single source of truth: the API's display+ hydration and each checker's candidate→finding mapping both derive from this (inverse below).+ `:collection` is the one subject that is not *in* a collection but *is* one - it has no+ `collection_id`/`creator_id` columns, so every consumer that touches those columns special-cases it."+ {:card :model/Card+ :collection :model/Collection+ :dashboard :model/Dashboard+ :document :model/Document+ :transform :model/Transform})++(def model->entity-type+ "Inverse of [[entity-type->model]] - some candidate sources (e.g. `find-candidates`) return `:model`+ keywords like `:model/Card`."+ (set/map-invert entity-type->model))++(defn entity-collection-clauses+ "For each of `entity-types`, a clause keeping findings whose entity currently lives in a collection+ satisfying `coll-pred-fn` - a fn of the entity-type and the column holding the entity's collection id.+ Checked against the entity's own table, so it reflects where the entity is now. A `:collection` subject+ *is* the collection, so its predicate is keyed on its own `:id`. The entity-type is passed so a caller+ can vary the predicate per type - transforms outlive their folder's archiving, so their visibility+ differs. Callers combine the seq with `:or`/`:and`."+ [entity-types coll-pred-fn]+ (for [etype entity-types+ :let [model (entity-type->model etype)]+ :when model]+ [:and+ [:= :entity_type (name etype)]+ [:in :entity_id ^:allow-subquery {:select [:id]+ :from [(t2/table-name model)]+ :where (coll-pred-fn etype (if (= etype :collection) :id :collection_id))}]]))++(def eligible-collection-where+ "The WHERE defining a collection *subject* - the finalized content-diagnostics eligibility set, stated+ directly (not via `mi/exclude-internal-content-hsql`) so the scope is visible here and stable against+ platform changes to \"internal content\". Content-diagnostics analyzes real user content only, so a+ collection tree that structurally cannot hold it is out. Included: the default (nil), transforms, and+ tenant namespaces, library-metrics trees, and tenant-specific root collections. Excluded: the snippet+ and analytics namespaces, the Trash, instance-analytics, library, and library-data types, archived+ collections, and sample content. Personal collections ARE included (the scan is permission-agnostic;+ serve-time filters handle exclusion). The single definition of what a collection subject is, so no two+ checkers can ever scan divergent collection sets."+ [:and+ [:= :archived false]+ [:= :is_sample false]+ ;; namespace denylist: drop snippets + analytics (the audit tree); keep default/transforms/tenant.+ ;; NULL-safe - the nil arm keeps default-namespace collections the NOT-IN would otherwise drop.+ [:or [:= :namespace nil]+ [:not-in :namespace [(name collection/snippets-ns) "analytics"]]]+ ;; type denylist: Trash lives in the default namespace, so only this arm drops it; instance-analytics is+ ;; already dropped by the analytics-namespace arm, listed here too to mirror the spec. The library root+ ;; holds nothing but the two sub-roots and library-data is a Tables-only tree; library-metrics stays in.+ [:or [:= :type nil]+ [:not-in :type [collection/trash-collection-type+ collection/instance-analytics-collection-type+ collection/library-collection-type+ collection/library-data-collection-type]]]])++(defn eligible-container-clause+ "WHERE fragment keeping rows whose `collection-id-col` is an eligible *container*: the root (NULL) or a+ collection satisfying [[eligible-collection-where]]. Content inside an ineligible container (audit,+ sample) produces no item findings. Container-gating covers the kinds whose lifecycle follows their+ container's (card/dashboard/document - archiving a folder archives them); transforms run regardless of+ their folder's state, so transform findings are never container-gated."+ [collection-id-col]+ [:or+ [:= collection-id-col nil]+ [:in collection-id-col ^:allow-subquery {:select [:id]+ :from [(t2/table-name :model/Collection)]+ :where eligible-collection-where}]])++;;; ----------------------------- entity-type multimethod dispatch (shared) -----------------------------+;;; What the serve/scan multimethods dispatch on: a module-local `hierarchy` (keeping bare entity-type+;;; keywords out of the global one - the driver.impl pattern) and a per-type column registry, so the+;;; multimethods carry behavior, not column lists.++(def hierarchy+ "Dispatch hierarchy for the module's per-entity-type multimethods (module-local, mirroring+ `metabase.driver.impl/hierarchy`). card/dashboard/document derive `::collection-item` and share one method+ each (collection-gated read, no owner, column-resident display fields, archivable); transform and+ collection diverge and carry explicit methods (transform has an owner, a non-collection-based read gate,+ and no archived column; collection is not *in* a collection but *is* one). Add a type by deriving it+ here or giving it its own methods - an unregistered type throws at dispatch."+ (-> (make-hierarchy)+ (derive :card ::collection-item)+ (derive :dashboard ::collection-item)+ (derive :document ::collection-item)))++(def ^:private entity-spec+ "Per-entity-type column lists the serve/scan multimethods read, so column choices stay out of `defmethod`+ bodies. Per type: `:context` = extra display cols beyond `[:id :collection_id]`; `:peer` / `:candidate` =+ extra cols the duplicate-entity hydrate / duplicated checker select beyond `[:id :name]`. Only card carries+ the `:card_schema` its after-select hook requires; transform has only `:context` (its peer/candidate reads+ are explicit methods). `collection` is absent - it is not+ column-resident (its breadcrumb anchor is parsed from `location`), so it carries its own `entity-context`+ method rather than going through the shared column path; its peer/candidate reads are explicit methods too."+ ;; card :context has no :type - the finding's own card_type is served from the finding row;+ ;; :peer keeps :type (+ the :card_schema it forces) for live peer hydration.+ {:card {:context [:description :view_count :display]+ :peer [:view_count :type :display :card_schema]+ :candidate [:card_schema]}+ :dashboard {:context [:description :view_count]+ :peer [:view_count]+ :candidate []}+ :document {:context [:view_count]+ :peer [:view_count]+ :candidate []}+ :transform {:context [:description :owner_user_id :owner_email]}})++(defn context-cols+ "Extra display cols `entity-context` selects for `entity-type` beyond `[:id :collection_id]` (see+ `entity-spec`)."+ [entity-type]+ (get-in entity-spec [entity-type :context]))++(defn peer-select-cols+ "Extra cols the duplicate-entity hydrate selects for `entity-type` beyond `[:id :name]` (see+ `entity-spec`)."+ [entity-type]+ (get-in entity-spec [entity-type :peer]))++(defn candidate-cols+ "Extra cols the duplicated checker selects for `entity-type` beyond `[:id :name]` (see `entity-spec`)."+ [entity-type]+ (get-in entity-spec [entity-type :candidate]))++(defn entity-root-namespace+ "The namespace of the root a root-resident subject sits under (`collection-namespace` applies only to+ collection subjects). Shared by the scan's root-label stamping and the serve layer's root breadcrumb+ so the stored sort label and the served breadcrumb agree."+ [entity-type collection-namespace]+ (case entity-type+ :collection collection-namespace+ :transform collection/transforms-ns+ nil))++(defn remove-document-internal-card-findings-xf+ "Transducer dropping findings whose subject is a card a document owns (`report_card.document_id`+ non-NULL): the copy an embed makes, a card authored inside the document, a card an exploration Summary+ materializes. The platform already keeps these out of collection items and out of card search; findings+ follow suit.++ Only the subject is dropped, never `details` - a document's `slow` finding names these same cards in+ `slow_entity_ids`, so a wider filter would empty every document roll-up.++ `duplicated` excludes them from its candidate rows too: peers come from that row set, so a copy dropped+ only here leaves the card it copies flagged against a peer no one can see.++ The owned ids are read once, when the transducer is built, so the caller can fuse this into the pass it+ already makes over the findings."+ []+ ;; the whole owned set rather than the ids a finding mentions, so the filter stays stateless and+ ;; composable; `idx_repord_card_document_id` serves the predicate. The card after-select still runs per+ ;; row, but an :id-only projection short-circuits its schema-upgrade and metric-description branches+ (let [document-owned (cd.db/document-owned-card-ids)]+ (remove #(and (= (:entity-type %) :card) (contains? document-owned (:entity-id %))))))++(defn attach-entity-attrs+ "Stamp each finding with the denormalized display/sort/filter columns - `:entity-name`,+ `:entity-created-at`, `:entity-creator-id`, `:entity-creator-name`, `:entity-kind`,+ and (cards only) `:card-type` - batch-resolved from each entity's own model (F ≪ N: one query per+ entity-type over just the flagged ids, plus one `creator_id → common_name` lookup over the distinct+ creators). Values a checker has already set win (e.g. the stale checker's `:entity-name` from its own+ query), so this only fills what the checker left unset. Every covered model exposes `name`/`created_at`;+ `creator_id` is selected only where the model has it - collections have none (a personal collection's+ owner is NOT a creator proxy), so their creator columns stay NULL."+ [findings]+ (let [attrs-by-key (into {}+ (for [[entity-type findings-for-type] (group-by :entity-type findings)+ :let [model (entity-type->model entity-type)]+ :when model+ :let [cols (cond-> [:id :name :created_at]+ (not= entity-type :collection) (conj :creator_id)+ ;; :type makes the select "plausible" to the Card+ ;; after-select hook, which then requires :card_schema+ (= entity-type :card) (conj :type :card_schema))+ id->attrs (cd.db/entity-attrs-by-id+ model cols+ (into #{} (map :entity-id) findings-for-type))]+ [id attrs] id->attrs]+ [[entity-type id] attrs]))+ creator-id->name (if-let [ids (not-empty (into #{} (keep :creator_id) (vals attrs-by-key)))]+ (cd.db/user-names-by-id ids)+ {})]+ (mapv (fn [{:keys [entity-type entity-id] :as finding}]+ (let [{:keys [name created_at creator_id] card-type :type} (get attrs-by-key [entity-type entity-id])]+ (merge {:entity-name name+ :entity-created-at created_at+ :entity-creator-id creator_id+ :entity-creator-name (get creator-id->name creator_id)+ ;; report_card.type at scan time (nil for non-cards) - what `entity-types` filters+ ;; on when given a card sub-kind+ :card-type card-type+ ;; flat kind: card sub-kind for cards (fallback :card if entity vanished),+ ;; else entity-type - one column serves filter and sort+ :entity-kind (if (= entity-type :card) (or card-type :card) entity-type)}+ finding)))+ findings)))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/db.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/db.cljnew file mode 100644index 00000000000..bff6959fb79--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/db.clj@@ -0,0 +1,490 @@+(ns metabase-enterprise.content-diagnostics.db+ "Application database queries for the Content Diagnostics module. Every function here is a direct+ Toucan 2 call with no additional logic, so the rest of the module only touches `toucan2.core` for the+ model definition, hydration and transactions.++ Nothing module-internal is required here beyond the leaf `schema` vocabulary: `common` holds the+ entity-type → model mapping and the shared WHERE fragments, and its own queries route through this+ namespace, so callers pass the model and any prebuilt fragment in as arguments."+ (:require+ [medley.core :as m]+ [metabase-enterprise.content-diagnostics.schema :as cd.schema]+ [metabase.app-db.core :as mdb]+ [metabase.collections.models.collection :as collection]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.models.interface :as mi]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;; ------------------------------------------------ Findings -------------------------------------------------++(def ^:private imbalanced-details+ "What `sparse` and `crowded` freeze: the limit that was crossed and the unit it counts in. `empty`+ builds on this."+ [:map {:closed true}+ [:threshold :int]+ [:unit :string]])++(def ^:private finding-details+ "The scan-time evidence blob each finding type freezes into `content_diagnostics_finding.details`.+ Closed per type, so a checker can neither persist a key the serve layer never reads nor borrow one+ belonging to a different finding type."+ {:stale [:map {:closed true} [:threshold_days :int]]+ ;; a leaf (card, transform) freezes the threshold it crossed; a container roll-up (dashboard,+ ;; document) names its slow culprit cards instead - never both+ :slow [:or+ [:map {:closed true} [:threshold_ms :int]]+ [:map {:closed true} [:slow_entity_ids [:sequential ::lib.schema.id/card]]]]+ :duplicate_name [:map {:closed true}+ [:normalized_name :string]+ [:duplicate_entity_ids [:sequential ms/PositiveInt]]]+ ;; `as_of` is the empty card's last clean run - a temporal here, a string in the response, because it+ ;; round-trips through the JSON blob+ :empty (conj imbalanced-details [:as_of {:optional true} ms/TemporalInstant])+ :sparse imbalanced-details+ :crowded imbalanced-details})++(def ^:private finding-row-columns+ "The columns every finding writes, whatever its type. `detected_at` and `invalidated_at` are absent by+ design: the first takes the column default, the second is stamped later by invalidation."+ [[:scan_id :string]+ [:entity_type (into [:enum] cd.schema/entity-types)]+ [:entity_id ms/PositiveInt]+ [:scope_collection_id [:maybe ::lib.schema.id/collection]]+ [:last_active_at [:maybe ms/TemporalInstant]]+ [:duration_ms [:maybe :int]]+ [:content_count [:maybe :int]]+ [:duplicate_count [:maybe :int]]+ [:entity_name [:maybe :string]]+ [:entity_created_at [:maybe ms/TemporalInstant]]+ [:entity_creator_id [:maybe ::lib.schema.id/user]]+ [:entity_creator_name [:maybe :string]]+ [:card_type [:maybe :keyword]]+ [:entity_collection_name [:maybe :string]]+ [:entity_kind [:maybe :keyword]]])++(def ^:private FindingRow+ "One `content_diagnostics_finding` row as a scan writes it, dispatched on `finding_type` so every row+ is checked against its own type's `details`. A finding type with no arm fails here rather than+ persisting a blob nothing can read."+ (into [:multi {:dispatch :finding_type}]+ (map (fn [[finding-type details]]+ [finding-type (into [:map {:closed true}+ [:finding_type [:= finding-type]]+ [:details details]]+ finding-row-columns)]))+ finding-details))++(mu/defn insert-findings!+ "Insert one chunk of `content_diagnostics_finding` `rows`."+ [rows :- [:sequential FindingRow]]+ (t2/insert! :model/ContentDiagnosticsFinding rows))++(mu/defn invalidate-superseded!+ "Stamp `invalidated_at` on every still-active finding of `finding-types` left by a scan other than+ `scan-id`; returns the row count."+ [scan-id :- :string+ finding-types :- [:set :keyword]]+ (t2/update! :model/ContentDiagnosticsFinding+ {:scan_id [:not= scan-id]+ :finding_type [:in finding-types]+ :invalidated_at nil}+ {:invalidated_at (mi/now)}))++(mu/defn invalidate-for-entity!+ "Stamp `invalidated_at` on one entity's still-active findings; returns the row count."+ [entity-type :- :keyword+ entity-id :- ms/PositiveInt]+ (t2/update! :model/ContentDiagnosticsFinding+ {:entity_type entity-type+ :entity_id entity-id+ :invalidated_at nil}+ {:invalidated_at (mi/now)}))++(mu/defn invalidate-findings-where!+ "Stamp `invalidated_at` on every finding matching `where`; returns the row count."+ [where :- vector?]+ (t2/query-one {:update (t2/table-name :model/ContentDiagnosticsFinding)+ :set {:invalidated_at (mi/now)}+ :where where}))++(mu/defn active-finding-ids+ "The ids among `ids` of the still-active findings that also match `where`."+ [ids :- [:sequential ms/PositiveInt]+ where :- vector?]+ (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:and [:in :id ids] [:= :invalidated_at nil] where]}))++(mu/defn findings-page+ "One page of findings matching `where`, sorted by `order-by`; `limit`/`offset` may be nil for no+ restriction."+ [where :- vector?+ order-by :- vector?+ limit :- [:maybe ms/PositiveInt]+ offset :- [:maybe ms/IntGreaterThanOrEqualToZero]]+ (t2/select :model/ContentDiagnosticsFinding+ (m/assoc-some {:where where, :order-by order-by} :limit limit :offset offset)))++(mu/defn finding-count+ "The number of findings matching `where`."+ [where :- vector?]+ (t2/count :model/ContentDiagnosticsFinding {:where where}))++(mu/defn last-detected-at+ "The `detected_at` of the most recent finding overall (≈ the latest scan's time), or nil if there are+ none."+ []+ (t2/select-one-fn :detected_at :model/ContentDiagnosticsFinding {:order-by [[:detected_at :desc]]}))++(mu/defn any-findings?+ "Whether any finding exists, invalidated ones included."+ []+ (t2/exists? :model/ContentDiagnosticsFinding))++(defn- delete-batch-query+ "Honey SQL deleting up to `batch-size` findings invalidated before `cutoff` on `db-type`. MySQL rejects+ a subquery reading the table being deleted from (error 1093), so it gets `DELETE ... LIMIT` instead.+ Every app DB `mdb/spec` supports needs an arm here; `finding-test` fails when one is missing."+ [db-type cutoff batch-size]+ (let [table (t2/table-name :model/ContentDiagnosticsFinding)]+ (case db-type+ (:postgres :h2)+ {:delete-from table+ :where [:in :id ^:allow-subquery {:select [:id]+ :from [table]+ :where [:< :invalidated_at cutoff]+ :order-by [[:id :asc]]+ :limit batch-size}]}++ :mysql+ {:delete-from table+ :where [:< :invalidated_at cutoff]+ :order-by [[:id :asc]]+ :limit batch-size})))++(mu/defn delete-invalidated-batch!+ "Delete up to `batch-size` findings invalidated before `cutoff`; returns the row count."+ [cutoff :- ms/TemporalInstant+ batch-size :- ms/PositiveInt]+ (t2/query-one (delete-batch-query (mdb/db-type) cutoff batch-size)))++;;; ----------------------------------------------- Collections -----------------------------------------------++(mu/defn collection-ids+ "The ids of the Collections matching `where`."+ [where :- vector?]+ (t2/select-pks-set :model/Collection {:where where}))++(mu/defn collection-locations+ "The `(id, location)` rows of the Collections matching `where`."+ [where :- vector?]+ (t2/select [:model/Collection :id :location] {:where where}))++(mu/defn collections+ "The whole Collection rows matching `where` - the breadcrumb hydrate needs `:location`."+ [where :- vector?]+ (t2/select :model/Collection {:where where}))++(mu/defn collection-context-rows+ "The display context of the Collections with `collection-ids`: `description`, the `location` their+ breadcrumb anchor is parsed from, `personal_owner_id`, and the tree's `namespace`."+ [collection-ids :- [:set ::lib.schema.id/collection]]+ (t2/select [:model/Collection :id :description :location :personal_owner_id :namespace]+ :id [:in collection-ids]))++(mu/defn collection-names-by-id+ "`{id → name}` for the Collections with `collection-ids`."+ [collection-ids :- [:set ::lib.schema.id/collection]]+ (t2/select-pk->fn :name [:model/Collection :id :name] :id [:in collection-ids]))++(mu/defn collection-namespaces-by-id+ "`{id → namespace}` for the Collections with `collection-ids`."+ [collection-ids :- [:set ::lib.schema.id/collection]]+ (t2/select-pk->fn :namespace [:model/Collection :id :namespace] :id [:in collection-ids]))++(mu/defn collection-parent-ids-by-id+ "`{id → parent collection id}` for the Collections with `collection-ids`, parsed from `location`; nil+ for a collection at the root of its tree."+ [collection-ids :- [:set ::lib.schema.id/collection]]+ (t2/select-pk->fn (comp collection/location-path->parent-id :location)+ [:model/Collection :id :location]+ :id [:in collection-ids]))++;;; --------------------------------------------- Entities of a type ------------------------------------------++(mu/defn collection-ids-by-entity-id+ "`{id → collection_id}` for the rows of `model` with `ids`."+ [model :- :keyword+ ids :- [:set ms/PositiveInt]]+ (t2/select-pk->fn :collection_id [model :id :collection_id] :id [:in ids]))++(mu/defn entity-attrs-by-id+ "`{id → {:name :created_at :creator_id :type}}` for the rows of `model` with `ids`.+ `select-cols` is the whole projection - the subset of those columns the model actually has, `:id`,+ and whatever its after-select hook requires."+ [model :- :keyword+ select-cols :- [:maybe [:sequential :keyword]]+ ids :- [:set ms/PositiveInt]]+ (t2/select-pk->fn #(select-keys % [:name :created_at :creator_id :type])+ (into [model] select-cols)+ :id [:in ids]))++(mu/defn entity-context-rows+ "The `(id, collection_id)` rows of `model` with `ids`, plus the `extra-cols` its display context+ needs."+ [model :- :keyword+ extra-cols :- [:maybe [:sequential :keyword]]+ ids :- [:set ms/PositiveInt]]+ (t2/select (into [model :id :collection_id] extra-cols) :id [:in ids]))++(mu/defn entity-rows+ "The rows of `selectable` - a model, or a model with a column projection - with `ids`."+ [selectable :- [:or :keyword vector?]+ ids :- [:set ms/PositiveInt]]+ (t2/select selectable :id [:in ids]))++(mu/defn document-owned-card-ids+ "The ids of every Card a Document owns."+ []+ (t2/select-pks-set [:model/Card :id] {:where [:not= :document_id nil]}))++;;; -------------------------------------------------- Users --------------------------------------------------++(mu/defn user-names-by-id+ "`{id → common_name}` for the Users with `user-ids`."+ [user-ids :- [:set ::lib.schema.id/user]]+ ;; the projection is what `add-common-name` reads - a bare model select would fetch every user+ ;; column to derive one field+ (t2/select-pk->fn :common_name [:model/User :id :email :first_name :last_name]+ :id [:in user-ids]))++(mu/defn user-contacts-by-id+ "`{id → {:id :common_name :email}}` for the Users with `user-ids`."+ [user-ids :- [:set ::lib.schema.id/user]]+ (t2/select-pk->fn #(select-keys % [:id :common_name :email])+ [:model/User :id :email :first_name :last_name]+ :id [:in user-ids]))++;;; -------------------------------------------------- Cards --------------------------------------------------++(mu/defn name-rows+ "The `(id, name)` rows of `model` matching `where` (every row when nil), plus `extra-cols` - the+ projection a caller needs on top of the name, such as the `:card_schema` every Card select requires."+ [model :- :keyword+ extra-cols :- [:maybe [:sequential :keyword]]+ where :- [:maybe vector?]]+ (t2/select (into [model :id :name] extra-cols) (m/assoc-some {} :where where)))++(mu/defn card-summaries-by-id+ "`{id → {:id :name :entity_type :card_type :view_count}}` for the Cards matching `where` - the+ columns a `slow` roll-up's culprit list serves: `name`, the `type` enum driving the per-member+ link, and the live `view_count`. `:card_schema` is required on any Card select - its+ after-select schema-upgrade hook reads it."+ [where :- vector?]+ (t2/select-pk->fn (fn [c] {:id (:id c)+ :name (:name c)+ :entity_type :card+ :card_type (:type c)+ :view_count (:view_count c)})+ [:model/Card :id :name :type :view_count :card_schema]+ {:where where}))++(mu/defn collection-item-card-rows+ "The `(id, collection_id)` rows of the non-archived Cards that are direct collection items - a card+ owned by a dashboard or a document lives in that container, not in the collection - in the containers+ `eligible-clause` allows."+ [eligible-clause :- vector?]+ (t2/query {:select [:id :collection_id]+ :from [:report_card]+ :where [:and+ [:= :archived false]+ [:= :dashboard_id nil]+ [:= :document_id nil]+ eligible-clause]}))++(mu/defn cards-with-empty-latest-run+ "The `(card_id, started_at)` rows of the non-archived Cards in the containers `eligible-clause` allows+ whose latest *clean* run since `cutoff` returned zero rows. Clean means unparameterized, unsandboxed, not+ a cache hit and error-free; `parameterized` is matched strictly against `false`, so legacy rows predating+ these columns (NULL) fall out, and a NULL `is_sandboxed` is treated as not sandboxed.++ `cutoff` bounds the window the way `card-run-medians` does; without it the ranking covered every+ `query_execution` row ever recorded, which on Postgres means every monthly partition."+ [cutoff :- ms/TemporalInstant+ eligible-clause :- vector?]+ (t2/query {:select [:card_id :started_at]+ :from [[^:allow-subquery+ {:select [:qe.card_id :qe.started_at :qe.result_rows+ [[:over [[:row_number] ^:allow-subquery+ {:partition-by :qe.card_id+ :order-by [[:qe.started_at :desc]+ [:qe.id :desc]]}]]+ :rn]]+ :from [[:query_execution :qe]]+ :join [[:report_card :c] [:= :c.id :qe.card_id]]+ :where [:and+ [:= :c.archived false]+ [:>= :qe.started_at cutoff]+ [:= :qe.parameterized false]+ [:= [:coalesce :qe.is_sandboxed false] false]+ [:not= :qe.cache_hit true]+ [:= :qe.error nil]+ eligible-clause]}+ :ranked]]+ :where [:and [:= :rn 1] [:= :result_rows 0]]}))++(mu/defn card-run-medians+ "The `(card_id, median_ms)` rows of the non-archived Cards in the containers `eligible-clause` allows+ whose median `running_time` over their non-cache-hit executions since `cutoff` exceeds `threshold-ms`.+ The app DBs share no median aggregate (H2 has MEDIAN, Postgres percentile_cont, MySQL neither), so it+ is computed portably: rank each card's executions by `running_time`, keep the middle row (odd count)+ or middle two (even), and AVG them. `median_ms` comes back as a BigDecimal."+ [threshold-ms :- ms/PositiveInt+ cutoff :- ms/TemporalInstant+ eligible-clause :- vector?]+ (t2/query {:select [:card_id [[:avg :running_time] :median_ms]]+ :from [[^:allow-subquery+ {:select [:qe.card_id :qe.running_time+ [[:over [[:row_number] ^:allow-subquery+ {:partition-by :qe.card_id+ :order-by [[:qe.running_time :asc]]}]]+ :rn]+ [[:over [[:count :*] ^:allow-subquery {:partition-by :qe.card_id}]] :cnt]]+ :from [[:query_execution :qe]]+ :join [[:report_card :c] [:= :c.id :qe.card_id]]+ :where [:and+ [:not= :qe.running_time nil]+ [:not= :qe.cache_hit true]+ [:= :c.archived false]+ [:>= :qe.started_at cutoff]+ eligible-clause]}+ :ranked]]+ :where [:and+ [:>= :rn [:/ :cnt 2.0]]+ [:<= :rn [:+ [:/ :cnt 2.0] 1]]]+ :group-by [:card_id]+ :having [:> [:avg :running_time] threshold-ms]}))++;;; ------------------------------------------------ Dashboards -----------------------------------------------++(mu/defn active-dashboard-rows+ "The `(id, collection_id)` rows of the non-archived Dashboards in the containers `eligible-clause`+ allows."+ [eligible-clause :- vector?]+ (t2/query {:select [:id :collection_id]+ :from [:report_dashboard]+ :where [:and [:= :archived false] eligible-clause]}))++(mu/defn dashboard-dashcard-counts+ "The `(dashboard_id, cnt)` primary-dashcard count of every Dashboard that has one; no row means zero.+ A series card layers onto another dashcard without taking a layout slot of its own, so it is not+ counted."+ []+ (t2/query {:select [:dashboard_id [[:count :*] :cnt]]+ :from [:report_dashboardcard]+ :group-by [:dashboard_id]}))++(mu/defn dashboard-tab-dashcard-counts+ "The `(dashboard_id, dashboard_tab_id, cnt)` primary-dashcard counts, one row per tab."+ []+ (t2/query {:select [:dashboard_id :dashboard_tab_id [[:count :*] :cnt]]+ :from [:report_dashboardcard]+ :group-by [:dashboard_id :dashboard_tab_id]}))++(mu/defn dashboard-tab-counts+ "The `(dashboard_id, cnt)` tab count of every Dashboard that has tabs; no row means a tabless+ dashboard."+ []+ (t2/query {:select [:dashboard_id [[:count :*] :cnt]]+ :from [:dashboard_tab]+ :group-by [:dashboard_id]}))++(mu/defn dashboard-primary-card-pairs+ "The `(dashboard_id, card_id)` pairs where a Dashboard matching `eligible-clause` renders one of+ `card-ids` as a primary dashcard. A card on several tabs yields several rows."+ [eligible-clause :- vector?+ card-ids :- [:sequential ::lib.schema.id/card]]+ (t2/query {:select [[:dc.dashboard_id :dashboard_id] [:dc.card_id :card_id]]+ :from [[:report_dashboardcard :dc]]+ :join [[:report_dashboard :d] [:= :d.id :dc.dashboard_id]]+ :where [:and eligible-clause [:in :dc.card_id card-ids]]}))++(mu/defn dashboard-series-card-pairs+ "The `(dashboard_id, card_id)` pairs where a Dashboard matching `eligible-clause` renders one of+ `card-ids` as a combined-series card layered onto a dashcard."+ [eligible-clause :- vector?+ card-ids :- [:sequential ::lib.schema.id/card]]+ (t2/query {:select [[:dc.dashboard_id :dashboard_id] [:s.card_id :card_id]]+ :from [[:dashboardcard_series :s]]+ :join [[:report_dashboardcard :dc] [:= :dc.id :s.dashboardcard_id]+ [:report_dashboard :d] [:= :d.id :dc.dashboard_id]]+ :where [:and eligible-clause [:in :s.card_id card-ids]]}))++;;; ------------------------------------------------- Documents -----------------------------------------------++(mu/defn document-rows+ "The `(id, collection_id)` rows of the non-archived Documents in the containers `eligible-clause`+ allows - the light form, with no AST."+ [eligible-clause :- vector?]+ (t2/query {:select [:id :collection_id]+ :from [(t2/table-name :model/Document)]+ :where [:and [:= :archived false] eligible-clause]}))++(mu/defn active-document-rows+ "The non-archived Documents in the containers `eligible-clause` allows, with the `:document` AST the+ content verdicts parse."+ [eligible-clause :- vector?]+ (t2/select [:model/Document :id :collection_id :document :content_type]+ {:where [:and [:= :archived false] eligible-clause]}))++(mu/defn documents-of-content-type+ "The non-archived Documents of `content-type` in the containers `eligible-clause` allows, with the+ `:document` AST their embedded card ids are parsed from."+ [content-type :- :string+ eligible-clause :- vector?]+ (t2/select [:model/Document :id :document :content_type]+ {:where [:and+ [:= :archived false]+ [:= :content_type content-type]+ eligible-clause]}))++;;; ------------------------------------------------- Transforms ----------------------------------------------++(mu/defn transform-rows+ "The `(id, collection_id)` rows of every Transform - transforms are hard-deleted, so there is no+ `archived` column to filter on."+ []+ (t2/query {:select [:id :collection_id]+ :from [:transform]}))++(mu/defn transforms+ "The whole Transform rows matching `where` - `mi/can-read?` reads `:source`, so the caller needs them+ all."+ [where :- vector?]+ (t2/select :model/Transform {:where where}))++(mu/defn finished-transform-run-spans+ "The `(transform_id, start_time, end_time)` of each Transform's latest finished run - succeeded, failed+ or timed out, started at or after `cutoff`. Canceled runs are excluded: their duration measures when+ someone hit cancel, not the transform. Runs per transform are serialized+ (`idx_unique_active_transform_run` allows one active run at a time), so the MAX of each timestamp+ belongs to the same (latest) row - one grouped query, no fetch of the full run history."+ [cutoff :- ms/TemporalInstant]+ (t2/query {:select [:transform_id+ [[:max :start_time] :start_time]+ [[:max :end_time] :end_time]]+ :from [:transform_run]+ :where [:and+ [:in :status ["succeeded" "failed" "timeout"]]+ [:>= :start_time cutoff]]+ :group-by [:transform_id]}))++;;; ------------------------------------------------ Task history ---------------------------------------------++(mu/defn task-history-details+ "The `task_details` of every `task_history` row for `task-name`."+ [task-name :- :string]+ (t2/select-fn-vec :task_details [:model/TaskHistory :task_details] :task task-name))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/events.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/events.cljnew file mode 100644index 00000000000..ca022ee8ae9--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/events.clj@@ -0,0 +1,80 @@+(ns metabase-enterprise.content-diagnostics.events+ "Keep content-diagnostics findings consistent with entity lifecycle. When a flagged entity is archived+ (card/dashboard/document), a collection subtree is archived, or a transform is deleted, its active+ findings are invalidated so it drops out of the served set immediately instead of lingering until the+ next scan."+ (:require+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase.collections.models.collection :as collection]+ [metabase.events.core :as events]+ [metabase.premium-features.core :as premium-features]+ [metabase.util.log :as log]+ [methodical.core :as methodical]))++(defn- invalidate!+ "Run `invalidate-thunk`, logging a failure instead of rethrowing it - `publish-event!` would otherwise fail+ the archive that triggered it."+ [log-ctx invalidate-thunk]+ (try+ (invalidate-thunk)+ (catch Throwable e+ (log/error e "Failed to invalidate content-diagnostics findings" log-ctx))))++(defn- invalidate-entity!+ "Invalidate one entity's findings."+ [entity-type entity-id]+ (invalidate! {:entity-type entity-type :entity-id entity-id}+ #(finding/invalidate-for-entity! entity-type entity-id)))++(defn- invalidate-collection-subtree!+ "Invalidate the findings of a collection subtree (self + descendants)."+ [{collection-id :id :as collection}]+ (invalidate! {:collection-id collection-id}+ #(finding/invalidate-for-collection-subtree!+ (conj (collection/descendant-ids collection) collection-id))))++;; ### Cards - archived via `PUT /api/card/:id` (`:event/card-update` fires on every update)+(events/derive! ::card-archived :metabase/event)+(events/derive! :event/card-update ::card-archived)++(methodical/defmethod events/publish-event! ::card-archived+ [_ {:keys [object]}]+ (when (and (premium-features/has-feature? :content-diagnostics) (:archived object))+ (invalidate-entity! :card (:id object))))++;; ### Dashboards - archived via `PUT /api/dashboard/:id` (`:event/dashboard-update` fires on every update)+(events/derive! ::dashboard-archived :metabase/event)+(events/derive! :event/dashboard-update ::dashboard-archived)++(methodical/defmethod events/publish-event! ::dashboard-archived+ [_ {:keys [object]}]+ (when (and (premium-features/has-feature? :content-diagnostics) (:archived object))+ (invalidate-entity! :dashboard (:id object))))++;; ### Collections - archived via `PUT /api/collection/:id`. Archiving cascades to the whole subtree+;; without emitting per-descendant events, so invalidate the subtree (self + descendants) here.+(events/derive! ::collection-archived :metabase/event)+(events/derive! :event/collection-update ::collection-archived)++(methodical/defmethod events/publish-event! ::collection-archived+ [_ {:keys [object]}]+ (when (and (premium-features/has-feature? :content-diagnostics) (:archived object))+ (invalidate-collection-subtree! object)))++;; ### Documents - archiving publishes `:event/document-delete` (its own signal, not the after-update hook)+(events/derive! ::document-deleted :metabase/event)+(events/derive! :event/document-delete ::document-deleted)++(methodical/defmethod events/publish-event! ::document-deleted+ [_ {:keys [object]}]+ (when (premium-features/has-feature? :content-diagnostics)+ (invalidate-entity! :document (:id object))))++;; ### Transforms - not archivable; hard `DELETE /api/transform/:id` publishes `:event/transform-delete`+(events/derive! ::transform-deleted :metabase/event)+(events/derive! :event/transform-delete ::transform-deleted)++(methodical/defmethod events/publish-event! ::transform-deleted+ [_ {:keys [object]}]+ (when (premium-features/has-feature? :content-diagnostics)+ (invalidate-entity! :transform (:id object))))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/init.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/init.cljnew file mode 100644index 00000000000..cbe505d8e5d--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/init.clj@@ -0,0 +1,9 @@+(ns metabase-enterprise.content-diagnostics.init+ "Loader for the Content Diagnostics module - pulls in settings, models, and the scheduled tasks so+ their `defsetting` / model registrations / `task/init!` methods are registered at startup."+ (:require+ [metabase-enterprise.content-diagnostics.events]+ [metabase-enterprise.content-diagnostics.models.finding]+ [metabase-enterprise.content-diagnostics.settings]+ [metabase-enterprise.content-diagnostics.task.finding-trimmer]+ [metabase-enterprise.content-diagnostics.task.scan]))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/models/finding.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/models/finding.cljnew file mode 100644index 00000000000..52588ef8d5c--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/models/finding.clj@@ -0,0 +1,78 @@+(ns metabase-enterprise.content-diagnostics.models.finding+ "Toucan model for `content_diagnostics_finding` — a detected problem for one entity, from one scan run.+ Scan-snapshot, latest-wins; a stamped `invalidated_at` evicts a superseded/invalidated finding from the+ served set (NULL = active)."+ (:require+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.models.interface :as mi]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(methodical/defmethod t2/table-name :model/ContentDiagnosticsFinding [_model] :content_diagnostics_finding)++(doto :model/ContentDiagnosticsFinding+ (derive :metabase/model))++(t2/deftransforms :model/ContentDiagnosticsFinding+ {:entity_type mi/transform-keyword+ :finding_type mi/transform-keyword+ ;; nullable - card findings only+ :card_type mi/transform-keyword+ :entity_kind mi/transform-keyword+ :details mi/transform-json})++(def ^:private delete-batch-size+ "Rows per DELETE, so a backlog is cleared in short transactions rather than one long one."+ 1000)++(defn delete-invalidated-before!+ "Hard-delete every finding invalidated before `cutoff`, a batch at a time; returns the row count.+ Active findings need no guard - their `invalidated_at` is NULL, and NULL < cutoff is never true."+ [cutoff]+ (loop [deleted 0]+ (let [n (long (cd.db/delete-invalidated-batch! cutoff delete-batch-size))]+ (if (< n delete-batch-size)+ (+ deleted n)+ (recur (+ deleted n))))))++(defn invalidate-superseded!+ "Soft-invalidate (stamp `invalidated_at`, never delete) every still-active finding of `finding-types`+ from a *prior* scan. Entities the new scan re-flagged keep a newer active row; entities it no longer+ flags drop out of the active set. Filtering on `invalidated_at` NULL keeps this idempotent."+ [scan-id finding-types]+ (when (seq finding-types)+ (cd.db/invalidate-superseded! scan-id finding-types)))++(defn invalidate-for-entity!+ "Soft-invalidate every still-active finding for a single entity (`entity-type` + `entity-id`) - e.g. when+ the entity is archived or deleted through any path."+ [entity-type entity-id]+ (cd.db/invalidate-for-entity! entity-type entity-id))++(defn invalidate-for-collection-subtree!+ "Soft-invalidate the active findings of an archived collection subtree: the collections themselves, and the+ collection items currently in them (live `collection_id`, not the scan-time `scope_collection_id`).+ Archiving a collection does not archive its transforms, so their findings stay."+ [collection-ids]+ (when (seq collection-ids)+ (let [archived-types (conj (descendants common/hierarchy ::common/collection-item) :collection)]+ (cd.db/invalidate-findings-where!+ [:and+ [:= :invalidated_at nil]+ (into [:or] (common/entity-collection-clauses+ archived-types+ (fn [_etype coll-col] [:in coll-col collection-ids])))]))))++(defn invalidate-by-ids!+ "Soft-invalidate the still-active findings among `ids` that match `where`, in one transaction; returns+ the set of ids found active. Two racing calls can both report an id; the `invalidated_at` NULL guard+ still stamps it once."+ [ids where]+ (t2/with-transaction [_conn]+ (let [eligible (set (cd.db/active-finding-ids ids where))]+ (when (seq eligible)+ (cd.db/invalidate-findings-where! [:and+ [:in :id (vec eligible)]+ [:= :invalidated_at nil]]))+ eligible)))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/scan.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/scan.cljnew file mode 100644index 00000000000..fef623d9868--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/scan.clj@@ -0,0 +1,253 @@+(ns metabase-enterprise.content-diagnostics.scan+ "Content Diagnostics scan pipeline. A scan runs every registered *checker* instance-wide and writes+ one `scan_id` batch of findings. Orchestration only - the per-finding-type detection logic lives in+ `checkers/*`, the shared entity-type mapping + denormalization helper in `common`."+ (:require+ [medley.core :as m]+ [metabase-enterprise.content-diagnostics.checkers.duplicated :as duplicated]+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.crowded :as imbalanced.crowded]+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.empty :as imbalanced.empty]+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.sparse :as imbalanced.sparse]+ [metabase-enterprise.content-diagnostics.checkers.slow :as slow]+ [metabase-enterprise.content-diagnostics.checkers.stale :as stale]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase.analytics-interface.core :as analytics]+ [metabase.collections.models.collection :as collection]+ [metabase.premium-features.core :refer [defenterprise]]+ [metabase.tracing.core :as tracing]+ [metabase.util :as u]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++;;; ---------------------------------------------- checkers ----------------------------------------------+;;; A checker is a 0-arg fn returning a seq of finding maps:+;;; {:entity-type <kw> :entity-id <int> :finding-type <kw> :details <map>}++(def checkers+ "Ordered checker registry. Each entry **declares** the finding-types it owns, a `:name` (its+ `checker.<name>` stage label in the scan's metrics and spans), and a 0-arg `:run` fn+ returning finding maps. Declaring the types (rather than inferring them from a scan's output) is what+ lets post-scan invalidation know its supersession scope even when a scan emits **zero** rows - i.e. an+ all-clean scan still resolves the previous scan's findings."+ [{:name "stale" :finding-types #{:stale} :run stale/checker}+ {:name "slow" :finding-types #{:slow} :run slow/checker}+ {:name "duplicated" :finding-types #{:duplicate_name} :run duplicated/checker}+ ;; the imbalanced family: three independent checkers (one namespace each under checkers/imbalanced/)+ ;; with no cross-type precedence, so one entity can carry several of these finding types at once;+ ;; each declared type scopes its own supersession+ {:name "empty" :finding-types #{:empty} :run imbalanced.empty/checker}+ {:name "sparse" :finding-types #{:sparse} :run imbalanced.sparse/checker}+ {:name "crowded" :finding-types #{:crowded} :run imbalanced.crowded/checker}])++(defn covered-finding-types+ "The set of finding-types the registered checkers own - the supersession scope for post-scan invalidation."+ []+ (into #{} (mapcat :finding-types) checkers))++(defn- run-stage!+ "Run one scan stage `f` inside a `:tasks` span, adding its duration to `scan-stage-ms` on success. A throw+ is re-thrown wrapped with the stage name so `scan!` can label `scan-runs`; the stage counter is+ skipped. The try sits outside the span so the exception reaches clj-otel first and the span ends in error."+ [span-suffix stage f]+ (let [timer (u/start-timer)]+ (try+ (let [result (tracing/with-span :tasks (str "task.content-diagnostics-scan." span-suffix)+ {:content-diagnostics/stage stage}+ (f))]+ (analytics/inc! :metabase-content-diagnostics/scan-stage-ms {:stage stage} (u/since-ms timer))+ result)+ (catch Throwable t+ ;; keep the class in the message: `ex-message` is often nil, and the wrapper hides the original type+ (throw (ex-info (format "Content Diagnostics scan stage %s failed: %s: %s"+ stage (.getName (class t)) (ex-message t))+ {:content-diagnostics/stage stage}+ t))))))++(defn detect+ "Run every checker instance-wide - each as its own `checker.<name>` stage - and return a de-duplicated+ vector of finding maps. De-dupes on (entity-type, entity-id, finding-type): a checker or the stale query's+ one-to-many joins can emit the same finding twice, and no intra-scan duplicate may reach the DB. Also+ drops findings on document-owned cards, so every checker gets that rule for free."+ []+ (let [findings-per-checker+ (mapv (fn [{checker-name :name :keys [run]}]+ (run-stage! "checker" (str "checker." checker-name)+ ;; realize inside the stage, so a lazy checker's work and throws stay its own+ #(let [findings (vec (run))]+ ;; per-checker, and before both the dedupe and the document-owned drop - a+ ;; different grain from `inserted-finding-count`+ (tracing/add-span-attrs!+ :tasks+ {:content-diagnostics/checker-finding-count (count findings)})+ findings)))+ checkers)]+ (into []+ (comp cat+ (m/distinct-by (juxt :entity-type :entity-id :finding-type))+ (common/remove-document-internal-card-findings-xf))+ findings-per-checker)))++;;; ----------------------------------------------- scan ------------------------------------------------++(defn- scope-collection-id-lookup+ "Batched `{[entity-type entity-id] → collection_id}` for the findings' entities - **one** query per+ entity-type (over just the flagged entities, F ≪ N). Entity types whose model has no `collection_id`+ contribute nothing; an entity at the root maps to nil. A `:collection` subject has no `collection_id`+ column - \"where it lived when flagged\" is its **parent**, parsed from `location` (nil at root)."+ [findings]+ (into {}+ (for [[entity-type findings-for-type] (group-by :entity-type findings)+ :let [model (common/entity-type->model entity-type)]+ :when model+ :let [ids (set (map :entity-id findings-for-type))+ id->coll (if (= entity-type :collection)+ (cd.db/collection-parent-ids-by-id ids)+ (cd.db/collection-ids-by-entity-id model ids))]+ {:keys [entity-id]} findings-for-type]+ [[entity-type entity-id] (get id->coll entity-id)])))++(defn- attach-scope-collection-ids+ "Stamp each finding with `:scope-collection-id` - the collection the entity lived in at scan time."+ [findings]+ (let [lookup (scope-collection-id-lookup findings)]+ (mapv #(assoc % :scope-collection-id (get lookup [(:entity-type %) (:entity-id %)])) findings)))++(defn- attach-collection-names+ "Stamp each finding with `:entity-collection-name` - the scan-time name of its `:scope-collection-id`+ collection. Root-resident entities store their root's label (\"Our analytics\"; \"Transforms\" for a+ transform) so the sort places them under the label the UI shows. The label is realized in the site+ locale - no user locale is bound in the scan job."+ [findings]+ (let [ids (into #{} (keep :scope-collection-id) findings)+ id->name (when (seq ids)+ (cd.db/collection-names-by-id ids))+ ;; root-resident collection subjects sit under their own tree's root (GDGT-2921 admits+ ;; transforms/tenant-namespace collections as subjects), so look their namespace up+ root-colls (into #{}+ (comp (filter #(and (= (:entity-type %) :collection)+ (nil? (:scope-collection-id %))))+ (map :entity-id))+ findings)+ id->ns (when (seq root-colls)+ (cd.db/collection-namespaces-by-id root-colls))+ ;; str realizes the label NOW, in the job's (site) locale+ root-label (memoize (fn [collection-namespace]+ (str (:name (collection/root-collection-with-ui-details+ collection-namespace)))))]+ (mapv (fn [{:keys [scope-collection-id entity-type entity-id] :as finding}]+ (assoc finding :entity-collection-name+ (if scope-collection-id+ (get id->name scope-collection-id)+ (root-label (common/entity-root-namespace entity-type (get id->ns entity-id))))))+ findings)))++(def ^:private insert-batch-size+ "Rows per INSERT. Postgres caps a prepared statement at 65,535 bind parameters; at ~17 columns/row a+ single all-rows insert overflows past ~3.8k findings. 1000 keeps us well under (mirrors+ `mark-stale-batch-size` in the deps module)."+ 1000)++(defn- insert-findings!+ "Persist findings as independent **chunk-committed** transactions - each chunk is its own transaction,+ so completed chunks are durable even if a later chunk fails (no single all-rows transaction held open+ for the whole scan). Chunking also keeps each statement under the bind-parameter cap."+ [scan-id findings]+ (doseq [chunk (partition-all insert-batch-size findings)]+ (t2/with-transaction [_conn]+ (cd.db/insert-findings!+ (for [{:keys [entity-type entity-id finding-type details scope-collection-id+ last-active-at duration-ms content-count duplicate-count+ entity-name entity-created-at entity-creator-id entity-creator-name+ card-type entity-collection-name entity-kind]}+ chunk]+ {:scan_id scan-id+ :entity_type entity-type+ :entity_id entity-id+ :finding_type finding-type+ :scope_collection_id scope-collection-id+ :last_active_at last-active-at+ :duration_ms duration-ms+ :content_count content-count+ :duplicate_count duplicate-count+ :entity_name entity-name+ :entity_created_at entity-created-at+ :entity_creator_id entity-creator-id+ :entity_creator_name entity-creator-name+ :card_type card-type+ :entity_collection_name entity-collection-name+ :entity_kind entity-kind+ :details details})))))++(defn- count-persisted!+ "Bump `findings-persisted` per finding type - clamped to the registry's declared types, `unknown` otherwise."+ [findings]+ (let [known (covered-finding-types)]+ (doseq [[finding-type n] (frequencies (map :finding-type findings))]+ (analytics/inc! :metabase-content-diagnostics/findings-persisted+ {:finding-type (if (contains? known finding-type) (name finding-type) "unknown")}+ n))))++(defn- run-pipeline!+ "The scan's write path - detect → enrich → insert → count → supersede - returning the persisted findings.+ `detect` runs its own per-checker stages, so nesting it under `enrich` would fold every checker's time+ into that stage. `count-persisted!` sits outside any stage, so a throw from it is attributed to `unknown`."+ [scan-id]+ (let [detected (detect)+ findings (run-stage! "enrich" "enrich"+ #(attach-collection-names (attach-scope-collection-ids detected)))]+ (run-stage! "insert" "insert"+ ;; attribute first - `add-span-attrs!` is fail-loud in dev/test, and throwing after the+ ;; chunks commit would count as an insert failure and skip supersession+ #(do (tracing/add-span-attrs!+ :tasks+ {:content-diagnostics/inserted-finding-count (count findings)})+ (insert-findings! scan-id findings)))+ ;; persisted - count them before supersession, which can still fail+ (count-persisted! findings)+ ;; write-side resolution: supersede prior-scan findings the new batch didn't re-emit. Only runs on+ ;; success - a failed scan never invalidates prior findings, though already-committed chunks of the+ ;; failed batch stay active alongside them.+ (run-stage! "invalidate" "invalidate"+ #(let [n (finding/invalidate-superseded! scan-id (covered-finding-types))]+ (tracing/add-span-attrs! :tasks {:content-diagnostics/superseded-count (or n 0)})))+ findings))++(defn scan!+ "Run a full scan synchronously: every checker → one `scan_id` batch → persisted. The persisted findings+ are the real result; returns the scan's topline `{:scan_id :finding_count :duration_ms}` for callers+ that report it (the demo `POST /scan`). Emits `scan-duration-ms` and `scan-runs` on both outcomes, the+ failing stage landing on `scan-runs`."+ []+ (let [timer (u/start-timer)+ scan-id (str (random-uuid))]+ (try+ ;; once, on the enclosing span - defjob's root, or the API request span for POST /scan - not per stage+ (tracing/add-span-attrs! :tasks {:content-diagnostics/scan-id scan-id})+ (let [findings (run-pipeline! scan-id)+ duration-ms (u/since-ms timer)+ topline {:scan_id scan-id+ :finding_count (count findings)+ ;; coerce to a primitive double so Math/round resolves without+ ;; reflection (u/since-ms is un-hinted)+ :duration_ms (Math/round (double duration-ms))}]+ (log/infof "Content Diagnostics scan %s: %d findings in %.0f ms" scan-id (count findings) duration-ms)+ ;; last, so a throw from anything above it reaches the catch and is counted once, as an error+ (analytics/inc! :metabase-content-diagnostics/scan-duration-ms {:status "ok"} duration-ms)+ (analytics/inc! :metabase-content-diagnostics/scan-runs {:status "ok" :stage "none"})+ topline)+ (catch Throwable t+ (analytics/inc! :metabase-content-diagnostics/scan-duration-ms {:status "error"} (u/since-ms timer))+ (analytics/inc! :metabase-content-diagnostics/scan-runs+ {:status "error" :stage (get (ex-data t) :content-diagnostics/stage "unknown")})+ (throw t)))))++(defenterprise run-content-diagnostics-scan!+ "EE implementation: runs a full scan synchronously and returns its topline. Only for the testing API —+ see [[metabase.testing-api.api]]."+ :feature :none+ []+ (scan!))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/schema.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/schema.cljnew file mode 100644index 00000000000..7f4bebc172e--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/schema.clj@@ -0,0 +1,10 @@+(ns metabase-enterprise.content-diagnostics.schema)++(def entity-types+ "Every entity type a finding can be about."+ #{:card :collection :dashboard :document :transform})++(def non-collection-entity-types+ "Every entity type but `:collection`: what the stale checker scans, and the `/stale` and `/slow`+ filter vocabulary."+ (disj entity-types :collection))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/settings.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/settings.cljnew file mode 100644index 00000000000..9dc23a01e0f--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/settings.clj@@ -0,0 +1,107 @@+(ns metabase-enterprise.content-diagnostics.settings+ (:require+ [metabase.settings.core :refer [defsetting]]+ [metabase.util.i18n :refer [deferred-tru]]))++(defsetting content-diagnostics-stale-threshold-days+ (deferred-tru "Content inactive beyond this many days is flagged stale by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 90+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-finding-retention-days+ (deferred-tru "Invalidated Content Diagnostics findings are deleted once they are this many days old.")+ :encryption :no+ :visibility :admin+ :default 30+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-empty-card-lookback-days+ (deferred-tru (str "A card is flagged empty by Content Diagnostics only if it returned no rows on a "+ "clean run within this many days."))+ :encryption :no+ :visibility :admin+ :default 90+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-slow-card-threshold-seconds+ (deferred-tru "Cards whose median query time exceeds this are flagged slow by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 15+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-slow-transform-threshold-seconds+ (deferred-tru "Transforms whose last run took longer than this are flagged slow by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 60+ :type :positive-integer+ :export? true+ :doc false)++;;; imbalanced (empty/sparse/crowded) thresholds - one setting per bound. `empty` has none (0 is+;;; naturally empty). crowded flags strictly more than its threshold, sparse strictly fewer.++(defsetting content-diagnostics-crowded-collection-threshold-items+ (deferred-tru "Collections holding more direct items than this are flagged crowded by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 100+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab+ (deferred-tru "Dashboards with more cards than this on any one tab are flagged crowded by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 20+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-crowded-dashboard-threshold-tabs+ (deferred-tru "Dashboards with more tabs than this are flagged crowded by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 5+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-crowded-document-threshold-cards+ (deferred-tru "Documents embedding more cards than this are flagged crowded by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 20+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-sparse-collection-threshold-items+ (deferred-tru "Non-empty collections holding fewer direct items than this are flagged sparse by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 5+ :type :positive-integer+ :export? true+ :doc false)++(defsetting content-diagnostics-sparse-dashboard-threshold-dashcards+ (deferred-tru "Non-empty dashboards with fewer total cards than this are flagged sparse by Content Diagnostics.")+ :encryption :no+ :visibility :admin+ :default 4+ :type :positive-integer+ :export? true+ :doc false)diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/finding_trimmer.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/finding_trimmer.cljnew file mode 100644index 00000000000..056a9bc5b57--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/finding_trimmer.clj@@ -0,0 +1,62 @@+(ns metabase-enterprise.content-diagnostics.task.finding-trimmer+ "Scheduled task to delete invalidated Content Diagnostics findings once they pass the retention+ window. Nothing else deletes them - the scan and the archive event handlers only stamp+ `invalidated_at` - so without this the table grows without bound."+ (:require+ [clojurewerkz.quartzite.jobs :as jobs]+ [clojurewerkz.quartzite.schedule.cron :as cron]+ [clojurewerkz.quartzite.triggers :as triggers]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase.task-history.core :as task-history]+ [metabase.task.core :as task]+ [metabase.util.log :as log])+ (:import+ (org.quartz DisallowConcurrentExecution)))++(set! *warn-on-reflection* true)++(def ^:private trimmer-job-key+ (jobs/key "metabase.task.content-diagnostics-finding-trimmer.job"))++(def ^:private trimmer-trigger-key+ (triggers/key "metabase.task.content-diagnostics-finding-trimmer.trigger"))++(defn- trim-old-findings!+ "Delete findings invalidated longer ago than the retention window. Runs whatever the token features+ are, unlike the scan job - a lapsed `:content-diagnostics` token would otherwise strand invalidated+ rows forever."+ []+ (let [cutoff (t/minus (t/offset-date-time)+ (t/days (long (cd.settings/content-diagnostics-finding-retention-days))))+ deleted (finding/delete-invalidated-before! cutoff)]+ (log/infof "Trimmed %d Content Diagnostics finding(s) invalidated before %s" deleted cutoff)))++(defn- trim-with-history!+ "Run [[trim-old-findings!]], recording the run in `task_history`."+ []+ (task-history/with-task-history {:task "content-diagnostics-trimmer"}+ (trim-old-findings!)))++(task/defjob ^{DisallowConcurrentExecution true+ :doc "Content Diagnostics - delete invalidated findings past retention."}+ ContentDiagnosticsFindingTrimmer [_ctx]+ (trim-with-history!))++(defmethod task/init! ::ContentDiagnosticsFindingTrimmer [_]+ (let [job (jobs/build+ (jobs/of-type ContentDiagnosticsFindingTrimmer)+ (jobs/store-durably)+ (jobs/with-identity trimmer-job-key)+ (jobs/with-description "Content Diagnostics finding trimmer"))+ trigger (triggers/build+ (triggers/with-identity trimmer-trigger-key)+ (triggers/for-job trimmer-job-key)+ (triggers/with-schedule+ (cron/schedule+ ;; 16:00, twelve hours off the scan's 04:00 - both jobs write to+ ;; content_diagnostics_finding, so they are deliberately kept apart+ (cron/cron-schedule "0 0 16 * * ? *")+ (cron/with-misfire-handling-instruction-fire-and-proceed))))]+ (task/schedule-task! job trigger)))diff --git a/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/scan.clj b/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/scan.cljnew file mode 100644index 00000000000..5b038a169d0--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/content_diagnostics/task/scan.clj@@ -0,0 +1,138 @@+(ns metabase-enterprise.content-diagnostics.task.scan+ "Quartz job that runs the Content Diagnostics scan: daily on a schedule (stored durably), and once shortly+ after startup on an instance that has never scanned, so an upgrade doesn't leave the findings empty until+ the first scheduled run."+ (:require+ [clojurewerkz.quartzite.jobs :as jobs]+ [clojurewerkz.quartzite.schedule.cron :as cron]+ [clojurewerkz.quartzite.schedule.simple :as simple]+ [clojurewerkz.quartzite.triggers :as triggers]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase.premium-features.core :as premium-features]+ [metabase.task-history.core :as task-history]+ [metabase.task.core :as task]+ [metabase.tracing.core :as tracing]+ [metabase.util.log :as log])+ (:import+ (java.time Instant)+ (java.util Date)+ (org.quartz DisallowConcurrentExecution JobExecutionContext ObjectAlreadyExistsException TriggerKey)))++(set! *warn-on-reflection* true)++(def scan-job-key+ "Quartz key for the Content Diagnostics scan job."+ (jobs/key "metabase.task.content-diagnostics-scan.job"))++(def ^:private scan-trigger-key+ (triggers/key "metabase.task.content-diagnostics-scan.trigger"))++(def ^:private scan-task-name+ "The `task_history` task name for every scan run, scheduled or backfill - the backfill relies on finding+ its own rows under it to stay a one-off."+ "content-diagnostics-scan")++(defn- scan-when-enabled!+ "Run the scan iff the `:content-diagnostics` premium feature is present, recording the firing in+ `task_history` either way - the job is scheduled on every EE instance regardless of token features."+ ([]+ (scan-when-enabled! :cron))+ ([run-method]+ (let [enabled? (premium-features/has-feature? :content-diagnostics)]+ (task-history/with-task-history+ {:task scan-task-name+ :task_details (cond-> {:run-method run-method}+ (not enabled?) (assoc :skipped-reason "content-diagnostics-disabled"))+ :on-success-info (fn [info result]+ (if-let [scan-id (:scan_id result)]+ (assoc-in info [:task_details :scan-id] scan-id)+ info))}+ (when enabled?+ (scan/scan!))))))++;;; ------------------------------------------------ Upgrade backfill -----------------------------------------++;; A second trigger on the scan job rather than a job of its own, so `DisallowConcurrentExecution` keeps the two+;; runs from overlapping - each run's supersession would invalidate the other's findings.+(def ^:private backfill-trigger-key+ (triggers/key "metabase.task.content-diagnostics-scan-backfill.trigger"))++(def ^:private backfill-startup-delay-seconds 60)++(defn- scanned-before?+ "Whether a real scan has run or is running.+ Firings skipped for the missing premium feature don't count, so an instance that ran the schedule unlicensed+ still qualifies for the backfill once licensed."+ []+ (boolean (some #(not (contains? % :skipped-reason))+ (cd.db/task-history-details scan-task-name))))++(defn- backfill-scan!+ "Scan only if the instance has never scanned and has the `:content-diagnostics` feature. A skip only logs, so it+ leaves no `task_history` row."+ []+ (cond+ ;; cheapest first: the feature check is usually cached, and `scanned-before?` reads every `task_history` row+ ;; for the scan+ (not (premium-features/has-feature? :content-diagnostics))+ (log/info "Skipping Content Diagnostics upgrade backfill: the :content-diagnostics feature is absent")++ (cd.db/any-findings?)+ (log/info "Skipping Content Diagnostics upgrade backfill: findings already exist")++ (scanned-before?)+ (log/info "Skipping Content Diagnostics upgrade backfill: a scan has already run")++ :else+ (scan-when-enabled! :upgrade-backfill)))++;;; ---------------------------------------------------- Job --------------------------------------------------++(defn- scan-for-trigger!+ [^TriggerKey trigger-key]+ (let [backfill? (= backfill-trigger-key trigger-key)+ ;; both triggers fire the one job, so its log context and span don't say which run this is+ run-method (if backfill? "upgrade-backfill" "cron")]+ (tracing/add-span-attrs! :tasks {:content-diagnostics/run-method run-method})+ (log/with-context {:content-diagnostics/run-method run-method}+ (if backfill?+ (backfill-scan!)+ (scan-when-enabled!)))))++(task/defjob ^{DisallowConcurrentExecution true+ :doc "Content Diagnostics - scan for problematic content."}+ ContentDiagnosticsScan [ctx]+ (scan-for-trigger! (.getKey (.getTrigger ^JobExecutionContext ctx))))++(defn- backfill-trigger []+ (triggers/build+ (triggers/with-identity backfill-trigger-key)+ (triggers/for-job scan-job-key)+ (triggers/start-at (Date/from (.plusSeconds (Instant/now) (long backfill-startup-delay-seconds))))+ (triggers/with-schedule+ (simple/schedule+ (simple/with-misfire-handling-instruction-fire-now)))))++(defmethod task/init! ::ContentDiagnosticsScan [_]+ (let [job (jobs/build+ (jobs/of-type ContentDiagnosticsScan)+ (jobs/store-durably)+ (jobs/with-identity scan-job-key)+ (jobs/with-description "Content Diagnostics scan"))+ trigger (triggers/build+ (triggers/with-identity scan-trigger-key)+ (triggers/for-job scan-job-key)+ (triggers/with-schedule+ (cron/schedule+ (cron/cron-schedule "0 0 4 * * ? *")+ (cron/with-misfire-handling-instruction-fire-and-proceed))))]+ (task/schedule-task! job trigger))+ ;; added on its own: `task/schedule-task!` on the existing job would replace the cron trigger with it. A pending+ ;; one is replaced, so in a rolling upgrade it fires 60s after the last node boots, once old nodes have usually+ ;; drained.+ (let [trigger (backfill-trigger)]+ (try+ (task/add-trigger! trigger)+ (catch ObjectAlreadyExistsException _+ (task/reschedule-trigger! trigger)))))diff --git a/enterprise/backend/src/metabase_enterprise/core/init.clj b/enterprise/backend/src/metabase_enterprise/core/init.cljindex 92ecf9e0d37..aa53d16f9c0 100644--- a/enterprise/backend/src/metabase_enterprise/core/init.clj+++ b/enterprise/backend/src/metabase_enterprise/core/init.clj@@ -12,6 +12,7 @@[metabase-enterprise.api-keys.init][metabase-enterprise.audit-app.init][metabase-enterprise.cache.init]+ [metabase-enterprise.content-diagnostics.init][metabase-enterprise.custom-viz-plugin.init][metabase-enterprise.data-apps.init][metabase-enterprise.data-complexity-score.init]diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex fc274e5dfba..f5f4659f3a9 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -91,6 +91,7 @@"Comment""CommentReaction""ConnectionImpersonation"+ "ContentDiagnosticsFinding""ContentTranslation""DashboardBookmark""DataAppGroupAssignment"diff --git a/enterprise/backend/src/metabase_enterprise/stale/db.clj b/enterprise/backend/src/metabase_enterprise/stale/db.cljindex 4402cba3b50..416f03a9020 100644--- a/enterprise/backend/src/metabase_enterprise/stale/db.clj+++ b/enterprise/backend/src/metabase_enterprise/stale/db.clj@@ -82,15 +82,16 @@[[^:allow-subquery {:union-all union-queries} :dummy_alias]])(mu/defn stale-content-rows- "A page of `:id`/`:model` rows from the union of `union-queries`, sorted by `sort-column` (`:name` or+ "A page of `select-columns` rows from the union of `union-queries`, sorted by `sort-column` (`:name` or`:last_used_at`) in `sort-direction`, skipping `offset` and returning up to `limit` (either may be nil for norestriction)."- [union-queries :- [:sequential ::staleness/query]- sort-column :- [:enum :name :last_used_at]+ [union-queries :- [:sequential ::staleness/query]+ select-columns :- [:sequential :keyword]+ sort-column :- [:enum :name :last_used_at]sort-direction :- [:enum :asc :desc]- limit :- [:maybe :int]- offset :- [:maybe :int]]- (t2/query (cond-> {:select [:id :model]+ limit :- [:maybe :int]+ offset :- [:maybe :int]]+ (t2/query (cond-> {:select select-columns:from (stale-content-union union-queries):order-by [[(case sort-column:name :%lower.namediff --git a/enterprise/backend/src/metabase_enterprise/stale/impl.clj b/enterprise/backend/src/metabase_enterprise/stale/impl.cljindex 2e2ca167274..11b0f13abe7 100644--- a/enterprise/backend/src/metabase_enterprise/stale/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/stale/impl.clj@@ -9,13 +9,19 @@(def ^:private FindStaleContentArgs[:map {:closed true}- [:collection-ids [:set {:doc "The set of collection IDs to search for stale content."} [:maybe :int]]]+ [:collection-ids [:or+ {:doc "Collection IDs to search: a set (a nil member means the root), or :all for instance-wide."}+ [:= :all]+ [:set [:maybe :int]]]][:cutoff-date [:time/local-date {:doc "The cutoff date for stale content."}]][:limit [:maybe {:doc "The limit for pagination."} :int]][:offset [:maybe {:doc "The offset for pagination."} :int]][:sort-column [:enum {:doc "The column to sort by."} :name :last_used_at]][:sort-direction [:enum {:doc "The direction to sort by."} :asc :desc]]- [:models {:optional true} [:set {:doc "The set of models to search for stale content."} :keyword]]])+ [:models {:optional true} [:set {:doc "The set of models to search for stale content."} :keyword]]+ [:include-columns {:optional true}+ [:set {:doc "Extra union columns to return on each row (rows carry only :id and :model by default)."}+ [:enum :name :last_used_at :collection_id]]]])(defn- queries [{:keys [models] :or {models #{:model/Card :model/Dashboard}} :as args}];; Ensure each model's namespace is loaded so its `find-stale-query` method is registered before we@@ -34,8 +40,8 @@Arguments are defined by [[FindStaleContentArgs]]:- - `collection-ids`: the set of collection IDs to look for stale content in. Non-recursive, the exact set you pass in- will be searched+ - `collection-ids`: the set of collection IDs to look for stale content in (a nil member means root-level+ content), or `:all` to search the whole instance. Non-recursive, the exact set you pass in will be searched- `cutoff-date`: if something was last accessed before this date, it is 'stale'@@ -45,18 +51,24 @@- `sort-direction`: `:asc` or `:desc`+ - `include-columns`: extra union columns (`:name`, `:last_used_at`, `:collection_id`) to return on each+ row; rows carry only `:id` and `:model` by default+Returns a map containing two keys,- `:rows` (a collection of maps containing an `:id` and `:model` field, like `{:id 1 :model :model/Card}`), and- `:total` (the total count of stale elements that could be found if you iterated through all pages)"- [{:keys [collection-ids limit offset sort-column sort-direction] :as args} :- FindStaleContentArgs]- (when (contains? collection-ids :root) (throw (ex-info "not implemented." {:collection-ids collection-ids})))+ [{:keys [collection-ids limit offset sort-column sort-direction include-columns] :as args} :- FindStaleContentArgs]+ (when (and (set? collection-ids) (contains? collection-ids :root))+ (throw (ex-info "not implemented." {:collection-ids collection-ids})))(let [union-queries (queries args)]{:rows (into [](comp- (map #(select-keys % [:id :model]))+ (map #(select-keys % (into [:id :model] include-columns)))(map (fn [v] (update v :model #(keyword "model" %)))))- (stale.db/stale-content-rows union-queries sort-column sort-direction limit offset))+ (stale.db/stale-content-rows union-queries+ (into [:id :model] (sort include-columns))+ sort-column sort-direction limit offset)):total (stale.db/stale-content-count union-queries)}))diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.cljindex 331de7c103d..1c37fb74feb 100644--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj@@ -71,6 +71,7 @@:database-routing:tenants:cloud-custom-smtp+ :content-diagnostics:writable-connection}(is (= {:admin_security_center false ;; requires self-hosted (non-cloud):advanced_permissions true@@ -128,6 +129,7 @@:database_routing true:tenants true:cloud_custom_smtp true+ :content_diagnostics true:etl_connections false:etl_connections_pg false:dependencies falsediff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/api_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/api_test.cljnew file mode 100644index 00000000000..1b41076e043--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/api_test.clj@@ -0,0 +1,169 @@+(ns metabase-enterprise.content-diagnostics.api-test+ "Who may invoke the content-diagnostics endpoints at all. The reads take the same union as the FE+ `canAccessContentDiagnostics` guard. What an authorized caller then sees is collection-filtered in+ `api.common` and covered by the per-finding-type suites."+ (:require+ [clojure.edn :as edn]+ [clojure.java.io :as io]+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.api :as cd.api]+ [metabase.permissions.core :as perms]+ [metabase.test :as mt]))++(def ^:private read-endpoints+ ["ee/content-diagnostics/stale"+ "ee/content-diagnostics/slow"+ "ee/content-diagnostics/imbalanced"+ "ee/content-diagnostics/duplicated"])++(defn- check-reads+ "Assert `status` on every finding-list endpoint - they share one gate, so they share one row of the+ matrix."+ [user status]+ (doseq [endpoint read-endpoints]+ (testing endpoint+ (mt/user-http-request user :get status endpoint))))++(def ^:private write-endpoints+ ["ee/content-diagnostics/invalidate"])++(defn- check-writes+ "Assert `status` on every POST endpoint. The body names an id no finding has, so a 200 changes nothing."+ [user status]+ (doseq [endpoint write-endpoints]+ (testing endpoint+ (mt/user-http-request user :post status endpoint {:ids [Integer/MAX_VALUE]}))))++(defn- routes-for+ [method]+ (set (map #(str "ee/content-diagnostics" %)+ (keep (fn [[m route]] (when (= method m) route))+ (keys (:api/endpoints (meta (the-ns 'metabase-enterprise.content-diagnostics.api))))))))++(deftest read-endpoints-are-all-covered-test+ (testing "every GET the namespace defines is exercised by this suite's matrix"+ ;; The gate is namespace-wide middleware, so a new endpoint is gated automatically - but it would go+ ;; uncovered here. Fail loudly instead, so whoever adds one classifies it.+ (is (= (routes-for :get)+ (set read-endpoints)))))++(deftest write-endpoints-are-all-covered-test+ (testing "every POST the namespace defines is exercised by this suite's matrix"+ (is (= (routes-for :post)+ (set write-endpoints)))))++(deftest writes-access-matrix-test+ (testing "POST writes take the same audience gate as the reads"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (testing "superuser"+ (check-writes :crowberto 200))+ (testing "plain authed user"+ (check-writes :rasta 403))+ (testing "unauthenticated"+ (doseq [endpoint write-endpoints]+ (testing endpoint+ (mt/client :post 401 endpoint {:ids [Integer/MAX_VALUE]}))))+ (testing "non-admin data analyst"+ (mt/with-data-analyst-role! (mt/user->id :rasta)+ (check-writes :rasta 200)))+ (testing "non-admin `:monitoring` grantee"+ (mt/with-user-in-groups [group {:name "Content Diagnostics Monitoring"}+ user [group]]+ (perms/grant-application-permissions! group :monitoring)+ (check-writes user 200))))+ (testing "an unlicensed instance answers 402"+ (mt/with-premium-features #{}+ (check-writes :rasta 402)))))++(deftest reads-allow-superuser-test+ (testing "GET reads serve a superuser"+ (mt/with-premium-features #{:content-diagnostics}+ (check-reads :crowberto 200))))++(deftest reads-reject-plain-user-test+ (testing "GET reads 403 a plain authed user - no analyst flag, no `:monitoring` grant"+ ;; The behavior change: before the gate these answered 200 with an empty-or-filtered list.+ (mt/with-premium-features #{:content-diagnostics}+ (check-reads :rasta 403))))++(deftest reads-reject-unauthenticated-test+ (testing "an unauthenticated request still gets 401, not the audience gate's 403"+ ;; Pins the middleware order in `api/routes`: `+auth` must stay outermost.+ (mt/with-premium-features #{:content-diagnostics}+ (doseq [endpoint read-endpoints]+ (testing endpoint+ (mt/client :get 401 endpoint))))))++(deftest reads-allow-data-analyst-test+ (testing "GET reads serve a non-admin data analyst"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-data-analyst-role! (mt/user->id :rasta)+ (check-reads :rasta 200)))))++(deftest reads-analyst-arm-needs-advanced-permissions-test+ (testing "without `:advanced-permissions` the role is not entitled, so a non-admin data analyst stays 403"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-data-analyst-role! (mt/user->id :rasta)+ (check-reads :rasta 403)))))++(deftest reads-allow-monitoring-grantee-test+ (testing "GET reads serve a non-admin holding the `:monitoring` application permission"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-user-in-groups [group {:name "Content Diagnostics Monitoring"}+ user [group]]+ (testing "before the grant"+ (check-reads user 403))+ (perms/grant-application-permissions! group :monitoring)+ (testing "after the grant"+ (check-reads user 200))))))++(deftest reads-monitoring-arm-needs-advanced-permissions-test+ (testing "without `:advanced-permissions` the `:monitoring` arm is the OSS stub, so a grantee stays 403"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-user-in-groups [group {:name "Content Diagnostics Monitoring"}+ user [group]]+ (perms/grant-application-permissions! group :monitoring)+ (check-reads user 403)))))++(deftest feature-gate-precedes-audience-gate-test+ (testing "an unlicensed instance still answers 402, not 403 - the mount's feature gate runs first"+ ;; Both gates would reject a plain user; the license answer has to stay the visible one, since it is+ ;; what tells an operator the feature is unavailable rather than the caller unauthorized.+ (mt/with-premium-features #{}+ (check-reads :rasta 402))))++(defn- remembered-sort-columns+ "The `sort_column` values the user-key-value schema accepts, read from the edn that declares them."+ []+ (->> (slurp (io/resource "user_key_value_types/content_diagnostics.edn"))+ edn/read-string+ (tree-seq coll? seq)+ ;; keyed on the :sort_column entry, not on one of the enum's own members - a second+ ;; [:enum … "name" …] added earlier in the file would otherwise silently redirect this+ (filter #(and (vector? %) (= :sort_column (first %))))+ first+ last+ rest+ set))++(deftest remembered-sort-column-covers-every-endpoint-test+ (testing "the user-key-value `sort_column` enum accepts exactly what the endpoints accept"+ ;; All four pages persist their params with `withSetLastUsedParams: true`, so a sort the endpoint+ ;; honours but the stored schema rejects works for the current visit and is then silently forgotten -+ ;; the PUT 400s and nothing surfaces it. `collection-name` and `finding-type` were both in that gap.+ (let [served (into #{} (comp (mapcat keys) (map name))+ [@#'cd.api/stale-sort-column->field+ @#'cd.api/slow-sort-column->field+ @#'cd.api/imbalanced-sort-column->field+ @#'cd.api/duplicated-sort-column->field])]+ (is (= served (remembered-sort-columns)))+ ;; the PUTs below write :rasta's real content_diagnostics/stale preference row in the shared app DB+ (mt/with-model-cleanup [:model/UserKeyValue]+ (testing "and each one really survives the PUT the pages make"+ (doseq [col (sort served)]+ (testing col+ (mt/user-http-request :rasta :put 200 "user-key-value/namespace/content_diagnostics/key/stale"+ {:value {:sort_column col}}))))+ (testing "a column no endpoint serves is still rejected"+ (mt/user-http-request :rasta :put 400 "user-key-value/namespace/content_diagnostics/key/stale"+ {:value {:sort_column "not-a-sortable-column"}}))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded_test.cljnew file mode 100644index 00000000000..9b3f3fad305--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/crowded_test.clj@@ -0,0 +1,155 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.crowded-test+ "The `crowded` imbalanced checker: too much content, across collection (raw direct item count),+ dashboard (dashcards-per-tab then tabs, a within-type precedence), and document (embedded cards).+ Asserts only `:crowded` findings; cross-type co-occurrence lives in the imbalanced integration suite."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase.collections.models.collection :as collection]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- crowded-by-entity!+ "Run a scan and index its `:crowded` findings by `[entity-type entity-id]` (the crowded checker emits+ at most one per entity, so the key is unique)."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (t2/select-fn->fn (juxt :entity_type :entity_id) identity+ :model/ContentDiagnosticsFinding+ :scan_id scan-id :finding_type :crowded)))++;;; ---------------------------------------------------- collections ----------------------------------------++(deftest crowded-collection-test+ (testing "collection crowded sits on the raw direct item count, strictly > the bound; a child collection counts as a direct item"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-crowded-collection-threshold-items 3]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; 3 cards + 1 child collection = 4 items > 3 → crowded+ :model/Collection {crowded-coll :id} {}+ :model/Card _ {:collection_id crowded-coll}+ :model/Card _ {:collection_id crowded-coll}+ :model/Card _ {:collection_id crowded-coll}+ :model/Collection {crowded-child :id} {:location (collection/location-path crowded-coll)}+ :model/Card _ {:collection_id crowded-child}+ ;; exactly 3 items → not crowded (strictly >)+ :model/Collection {at-coll :id} {}+ :model/Card _ {:collection_id at-coll}+ :model/Card _ {:collection_id at-coll}+ :model/Card _ {:collection_id at-coll}]+ (let [by-entity (crowded-by-entity!)]+ (testing "4 direct items (3 cards + a child collection) > 3 → crowded"+ (let [f (by-entity [:collection crowded-coll])]+ (is (some? f))+ (is (= :crowded (:finding_type f)))+ (is (= 4 (:content_count f)))+ (is (= {:threshold 3 :unit "items"} (:details f)))))+ (testing "exactly at the bound (3 items) → not crowded"+ (is (nil? (by-entity [:collection at-coll])))))))))))++;;; ----------------------------------------------------- dashboards -----------------------------------------++(deftest crowded-dashboard-test+ (testing "dashboard crowding checks dashcards-per-tab first, then tabs (one crowded finding max); a tabless dashboard is one implicit tab"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab 2+ content-diagnostics-crowded-dashboard-threshold-tabs 2]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; 3 dashcards on the single implicit tab (>2) → crowded by dashcards+ :model/Dashboard {slot-crowded :id} {:collection_id coll}+ :model/DashboardCard _ {:dashboard_id slot-crowded}+ :model/DashboardCard _ {:dashboard_id slot-crowded}+ :model/DashboardCard _ {:dashboard_id slot-crowded}+ ;; 3 tabs (>2), no tab over the dashcard bound → crowded by tabs+ :model/Dashboard {tab-crowded :id} {:collection_id coll}+ :model/DashboardTab {tc-t1 :id} {:dashboard_id tab-crowded}+ :model/DashboardTab {tc-t2 :id} {:dashboard_id tab-crowded}+ :model/DashboardTab {tc-t3 :id} {:dashboard_id tab-crowded}+ :model/DashboardCard _ {:dashboard_id tab-crowded :dashboard_tab_id tc-t1}+ :model/DashboardCard _ {:dashboard_id tab-crowded :dashboard_tab_id tc-t2}+ :model/DashboardCard _ {:dashboard_id tab-crowded :dashboard_tab_id tc-t3}+ ;; both violations - the dashcards-per-tab check wins (within-type precedence)+ :model/Dashboard {both :id} {:collection_id coll}+ :model/DashboardTab {b-t1 :id} {:dashboard_id both}+ :model/DashboardTab _ {:dashboard_id both}+ :model/DashboardTab _ {:dashboard_id both}+ :model/DashboardCard _ {:dashboard_id both :dashboard_tab_id b-t1}+ :model/DashboardCard _ {:dashboard_id both :dashboard_tab_id b-t1}+ :model/DashboardCard _ {:dashboard_id both :dashboard_tab_id b-t1}+ ;; tabless with 2 dashcards: one implicit tab (not 0 tabs), at every bound → not crowded+ :model/Dashboard {ok-tabless :id} {:collection_id coll}+ :model/DashboardCard _ {:dashboard_id ok-tabless}+ :model/DashboardCard _ {:dashboard_id ok-tabless}]+ (let [by-entity (crowded-by-entity!)]+ (testing "3 dashcards on one (implicit) tab > 2 → crowded, unit dashcards"+ (let [f (by-entity [:dashboard slot-crowded])]+ (is (some? f))+ (is (= 3 (:content_count f)))+ (is (= {:threshold 2 :unit "dashcards"} (:details f)))))+ (testing "3 tabs > 2 (no tab over the dashcard bound) → crowded, unit tabs"+ (let [f (by-entity [:dashboard tab-crowded])]+ (is (some? f))+ (is (= 3 (:content_count f)))+ (is (= {:threshold 2 :unit "tabs"} (:details f)))))+ (testing "both violations → ONE finding, the dashcards-per-tab one"+ (let [f (by-entity [:dashboard both])]+ (is (some? f))+ (is (= "dashcards" (get-in f [:details :unit])))))+ (testing "a tabless dashboard exactly at the bounds → not crowded (implicit tab is 1, not 0)"+ (is (nil? (by-entity [:dashboard ok-tabless])))))))))))++;;; ------------------------------------------------------ documents -----------------------------------------++(defn- doc-ast+ "A prose-mirror document AST wrapping `nodes`."+ [& nodes]+ {:type "doc" :content (vec nodes)})++(deftest crowded-document-test+ (testing "document crowded = embedded card count strictly > the bound"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-crowded-document-threshold-cards 2]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; 3 card embeds > 2 → crowded+ :model/Document {crowded-doc :id} {:collection_id coll+ :document (apply doc-ast+ (for [i [101 102 103]]+ {:type "cardEmbed" :attrs {:id i}}))}+ ;; 2 embeds → at the bound, not crowded+ :model/Document {at-doc :id} {:collection_id coll+ :document (apply doc-ast+ (for [i [104 105]]+ {:type "cardEmbed" :attrs {:id i}}))}]+ (let [by-entity (crowded-by-entity!)]+ (testing "3 embedded cards > 2 → crowded with the count"+ (let [f (by-entity [:document crowded-doc])]+ (is (some? f))+ (is (= 3 (:content_count f)))+ (is (= {:threshold 2 :unit "cards"} (:details f)))))+ (testing "exactly at the bound → not crowded"+ (is (nil? (by-entity [:document at-doc])))))))))))++;;; ------------------------------------------------- frozen thresholds --------------------------------------++(deftest crowded-threshold-frozen-at-scan-time-test+ (testing "the bound is read from the setting at scan time and frozen in details; a later change rewrites nothing"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card _ {:collection_id coll}+ :model/Card _ {:collection_id coll}+ :model/Card _ {:collection_id coll}]+ (mt/with-temporary-setting-values [content-diagnostics-crowded-collection-threshold-items 2]+ (scan/scan!))+ (testing "flagged against the non-default bound (2, not 100) - the setting is read, not hard-coded"+ (mt/with-temporary-setting-values [content-diagnostics-crowded-collection-threshold-items 50]+ (let [f (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :collection :entity_id coll :finding_type :crowded)]+ (is (some? f))+ (is (= 3 (:content_count f)))+ (is (= 2 (get-in f [:details :threshold])))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty_test.cljnew file mode 100644index 00000000000..1ba72b6e4a5--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/empty_test.clj@@ -0,0 +1,341 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.empty-test+ "The `empty` imbalanced checker: content with nothing in it, across collection (0 direct items),+ card (last clean-run signal), dashboard (0 dashcards), and document (no content, fail-closed).+ Transforms are never `empty`. Asserts only `:empty` findings; the cross-type co-occurrence that+ independent checkers now allow is covered in the imbalanced integration suite."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase.collections.models.collection :as collection]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- empty-by-entity!+ "Run a scan and index its `:empty` findings by `[entity-type entity-id]` (the empty checker emits at+ most one per entity, so the key is unique)."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (t2/select-fn->fn (juxt :entity_type :entity_id) identity+ :model/ContentDiagnosticsFinding+ :scan_id scan-id :finding_type :empty)))++;;; ---------------------------------------------------- collections ----------------------------------------++(deftest empty-collection-test+ (testing "a collection with no direct items is empty; archived members never count; a collection with an item is not"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {empty-coll :id} {}+ ;; only an archived card - archived members don't count, so it IS empty+ :model/Collection {arch-coll :id} {}+ :model/Card _ {:collection_id arch-coll :archived true}+ ;; one direct item - not empty+ :model/Collection {full-coll :id} {}+ :model/Card _ {:collection_id full-coll}]+ (let [by-entity (empty-by-entity!)]+ (testing "0 items → empty, content_count 0, implicit threshold 0 frozen with the unit"+ (let [f (by-entity [:collection empty-coll])]+ (is (some? f))+ (is (= 0 (:content_count f)))+ (is (= {:threshold 0 :unit "items"} (:details f)))))+ (testing "a collection holding only an archived card is empty"+ (is (some? (by-entity [:collection arch-coll]))))+ (testing "a collection holding a real item is not empty"+ (is (nil? (by-entity [:collection full-coll]))))))))))++(deftest empty-collection-direct-count-test+ (testing "collection emptiness is the direct-item count alone - item verdicts and depth never cascade"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; a chain whose only leaf is an empty dashboard - every level holds one direct item+ ;; (the child collection, or the dashboard), so no level is empty+ :model/Collection {gp :id} {}+ :model/Collection {p :id} {:location (collection/location-path gp)}+ :model/Collection {c :id} {:location (collection/location-path gp p)}+ :model/Dashboard {empty-dash :id} {:collection_id c}+ ;; the same chain shape ending in a card - likewise no level is empty+ :model/Collection {gp2 :id} {}+ :model/Collection {p2 :id} {:location (collection/location-path gp2)}+ :model/Collection {c2 :id} {:location (collection/location-path gp2 p2)}+ :model/Card _ {:collection_id c2}+ ;; 3 empty dashboards + 1 never-run card = 4 direct items - not empty+ :model/Collection {mixed :id} {}+ :model/Dashboard _ {:collection_id mixed}+ :model/Dashboard _ {:collection_id mixed}+ :model/Dashboard _ {:collection_id mixed}+ :model/Card _ {:collection_id mixed}]+ (let [by-entity (empty-by-entity!)]+ (testing "the empty dashboard is flagged, but no ancestor is - its emptiness never cascades"+ (is (some? (by-entity [:dashboard empty-dash])))+ (doseq [coll-id [gp p c]]+ (is (nil? (by-entity [:collection coll-id])) (str "collection " coll-id))))+ (testing "the card-ended chain is likewise empty-free at every level"+ (doseq [coll-id [gp2 p2 c2]]+ (is (nil? (by-entity [:collection coll-id])) (str "collection " coll-id))))+ (testing "empty items still count as direct items"+ (is (nil? (by-entity [:collection mixed]))))))))))++(deftest empty-excluded-collection-subjects-test+ (testing "trash, snippet-namespace, archived, and instance-analytics collections are never empty subjects"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {snippet-coll :id} {:namespace "snippets"}+ :model/Collection {archived-coll :id} {:archived true}+ :model/Collection {ia-coll :id} {:type "instance-analytics"}]+ (let [by-entity (empty-by-entity!)]+ (doseq [[label coll-id] {"snippet-namespace" snippet-coll+ "archived" archived-coll+ "instance-analytics" ia-coll+ "trash" (collection/trash-collection-id)}]+ (testing (str label " collection produces no finding")+ (is (nil? (by-entity [:collection coll-id])))))))))))++(deftest empty-item-container-scoping-test+ (testing "empty items inside ineligible containers produce no item findings; root-resident ones still do"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp+ [:model/Collection {sample-coll :id} {:is_sample true}+ :model/Collection {ia-coll :id} {:namespace "analytics"+ :type collection/instance-analytics-collection-type}+ ;; dashcard-less dashboards - empty on their own merits, but in ineligible containers+ :model/Dashboard {sample-dash :id} {:collection_id sample-coll}+ :model/Dashboard {ia-dash :id} {:collection_id ia-coll}+ ;; a sample-resident card whose latest clean run returned 0 rows - the card probe must skip it+ :model/Card {sample-card :id} {:collection_id sample-coll}+ :model/QueryExecution _ {:card_id sample-card :started_at now+ :parameterized false :cache_hit false :result_rows 0}+ ;; root-resident empty dashboard: the root is always an eligible container+ :model/Dashboard {root-dash :id} {}]+ (let [by-entity (empty-by-entity!)]+ (doseq [[label entity-key] {"sample-resident dashboard" [:dashboard sample-dash]+ "audit-resident dashboard" [:dashboard ia-dash]+ "sample-resident card" [:card sample-card]}]+ (is (nil? (by-entity entity-key)) label))+ (is (some? (by-entity [:dashboard root-dash]))))))))))++;;; ------------------------------------------------------- cards --------------------------------------------++(deftest empty-card-test+ (testing "card-empty rides the latest clean (unparameterized, unsandboxed, non-cache-hit, error-free) execution; other runs neither flag nor clear"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)+ ago #(t/minus now (t/minutes %))]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; flagged: deciding run (10m ago) returned 0 rows; the newer parameterized, cache-hit,+ ;; and errored runs are outside the evidence set - they don't clear it (and the errored+ ;; run's own result_rows 0 must not steal as_of)+ :model/Card {flagged :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id flagged :started_at (ago 10)+ :parameterized false :cache_hit false :result_rows 0}+ :model/QueryExecution _ {:card_id flagged :started_at (ago 5)+ :parameterized true :cache_hit false :result_rows 5}+ :model/QueryExecution _ {:card_id flagged :started_at (ago 2)+ :parameterized false :cache_hit false :result_rows 0+ :error "Table not found"}+ :model/QueryExecution _ {:card_id flagged :started_at (ago 1)+ :parameterized false :cache_hit true :result_rows 5}+ ;; healthy: the latest unparameterized run has rows; the older 0-row run is superseded+ :model/Card {healthy :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id healthy :started_at (ago 10)+ :parameterized false :cache_hit false :result_rows 0}+ :model/QueryExecution _ {:card_id healthy :started_at (ago 5)+ :parameterized false :cache_hit false :result_rows 7}+ ;; a newer PARAMETERIZED 0-row run must not flag (a filter yielding 0 rows on a healthy card)+ :model/Card {param-zero :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id param-zero :started_at (ago 10)+ :parameterized false :cache_hit false :result_rows 9}+ :model/QueryExecution _ {:card_id param-zero :started_at (ago 5)+ :parameterized true :cache_hit false :result_rows 0}+ ;; skipped: never ran unparameterized (unknown ≠ empty)+ :model/Card {never :id} {:collection_id coll}+ :model/Card {only-param :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id only-param :started_at (ago 5)+ :parameterized true :cache_hit false :result_rows 0}+ ;; skipped: a crashed run stamps result_rows 0 but means "broken", not "empty"+ :model/Card {only-errored :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id only-errored :started_at (ago 5)+ :parameterized false :cache_hit false :result_rows 0+ :error "Table not found"}+ ;; skipped: a sandboxed run's 0 rows is per-user filtering, not instance-wide emptiness+ :model/Card {only-sandboxed :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id only-sandboxed :started_at (ago 5)+ :parameterized false :cache_hit false :result_rows 0+ :is_sandboxed true}+ ;; excluded: archived card with a 0-row latest run+ :model/Card {archived-card :id} {:collection_id coll :archived true}+ :model/QueryExecution _ {:card_id archived-card :started_at (ago 5)+ :parameterized false :cache_hit false :result_rows 0}+ ;; a collection holding only a flagged-empty card: one direct item, so the collection is+ ;; not empty - the card's own finding is the signal+ :model/Collection {only-empty-card-coll :id} {}+ :model/Card {c0 :id} {:collection_id only-empty-card-coll}+ :model/QueryExecution _ {:card_id c0 :started_at (ago 5)+ :parameterized false :cache_hit false :result_rows 0}]+ (let [by-entity (empty-by-entity!)]+ (testing "flagged on the deciding run, as_of frozen to that run's start"+ (let [f (by-entity [:card flagged])]+ (is (some? f))+ (is (= 0 (:content_count f)))+ (is (= 0 (get-in f [:details :threshold])))+ (is (= "rows" (get-in f [:details :unit])))+ (is (= (-> (ago 10) t/instant (t/truncate-to :millis))+ (-> (get-in f [:details :as_of]) t/offset-date-time t/instant (t/truncate-to :millis))))))+ (testing "a newer unparameterized run with rows clears the older 0-row verdict"+ (is (nil? (by-entity [:card healthy]))))+ (testing "a newer parameterized 0-row run does not flag"+ (is (nil? (by-entity [:card param-zero]))))+ (testing "never run unparameterized → skipped (unknown, not empty)"+ (is (nil? (by-entity [:card never])))+ (is (nil? (by-entity [:card only-param]))))+ (testing "an errored run's result_rows 0 does not flag - errored runs are outside the evidence set"+ (is (nil? (by-entity [:card only-errored]))))+ (testing "a sandboxed run's result_rows 0 does not flag - sandboxed runs are outside the evidence set"+ (is (nil? (by-entity [:card only-sandboxed]))))+ (testing "archived card excluded even with a 0-row latest run"+ (is (nil? (by-entity [:card archived-card]))))+ (testing "a collection holding only a flagged-empty card is not empty - the card carries the finding"+ (is (some? (by-entity [:card c0])))+ (is (nil? (by-entity [:collection only-empty-card-coll])))))))))))++(deftest empty-card-lookback-window-test+ (testing "card-empty only considers clean runs inside `content-diagnostics-empty-card-lookback-days`"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [window (cd.settings/content-diagnostics-empty-card-lookback-days)+ now (t/offset-date-time)+ inside (t/minus now (t/days (dec window)))+ before (t/minus now (t/days (inc window)))]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; the only clean run is older than the window: no evidence about this card today, and a card+ ;; nobody has run in that long is already the `stale` checker's business+ :model/Card {stale-evidence :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id stale-evidence :started_at before+ :parameterized false :cache_hit false :result_rows 0}+ :model/Card {in-window :id} {:collection_id coll}+ :model/QueryExecution _ {:card_id in-window :started_at inside+ :parameterized false :cache_hit false :result_rows 0}]+ (let [by-entity (empty-by-entity!)]+ (testing "a clean 0-row run older than the window does not flag"+ (is (nil? (by-entity [:card stale-evidence]))))+ ;; the control: the cutoff is not over-filtering (and is the right way round)+ (testing "a clean 0-row run inside the window still flags"+ (is (some? (by-entity [:card in-window])))))+ (testing "the window is the setting, not a constant: narrowing it drops the in-window run too"+ (mt/with-temporary-setting-values [content-diagnostics-empty-card-lookback-days 1]+ (is (nil? (get (empty-by-entity!) [:card in-window])))))))))))++;;; ----------------------------------------------------- dashboards -----------------------------------------++(deftest empty-dashboard-test+ (testing "a dashboard with 0 dashcards is empty (a tabless one and an all-empty-tabs one both qualify); one with a dashcard is not"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ :model/Dashboard {tabless-empty :id} {:collection_id coll}+ ;; 3 tabs, none holding a dashcard → still 0 dashcards → empty+ :model/Dashboard {tabs-empty :id} {:collection_id coll}+ :model/DashboardTab _ {:dashboard_id tabs-empty}+ :model/DashboardTab _ {:dashboard_id tabs-empty}+ :model/DashboardTab _ {:dashboard_id tabs-empty}+ ;; has a dashcard → not empty+ :model/Dashboard {has-card :id} {:collection_id coll}+ :model/DashboardCard _ {:dashboard_id has-card}]+ (let [by-entity (empty-by-entity!)]+ (testing "tabless with 0 dashcards → empty, threshold 0 with the dashcards unit"+ (let [f (by-entity [:dashboard tabless-empty])]+ (is (some? f))+ (is (= 0 (:content_count f)))+ (is (= {:threshold 0 :unit "dashcards"} (:details f)))))+ (testing "tabs present but no dashcards → still empty"+ (is (some? (by-entity [:dashboard tabs-empty]))))+ (testing "a dashboard with a dashcard is not empty"+ (is (nil? (by-entity [:dashboard has-card]))))))))))++;;; ------------------------------------------------------ documents -----------------------------------------++(defn- doc-ast+ "A prose-mirror document AST wrapping `nodes`."+ [& nodes]+ {:type "doc" :content (vec nodes)})++(deftest empty-document-test+ (testing "document empty = no content of ANY kind (fail closed on unknown nodes; a reference label is content)"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ :model/Document {empty-doc :id} {:collection_id coll+ :document (doc-ast {:type "paragraph"})}+ :model/Document {whitespace-doc :id} {:collection_id coll+ :document (doc-ast {:type "paragraph"+ :content [{:type "text" :text " \n "}]})}+ ;; an unknown node type (no image node exists today) must read as content, not silently as empty+ :model/Document {image-doc :id} {:collection_id coll+ :document (doc-ast {:type "image" :attrs {:src "x.png"}})}+ ;; a reference node's label is content even with no text nodes+ :model/Document {link-doc :id} {:collection_id coll+ :document (doc-ast {:type "paragraph"+ :content [{:type "smartLink"+ :attrs {:label "Quarterly Report"}}]})}]+ (let [by-entity (empty-by-entity!)]+ (testing "a document with only structural nodes is empty"+ (let [f (by-entity [:document empty-doc])]+ (is (some? f))+ (is (= 0 (:content_count f)))+ (is (= {:threshold 0 :unit "cards"} (:details f)))))+ (testing "whitespace-only text is not content"+ (is (some? (by-entity [:document whitespace-doc]))))+ (testing "an image-only document is NOT empty (fail closed)"+ (is (nil? (by-entity [:document image-doc]))))+ (testing "a reference label is content"+ (is (nil? (by-entity [:document link-doc]))))))))))++;;; ----------------------------------------------------- transforms -----------------------------------------++(deftest empty-transform-folder-test+ (testing "transform folders count their transforms as direct items - only an item-less one reads empty"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; a folder whose only transform has never run (nil estimate) - one item, not empty+ :model/Collection {live-folder :id} {:namespace "transforms"}+ :model/Transform _ {:collection_id live-folder}+ ;; a folder holding only a 0-row transform - still one direct item, so not empty+ :model/Collection {dead-folder :id} {:namespace "transforms"}+ :model/Table {zero-table :id} {:estimated_row_count 0}+ :model/Transform _ {:collection_id dead-folder+ :target_table_id zero-table}+ ;; a transforms folder with nothing in it at all - empty+ :model/Collection {bare-folder :id} {:namespace "transforms"}]+ (let [by-entity (empty-by-entity!)]+ (testing "a folder holding a never-run transform is not empty"+ (is (nil? (by-entity [:collection live-folder]))))+ (testing "a folder holding a 0-row transform is not empty - the transform is still an item"+ (is (nil? (by-entity [:collection dead-folder]))))+ (testing "an item-less transforms folder is empty"+ (is (some? (by-entity [:collection bare-folder]))))))))))++(deftest transforms-are-never-empty-test+ (testing "a transform is never an `empty` subject, whatever its target table's row estimate says"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; a target table synced with a row estimate of 0 - the one signal a row-count check would read+ :model/Table {zero-table :id} {:estimated_row_count 0}+ :model/Transform {zero-rows :id} {:target_table_id zero-table}+ :model/Transform {never :id} {}]+ (let [by-entity (empty-by-entity!)]+ (is (nil? (by-entity [:transform zero-rows])))+ (is (nil? (by-entity [:transform never])))+ (testing "no transform reaches the empty findings at all"+ (is (empty? (filter (comp #{:transform} first) (keys by-entity)))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse_test.cljnew file mode 100644index 00000000000..2cd37f6d9d1--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/checkers/imbalanced/sparse_test.clj@@ -0,0 +1,115 @@+(ns metabase-enterprise.content-diagnostics.checkers.imbalanced.sparse-test+ "The `sparse` imbalanced checker: a little content, not none, across collection (raw direct item+ count) and dashboard (dashcards total across tabs). The rule floors at 1, so a zero-count subject is+ the `empty` checker's alone. Asserts only `:sparse` findings; cross-type co-occurrence lives in the+ imbalanced integration suite."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase.collections.models.collection :as collection]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- sparse-by-entity!+ "Run a scan and index its `:sparse` findings by `[entity-type entity-id]` (the sparse checker emits at+ most one per entity, so the key is unique)."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (t2/select-fn->fn (juxt :entity_type :entity_id) identity+ :model/ContentDiagnosticsFinding+ :scan_id scan-id :finding_type :sparse)))++;;; ---------------------------------------------------- collections ----------------------------------------++(deftest sparse-transform-folder-test+ (testing "transforms count as direct collection items - a folder with one transform is sparse"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-sparse-collection-threshold-items 3]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {folder :id} {:namespace "transforms"}+ :model/Transform _ {:collection_id folder}]+ (let [f (get (sparse-by-entity!) [:collection folder])]+ (is (some? f))+ (is (= 1 (:content_count f))))))))))++(deftest sparse-collection-test+ (testing "collection sparse sits on the raw direct item count, strictly < the bound and flooring at 1; a child collection counts as a direct item"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-sparse-collection-threshold-items 3]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; 0 items → not sparse (the empty checker's domain, sparse floors at 1)+ :model/Collection {zero-coll :id} {}+ ;; 2 cards < 3 → sparse+ :model/Collection {sparse-coll :id} {}+ :model/Card _ {:collection_id sparse-coll}+ :model/Card _ {:collection_id sparse-coll}+ ;; exactly 3 → not sparse (strictly <)+ :model/Collection {at-coll :id} {}+ :model/Card _ {:collection_id at-coll}+ :model/Card _ {:collection_id at-coll}+ :model/Card _ {:collection_id at-coll}+ ;; 1 card + 1 child collection = 2 items → sparse (child collections count as items)+ :model/Collection {child-counts :id} {}+ :model/Card _ {:collection_id child-counts}+ :model/Collection _ {:location (collection/location-path child-counts)}]+ (let [by-entity (sparse-by-entity!)]+ (testing "0 items → not sparse (floors at 1)"+ (is (nil? (by-entity [:collection zero-coll]))))+ (testing "2 items < 3 → sparse, raw count in content_count, bound + unit frozen"+ (let [f (by-entity [:collection sparse-coll])]+ (is (some? f))+ (is (= :sparse (:finding_type f)))+ (is (= 2 (:content_count f)))+ (is (= {:threshold 3 :unit "items"} (:details f)))))+ (testing "exactly at the bound (3 items) → not sparse"+ (is (nil? (by-entity [:collection at-coll]))))+ (testing "a child collection counts toward the item total (1 card + 1 child = 2 → sparse)"+ (let [f (by-entity [:collection child-counts])]+ (is (some? f))+ (is (= 2 (:content_count f))))))))))))++;;; ----------------------------------------------------- dashboards -----------------------------------------++(deftest sparse-dashboard-test+ (testing "dashboard sparse counts dashcards across ALL tabs (per-tab counting is crowded-only), strictly < the bound and flooring at 1"+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-temporary-setting-values [content-diagnostics-sparse-dashboard-threshold-dashcards 4]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; 3 dashcards total < 4 → sparse+ :model/Dashboard {sparse-dash :id} {:collection_id coll}+ :model/DashboardCard _ {:dashboard_id sparse-dash}+ :model/DashboardCard _ {:dashboard_id sparse-dash}+ :model/DashboardCard _ {:dashboard_id sparse-dash}+ ;; 3 dashcards on each of 2 tabs = 6 total → NOT sparse (count spans tabs, 6 not < 4)+ :model/Dashboard {two-tabs :id} {:collection_id coll}+ :model/DashboardTab {tt-t1 :id} {:dashboard_id two-tabs}+ :model/DashboardTab {tt-t2 :id} {:dashboard_id two-tabs}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t1}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t1}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t1}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t2}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t2}+ :model/DashboardCard _ {:dashboard_id two-tabs :dashboard_tab_id tt-t2}+ ;; exactly at the bound (4) → not sparse+ :model/Dashboard {at-dash :id} {:collection_id coll}+ :model/DashboardCard _ {:dashboard_id at-dash}+ :model/DashboardCard _ {:dashboard_id at-dash}+ :model/DashboardCard _ {:dashboard_id at-dash}+ :model/DashboardCard _ {:dashboard_id at-dash}+ ;; 0 dashcards → not sparse (floors at 1, the empty checker's domain)+ :model/Dashboard {empty-dash :id} {:collection_id coll}]+ (let [by-entity (sparse-by-entity!)]+ (testing "3 dashcards < 4 → sparse with the total as content_count"+ (let [f (by-entity [:dashboard sparse-dash])]+ (is (some? f))+ (is (= 3 (:content_count f)))+ (is (= {:threshold 4 :unit "dashcards"} (:details f)))))+ (testing "6 dashcards across 2 tabs is not sparse - the count spans tabs"+ (is (nil? (by-entity [:dashboard two-tabs]))))+ (testing "exactly 4 dashcards → not sparse (strictly <)"+ (is (nil? (by-entity [:dashboard at-dash]))))+ (testing "0 dashcards → not sparse (floors at 1)"+ (is (nil? (by-entity [:dashboard empty-dash])))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/common_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/common_test.cljnew file mode 100644index 00000000000..1e5d32cbe58--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/common_test.clj@@ -0,0 +1,298 @@+(ns metabase-enterprise.content-diagnostics.common-test+ "Direct contract tests for the shared module core both checkers and the read layer depend on: the+ collection-subject eligibility WHERE, the entity-type <-> model mapping and `attach-entity-attrs`, the+ scan-time denormalization helper. These pin the helper's semantics (batched per-type stamping,+ checker-set values win, missing entity is nil) independently of the full scan/serve pipeline that+ exercises them end to end, plus the module-wide fail-closed dispatch contract of the per-entity-type+ multimethods keyed off `common/hierarchy` and of the per-finding-type `finalize-finding` multimethod in+ the read layer."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.api.common :as api.common]+ [metabase-enterprise.content-diagnostics.checkers.duplicated :as checkers.duplicated]+ [metabase-enterprise.content-diagnostics.checkers.imbalanced.common :as imbalanced.common]+ [metabase-enterprise.content-diagnostics.common :as common]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.schema :as cd.schema]+ [metabase.collections.models.collection :as collection]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(deftest eligible-collection-where-contract-test+ (testing "the shared WHERE admits exactly the collection subjects the base principle allows"+ (mt/with-temp+ [:model/Collection {regular :id} {}+ ;; transforms stands in for every non-{snippet,analytics} namespace: the WHERE treats them+ ;; identically, so this also covers the tenant namespaces (which can't be temp-created here -+ ;; they need `perms/use-tenants`)+ :model/Collection {transforms-coll :id} {:namespace "transforms"}+ :model/Collection {lib-metrics :id} {:type collection/library-metrics-collection-type}+ :model/Collection {tenant-root :id} {:type collection/tenant-specific-root-collection-type}+ :model/Collection {sample :id} {:is_sample true}+ :model/Collection {lib-root :id} {:type collection/library-collection-type}+ :model/Collection {lib-data :id} {:type collection/library-data-collection-type}+ :model/Collection {ia :id} {:type collection/instance-analytics-collection-type}+ :model/Collection {snippets :id} {:namespace "snippets"}+ :model/Collection {archived :id} {:archived true}]+ (let [eligible (t2/select-pks-set :model/Collection {:where common/eligible-collection-where})]+ (testing "in: regular, non-internal namespaces, library-metrics (user metric cards), tenant root"+ (doseq [[label id] {"regular" regular+ "transforms-ns" transforms-coll+ "library-metrics" lib-metrics+ "tenant-root-type" tenant-root}]+ (is (contains? eligible id) label)))+ (testing "out: sample, library root (holds only the sub-roots), library-data (Tables-only),+ instance-analytics, snippets, archived, Trash"+ (doseq [[label id] {"sample" sample+ "library-root" lib-root+ "library-data" lib-data+ "instance-analytics" ia+ "snippets-ns" snippets+ "archived" archived+ "trash" (collection/trash-collection-id)}]+ (is (not (contains? eligible id)) label)))))))++(deftest imbalanced-subjects-match-the-shared-definition-test+ (testing "the imbalanced checkers scan exactly the shared collection-subject set"+ (mt/with-temp [:model/Collection {regular :id} {}+ :model/Collection {transforms-coll :id} {:namespace "transforms"}]+ (let [shared (t2/select-pks-set :model/Collection {:where common/eligible-collection-where})+ imbalanced (into #{} (map :id) (imbalanced.common/eligible-collections))]+ (is (= shared imbalanced))+ (is (contains? imbalanced regular))+ (is (contains? imbalanced transforms-coll))))))++(deftest entity-type->model-is-an-invertible-source-of-truth-test+ (testing "entity-type->model and model->entity-type are mutual inverses over every covered type"+ (doseq [[etype model] common/entity-type->model]+ (is (= etype (common/model->entity-type model)))+ (is (= model (common/entity-type->model etype)))))+ (testing "it covers exactly the shared entity-type vocabulary"+ (is (= cd.schema/entity-types (set (keys common/entity-type->model))))))++(deftest entity-type-hierarchy-and-registry-test+ (testing "card/dashboard/document derive ::collection-item; transform and collection are explicit outliers"+ (doseq [etype [:card :dashboard :document]]+ (is (isa? common/hierarchy etype ::common/collection-item)+ (str etype " should derive ::collection-item")))+ (is (not (isa? common/hierarchy :transform ::common/collection-item))+ "transform must not derive ::collection-item - it carries explicit methods")+ (is (not (isa? common/hierarchy :collection ::common/collection-item))+ "collection must not derive ::collection-item - it is a distinct, non-column-resident outlier"))+ (testing "the column registry covers every collection-resident type it feeds"+ (doseq [etype [:card :dashboard :document]]+ (is (vector? (common/context-cols etype)) (str etype " should have context-cols"))+ (is (vector? (common/peer-select-cols etype)))+ (is (vector? (common/candidate-cols etype))))+ (testing "transform is column-based for context only - its peer/candidate reads are bespoke methods"+ (is (vector? (common/context-cols :transform)))+ (is (nil? (common/peer-select-cols :transform)))+ (is (nil? (common/candidate-cols :transform))))+ (testing "collection has no context-cols entry - its breadcrumb anchor comes from location, not a column"+ (is (nil? (common/context-cols :collection))))))++(deftest entity-type-multimethods-are-fail-closed-test+ ;; The per-entity-type multimethods all dispatch through `common/hierarchy` with NO permissive :default, so+ ;; (1) every covered type must resolve a method - a new type left unregistered fails here, not in prod -+ ;; and (2) an unregistered type throws at dispatch rather than silently inheriting collection_id/owner+ ;; assumptions it violates.+ (let [mm-by-name {"read-entity-rows" @#'api.common/read-entity-rows+ "hydrate-owner" @#'api.common/hydrate-owner+ "entity-context" @#'api.common/entity-context+ "candidate-rows" @#'checkers.duplicated/candidate-rows}]+ (testing "every duplicated/serve non-collection entity-type resolves a method (registry completeness)"+ (doseq [[mm-name mm] mm-by-name+ etype cd.schema/non-collection-entity-types]+ (is (some? (get-method mm etype))+ (format "%s has no method for non-collection type %s" mm-name etype))))+ (testing ":collection resolves candidate-rows/read-entity-rows/entity-context,+ but is intentionally NOT a hydrate-owner subject (collections have no owner column)"+ (doseq [[mm-name mm] (dissoc mm-by-name "hydrate-owner")]+ (is (some? (get-method mm :collection))+ (format "%s must resolve :collection" mm-name)))+ (is (nil? (get-method @#'api.common/hydrate-owner :collection))+ "hydrate-owner must stay collection-free - collection context comes from entity-context"))+ (testing "an unregistered entity-type resolves no method (no catch-all :default)"+ (doseq [[mm-name mm] mm-by-name]+ (is (nil? (get-method mm :not-an-entity-type))+ (format "%s must stay fail-closed for unregistered types" mm-name))))+ (testing "invoking a multimethod with an unregistered entity-type throws at dispatch"+ (is (thrown? IllegalArgumentException (@#'api.common/hydrate-owner :not-an-entity-type [])))+ (is (thrown? IllegalArgumentException (@#'api.common/entity-context :not-an-entity-type #{})))+ (is (thrown? IllegalArgumentException (@#'api.common/read-entity-rows :not-an-entity-type #{} nil)))+ (is (thrown? IllegalArgumentException (@#'checkers.duplicated/candidate-rows :not-an-entity-type))))))++(deftest finding-type-multimethod-is-fail-closed-test+ ;; `finalize-finding` dispatches per row on the stored `finding_type` with NO permissive :default, so a+ ;; new finding type left unregistered fails here (and at dispatch), not by silently serving an unfinalized+ ;; row missing its native top-level column / details rewrite.+ (let [served-finding-types (scan/covered-finding-types)]+ (testing "every served finding-type resolves a method (registry completeness)"+ (doseq [ftype served-finding-types]+ (is (some? (get-method @#'api.common/finalize-finding ftype))+ (format "finalize-finding has no method for finding-type %s" ftype))))+ (testing "an unregistered finding-type resolves no method (no catch-all :default)"+ (is (nil? (get-method @#'api.common/finalize-finding :not-a-finding-type))))+ (testing "invoking a multimethod with an unregistered finding-type throws at dispatch"+ (is (thrown? IllegalArgumentException+ (@#'api.common/finalize-finding :not-a-finding-type {} {} {}))))))++(deftest hydrate-findings-tolerates-a-deleted-card-test+ (testing "a card finding whose entity is gone still hydrates - and keeps its stored card_type"+ ;; Not reachable over HTTP - `visible-findings-clause` already drops a finding whose entity row is gone -+ ;; so the hydrator's guard is pinned directly here. It still fires in the wild: a card can be deleted+ ;; between the findings query and the hydration query. card_type is denormalized on the finding row,+ ;; so unlike the live-hydrated fields (description/collection) it survives the entity's deletion.+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id :type :model}]+ (t2/delete! :model/Card :id card-id)+ (let [[row] (api.common/hydrate-findings [{:id 1+ :finding_type :stale+ :entity_type :card+ :entity_id card-id+ :card_type :model+ :details {}}]+ nil)]+ (is (some? row))+ (is (= :model (:card_type row)))+ (testing "the live-hydrated fields degrade to nil"+ (is (nil? (get-in row [:details :description])))+ (is (nil? (get-in row [:details :collection]))))))))++(deftest hydrate-findings-attaches-can-write-test+ (testing "can_write reflects whether the current user can trash the entity (curate its collection)"+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id}]+ (let [can-write? #(-> (api.common/hydrate-findings [{:id 1+ :finding_type :stale+ :entity_type :card+ :entity_id card-id+ :details {}}]+ nil)+ first+ :can_write)]+ (testing "an admin can"+ (mt/with-current-user (mt/user->id :crowberto)+ (is (true? (can-write?)))))+ (testing "a user without curate permission on its collection cannot"+ (mt/with-non-admin-groups-no-collection-perms coll-id+ (mt/with-current-user (mt/user->id :rasta)+ (is (false? (can-write?))))))))))++(deftest root-resident-collection-breadcrumb-names-its-own-tree-test+ (testing "a root-resident collection subject's root breadcrumb is labeled by its own namespace, not+ blanket \"Our analytics\" (duplicated spans transforms-ns collections)"+ (mt/with-temp [:model/Collection {transforms-coll :id} {:namespace "transforms"}+ :model/Collection {default-coll :id} {}]+ (let [crumb (fn [coll-id]+ (-> (api.common/hydrate-findings+ [{:id 1+ :finding_type :duplicate_name+ :entity_type :collection+ :entity_id coll-id+ :details {}}]+ nil)+ first+ (get-in [:details :collection])))]+ (testing "a transforms-namespace collection at its tree's root gets the Transforms root label"+ (let [{:keys [id name effective_ancestors]} (crumb transforms-coll)]+ (is (= "root" id))+ (is (= "Transforms" (str name)))+ (is (= [] effective_ancestors))))+ (testing "a default-namespace collection at the root keeps Our analytics"+ (let [{:keys [id name]} (crumb default-coll)]+ (is (= "root" id))+ (is (= "Our analytics" (str name)))))))))++(deftest attach-entity-attrs-stamps-denormalized-columns-test+ (testing "each finding is stamped with its entity's name/created_at/creator across multiple entity types"+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id+ :name "Revenue"+ :type :model+ :creator_id (mt/user->id :rasta)}+ :model/Dashboard {dash-id :id} {:collection_id coll-id+ :name "Ops"+ :creator_id (mt/user->id :crowberto)}]+ (let [by-key (into {}+ (map (juxt (juxt :entity-type :entity-id) identity))+ (common/attach-entity-attrs+ [{:entity-type :card :entity-id card-id :finding-type :slow}+ {:entity-type :dashboard :entity-id dash-id :finding-type :slow}]))]+ (testing "card: name + created_at + creator id and resolved common_name + card-type"+ (let [f (by-key [:card card-id])]+ (is (= "Revenue" (:entity-name f)))+ (is (some? (:entity-created-at f)))+ (is (= (mt/user->id :rasta) (:entity-creator-id f)))+ (is (= "Rasta Toucan" (:entity-creator-name f)))+ (is (= :model (:card-type f)))))+ (testing "dashboard: resolves its own creator, distinct from the card's; no card-type"+ (let [f (by-key [:dashboard dash-id])]+ (is (= "Ops" (:entity-name f)))+ (is (= (mt/user->id :crowberto) (:entity-creator-id f)))+ (is (= "Crowberto Corv" (:entity-creator-name f)))+ (is (nil? (:card-type f)))))))))++(deftest attach-entity-attrs-collection-has-no-creator-test+ (testing "a collection finding gets name/created_at but NULL creator columns - collections have no+ creator_id, and a personal collection's owner is not a creator proxy"+ (mt/with-temp [:model/Collection {coll-id :id} {:name "Team Reports"}]+ (let [[out] (common/attach-entity-attrs+ [{:entity-type :collection :entity-id coll-id :finding-type :empty}])]+ (is (= "Team Reports" (:entity-name out)))+ (is (some? (:entity-created-at out)))+ (is (nil? (:entity-creator-id out)))+ (is (nil? (:entity-creator-name out)))))))++(deftest attach-entity-attrs-lets-checker-set-values-win-test+ (testing "a value the checker already set (e.g. stale's scan-time entity-name) is not overwritten"+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id :name "Live Name"}]+ (let [[out] (common/attach-entity-attrs+ [{:entity-type :card :entity-id card-id :finding-type :stale+ :entity-name "Scan-Time Name"}])]+ (testing "the checker's entity-name wins over the live model name"+ (is (= "Scan-Time Name" (:entity-name out))))+ (testing "attrs the checker did not set are still filled from the model"+ (is (some? (:entity-created-at out))))))))++(deftest attach-entity-attrs-tolerates-a-missing-entity-test+ (testing "a finding whose entity no longer exists gets nil denormalized attrs, not an error"+ (let [[out] (common/attach-entity-attrs+ [{:entity-type :card :entity-id Integer/MAX_VALUE :finding-type :slow}])]+ (is (nil? (:entity-name out)))+ (is (nil? (:entity-created-at out)))+ (is (nil? (:entity-creator-id out)))+ (is (nil? (:entity-creator-name out)))+ (testing "a missing card gets :entity-kind :card (the fallback)"+ (is (= :card (:entity-kind out)))))))++(deftest attach-entity-attrs-stamps-entity-kind-test+ (testing "entity-kind = card_type for cards, entity-type otherwise"+ (mt/with-temp [:model/Card {model-id :id} {:type :model}+ :model/Card {q-id :id} {:type :question}+ :model/Dashboard {dash-id :id} {}]+ (is (=? [{:entity-kind :model}+ {:entity-kind :question}+ {:entity-kind :dashboard}]+ (common/attach-entity-attrs+ [{:entity-type :card :entity-id model-id}+ {:entity-type :card :entity-id q-id}+ {:entity-type :dashboard :entity-id dash-id}]))))))++(deftest remove-document-internal-card-findings-xf-test+ (testing "the transducer drops card subjects a document owns, and only card subjects"+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Document {doc-id :id} {:collection_id coll-id}+ :model/Card {owned :id} {:collection_id coll-id :document_id doc-id}+ :model/Card {live :id} {:collection_id coll-id}]+ (is (= [{:entity-type :card :entity-id live}+ ;; the entity-type guard: a non-card subject is kept even when its id collides with an+ ;; owned card's - entity ids are only unique within a type+ {:entity-type :document :entity-id owned}+ {:entity-type :dashboard :entity-id owned}]+ (into [] (common/remove-document-internal-card-findings-xf)+ [{:entity-type :card :entity-id owned}+ {:entity-type :card :entity-id live}+ {:entity-type :document :entity-id owned}+ {:entity-type :dashboard :entity-id owned}]))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/db_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/db_test.cljnew file mode 100644index 00000000000..a5cb4e43fd7--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/db_test.clj@@ -0,0 +1,62 @@+(ns metabase-enterprise.content-diagnostics.db-test+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- row+ "A minimal valid finding row of `finding-type`, carrying `details`. `entity-id` varies per row: the+ table's unique index spans (scan_id, entity_type, entity_id, finding_type), so two accepted rows of+ the same finding type would otherwise collide."+ [entity-id finding-type details]+ {:scan_id "db-test"+ :entity_type :card+ :entity_id entity-id+ :finding_type finding-type+ :scope_collection_id nil+ :last_active_at nil+ :duration_ms nil+ :content_count nil+ :duplicate_count nil+ :entity_name nil+ :entity_created_at nil+ :entity_creator_id nil+ :entity_creator_name nil+ :card_type nil+ :entity_collection_name nil+ :entity_kind nil+ :details details})++(deftest insert-findings-checks-details-against-the-finding-type-test+ (testing "`details` is validated against the row's own finding type, not the union of every type"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (testing "a leaf slow finding's own details are accepted"+ (is (some? (cd.db/insert-findings! [(row 1 :slow {:threshold_ms 15000})]))))+ (testing "so is the container roll-up's alternative shape"+ (is (some? (cd.db/insert-findings! [(row 2 :slow {:slow_entity_ids [2 3]})]))))+ (testing "but another finding type's details are rejected"+ (is (thrown? clojure.lang.ExceptionInfo+ (cd.db/insert-findings! [(row 3 :slow {:duplicate_entity_ids [2]})]))))+ (testing "and so is mixing two types' details in one blob"+ (is (thrown? clojure.lang.ExceptionInfo+ (cd.db/insert-findings! [(row 4 :slow {:threshold_ms 1 :threshold_days 30})]))))+ (testing "an unregistered finding type has no arm at all"+ (is (thrown? clojure.lang.ExceptionInfo+ (cd.db/insert-findings! [(row 5 :trash-not-emptied {:threshold_ms 1})])))))))++(deftest insert-findings-empty-as-of-test+ (testing "an empty card's `as_of` is written as a temporal and read back as an ISO-8601 string"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [as-of (t/offset-date-time)]+ (cd.db/insert-findings! [(row 1 :empty {:threshold 0 :unit "rows" :as_of as-of})])+ (let [stored (t2/select-one-fn :details :model/ContentDiagnosticsFinding+ :scan_id "db-test" :entity_id 1 :finding_type :empty)]+ (is (string? (:as_of stored)))+ (is (= (-> as-of t/instant (t/truncate-to :millis))+ (-> (:as_of stored) t/offset-date-time t/instant (t/truncate-to :millis)))))+ (testing "so a string `as_of`, as read back, is not a valid write"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid input"+ (cd.db/insert-findings! [(row 2 :empty {:threshold 0 :unit "rows"+ :as_of (str as-of)})]))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/duplicated_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/duplicated_test.cljnew file mode 100644index 00000000000..1ff3ed909e5--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/duplicated_test.clj@@ -0,0 +1,785 @@+(ns ^:synchronized metabase-enterprise.content-diagnostics.duplicated-test+ "The `duplicated` checker (match mode `name`) flags clusters of ≥2 same-type non-archived entities+ whose normalized names collide, stamping the peer count in the top-level `duplicate_count` column and+ freezing the `normalized_name`/`duplicate_entity_ids` envelope in `details` at scan time. The+ `/duplicated` endpoint serves them with hydrated same-type peers and the shared filter/sort/pagination+ envelope."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.test-util :as cd.tu]+ [metabase.collections.models.collection :as collection]+ [metabase.permissions.core :as perms]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(use-fixtures :each cd.tu/with-authorized-reader!)++(defn- scope-prefix+ "Unique per-test entity-name prefix - both the checker's name grouping and the API reads are scoped by+ it (the checker scans instance-wide and the findings table is shared across tests)."+ []+ (str "cd-" (mt/random-name)))++(defn- duplicated-findings-by-entity!+ "Run a scan and index its `:duplicate_name` findings by `[entity-type entity-id]`."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (into {}+ (map (juxt (juxt :entity_type :entity_id) identity))+ (t2/select :model/ContentDiagnosticsFinding :scan_id scan-id :finding_type :duplicate_name))))++;;; --------------------------------------------- checker --------------------------------------------------++(deftest duplicated-checker-flags-same-name-clusters-test+ (testing "same-type same-name pairs are flagged for all four covered types; a unique name is not"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id :name (str prefix " Revenue")}+ :model/Card {card-b :id} {:collection_id coll-id :name (str prefix " Revenue")}+ :model/Card {unique-c :id} {:collection_id coll-id :name (str prefix " One Of A Kind")}+ :model/Dashboard {dash-a :id} {:collection_id coll-id :name (str prefix " Ops")}+ :model/Dashboard {dash-b :id} {:collection_id coll-id :name (str prefix " Ops")}+ :model/Document {doc-a :id} {:collection_id coll-id :name (str prefix " Notes")}+ :model/Document {doc-b :id} {:collection_id coll-id :name (str prefix " Notes")}+ ;; transform has no archived column - every row participates+ :model/Transform {xf-a :id} {:name (str prefix " ETL")}+ :model/Transform {xf-b :id} {:name (str prefix " ETL")}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "card pair: peers are symmetric, duplicate_count 1, the details envelope is frozen"+ (let [f (by-entity [:card card-a])]+ (is (some? f))+ (is (= 1 (:duplicate_count f)))+ (is (= {:normalized_name (u/lower-case-en (str prefix " Revenue"))+ :duplicate_entity_ids [card-b]}+ (:details f)))+ (testing "the other magnitude columns stay null on duplicated findings"+ (is (nil? (:duration_ms f)))+ (is (nil? (:last_active_at f)))))+ (is (= [card-a] (get-in (by-entity [:card card-b]) [:details :duplicate_entity_ids]))))+ (testing "a unique name yields no finding"+ (is (nil? (by-entity [:card unique-c]))))+ (testing "dashboard, document, and transform pairs are each flagged with each other as peer"+ (doseq [[etype a b] [[:dashboard dash-a dash-b]+ [:document doc-a doc-b]+ [:transform xf-a xf-b]]]+ (let [f (by-entity [etype a])]+ (is (some? f))+ (is (= 1 (:duplicate_count f)))+ (is (= [b] (get-in f [:details :duplicate_entity_ids])))))))))))))++(deftest duplicated-checker-normalization-test+ (testing "names collide under lowercase + trim + internal-whitespace collapse; commas stay meaningful"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; case-insensitive+ :model/Card {case-a :id} {:collection_id coll-id :name (str prefix " Rev")}+ :model/Card {case-b :id} {:collection_id coll-id :name (str prefix " REV")}+ ;; leading/trailing whitespace+ :model/Card {pad-a :id} {:collection_id coll-id :name (str " " prefix " pad ")}+ :model/Card {pad-b :id} {:collection_id coll-id :name (str prefix " pad")}+ ;; internal whitespace collapses+ :model/Card {gap-a :id} {:collection_id coll-id :name (str prefix " a gap")}+ :model/Card {gap-b :id} {:collection_id coll-id :name (str prefix " a gap")}+ ;; a comma is NOT whitespace - these two must stay distinct+ :model/Card {comma-a :id} {:collection_id coll-id :name (str prefix " a, b")}+ :model/Card {comma-b :id} {:collection_id coll-id :name (str prefix " a b")}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "case-insensitive match; normalized_name is the normalized (lowercased) form"+ (let [f (by-entity [:card case-a])]+ (is (some? f))+ (is (= (u/lower-case-en (str prefix " Rev"))+ (get-in f [:details :normalized_name])))+ (is (= [case-b] (get-in f [:details :duplicate_entity_ids])))))+ (testing "leading/trailing whitespace is ignored"+ (is (= [pad-b] (get-in (by-entity [:card pad-a]) [:details :duplicate_entity_ids]))))+ (testing "internal whitespace runs collapse to one space"+ (is (= [gap-b] (get-in (by-entity [:card gap-a]) [:details :duplicate_entity_ids]))))+ (testing "a comma-separated name does not match its space-separated sibling"+ (is (nil? (by-entity [:card comma-a])))+ (is (nil? (by-entity [:card comma-b])))))))))))++(deftest duplicated-checker-unicode-normalization-test+ (testing "diacritics fold, Unicode whitespace collapses, and zero-width invisibles are stripped before comparison"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; combining-mark accents fold: Café Über ≡ Cafe Uber+ :model/Card {accent-a :id} {:collection_id coll-id :name (str prefix " Café Über")}+ :model/Card {accent-b :id} {:collection_id coll-id :name (str prefix " Cafe Uber")}+ ;; a non-breaking space (U+00A0) collapses like a regular space+ :model/Card {nbsp-a :id} {:collection_id coll-id :name (str prefix " a\u00A0gap")}+ :model/Card {nbsp-b :id} {:collection_id coll-id :name (str prefix " a gap")}+ ;; an em space (U+2003, another Unicode whitespace form) collapses too+ :model/Card {emsp-a :id} {:collection_id coll-id :name (str prefix " b\u2003gap")}+ :model/Card {emsp-b :id} {:collection_id coll-id :name (str prefix " b gap")}+ ;; a zero-width space (U+200B) is invisible - stripped, so the surrounding text joins+ :model/Card {zwsp-a :id} {:collection_id coll-id :name (str prefix " c\u200Bjoin")}+ :model/Card {zwsp-b :id} {:collection_id coll-id :name (str prefix " cjoin")}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "combining-mark accents fold (Café Über ≡ Cafe Uber)"+ (is (= [accent-b] (get-in (by-entity [:card accent-a]) [:details :duplicate_entity_ids]))))+ (testing "a non-breaking space collapses to a regular space"+ (is (= [nbsp-b] (get-in (by-entity [:card nbsp-a]) [:details :duplicate_entity_ids]))))+ (testing "an em space collapses to a regular space"+ (is (= [emsp-b] (get-in (by-entity [:card emsp-a]) [:details :duplicate_entity_ids]))))+ (testing "a zero-width space is stripped, joining the surrounding text"+ (is (= [zwsp-b] (get-in (by-entity [:card zwsp-a]) [:details :duplicate_entity_ids])))))))))))++(deftest duplicated-checker-clusters-cards-across-sub-kinds-test+ (testing "cards cluster by normalized name regardless of sub-kind: a question and a model sharing a name are duplicates"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Card {question :id} {:collection_id coll-id :name (str prefix " Sales") :type :question}+ :model/Card {model :id} {:collection_id coll-id :name (str prefix " Sales") :type :model}]+ (let [by-entity (duplicated-findings-by-entity!)+ question-finding (by-entity [:card question])+ model-finding (by-entity [:card model])]+ (testing "the question and the model are mutual peers, entity_type :card"+ (is (some? question-finding))+ (is (some? model-finding))+ (is (= :card (:entity_type question-finding)))+ (is (= [model] (get-in question-finding [:details :duplicate_entity_ids])))+ (is (= [question] (get-in model-finding [:details :duplicate_entity_ids])))))))))))++(deftest duplicated-checker-excludes-archived-test+ (testing "archived cards/dashboards/documents neither get findings nor count as peers"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; a pair whose second member is archived - no cluster survives+ :model/Card {pair-live :id} {:collection_id coll-id :name (str prefix " Pair")}+ :model/Card {pair-archived :id} {:collection_id coll-id :name (str prefix " Pair") :archived true}+ :model/Dashboard {dpair-live :id} {:collection_id coll-id :name (str prefix " DPair")}+ :model/Dashboard {dpair-archived :id} {:collection_id coll-id :name (str prefix " DPair") :archived true}+ :model/Document {doc-live :id} {:collection_id coll-id :name (str prefix " Doc")}+ :model/Document {doc-archived :id} {:collection_id coll-id :name (str prefix " Doc") :archived true}+ ;; a trio whose third member is archived - the two live ones still cluster, count 1 not 2+ :model/Card {trio-a :id} {:collection_id coll-id :name (str prefix " Trio")}+ :model/Card {trio-b :id} {:collection_id coll-id :name (str prefix " Trio")}+ :model/Card {trio-archived :id} {:collection_id coll-id :name (str prefix " Trio") :archived true}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "a live entity whose only name-twin is archived is not flagged"+ (is (nil? (by-entity [:card pair-live])))+ (is (nil? (by-entity [:dashboard dpair-live])))+ (is (nil? (by-entity [:document doc-live]))))+ (testing "archived entities are never flagged themselves"+ (is (nil? (by-entity [:card pair-archived])))+ (is (nil? (by-entity [:dashboard dpair-archived])))+ (is (nil? (by-entity [:document doc-archived]))))+ (testing "an archived member drops out of a surviving cluster's peer set"+ (let [f (by-entity [:card trio-a])]+ (is (some? f))+ (is (= 1 (:duplicate_count f)))+ (is (= [trio-b] (get-in f [:details :duplicate_entity_ids])))+ (is (nil? (by-entity [:card trio-archived]))))))))))))++(deftest duplicated-checker-excludes-document-internal-cards-test+ (testing "a card a document owns neither clusters nor leaves a peer behind on the card it copies"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Document {doc-id :id} {:collection_id coll-id :name (str prefix " Notes")}+ ;; embedding clones the card under the same name - the pair must not be a cluster+ :model/Card {pair-live :id} {:collection_id coll-id :name (str prefix " Pair")}+ :model/Card {pair-owned :id} {:collection_id coll-id :name (str prefix " Pair")+ :document_id doc-id}+ ;; a genuine twin still clusters - and the copy stays out of its peer set+ :model/Card {trio-a :id} {:collection_id coll-id :name (str prefix " Trio")}+ :model/Card {trio-b :id} {:collection_id coll-id :name (str prefix " Trio")}+ :model/Card {trio-owned :id} {:collection_id coll-id :name (str prefix " Trio")+ :document_id doc-id}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "the copy is never flagged itself"+ (is (nil? (by-entity [:card pair-owned])))+ (is (nil? (by-entity [:card trio-owned]))))+ (testing "a card whose only name-twin is a document's copy is not flagged"+ (is (nil? (by-entity [:card pair-live]))))+ (testing "the copy drops out of a surviving cluster's peer set"+ (let [f (by-entity [:card trio-a])]+ (is (some? f))+ (is (= 1 (:duplicate_count f)))+ (is (= [trio-b] (get-in f [:details :duplicate_entity_ids]))))))))))))++(deftest duplicated-checker-skips-blank-names-test+ (testing "whitespace-only names - including Unicode whitespace and zero-width invisibles - never cluster; unknown is not duplicate"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Dashboard {blank-a :id} {:collection_id coll-id :name " "}+ :model/Dashboard {blank-b :id} {:collection_id coll-id :name " "}+ ;; NBSP-only and zero-width-only names normalize to blank too, so they never cluster+ :model/Dashboard {nbsp-a :id} {:collection_id coll-id :name "\u00A0\u00A0"}+ :model/Dashboard {nbsp-b :id} {:collection_id coll-id :name "\u00A0\u00A0"}+ :model/Dashboard {zwsp-a :id} {:collection_id coll-id :name "\u200B"}+ :model/Dashboard {zwsp-b :id} {:collection_id coll-id :name "\u200B"}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (are [d] (nil? (by-entity [:dashboard d]))+ blank-a blank-b nbsp-a nbsp-b zwsp-a zwsp-b)))))))++(deftest duplicated-checker-cluster-of-three-test+ (testing "every member of a cluster of 3 gets duplicate_count 2 and the other two as peers"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Nightly Sync")]+ (mt/with-temp [:model/Transform {xf-a :id} {:name nm}+ :model/Transform {xf-b :id} {:name nm}+ :model/Transform {xf-c :id} {:name nm}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (doseq [[member peers] {xf-a #{xf-b xf-c}+ xf-b #{xf-a xf-c}+ xf-c #{xf-a xf-b}}]+ (let [f (by-entity [:transform member])]+ (is (some? f))+ (is (= 2 (:duplicate_count f)))+ (is (= peers (set (get-in f [:details :duplicate_entity_ids])))))))))))))++(deftest duplicated-checker-same-type-only-test+ (testing "a name shared across different entity types is not a duplicate"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Shared Name")]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id :name nm}+ :model/Dashboard {dash-id :id} {:collection_id coll-id :name nm}+ :model/Document {doc-id :id} {:collection_id coll-id :name nm}+ :model/Transform {xf-id :id} {:name nm}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (is (nil? (by-entity [:card card-id])))+ (is (nil? (by-entity [:dashboard dash-id])))+ (is (nil? (by-entity [:document doc-id])))+ (is (nil? (by-entity [:transform xf-id]))))))))))++(deftest duplicated-supersession-test+ (testing "renaming one of a pair to be unique supersedes both findings on the next scan"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Sales")]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id :name nm}+ :model/Card {card-b :id} {:collection_id coll-id :name nm}]+ (let [by-entity (duplicated-findings-by-entity!)+ fa (by-entity [:card card-a])+ fb (by-entity [:card card-b])]+ (is (some? fa))+ (is (some? fb))+ (t2/update! :model/Card card-b {:name (str prefix " Renamed Unique")})+ (scan/scan!)+ (testing "both prior findings are soft-invalidated and neither entity has an active one"+ (is (some? (:invalidated_at (t2/select-one :model/ContentDiagnosticsFinding :id (:id fa)))))+ (is (some? (:invalidated_at (t2/select-one :model/ContentDiagnosticsFinding :id (:id fb)))))+ (is (empty? (t2/select :model/ContentDiagnosticsFinding+ :finding_type :duplicate_name :invalidated_at nil+ :entity_type :card :entity_id [:in [card-a card-b]])))))))))))++;;; ------------------------------------------------- API --------------------------------------------------++(deftest duplicated-finding-type-test+ (testing "the `duplicated` umbrella stores and serves the `duplicate_name` finding type"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Sales Model")]+ (mt/with-temp+ [:model/Collection {coll-id :id} {:name "Analytics"}+ :model/Card {card-a :id} {:collection_id coll-id :name nm}+ :model/Card _ {:collection_id coll-id :name nm}]+ (scan/scan!)+ (testing "the checker writes `duplicate_name`"+ (is (= #{:duplicate_name}+ (t2/select-fn-set :finding_type :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id card-a))))+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/content-diagnostics/duplicated"+ :query prefix)]+ (is (= 2 (:total resp)))+ (is (= #{"duplicate_name"} (into #{} (map :finding_type) (:data resp)))))))))))++(deftest duplicated-api-hydration-test+ (testing "GET /duplicated serves findings with hydrated context + same-type peers"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Sales Model")]+ (mt/with-temp+ [:model/Collection {coll-id :id} {:name "Analytics"}+ :model/Card {card-a :id} {:collection_id coll-id :name nm :type :model+ :creator_id (mt/user->id :rasta) :view_count 4 :display :table}+ :model/Card {card-b :id} {:collection_id coll-id :name nm :type :model :view_count 7 :display :table}]+ (scan/scan!)+ ;; Visualization display is live context, not frozen with the scan verdict.+ (t2/update! :model/Card card-a {:display :bar})+ (t2/update! :model/Card card-b {:display :line})+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/content-diagnostics/duplicated"+ :query prefix)+ by-id (into {} (map (juxt (juxt :entity_type :entity_id) identity)) (:data resp))]+ (testing "the envelope carries last_scan_at + total"+ (is (contains? resp :last_scan_at))+ (is (= 2 (:total resp))))+ (let [f (by-id ["card" card-a])]+ (is (some? f))+ (is (= nm (:entity_display_name f)))+ (testing "the flagged card carries its own type as a top-level card_type"+ (is (= "model" (:card_type f))))+ (testing "the finding carries the current visualization display without rescanning"+ (is (= "bar" (:display f))))+ (testing "top-level duplicate_count + normalized_name in details"+ (is (= 1 (:duplicate_count f)))+ (is (= (u/lower-case-en nm) (get-in f [:details :normalized_name]))))+ (testing "the flagged entity's own live view_count is hydrated into details"+ (is (= 4 (get-in f [:details :view_count]))))+ (testing "the peer hydrates with its card sub-kind and live view_count"+ (is (= [{:id card-b :name nm :entity_type "card" :card_type "model" :display "line" :view_count 7}]+ (get-in f [:details :duplicate_entities]))))+ (testing "the raw stored peer ids are not served"+ (is (not (contains? (:details f) :duplicate_entity_ids))))+ (testing "shared display hydration: collection breadcrumb + denormalized creator"+ (is (= coll-id (get-in f [:details :collection :id])))+ (is (= (mt/user->id :rasta) (get-in f [:details :creator :id]))))))))))))++(deftest duplicated-api-subject-view-count-test+ (testing "GET /duplicated hydrates each finding's own live view_count into details; a transform omits it"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {card-id :id} {:collection_id coll-id :view_count 7}+ :model/Dashboard {dash-id :id} {:collection_id coll-id :view_count 12}+ :model/Document {doc-id :id} {:collection_id coll-id :view_count 3}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (let [prefix (scope-prefix)]+ (doseq [[etype eid] [[:card card-id] [:dashboard dash-id] [:document doc-id] [:transform xform-id]]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "vc"+ :entity_type etype+ :entity_id eid+ :entity_name (str prefix "-" (name etype))+ :finding_type :duplicate_name+ :duplicate_count 1+ :details {:normalized_name "x"+ :duplicate_entity_ids []}}))+ (let [by-type (into {} (map (juxt :entity_type identity))+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/duplicated" :query prefix)))]+ (testing "card/dashboard/document each carry their own live view_count in details"+ (are [etype vc] (= vc (get-in by-type [etype :details :view_count]))+ "card" 7+ "dashboard" 12+ "document" 3))+ (testing "a transform (no view_count column) omits the key entirely from details"+ (is (contains? by-type "transform"))+ (is (not (contains? (get-in by-type ["transform" :details]) :view_count))))+ (testing "view_count is hydrated live at read time, not frozen at scan time"+ (t2/update! :model/Card card-id {:view_count 99})+ (let [card-f (some #(when (= "card" (:entity_type %)) %)+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/duplicated" :query prefix)))]+ (is (= 99 (get-in card-f [:details :view_count]))))))))))))++(deftest duplicated-api-filter-and-sort-test+ (testing "GET /duplicated filters by entity-types/min-duplicate-count and sorts by duplicate-count/name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {card-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (perms/grant-collection-read-permissions! (perms/all-users-group) tcoll-id)+ (let [prefix (scope-prefix)+ insert (fn [etype eid nm dup-count]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "s"+ :entity_type etype+ :entity_id eid+ :entity_name (str prefix " " nm)+ :entity_kind etype+ :finding_type :duplicate_name+ :duplicate_count dup-count+ :details {:normalized_name nm+ :duplicate_entity_ids []}})))+ card-fid (insert :card card-id "Alpha" 1)+ dash-fid (insert :dashboard dash-id "Beta" 2)+ xform-fid (insert :transform xform-id "Gamma" 4)+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix kvs)))))+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix kvs))))]+ (testing "no filter → all three"+ (is (= #{card-fid dash-fid xform-fid} (ids))))+ (testing "entity-types (single + multi)"+ (is (= #{card-fid} (ids :entity-types "card")))+ (is (= #{card-fid dash-fid} (ids :entity-types ["card" "dashboard"]))))+ (testing "min-duplicate-count floor is inclusive - a pair (count 1) drops at min 2, a trio (count 2) stays"+ (is (= #{dash-fid xform-fid} (ids :min-duplicate-count "2")))+ (is (= #{xform-fid} (ids :min-duplicate-count "3"))))+ (testing "sort by duplicate-count, both directions"+ (is (= [card-fid dash-fid xform-fid] (order :sort-column "duplicate-count" :sort-direction "asc")))+ (is (= [xform-fid dash-fid card-fid] (order :sort-column "duplicate-count" :sort-direction "desc"))))+ (testing "sort by name (Alpha < Beta < Gamma), both directions"+ (is (= [card-fid dash-fid xform-fid] (order :sort-column "name" :sort-direction "asc")))+ (is (= [xform-fid dash-fid card-fid] (order :sort-column "name" :sort-direction "desc")))))))))))++(deftest duplicated-api-paginates-test+ (testing "GET /duplicated honors limit/offset and reports the full valid total"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c1 :id} {:collection_id coll-id}+ :model/Card {c2 :id} {:collection_id coll-id}+ :model/Card {c3 :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)]+ (doseq [cid [c1 c2 c3]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "p" :entity_type :card :entity_id cid+ :entity_name (str prefix "-" cid)+ :finding_type :duplicate_name :duplicate_count 1+ :details {:normalized_name "x" :duplicate_entity_ids []}}))+ (let [page (fn [limit offset]+ (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/duplicated"+ :query prefix :limit limit :offset offset))]+ (testing "limit caps the page; total reflects the full valid set; limit/offset echoed back"+ (let [r (page 2 0)]+ (is (= 2 (count (:data r))))+ (is (= 3 (:total r)))+ (is (= 2 (:limit r)))+ (is (= 0 (:offset r)))))+ (testing "offset advances to the remainder"+ (is (= 1 (count (:data (page 2 2))))))))))))))++(deftest duplicated-api-hides-unreadable-peers-test+ (testing "GET /duplicated omits peers the caller can't read - the count can exceed the hydrated list"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {readable :id} {}+ :model/Collection {unreadable :id} {}+ :model/Card {open-card :id} {:collection_id readable}+ :model/Card {secret-card :id} {:collection_id unreadable}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) readable)+ (let [prefix (scope-prefix)+ fid (first (t2/insert-returning-pks!+ :model/ContentDiagnosticsFinding+ {:scan_id "perm" :entity_type :card :entity_id open-card+ :entity_name (str prefix "-card")+ :finding_type :duplicate_name :duplicate_count 1+ :details {:normalized_name "x"+ :duplicate_entity_ids [secret-card]}}))+ finding (fn [user]+ (some #(when (= fid (:id %)) %)+ (:data (mt/user-http-request user :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix))))]+ (testing "superuser sees the peer"+ (is (= [secret-card] (mapv :id (get-in (finding :crowberto) [:details :duplicate_entities])))))+ (testing "non-admin gets an empty peer list while duplicate_count still reports 1"+ (let [f (finding :rasta)]+ (is (= [] (get-in f [:details :duplicate_entities])))+ (is (= 1 (:duplicate_count f))))))))))))++(deftest duplicated-api-personal-peers-follow-param-test+ (testing "GET /duplicated peer hydration honors include-personal-collections like the findings filter"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))]+ (mt/with-temp [:model/Collection {reg-id :id} {}+ :model/Card {flagged :id} {:collection_id reg-id}+ :model/Card {reg-peer :id} {:collection_id reg-id}+ :model/Card {pers-peer :id} {:collection_id pers-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) reg-id)+ (let [prefix (scope-prefix)+ fid (first (t2/insert-returning-pks!+ :model/ContentDiagnosticsFinding+ {:scan_id "pc" :entity_type :card :entity_id flagged+ :entity_name (str prefix "-card")+ :finding_type :duplicate_name :duplicate_count 2+ :details {:normalized_name "x"+ :duplicate_entity_ids [reg-peer pers-peer]}}))+ peer-ids (fn [& kvs]+ (let [finding (some #(when (= fid (:id %)) %)+ (:data (apply mt/user-http-request+ :rasta :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix kvs)))]+ (into #{} (map :id) (get-in finding [:details :duplicate_entities]))))]+ (testing "default → the caller's own (readable) personal-collection peer is hidden too"+ (is (= #{reg-peer} (peer-ids))))+ (testing "include-personal-collections=true → both peers hydrate"+ (is (= #{reg-peer pers-peer} (peer-ids :include-personal-collections true))))))))))))++(deftest duplicated-api-transform-peers-hydrate-test+ (testing "GET /duplicated gates transform findings on the analyst role and peers on transform+ readability - neither on collection visibility"+ ;; Enable transforms via premium features, not the `transforms-enabled` setting: with no `:hosting`+ ;; in scope, `with-temporary-setting-values` would restore an explicit global `false` that disables+ ;; transforms for every other test in a parallel run.+ ;; The non-analyst actor is a `:monitoring` grantee rather than the suite's usual `:rasta` - the+ ;; namespace fixture makes rasta an analyst, and the analyst flag is exactly what this test varies.+ ;; `:monitoring` clears the endpoint's audience gate without it, so peer readability stays the only+ ;; difference from the superuser row.+ (mt/with-premium-features #{:content-diagnostics :transforms-basic :hosting :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-user-in-groups [group {:name "Content Diagnostics Monitoring"}+ monitoring-user [group]]+ (perms/grant-application-permissions! group :monitoring)+ (let [prefix (scope-prefix)+ nm (str prefix " Nightly Sync")]+ (mt/with-temp [:model/Transform {xf-a :id} {:name nm}+ :model/Transform {xf-b :id} {:name nm}]+ (scan/scan!)+ (let [rows (fn [user]+ (:data (mt/user-http-request user :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix)))+ finding (fn [user]+ (some #(when (= [xf-a "transform"] [(:entity_id %) (:entity_type %)]) %)+ (rows user)))]+ (testing "superuser: the peer hydrates from the transform model - no card_type, no view_count"+ (is (= [{:id xf-b :name nm :entity_type "transform"}]+ (get-in (finding :crowberto) [:details :duplicate_entities]))))+ (testing "a non-data-analyst gets no transform finding at all - the list agrees with the peer gate"+ ;; mi/can-read? on a transform needs superuser, or an analyst who can read the source+ ;; tables; the collection clause alone would have served this row's name, description,+ ;; duration and owner email to a :monitoring grantee that GET /api/transform/:id 403s.+ (is (nil? (finding monitoring-user)))+ (is (= [] (filterv (comp #{"transform"} :entity_type) (rows monitoring-user)))))))))))))++(deftest duplicated-api-archived-folder-transform-peer-test+ (testing "GET /duplicated keeps transform findings and peers in archived folders - folder state is not a transform lifecycle state"+ (mt/with-premium-features #{:content-diagnostics :transforms-basic :hosting :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Nightly Sync")]+ (mt/with-temp [:model/Collection {archived-folder :id} {:namespace "transforms" :archived true}+ :model/Transform {xf-live :id} {:name nm}+ :model/Transform {xf-shelved :id} {:name nm :collection_id archived-folder}]+ (scan/scan!)+ (let [rows (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/duplicated" :query prefix))+ by-id (into {} (map (juxt :entity_id identity)) rows)]+ (testing "the live transform's finding lists the shelved one as a peer"+ (is (= [{:id xf-shelved :name nm :entity_type "transform"}]+ (get-in (by-id xf-live) [:details :duplicate_entities]))))+ (testing "the shelved transform's own finding is served, listing the live peer"+ (is (= [{:id xf-live :name nm :entity_type "transform"}]+ (get-in (by-id xf-shelved) [:details :duplicate_entities])))))))))))++(deftest duplicated-api-does-not-leak-across-finding-types-test+ (testing "a duplicated finding never surfaces in /stale or /slow, and theirs never surface in /duplicated"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {stale-card :id} {:collection_id coll-id}+ :model/Card {slow-card :id} {:collection_id coll-id}+ :model/Card {dup-card :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ stale-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id stale-card+ :entity_name (str prefix "-stale")+ :finding_type :stale :details {:threshold_days 90}}))+ slow-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id slow-card+ :entity_name (str prefix "-slow")+ :finding_type :slow :duration_ms 20000+ :details {:threshold_ms 15000}}))+ dup-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id dup-card+ :entity_name (str prefix "-dup")+ :finding_type :duplicate_name :duplicate_count 1+ :details {:normalized_name "x"+ :duplicate_entity_ids []}}))+ ids (fn [path] (set (map :id (:data (mt/user-http-request :rasta :get 200 path :query prefix)))))]+ (testing "/duplicated returns only the duplicated finding"+ (is (= #{dup-fid} (ids "ee/content-diagnostics/duplicated"))))+ (testing "/slow and /stale each exclude the duplicated finding"+ (is (= #{slow-fid} (ids "ee/content-diagnostics/slow")))+ (is (= #{stale-fid} (ids "ee/content-diagnostics/stale")))))))))))++(deftest duplicated-api-feature-gated-test+ (testing "GET /duplicated is gated on the :content-diagnostics premium feature"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (testing "licensed → 200 with the paginated envelope"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (let [resp (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/duplicated")]+ (is (contains? resp :data))+ (is (contains? resp :total)))))+ (testing "unlicensed → 402"+ (mt/with-premium-features #{}+ (mt/user-http-request :rasta :get 402 "ee/content-diagnostics/duplicated"))))))++;;; ------------------------------------------- collection subject ----------------------------------------++(deftest duplicated-checker-flags-collection-name-clusters-test+ (testing "same-named eligible collections cluster instance-wide; archived / snippet-namespace / is_sample ones sit out"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Reports")]+ (mt/with-temp [:model/Collection {coll-a :id} {:name nm}+ :model/Collection {coll-b :id} {:name nm}+ :model/Collection {solo :id} {:name (str prefix " Unique")}+ :model/Collection {archived :id} {:name nm :archived true}+ :model/Collection {snippet :id} {:name nm :namespace "snippets"}+ :model/Collection {sample :id} {:name nm :is_sample true}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "the two eligible same-named collections are symmetric peers, duplicate_count 1"+ (is (= 1 (:duplicate_count (by-entity [:collection coll-a]))))+ (is (= [coll-b] (get-in (by-entity [:collection coll-a]) [:details :duplicate_entity_ids])))+ (is (= [coll-a] (get-in (by-entity [:collection coll-b]) [:details :duplicate_entity_ids]))))+ (testing "a uniquely-named collection and the ineligible (archived / snippet-namespace / is_sample) ones get no finding"+ (is (nil? (by-entity [:collection solo])))+ (is (nil? (by-entity [:collection archived])))+ (is (nil? (by-entity [:collection snippet])))+ (is (nil? (by-entity [:collection sample]))))+ (testing "the ineligible collections do not count as peers either"+ (is (= [coll-b] (get-in (by-entity [:collection coll-a]) [:details :duplicate_entity_ids])))))))))))++(deftest duplicated-checker-includes-non-default-namespace-collections-test+ (testing "non-default-namespace collections ARE subjects (only the snippet/analytics namespaces sit out)"+ ;; Transforms stands in for every non-{snippet,analytics} namespace: `eligible-collection-where` treats+ ;; them identically, so this also covers the tenant namespace (which can't be temp-created here - it+ ;; needs `perms/use-tenants`).+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Pipeline")]+ (mt/with-temp [:model/Collection {xf-a :id} {:name nm :namespace "transforms"}+ :model/Collection {xf-b :id} {:name nm :namespace "transforms"}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "same-named transform-namespace collections cluster as symmetric peers"+ (is (= 1 (:duplicate_count (by-entity [:collection xf-a]))))+ (is (= [xf-b] (get-in (by-entity [:collection xf-a]) [:details :duplicate_entity_ids])))+ (is (= [xf-a] (get-in (by-entity [:collection xf-b]) [:details :duplicate_entity_ids])))))))))))++(deftest duplicated-checker-item-container-scoping-test+ (testing "collection items in ineligible containers are not candidates; root-resident items still are"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ ia-name (str prefix " Audit Log")+ root-name (str prefix " Root Report")]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Collection {ia-coll :id} {:namespace "analytics"+ :type collection/instance-analytics-collection-type}+ ;; an eligible card + an audit-resident same-name card: no cluster of 2 forms+ :model/Card {eligible-card :id} {:collection_id coll-id :name ia-name}+ :model/Card {ia-card :id} {:collection_id ia-coll :name ia-name}+ ;; root-resident same-name cards: the root is always an eligible container+ :model/Card {root-a :id} {:name root-name}+ :model/Card {root-b :id} {:name root-name}]+ (let [by-entity (duplicated-findings-by-entity!)]+ (testing "the audit-resident card neither gets a finding nor makes its eligible twin a duplicate"+ (is (nil? (by-entity [:card ia-card])))+ (is (nil? (by-entity [:card eligible-card]))))+ (testing "root-resident cards still cluster"+ (is (= [root-b] (get-in (by-entity [:card root-a]) [:details :duplicate_entity_ids])))+ (is (= [root-a] (get-in (by-entity [:card root-b]) [:details :duplicate_entity_ids])))))))))))++(deftest duplicated-api-transforms-collection-root-breadcrumb-test+ (testing "GET /duplicated: a top-level transforms-namespace collection subject gets the Transforms root sentinel"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Pipeline")]+ (mt/with-temp [:model/Collection {xf-a :id} {:name nm :namespace "transforms"}+ :model/Collection {xf-b :id} {:name nm :namespace "transforms"}]+ (scan/scan!)+ (let [rows (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/duplicated" :query prefix))+ by-id (into {} (map (juxt :entity_id identity)) rows)]+ (testing "the sentinel names the transforms tree, not the default \"Our analytics\""+ (doseq [coll-id [xf-a xf-b]]+ (is (= {:id "root" :name "Transforms" :namespace "transforms" :effective_ancestors []}+ (get-in (by-id coll-id) [:details :collection]))))+ (is (= "transforms"+ (:namespace (first (get-in (by-id xf-a) [:details :duplicate_entities])))))))))))))++(deftest duplicated-api-collection-peers-hydrate-test+ (testing "GET /duplicated hydrates collection peers gated on the collection's own read visibility (its own :id)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Shared")]+ (mt/with-temp [:model/Collection {parent :id} {:name (str prefix " Parent")}+ :model/Collection {coll-a :id} {:name nm+ :description "the A one"+ :location (str "/" parent "/")}+ :model/Collection {coll-b :id} {:name nm}]+ ;; the caller can read the flagged collection but NOT its peer+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-a)+ (scan/scan!)+ (let [finding (fn [user]+ (some #(when (= [coll-a "collection"] [(:entity_id %) (:entity_type %)]) %)+ (:data (mt/user-http-request user :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix :entity-types "collection"))))]+ (testing "superuser: the peer hydrates as a bare collection object - no card_type, no view_count"+ (let [f (finding :crowberto)]+ (is (some? f))+ (is (= [{:id coll-b :name nm :entity_type "collection" :namespace nil}]+ (get-in f [:details :duplicate_entities])))+ (testing "context: the breadcrumb anchor is the parent (where it lives), plus description; no owner/creator"+ (is (= parent (get-in f [:details :collection :id])))+ (is (= "the A one" (get-in f [:details :description])))+ (is (nil? (get-in f [:details :owner])))+ (is (nil? (get-in f [:details :creator]))))))+ (testing "non-admin who can read the flagged collection but not the peer: finding shows, peer drops out"+ (let [f (finding :rasta)]+ (is (some? f))+ (is (= [] (get-in f [:details :duplicate_entities])))+ (is (= 1 (:duplicate_count f)))))))))))))++(deftest duplicated-api-drops-archived-peers-test+ (testing "GET /duplicated drops a peer archived after the scan"+ ;; The stored duplicate_entity_ids are raw, so the hydrator is the only gate on them. Without an+ ;; archived filter a trashed peer keeps showing in `duplicate_entities` until the next scan.+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ nm (str prefix " Quarterly Revenue")]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {kept :id} {:name nm :collection_id coll}+ :model/Card {doomed-peer :id} {:name nm :collection_id coll}]+ (scan/scan!)+ (let [peer-ids (fn [] (->> (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/duplicated"+ :query prefix))+ (some #(when (= kept (:entity_id %)) %))+ :details :duplicate_entities+ (into #{} (map :id))))]+ (testing "the peer hydrates while it is active"+ (is (= #{doomed-peer} (peer-ids))))+ (t2/update! :model/Card :id doomed-peer {:archived true :archived_directly true})+ (testing "and drops out once archived, like a deleted peer"+ (is (= #{} (peer-ids)))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/events_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/events_test.cljnew file mode 100644index 00000000000..c24dcc329d2--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/events_test.clj@@ -0,0 +1,115 @@+(ns metabase-enterprise.content-diagnostics.events-test+ "Archiving/deleting a flagged entity invalidates its active findings via event subscriptions, so it drops+ out of the served set without waiting for a rescan. Collection archival cascades to the subtree with no+ per-descendant events, so its handler invalidates the subtree's collections and whatever currently lives in+ them; transforms are not archived with their collection and are left alone."+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase.events.core :as events]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn- mk-finding!+ ([entity-type entity-id]+ (mk-finding! entity-type entity-id nil))+ ([entity-type entity-id scope-collection-id]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id (str (random-uuid))+ :entity_type entity-type+ :entity_id entity-id+ :finding_type :stale+ :scope_collection_id scope-collection-id+ :details {}}))))++(defn- invalidated? [finding-id]+ (some? (t2/select-one-fn :invalidated_at :model/ContentDiagnosticsFinding :id finding-id)))++(deftest invalidate-for-entity!-test+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [target (mk-finding! :card 700001)+ other (mk-finding! :card 700002)]+ (finding/invalidate-for-entity! :card 700001)+ (is (invalidated? target) "the entity's finding is invalidated")+ (is (not (invalidated? other)) "a different entity's finding is untouched"))))++(deftest invalidate-for-collection-subtree!-test+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Collection {other-coll-id :id} {}+ :model/Card {inside-id :id} {:collection_id coll-id}+ :model/Card {outside-id :id} {:collection_id other-coll-id}]+ (let [coll-self (mk-finding! :collection coll-id)+ inside (mk-finding! :card inside-id coll-id)+ ;; scanned inside the subtree but since moved out: live membership decides, not scan-time scope+ moved-out (mk-finding! :card outside-id coll-id)]+ (finding/invalidate-for-collection-subtree! [coll-id])+ (is (invalidated? coll-self) "the archived collection's own finding")+ (is (invalidated? inside) "a finding for a card currently inside the subtree")+ (is (not (invalidated? moved-out)) "a finding for a card that has left the subtree is untouched")))))++(deftest invalidate-for-collection-subtree!-leaves-transforms-test+ (testing "a transform is not archived with its collection, so its finding stays active"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (let [coll-self (mk-finding! :collection tcoll-id)+ xform (mk-finding! :transform xform-id tcoll-id)]+ (finding/invalidate-for-collection-subtree! [tcoll-id])+ (is (invalidated? coll-self) "the archived collection's own finding")+ (is (not (invalidated? xform)) "the transform inside it is untouched"))))))++(deftest archive-events-invalidate-findings-test+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [actor (mt/user->id :crowberto)]+ (testing "card archive invalidates; a non-archive update does not"+ (mt/with-temp [:model/Card card {}]+ (let [f (mk-finding! :card (:id card))]+ (events/publish-event! :event/card-update+ {:object (assoc card :archived false) :previous-object card :user-id actor})+ (is (not (invalidated? f)) "still active after a non-archive update")+ (events/publish-event! :event/card-update+ {:object (assoc card :archived true) :previous-object card :user-id actor})+ (is (invalidated? f) "invalidated once archived"))))+ (testing "dashboard archive invalidates"+ (let [f (mk-finding! :dashboard 810001)]+ (events/publish-event! :event/dashboard-update+ {:object (t2/instance :model/Dashboard {:id 810001 :archived true}) :user-id actor})+ (is (invalidated? f))))+ (testing "document delete (archive publishes :event/document-delete) invalidates"+ (let [f (mk-finding! :document 820001)]+ (events/publish-event! :event/document-delete+ {:object (t2/instance :model/Document {:id 820001}) :user-id actor})+ (is (invalidated? f))))+ (testing "transform hard delete invalidates"+ (let [f (mk-finding! :transform 830001)]+ (events/publish-event! :event/transform-delete+ {:object (t2/instance :model/Transform {:id 830001}) :user-id actor})+ (is (invalidated? f))))))))++(deftest collection-archive-event-invalidates-subtree-test+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection parent {}+ :model/Collection child {:location (format "/%d/" (:id parent))}+ :model/Collection {other-coll-id :id} {}+ :model/Card {child-card-id :id} {:collection_id (:id child)}+ :model/Card {other-card-id :id} {:collection_id other-coll-id}]+ (let [self-finding (mk-finding! :collection (:id parent))+ child-finding (mk-finding! :card child-card-id (:id child))+ outside (mk-finding! :card other-card-id)]+ (events/publish-event! :event/collection-update+ {:object (assoc parent :archived true) :user-id (mt/user->id :crowberto)})+ (is (invalidated? self-finding) "the archived collection's own finding")+ (is (invalidated? child-finding) "a finding for a card in a descendant collection")+ (is (not (invalidated? outside)) "an unrelated finding is untouched"))))))++(deftest archive-event-is-a-noop-without-the-feature-test+ (mt/with-premium-features #{}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [f (mk-finding! :dashboard 850001)]+ (events/publish-event! :event/dashboard-update+ {:object (t2/instance :model/Dashboard {:id 850001 :archived true})+ :user-id (mt/user->id :crowberto)})+ (is (not (invalidated? f)) "no invalidation when :content-diagnostics is not enabled")))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/imbalanced_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/imbalanced_test.cljnew file mode 100644index 00000000000..69457343126--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/imbalanced_test.clj@@ -0,0 +1,551 @@+(ns ^:synchronized metabase-enterprise.content-diagnostics.imbalanced-test+ "Integration + serve-layer tests for the imbalanced family. The three finding types+ (`empty`/`sparse`/`crowded`) are produced by independent checkers, unit-tested in+ `checkers/imbalanced/{empty,sparse,crowded}_test`. This suite covers what spans them: cross-type+ co-occurrence on a single entity (there is no precedence, so one entity can carry several types at+ once), the `/imbalanced` umbrella endpoint (finding-types + count-bound filters, sort, pagination,+ permissions, breadcrumb/owner hydration), and per-type scan supersession."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.test-util :as cd.tu]+ [metabase.collections.models.collection :as collection]+ [metabase.permissions.core :as perms]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(use-fixtures :each cd.tu/with-authorized-reader!)++(defn- scope-prefix+ "Unique per-test entity-name prefix, passed as `:query` so assertions only see rows the test seeded -+ the findings table is shared across tests."+ []+ (str "cd-" (mt/random-name)))++(defn- imbalanced-findings-by-entity!+ "Run a scan and group its imbalanced (empty/sparse/crowded) findings as+ `{[entity-type entity-id] {finding-type finding}}` - the three checkers are independent, so one+ entity can carry several finding types at once."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (update-vals (group-by (juxt :entity_type :entity_id)+ (t2/select :model/ContentDiagnosticsFinding+ :scan_id scan-id+ :finding_type [:in [:empty :sparse :crowded]]))+ (fn [findings] (into {} (map (juxt :finding_type identity)) findings)))))++;;; --------------------------------------- cross-type co-occurrence ----------------------------------------+;;; Independent checkers mean no precedence: an entity can be flagged on several axes at once (dashboard+;;; tabs vs dashcards). On collections all three band the SAME direct-item count: `empty` (0) never+;;; co-occurs with either band, and sparse/crowded stay disjoint while the sparse floor sits at or below+;;; the crowded ceiling (independent admin settings - nothing validates them against each other). The+;;; single-checker rules are covered in the per-checker namespaces.++(deftest imbalanced-collection-disjoint-bands-test+ (testing "banding one direct-item count keeps the collection bands disjoint under in-order thresholds"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-crowded-collection-threshold-items 3+ content-diagnostics-sparse-collection-threshold-items 3]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; 4 empty dashboards: count 4 > 3 → crowded alone - the items' own emptiness never+ ;; makes their holder empty+ :model/Collection {crowded-only :id} {}+ :model/Dashboard _ {:collection_id crowded-only}+ :model/Dashboard _ {:collection_id crowded-only}+ :model/Dashboard _ {:collection_id crowded-only}+ :model/Dashboard _ {:collection_id crowded-only}+ ;; 1 empty dashboard: count 1 (< 3) → sparse alone+ :model/Collection {sparse-only :id} {}+ :model/Dashboard _ {:collection_id sparse-only}]+ (let [by-entity (imbalanced-findings-by-entity!)]+ (testing "4 empty dashboards → crowded alone, never also empty"+ (let [fs (by-entity [:collection crowded-only])]+ (is (= #{:crowded} (set (keys fs))))+ (is (= 4 (:content_count (:crowded fs))))))+ (testing "1 empty dashboard → sparse alone, never also empty"+ (let [fs (by-entity [:collection sparse-only])]+ (is (= #{:sparse} (set (keys fs))))+ (is (= 1 (:content_count (:sparse fs)))))))))))))++(deftest imbalanced-dashboard-cooccurrence-test+ (testing "a dashboard can be crowded on tabs while independently empty or sparse on dashcards"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab 2+ content-diagnostics-crowded-dashboard-threshold-tabs 2+ content-diagnostics-sparse-dashboard-threshold-dashcards 2]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll :id} {}+ ;; 3 tabs (>2) holding 1 dashcard total (<2) → crowded (tabs) AND sparse+ :model/Dashboard {tabs-sparse :id} {:collection_id coll}+ :model/DashboardTab {ts-t1 :id} {:dashboard_id tabs-sparse}+ :model/DashboardTab _ {:dashboard_id tabs-sparse}+ :model/DashboardTab _ {:dashboard_id tabs-sparse}+ :model/DashboardCard _ {:dashboard_id tabs-sparse :dashboard_tab_id ts-t1}+ ;; 3 empty tabs (>2), 0 dashcards → crowded (tabs) AND empty+ :model/Dashboard {tabs-empty :id} {:collection_id coll}+ :model/DashboardTab _ {:dashboard_id tabs-empty}+ :model/DashboardTab _ {:dashboard_id tabs-empty}+ :model/DashboardTab _ {:dashboard_id tabs-empty}]+ (let [by-entity (imbalanced-findings-by-entity!)]+ (testing "3 tabs with 1 dashcard total → crowded (tabs) AND sparse, independently"+ (let [fs (by-entity [:dashboard tabs-sparse])]+ (is (= #{:crowded :sparse} (set (keys fs))))+ (is (= "tabs" (get-in fs [:crowded :details :unit])))+ (is (= 3 (:content_count (:crowded fs))))+ (is (= 1 (:content_count (:sparse fs))))))+ (testing "3 empty tabs → crowded (tabs) AND empty, independently"+ (let [fs (by-entity [:dashboard tabs-empty])]+ (is (= #{:crowded :empty} (set (keys fs))))+ (is (= "tabs" (get-in fs [:crowded :details :unit]))))))))))))++;;; -------------------------------------------------- API -------------------------------------------------++(deftest imbalanced-api-feature-gated-test+ (testing "GET /imbalanced is gated on the :content-diagnostics premium feature"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (testing "licensed → 200 with the paginated envelope"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (let [resp (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/imbalanced")]+ (is (contains? resp :data))+ (is (contains? resp :total)))))+ (testing "unlicensed → 402"+ (mt/with-premium-features #{}+ (mt/user-http-request :rasta :get 402 "ee/content-diagnostics/imbalanced"))))))++(defn- insert-imbalanced!+ "Insert one imbalanced finding row directly (API tests exercise the read path, not the checker).+ Optional keys (`:entity-kind`, `:entity-collection-name`, `:scope-collection-id`) are only needed by+ fixtures whose assertions touch them."+ [{:keys [entity-type entity-id entity-kind finding-type content-count details+ entity-collection-name scope-collection-id]+ nm :name+ :or {details {:threshold 5 :unit "items"}}}]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "imb-api"+ :entity_type entity-type+ :entity_id entity-id+ :entity_kind entity-kind+ :entity_name nm+ :entity_collection_name entity-collection-name+ :scope_collection_id scope-collection-id+ :finding_type finding-type+ :content_count content-count+ :details details})))++(deftest imbalanced-api-finding-types-test+ (testing "GET /imbalanced narrows by finding-types (default all three)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {c1 :id} {:collection_id coll}+ :model/Card {c2 :id} {:collection_id coll}+ :model/Dashboard {d1 :id} {:collection_id coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)+ empty-fid (insert-imbalanced! {:entity-type :card :entity-id c1+ :name (str prefix " Empty") :finding-type :empty+ :content-count 0})+ sparse-fid (insert-imbalanced! {:entity-type :dashboard :entity-id d1+ :name (str prefix " Sparse") :finding-type :sparse+ :content-count 2})+ crowded-fid (insert-imbalanced! {:entity-type :card :entity-id c2+ :name (str prefix " Crowded") :finding-type :crowded+ :content-count 30})+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix kvs)))))]+ (testing "omitted → all three finding types"+ (is (= #{empty-fid sparse-fid crowded-fid} (ids))))+ (testing "one type"+ (is (= #{empty-fid} (ids :finding-types "empty"))))+ (testing "two types"+ (is (= #{sparse-fid crowded-fid} (ids :finding-types ["sparse" "crowded"])))))))))))++(deftest imbalanced-api-multiple-types-per-entity-test+ (testing "one entity surfaces once per finding type - there is no read-time dedup across the umbrella"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)+ empty-fid (insert-imbalanced! {:entity-type :collection :entity-id coll+ :name (str prefix " Coll") :finding-type :empty+ :content-count 0})+ crowded-fid (insert-imbalanced! {:entity-type :collection :entity-id coll+ :name (str prefix " Coll") :finding-type :crowded+ :content-count 150})+ fetch (fn [& kvs] (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced" :query prefix kvs))]+ (testing "both rows serve by default; total counts findings, not entities"+ (let [resp (fetch)]+ (is (= #{empty-fid crowded-fid} (set (map :id (:data resp)))))+ (is (= 2 (:total resp)))))+ (testing "finding-types narrows within the entity's rows"+ (is (= #{crowded-fid} (set (map :id (:data (fetch :finding-types "crowded"))))))))))))))++(deftest imbalanced-api-entity-types-filter-test+ (testing "GET /imbalanced filters by entity-types, including the collection subject"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {card-id :id} {:collection_id coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)+ card-fid (insert-imbalanced! {:entity-type :card :entity-id card-id :entity-kind :card+ :name (str prefix " Card") :finding-type :empty+ :content-count 0})+ coll-fid (insert-imbalanced! {:entity-type :collection :entity-id coll :entity-kind :collection+ :name (str prefix " Coll") :finding-type :sparse+ :content-count 1})+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix kvs)))))]+ (testing "omitted → all entity types"+ (is (= #{card-fid coll-fid} (ids))))+ (testing "collection only"+ (is (= #{coll-fid} (ids :entity-types "collection"))))+ (testing "multiple values"+ (is (= #{card-fid coll-fid} (ids :entity-types ["card" "collection"])))))))))))++(deftest imbalanced-api-sort-test+ (testing "GET /imbalanced sorts by content-count and name, both directions"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {c1 :id} {:collection_id coll}+ :model/Card {c2 :id} {:collection_id coll}+ :model/Card {c3 :id} {:collection_id coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)+ a-fid (insert-imbalanced! {:entity-type :card :entity-id c1 :finding-type :sparse+ :name (str prefix " Alpha") :content-count 20})+ b-fid (insert-imbalanced! {:entity-type :card :entity-id c2 :finding-type :sparse+ :name (str prefix " Beta") :content-count 5})+ g-fid (insert-imbalanced! {:entity-type :card :entity-id c3 :finding-type :sparse+ :name (str prefix " Gamma") :content-count 10})+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix kvs))))]+ (testing "content-count asc/desc (isolated from the name order)"+ (is (= [b-fid g-fid a-fid] (order :sort-column "content-count" :sort-direction "asc")))+ (is (= [a-fid g-fid b-fid] (order :sort-column "content-count" :sort-direction "desc"))))+ (testing "name asc/desc"+ (is (= [a-fid b-fid g-fid] (order :sort-column "name" :sort-direction "asc")))+ (is (= [g-fid b-fid a-fid] (order :sort-column "name" :sort-direction "desc")))))))))))++(deftest imbalanced-api-paginates-test+ (testing "GET /imbalanced honors limit/offset and reports the full valid total"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {c1 :id} {:collection_id coll}+ :model/Card {c2 :id} {:collection_id coll}+ :model/Card {c3 :id} {:collection_id coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)]+ (doseq [cid [c1 c2 c3]]+ (insert-imbalanced! {:entity-type :card :entity-id cid :finding-type :empty+ :name (str prefix "-" cid) :content-count 0}))+ (let [page (fn [limit offset]+ (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/imbalanced"+ :query prefix :limit limit :offset offset))]+ (testing "limit caps the page; total reflects the full valid set; limit/offset echoed back"+ (let [r (page 2 0)]+ (is (= 2 (count (:data r))))+ (is (= 3 (:total r)))+ (is (= 2 (:limit r)))+ (is (= 0 (:offset r)))))+ (testing "offset advances to the remainder"+ (is (= 1 (count (:data (page 2 2))))))))))))))++(deftest imbalanced-api-does-not-leak-across-finding-types-test+ (testing "imbalanced rows never surface in /stale or /slow, and their rows never surface in /imbalanced"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {c-stale :id} {:collection_id coll}+ :model/Card {c-slow :id} {:collection_id coll}+ :model/Card {c-imb :id} {:collection_id coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll)+ (let [prefix (scope-prefix)+ stale-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id c-stale+ :entity_name (str prefix "-stale")+ :finding_type :stale :details {:threshold_days 90}}))+ slow-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id c-slow+ :entity_name (str prefix "-slow")+ :finding_type :slow :duration_ms 20000+ :details {:threshold_ms 15000}}))+ imb-fid (insert-imbalanced! {:entity-type :card :entity-id c-imb :finding-type :empty+ :name (str prefix "-imb") :content-count 0})+ ids (fn [path] (set (map :id (:data (mt/user-http-request :rasta :get 200 path+ :query prefix)))))]+ (testing "/imbalanced returns only the imbalanced finding"+ (is (= #{imb-fid} (ids "ee/content-diagnostics/imbalanced"))))+ (testing "/stale returns only the stale finding"+ (is (= #{stale-fid} (ids "ee/content-diagnostics/stale"))))+ (testing "/slow returns only the slow finding"+ (is (= #{slow-fid} (ids "ee/content-diagnostics/slow")))))))))))++(deftest imbalanced-api-collection-subject-permissions-test+ (testing "a collection finding is served only when the caller can read the collection ITSELF"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {readable :id} {}+ :model/Collection {unreadable :id} {}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) readable)+ (let [prefix (scope-prefix)+ r-fid (insert-imbalanced! {:entity-type :collection :entity-id readable+ :name (str prefix " Readable")+ :finding-type :sparse :content-count 1})+ u-fid (insert-imbalanced! {:entity-type :collection :entity-id unreadable+ :name (str prefix " Unreadable")+ :finding-type :sparse :content-count 1})+ ids-for (fn [user] (set (map :id (:data (mt/user-http-request+ user :get 200 "ee/content-diagnostics/imbalanced"+ :query prefix)))))]+ (testing "non-admin sees only the readable collection's finding"+ (let [ids (ids-for :rasta)]+ (is (contains? ids r-fid))+ (is (not (contains? ids u-fid)))))+ (testing "superuser sees both"+ (let [ids (ids-for :crowberto)]+ (is (contains? ids r-fid))+ (is (contains? ids u-fid)))))))))))++(deftest imbalanced-api-personal-collection-subject-test+ (testing "a personal collection's own finding is excluded by default, returned with include-personal-collections=true, and hydrates owner (not creator)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))+ prefix (scope-prefix)+ fid (insert-imbalanced! {:entity-type :collection :entity-id pers-id+ :name (str prefix " Personal") :finding-type :sparse+ :content-count 1})+ fetch (fn [& kvs] (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced" :query prefix kvs))]+ (testing "default (param omitted) → the personal collection's finding is excluded"+ (is (empty? (:data (fetch))))+ (is (zero? (:total (fetch)))))+ (testing "include-personal-collections=true → returned (bounded by visibility - it is rasta's own)"+ (let [row (first (filter #(= fid (:id %)) (:data (fetch :include-personal-collections true))))]+ (is (some? row))+ (testing "owner = the owning user; creator stays null (collections have no creator)"+ (is (=? {:id (mt/user->id :rasta)+ :type "user"}+ (get-in row [:details :owner])))+ (is (nil? (get-in row [:details :creator]))))+ (testing "a root-level personal collection's parent is root → the root sentinel breadcrumb"+ (is (= "root" (get-in row [:details :collection :id]))))))))))))++(deftest imbalanced-api-collection-breadcrumb-is-parent-test+ (testing "a collection finding's breadcrumb is its PARENT collection; the root sentinel at root; content_count stays top-level"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {parent :id} {:name "Parent Coll"}+ :model/Collection {child :id} {:location (collection/location-path parent)}+ :model/Collection {rooted :id} {}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) parent)+ (let [prefix (scope-prefix)+ child-fid (insert-imbalanced! {:entity-type :collection :entity-id child+ :name (str prefix " Child") :finding-type :sparse+ :content-count 2})+ rooted-fid (insert-imbalanced! {:entity-type :collection :entity-id rooted+ :name (str prefix " Rooted") :finding-type :sparse+ :content-count 2})+ rows (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/imbalanced" :query prefix))+ row-for (fn [fid] (first (filter #(= fid (:id %)) rows)))]+ (testing "nested collection → the parent's breadcrumb, with a null namespace (default tree)"+ (let [row (row-for child-fid)]+ (is (= parent (get-in row [:details :collection :id])))+ (is (= "Parent Coll" (get-in row [:details :collection :name])))+ (is (contains? (get-in row [:details :collection]) :namespace))+ (is (nil? (get-in row [:details :collection :namespace])))))+ (testing "root-level collection → the root sentinel (id \"root\", no ancestors), matching how the app links root"+ (let [breadcrumb (get-in (row-for rooted-fid) [:details :collection])]+ (is (= "root" (:id breadcrumb)))+ (is (contains? breadcrumb :namespace))+ (is (nil? (:namespace breadcrumb)))+ (is (= [] (:effective_ancestors breadcrumb)))))+ (testing "content_count is hoisted top-level, never duplicated inside details"+ (let [row (row-for child-fid)]+ (is (= 2 (:content_count row)))+ (is (not (contains? (:details row) :content_count))))))))))))++(deftest imbalanced-api-unreadable-parent-breadcrumb-test+ (testing "a readable collection under an unreadable parent serves its finding with a null breadcrumb - the parent's name never leaks"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {hidden-parent :id} {:name "Hidden Parent"}+ :model/Collection {child :id} {:location (collection/location-path hidden-parent)}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) child)+ (let [prefix (scope-prefix)+ fid (insert-imbalanced! {:entity-type :collection :entity-id child+ :name (str prefix " Child") :finding-type :sparse+ :content-count 1})+ row-for (fn [user]+ (->> (mt/user-http-request user :get 200+ "ee/content-diagnostics/imbalanced" :query prefix)+ :data+ (filter #(= fid (:id %)))+ first))]+ (testing "the finding itself is served - the subject collection is readable"+ (is (some? (row-for :rasta))))+ (testing "the unreadable parent degrades to a null breadcrumb (distinct from a root-resident+ entity, which gets the root sentinel) - the parent's name never leaks"+ (is (nil? (get-in (row-for :rasta) [:details :collection]))))+ (testing "an admin still gets the parent breadcrumb"+ (is (= hidden-parent (get-in (row-for :crowberto) [:details :collection :id])))))))))))++(deftest api-collection-name-permission-gate-test+ (testing "unreadable parent → collection_name nil, even though the stored column holds the hidden name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {parent-id :id} {:name "Hidden Parent"}+ :model/Collection {child-id :id} {:location (collection/location-path parent-id)}]+ ;; child readable, parent NOT+ (perms/grant-collection-read-permissions! (perms/all-users-group) child-id)+ (let [prefix (scope-prefix)+ fid (insert-imbalanced! {:entity-type :collection :entity-id child-id+ :entity-kind :collection+ :name (str prefix " child") :finding-type :empty+ :content-count 0+ :scope-collection-id parent-id+ :entity-collection-name "Hidden Parent"})+ [finding] (:data (mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix))]+ (is (=? {:id fid+ :collection_name nil+ :details {:collection nil}}+ finding)))))))))++(deftest api-collection-name-relocation-gate-test+ (testing "entity moved post-scan: the scan-time parent's name is not served when the caller cannot read it"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {hidden-id :id} {:name "Hidden Origin"}+ :model/Collection {public-id :id} {}+ ;; the card lives in the readable collection NOW; the finding row below says it+ ;; sat in the hidden one at scan time+ :model/Card {card-id :id} {:collection_id public-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) public-id)+ (let [prefix (scope-prefix)+ fid (insert-imbalanced! {:entity-type :card :entity-id card-id+ :entity-kind :question+ :name (str prefix " moved") :finding-type :empty+ :content-count 0+ :scope-collection-id hidden-id+ :entity-collection-name "Hidden Origin"})+ [finding] (:data (mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix))]+ ;; the row serves (live parent readable) but the scan-time parent's name must not - only+ ;; its live breadcrumb is the caller's to see+ (is (=? {:id fid+ :collection_name nil+ :details {:collection {:id public-id}}}+ finding)))))))))++(deftest imbalanced-api-as-of-serves-as-a-string-test+ (testing "an evidence-dated empty serves `details.as_of` as an ISO-8601 string"+ ;; as_of round-trips through the JSON details blob, so the response schema types it ms/TemporalString -+ ;; a Temporal-typed schema rejects the decoded value+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {card :id} {:collection_id coll}]+ (let [prefix (scope-prefix)+ as-of (t/offset-date-time)+ fid (insert-imbalanced! {:entity-type :card :entity-id card+ :name (str prefix " Empty") :finding-type :empty+ :content-count 0+ :details {:threshold 0 :unit "rows" :as_of as-of}})+ row (->> (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/imbalanced" :query prefix)+ :data+ (filter #(= fid (:id %)))+ first)]+ (is (some? row))+ (is (string? (get-in row [:details :as_of])))+ (testing "the stamped instant survives the round-trip"+ (is (= (-> as-of t/instant (t/truncate-to :millis))+ (-> (get-in row [:details :as_of]) t/offset-date-time t/instant+ (t/truncate-to :millis)))))))))))++;;; --------------------------------------------- scan supersession ----------------------------------------++(deftest imbalanced-scan-shares-batch-and-supersedes-per-type-test+ (testing "one scan writes stale + imbalanced in a single scan_id batch; a rescan supersedes a resolved sparse finding while the still-stale entity stays active"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Card {stale-card :id} {:collection_id coll+ :last_used_at (t/minus (t/offset-date-time) (t/days 400))}]+ (let [scan-1 (:scan_id (scan/scan!))+ sparse-row (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :collection :entity_id coll :finding_type :sparse)+ stale-row (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id stale-card :finding_type :stale)]+ (testing "the collection's sparse finding and the card's stale finding share one scan_id batch"+ (is (some? sparse-row))+ (is (some? stale-row))+ (is (= scan-1 (:scan_id sparse-row) (:scan_id stale-row))))+ ;; resolve the sparseness by lowering the bound (1 item is no longer < 1); the card stays stale+ (mt/with-temporary-setting-values [content-diagnostics-sparse-collection-threshold-items 1]+ (scan/scan!))+ (testing "the resolved sparse finding is soft-invalidated"+ (is (some? (:invalidated_at (t2/select-one :model/ContentDiagnosticsFinding+ :id (:id sparse-row))))))+ (testing "the still-stale entity keeps an active stale finding (per-type supersession)"+ (is (seq (t2/select :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id stale-card+ :finding_type :stale :invalidated_at nil))))))))))++(deftest api-sort-by-finding-type-test+ (testing "GET /imbalanced sorts by finding_type (alphabetical: crowded < empty < sparse)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-a :id} {}+ :model/Collection {coll-b :id} {}+ :model/Collection {coll-c :id} {}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-a)+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-b)+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-c)+ (let [prefix (scope-prefix)+ insert (fn [cid ftype]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "s" :entity_type :collection+ :entity_kind :collection+ :entity_id cid :finding_type ftype+ :entity_name (str prefix "-" (name ftype))+ :details {:threshold 5 :unit "items"}+ :content_count 1})))+ sparse-fid (insert coll-a :sparse)+ crowded-fid (insert coll-b :crowded)+ empty-fid (insert coll-c :empty)+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/imbalanced"+ :query prefix kvs))))]+ (is (= [crowded-fid empty-fid sparse-fid]+ (order :sort-column "finding-type" :sort-direction "asc")))+ (is (= [sparse-fid empty-fid crowded-fid]+ (order :sort-column "finding-type" :sort-direction "desc"))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/invalidate_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/invalidate_test.cljnew file mode 100644index 00000000000..4d3d574b416--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/invalidate_test.clj@@ -0,0 +1,146 @@+(ns ^:synchronized metabase-enterprise.content-diagnostics.invalidate-test+ "`POST /invalidate` dismisses the findings a caller can see and reports every other requested id as+ skipped."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.test-util :as cd.tu]+ [metabase.permissions.core :as perms]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(use-fixtures :each cd.tu/with-authorized-reader!)++(defn- invalidate!+ [ids]+ (mt/user-http-request :rasta :post 200 "ee/content-diagnostics/invalidate" {:ids ids}))++(defn- active?+ [finding-id]+ (t2/exists? :model/ContentDiagnosticsFinding :id finding-id :invalidated_at nil))++(defn- stamped?+ [finding-id]+ (t2/exists? :model/ContentDiagnosticsFinding :id finding-id :invalidated_at [:not= nil]))++(defn- stale-ids+ []+ (set (map :id (:data (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/stale")))))++(defn- nonexistent-id+ []+ (inc (or (t2/select-one-fn :id :model/ContentDiagnosticsFinding {:order-by [[:id :desc]]}) 0)))++(defn- with-cards!+ "Call `f` with the ids of two cards in a collection `:rasta` can read, and one in a collection it cannot."+ [f]+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {visible-coll :id} {}+ :model/Collection {hidden-coll :id} {}+ :model/Card {visible-card :id} {:collection_id visible-coll}+ :model/Card {other-card :id} {:collection_id visible-coll}+ :model/Card {hidden-card :id} {:collection_id hidden-coll}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) visible-coll)+ (f visible-card other-card hidden-card))))))++(deftest invalidate-happy-path-test+ (testing "active visible findings are invalidated and stamped"+ (with-cards!+ (fn [card other-card _]+ (let [a (cd.tu/insert-finding! "scan" card nil)+ b (cd.tu/insert-finding! "scan" other-card nil)]+ (is (= {:invalidated [a b], :skipped []}+ (invalidate! [a b])))+ (is (true? (stamped? a)))+ (is (true? (stamped? b))))))))++(deftest invalidate-mixed-test+ (testing "nonexistent and already-invalidated ids are skipped"+ (with-cards!+ (fn [card other-card _]+ (let [active (cd.tu/insert-finding! "scan" card nil)+ stamped (cd.tu/insert-finding! "scan" other-card (t/offset-date-time))+ missing (nonexistent-id)]+ (is (= {:invalidated [active], :skipped [missing stamped]}+ (invalidate! [active missing stamped]))))))))++(deftest invalidate-not-visible-test+ (testing "a finding whose entity the caller cannot read is skipped and left active"+ (with-cards!+ (fn [visible _ hidden]+ (let [ok (cd.tu/insert-finding! "scan" visible nil)+ unseen (cd.tu/insert-finding! "scan" hidden nil)]+ (is (= {:invalidated [ok], :skipped [unseen]}+ (invalidate! [ok unseen])))+ (is (true? (active? unseen))))))))++(deftest invalidate-skips-superseded-finding-test+ (testing "an older active finding behind a newer one for the same entity is skipped, and the newer one stays listed"+ (with-cards!+ (fn [card _ _]+ (let [older (cd.tu/insert-finding! "scan-old" card nil)+ newer (cd.tu/insert-finding! "scan-new" card nil)]+ (is (= {:invalidated [], :skipped [older]}+ (invalidate! [older])))+ (is (true? (active? older)))+ (is (contains? (stale-ids) newer))+ (testing "dismissing the newer one hides the entity, even though the older row is still active"+ (is (= {:invalidated [newer], :skipped []}+ (invalidate! [newer])))+ (is (not-any? #{older newer} (stale-ids)))))))))++(deftest invalidate-is-idempotent-test+ (testing "a repeat call skips everything"+ (with-cards!+ (fn [card other-card _]+ (let [ids [(cd.tu/insert-finding! "scan" card nil)+ (cd.tu/insert-finding! "scan" other-card nil)]]+ (is (= {:invalidated ids, :skipped []} (invalidate! ids)))+ (is (= {:invalidated [], :skipped ids} (invalidate! ids))))))))++(deftest invalidate-drops-finding-from-list-test+ (testing "the dismissed finding no longer appears under GET /stale"+ (with-cards!+ (fn [card _ _]+ (let [id (cd.tu/insert-finding! "scan" card nil)]+ (is (contains? (stale-ids) id))+ (invalidate! [id])+ (is (not (contains? (stale-ids) id))))))))++(deftest invalidate-dedupes-ids-test+ (testing "duplicate ids appear once in the response, in first-occurrence order"+ (with-cards!+ (fn [card _ _]+ (let [a (cd.tu/insert-finding! "scan" card nil)+ missing (nonexistent-id)]+ (is (= {:invalidated [a], :skipped [missing]}+ (invalidate! [missing a missing a]))))))))++(deftest invalidate-rolls-back-on-failure-test+ (testing "a failed write rolls back every stamp and answers 500"+ (with-cards!+ (fn [card other-card _]+ (let [a (cd.tu/insert-finding! "scan" card nil)+ b (cd.tu/insert-finding! "scan" other-card nil)+ stamp! cd.db/invalidate-findings-where!]+ ;; stamp for real, then fail, so the assertions prove a rollback rather than a write never made+ (mt/with-dynamic-fn-redefs [cd.db/invalidate-findings-where! (fn [where]+ (stamp! where)+ (throw (ex-info "boom" {})))]+ (mt/user-http-request :rasta :post 500 "ee/content-diagnostics/invalidate" {:ids [a b]}))+ (is (true? (active? a)))+ (is (true? (active? b))))))))++(deftest invalidate-rejects-bad-ids-size-test+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (testing "an empty `ids` is a 400"+ (mt/user-http-request :rasta :post 400 "ee/content-diagnostics/invalidate" {:ids []}))+ (testing "more than 500 `ids` is a 400"+ (mt/user-http-request :rasta :post 400 "ee/content-diagnostics/invalidate" {:ids (range 1 502)}))+ (testing "exactly 500 is accepted"+ (let [start (nonexistent-id)]+ (mt/user-http-request :rasta :post 200 "ee/content-diagnostics/invalidate"+ {:ids (range start (+ start 500))})))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/models/finding_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/models/finding_test.cljnew file mode 100644index 00000000000..38bd0f70fd5--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/models/finding_test.clj@@ -0,0 +1,135 @@+(ns metabase-enterprise.content-diagnostics.models.finding-test+ "Schema ↔ model contract for `content_diagnostics_finding`: a round-trip pins that the migration's+ columns and the model's `deftransforms` agree on every supported app-db (H2 locally; the CI backend+ matrices re-run this against Postgres and MySQL)."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.db :as cd.db]+ [metabase-enterprise.content-diagnostics.models.finding :as finding]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase-enterprise.content-diagnostics.test-util :as cd.test-util]+ [metabase.app-db.core :as mdb]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(deftest finding-round-trip-test+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "round-trip"+ :entity_type :card+ :entity_id 1+ :finding_type :stale+ :details {:threshold_days 90}}))+ row (t2/select-one :model/ContentDiagnosticsFinding :id fid)]+ (testing "keyword + JSON transforms round-trip; detected_at defaults; rows start active"+ (is (=? {:scan_id "round-trip"+ :entity_type :card+ :finding_type :stale+ :details {:threshold_days 90}+ :detected_at some?+ :invalidated_at nil}+ row)))+ (testing "stamping invalidated_at round-trips (the active-set filter's column)"+ (t2/update! :model/ContentDiagnosticsFinding fid {:invalidated_at (t/offset-date-time)})+ (is (some? (:invalidated_at (t2/select-one :model/ContentDiagnosticsFinding :id fid))))))))++(deftest stale-threshold-setting-default-test+ (testing "the staleness window defaults to 90 days"+ (is (= 90 (cd.settings/content-diagnostics-stale-threshold-days)))))++(deftest finding-retention-setting-default-test+ (testing "invalidated findings are kept for 30 days before the trimmer deletes them"+ (is (= 30 (cd.settings/content-diagnostics-finding-retention-days)))))++(deftest slow-threshold-setting-defaults-test+ (testing "the slow-card query-time threshold defaults to 15 seconds"+ (is (= 15 (cd.settings/content-diagnostics-slow-card-threshold-seconds))))+ (testing "the slow-transform run-time threshold defaults to 60 seconds"+ (is (= 60 (cd.settings/content-diagnostics-slow-transform-threshold-seconds)))))++(deftest imbalanced-threshold-setting-defaults-test+ (testing "crowded: collection >100 direct items"+ (is (= 100 (cd.settings/content-diagnostics-crowded-collection-threshold-items))))+ (testing "crowded: dashboard >20 dashcards on any one tab"+ (is (= 20 (cd.settings/content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab))))+ (testing "crowded: dashboard >5 tabs"+ (is (= 5 (cd.settings/content-diagnostics-crowded-dashboard-threshold-tabs))))+ (testing "crowded: document >20 embedded cards"+ (is (= 20 (cd.settings/content-diagnostics-crowded-document-threshold-cards))))+ (testing "sparse: non-empty collection <5 direct items"+ (is (= 5 (cd.settings/content-diagnostics-sparse-collection-threshold-items))))+ (testing "sparse: non-empty dashboard <4 dashcards total"+ (is (= 4 (cd.settings/content-diagnostics-sparse-dashboard-threshold-dashcards)))))++;; `duplicated` has no settings-defaults test: a duplicate is definitionally a cluster of >= 2, so there+;; is no threshold to configure.++(deftest duplicated-details-round-trip-test+ (testing "the duplicated details envelope survives the JSON round-trip"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [details {:normalized_name "orders by month"+ :duplicate_entity_ids [10 11]}+ fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "dup-round-trip"+ :entity_type :card+ :entity_id 1+ :finding_type :duplicate_name+ :duplicate_count 2+ :details details}))+ row (t2/select-one :model/ContentDiagnosticsFinding :id fid)]+ (is (= details (:details row)))+ (is (= 2 (:duplicate_count row)))))))++;;; ----------------------------------- trimming invalidated findings -----------------------------------++(deftest delete-batch-query-covers-every-supported-app-db-test+ (testing "every app DB Metabase can run on builds a batch-delete query"+ ;; `mdb/spec` has one method per supported app DB - a new one cannot connect without adding one -+ ;; so deriving the set from it fails here the moment a fourth app DB lands. `:default` is dropped+ ;; because a catch-all spec method is not itself a dispatch value the trimmer can be asked for.+ (doseq [db-type (remove #{:default} (keys (methods mdb/spec)))]+ (is (map? (#'cd.db/delete-batch-query db-type (t/offset-date-time)+ @#'finding/delete-batch-size))+ (str "no batch-delete arm for app db " db-type)))))++(def ^:private long-ago+ "Cutoff for every trim below. Years back, so a run matches only this suite's own rows - at a cutoff+ of `now` it would delete whatever expired findings another suite left in the shared app DB."+ (t/minus (t/offset-date-time) (t/years 5)))++(defn- insert-finding!+ [entity-id invalidated-at]+ (cd.test-util/insert-finding! "finding-trim-test" entity-id invalidated-at))++(deftest delete-invalidated-before-cutoff-test+ (testing "only findings invalidated before the cutoff are deleted"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [old (insert-finding! 1 (t/minus long-ago (t/days 1)))+ recent (insert-finding! 2 (t/plus long-ago (t/days 1)))]+ (finding/delete-invalidated-before! long-ago)+ (is (= #{recent}+ (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:in :id [old recent]]})))))))++(deftest delete-invalidated-before-spares-active-findings-test+ (testing "active findings (invalidated_at NULL) survive however old they are"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; the expired peer keeps the assertion discriminating - without it, a trim that deleted+ ;; nothing at all would pass just as well+ (let [active (insert-finding! 3 nil)+ expired (insert-finding! 4 (t/minus long-ago (t/days 1)))]+ (finding/delete-invalidated-before! long-ago)+ (is (= #{active}+ (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:in :id [active expired]]})))))))++(deftest delete-invalidated-before-clears-more-than-one-batch-test+ (testing "every matching finding is deleted, not just the first batch"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [ids (mapv #(insert-finding! % (t/minus long-ago (t/days 1))) (range 1000 1005))]+ ;; the count is what pins this - stopping after one batch would return 2, not 5+ (is (= 5 (with-redefs [finding/delete-batch-size 2]+ (finding/delete-invalidated-before! long-ago))))+ (is (empty? (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:in :id ids]})))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.cljnew file mode 100644index 00000000000..0f260b03603--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/scan_test.clj@@ -0,0 +1,1420 @@+(ns ^:synchronized metabase-enterprise.content-diagnostics.scan-test+ "The scan pipeline runs the instance-wide `stale` checker, persists a snapshot, and supersedes+ prior findings; the `GET /stale` API lists the latest valid finding per entity, live+ permission-filtered and batch-hydrated."+ (:require+ [clojure.set :as set]+ [clojure.test :refer :all]+ [honey.sql :as sql]+ [java-time.api :as t]+ [medley.core :as m]+ [metabase-enterprise.content-diagnostics.api.common :as api.common]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase-enterprise.content-diagnostics.task.scan :as task.scan]+ [metabase-enterprise.content-diagnostics.test-util :as cd.tu]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.prometheus :as prometheus]+ [metabase.collections.models.collection :as collection]+ [metabase.documents.prose-mirror :as prose-mirror]+ [metabase.permissions.core :as perms]+ [metabase.queries.schema :as queries.schema]+ [metabase.task.core :as task]+ [metabase.test :as mt]+ [metabase.tracing.core :as tracing]+ [metabase.util :as u]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(use-fixtures :each cd.tu/with-authorized-reader!)++(defn- stale-instant+ "Comfortably past the staleness threshold, whatever its default — derived from the setting so a+ default change can't silently desync the fixture."+ []+ (t/minus (t/offset-date-time) (t/days (+ (cd.settings/content-diagnostics-stale-threshold-days) 30))))++(defn- fresh-instant []+ (t/offset-date-time))++(defn- scope-prefix+ "Unique per-test entity-name prefix, passed as `:query` so assertions only see rows the test+ seeded - the findings table is shared, so an unscoped list read can see sibling tests' rows."+ []+ (str "cd-" (mt/random-name)))++(defn- finding-for+ [rows entity-type entity-id]+ (m/find-first #(and (= entity-type (:entity_type %)) (= entity-id (:entity_id %))) rows))++(deftest scan-detects-stale-test+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ ;; stale: last activity well past the threshold+ :model/Card {stale-card-1 :id} {:collection_id coll-id :last_used_at (stale-instant)}+ :model/Card {stale-card-2 :id} {:collection_id coll-id :last_used_at (stale-instant)}+ :model/Card {stale-card-3 :id} {:collection_id coll-id :last_used_at (stale-instant)}+ :model/Dashboard {stale-dash-1 :id} {:collection_id coll-id :last_viewed_at (stale-instant)}+ :model/Dashboard {stale-dash-2 :id} {:collection_id coll-id :last_viewed_at (stale-instant)}+ :model/Document {stale-doc :id} {:collection_id coll-id :last_viewed_at (stale-instant)}+ ;; never-used arms: document never viewed (view_count 0; last_viewed_at is+ ;; creation-stamped) / transform never ran — each + created before the cutoff+ :model/Document {never-viewed-doc :id} {:collection_id coll-id :created_at (stale-instant)}+ ;; transforms live in the :transforms collection namespace, not regular collections —+ ;; irrelevant to the instance-wide scan, so no collection at all+ :model/Transform {never-ran-transform :id} {:created_at (stale-instant)}+ ;; fresh: used just now (or, for the never-run transform, created just now) — must NOT be flagged+ :model/Card {fresh-card :id} {:collection_id coll-id :last_used_at (fresh-instant)}+ :model/Dashboard {fresh-dash :id} {:collection_id coll-id :last_viewed_at (fresh-instant)}+ :model/Document {fresh-doc :id} {:collection_id coll-id :last_viewed_at (fresh-instant)}+ :model/Transform {fresh-transform :id} {}]+ (scan/scan!)+ (let [stale-keys #{[:card stale-card-1] [:card stale-card-2] [:card stale-card-3]+ [:dashboard stale-dash-1] [:dashboard stale-dash-2]+ [:document stale-doc] [:document never-viewed-doc]+ [:transform never-ran-transform]}+ fresh-keys #{[:card fresh-card] [:dashboard fresh-dash]+ [:document fresh-doc] [:transform fresh-transform]}+ ;; recover this run's scan_id from a guaranteed-flagged temp entity — pins that the+ ;; persisted rows (not just scan!'s returned topline) carry the batch's scan_id+ scan-id (t2/select-one-fn :scan_id :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id stale-card-1+ :finding_type :stale)+ ;; :stale rows only - the same batch also carries the other checkers' findings (e.g. the+ ;; dashcardless fresh dashboard is legitimately imbalanced-empty)+ rows (t2/select :model/ContentDiagnosticsFinding :scan_id scan-id :finding_type :stale)+ found-keys (set (map (juxt :entity_type :entity_id) rows))]+ (testing "the run persisted one scan_id batch of findings"+ (is (string? scan-id))+ (is (seq rows))+ ;; a different entity type flagged in the same run shares the scan_id — one unified batch+ (is (= scan-id (t2/select-one-fn :scan_id :model/ContentDiagnosticsFinding+ :entity_type :dashboard :entity_id stale-dash-1))))+ (testing "every stale temp entity produced a :stale finding; no fresh one did"+ (is (every? found-keys stale-keys))+ (is (empty? (set/intersection found-keys fresh-keys))))+ (testing "persisted findings carry finding_type + scope_collection_id + last_active_at + details"+ (let [row (finding-for rows :card stale-card-1)]+ (is (=? {:finding_type :stale+ ;; scope_collection_id is stamped at scan time from the entity's collection+ :scope_collection_id coll-id+ ;; threshold derived from the setting, not a literal — a default change must not desync+ :details {:threshold_days (cd.settings/content-diagnostics-stale-threshold-days)}+ ;; last_active_at frozen from the card's last_used_at (top-level column, not in details)+ :last_active_at some?}+ row))+ ;; details holds ONLY the threshold+ (is (= #{:threshold_days} (set (keys (:details row)))))))+ (testing "dashboard finding freezes last_active_at from last_viewed_at (per-entity-type alias)"+ (let [row (finding-for rows :dashboard stale-dash-1)]+ (is (some? (:last_active_at row)))))+ (testing "never-used document (never viewed, created before the cutoff) lands with nil last_active_at"+ (let [row (finding-for rows :document never-viewed-doc)]+ (is (some? row))+ (is (nil? (:last_active_at row)))))+ (testing "never-ran transform (created before the cutoff) lands with nil last_active_at"+ (let [row (finding-for rows :transform never-ran-transform)]+ (is (some? row))+ (is (nil? (:last_active_at row)))))+ (testing "denormalized columns (entity_name/created_at/creator_id/creator_name) are stamped at scan time"+ (let [row (finding-for rows :card stale-card-1)]+ (is (=? {:entity_name some?+ :entity_created_at some?+ :entity_creator_id some?+ :entity_creator_name some?}+ row)))))))))++(deftest scan-stale-container-scoping-test+ (testing "stale candidates in ineligible containers are dropped; root-resident ones are kept"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [;; the stale query arms filter collection type but not is_sample - the content-diagnostics+ ;; container post-filter closes that gap+ :model/Collection {sample-coll :id} {:is_sample true}+ :model/Card {sample-card :id} {:collection_id sample-coll :last_used_at (stale-instant)}+ ;; root-resident stale card: the root is always an eligible container+ :model/Card {root-card :id} {:last_used_at (stale-instant)}+ ;; transforms are container-exempt: they run regardless of their folder's state, so a+ ;; never-run transform in an ARCHIVED folder is still a stale finding+ :model/Collection {archived-folder :id} {:namespace "transforms" :archived true}+ :model/Transform {shelved-transform :id} {:collection_id archived-folder+ :created_at (stale-instant)}]+ (let [scan-id (:scan_id (scan/scan!))+ stale-key? (t2/select-fn-set (juxt :entity_type :entity_id)+ :model/ContentDiagnosticsFinding+ :scan_id scan-id :finding_type :stale)]+ (is (not (contains? stale-key? [:card sample-card])))+ (is (contains? stale-key? [:card root-card]))+ (is (contains? stale-key? [:transform shelved-transform]))))))))++(deftest scan-excludes-document-internal-card-findings-test+ (testing "a card a document owns is never a card finding - whatever the checker - while its document is"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)+ card-name (str (scope-prefix) " Revenue")]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; the standalone original: stale, slow (30s > 10s), and 0-row - it stays flagged+ :model/Card {original :id} {:collection_id coll-id :name card-name+ :last_used_at (stale-instant)}+ :model/QueryExecution _ {:card_id original :started_at now :cache_hit false+ :parameterized false :running_time 30000 :result_rows 0}+ :model/Document {doc-id :id} {:collection_id coll-id+ :creator_id (mt/user->id :rasta)+ :content_type prose-mirror/prose-mirror-content-type+ :last_viewed_at (stale-instant)}+ ;; the document's own copy - same name, same signals, but owned by the document+ :model/Card {doc-card :id} {:collection_id coll-id :name card-name+ :document_id doc-id+ :last_used_at (stale-instant)}+ :model/QueryExecution _ {:card_id doc-card :started_at now :cache_hit false+ :parameterized false :running_time 30000 :result_rows 0}]+ ;; the body can only be written once the copy has an id, and `collection_id` rides along+ ;; because the document's after-update syncs it onto every card the document owns+ (t2/update! :model/Document doc-id+ {:collection_id coll-id+ :document {:type "doc"+ :content [{:type "cardEmbed" :attrs {:id doc-card}}]}})+ (let [scan-id (:scan_id (scan/scan!))+ rows (t2/select :model/ContentDiagnosticsFinding :scan_id scan-id)+ key? (into #{} (map (juxt :entity_type :entity_id :finding_type)) rows)]+ (testing "no checker flags the document's copy"+ (is (= #{} (into #{} (comp (filter #(and (= :card (:entity_type %))+ (= doc-card (:entity_id %))))+ (map :finding_type))+ rows))))+ (testing "the original still is - ownership is the exclusion, not the signals"+ (doseq [finding-type [:stale :slow :empty]]+ (is (contains? key? [:card original finding-type])+ (str "original missing its " finding-type " finding"))))+ (testing "neither card is duplicated - the copy leaves no dangling peer on the original"+ (is (not (contains? key? [:card original :duplicate_name]))))+ (testing "the document roll-up survives, still naming the copy as its culprit"+ (is (= [doc-card]+ (->> rows+ (m/find-first #(and (= :document (:entity_type %))+ (= doc-id (:entity_id %))+ (= :slow (:finding_type %))))+ :details+ :slow_entity_ids))))))))))))++(deftest scan-denormalizes-card-type-test+ (testing "scan! stamps each card finding's card_type column from report_card.type; non-card rows stay NULL"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {question-id :id} {:collection_id coll-id :type :question+ :last_used_at (stale-instant)}+ :model/Card {model-id :id} {:collection_id coll-id :type :model+ :last_used_at (stale-instant)}+ :model/Card {metric-id :id} {:collection_id coll-id :type :metric+ :last_used_at (stale-instant)}+ :model/Dashboard {dash-id :id} {:collection_id coll-id :last_viewed_at (stale-instant)}]+ (let [card-id-by-type {:question question-id :model model-id :metric metric-id}]+ (testing "the card fixtures cover every card type the app defines"+ ;; drives the per-type assertions below - a newly added card type fails here rather+ ;; than silently going uncovered+ (is (= queries.schema/card-types (set (keys card-id-by-type)))))+ (scan/scan!)+ (let [row-for (fn [etype eid]+ (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type etype :entity_id eid+ :finding_type :stale :invalidated_at nil))]+ (testing "each card finding stores its card's type verbatim"+ (doseq [[card-type card-id] card-id-by-type]+ (is (= card-type (:card_type (row-for :card card-id)))+ (str card-type " card finding should store its card_type"))))+ (testing "a non-card finding stores NULL card_type"+ (let [row (row-for :dashboard dash-id)]+ (is (some? row))+ (is (nil? (:card_type row))))))))))))++(deftest scan-denormalizes-collection-name-test+ (testing "scan stamps entity_collection_name from scope_collection_id (root rows get the site-locale root label)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [prefix (scope-prefix)+ coll-name (str prefix " Container")]+ (mt/with-temp [:model/Collection {coll-id :id} {:name coll-name}+ ;; same name in one collection → duplicated findings with a collection parent+ :model/Card {in-a :id} {:collection_id coll-id :name (str prefix " twin")}+ :model/Card {in-b :id} {:collection_id coll-id :name (str prefix " twin")}+ ;; same name at root → duplicated findings stamped with the root label+ :model/Card {root-a :id} {:collection_id nil :name (str prefix " rootless")}+ :model/Card {root-b :id} {:collection_id nil :name (str prefix " rootless")}+ ;; two top-level SAME-NAMED collections in the transforms namespace → duplicated+ ;; collection subjects at root (GDGT-2921 admits transforms-namespace collections+ ;; as duplicated subjects); their root label must be "Transforms", not the default+ :model/Collection {xf-a :id} {:name (str prefix " Pipeline") :namespace "transforms"}+ :model/Collection {xf-b :id} {:name (str prefix " Pipeline") :namespace "transforms"}]+ (scan/scan!)+ (let [by-entity (t2/select-fn->fn :entity_id :entity_collection_name+ :model/ContentDiagnosticsFinding+ :finding_type :duplicate_name+ :entity_type :card+ :entity_id [:in [in-a in-b root-a root-b]])+ ;; queried separately (entity_type-scoped) so a collection id can't collide with a card+ ;; id from a different sequence+ by-xf-coll (t2/select-fn->fn :entity_id :entity_collection_name+ :model/ContentDiagnosticsFinding+ :finding_type :duplicate_name+ :entity_type :collection+ :entity_id [:in [xf-a xf-b]])]+ (is (= coll-name (get by-entity in-a)))+ (is (= coll-name (get by-entity in-b)))+ (is (= "Our analytics" (get by-entity root-a)))+ (is (= "Our analytics" (get by-entity root-b)))+ (is (= "Transforms" (get by-xf-coll xf-a)))+ (is (= "Transforms" (get by-xf-coll xf-b))))))))))++(deftest scan-soft-invalidates-superseded-findings-test+ (testing "a fresh scan supersedes prior findings it no longer produces — via soft invalidation, not delete"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {resolved :id} {:collection_id coll-id :last_used_at (stale-instant)}+ :model/Card {still :id} {:collection_id coll-id :last_used_at (stale-instant)}]+ (scan/scan!) ; scan 1: both stale+ (t2/update! :model/Card resolved {:last_used_at (fresh-instant)}) ; resolved is now fresh+ (scan/scan!) ; scan 2: only `still` is stale+ (let [active (set (map :entity_id (t2/select :model/ContentDiagnosticsFinding+ :entity_type :card+ :entity_id [:in [resolved still]]+ :invalidated_at nil)))]+ (testing "the re-flagged card stays active; the resolved one does not"+ (is (contains? active still))+ (is (not (contains? active resolved))))+ (testing "the resolved card's prior finding is soft-invalidated (kept + timestamped), not deleted"+ (let [rows (t2/select :model/ContentDiagnosticsFinding :entity_type :card :entity_id resolved)]+ (is (seq rows)) ; not hard-deleted — history retained+ (is (every? :invalidated_at rows))))))))))++(defn- scan-task-history+ "The `content-diagnostics-scan` TaskHistory rows, oldest first."+ []+ (t2/select [:model/TaskHistory :status :task_details]+ :task "content-diagnostics-scan"+ {:order-by [[:id :asc]]}))++(deftest scan-job-gated-on-premium-feature-test+ (let [scans (atom 0)]+ (mt/with-dynamic-fn-redefs [scan/scan! (fn []+ (swap! scans inc)+ {:scan_id "scan-job-test-scan" :finding_count 0 :duration_ms 0})]+ (testing "the scheduled job body no-ops without the :content-diagnostics feature, recording the skip"+ (mt/with-model-cleanup [:model/TaskHistory]+ (mt/with-premium-features #{}+ (#'task.scan/scan-when-enabled!)+ (is (zero? @scans))+ (is (=? [{:status :success+ :task_details {:skipped-reason "content-diagnostics-disabled"+ :run-method "cron"}}]+ (scan-task-history))))))+ (testing "the scheduled job body scans when the feature is present, recording the scan id"+ (mt/with-model-cleanup [:model/TaskHistory]+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (#'task.scan/scan-when-enabled!)+ (is (= 1 @scans))+ (is (= [{:status :success+ :task_details {:scan-id "scan-job-test-scan"+ :run-method "cron"}}]+ (map #(into {} %) (scan-task-history))))))))))++(deftest scan-job-failure-recorded-in-task-history-test+ (testing "a scan that throws still fails the job, and its task_history row records the exception"+ (mt/with-model-cleanup [:model/TaskHistory]+ (mt/with-dynamic-fn-redefs [scan/scan! (fn [] (throw (ex-info "scan blew up" {})))]+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"scan blew up"+ (#'task.scan/scan-when-enabled!)))+ (is (=? [{:status :failed+ :task_details {:status "failed"+ :message "scan blew up"}}]+ (scan-task-history))))))))++;;; ------------------------------------------------ Upgrade backfill -----------------------------------------++(defn- backfill-scans!+ "Run the scan job's body for the backfill trigger under `features`, returning how many times it scanned."+ [features]+ (let [scans (atom 0)]+ (mt/with-dynamic-fn-redefs [scan/scan! (fn []+ (swap! scans inc)+ {:scan_id "backfill-test-scan" :finding_count 0 :duration_ms 0})]+ (mt/with-premium-features features+ (#'task.scan/scan-for-trigger! @#'task.scan/backfill-trigger-key)))+ @scans))++(defn- never-scanned?+ "Precondition for the backfill tests: the shared app DB holds no scan history and no findings."+ []+ (and (empty? (scan-task-history))+ (not (t2/exists? :model/ContentDiagnosticsFinding))))++(deftest backfill-scans-a-never-scanned-licensed-instance-test+ (mt/with-model-cleanup [:model/TaskHistory]+ (is (never-scanned?))+ (testing "scans once, recording the run under the scan's task name with its run method"+ (is (= 1 (backfill-scans! #{:content-diagnostics :advanced-permissions})))+ (is (= [{:status :success+ :task_details {:scan-id "backfill-test-scan"+ :run-method "upgrade-backfill"}}]+ (map #(into {} %) (scan-task-history)))))+ (testing "its own row suppresses a second backfill, so an all-clean instance doesn't re-scan every boot"+ (is (zero? (backfill-scans! #{:content-diagnostics :advanced-permissions})))+ (is (= 1 (count (scan-task-history)))))))++(deftest backfill-skips-without-the-feature-test+ (mt/with-model-cleanup [:model/TaskHistory]+ (is (never-scanned?))+ (is (zero? (backfill-scans! #{})))+ (testing "a skip decision writes no task_history row"+ (is (empty? (scan-task-history))))))++(deftest backfill-skips-when-findings-exist-test+ (doseq [[desc invalidated-at] {"an active finding" nil+ "only invalidated findings" (t/instant)}]+ (testing desc+ (mt/with-model-cleanup [:model/TaskHistory :model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Card {card-id :id} {}]+ (is (never-scanned?))+ (cd.tu/insert-finding! "old-scan" card-id invalidated-at)+ (is (zero? (backfill-scans! #{:content-diagnostics :advanced-permissions})))+ (is (empty? (scan-task-history))))))))++(deftest backfill-skips-after-a-real-scan-test+ (testing "a completed scheduled scan suppresses the backfill"+ (mt/with-model-cleanup [:model/TaskHistory]+ (is (never-scanned?))+ (mt/with-dynamic-fn-redefs [scan/scan! (constantly {:scan_id "cron-scan" :finding_count 0 :duration_ms 0})]+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (#'task.scan/scan-for-trigger! @#'task.scan/scan-trigger-key)))+ (is (=? [{:task_details {:run-method "cron"}}]+ (scan-task-history)))+ (is (zero? (backfill-scans! #{:content-diagnostics :advanced-permissions})))+ (is (= 1 (count (scan-task-history))))))+ (testing "an in-flight scan suppresses the backfill"+ (mt/with-model-cleanup [:model/TaskHistory]+ (is (never-scanned?))+ (t2/insert! :model/TaskHistory {:task "content-diagnostics-scan"+ :status :started+ :started_at (t/instant)})+ (is (zero? (backfill-scans! #{:content-diagnostics :advanced-permissions})))+ (is (= 1 (count (scan-task-history)))))))++(deftest backfill-ignores-feature-skipped-firings-test+ (testing "a scheduled firing skipped for the missing feature is not a prior scan"+ (mt/with-model-cleanup [:model/TaskHistory]+ (is (never-scanned?))+ (mt/with-premium-features #{}+ (#'task.scan/scan-when-enabled!))+ (is (=? [{:task_details {:skipped-reason "content-diagnostics-disabled"}}]+ (scan-task-history)))+ (is (= 1 (backfill-scans! #{:content-diagnostics :advanced-permissions}))))))++(deftest backfill-trigger-leaves-the-scheduled-scan-alone-test+ (testing "the backfill is a second trigger on the scan job, and re-initializing as nodes boot keeps the daily one"+ (mt/with-temp-scheduler!+ (task/init! ::task.scan/ContentDiagnosticsScan)+ (task/init! ::task.scan/ContentDiagnosticsScan)+ (let [scan-jobs (filter #(re-find #"content-diagnostics-scan" (:key %))+ (mt/scheduler-current-tasks))]+ (is (= ["metabase.task.content-diagnostics-scan.job"]+ (map :key scan-jobs)))+ (is (=? [{:key "metabase.task.content-diagnostics-scan-backfill.trigger"}+ {:key "metabase.task.content-diagnostics-scan.trigger"+ :cron-schedule "0 0 4 * * ? *"}]+ (sort-by :key (:triggers (first scan-jobs))))))))+ (testing "a node booting while the backfill is pending pushes it back, so it fires after the last boot"+ (mt/with-temp-scheduler!+ ;; the backfill is the scan job's one trigger without a cron `:schedule`+ (let [backfill-start #(:start-time (m/find-first (comp nil? :schedule)+ (:triggers (task/job-info task.scan/scan-job-key))))]+ (task/init! ::task.scan/ContentDiagnosticsScan)+ (let [first-start (backfill-start)]+ (with-redefs [task.scan/backfill-startup-delay-seconds 3600]+ (task/init! ::task.scan/ContentDiagnosticsScan))+ (is (pos? (compare (backfill-start) first-start))))))))++(deftest api-latest-per-entity-and-hydration-test+ (testing "GET /stale returns the latest valid finding per entity, batch-hydrated"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id+ :name "Quarterly Revenue"+ :creator_id (mt/user->id :rasta)}]+ ;; rasta reads this collection → the visibility filter scopes `total` to this card's findings+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ card-name (str prefix " Quarterly Revenue")+ insert (fn [scan threshold]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id scan+ :entity_type :card+ :entity_id card-id+ :finding_type :stale+ :last_active_at (t/offset-date-time 2025 9 1)+ :entity_name card-name+ :entity_creator_id (mt/user->id :rasta)+ :entity_creator_name "Rasta Toucan"+ :details {:threshold_days threshold}})))+ old-id (insert "scan-old" 30)+ new-id (insert "scan-new" 90)+ fetch #(mt/user-http-request :rasta :get 200 "ee/content-diagnostics/stale"+ :query prefix)+ fetched-ids (fn [resp] (set (map :id (:data resp))))]+ ;; membership assertions (robust to other rows / pagination), not absolute page counts+ (testing "the latest finding is returned, the superseded older one is not - and `total` counts only the latest"+ (let [resp (fetch)+ ids (fetched-ids resp)]+ (is (contains? ids new-id))+ (is (not (contains? ids old-id)))+ ;; total must exclude the older row even though it is stale=false: it isn't MAX(id) for the entity+ (is (= 1 (:total resp)))))+ (testing "the returned finding has flat identity + a nested typed details (collection, description, owner, creator)"+ (let [row (first (filter #(= new-id (:id %)) (:data (fetch))))]+ (testing "entity_display_name read from the denormalized entity_name"+ (is (= card-name (:entity_display_name row))))+ (testing "collection breadcrumb still live-hydrated (not denormalized)"+ (is (= coll-id (get-in row [:details :collection :id]))))+ (testing "threshold_days stays in details; last_active_at is hoisted to a top-level property"+ (is (= 90 (get-in row [:details :threshold_days])))+ (is (some? (:last_active_at row)))+ (is (not (contains? (:details row) :last_active_at))))+ (testing "creator read from the denormalized columns (id + common_name, no live hydrate)"+ (is (=? {:id (mt/user->id :rasta)+ :name "Rasta Toucan"+ :type "user"}+ (get-in row [:details :creator]))))+ (testing "card has no owner column → owner is null"+ (is (nil? (get-in row [:details :owner]))))))+ (testing "invalidating the latest hides the entity AND drops it from total; older row does NOT resurface"+ (t2/update! :model/ContentDiagnosticsFinding new-id {:invalidated_at (t/offset-date-time)})+ (let [resp (fetch)+ ids (fetched-ids resp)]+ (is (not (contains? ids new-id)))+ (is (not (contains? ids old-id)))+ ;; total excludes the invalidated latest, and does not fall back to the older row+ (is (= 0 (:total resp))))))))))))++(deftest api-stale-card-with-legacy-metadata-test+ (testing "GET /stale checks Card write permissions without reading legacy result metadata"+ (mt/with-premium-features #{:content-diagnostics}+ (let [prefix (scope-prefix)]+ (mt/with-temp [:model/Card {card-id :id} {}+ :model/ContentDiagnosticsFinding {finding-id :id}+ {:scan_id "legacy-metadata"+ :entity_type :card+ :entity_id card-id+ :entity_name prefix+ :finding_type :stale+ :details {}}]+ (let [original-metadata (:result_metadata (t2/query-one {:select [:result_metadata]+ :from [:report_card]+ :where [:= :id card-id]}))]+ ;; Bypass Card's write transforms to reproduce stored metadata from older versions.+ (t2/query {:update :report_card+ :set {:result_metadata (json/encode [{:name "legacy_field"+ :display_name "Legacy Field"+ :base_type "type/Integer"+ :effective_type "type/Integer"+ :field_ref ["field" 1 nil]+ :table_id 1}])}+ :where [:= :id card-id]})+ (try+ (let [rows (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale" :query prefix))]+ (is (=? {:id finding-id+ :entity_id card-id+ :can_write true}+ (some #(when (= finding-id (:id %)) %) rows))))+ (finally+ (t2/query {:update :report_card+ :set {:result_metadata original-metadata}+ :where [:= :id card-id]})))))))))++(deftest api-stale-card-write-permissions-are-batched-test+ (testing "GET /stale does not fetch each Card to resolve its parent Document for write permissions"+ (mt/with-premium-features #{:content-diagnostics}+ (let [prefix (scope-prefix)+ finding (fn [card-id]+ {:scan_id "batched-card-permissions"+ :entity_type :card+ :entity_id card-id+ :entity_name prefix+ :finding_type :stale+ :details {}})+ fetch #(mt/user-http-request :crowberto :get 200 "ee/content-diagnostics/stale" :query prefix)+ count-fetch (fn []+ (t2/with-call-count [call-count]+ (fetch)+ (call-count)))]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {first-card-id :id} {:collection_id coll-id}+ :model/ContentDiagnosticsFinding {first-finding-id :id} (finding first-card-id)]+ (fetch) ; warm one-time request setup outside the measured window+ (let [one-card-calls (count-fetch)]+ (mt/with-temp [:model/Card {second-card-id :id} {:collection_id coll-id}+ :model/ContentDiagnosticsFinding {second-finding-id :id} (finding second-card-id)]+ (is (= #{first-finding-id second-finding-id}+ (set (map :id (:data (fetch))))))+ (is (= one-card-calls (count-fetch))))))))))++(deftest api-include-personal-collections-test+ (testing "GET /stale excludes personal-collection findings by default; includes them with the param"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; rasta's permanent personal collection - never with-temp (a personal collection can't be deleted)+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))]+ (mt/with-temp [:model/Collection {reg-id :id} {}+ :model/Card {reg-card :id} {:collection_id reg-id}+ :model/Card {pers-card :id} {:collection_id pers-id}]+ ;; rasta can read the regular collection; their own personal collection is always self-visible+ (perms/grant-collection-read-permissions! (perms/all-users-group) reg-id)+ (let [prefix (scope-prefix)+ insert (fn [card]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "p-scan"+ :entity_type :card+ :entity_id card+ :entity_name (str prefix "-" card)+ :finding_type :stale+ :details {}})))+ reg-fid (insert reg-card)+ pers-fid (insert pers-card)+ fetch (fn [& kvs] (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale" :query prefix kvs))+ fetched-ids (fn [resp] (set (map :id (:data resp))))]+ (testing "default (param omitted) → personal-collection finding excluded, regular one returned"+ (let [resp (fetch)]+ (is (contains? (fetched-ids resp) reg-fid))+ (is (not (contains? (fetched-ids resp) pers-fid)))+ ;; total honors the live filter - only the regular finding counts+ (is (= 1 (:total resp)))))+ (testing "include-personal-collections=false → identical to default"+ (let [ids (fetched-ids (fetch :include-personal-collections false))]+ (is (contains? ids reg-fid))+ (is (not (contains? ids pers-fid)))))+ (testing "include-personal-collections=true → both returned, total counts both"+ (let [resp (fetch :include-personal-collections true)]+ (is (contains? (fetched-ids resp) reg-fid))+ (is (contains? (fetched-ids resp) pers-fid))+ (is (= 2 (:total resp)))))+ (testing "exclusion is live: moving the card out of the personal collection re-surfaces it"+ (t2/update! :model/Card pers-card {:collection_id reg-id})+ (let [ids (fetched-ids (fetch))] ; default (exclude personal)+ (is (contains? ids pers-fid)))))))))))) ; now in a regular collection → returned++(deftest api-personal-collection-exclusion-spans-descendants-test+ (testing "the personal-collection exclusion reaches nested descendants, and nothing outside them"+ ;; The exclusion is a non-correlated subquery that matches the first segment of `location` against the+ ;; personal roots, so depth and over-matching are the two ways it could go wrong: a card two levels+ ;; under a personal collection must still be excluded, and one at the same depth under a regular+ ;; collection must not be. NB this pins semantic equivalence with the old LIKE-based matcher, which+ ;; also handled any depth - the bind-parameter blowup the rewrite actually fixes is pinned by+ ;; `personal-collection-exclusion-binds-a-constant-param-count-test` below.+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))]+ (mt/with-temp [:model/Collection {p-sub :id} {:location (str "/" pers-id "/")}+ :model/Collection {p-subsub :id} {:location (str "/" pers-id "/" p-sub "/")}+ :model/Collection {reg :id} {}+ :model/Collection {reg-sub :id} {:location (str "/" reg "/")}+ :model/Card {deep-personal :id} {:collection_id p-subsub}+ :model/Card {deep-regular :id} {:collection_id reg-sub}+ :model/Card {root-resident :id} {:collection_id nil}]+ (let [prefix (scope-prefix)+ insert! (fn [card] (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "pd" :entity_type :card :entity_id card+ :entity_name (str prefix "-" card)+ :finding_type :stale :details {}}))+ listed (fn [& kvs] (into #{} (map :entity_id)+ (:data (apply mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale" :query prefix kvs))))]+ (run! insert! [deep-personal deep-regular root-resident])+ (testing "default: the personal descendant two levels down is excluded, while a same-depth+ regular descendant and a root-resident card are kept"+ (is (= #{deep-regular root-resident} (listed))))+ (testing "include-personal-collections=true → the deep personal card comes back"+ (is (= #{deep-personal deep-regular root-resident}+ (listed :include-personal-collections true)))))))))))++(deftest personal-collection-exclusion-binds-a-constant-param-count-test+ (testing "the personal-collection exclusion inlines no collection ids, so its bind-parameter count cannot+ grow with the number of personal collections"+ ;; The defect behind the subquery rewrite was 5N inlined bind parameters crossing Postgres's 65,535+ ;; limit at roughly 13k personal collections. That is not reproducible in CI, so pin the property that+ ;; makes it impossible instead: the clause is data-independent. A reintroduced id set would grow these+ ;; params and fail here, which `api-personal-collection-exclusion-spans-descendants-test` cannot do -+ ;; the old LIKE matcher handled any depth too, so that test is a semantic-equivalence guard only.+ (let [params #(rest (sql/format {:select [:*]+ :from [:content_diagnostics_finding]+ :where (api.common/exclude-personal-collections-clause true)}))+ baseline (params)]+ (mt/with-temp [:model/User {user-id :id} {}]+ (collection/user->personal-collection user-id)+ (is (= baseline (params)))))))++(deftest api-paginates-test+ (testing "GET /stale honors limit/offset and reports the full valid total"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; real cards in a real collection - the read layer permission-filters against live entities+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c1 :id} {:collection_id coll-id}+ :model/Card {c2 :id} {:collection_id coll-id}+ :model/Card {c3 :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)]+ (doseq [cid [c1 c2 c3]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "p" :entity_type :card :entity_id cid+ :entity_name (str prefix "-" cid)+ :finding_type :stale :details {}}))+ (let [page (fn [limit offset]+ (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/stale"+ :query prefix :limit limit :offset offset))]+ (testing "limit caps the page; total reflects the full valid set; limit/offset echoed back"+ (let [r (page 2 0)]+ (is (= 2 (count (:data r))))+ (is (= 3 (:total r)))+ (is (= 2 (:limit r)))+ (is (= 0 (:offset r)))))+ (testing "offset advances to the remainder"+ (is (= 1 (count (:data (page 2 2))))))))))))))++(deftest api-permission-filtered-test+ (testing "GET /stale returns only findings whose entity the current user can read"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {readable :id} {}+ :model/Collection {unreadable :id} {}+ :model/Card {r-card :id} {:collection_id readable}+ :model/Card {u-card :id} {:collection_id unreadable}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) readable)+ (let [insert (fn [card]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "perm"+ :entity_type :card+ :entity_id card+ :finding_type :stale+ :details {}})))+ r-fid (insert r-card)+ u-fid (insert u-card)+ ids-for (fn [user] (set (map :id (:data (mt/user-http-request+ user :get 200 "ee/content-diagnostics/stale")))))]+ (testing "non-admin sees only the finding in the readable collection"+ (let [ids (ids-for :rasta)]+ (is (contains? ids r-fid))+ (is (not (contains? ids u-fid)))))+ (testing "superuser sees findings in every collection"+ (let [ids (ids-for :crowberto)]+ (is (contains? ids r-fid))+ (is (contains? ids u-fid)))))))))))++(deftest api-sort-test+ (testing "GET /stale honors sort-column + sort-direction (native columns only)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ ;; card flagged later than the dashboard → detected-at order ≠ entity-type order (each isolable)+ (let [prefix (scope-prefix)+ card-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "s" :entity_type :card :entity_id card-id+ :entity_name (str prefix "-" card-id)+ :entity_kind :question+ :finding_type :stale :details {}+ :detected_at (t/offset-date-time 2025 6 1)}))+ dash-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "s" :entity_type :dashboard :entity_id dash-id+ :entity_name (str prefix "-" dash-id)+ :entity_kind :dashboard+ :finding_type :stale :details {}+ :detected_at (t/offset-date-time 2025 1 1)}))+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs))))]+ (testing "sort-column=entity-type - flat kind lexical: dashboard < question"+ (is (= [dash-fid card-fid] (order :sort-column "entity-type" :sort-direction "asc")))+ (is (= [card-fid dash-fid] (order :sort-column "entity-type" :sort-direction "desc"))))+ (testing "sort-column=detected-at - dashboard (Jan) before card (Jun)"+ (is (= [dash-fid card-fid] (order :sort-column "detected-at" :sort-direction "asc")))+ (is (= [card-fid dash-fid] (order :sort-column "detected-at" :sort-direction "desc"))))+ (testing "default sort = detected-at asc"+ (is (= [dash-fid card-fid] (order)))))))))))++(deftest api-sort-by-flat-entity-type-test+ (testing "GET /stale sort-column=entity-type orders card sub-kinds as peers (dashboard < model < question)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {question-id :id} {:collection_id coll-id}+ :model/Card {model-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [row]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ (merge {:scan_id "s" :finding_type :stale+ :details {}}+ row))))+ ;; ids inserted in an order that differs from the expected sort, to isolate the column+ question-fid (insert {:entity_type :card :entity_id question-id :card_type :question+ :entity_kind :question :entity_name (str prefix " q")})+ dash-fid (insert {:entity_type :dashboard :entity_id dash-id+ :entity_kind :dashboard :entity_name (str prefix " d")})+ model-fid (insert {:entity_type :card :entity_id model-id :card_type :model+ :entity_kind :model :entity_name (str prefix " m")})+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs))))]+ (is (= [dash-fid model-fid question-fid]+ (order :sort-column "entity-type" :sort-direction "asc")))+ (is (= [question-fid model-fid dash-fid]+ (order :sort-column "entity-type" :sort-direction "desc"))))))))))++(deftest api-sort-by-entity-attrs-test+ (testing "GET /stale sorts by denormalized entity columns (name / created-at / created-by / last-active-at)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id}+ :model/Card {card-b :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ ;; each attr orders A,B differently so the column under test is isolated+ ;; creator NAME order (Amy < Zoe) is the inverse of creator ID order (5 < 10) → isolates name-sort+ (let [prefix (scope-prefix)+ insert (fn [row]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ (merge {:scan_id "s"+ :entity_type :card+ :finding_type :stale+ :details {}}+ row))))+ ;; lowercase-first "alpha" (vs capitalized "Beta") pins CASE-INSENSITIVE ordering: a+ ;; bytewise sort would put "Beta" (B = 0x42) before "alpha" (a = 0x61), the opposite of+ ;; the asserted order+ a-fid (insert {:entity_id card-a+ :entity_name (str prefix " alpha")+ :entity_created_at (t/offset-date-time 2025 1 1)+ :entity_creator_id 10+ :entity_creator_name "amy"+ :last_active_at (t/offset-date-time 2025 6 1)})+ b-fid (insert {:entity_id card-b+ :entity_name (str prefix " Beta")+ :entity_created_at (t/offset-date-time 2025 6 1)+ :entity_creator_id 5+ :entity_creator_name "Zoe"+ :last_active_at (t/offset-date-time 2025 1 1)})+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs))))]+ (testing "name - alpha < Beta, case-insensitive"+ (is (= [a-fid b-fid] (order :sort-column "name" :sort-direction "asc")))+ (is (= [b-fid a-fid] (order :sort-column "name" :sort-direction "desc"))))+ (testing "created-at - Jan before Jun"+ (is (= [a-fid b-fid] (order :sort-column "created-at" :sort-direction "asc"))))+ (testing "created-by - amy < Zoe, case-insensitive (a bytewise sort puts Zoe first),+ independent of the 10-vs-5 id order"+ (is (= [a-fid b-fid] (order :sort-column "created-by" :sort-direction "asc")))+ (is (= [b-fid a-fid] (order :sort-column "created-by" :sort-direction "desc"))))+ (testing "last-active-at - Jan before Jun"+ (is (= [b-fid a-fid] (order :sort-column "last-active-at" :sort-direction "asc")))))))))))++(deftest api-sort-by-collection-name-test+ (testing "GET /stale sorts by the denormalized entity_collection_name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id}+ :model/Card {card-b :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [row]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ (merge {:scan_id "s" :entity_type :card+ :finding_type :stale :details {}}+ row))))+ ;; collection order (alpha < Beta) inverts entity-name order so the column under test is+ ;; isolated. Lowercase-first fixture pins CASE-INSENSITIVE ordering: a bytewise+ ;; sort would put "Beta" (B = 0x42) before "alpha" (a = 0x61).+ a-fid (insert {:entity_id card-b+ :entity_name (str prefix " b")+ :entity_collection_name "alpha collection"})+ b-fid (insert {:entity_id card-a+ :entity_name (str prefix " a")+ :entity_collection_name "Beta Collection"})+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs))))]+ (is (= [a-fid b-fid] (order :sort-column "collection-name" :sort-direction "asc")))+ (is (= [b-fid a-fid] (order :sort-column "collection-name"+ :sort-direction "desc"))))))))))++(deftest api-entity-types-filter-test+ (testing "GET /stale filters by entity-types (repeatable; omitted = all)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ ;; transforms only go in the :transforms collection namespace+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {card-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/Document {doc-id :id} {:collection_id coll-id}+ :model/Transform {transform-id :id} {:collection_id tcoll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (perms/grant-collection-read-permissions! (perms/all-users-group) tcoll-id)+ (let [prefix (scope-prefix)+ insert (fn [etype eid]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "e" :entity_type etype :entity_id eid+ :entity_name (str prefix "-" eid)+ :entity_kind etype+ :finding_type :stale :details {}})))+ card-fid (insert :card card-id)+ dash-fid (insert :dashboard dash-id)+ doc-fid (insert :document doc-id)+ transform-fid (insert :transform transform-id)+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs)))))]+ (testing "omitted → all entity types"+ (is (= #{card-fid dash-fid doc-fid transform-fid} (ids))))+ (testing "single value → only that type"+ (is (= #{card-fid} (ids :entity-types "card")))+ (is (= #{dash-fid} (ids :entity-types "dashboard")))+ ;; also exercises document hydration (no description column → returned description: nil)+ (is (= #{doc-fid} (ids :entity-types "document")))+ (is (= #{transform-fid} (ids :entity-types "transform"))))+ (testing "multiple values → union of the given types"+ (is (= #{card-fid dash-fid} (ids :entity-types ["card" "dashboard"])))))))))))++(deftest api-card-type-test+ (testing "GET /stale serves each card finding's stored card_type as a top-level field - card findings only"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ ;; transforms only go in the :transforms collection namespace+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {question-id :id} {:collection_id coll-id :type :question}+ :model/Card {model-id :id} {:collection_id coll-id :type :model}+ :model/Card {metric-id :id} {:collection_id coll-id :type :metric}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/Document {doc-id :id} {:collection_id coll-id}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (let [prefix (scope-prefix)+ card-id-by-type {:question question-id :model model-id :metric metric-id}+ insert! (fn [etype eid & [card-type]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "ct"+ :entity_type etype+ :entity_id eid+ :entity_name (str prefix "-" (name etype) "-" eid)+ :finding_type :stale+ ;; what scan-time denormalization stamps (nil for non-cards)+ :card_type card-type+ :details {}}))]+ (testing "the card fixtures cover every card type the app defines"+ ;; drives the per-type assertions below - a newly added card type fails here rather+ ;; than silently going uncovered+ (is (= queries.schema/card-types (set (keys card-id-by-type)))))+ (doseq [[card-type card-id] card-id-by-type]+ (insert! :card card-id card-type))+ (doseq [[etype eid] [[:dashboard dash-id] [:document doc-id] [:transform xform-id]]]+ (insert! etype eid))+ (let [by-id (into {} (map (juxt (juxt :entity_type :entity_id) identity))+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale" :query prefix)))]+ (testing "every card type is served verbatim; entity_type stays \"card\""+ (doseq [[card-type card-id] card-id-by-type]+ (is (=? {:entity_type "card" :card_type (name card-type)} (by-id ["card" card-id]))+ (str card-type " finding should carry its card_type"))))+ (testing "dashboard/document/transform findings carry no card_type key at all"+ (doseq [k [["dashboard" dash-id] ["document" doc-id] ["transform" xform-id]]]+ (let [finding (get by-id k)]+ (is (some? finding) (str k " should be served"))+ (is (not (contains? finding :card_type)))))))))))))++(deftest api-entity-types-card-sub-kinds-test+ (testing "GET /stale entity-types takes the card sub-kinds as peers of the other entity types"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {question-id :id} {:collection_id coll-id :type :question}+ :model/Card {model-id :id} {:collection_id coll-id :type :model}+ :model/Card {metric-id :id} {:collection_id coll-id :type :metric}+ ;; stands in for a pre-migration finding row (NULL card_type)+ :model/Card {legacy-id :id} {:collection_id coll-id :type :question}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}]+ (let [prefix (scope-prefix)+ insert! (fn [etype eid card-type]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "ctf" :entity_type etype+ :entity_id eid+ :entity_name (str prefix "-" eid)+ :card_type card-type+ :entity_kind (or card-type etype)+ :finding_type :stale :details {}})))+ question-fid (insert! :card question-id :question)+ model-fid (insert! :card model-id :model)+ metric-fid (insert! :card metric-id :metric)+ legacy-fid (insert! :card legacy-id nil)+ dash-fid (insert! :dashboard dash-id nil)+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs)))))]+ (testing "omitted → all findings, including the NULL-card_type legacy row"+ (is (= #{question-fid model-fid metric-fid legacy-fid dash-fid} (ids))))+ (testing "a sub-kind selects only cards of that type - no other entity type rides along"+ (is (= #{model-fid} (ids :entity-types "model"))))+ (testing "sub-kinds and plain entity types compose as peers in one list"+ (is (= #{model-fid dash-fid} (ids :entity-types ["model" "dashboard"]))))+ (testing "several sub-kinds union"+ (is (= #{question-fid metric-fid} (ids :entity-types ["question" "metric"]))))+ (testing "`card` means any card type, keeping a NULL card_type row that a sub-kind drops"+ (is (= #{question-fid model-fid metric-fid legacy-fid} (ids :entity-types "card")))+ ;; the canonical all-types list, so a newly added card type can't silently weaken this+ (is (not (contains? (ids :entity-types (mapv name queries.schema/card-types)) legacy-fid))))+ (testing "`card` subsumes a sub-kind listed alongside it"+ (is (= #{question-fid model-fid metric-fid legacy-fid}+ (ids :entity-types ["card" "model"]))))))))))++(deftest api-transform-owner-hydration-test+ (testing "GET /stale hydrates the transform owner - a Metabase user or an external email, exclusively"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [;; transforms only go in the :transforms collection namespace+ :model/Collection {coll-id :id} {:namespace "transforms"}+ :model/Transform {owned-id :id} {:collection_id coll-id+ :owner_user_id (mt/user->id :rasta)}+ :model/Transform {external-id :id} {:collection_id coll-id+ :owner_email "[email protected]"}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [tid]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "o" :entity_type :transform :entity_id tid+ :entity_name (str prefix "-" tid)+ :finding_type :stale :details {}})))+ owned-fid (insert owned-id)+ external-fid (insert external-id)+ rows (:data (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/stale"+ :query prefix))+ owner-for (fn [fid] (get-in (first (filter #(= fid (:id %)) rows)) [:details :owner]))]+ (testing "owner_user_id → full user object"+ (is (=? {:id (mt/user->id :rasta)+ :name "Rasta Toucan"+ :email "[email protected]"+ :type "user"}+ (owner-for owned-fid))))+ (testing "owner_email (no Metabase account) → external"+ (is (=? {:email "[email protected]"+ :type "external"}+ (owner-for external-fid)))))))))))++(deftest api-archived-folder-transform-test+ (testing "GET /stale serves transforms in archived folders - archiving a folder neither archives nor stops its transforms - while archived-folder cards stay hidden"+ ;; The unprivileged actor is a `:monitoring` grantee rather than the suite's usual `:rasta`: the+ ;; namespace fixture makes rasta an analyst, and an analyst reads every `transforms`-namespace+ ;; collection outright (`collection/visible-collection-query`) - which is the very thing the last+ ;; assertion denies. `:monitoring` clears the endpoint gate without touching collection visibility.+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-user-in-groups [group {:name "Content Diagnostics Monitoring"}+ unprivileged [group]]+ (perms/grant-application-permissions! group :monitoring)+ (mt/with-temp [:model/Collection {xf-folder :id} {:name "Shelved pipelines"+ :namespace "transforms"+ :archived true}+ :model/Transform {xf-id :id} {:collection_id xf-folder}+ :model/Collection {card-folder :id} {:archived true}+ :model/Card {card-id :id} {:collection_id card-folder}]+ (let [prefix (scope-prefix)+ insert! (fn [etype eid]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "af" :entity_type etype :entity_id eid+ :entity_name (str prefix "-" eid)+ :finding_type :stale :details {}})))+ xf-fid (insert! :transform xf-id)+ card-fid (insert! :card card-id)+ rows-for (fn [user] (:data (mt/user-http-request user :get 200+ "ee/content-diagnostics/stale" :query prefix)))+ rows (rows-for :crowberto)+ ids (set (map :id rows))]+ (testing "the archived-folder transform finding is served"+ (is (contains? ids xf-fid)))+ (testing "its breadcrumb names the archived folder"+ (is (=? {:id xf-folder+ :name "Shelved pipelines"+ :namespace "transforms"}+ (get-in (finding-for rows "transform" xf-id) [:details :collection]))))+ (testing "a card in an archived folder stays hidden"+ (is (not (contains? ids card-fid))))+ (testing "archived-folder inclusion does not bypass collection permissions"+ (is (not (contains? (set (map :id (rows-for unprivileged))) xf-fid))))))))))))++(deftest api-transform-findings-gate-on-analyst-role-test+ (testing "GET /stale serves a transform finding to any data analyst, source-table perms or not"+ ;; The chosen read model for transform findings is the analyst role plus an enabled transforms feature -+ ;; the two conjuncts `transforms.u/enabled-source-types-for-user` gates on, used by `GET /api/transform`+ ;; and by a collection's items. Neither of those lists applies the source-table check, so hiding here+ ;; would make the monitor stricter than the feature it monitors and would make `total` disagree with the+ ;; page. The source-table check stays on the peer hydrator, covered by+ ;; `duplicated-api-transform-peers-hydrate-test`, which also pins that a non-analyst `:monitoring`+ ;; grantee gets no transform finding at all.+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Transform {xf-id :id} {}]+ (let [prefix (scope-prefix)+ fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "src"+ :entity_type :transform+ :entity_id xf-id+ :entity_name (str prefix "-" xf-id)+ :finding_type :stale+ :details {}}))+ resp (fn [user] (mt/user-http-request user :get 200 "ee/content-diagnostics/stale"+ :query prefix))]+ (testing "a superuser reads every transform"+ (let [r (resp :crowberto)]+ (is (= #{fid} (set (map :id (:data r)))))+ (is (= 1 (:total r)))))+ ;; the suite fixture makes rasta an analyst, so only the source-table half differs here+ (testing "an analyst blocked from the source database still sees it, and `total` agrees"+ (mt/with-db-perm-for-group! (perms/all-users-group) (mt/id) :perms/view-data :blocked+ (let [r (resp :rasta)]+ (is (= #{fid} (set (map :id (:data r)))))+ (is (= 1 (:total r))))))))))))++(deftest api-transform-findings-need-the-transforms-feature-test+ (testing "GET /stale withholds transform findings when transforms are feature-disabled, even from a superuser"+ ;; matches the rest of the product: with transforms off, `GET /api/transform` 403s and a collection+ ;; lists none+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Transform {xf-id :id} {}]+ (let [prefix (scope-prefix)+ _ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "feat"+ :entity_type :transform+ :entity_id xf-id+ :entity_name (str prefix "-" xf-id)+ :finding_type :stale+ :details {}})+ listed (fn [] (into #{} (map :entity_id)+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale"+ :query prefix))))]+ (testing "transforms enabled: the finding is served"+ (mt/with-premium-features #{:content-diagnostics :transforms-basic :hosting}+ (is (= #{xf-id} (listed)))))+ (testing "transforms disabled: it is withheld, though the row is still in the table"+ (mt/with-premium-features #{:content-diagnostics}+ (is (= #{} (listed))))+ (is (some? (t2/select-one-pk :model/ContentDiagnosticsFinding+ :entity_type "transform" :entity_id xf-id)))))))))++(deftest api-threshold-days-filter-test+ (testing "GET /stale threshold-days drops findings less stale than the cutoff; never-used always passes"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c-old :id} {:collection_id coll-id}+ :model/Card {c-recent :id} {:collection_id coll-id}+ :model/Card {c-never :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [card active-at]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "t" :entity_type :card :entity_id card+ :entity_name (str prefix "-" card)+ :finding_type :stale :details {}+ :last_active_at active-at})))+ old-fid (insert c-old (t/minus (t/offset-date-time) (t/days 400)))+ recent-fid (insert c-recent (t/minus (t/offset-date-time) (t/days 100)))+ never-fid (insert c-never nil)+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix kvs)))))]+ (testing "no threshold → all three"+ (is (= #{old-fid recent-fid never-fid} (ids))))+ (testing "threshold-days=200 → keeps the 400-day-old + never-used, drops the 100-day-old"+ (is (= #{old-fid never-fid} (ids :threshold-days "200"))))+ (testing "threshold-days=50 → all qualify (each ≥50 days stale, or never used)"+ (is (= #{old-fid recent-fid never-fid} (ids :threshold-days "50")))))))))))++(deftest api-query-search-test+ (testing "GET /stale ?query= case-insensitively substring-matches the denormalized entity name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c-rev :id} {:collection_id coll-id}+ :model/Card {c-cost :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [card nm]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "q" :entity_type :card :entity_id card+ :finding_type :stale :details {}+ :entity_name (str prefix " " nm)})))+ rev-fid (insert c-rev "Quarterly Revenue")+ cost-fid (insert c-cost "Cost Analysis")+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale" kvs)))))]+ ;; the no-query/blank cases can't be prefix-scoped (that's what they test) → membership, not+ ;; exact sets, so rows from other tests on the shared findings table can't break them+ (testing "no query → all findings"+ (is (set/subset? #{rev-fid cost-fid} (ids))))+ (testing "substring match, case-insensitive"+ (is (= #{rev-fid} (ids :query (u/upper-case-en (str prefix " quarterly rev")))))+ (is (= #{cost-fid} (ids :query (u/lower-case-en (str prefix " cost"))))))+ (testing "no match → empty"+ (is (empty? (ids :query (str prefix " zzz")))))+ (testing "blank query is a no-op → all findings"+ (is (set/subset? #{rev-fid cost-fid} (ids :query " ")))))))))))++(deftest api-response-serves-entity-kind-test+ (testing "findings carry additive entity_kind; the entity_type/card_type pair is unchanged"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id :type :model}+ :model/Card {card-b :id} {:collection_id coll-id :type :model}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [row] (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ (merge {:scan_id "s" :entity_type :card+ :finding_type :stale :details {}}+ row))))+ kind-fid (insert {:entity_id card-a :card_type :model :entity_kind :model+ :entity_name (str prefix " new-row")})+ ;; pre-migration shape: entity_kind NULL, card_type present - coalesce covers it+ old-fid (insert {:entity_id card-b :card_type :model+ :entity_name (str prefix " old-row")})+ by-id (m/index-by :id (:data (mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/stale"+ :query prefix)))]+ (is (= "model" (get-in by-id [kind-fid :entity_kind])))+ (is (= "model" (get-in by-id [old-fid :entity_kind])))+ ;; the shipped pair is untouched+ (is (= "card" (get-in by-id [kind-fid :entity_type])))+ (is (= "model" (get-in by-id [kind-fid :card_type]))))))))))++(deftest api-response-serves-collection-name-test+ (testing "top-level collection_name = scan-time stored name; nil for pre-migration (NULL-column) rows"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card-a :id} {:collection_id coll-id}+ :model/Card {card-b :id} {:collection_id nil}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [row] (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ (merge {:scan_id "s" :entity_type :card+ :finding_type :stale :details {}}+ row))))+ in-fid (insert {:entity_id card-a :entity_name (str prefix " in")+ :scope_collection_id coll-id+ :entity_collection_name "Scan Time Name"})+ ;; pre-migration shape: NULL entity_collection_name on a root-resident row - the+ ;; breadcrumb (root sentinel) passes the gate, the nil comes from the column.+ ;; Post-migration scans stamp root rows with the root label (pinned by+ ;; scan-denormalizes-collection-name-test).+ ;; Queried as :crowberto - the root-resident row is excluded from :rasta's response+ ;; entirely (root perms are stripped by with-non-admin-groups-no-root-collection-perms),+ ;; which would make the nil assertion vacuous rather than exercising the gate.+ old-fid (insert {:entity_id card-b :entity_name (str prefix " root")})+ by-id (m/index-by :id (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale"+ :query prefix)))]+ (is (= "Scan Time Name" (get-in by-id [in-fid :collection_name])))+ ;; presence first - `get-in` on a missing key is indistinguishable from a present nil value,+ ;; so without this the nil assertion below would pass vacuously if the row were dropped+ (is (some? (get by-id old-fid)))+ (is (nil? (get-in by-id [old-fid :collection_name]))))))))))++(deftest api-drops-archived-entities-without-a-rescan-test+ (testing "GET /stale drops a card archived after the scan, even when no card event fired"+ ;; Invalidation rides :event/card-update, but some archive paths publish none: archiving a dashboard+ ;; archives its dashboard questions through one bulk t2/update! in+ ;; `dashboards.db/archive-dashboard-questions!`, and so does removing a question from its dashboard.+ ;; The serve gate therefore has to check `archived` itself rather than trust the event.+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll :id} {}+ :model/Dashboard {dash :id} {:collection_id coll}+ :model/Card {dq :id} {:collection_id coll :dashboard_id dash}+ :model/Card {plain :id} {:collection_id coll}]+ (let [prefix (scope-prefix)+ insert! (fn [card-id suffix]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "s" :entity_type :card :entity_id card-id+ :finding_type :stale :details {:threshold_days 90}+ :entity_name (str prefix " " suffix)}))+ listed (fn [] (into #{} (map :entity_id)+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/stale"+ :query prefix))))]+ (insert! dq "dq")+ (insert! plain "plain")+ (testing "both findings are served while the cards are active"+ (is (= #{dq plain} (listed))))+ ;; exactly what archive-dashboard-questions! does - a bulk update, so no :event/card-update+ (t2/update! :model/Card :id dq {:archived true :archived_directly false})+ (testing "the archived dashboard question drops out immediately; its sibling is untouched"+ (is (= #{plain} (listed))))))))))++(deftest scan-survives-unbounded-upstream-names-test+ (testing "a name longer than any varchar cap round-trips: the denormalized columns are text, not truncated"+ ;; collection/document/transform names are `text` upstream and a creator's common_name is first+last+ ;; (509) or the email (citext on Postgres), so a cap on entity_name/entity_creator_name would throw in+ ;; the insert stage - leaving the already-committed chunks active, invalidate-superseded! unrun, and+ ;; every later scan failing the same way.+ (mt/with-premium-features #{:content-diagnostics}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [long-name (str (scope-prefix) " " (apply str (repeat 300 "n")))+ first-name (apply str (repeat 254 "f"))+ last-name (apply str (repeat 254 "l"))]+ (mt/with-temp [:model/User {creator :id} {:first_name first-name :last_name last-name}+ ;; an empty collection, so the imbalanced checker flags the collection itself and+ ;; its name lands in entity_name+ :model/Collection {coll :id} {:name long-name}+ :model/Card _ {:collection_id coll :creator_id creator+ :last_used_at (stale-instant)}]+ (scan/scan!)+ (testing "the collection's 300-character name is stored and served in full"+ (is (= long-name+ (t2/select-one-fn :entity_name :model/ContentDiagnosticsFinding+ :entity_type "collection" :entity_id coll))))+ (testing "the card's 509-character creator name is stored in full"+ (is (= (str first-name " " last-name)+ (t2/select-one-fn :entity_creator_name :model/ContentDiagnosticsFinding+ :entity_type "card" :entity_id+ (t2/select-one-pk :model/Card :collection_id coll)))))))))))++(deftest api-endpoint-is-feature-gated-test+ (testing "GET /stale is gated on the :content-diagnostics premium feature (premium-handler)"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (testing "licensed → 200 with the paginated envelope"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (let [resp (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/stale")]+ (is (contains? resp :data))+ (is (contains? resp :total)))))+ (testing "unlicensed → 402"+ (mt/with-premium-features #{}+ (mt/user-http-request :rasta :get 402 "ee/content-diagnostics/stale"))))))++;;; ---------------------------------------------- metrics -----------------------------------------------++(defn- runs+ "The `scan-runs` count for one (status, stage) pair - `stage` is `none` on the success arm."+ [system status stage]+ (mt/metric-value system :metabase-content-diagnostics/scan-runs {:status status :stage stage}))++(defn- duration-ms [system status]+ (mt/metric-value system :metabase-content-diagnostics/scan-duration-ms {:status status}))++(def ^:private all-stages+ "Every `stage` label a successful scan emits. Deriving the checker arms from the registry keeps a new+ checker from silently falling out of the coverage below."+ (into ["enrich" "insert" "invalidate"]+ (map #(str "checker." (:name %)))+ scan/checkers))++(deftest scan-metrics-test+ ;; one registry boot for every block - boot is expensive; blocks that re-read a metric clear! it first+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-prometheus-system! [_ system]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ ;; guarantees >= 1 finding, so the insert stage really runs a chunk+ :model/Card _ {:collection_id coll-id :last_used_at (stale-instant)}]+ (testing "feature absent: the scheduled scan does not run and emits nothing"+ ;; a metric reading 0 would prove nothing - an untouched label child reads 0 either way+ (let [emitted (atom [])]+ (mt/with-dynamic-fn-redefs [analytics/inc! (fn [& args] (swap! emitted conj args))+ analytics/observe! (fn [& args] (swap! emitted conj args))]+ (mt/with-premium-features #{}+ (#'task.scan/scan-when-enabled!)))+ (is (= [] @emitted))))+ (testing "successful scan: counted once, timed, every stage timed, findings counted"+ (scan/scan!)+ (is (== 1 (runs system "ok" "none")))+ (is (pos? (duration-ms system "ok")))+ (is (== 0 (duration-ms system "error")))+ (doseq [stage all-stages]+ (is (pos? (mt/metric-value system :metabase-content-diagnostics/scan-stage-ms {:stage stage}))+ stage))+ (is (<= 1 (mt/metric-value system :metabase-content-diagnostics/findings-persisted+ {:finding-type "stale"})))+ (doseq [stage (conj all-stages "unknown")]+ (is (== 0 (runs system "error" stage)) stage)))+ (testing "a throwing checker: the scan fails, attributed to that checker's stage"+ (prometheus/clear! :metabase-content-diagnostics/scan-duration-ms)+ (prometheus/clear! :metabase-content-diagnostics/scan-runs)+ (with-redefs [scan/checkers [{:name "slow" :finding-types #{:slow}+ :run (fn [] (throw (ex-info "boom" {})))}]]+ ;; the wrapped message keeps the original class, not just its message+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"stage checker\.slow failed: clojure\.lang\.ExceptionInfo: boom"+ (scan/scan!))))+ (is (== 1 (runs system "error" "checker.slow")))+ (is (== 0 (runs system "ok" "none")))+ (is (pos? (duration-ms system "error")))+ (is (== 0 (duration-ms system "ok"))))+ (testing "insert failure: attributed to the insert stage; nothing counted as persisted"+ (prometheus/clear! :metabase-content-diagnostics/scan-runs)+ (prometheus/clear! :metabase-content-diagnostics/findings-persisted)+ (with-redefs [t2/insert! (fn [& _] (throw (ex-info "db down" {})))]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"stage insert failed" (scan/scan!))))+ (is (== 1 (runs system "error" "insert")))+ (is (== 0 (mt/metric-value system :metabase-content-diagnostics/findings-persisted+ {:finding-type "stale"}))))+ (testing "a throw outside any stage is attributed to unknown"+ (prometheus/clear! :metabase-content-diagnostics/scan-duration-ms)+ (prometheus/clear! :metabase-content-diagnostics/scan-runs)+ ;; `count-persisted!` calls `covered-finding-types` outside any `run-stage!` - no stage in ex-data+ (mt/with-dynamic-fn-redefs [scan/covered-finding-types (fn [] (throw (ex-info "boom" {})))]+ (is (thrown? clojure.lang.ExceptionInfo (scan/scan!))))+ (is (== 1 (runs system "error" "unknown")))+ (is (pos? (duration-ms system "error"))))+ (testing "an undeclared finding type clamps to unknown"+ (prometheus/clear! :metabase-content-diagnostics/findings-persisted)+ (#'scan/count-persisted! [{:finding-type :bogus}])+ (is (== 1 (mt/metric-value system :metabase-content-diagnostics/findings-persisted+ {:finding-type "unknown"})))+ (is (== 0 (mt/metric-value system :metabase-content-diagnostics/findings-persisted+ {:finding-type "stale"}))))+ (testing "with :tasks tracing enabled the scan still succeeds and no span attribute collides"+ (prometheus/clear! :metabase-content-diagnostics/scan-runs)+ (try+ ;; every `add-span-attrs!` runs for real here, and a duplicate key within one span is+ ;; fail-loud in dev/test - a collision would surface as a scan failure+ (tracing/init-enabled-groups! "tasks" "INFO")+ (is (map? (scan/scan!)))+ (is (== 1 (runs system "ok" "none")))+ (is (== 0 (runs system "error" "unknown")))+ (finally+ (tracing/shutdown-groups!)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/slow_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/slow_test.cljnew file mode 100644index 00000000000..adba1fef7b5--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/slow_test.clj@@ -0,0 +1,811 @@+(ns ^:synchronized metabase-enterprise.content-diagnostics.slow-test+ "The `slow` checker flags card/transform leaves (over a duration threshold) and dashboard/document+ containers that embed a slow card, stamping the measured magnitude in the top-level `duration_ms`+ column and freezing `threshold_ms` (leaf only) in `details` at scan time. The `/slow` endpoint serves+ them with leaf-vs-container `details`, hydrated roll-up culprits, and the shared filter/sort/pagination+ envelope."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.checkers.slow :as checkers.slow]+ [metabase-enterprise.content-diagnostics.scan :as scan]+ [metabase-enterprise.content-diagnostics.test-util :as cd.tu]+ [metabase.collections.models.collection :as collection]+ [metabase.documents.prose-mirror :as prose-mirror]+ [metabase.permissions.core :as perms]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(use-fixtures :each cd.tu/with-authorized-reader!)++(defn- scope-prefix+ "Unique per-test entity-name prefix, passed as `:query` so assertions only see rows the test seeded -+ the findings table is shared across tests."+ []+ (str "cd-" (mt/random-name)))++(defn- slow-findings-by-entity!+ "Run a scan and index its `:slow` findings by `[entity-type entity-id]`."+ []+ (let [scan-id (:scan_id (scan/scan!))]+ (t2/select-fn->fn (juxt :entity_type :entity_id) identity+ :model/ContentDiagnosticsFinding :scan_id scan-id :finding_type :slow)))++;;; --------------------------------------------- checker --------------------------------------------------++(deftest slow-checker-detects-leaves-and-containers-test+ (testing "card/transform leaves and dashboard/document container roll-ups are flagged; fast/archived ones are not"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10+ content-diagnostics-slow-transform-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; slow card: median running_time 30s > 10s threshold (cache hits are ignored)+ :model/Card {slow-card :id} {:collection_id coll-id :name "Full Orders Export"}+ :model/QueryExecution _ {:card_id slow-card :started_at now+ :cache_hit false :running_time 30000}+ ;; a cache hit at a huge time must NOT count toward the median (it would shift it to 65s)+ :model/QueryExecution _ {:card_id slow-card :started_at now+ :cache_hit true :running_time 99999}+ ;; fast card: 500ms < 10s - never flagged, and never a culprit+ :model/Card {fast-card :id} {:collection_id coll-id :name "Quick Lookup"}+ :model/QueryExecution _ {:card_id fast-card :started_at now+ :cache_hit false :running_time 500}+ ;; archived card whose median IS slow - must be excluded (not flagged, not a culprit)+ :model/Card {archived-card :id} {:collection_id coll-id :archived true}+ :model/QueryExecution _ {:card_id archived-card :started_at now+ :cache_hit false :running_time 40000}+ ;; container roll-ups: a dashboard and a document embedding the slow card+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id slow-card}+ ;; the same slow card on a second tab must dedupe to one culprit id+ :model/DashboardCard _ {:dashboard_id dash-id :card_id slow-card}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id fast-card}+ :model/Document {doc-id :id} {:collection_id coll-id+ :creator_id (mt/user->id :rasta)+ :content_type prose-mirror/prose-mirror-content-type+ :document {:type "doc"+ :content [{:type "cardEmbed"+ :attrs {:id slow-card}}]}}+ ;; a dashboard with only the fast card must NOT be flagged+ :model/Dashboard {fast-dash :id} {:collection_id coll-id}+ :model/DashboardCard _ {:dashboard_id fast-dash :card_id fast-card}+ ;; an archived dashboard embedding the slow card must NOT be flagged+ :model/Dashboard {archived-dash :id} {:collection_id coll-id :archived true}+ :model/DashboardCard _ {:dashboard_id archived-dash :card_id slow-card}+ ;; transform leaves: one slow succeeded run (60s), one fast (1s)+ :model/Transform {slow-xform :id} {}+ :model/TransformRun _ {:transform_id slow-xform :status :succeeded+ :start_time (t/minus now (t/minutes 2))+ :end_time (t/minus now (t/minutes 1))}+ :model/Transform {fast-xform :id} {}+ :model/TransformRun _ {:transform_id fast-xform :status :succeeded+ :start_time (t/minus now (t/seconds 61))+ :end_time (t/minus now (t/seconds 60))}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "slow card leaf: measured median in top-level duration_ms; threshold frozen in details"+ (let [f (by-entity [:card slow-card])]+ (is (some? f))+ (is (= 30000 (:duration_ms f)))+ (is (= 10000 (get-in f [:details :threshold_ms])))+ (testing "leaf details holds ONLY the threshold"+ (is (= #{:threshold_ms} (set (keys (:details f))))))))+ (testing "fast card is not flagged"+ (is (nil? (by-entity [:card fast-card]))))+ (testing "archived card is not flagged even though its median is slow"+ (is (nil? (by-entity [:card archived-card]))))+ (testing "slow transform leaf: 60s duration over the 10s threshold, both frozen in ms"+ (let [f (by-entity [:transform slow-xform])]+ (is (some? f))+ (is (= 60000 (:duration_ms f)))+ (is (= 10000 (get-in f [:details :threshold_ms])))))+ (testing "fast transform is not flagged"+ (is (nil? (by-entity [:transform fast-xform]))))+ (testing "dashboard container: dedupes the twice-embedded slow card, stamps representative duration"+ (let [f (by-entity [:dashboard dash-id])]+ (is (some? f))+ (is (= [slow-card] (get-in f [:details :slow_entity_ids])))+ (is (= 30000 (:duration_ms f)))))+ (testing "dashboard with only fast cards is not flagged"+ (is (nil? (by-entity [:dashboard fast-dash]))))+ (testing "archived dashboard is not flagged"+ (is (nil? (by-entity [:dashboard archived-dash]))))+ (testing "document container rolls up the embedded slow card with its representative duration"+ (let [f (by-entity [:document doc-id])]+ (is (some? f))+ (is (= [slow-card] (get-in f [:details :slow_entity_ids])))+ (is (= 30000 (:duration_ms f)))))))))))))++(deftest slow-checker-container-scoping-test+ (testing "slow content in ineligible containers is not flagged; root-resident slow content still is"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp+ [:model/Collection {ia-coll :id} {:namespace "analytics"+ :type collection/instance-analytics-collection-type}+ ;; audit-resident slow card: 30s > 10s but in an ineligible container+ :model/Card {audit-card :id} {:collection_id ia-coll}+ :model/QueryExecution _ {:card_id audit-card :started_at now+ :cache_hit false :running_time 30000}+ ;; root-resident slow card: the root is always an eligible container+ :model/Card {root-card :id} {}+ :model/QueryExecution _ {:card_id root-card :started_at now+ :cache_hit false :running_time 30000}+ ;; an audit-resident dashboard embedding an ELIGIBLE slow card: the container clause is+ ;; re-applied to the dashboard itself, so the roll-up must not fire either+ :model/Dashboard {ia-dash :id} {:collection_id ia-coll}+ :model/DashboardCard _ {:dashboard_id ia-dash :card_id root-card}]+ (let [by-entity (slow-findings-by-entity!)]+ (is (nil? (by-entity [:card audit-card])))+ (is (some? (by-entity [:card root-card])))+ (is (nil? (by-entity [:dashboard ia-dash])))))))))))++(deftest slow-checker-threshold-boundary-test+ (testing "the measured duration must strictly exceed the threshold (boundary is exclusive) for both leaf types"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10+ content-diagnostics-slow-transform-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {at-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id at-card :started_at now+ :cache_hit false :running_time 10000}+ :model/Card {over-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id over-card :started_at now+ :cache_hit false :running_time 10001}+ :model/Transform {at-xform :id} {}+ :model/TransformRun _ {:transform_id at-xform :status :succeeded+ :start_time (t/minus now (t/seconds 10))+ :end_time now}+ :model/Transform {over-xform :id} {}+ :model/TransformRun _ {:transform_id over-xform :status :succeeded+ :start_time (t/minus now (t/seconds 11))+ :end_time now}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "card exactly at the threshold (10000ms) → not flagged"+ (is (nil? (by-entity [:card at-card]))))+ (testing "card just over the threshold (10001ms) → flagged"+ (is (some? (by-entity [:card over-card]))))+ (testing "transform run exactly at the threshold (10s) → not flagged"+ (is (nil? (by-entity [:transform at-xform]))))+ (testing "transform run over the threshold (11s) → flagged"+ (is (some? (by-entity [:transform over-xform]))))))))))))++(deftest slow-container-duration-is-slowest-culprit-test+ (testing "a container's duration_ms is the slowest culprit card's median"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c20 :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id c20 :started_at (t/offset-date-time)+ :cache_hit false :running_time 20000}+ :model/Card {c40 :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id c40 :started_at (t/offset-date-time)+ :cache_hit false :running_time 40000}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id c20}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id c40}]+ (let [f (get (slow-findings-by-entity!) [:dashboard dash-id])]+ (is (some? f))+ (testing "duration_ms is the slowest culprit's median (40s), not the fastest or a sum"+ (is (= 40000 (:duration_ms f))))+ (is (= #{c20 c40} (set (get-in f [:details :slow_entity_ids])))))))))))++(deftest slow-dashboard-covers-series-not-filter-source-test+ (testing "a dashboard is flagged for a combined-series slow card (renders on load) but NOT for a filter card value-source (fetched on demand, cached, a different limited query)"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Card {slow-card :id} {:collection_id coll-id :name "Slow One"}+ :model/QueryExecution _ {:card_id slow-card :started_at (t/offset-date-time)+ :cache_hit false :running_time 30000}+ ;; a fast primary card, so each dashboard's ONLY slow reference is the non-primary one+ :model/Card {fast-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id fast-card :started_at (t/offset-date-time)+ :cache_hit false :running_time 500}+ ;; dashboard S: the slow card is a combined SERIES on a dashcard whose primary card is fast+ :model/Dashboard {series-dash :id} {:collection_id coll-id}+ :model/DashboardCard {series-dc :id} {:dashboard_id series-dash :card_id fast-card}+ :model/DashboardCardSeries _ {:dashboardcard_id series-dc :card_id slow-card :position 0}+ ;; dashboard F: the slow card is ONLY a filter's card value-source (no dashcards at all).+ ;; The dropdown query is on-demand + cached + a different limited distinct-values query, so+ ;; this must NOT be attributed to the dashboard's render-time slowness.+ :model/Dashboard {filter-dash :id} {:collection_id coll-id}+ :model/ParameterCard _ {:parameterized_object_type "dashboard"+ :parameterized_object_id filter-dash+ :parameter_id "p1"+ :card_id slow-card}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "a slow SERIES card flags the dashboard, with the series card as the culprit"+ (let [f (by-entity [:dashboard series-dash])]+ (is (some? f))+ (is (= [slow-card] (get-in f [:details :slow_entity_ids])))))+ (testing "a dashboard whose only slow reference is a filter card value-source is NOT flagged"+ (is (nil? (by-entity [:dashboard filter-dash])))))))))))++(deftest slow-transform-uses-latest-finished-run-test+ (testing "transform slowness uses the latest FINISHED (succeeded/failed/timeout) run; never-run yields nothing"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-transform-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)+ days-ago #(t/minus now (t/days %))]+ (mt/with-temp+ [;; the latest finished run is the newer FAILED one (5m) - failed runs finished, so they count+ :model/Transform {failing-latest :id} {}+ :model/TransformRun _ {:transform_id failing-latest :status :succeeded+ :start_time (days-ago 9)+ :end_time (t/plus (days-ago 9) (t/minutes 1))}+ :model/TransformRun _ {:transform_id failing-latest :status :failed+ :start_time (days-ago 8)+ :end_time (t/plus (days-ago 8) (t/minutes 5))}+ ;; latest finished run is fast (1s) even though an older succeeded run was slow (60s)+ :model/Transform {recovered :id} {}+ :model/TransformRun _ {:transform_id recovered :status :succeeded+ :start_time (days-ago 10)+ :end_time (t/plus (days-ago 10) (t/minutes 1))}+ :model/TransformRun _ {:transform_id recovered :status :succeeded+ :start_time (days-ago 7)+ :end_time (t/plus (days-ago 7) (t/seconds 1))}+ ;; a slow CANCELED run doesn't count as finished - the fast succeeded run before it wins+ :model/Transform {canceled-latest :id} {}+ :model/TransformRun _ {:transform_id canceled-latest :status :succeeded+ :start_time (days-ago 6)+ :end_time (t/plus (days-ago 6) (t/seconds 1))}+ :model/TransformRun _ {:transform_id canceled-latest :status :canceled+ :start_time (days-ago 5)+ :end_time (t/plus (days-ago 5) (t/minutes 5))}+ ;; a TIMED-OUT run (2m) counts as finished+ :model/Transform {timed-out :id} {}+ :model/TransformRun _ {:transform_id timed-out :status :succeeded+ :start_time (days-ago 4)+ :end_time (t/plus (days-ago 4) (t/seconds 1))}+ :model/TransformRun _ {:transform_id timed-out :status :timeout+ :start_time (days-ago 3)+ :end_time (t/plus (days-ago 3) (t/minutes 2))}+ ;; never ran at all+ :model/Transform {never :id} {}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "flagged on its latest finished run - the 5m failed one, not the older 60s success"+ (let [f (by-entity [:transform failing-latest])]+ (is (some? f))+ (is (= 300000 (:duration_ms f)))))+ (testing "a transform whose latest finished run is fast is not flagged"+ (is (nil? (by-entity [:transform recovered]))))+ (testing "a slow canceled run is ignored - the latest finished run (fast) wins"+ (is (nil? (by-entity [:transform canceled-latest]))))+ (testing "a timed-out run counts - flagged on its 2m duration"+ (let [f (by-entity [:transform timed-out])]+ (is (some? f))+ (is (= 120000 (:duration_ms f)))))+ (testing "a never-run transform yields no finding"+ (is (nil? (by-entity [:transform never]))))))))))))++(deftest slow-card-uses-median-test+ (testing "card slowness is the MEDIAN running_time, so outliers don't drag a typically-fast card over"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; odd count {1s 1s 60s}: the mean (20.7s) would flag it; the median (1s) must not+ :model/Card {outlier-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id outlier-card :started_at now+ :cache_hit false :running_time 1000}+ :model/QueryExecution _ {:card_id outlier-card :started_at now+ :cache_hit false :running_time 1000}+ :model/QueryExecution _ {:card_id outlier-card :started_at now+ :cache_hit false :running_time 60000}+ ;; even count {20s 30s}: the median is the average of the middle pair (25s)+ :model/Card {even-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id even-card :started_at now+ :cache_hit false :running_time 20000}+ :model/QueryExecution _ {:card_id even-card :started_at now+ :cache_hit false :running_time 30000}+ ;; even pair {10000 10001} straddling the 10s threshold: the unrounded median (10000.5)+ ;; must be what the SQL compares (flagged), and the stored duration_ms rounds up+ :model/Card {half-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id half-card :started_at now+ :cache_hit false :running_time 10000}+ :model/QueryExecution _ {:card_id half-card :started_at now+ :cache_hit false :running_time 10001}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "a fast card with one slow outlier execution is not flagged (the mean would have)"+ (is (nil? (by-entity [:card outlier-card]))))+ (testing "even count: median is the mean of the two middle values"+ (let [f (by-entity [:card even-card])]+ (is (some? f))+ (is (= 25000 (:duration_ms f)))))+ (testing "fractional median 10000.5 clears the 10000 threshold unrounded; duration_ms rounds to 10001"+ (let [f (by-entity [:card half-card])]+ (is (some? f))+ (is (= 10001 (:duration_ms f)))))))))))))++(deftest slow-lookback-window-test+ (testing "only activity within the lookback window counts - older executions and runs are invisible"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10+ content-diagnostics-slow-transform-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; freeze the clock so the scan-time cutoff (now - lookback) is identical to what the fixtures+ ;; compute - the exact >= boundary can then be pinned without racing the scan+ (mt/with-clock #t "2026-07-15T12:00Z[UTC]"+ (let [now (t/offset-date-time)+ cutoff (t/minus now (t/days @#'checkers.slow/lookback-days))]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ ;; an out-of-window slow execution must not drag a recently-fast card's median over+ :model/Card {recent-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id recent-card :started_at (t/minus cutoff (t/days 1))+ :cache_hit false :running_time 99999}+ :model/QueryExecution _ {:card_id recent-card :started_at now+ :cache_hit false :running_time 500}+ ;; executions exactly AT the cutoff count (inclusive >=); 1s older do not+ :model/Card {at-edge-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id at-edge-card :started_at cutoff+ :cache_hit false :running_time 30000}+ :model/Card {past-edge-card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id past-edge-card+ :started_at (t/minus cutoff (t/seconds 1))+ :cache_hit false :running_time 30000}+ ;; the same boundary for transform runs, keyed on start_time+ :model/Transform {at-edge-xform :id} {}+ :model/TransformRun _ {:transform_id at-edge-xform :status :succeeded+ :start_time cutoff+ :end_time (t/plus cutoff (t/minutes 1))}+ :model/Transform {past-edge-xform :id} {}+ :model/TransformRun _ {:transform_id past-edge-xform :status :succeeded+ :start_time (t/minus cutoff (t/seconds 1))+ :end_time (t/plus cutoff (t/minutes 1))}]+ (let [by-entity (slow-findings-by-entity!)]+ (testing "an out-of-window slow execution doesn't affect the in-window median"+ (is (nil? (by-entity [:card recent-card]))))+ (testing "an execution exactly at the cutoff is included"+ (is (some? (by-entity [:card at-edge-card]))))+ (testing "an execution 1s before the cutoff is excluded"+ (is (nil? (by-entity [:card past-edge-card]))))+ (testing "a run started exactly at the cutoff is included"+ (is (some? (by-entity [:transform at-edge-xform]))))+ (testing "a run started 1s before the cutoff is excluded"+ (is (nil? (by-entity [:transform past-edge-xform])))))))))))))++(deftest slow-threshold-frozen-at-scan-time-test+ (testing "threshold_ms is frozen in details at scan time; a later setting change does not rewrite it"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {card :id} {:collection_id coll-id}+ :model/QueryExecution _ {:card_id card :started_at (t/offset-date-time)+ :cache_hit false :running_time 30000}]+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (slow-findings-by-entity!))+ (testing "the frozen detail stays at the scan-time threshold (10s), not the new one (5s)"+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 5]+ (let [f (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id card :finding_type :slow)]+ (is (= 10000 (get-in f [:details :threshold_ms])))))))))))++;;; ------------------------------------------------- API --------------------------------------------------++(deftest slow-api-hydration-test+ (testing "GET /slow serves leaf and container findings with hydrated context + culprits"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll-id :id} {:name "Analytics"}+ :model/Card {slow-card :id} {:collection_id coll-id+ :name "Full Orders Export"+ :type :model+ :display :table+ :view_count 5+ :creator_id (mt/user->id :rasta)}+ :model/QueryExecution _ {:card_id slow-card :started_at (t/offset-date-time)+ :cache_hit false :running_time 25000}+ :model/Dashboard {dash-id :id} {:collection_id coll-id :name "Ops Dashboard"}+ :model/DashboardCard _ {:dashboard_id dash-id :card_id slow-card}]+ (scan/scan!)+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/content-diagnostics/slow")+ by-id (into {} (map (juxt (juxt :entity_type :entity_id) identity)) (:data resp))]+ (testing "the envelope carries last_scan_at + total"+ (is (contains? resp :last_scan_at))+ (is (pos-int? (:total resp))))+ (testing "leaf (card): top-level duration_ms + frozen threshold_ms + collection + creator"+ (let [f (by-id ["card" slow-card])]+ (is (some? f))+ (is (= "Full Orders Export" (:entity_display_name f)))+ (testing "the flagged card carries its own type as a top-level card_type"+ (is (= "model" (:card_type f))))+ (is (= "table" (:display f)))+ (is (= 25000 (:duration_ms f)))+ (is (= 10000 (get-in f [:details :threshold_ms])))+ (is (= coll-id (get-in f [:details :collection :id])))+ (is (= (mt/user->id :rasta) (get-in f [:details :creator :id])))+ (is (= "user" (get-in f [:details :creator :type])))+ (testing "card has no owner column → owner null"+ (is (nil? (get-in f [:details :owner]))))+ (testing "slow leaves carry no last_active_at key (that column is stale-only)"+ (is (not (contains? f :last_active_at))))))+ (testing "container (dashboard): representative duration + stored ids hydrated into slow_entities"+ (let [f (by-id ["dashboard" dash-id])]+ (is (some? f))+ (is (= 25000 (:duration_ms f)))+ (is (nil? (get-in f [:details :slow_entity_ids])))+ (let [entities (get-in f [:details :slow_entities])]+ (is (= 1 (count entities)))+ (testing "culprits carry identity/type/usage for links, without visualization display"+ (is (= {:id slow-card :name "Full Orders Export"+ :entity_type "card" :card_type "model" :view_count 5}+ (first entities))))))))))))))++(deftest slow-api-transform-root-breadcrumb-test+ (testing "GET /slow: a transform's breadcrumb carries the transforms namespace, at root and nested"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-temporary-setting-values [content-diagnostics-slow-transform-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [now (t/offset-date-time)]+ (mt/with-temp+ ;; no :collection_id → the transform sits at the root of the :transforms tree+ [:model/Transform {xform :id} {}+ :model/TransformRun _ {:transform_id xform :status :succeeded+ :start_time (t/minus now (t/minutes 2))+ :end_time (t/minus now (t/minutes 1))}+ :model/Collection {tcoll :id} {:name "T Folder" :namespace "transforms"}+ :model/Transform {nested-xform :id} {:collection_id tcoll}+ :model/TransformRun _ {:transform_id nested-xform :status :succeeded+ :start_time (t/minus now (t/minutes 2))+ :end_time (t/minus now (t/minutes 1))}]+ (scan/scan!)+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/content-diagnostics/slow")+ by-id (into {} (map (juxt (juxt :entity_type :entity_id) identity)) (:data resp))+ f (by-id ["transform" xform])]+ (is (some? f))+ (testing "root sentinel is namespaced to Transforms, not the default \"Our analytics\""+ (is (= {:id "root" :name "Transforms" :namespace "transforms" :effective_ancestors []}+ (get-in f [:details :collection]))))+ (testing "a transform nested in a transforms-namespace collection carries that namespace"+ (is (= {:id tcoll+ :name "T Folder"+ :namespace "transforms"+ :effective_ancestors [{:id "root" :name "Transforms"}]}+ (get-in (by-id ["transform" nested-xform]) [:details :collection]))))))))))))++(deftest slow-api-subject-view-count-test+ (testing "GET /slow hydrates each finding's own live view_count into details; a transform omits it"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp+ [:model/Collection {coll-id :id} {}+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {card-id :id} {:collection_id coll-id :view_count 7}+ :model/Dashboard {dash-id :id} {:collection_id coll-id :view_count 12}+ :model/Document {doc-id :id} {:collection_id coll-id+ :view_count 3+ :creator_id (mt/user->id :rasta)+ :content_type prose-mirror/prose-mirror-content-type+ :document {:type "doc" :content []}}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (let [prefix (scope-prefix)]+ (doseq [[etype eid] [[:card card-id] [:dashboard dash-id] [:document doc-id] [:transform xform-id]]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "vc"+ :entity_type etype+ :entity_id eid+ :entity_name (str prefix "-" (name etype))+ :finding_type :slow+ :duration_ms 20000+ :details {:threshold_ms 15000}}))+ (let [by-type (into {} (map (juxt :entity_type identity))+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/slow" :query prefix)))]+ (testing "card/dashboard/document each carry their own live view_count in details"+ (is (= 7 (get-in by-type ["card" :details :view_count])))+ (is (= 12 (get-in by-type ["dashboard" :details :view_count])))+ (is (= 3 (get-in by-type ["document" :details :view_count]))))+ (testing "a transform (no view_count column) omits the key entirely from details"+ (is (contains? by-type "transform"))+ (is (not (contains? (get-in by-type ["transform" :details]) :view_count))))+ (testing "view_count is hydrated live at read time, not frozen at scan time"+ (t2/update! :model/Card card-id {:view_count 99})+ (let [card-f (some #(when (= "card" (:entity_type %)) %)+ (:data (mt/user-http-request :crowberto :get 200+ "ee/content-diagnostics/slow" :query prefix)))]+ (is (= 99 (get-in card-f [:details :view_count]))))))))))))++(deftest slow-api-filter-and-sort-test+ (testing "GET /slow filters by entity-types/min-duration-ms and sorts by duration-ms/name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions :transforms-basic :hosting}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Collection {tcoll-id :id} {:namespace "transforms"}+ :model/Card {card-id :id} {:collection_id coll-id}+ :model/Dashboard {dash-id :id} {:collection_id coll-id}+ :model/Transform {xform-id :id} {:collection_id tcoll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (perms/grant-collection-read-permissions! (perms/all-users-group) tcoll-id)+ (let [prefix (scope-prefix)+ insert (fn [etype eid nm duration]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "s"+ :entity_type etype+ :entity_id eid+ :entity_name (str prefix " " nm)+ :entity_kind etype+ :finding_type :slow+ :duration_ms duration+ :details {:threshold_ms 15000}})))+ card-fid (insert :card card-id "Alpha" 20000) ; leaf+ dash-fid (insert :dashboard dash-id "Beta" 50000) ; container (representative duration)+ xform-fid (insert :transform xform-id "Gamma" 80000) ; leaf+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/slow" :query prefix kvs)))))+ order (fn [& kvs] (mapv :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/slow" :query prefix kvs))))]+ (testing "no filter → all three"+ (is (= #{card-fid dash-fid xform-fid} (ids))))+ (testing "entity-types (single + multi)"+ (is (= #{card-fid} (ids :entity-types "card")))+ (is (= #{card-fid dash-fid} (ids :entity-types ["card" "dashboard"]))))+ (testing "min-duration-ms floor - a container passes/fails on its representative duration"+ (is (= #{dash-fid xform-fid} (ids :min-duration-ms "50000"))) ; dash (50000) passes, card dropped+ (is (= #{xform-fid} (ids :min-duration-ms "80000")))) ; dash now fails, leaf-only+ (testing "sort by duration-ms, both directions"+ (is (= [card-fid dash-fid xform-fid] (order :sort-column "duration-ms" :sort-direction "asc")))+ (is (= [xform-fid dash-fid card-fid] (order :sort-column "duration-ms" :sort-direction "desc"))))+ (testing "sort by name (Alpha < Beta < Gamma), both directions"+ (is (= [card-fid dash-fid xform-fid] (order :sort-column "name" :sort-direction "asc")))+ (is (= [xform-fid dash-fid card-fid] (order :sort-column "name" :sort-direction "desc")))))))))))++(deftest slow-api-paginates-test+ (testing "GET /slow honors limit/offset and reports the full valid total"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c1 :id} {:collection_id coll-id}+ :model/Card {c2 :id} {:collection_id coll-id}+ :model/Card {c3 :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)]+ (doseq [cid [c1 c2 c3]]+ (t2/insert! :model/ContentDiagnosticsFinding+ {:scan_id "p" :entity_type :card :entity_id cid+ :entity_name (str prefix "-" cid)+ :finding_type :slow :duration_ms 20000 :details {:threshold_ms 15000}}))+ (let [page (fn [limit offset]+ (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/slow"+ :query prefix :limit limit :offset offset))]+ (testing "limit caps the page; total reflects the full valid set; limit/offset echoed back"+ (let [r (page 2 0)]+ (is (= 2 (count (:data r))))+ (is (= 3 (:total r)))+ (is (= 2 (:limit r)))+ (is (= 0 (:offset r)))))+ (testing "offset advances to the remainder"+ (is (= 1 (count (:data (page 2 2))))))))))))))++(deftest slow-api-include-personal-collections-test+ (testing "GET /slow excludes personal-collection findings by default; includes them with the param"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))]+ (mt/with-temp [:model/Collection {reg-id :id} {}+ :model/Card {reg-card :id} {:collection_id reg-id}+ :model/Card {pers-card :id} {:collection_id pers-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) reg-id)+ (let [prefix (scope-prefix)+ insert (fn [card]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "p-scan" :entity_type :card :entity_id card+ :entity_name (str prefix "-" card)+ :finding_type :slow :duration_ms 20000 :details {}})))+ reg-fid (insert reg-card)+ pers-fid (insert pers-card)+ fetch (fn [& kvs] (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/slow" :query prefix kvs))+ fetched-ids (fn [resp] (set (map :id (:data resp))))]+ (testing "default (param omitted) → personal-collection finding excluded"+ (let [resp (fetch)]+ (is (contains? (fetched-ids resp) reg-fid))+ (is (not (contains? (fetched-ids resp) pers-fid)))+ (is (= 1 (:total resp)))))+ (testing "include-personal-collections=true → both returned, total counts both"+ (let [resp (fetch :include-personal-collections true)]+ (is (contains? (fetched-ids resp) reg-fid))+ (is (contains? (fetched-ids resp) pers-fid))+ (is (= 2 (:total resp)))))))))))))++(deftest slow-api-personal-culprits-follow-param-test+ (testing "GET /slow culprit hydration honors include-personal-collections like the findings filter"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (let [pers-id (:id (collection/user->personal-collection (mt/user->id :rasta)))]+ (mt/with-temp [:model/Collection {reg-id :id} {}+ :model/Dashboard {dash-id :id} {:collection_id reg-id}+ :model/Card {reg-card :id} {:collection_id reg-id}+ :model/Card {pers-card :id} {:collection_id pers-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) reg-id)+ (let [prefix (scope-prefix)+ fid (first (t2/insert-returning-pks!+ :model/ContentDiagnosticsFinding+ {:scan_id "pc" :entity_type :dashboard :entity_id dash-id+ :entity_name (str prefix "-dash")+ :finding_type :slow :duration_ms 20000+ :details {:slow_entity_ids [reg-card pers-card]}}))+ culprit-ids (fn [& kvs]+ (let [finding (some #(when (= fid (:id %)) %)+ (:data (apply mt/user-http-request+ :rasta :get 200+ "ee/content-diagnostics/slow"+ :query prefix kvs)))]+ (into #{} (map :id) (get-in finding [:details :slow_entities]))))]+ (testing "default → the caller's own (readable) personal-collection culprit is hidden too"+ (is (= #{reg-card} (culprit-ids))))+ (testing "include-personal-collections=true → both culprits hydrate"+ (is (= #{reg-card pers-card} (culprit-ids :include-personal-collections true))))))))))))++(deftest slow-api-query-search-test+ (testing "GET /slow ?query= case-insensitively substring-matches the denormalized entity name"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {c-rev :id} {:collection_id coll-id}+ :model/Card {c-cost :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ insert (fn [card nm]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "q" :entity_type :card :entity_id card+ :finding_type :slow :duration_ms 20000 :details {}+ :entity_name (str prefix " " nm)})))+ rev-fid (insert c-rev "Quarterly Revenue")+ cost-fid (insert c-cost "Cost Analysis")+ ids (fn [& kvs] (set (map :id (:data (apply mt/user-http-request :rasta :get 200+ "ee/content-diagnostics/slow" kvs)))))]+ (testing "substring match, case-insensitive"+ (is (= #{rev-fid} (ids :query (str prefix " QUARTERLY REV"))))+ (is (= #{cost-fid} (ids :query (str prefix " cost")))))+ (testing "no match → empty"+ (is (empty? (ids :query (str prefix " zzz"))))))))))))++(deftest slow-api-permission-filtered-test+ (testing "GET /slow returns only findings whose entity the current user can read"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {readable :id} {}+ :model/Collection {unreadable :id} {}+ :model/Card {r-card :id} {:collection_id readable}+ :model/Card {u-card :id} {:collection_id unreadable}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) readable)+ (let [insert (fn [card]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "perm" :entity_type :card :entity_id card+ :finding_type :slow :duration_ms 20000 :details {}})))+ r-fid (insert r-card)+ u-fid (insert u-card)+ ids-for (fn [user] (set (map :id (:data (mt/user-http-request+ user :get 200 "ee/content-diagnostics/slow")))))]+ (testing "non-admin sees only the finding in the readable collection"+ (let [ids (ids-for :rasta)]+ (is (contains? ids r-fid))+ (is (not (contains? ids u-fid)))))+ (testing "superuser sees findings in every collection"+ (let [ids (ids-for :crowberto)]+ (is (contains? ids r-fid))+ (is (contains? ids u-fid)))))))))))++(deftest slow-api-hides-unreadable-culprits-test+ (testing "GET /slow omits culprit cards the caller can't read from a container's slow_entities"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {readable :id} {}+ :model/Collection {unreadable :id} {}+ :model/Dashboard {dash-id :id} {:collection_id readable}+ :model/Card {open-card :id} {:collection_id readable}+ :model/Card {secret-card :id} {:collection_id unreadable}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) readable)+ (let [prefix (scope-prefix)+ fid (first (t2/insert-returning-pks!+ :model/ContentDiagnosticsFinding+ {:scan_id "perm" :entity_type :dashboard :entity_id dash-id+ :entity_name (str prefix "-dash")+ :finding_type :slow :duration_ms 20000+ :details {:slow_entity_ids [open-card secret-card]}}))+ culprit-ids (fn [user]+ (let [finding (some #(when (= fid (:id %)) %)+ (:data (mt/user-http-request+ user :get 200 "ee/content-diagnostics/slow"+ :query prefix)))]+ (into #{} (map :id) (get-in finding [:details :slow_entities]))))]+ (testing "superuser sees both culprits"+ (is (= #{open-card secret-card} (culprit-ids :crowberto))))+ (testing "non-admin sees only the culprit they can read"+ (is (= #{open-card} (culprit-ids :rasta)))))))))))++(deftest slow-api-does-not-leak-across-finding-types-test+ (testing "a stale finding never surfaces in /slow, and a slow finding never surfaces in /stale"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ :model/Card {stale-card :id} {:collection_id coll-id}+ :model/Card {slow-card :id} {:collection_id coll-id}]+ (perms/grant-collection-read-permissions! (perms/all-users-group) coll-id)+ (let [prefix (scope-prefix)+ stale-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id stale-card+ :entity_name (str prefix "-stale")+ :finding_type :stale :details {:threshold_days 90}}))+ slow-fid (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id "x" :entity_type :card :entity_id slow-card+ :entity_name (str prefix "-slow")+ :finding_type :slow :duration_ms 20000+ :details {:threshold_ms 15000}}))+ ids (fn [path] (set (map :id (:data (mt/user-http-request :rasta :get 200 path :query prefix)))))]+ (testing "/slow returns only the slow finding"+ (let [slow-ids (ids "ee/content-diagnostics/slow")]+ (is (contains? slow-ids slow-fid))+ (is (not (contains? slow-ids stale-fid)))))+ (testing "/stale returns only the stale finding"+ (let [stale-ids (ids "ee/content-diagnostics/stale")]+ (is (contains? stale-ids stale-fid))+ (is (not (contains? stale-ids slow-fid))))))))))))++(deftest slow-api-feature-gated-test+ (testing "GET /slow is gated on the :content-diagnostics premium feature (premium-handler)"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (testing "licensed → 200 with the paginated envelope"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (let [resp (mt/user-http-request :rasta :get 200 "ee/content-diagnostics/slow")]+ (is (contains? resp :data))+ (is (contains? resp :total)))))+ (testing "unlicensed → 402"+ (mt/with-premium-features #{}+ (mt/user-http-request :rasta :get 402 "ee/content-diagnostics/slow"))))))++(deftest scan-runs-both-checkers-and-supersedes-per-type-test+ (testing "one scan writes stale + slow in a single scan_id batch; a rescan supersedes a fixed slow finding while a still-stale entity keeps an active stale finding"+ (mt/with-premium-features #{:content-diagnostics :advanced-permissions}+ (mt/with-temporary-setting-values [content-diagnostics-slow-card-threshold-seconds 10]+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ (mt/with-temp [:model/Collection {coll-id :id} {}+ ;; stale (old last_used_at) but fast+ :model/Card {stale-card :id} {:collection_id coll-id+ :last_used_at (t/minus (t/offset-date-time) (t/days 400))}+ ;; slow (mean 30s) but fresh+ :model/Card {slow-card :id} {:collection_id coll-id :last_used_at (t/offset-date-time)}+ :model/QueryExecution _ {:card_id slow-card :started_at (t/offset-date-time)+ :cache_hit false :running_time 30000}]+ (let [scan-1 (:scan_id (scan/scan!))+ stale-row (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id stale-card :finding_type :stale)+ slow-row (t2/select-one :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id slow-card :finding_type :slow)]+ (testing "both finding types share one scan_id batch"+ (is (some? stale-row))+ (is (some? slow-row))+ (is (= scan-1 (:scan_id stale-row) (:scan_id slow-row))))+ ;; fix the slow card (drop its slow executions); stale card stays stale+ (t2/delete! :model/QueryExecution :card_id slow-card)+ (scan/scan!)+ (testing "the fixed slow finding is soft-invalidated"+ (is (some? (:invalidated_at (t2/select-one :model/ContentDiagnosticsFinding :id (:id slow-row))))))+ (testing "the still-stale entity keeps an active stale finding (per-type supersession)"+ (is (seq (t2/select :model/ContentDiagnosticsFinding+ :entity_type :card :entity_id stale-card+ :finding_type :stale :invalidated_at nil)))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/task/finding_trimmer_test.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/task/finding_trimmer_test.cljnew file mode 100644index 00000000000..64dea86e322--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/task/finding_trimmer_test.clj@@ -0,0 +1,56 @@+(ns metabase-enterprise.content-diagnostics.task.finding-trimmer-test+ "The trimmer's retention window: which invalidated findings the scheduled task deletes, and which it+ leaves in place."+ (:require+ [clojure.test :refer :all]+ [java-time.api :as t]+ [metabase-enterprise.content-diagnostics.settings :as cd.settings]+ [metabase-enterprise.content-diagnostics.task.finding-trimmer :as task.finding-trimmer]+ [metabase-enterprise.content-diagnostics.test-util :as cd.test-util]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- insert-finding!+ [entity-id invalidated-at]+ (cd.test-util/insert-finding! "finding-trimmer-test" entity-id invalidated-at))++(defn- days-ago+ [n]+ (t/minus (t/offset-date-time) (t/days (long n))))++(deftest trims-findings-past-the-retention-window-test+ (testing "findings invalidated longer ago than the retention window are deleted; newer and active ones stay"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; the only test that runs at the real cutoff, so it can also sweep up an expired finding+ ;; another suite left behind; the assertion is id-scoped so that cannot affect the result+ (let [retention (cd.settings/content-diagnostics-finding-retention-days)+ old (insert-finding! 1 (days-ago (inc retention)))+ recent (insert-finding! 2 (days-ago (dec retention)))+ active (insert-finding! 3 nil)]+ (#'task.finding-trimmer/trim-old-findings!)+ (is (= #{recent active}+ (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:in :id [old recent active]]})))))))++(deftest honors-the-retention-setting-test+ (testing "a longer retention window spares findings the default would have deleted"+ (mt/with-model-cleanup [:model/ContentDiagnosticsFinding]+ ;; under the 30-day default both fixtures would be deleted, so `recent` surviving is what+ ;; proves the task read the setting+ (let [retention-days (* 365 5)+ old (insert-finding! 4 (days-ago (* 365 6)))+ recent (insert-finding! 5 (days-ago 365))]+ (mt/with-temporary-setting-values [content-diagnostics-finding-retention-days retention-days]+ (#'task.finding-trimmer/trim-old-findings!))+ (is (= #{recent}+ (t2/select-pks-set :model/ContentDiagnosticsFinding+ {:where [:in :id [old recent]]})))))))++(deftest records-runs-in-task-history-test+ (testing "the scheduled job body records its run in task_history"+ (mt/with-model-cleanup [:model/TaskHistory]+ (#'task.finding-trimmer/trim-with-history!)+ (is (=? [{:status :success}]+ (t2/select [:model/TaskHistory :status] :task "content-diagnostics-trimmer"))))))diff --git a/enterprise/backend/test/metabase_enterprise/content_diagnostics/test_util.clj b/enterprise/backend/test/metabase_enterprise/content_diagnostics/test_util.cljnew file mode 100644index 00000000000..a8374adcc20--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/content_diagnostics/test_util.clj@@ -0,0 +1,29 @@+(ns metabase-enterprise.content-diagnostics.test-util+ "Shared fixtures for the content-diagnostics suites."+ (:require+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(defn insert-finding!+ "Insert one finding, returning its id. A nil `invalidated-at` leaves the row active."+ [scan-id entity-id invalidated-at]+ (first (t2/insert-returning-pks! :model/ContentDiagnosticsFinding+ {:scan_id scan-id+ :entity_type :card+ :entity_id entity-id+ :finding_type :stale+ :invalidated_at invalidated-at})))++(defn with-authorized-reader!+ "`clojure.test` fixture giving `:rasta` the data-analyst role, so suites driving the finding-list+ endpoints as `:rasta` clear the audience gate. A superuser would bypass the collection filtering these+ suites assert on. The gate only accepts the role with `:advanced-permissions`, so each test's own+ `with-premium-features` must include it.++ The flag is not inert:+ - `collection/visible-collection-query` grants read on every `transforms`-namespace collection, so a+ test denying an unprivileged user sight of a transform needs its own `:monitoring` actor.+ - It writes the shared `:rasta` row, so the installing namespace must be `^:synchronized`."+ [thunk]+ (mt/with-data-analyst-role! (mt/user->id :rasta)+ (thunk)))diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.cljindex 19099f1f387..283d92819c0 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj@@ -52,6 +52,9 @@:model/CardBookmark:model/ChannelTemplate:model/CollectionBookmark+ ;; Content Diagnostics findings are scan-snapshot runtime data (its `entity_id` column is a+ ;; polymorphic reference to the flagged entity, not a serdes NanoID) -- not portable content.+ :model/ContentDiagnosticsFinding:model/ContentTranslation:model/DashboardBookmark:model/DataAppGroupAssignmentdiff --git a/enterprise/backend/test/metabase_enterprise/stale/impl_test.clj b/enterprise/backend/test/metabase_enterprise/stale/impl_test.cljindex c10b3fbcb5c..cc94cdefc4e 100644--- a/enterprise/backend/test/metabase_enterprise/stale/impl_test.clj+++ b/enterprise/backend/test/metabase_enterprise/stale/impl_test.clj@@ -9,6 +9,7 @@[metabase.stale-test :refer [with-stale-itemsstale-dashboardstale-card+ stale-documentdate-months-agodatetime-months-ago]][metabase.test :as mt]@@ -45,6 +46,91 @@:sort-column :name:sort-direction :asc}))))))+(deftest can-find-stale-documents+ (mt/with-temp [:model/Document {id :id} (stale-document+ {:name "My Stale Document"+ :collection_id nil})]+ (let [base {:collection-ids #{nil}+ :cutoff-date (date-months-ago 6)+ :limit 10+ :offset 0+ :sort-column :name+ :sort-direction :asc}]+ (testing "a stale document is found when documents are searched"+ (is (= [{:id id :model :model/Document}]+ (:rows (stale/find-candidates (assoc base :models #{:model/Document}))))))+ (testing "documents are not part of the default model set"+ (is (not-any? #(= (:id %) id)+ (:rows (stale/find-candidates base))))))))++(deftest publicly-shared-documents-are-excluded+ (mt/with-temp [:model/Collection {col-id :id} {}+ :model/Document {doc-id1 :id} (stale-document {:name "A"+ :collection_id col-id+ :public_uuid (str (random-uuid))})+ :model/Document {doc-id2 :id} (stale-document {:name "B"+ :collection_id col-id})]+ (let [rows #(:rows (stale/find-candidates {:collection-ids #{col-id}+ :cutoff-date (date-months-ago 6)+ :limit 10+ :offset 0+ :sort-column :name+ :sort-direction :asc+ :models #{:model/Document}}))]+ (testing "when public sharing is disabled, publicly shared documents are still returned"+ (mt/with-temporary-setting-values [enable-public-sharing false]+ (is (= [{:id doc-id1 :model :model/Document}+ {:id doc-id2 :model :model/Document}]+ (rows)))))+ (testing "when public sharing is enabled, publicly shared documents are excluded"+ (mt/with-temporary-setting-values [enable-public-sharing true]+ (is (= [{:id doc-id2 :model :model/Document}]+ (rows))))))))++(deftest never-viewed-documents-created-before-cutoff-are-stale+ ;; A serdes import restores the old `created_at` but :skip's `last_viewed_at`/`view_count`+ ;; (make-spec "Document", document.clj), so an imported never-viewed doc has an OLD created_at+ ;; yet a fresh `last_viewed_at` — only the `view_count = 0 AND created_at <= cutoff` arm catches+ ;; it; the `last_viewed_at <= cutoff` arm can't.+ (mt/with-temp [:model/Document {never-viewed-id :id} {:name "Never viewed, created before cutoff"+ :collection_id nil+ :view_count 0+ :last_viewed_at (datetime-months-ago 1)+ :created_at (datetime-months-ago 7)}+ :model/Document {viewed-id :id} {:name "Viewed recently, created before cutoff"+ :collection_id nil+ :view_count 1+ :last_viewed_at (datetime-months-ago 1)+ :created_at (datetime-months-ago 7)}]+ (let [ids (into #{}+ (map :id)+ (:rows (stale/find-candidates+ {:collection-ids #{nil}+ :cutoff-date (date-months-ago 6)+ :limit 100+ :offset 0+ :sort-column :name+ :sort-direction :asc+ :models #{:model/Document}})))]+ (testing "a never-viewed document created before the cutoff is stale despite a recent last_viewed_at"+ (is (contains? ids never-viewed-id)))+ (testing "a document viewed after the cutoff is not stale"+ (is (not (contains? ids viewed-id)))))))++(deftest documents-in-non-standard-collection-types-are-excluded+ (mt/with-temp [:model/Collection {col-id :id} {:type "instance-analytics"}+ :model/Document _ (stale-document {:name "Doc" :collection_id col-id})]+ (is (= {:rows [] :total 0}+ (stale/find-candidates+ {:collection-ids #{col-id}+ :cutoff-date (date-months-ago 6)+ :limit 10+ :offset 0+ :sort-column :name+ :sort-direction :asc+ :models #{:model/Document}}))+ "should not include documents in non-standard collections")))+(deftest results-can-be-sorted(mt/with-temp [:model/Dashboard {id1 :id} {:name "Z":last_viewed_at (datetime-months-ago 10)}@@ -182,6 +268,71 @@:sort-column :name:sort-direction :asc})))))+(deftest include-columns-adds-union-columns-to-rows+ (mt/with-temp [:model/Collection {col-id :id} {}+ :model/Dashboard {id :id} (stale-dashboard {:name "My Stale Dashboard"+ :collection_id col-id})]+ (let [base {:collection-ids #{col-id}+ :cutoff-date (date-months-ago 6)+ :limit 10+ :offset 0+ :sort-column :name+ :sort-direction :asc}]+ (testing "rows carry only :id and :model by default"+ (is (= [{:id id :model :model/Dashboard}]+ (:rows (stale/find-candidates base)))))+ (testing ":include-columns adds the requested union columns"+ (is (=? [{:id id+ :model :model/Dashboard+ :name "My Stale Dashboard"+ :last_used_at some?}]+ (:rows (stale/find-candidates (assoc base :include-columns #{:name :last_used_at})))))))))++(deftest collection-ids-all-searches-instance-wide+ (mt/with-temp [:model/Collection {col-id :id} {}+ :model/Dashboard {in-coll-id :id} (stale-dashboard {:name "A" :collection_id col-id})+ :model/Dashboard {at-root-id :id} (stale-dashboard {:name "B" :collection_id nil})+ :model/Dashboard {fresh-id :id} {:name "C"+ :collection_id col-id+ :last_viewed_at (datetime-months-ago 1)}]+ (let [ids (into #{}+ (map :id)+ (:rows (stale/find-candidates+ {:collection-ids :all+ :cutoff-date (date-months-ago 6)+ :limit nil+ :offset nil+ :sort-column :name+ :sort-direction :asc+ :models #{:model/Dashboard}})))]+ (testing ":all finds stale content both in collections and at the root"+ (is (contains? ids in-coll-id))+ (is (contains? ids at-root-id)))+ (testing "content used since the cutoff is still excluded"+ (is (not (contains? ids fresh-id)))))))++(deftest collection-set-with-nil-member-includes-root-content+ (mt/with-temp [:model/Collection {col-id :id} {}+ :model/Collection {other-col-id :id} {}+ :model/Dashboard {in-coll-id :id} (stale-dashboard {:name "A" :collection_id col-id})+ :model/Dashboard {at-root-id :id} (stale-dashboard {:name "B" :collection_id nil})+ :model/Dashboard {elsewhere-id :id} (stale-dashboard {:name "C" :collection_id other-col-id})]+ (let [ids (into #{}+ (map :id)+ (:rows (stale/find-candidates+ {:collection-ids #{col-id nil}+ :cutoff-date (date-months-ago 6)+ :limit nil+ :offset nil+ :sort-column :name+ :sort-direction :asc+ :models #{:model/Dashboard}})))]+ (testing "a nil member selects root-level content alongside the named collection"+ (is (contains? ids in-coll-id))+ (is (contains? ids at-root-id)))+ (testing "collections outside the set are excluded"+ (is (not (contains? ids elsewhere-id)))))))+(deftest cutoff-date-is-taken-into-account(mt/with-temp [:model/Collection {col-id :id} {}:model/Dashboard _ {:name "A"diff --git a/enterprise/frontend/src/metabase-enterprise/api/content-diagnostics.ts b/enterprise/frontend/src/metabase-enterprise/api/content-diagnostics.tsnew file mode 100644index 00000000000..bc703756f2a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/api/content-diagnostics.ts@@ -0,0 +1,83 @@+import type {+ InvalidateFindingsRequest,+ InvalidateFindingsResponse,+ ListDuplicatedFindingsRequest,+ ListDuplicatedFindingsResponse,+ ListImbalancedFindingsRequest,+ ListImbalancedFindingsResponse,+ ListSlowFindingsRequest,+ ListSlowFindingsResponse,+ ListStaleFindingsRequest,+ ListStaleFindingsResponse,+} from "metabase-types/api";++import { EnterpriseApi } from "./api";+import { listTag } from "./tags";++export const contentDiagnosticsApi = EnterpriseApi.injectEndpoints({+ endpoints: (builder) => ({+ invalidateFindings: builder.mutation<+ InvalidateFindingsResponse,+ InvalidateFindingsRequest+ >({+ query: (body) => ({+ method: "POST",+ url: "/api/ee/content-diagnostics/invalidate",+ body,+ }),+ invalidatesTags: () => [listTag("content-diagnostics-finding")],+ }),+ listStaleFindings: builder.query<+ ListStaleFindingsResponse,+ ListStaleFindingsRequest+ >({+ query: (params) => ({+ method: "GET",+ url: "/api/ee/content-diagnostics/stale",+ params,+ }),+ providesTags: () => [listTag("content-diagnostics-finding")],+ }),+ listSlowFindings: builder.query<+ ListSlowFindingsResponse,+ ListSlowFindingsRequest+ >({+ query: (params) => ({+ method: "GET",+ url: "/api/ee/content-diagnostics/slow",+ params,+ }),+ providesTags: () => [listTag("content-diagnostics-finding")],+ }),+ listDuplicatedFindings: builder.query<+ ListDuplicatedFindingsResponse,+ ListDuplicatedFindingsRequest+ >({+ query: (params) => ({+ method: "GET",+ url: "/api/ee/content-diagnostics/duplicated",+ params,+ }),+ providesTags: () => [listTag("content-diagnostics-finding")],+ }),+ listImbalancedFindings: builder.query<+ ListImbalancedFindingsResponse,+ ListImbalancedFindingsRequest+ >({+ query: (params) => ({+ method: "GET",+ url: "/api/ee/content-diagnostics/imbalanced",+ params,+ }),+ providesTags: () => [listTag("content-diagnostics-finding")],+ }),+ }),+});++export const {+ useInvalidateFindingsMutation,+ useListStaleFindingsQuery,+ useListSlowFindingsQuery,+ useListDuplicatedFindingsQuery,+ useListImbalancedFindingsQuery,+} = contentDiagnosticsApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex 675499db647..7330db355ef 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -6,6 +6,7 @@ export * from "./api";export * from "./audit-info";export * from "./billing-info";export * from "./cloud-add-ons";+export * from "./content-diagnostics";export * from "./content-translation";export * from "./custom-viz-plugin";export * from "./data-app";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/constants.ts b/enterprise/frontend/src/metabase-enterprise/monitor/constants.tsnew file mode 100644index 00000000000..45d559e788d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/constants.ts@@ -0,0 +1 @@+export const PAGE_SIZE = 25;diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/analytics.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/analytics.tsnew file mode 100644index 00000000000..a2a0d94a374--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/analytics.ts@@ -0,0 +1,149 @@+import { trackSimpleEvent } from "metabase/analytics";+import type {+ CollectionId,+ ContentDiagnosticsEntityId,+ ContentDiagnosticsEntityType,+} from "metabase-types/api";++import type {+ ContentDiagnosticsFilterDimension,+ ContentDiagnosticsTab,+} from "./components/types";++export const trackContentDiagnosticsTabViewed = (+ tab: ContentDiagnosticsTab,+) => {+ trackSimpleEvent({+ event: "content_diagnostics_tab_viewed",+ event_detail: tab,+ });+};++export const trackContentDiagnosticsFindingSelected = ({+ tab,+ entityId,+ entityType,+}: {+ tab: ContentDiagnosticsTab;+ entityId: ContentDiagnosticsEntityId;+ entityType: ContentDiagnosticsEntityType;+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_finding_selected",+ triggered_from: tab,+ target_id: entityId,+ event_detail: entityType,+ });+};++export const trackContentDiagnosticsEntityOpened = ({+ tab,+ entityId,+ entityType,+}: {+ tab: ContentDiagnosticsTab;+ entityId: ContentDiagnosticsEntityId;+ entityType: ContentDiagnosticsEntityType;+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_entity_opened",+ triggered_from: tab,+ target_id: entityId,+ event_detail: entityType,+ });+};++export const trackContentDiagnosticsDuplicateOpened = ({+ tab,+ entityId,+ entityType,+}: {+ tab: ContentDiagnosticsTab;+ entityId: ContentDiagnosticsEntityId;+ entityType: ContentDiagnosticsEntityType;+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_duplicate_opened",+ triggered_from: tab,+ target_id: entityId,+ event_detail: entityType,+ });+};++export const trackContentDiagnosticsLocationOpened = (+ tab: ContentDiagnosticsTab,+ collectionId: CollectionId,+) => {+ trackSimpleEvent({+ event: "content_diagnostics_location_opened",+ triggered_from: tab,+ event_detail: String(collectionId),+ });+};++export const trackContentDiagnosticsFiltersChanged = ({+ tab,+ dimension,+}: {+ tab: ContentDiagnosticsTab;+ dimension: ContentDiagnosticsFilterDimension;+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_filters_changed",+ triggered_from: tab,+ event_detail: dimension,+ });+};++export const trackContentDiagnosticsFiltersReset = (+ tab: ContentDiagnosticsTab,+) => {+ trackSimpleEvent({+ event: "content_diagnostics_filters_reset",+ triggered_from: tab,+ });+};++export const trackContentDiagnosticsFindingsBulkTrashed = ({+ tab,+ removedCount,+ selectedCount,+ durationMs,+ result,+}: {+ tab: ContentDiagnosticsTab;+ removedCount: number;+ selectedCount: number;+ durationMs: number;+ result: "success" | "partial" | "failure";+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_findings_bulk_trashed",+ triggered_from: tab,+ event_detail: `${removedCount}/${selectedCount}`,+ duration_ms: durationMs,+ result,+ });+};++export const trackContentDiagnosticsFindingsBulkDismissed = ({+ tab,+ dismissedCount,+ selectedCount,+ durationMs,+ result,+}: {+ tab: ContentDiagnosticsTab;+ dismissedCount: number;+ selectedCount: number;+ durationMs: number;+ result: "success" | "partial" | "failure";+}) => {+ trackSimpleEvent({+ event: "content_diagnostics_findings_bulk_dismissed",+ triggered_from: tab,+ event_detail: `${dismissedCount}/${selectedCount}`,+ duration_ms: durationMs,+ result,+ });+};diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.module.css b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.module.cssnew file mode 100644index 00000000000..8bab38c904b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.module.css@@ -0,0 +1,5 @@+.bulkActions {+ transform: translateX(-50%);+ /* Keep bulk actions below loading overlays and confirmation dialogs. */+ z-index: calc(var(--mb-overlay-z-index) - 2);+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.tsxnew file mode 100644index 00000000000..dcffc6aba8c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.tsx@@ -0,0 +1,91 @@+import { msgid, ngettext, t } from "ttag";++import { BulkActionButton } from "metabase/common/components/BulkActionBar";+import { ConfirmModal } from "metabase/common/components/ConfirmModal";+import { Box, Card, Flex, Text, Tooltip } from "metabase/ui";+import type {+ ContentDiagnosticsBaseFinding,+ ContentDiagnosticsFindingId,+} from "metabase-types/api";++import type { ContentDiagnosticsTab } from "../types";++import S from "./ContentDiagnosticsBulkActionsBar.module.css";+import { ContentDiagnosticsBulkDismissButton } from "./ContentDiagnosticsBulkDismissButton";+import { useBulkTrashConfirmation } from "./use-bulk-trash-confirmation";++type ContentDiagnosticsBulkActionsBarProps = {+ selectedFindings: readonly ContentDiagnosticsBaseFinding[];+ tab: ContentDiagnosticsTab;+ enableTrash?: boolean;+ onSettled: (+ failedFindingIds: ContentDiagnosticsFindingId[],+ settledFindingIds: ContentDiagnosticsFindingId[],+ ) => void;+};++export function ContentDiagnosticsBulkActionsBar({+ selectedFindings,+ tab,+ onSettled,+ enableTrash = true,+}: ContentDiagnosticsBulkActionsBarProps) {+ const count = selectedFindings.length;+ const { canTrash, actionLabel, open, confirmationProps } =+ useBulkTrashConfirmation({ selectedFindings, tab, enableTrash, onSettled });+ return (+ <>+ {count > 0 && (+ <Box+ pos="absolute"+ left="50%"+ bottom="var(--mantine-spacing-lg)"+ className={S.bulkActions}+ data-testid="content-diagnostics-bulk-actions"+ >+ <Card+ bg="tooltip-background"+ c="tooltip-text"+ py="md"+ px="lg"+ data-testid="toast-card"+ >+ <Flex align="center" justify="space-between" gap="xxxl">+ <Text c="tooltip-text">+ {ngettext(+ msgid`${count} item selected`,+ `${count} items selected`,+ count,+ )}+ </Text>+ <Flex gap="sm" align="center">+ <ContentDiagnosticsBulkDismissButton+ tab={tab}+ findingIds={selectedFindings.map((finding) => finding.id)}+ onDismiss={(ids) => onSettled([], ids)}+ />+ {enableTrash && (+ <Tooltip+ label={t`You don't have permission to delete some selected items.`}+ disabled={canTrash}+ >+ <span>+ <BulkActionButton+ danger+ disabled={!canTrash}+ onClick={open}+ >+ {actionLabel}+ </BulkActionButton>+ </span>+ </Tooltip>+ )}+ </Flex>+ </Flex>+ </Card>+ </Box>+ )}+ <ConfirmModal {...confirmationProps} />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.unit.spec.tsxnew file mode 100644index 00000000000..4632d268625--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkActionsBar.unit.spec.tsx@@ -0,0 +1,618 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import type { ComponentProps } from "react";++import {+ setupCardEndpoints,+ setupDeleteTransformEndpoint,+ setupInvalidateFindingsEndpoint,+ setupInvalidateFindingsEndpointWithError,+ setupListDuplicatedFindingsEndpoint,+ setupListImbalancedFindingsEndpoint,+ setupListSlowFindingsEndpoint,+ setupListStaleFindingsEndpoint,+ setupUpdateCardEndpointWithError,+} from "__support__/server-mocks";+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import { defer } from "metabase/utils/promise";+import {+ useListDuplicatedFindingsQuery,+ useListImbalancedFindingsQuery,+ useListSlowFindingsQuery,+ useListStaleFindingsQuery,+} from "metabase-enterprise/api";+import type {+ CardId,+ ContentDiagnosticsBaseFinding,+ InvalidateFindingsResponse,+ TransformId,+} from "metabase-types/api";+import {+ createMockCard,+ createMockContentDiagnosticsStaleFinding,+ createMockListDuplicatedFindingsResponse,+ createMockListImbalancedFindingsResponse,+ createMockListSlowFindingsResponse,+ createMockListStaleFindingsResponse,+} from "metabase-types/api/mocks";++import { ContentDiagnosticsBulkActionsBar } from "./ContentDiagnosticsBulkActionsBar";++const { trackSimpleEvent, trackSchemaEvent } =+ jest.requireMock("metabase/analytics");++type SetupOpts = Partial<+ Pick<+ ComponentProps<typeof ContentDiagnosticsBulkActionsBar>,+ "selectedFindings"+ >+>;++function setup({ selectedFindings = [] }: SetupOpts = {}) {+ const onSettled = jest.fn();+ const { store, rerender } = renderWithProviders(+ <ContentDiagnosticsBulkActionsBar+ tab="stale"+ selectedFindings={selectedFindings}+ onSettled={onSettled}+ />,+ );+ return { onSettled, store, rerender };+}++describe("ContentDiagnosticsBulkActionsBar", () => {+ beforeEach(() => {+ trackSimpleEvent.mockClear();+ trackSchemaEvent.mockClear();+ });++ it("uses recoverable trash wording for archivable-only selections", async () => {+ setup({+ selectedFindings: [+ createMockCardFinding({ id: 1, entity_id: 1 }),+ createMockCardFinding({ id: 2, entity_id: 2 }),+ ],+ });++ expect(screen.getByText("2 items selected")).toBeInTheDocument();+ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );++ const dialog = await screen.findByRole("dialog");+ expect(+ within(dialog).getByText("Move 2 items to trash?"),+ ).toBeInTheDocument();+ expect(+ within(dialog).getByText("You can restore items from the trash."),+ ).toBeInTheDocument();+ });++ it("uses permanent-delete wording for transform-only selections", async () => {+ setup({+ selectedFindings: [createMockTransformFinding({ id: 1, entity_id: 1 })],+ });++ await userEvent.click(screen.getByRole("button", { name: "Delete" }));++ const dialog = await screen.findByRole("dialog");+ expect(within(dialog).getByText("Delete 1 transform?")).toBeInTheDocument();+ expect(+ within(dialog).getByText(+ "1 transform will be permanently deleted and cannot be restored.",+ ),+ ).toBeInTheDocument();+ });++ it("warns about both outcomes for mixed selections", async () => {+ setup({+ selectedFindings: [+ createMockCardFinding({ id: 1, entity_id: 1 }),+ createMockTransformFinding({ id: 2, entity_id: 2 }),+ ],+ });++ await userEvent.click(screen.getByRole("button", { name: "Delete" }));++ const dialog = await screen.findByRole("dialog");+ expect(+ within(dialog).getByText("Delete selected items?"),+ ).toBeInTheDocument();+ expect(+ within(dialog).getByText(+ "1 item will be moved to the trash and can be restored later. 1 transform will be permanently deleted and cannot be restored.",+ ),+ ).toBeInTheDocument();+ });++ it("hard-deletes a transform on confirm and reports it as a transform delete", async () => {+ setupDeleteTransformEndpoint(7);+ const { onSettled } = setup({+ selectedFindings: [createMockTransformFinding({ id: 1, entity_id: 7 })],+ });++ await userEvent.click(screen.getByRole("button", { name: "Delete" }));+ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Delete" }),+ );++ await waitFor(() => {+ expect(onSettled).toHaveBeenCalledWith([], [1]);+ });++ expect(fetchMock.callHistory.calls("path:/api/transform/7")).toHaveLength(+ 1,+ );+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "transform_deleted",+ target_id: 7,+ triggered_from: "content_diagnostics",+ result: "success",+ }),+ );+ });++ it("archives on confirm, reports success, and clears the selection", async () => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ const { onSettled, store } = setup({+ selectedFindings: [createMockCardFinding({ id: 1, entity_id: 1 })],+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Move to trash" }),+ );++ await waitFor(() => {+ expect(onSettled).toHaveBeenCalledWith([], [1]);+ });++ const calls = fetchMock.callHistory.calls("path:/api/card/1");+ expect(calls).toHaveLength(1);+ const [putCall] = calls;+ expect(JSON.parse(String(putCall.options?.body))).toMatchObject({+ archived: true,+ });+ expect(hasUndo(store, "Moved 1 item to the trash")).toBe(true);+ });++ it.each(["model", "metric"] as const)(+ "tracks a trashed %s as its card subtype",+ async (cardType) => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ const { onSettled } = setup({+ selectedFindings: [+ createMockCardFinding({ id: 1, entity_id: 1, card_type: cardType }),+ ],+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Move to trash",+ }),+ );+ await waitFor(() => expect(onSettled).toHaveBeenCalledWith([], [1]));+ expect(trackSchemaEvent).toHaveBeenCalledWith(+ "simple_event",+ expect.objectContaining({+ event: "moved-to-trash",+ event_detail: cardType,+ }),+ );+ },+ );++ it("keeps failed items selected and warns when some entities can't be trashed", async () => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ setupUpdateCardEndpointWithError(2);+ const { onSettled, store } = setup({+ selectedFindings: [+ createMockCardFinding({ id: 1, entity_id: 1 }),+ createMockCardFinding({ id: 2, entity_id: 2 }),+ ],+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Move to trash" }),+ );++ await waitFor(() => {+ expect(onSettled).toHaveBeenCalledWith([2], [1, 2]);+ });+ expect(hasUndo(store, "Couldn't remove 1 item")).toBe(true);+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_findings_bulk_trashed",+ triggered_from: "stale",+ event_detail: "1/2",+ result: "partial",+ }),+ );+ });++ it("does not trash or track when the selected finding disappears before confirmation", async () => {+ const { rerender, onSettled, store } = setup({+ selectedFindings: [createMockCardFinding({ id: 1, entity_id: 1 })],+ });+ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ const dialog = await screen.findByRole("dialog");++ rerender(+ <ContentDiagnosticsBulkActionsBar+ tab="stale"+ selectedFindings={[]}+ onSettled={onSettled}+ />,+ );+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Move to trash" }),+ );++ expect(onSettled).not.toHaveBeenCalled();+ expect(hasUndo(store, "Moved 0 items to the trash")).toBe(false);+ expect(trackSimpleEvent).not.toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_findings_bulk_trashed",+ }),+ );+ });++ it("tracks a fully trashed selection as a success", async () => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ setupCardEndpoints(createMockCard({ id: 2 }));+ const { onSettled } = setup({+ selectedFindings: [+ createMockCardFinding({ id: 1, entity_id: 1 }),+ createMockCardFinding({ id: 2, entity_id: 2 }),+ ],+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Move to trash",+ }),+ );++ await waitFor(() => {+ expect(onSettled).toHaveBeenCalledWith([], [1, 2]);+ });+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_findings_bulk_trashed",+ event_detail: "2/2",+ result: "success",+ }),+ );+ });++ it("tracks a selection where nothing could be trashed as a failure", async () => {+ setupUpdateCardEndpointWithError(1);+ const { onSettled } = setup({+ selectedFindings: [createMockCardFinding({ id: 1, entity_id: 1 })],+ });++ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Move to trash",+ }),+ );++ await waitFor(() => {+ expect(onSettled).toHaveBeenCalledWith([1], [1]);+ });+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_findings_bulk_trashed",+ event_detail: "0/1",+ result: "failure",+ }),+ );+ });++ describe("dismiss", () => {+ const INVALIDATE_ENDPOINT = "path:/api/ee/content-diagnostics/invalidate";++ it.each([+ { count: 1, label: "Dismiss finding" },+ { count: 2, label: "Dismiss findings" },+ ])(+ "labels both dismissal actions for $count selected findings",+ async ({ count, label }) => {+ setup({+ selectedFindings: Array.from({ length: count }, (_, index) =>+ createMockCardFinding({ id: index + 1 }),+ ),+ });++ const dialog = await openConfirmation(label);+ expect(+ within(dialog).getByRole("button", { name: label }),+ ).toBeEnabled();+ expect(+ within(dialog).getByRole("button", { name: "Cancel" }),+ ).toBeEnabled();+ },+ );++ it("dismisses mixed finding types using finding IDs without deleting content", async () => {+ setupInvalidateFindingsEndpoint({ invalidated: [11, 22], skipped: [] });+ const { onSettled, store } = setup({+ selectedFindings: [+ createMockCardFinding({ id: 11, entity_id: 101 }),+ createMockTransformFinding({ id: 22, entity_id: 202 }),+ ],+ });+ const dialog = await openConfirmation("Dismiss findings");+ expect(+ within(dialog).getByText(+ "Dismissed findings will be hidden for everyone. The underlying content will not be deleted.",+ ),+ ).toBeInTheDocument();+ expect(fetchMock.callHistory.calls(INVALIDATE_ENDPOINT)).toHaveLength(0);+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Dismiss findings" }),+ );+ await waitFor(() => expect(onSettled).toHaveBeenCalledWith([], [11, 22]));+ const calls = fetchMock.callHistory.calls(INVALIDATE_ENDPOINT);+ expect(calls).toHaveLength(1);+ expect(JSON.parse(String(calls[0].options.body))).toEqual({+ ids: [11, 22],+ });+ expect(+ fetchMock.callHistory+ .calls()+ .filter(+ ({ options }) =>+ options.method === "PUT" || options.method === "DELETE",+ ),+ ).toHaveLength(0);+ expect(hasUndo(store, "Dismissed 2 findings")).toBe(true);+ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_findings_bulk_dismissed",+ triggered_from: "stale",+ event_detail: "2/2",+ duration_ms: expect.any(Number),+ result: "success",+ });+ });++ it("refreshes findings across every diagnostics tab after dismissal", async () => {+ let dismissed = false;+ const getTotal = () => (dismissed ? 0 : 1);+ setupListStaleFindingsEndpoint(() =>+ createMockListStaleFindingsResponse({ total: getTotal() }),+ );+ setupListSlowFindingsEndpoint(() =>+ createMockListSlowFindingsResponse({ total: getTotal() }),+ );+ setupListDuplicatedFindingsEndpoint(() =>+ createMockListDuplicatedFindingsResponse({ total: getTotal() }),+ );+ setupListImbalancedFindingsEndpoint(() =>+ createMockListImbalancedFindingsResponse({ total: getTotal() }),+ );+ setupInvalidateFindingsEndpoint(() => {+ dismissed = true;+ return { invalidated: [11], skipped: [] };+ });++ function DiagnosticsCounts() {+ const stale = useListStaleFindingsQuery({});+ const slow = useListSlowFindingsQuery({});+ const duplicated = useListDuplicatedFindingsQuery({});+ const imbalanced = useListImbalancedFindingsQuery({});+ return (+ <>+ <div>Stale: {stale.data?.total}</div>+ <div>Slow: {slow.data?.total}</div>+ <div>Duplicated: {duplicated.data?.total}</div>+ <div>Imbalanced: {imbalanced.data?.total}</div>+ </>+ );+ }++ renderWithProviders(+ <>+ <DiagnosticsCounts />+ <ContentDiagnosticsBulkActionsBar+ tab="stale"+ selectedFindings={[createMockCardFinding({ id: 11 })]}+ onSettled={jest.fn()}+ />+ </>,+ );+ const TABS = ["Stale", "Slow", "Duplicated", "Imbalanced"];+ await Promise.all(+ TABS.map(async (tab) => {+ expect(await screen.findByText(`${tab}: 1`)).toBeInTheDocument();+ }),+ );+ const dialog = await openConfirmation();+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Dismiss finding" }),+ );+ await Promise.all(+ TABS.map(async (tab) => {+ expect(await screen.findByText(`${tab}: 0`)).toBeInTheDocument();+ }),+ );+ });++ it("does not dismiss when confirmation is canceled", async () => {+ const { onSettled } = setup({+ selectedFindings: [createMockCardFinding({ id: 11 })],+ });+ const dialog = await openConfirmation();+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Cancel" }),+ );+ expect(fetchMock.callHistory.calls(INVALIDATE_ENDPOINT)).toHaveLength(0);+ expect(onSettled).not.toHaveBeenCalled();+ expect(trackSimpleEvent).not.toHaveBeenCalled();+ });++ it("preserves selection on error and allows retry", async () => {+ setupInvalidateFindingsEndpointWithError({ message: "Dismiss failed" });+ const { onSettled, store } = setup({+ selectedFindings: [createMockCardFinding({ id: 11 })],+ });+ const dialog = await openConfirmation();+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Dismiss finding" }),+ );+ await waitFor(() => expect(hasUndo(store, "Dismiss failed")).toBe(true));+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();+ expect(onSettled).not.toHaveBeenCalled();+ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_findings_bulk_dismissed",+ triggered_from: "stale",+ event_detail: "0/1",+ duration_ms: expect.any(Number),+ result: "failure",+ });+ fetchMock.modifyRoute("invalidate-findings", {+ response: { invalidated: [11], skipped: [] },+ });+ const retryDialog = await openConfirmation();+ await userEvent.click(+ within(retryDialog).getByRole("button", { name: "Dismiss finding" }),+ );+ await waitFor(() => expect(onSettled).toHaveBeenCalledWith([], [11]));+ expect(fetchMock.callHistory.calls(INVALIDATE_ENDPOINT)).toHaveLength(2);+ });++ it.each([+ { invalidated: [11], skipped: [22], message: "Dismissed 1 finding" },+ {+ invalidated: [],+ skipped: [11, 22],+ message: "No findings were dismissed",+ },+ ])(+ "reports the actual dismissed count: $message",+ async ({ invalidated, skipped, message }) => {+ setupInvalidateFindingsEndpoint({ invalidated, skipped });+ const { onSettled, store } = setup({+ selectedFindings: [+ createMockCardFinding({ id: 11 }),+ createMockCardFinding({ id: 22 }),+ ],+ });+ const dialog = await openConfirmation("Dismiss findings");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Dismiss findings" }),+ );+ await waitFor(() =>+ expect(onSettled).toHaveBeenCalledWith([], [11, 22]),+ );+ expect(hasUndo(store, message)).toBe(true);+ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_findings_bulk_dismissed",+ triggered_from: "stale",+ event_detail: `${invalidated.length}/2`,+ duration_ms: expect.any(Number),+ result: "partial",+ });+ },+ );++ it("keeps confirmation open and blocks duplicate requests while dismissing", async () => {+ const response = defer<InvalidateFindingsResponse>();+ setupInvalidateFindingsEndpoint(() => response.promise);+ const { onSettled } = setup({+ selectedFindings: [createMockCardFinding({ id: 11 })],+ });+ try {+ const dialog = await openConfirmation();+ const confirm = within(dialog).getByRole("button", {+ name: "Dismiss finding",+ });+ await userEvent.click(confirm);+ expect(dialog).toBeVisible();+ expect(confirm).toBeDisabled();+ expect(+ within(dialog).getByRole("button", { name: "Cancel" }),+ ).toBeDisabled();+ await userEvent.click(confirm);+ expect(fetchMock.callHistory.calls(INVALIDATE_ENDPOINT)).toHaveLength(+ 1,+ );+ response.resolve({ invalidated: [11], skipped: [] });+ await waitFor(() => expect(onSettled).toHaveBeenCalledWith([], [11]));+ await waitFor(() =>+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument(),+ );+ } finally {+ response.resolve({ invalidated: [11], skipped: [] });+ }+ });++ it("does not send an empty selection if findings disappear before confirmation", async () => {+ const { rerender, onSettled } = setup({+ selectedFindings: [createMockCardFinding({ id: 11 })],+ });+ await openConfirmation();+ rerender(+ <ContentDiagnosticsBulkActionsBar+ tab="stale"+ selectedFindings={[]}+ onSettled={onSettled}+ />,+ );+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();+ expect(fetchMock.callHistory.calls(INVALIDATE_ENDPOINT)).toHaveLength(0);+ expect(onSettled).not.toHaveBeenCalled();+ });+ });+});++function createMockCardFinding(+ opts: Partial<Pick<ContentDiagnosticsBaseFinding, "id" | "card_type">> & {+ entity_id?: CardId;+ } = {},+): ContentDiagnosticsBaseFinding {+ return createMockContentDiagnosticsStaleFinding({+ entity_type: "card",+ ...opts,+ });+}++function createMockTransformFinding(+ opts: Partial<Pick<ContentDiagnosticsBaseFinding, "id">> & {+ entity_id?: TransformId;+ } = {},+): ContentDiagnosticsBaseFinding {+ return createMockContentDiagnosticsStaleFinding({+ entity_type: "transform",+ ...opts,+ });+}++function hasUndo(store: ReturnType<typeof setup>["store"], message: string) {+ return store.getState().undo.some((undo) => undo.message === message);+}++async function openConfirmation(name = "Dismiss finding") {+ await userEvent.click(screen.getByRole("button", { name }));+ return screen.findByRole("dialog");+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkDismissButton.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkDismissButton.tsxnew file mode 100644index 00000000000..ee59264563a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/ContentDiagnosticsBulkDismissButton.tsx@@ -0,0 +1,122 @@+import { useDisclosure } from "@mantine/hooks";+import { msgid, ngettext, t } from "ttag";++import { getErrorMessage } from "metabase/api/utils";+import { BulkActionButton } from "metabase/common/components/BulkActionBar";+import { ConfirmModal } from "metabase/common/components/ConfirmModal";+import { useDispatch } from "metabase/redux";+import { addUndo } from "metabase/redux/undo";+import { useInvalidateFindingsMutation } from "metabase-enterprise/api";+import type {+ ContentDiagnosticsFindingId,+ InvalidateFindingsResponse,+} from "metabase-types/api";++import { trackContentDiagnosticsFindingsBulkDismissed } from "../../analytics";+import type { ContentDiagnosticsTab } from "../types";++interface ContentDiagnosticsBulkDismissButtonProps {+ tab: ContentDiagnosticsTab;+ findingIds: readonly ContentDiagnosticsFindingId[];+ onDismiss: (findingIds: ContentDiagnosticsFindingId[]) => void;+}++function getDismissLabel(count: number) {+ return ngettext(msgid`Dismiss finding`, `Dismiss findings`, count);+}++export function ContentDiagnosticsBulkDismissButton({+ tab,+ findingIds,+ onDismiss,+}: ContentDiagnosticsBulkDismissButtonProps) {+ const dispatch = useDispatch();+ const [isOpen, { open, close }] = useDisclosure();+ const [invalidateFindings, { isLoading }] = useInvalidateFindingsMutation();++ const handleConfirm = async () => {+ if (isLoading) {+ return;+ }+ if (findingIds.length === 0) {+ close();+ return;+ }++ const startTime = performance.now();+ let result: InvalidateFindingsResponse;+ try {+ result = await invalidateFindings({ ids: findingIds }).unwrap();+ } catch (error) {+ trackContentDiagnosticsFindingsBulkDismissed({+ tab,+ dismissedCount: 0,+ selectedCount: findingIds.length,+ durationMs: Math.trunc(performance.now() - startTime),+ result: "failure",+ });+ dispatch(+ addUndo({+ icon: "warning",+ message: getErrorMessage(error, t`Couldn't dismiss findings`),+ }),+ );+ close();+ return;+ }++ const count = result.invalidated.length;+ trackContentDiagnosticsFindingsBulkDismissed({+ tab,+ dismissedCount: count,+ selectedCount: findingIds.length,+ durationMs: Math.trunc(performance.now() - startTime),+ result: result.skipped.length > 0 ? "partial" : "success",+ });+ dispatch(+ addUndo({+ message:+ count === 0+ ? t`No findings were dismissed`+ : ngettext(+ msgid`Dismissed ${count} finding`,+ `Dismissed ${count} findings`,+ count,+ ),+ }),+ );+ close();+ // Skipped IDs are no longer active or visible, rather than failed writes.+ onDismiss([...result.invalidated, ...result.skipped]);+ };++ return (+ <>+ <BulkActionButton disabled={isLoading} onClick={open}>+ {getDismissLabel(findingIds.length)}+ </BulkActionButton>+ <ConfirmModal+ opened={isOpen}+ title={ngettext(+ msgid`Dismiss ${findingIds.length} finding?`,+ `Dismiss ${findingIds.length} findings?`,+ findingIds.length,+ )}+ message={t`Dismissed findings will be hidden for everyone. The underlying content will not be deleted.`}+ confirmButtonText={getDismissLabel(findingIds.length)}+ confirmButtonProps={{+ color: "brand",+ loading: isLoading,+ disabled: isLoading,+ }}+ closeButtonProps={{ disabled: isLoading }}+ onConfirm={handleConfirm}+ onClose={() => {+ if (!isLoading) {+ close();+ }+ }}+ />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/index.tsnew file mode 100644index 00000000000..8cd624a5b2b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/index.ts@@ -0,0 +1 @@+export { ContentDiagnosticsBulkActionsBar } from "./ContentDiagnosticsBulkActionsBar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-confirmation.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-confirmation.tsnew file mode 100644index 00000000000..cc8d8b7a69b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-confirmation.ts@@ -0,0 +1,161 @@+import { useDisclosure } from "@mantine/hooks";+import { msgid, ngettext, t } from "ttag";++import { useDispatch } from "metabase/redux";+import { addUndo } from "metabase/redux/undo";+import type {+ ContentDiagnosticsBaseFinding,+ ContentDiagnosticsFindingId,+} from "metabase-types/api";++import type { ContentDiagnosticsTab } from "../types";++import { useBulkTrashFindings } from "./use-bulk-trash-findings";++interface BulkTrashConfirmationOptions {+ selectedFindings: readonly ContentDiagnosticsBaseFinding[];+ tab: ContentDiagnosticsTab;+ enableTrash: boolean;+ onSettled: (+ failedFindingIds: ContentDiagnosticsFindingId[],+ settledFindingIds: ContentDiagnosticsFindingId[],+ ) => void;+}++export function useBulkTrashConfirmation({+ selectedFindings,+ tab,+ enableTrash,+ onSettled,+}: BulkTrashConfirmationOptions) {+ const dispatch = useDispatch();+ const trashFindings = useBulkTrashFindings();+ const [isConfirmOpen, { open, close }] = useDisclosure();++ const canTrash = selectedFindings.every((finding) => finding.can_write);+ const transformCount = selectedFindings.filter(+ (finding) => finding.entity_type === "transform",+ ).length;+ const archivableCount = selectedFindings.length - transformCount;+ const trashCopy = getTrashCopy(archivableCount, transformCount);++ const handleConfirm = async () => {+ if (selectedFindings.length === 0 || !enableTrash || !canTrash) {+ close();+ return;+ }+ const { total, failedFindings } = await trashFindings(+ selectedFindings,+ tab,+ );+ close();++ if (failedFindings.length > 0) {+ dispatch(+ addUndo({+ icon: "warning",+ message: ngettext(+ msgid`Couldn't remove ${failedFindings.length} item`,+ `Couldn't remove ${failedFindings.length} items`,+ failedFindings.length,+ ),+ }),+ );+ } else {+ dispatch(addUndo({ message: getResultMessage(total, transformCount) }));+ }++ onSettled(+ failedFindings.map((finding) => finding.id),+ selectedFindings.map((finding) => finding.id),+ );+ };++ return {+ canTrash,+ actionLabel: trashCopy.actionLabel,+ open,+ confirmationProps: {+ opened: isConfirmOpen,+ title: trashCopy.title,+ message: trashCopy.message,+ confirmButtonText: trashCopy.confirmLabel,+ onConfirm: handleConfirm,+ onClose: close,+ },+ };+}++type TrashCopy = {+ actionLabel: string;+ title: string;+ message: string;+ confirmLabel: string;+};++// Transforms are permanently deleted (no restore), so any selection that includes+// one drops the "trash" language for "delete". A pure-archivable selection keeps+// the recoverable "move to trash" wording (matching the collections screen).+function getTrashCopy(+ archivableCount: number,+ transformCount: number,+): TrashCopy {+ if (transformCount === 0) {+ return {+ actionLabel: t`Move to trash`,+ title: ngettext(+ msgid`Move ${archivableCount} item to trash?`,+ `Move ${archivableCount} items to trash?`,+ archivableCount,+ ),+ message: t`You can restore items from the trash.`,+ confirmLabel: t`Move to trash`,+ };+ }++ const deletePart = ngettext(+ msgid`${transformCount} transform will be permanently deleted and cannot be restored.`,+ `${transformCount} transforms will be permanently deleted and cannot be restored.`,+ transformCount,+ );++ if (archivableCount === 0) {+ return {+ actionLabel: t`Delete`,+ title: ngettext(+ msgid`Delete ${transformCount} transform?`,+ `Delete ${transformCount} transforms?`,+ transformCount,+ ),+ message: deletePart,+ confirmLabel: t`Delete`,+ };+ }++ const trashPart = ngettext(+ msgid`${archivableCount} item will be moved to the trash and can be restored later.`,+ `${archivableCount} items will be moved to the trash and can be restored later.`,+ archivableCount,+ );+ return {+ actionLabel: t`Delete`,+ title: t`Delete selected items?`,+ message: `${trashPart} ${deletePart}`,+ confirmLabel: t`Delete`,+ };+}++function getResultMessage(count: number, transformCount: number): string {+ if (transformCount === 0) {+ return ngettext(+ msgid`Moved ${count} item to the trash`,+ `Moved ${count} items to the trash`,+ count,+ );+ }+ return ngettext(+ msgid`Deleted ${count} item`,+ `Deleted ${count} items`,+ count,+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-findings.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-findings.tsnew file mode 100644index 00000000000..7d272e96f6a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsBulkActionsBar/use-bulk-trash-findings.ts@@ -0,0 +1,103 @@+import { useCallback } from "react";+import { match } from "ts-pattern";++import { useDeleteTransformMutation } from "metabase/api";+import { archiveAndTrack } from "metabase/archive/analytics";+import { useSetArchive } from "metabase/archive/hooks/use-set-archive";+import { deleteTransformAndTrack } from "metabase/transforms/analytics";+import { isNotNull } from "metabase/utils/types";+import type { ContentDiagnosticsBaseFinding } from "metabase-types/api";++import { trackContentDiagnosticsFindingsBulkTrashed } from "../../analytics";+import type { ContentDiagnosticsTab } from "../types";++export type BulkTrashResult = {+ total: number;+ failedFindings: ContentDiagnosticsBaseFinding[];+};++// Cards (question/model/metric), dashboards, documents and collections archive+// under a model that matches their entity type. Transforms have no archived+// state, so they are hard-deleted instead.+type DiagnosticsArchiveModel =+ | "card"+ | "dataset"+ | "metric"+ | "dashboard"+ | "collection"+ | "document";++function getArchivableModel(+ finding: ContentDiagnosticsBaseFinding,+): DiagnosticsArchiveModel | null {+ if (finding.entity_type === "transform") {+ return null;+ }+ if (finding.entity_type === "card") {+ return match(finding.card_type)+ .returnType<DiagnosticsArchiveModel>()+ .with("model", () => "dataset")+ .with("metric", () => "metric")+ .otherwise(() => "card");+ }+ return finding.entity_type;+}++export function useBulkTrashFindings() {+ const archive = useSetArchive();+ const [deleteTransform] = useDeleteTransformMutation();++ return useCallback(+ async (+ findings: readonly ContentDiagnosticsBaseFinding[],+ tab: ContentDiagnosticsTab,+ ): Promise<BulkTrashResult> => {+ if (findings.length === 0) {+ return { total: 0, failedFindings: [] };+ }+ const startTime = performance.now();+ const trashFinding = (finding: ContentDiagnosticsBaseFinding) => {+ const model = getArchivableModel(finding);+ if (model === null) {+ return deleteTransformAndTrack({+ deleteTransform: () => deleteTransform(finding.entity_id).unwrap(),+ transformId: finding.entity_id,+ triggeredFrom: "content_diagnostics",+ });+ }+ return archiveAndTrack({+ archive: () =>+ archive({ model, id: finding.entity_id }, true, { notify: false }),+ model: model === "dataset" ? "model" : model,+ modelId: finding.entity_id,+ triggeredFrom: "content_diagnostics",+ });+ };++ const outcomes = await Promise.all(+ findings.map(async (finding) => {+ try {+ await trashFinding(finding);+ return null;+ } catch {+ return finding;+ }+ }),+ );+ const failedFindings = outcomes.filter(isNotNull);+ const removedCount = findings.length - failedFindings.length;+ trackContentDiagnosticsFindingsBulkTrashed({+ tab,+ removedCount,+ selectedCount: findings.length,+ durationMs: Math.trunc(performance.now() - startTime),+ result: match({ removedCount, failedCount: failedFindings.length })+ .with({ removedCount: 0 }, () => "failure" as const)+ .with({ failedCount: 0 }, () => "success" as const)+ .otherwise(() => "partial" as const),+ });+ return { total: findings.length, failedFindings };+ },+ [archive, deleteTransform],+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsContent.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsContent.tsxnew file mode 100644index 00000000000..1574afe0947--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ContentDiagnosticsContent.tsx@@ -0,0 +1,331 @@+import { useElementSize } from "@mantine/hooks";+import type { OnChangeFn, RowSelectionState } from "@tanstack/react-table";+import {+ type ComponentProps,+ type ReactNode,+ useEffect,+ useLayoutEffect,+ useMemo,+ useState,+} from "react";++import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";+import { MonitorMain } from "metabase/monitor/components/MonitorLayout";+import { Sidebar } from "metabase/monitor/components/MonitorLayout/Sidebar";+import { Center, Flex } from "metabase/ui";+import type { Sorting } from "metabase/utils/sorting";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";+import type {+ ContentDiagnosticsBaseFinding,+ ContentDiagnosticsFilterType,+ ContentDiagnosticsFindingId,+ SortDirection,+} from "metabase-types/api";++import {+ trackContentDiagnosticsFiltersChanged,+ trackContentDiagnosticsFiltersReset,+ trackContentDiagnosticsFindingSelected,+ trackContentDiagnosticsTabViewed,+} from "../analytics";++import { ContentDiagnosticsBulkActionsBar } from "./ContentDiagnosticsBulkActionsBar";+import { DiagnosticsHeader } from "./DiagnosticsHeader";+import { DiagnosticsPagination } from "./DiagnosticsPagination";+import type {+ ContentDiagnosticsBaseFilterOptions,+ ContentDiagnosticsParamsOptions,+ ContentDiagnosticsTab,+} from "./types";+import { getChangedFilterDimension } from "./utils";++type Params<TSortColumn extends string> = {+ page?: number;+ query?: string;+ sortColumn?: TSortColumn;+ sortDirection?: SortDirection;+};++type ContentConfig<+ TParams extends Params<TSortColumn>,+ TFilterOptions extends+ ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+ TSortColumn extends string,+> = {+ getDefaultFilterOptions: () => TFilterOptions;+ getFilterOptions: (params: TParams) => TFilterOptions;+ getFilterParams: (options: TFilterOptions) => Partial<TParams>;+ sortColumns: readonly TSortColumn[];+};++type FilterBarProps<TFilterOptions> = {+ query?: string;+ filterOptions: TFilterOptions;+ isLoading: boolean;+ onQueryChange: (query: string | undefined) => void;+ onFilterOptionsChange: (options: TFilterOptions) => void;+ onReset: () => void;+};++type TableProps<TParams, TFinding, TSortColumn extends string> = {+ findings: TFinding[];+ params: TParams;+ sortOptions: Sorting<TSortColumn> | undefined;+ isFetching: boolean;+ isLoading: boolean;+ rowSelection: RowSelectionState;+ onSelect: (finding: TFinding) => void;+ onSortOptionsChange: (sorting: Sorting<TSortColumn> | undefined) => void;+ onRowSelectionChange: OnChangeFn<RowSelectionState>;+};++type ContentDiagnosticsContentProps<+ TParams extends Params<TSortColumn>,+ TFilterOptions extends+ ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+ TFinding extends ContentDiagnosticsBaseFinding,+ TSortColumn extends string,+> = {+ tab: ContentDiagnosticsTab;+ config: ContentConfig<TParams, TFilterOptions, TSortColumn>;+ params: TParams;+ filterOptions: TFilterOptions;+ isLoadingParams: boolean;+ onParamsChange: (+ params: TParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+ data?: { data: TFinding[]; total: number };+ isFetchingFindings: boolean;+ isLoadingFindings: boolean;+ error: ComponentProps<typeof DelayedLoadingAndErrorWrapper>["error"];+ renderFilterBar: (props: FilterBarProps<TFilterOptions>) => ReactNode;+ renderTable: (props: TableProps<TParams, TFinding, TSortColumn>) => ReactNode;+ renderSidebar: (finding: TFinding, onClose: () => void) => ReactNode;+ enableBulkTrash?: boolean;+};++export function ContentDiagnosticsContent<+ TParams extends Params<TSortColumn>,+ TFilterOptions extends+ ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+ TFinding extends ContentDiagnosticsBaseFinding,+ TSortColumn extends string,+>({+ tab,+ config,+ params,+ filterOptions,+ isLoadingParams,+ onParamsChange,+ data,+ isFetchingFindings,+ isLoadingFindings,+ error,+ renderFilterBar,+ renderTable,+ renderSidebar,+ enableBulkTrash = true,+}: ContentDiagnosticsContentProps<+ TParams,+ TFilterOptions,+ TFinding,+ TSortColumn+>) {+ const { ref: containerRef, width: containerWidth } = useElementSize();+ const [selectedFindingId, setSelectedFindingId] =+ useState<ContentDiagnosticsFindingId>();+ const [rowSelection, setRowSelection] = useState<RowSelectionState>({});++ const { page = 0, query, sortColumn, sortDirection } = params;+ const sortOptions = useMemo(+ (): Sorting<TSortColumn> | undefined =>+ sortColumn != null && sortDirection != null+ ? { column: sortColumn, direction: sortDirection }+ : undefined,+ [sortColumn, sortDirection],+ );+ const isFetching = isFetchingFindings || isLoadingParams;+ const isLoading = isLoadingFindings || isLoadingParams;+ const findings = useMemo(() => data?.data ?? [], [data]);+ const totalCount = data?.total ?? 0;+ const selectedFinding = findings.find(+ (finding) => finding.id === selectedFindingId,+ );+ const selectedFindings = findings.filter(+ (finding) => rowSelection[finding.id],+ );++ useEffect(() => {+ trackContentDiagnosticsTabViewed(tab);+ }, [tab]);++ // RTK Query can keep the old page's data during a refetch. Only clamp once+ // the response for the requested page has settled, to avoid changing pages+ // based on a stale total.+ useEffect(() => {+ if (+ !isFetching &&+ data != null &&+ page > Math.max(0, Math.ceil(totalCount / PAGE_SIZE) - 1)+ ) {+ setRowSelection({});+ onParamsChange({+ ...params,+ page: Math.max(0, Math.ceil(totalCount / PAGE_SIZE) - 1),+ });+ }+ }, [data, isFetching, onParamsChange, page, params, totalCount]);++ useLayoutEffect(() => {+ if (selectedFindingId != null && selectedFinding == null) {+ setSelectedFindingId(undefined);+ }+ }, [selectedFindingId, selectedFinding]);++ const clearRowSelection = () => setRowSelection({});+ const handleQueryChange = (value: string | undefined) => {+ clearRowSelection();+ onParamsChange({ ...params, query: value, page: undefined });+ };+ const handleFilterOptionsChange = (nextOptions: TFilterOptions) => {+ const dimension = getChangedFilterDimension(filterOptions, nextOptions);+ if (dimension !== null) {+ trackContentDiagnosticsFiltersChanged({ tab, dimension });+ }+ clearRowSelection();+ onParamsChange(+ { ...params, ...config.getFilterParams(nextOptions), page: undefined },+ { withSetLastUsedParams: true },+ );+ };+ const handleReset = () => {+ trackContentDiagnosticsFiltersReset(tab);+ clearRowSelection();+ onParamsChange(+ {+ ...params,+ ...config.getFilterParams(config.getDefaultFilterOptions()),+ query: undefined,+ page: undefined,+ },+ { withSetLastUsedParams: true },+ );+ };+ const handlePageChange = (nextPage: number) => {+ clearRowSelection();+ onParamsChange({ ...params, page: nextPage });+ };+ const handleSortOptionsChange = (+ nextSort: Sorting<TSortColumn> | undefined,+ ) => {+ clearRowSelection();+ onParamsChange(+ {+ ...params,+ sortColumn: nextSort?.column,+ sortDirection: nextSort?.direction,+ page: undefined,+ },+ { withSetLastUsedParams: true },+ );+ };+ const handleSelect = (finding: TFinding) => {+ if (finding.id === selectedFindingId) {+ return;+ }+ trackContentDiagnosticsFindingSelected({+ tab,+ entityId: finding.entity_id,+ entityType: finding.entity_type,+ });+ setSelectedFindingId(finding.id);+ };+ const handleBulkActionSettled = (+ failedIds: readonly ContentDiagnosticsFindingId[],+ settledIds: readonly ContentDiagnosticsFindingId[],+ ) => {+ setRowSelection((selection) =>+ reconcileRowSelection({ selection, failedIds, settledIds }),+ );+ };++ return (+ <>+ <Flex ref={containerRef} h="100%" wrap="nowrap">+ <MonitorMain pos="relative">+ <DiagnosticsHeader />+ {renderFilterBar({+ query,+ filterOptions,+ isLoading,+ onQueryChange: handleQueryChange,+ onFilterOptionsChange: handleFilterOptionsChange,+ onReset: handleReset,+ })}+ {error != null ? (+ <Center flex={1}>+ <DelayedLoadingAndErrorWrapper+ loading={isLoading}+ error={error}+ />+ </Center>+ ) : (+ renderTable({+ findings,+ params,+ sortOptions,+ isFetching,+ isLoading,+ rowSelection,+ onSelect: handleSelect,+ onSortOptionsChange: handleSortOptionsChange,+ onRowSelectionChange: setRowSelection,+ })+ )}+ {!isLoading && error == null && (+ <DiagnosticsPagination+ page={page}+ pageItemCount={findings.length}+ totalCount={totalCount}+ onPageChange={handlePageChange}+ />+ )}+ <ContentDiagnosticsBulkActionsBar+ enableTrash={enableBulkTrash}+ tab={tab}+ selectedFindings={selectedFindings}+ onSettled={handleBulkActionSettled}+ />+ </MonitorMain>+ {selectedFinding != null && (+ <Sidebar containerWidth={containerWidth}>+ {renderSidebar(selectedFinding, () =>+ setSelectedFindingId(undefined),+ )}+ </Sidebar>+ )}+ </Flex>+ </>+ );+}++function reconcileRowSelection({+ selection,+ failedIds,+ settledIds,+}: {+ selection: RowSelectionState;+ failedIds: readonly ContentDiagnosticsFindingId[];+ settledIds: readonly ContentDiagnosticsFindingId[];+}): RowSelectionState {+ const settledKeys = new Set(settledIds.map((id) => id.toString()));+ return {+ ...Object.fromEntries(+ Object.entries(selection).filter(([id]) => !settledKeys.has(id)),+ ),+ ...Object.fromEntries(+ settledIds.filter((id) => failedIds.includes(id)).map((id) => [id, true]),+ ),+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsEntityIcon.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsEntityIcon.tsxnew file mode 100644index 00000000000..e426f756aac--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsEntityIcon.tsx@@ -0,0 +1,57 @@+import { type ComponentPropsWithoutRef, forwardRef } from "react";++import { EntityIcon } from "metabase/common/components/EntityIcon";+import type { IconModel } from "metabase/common/utils/icon";+import type { useGetIcon } from "metabase/hooks/use-icon";+import { Box, type BoxProps } from "metabase/ui";+import type {+ CardType,+ ContentDiagnosticsBaseFinding,+} from "metabase-types/api";++import { getEntityTypeLabel } from "./utils";++const CARD_MODELS = {+ question: "card",+ model: "dataset",+ metric: "metric",+} satisfies Record<CardType, IconModel>;++type DiagnosticsEntityIconProps = Omit<+ ComponentPropsWithoutRef<"span">,+ "children"+> &+ Pick<BoxProps, "mx"> & {+ entity: Pick<+ ContentDiagnosticsBaseFinding,+ "entity_type" | "card_type" | "display"+ >;+ getIcon: ReturnType<typeof useGetIcon>;+ };++export const DiagnosticsEntityIcon = forwardRef<+ HTMLSpanElement,+ DiagnosticsEntityIconProps+>(function DiagnosticsEntityIcon({ entity, getIcon, ...props }, ref) {+ const model =+ entity.entity_type === "card"+ ? CARD_MODELS[entity.card_type ?? "question"]+ : entity.entity_type;+ const label = getEntityTypeLabel(entity);++ return (+ <Box+ component="span"+ display="inline-flex"+ flex="0 0 auto"+ ref={ref}+ {...props}+ >+ <EntityIcon+ {...getIcon({ model, display: entity.display })}+ aria-label={label}+ alt={label}+ />+ </Box>+ );+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/DiagnosticsFilterPicker.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/DiagnosticsFilterPicker.tsxnew file mode 100644index 00000000000..8f23ad2fed1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/DiagnosticsFilterPicker.tsx@@ -0,0 +1,134 @@+import { useDisclosure } from "@mantine/hooks";+import type { ChangeEvent, ReactNode } from "react";+import { t } from "ttag";++import {+ Box,+ Button,+ Checkbox,+ FixedSizeIcon,+ Indicator,+ Input,+ Popover,+ Stack,+} from "metabase/ui";+import type { ContentDiagnosticsFilterType } from "metabase-types/api";++import type { ContentDiagnosticsBaseFilterOptions } from "../types";+import { getFilterTypeLabel } from "../utils";++type DiagnosticsFilterPickerProps<+ TType extends ContentDiagnosticsFilterType,+ TOptions extends ContentDiagnosticsBaseFilterOptions<TType>,+> = {+ filterOptions: TOptions;+ availableTypes: TType[];+ isDisabled?: boolean;+ hasDefaultOptions?: boolean;+ extraFilters?: ReactNode;+ canReset?: boolean;+ onFilterOptionsChange: (filterOptions: TOptions) => void;+ onReset: () => void;+};++export function DiagnosticsFilterPicker<+ TType extends ContentDiagnosticsFilterType,+ TOptions extends ContentDiagnosticsBaseFilterOptions<TType>,+>({+ filterOptions,+ availableTypes,+ isDisabled = false,+ hasDefaultOptions = false,+ extraFilters,+ canReset,+ onFilterOptionsChange,+ onReset,+}: DiagnosticsFilterPickerProps<TType, TOptions>) {+ const [isOpened, { toggle, close }] = useDisclosure();++ const handleTypesChange = (newValue: string[]) => {+ const selectedTypes = availableTypes.filter((type) =>+ newValue.includes(type),+ );+ const entityTypes =+ selectedTypes.length > 0 ? selectedTypes : availableTypes;+ onFilterOptionsChange({ ...filterOptions, entityTypes });+ };++ const handleReset = () => {+ onReset();+ close();+ };++ const handlePersonalCollectionsChange = (+ event: ChangeEvent<HTMLInputElement>,+ ) => {+ onFilterOptionsChange({+ ...filterOptions,+ includePersonalCollections: event.target.checked,+ });+ };++ return (+ <Popover opened={isOpened} onDismiss={close}>+ <Popover.Target>+ <Indicator+ size={8}+ offset={12}+ disabled={hasDefaultOptions}+ attributes={{+ indicator: {+ "data-testid": "content-diagnostics-filter-indicator",+ },+ }}+ >+ <Button+ size="lg"+ leftSection={<FixedSizeIcon name="filter" aria-hidden />}+ disabled={isDisabled}+ data-testid="content-diagnostics-filter-button"+ onClick={toggle}+ >+ {t`Filter`}+ </Button>+ </Indicator>+ </Popover.Target>+ <Popover.Dropdown>+ <Box w="20rem" p="md">+ <Stack>+ {availableTypes.length > 0 && (+ <Checkbox.Group+ label={t`Entity type`}+ value={filterOptions.entityTypes}+ onChange={handleTypesChange}+ >+ <Stack gap="sm" mt="sm">+ {availableTypes.map((type) => (+ <Checkbox+ key={type}+ value={type}+ label={getFilterTypeLabel(type)}+ />+ ))}+ </Stack>+ </Checkbox.Group>+ )}+ {extraFilters}+ <Input.Wrapper label={t`Location`}>+ <Stack gap="sm" mt="sm">+ <Checkbox+ label={t`Include items in personal collections`}+ checked={filterOptions.includePersonalCollections}+ onChange={handlePersonalCollectionsChange}+ />+ </Stack>+ </Input.Wrapper>+ <Button fullWidth disabled={!canReset} onClick={handleReset}>+ {t`Reset to defaults`}+ </Button>+ </Stack>+ </Box>+ </Popover.Dropdown>+ </Popover>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/index.tsnew file mode 100644index 00000000000..a6f65b03926--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsFilterPicker/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsFilterPicker";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/DiagnosticsHeader.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/DiagnosticsHeader.tsxnew file mode 100644index 00000000000..13d0c283460--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/DiagnosticsHeader.tsx@@ -0,0 +1,55 @@+import { memo } from "react";+import { c, t } from "ttag";++import {+ type PillTab,+ PillTabNavigation,+} from "metabase/common/components/PillTabNavigation";+import { MonitorHeaderTitle } from "metabase/monitor/components/MonitorHeaderTitle";+import { Stack } from "metabase/ui";+import * as Urls from "metabase/urls";++export const DiagnosticsHeader = memo(function DiagnosticsHeader() {+ const tabs: PillTab[] = [+ {+ label: c("Navigation tab for content that hasn't been used recently")+ .t`Stale`,+ to: Urls.staleContent(),+ icon: "clock",+ },+ {+ label: c("Navigation tab for duplicated content").t`Duplicated`,+ to: Urls.duplicatedContent(),+ icon: "copy",+ },+ {+ label: c("Navigation tab for slow-loading content").t`Slow`,+ to: Urls.slowContent(),+ icon: "gauge",+ },+ {+ label: c(+ "Navigation tab for empty content, e.g. a collection with no items",+ ).t`Empty`,+ to: Urls.imbalancedContent("empty"),+ icon: "unreferenced",+ },+ {+ label: c("Navigation tab for content with very few items").t`Sparse`,+ to: Urls.imbalancedContent("sparse"),+ icon: "layout_grid",+ },+ {+ label: c("Navigation tab for content with too many items").t`Crowded`,+ to: Urls.imbalancedContent("crowded"),+ icon: "grid_bordered",+ },+ ];++ return (+ <Stack gap="xl">+ <MonitorHeaderTitle>{t`Content diagnostics`}</MonitorHeaderTitle>+ <PillTabNavigation tabs={tabs} />+ </Stack>+ );+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/index.tsnew file mode 100644index 00000000000..82bcc69b372--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsHeader/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsHeader";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsxnew file mode 100644index 00000000000..016a80550f1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsx@@ -0,0 +1,31 @@+import { PaginationControls } from "metabase/common/components/PaginationControls";+import { Flex } from "metabase/ui";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";++type DiagnosticsPaginationProps = {+ page: number;+ pageItemCount: number;+ totalCount: number;+ onPageChange: (page: number) => void;+};++export function DiagnosticsPagination({+ page,+ pageItemCount,+ totalCount,+ onPageChange,+}: DiagnosticsPaginationProps) {+ return (+ <Flex justify="end">+ <PaginationControls+ page={page}+ pageSize={PAGE_SIZE}+ itemsLength={pageItemCount}+ total={totalCount}+ showTotal+ onPreviousPage={() => onPageChange(page - 1)}+ onNextPage={() => onPageChange(page + 1)}+ />+ </Flex>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/index.tsnew file mode 100644index 00000000000..7f108105504--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsPagination/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsPagination";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/DiagnosticsSearchInput.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/DiagnosticsSearchInput.tsxnew file mode 100644index 00000000000..0013c945319--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/DiagnosticsSearchInput.tsx@@ -0,0 +1,50 @@+import { useDebouncedCallback } from "@mantine/hooks";+import { type ChangeEvent, useEffect, useState } from "react";+import { t } from "ttag";++import { FixedSizeIcon, TextInput } from "metabase/ui";+import { SEARCH_DEBOUNCE_DURATION } from "metabase/utils/constants";++type DiagnosticsSearchInputProps = {+ query?: string;+ onQueryChange: (query: string | undefined) => void;+};++export function DiagnosticsSearchInput({+ query,+ onQueryChange,+}: DiagnosticsSearchInputProps) {+ const [searchValue, setSearchValue] = useState(query ?? "");++ const handleSearchDebounce = useDebouncedCallback(+ (newSearchValue: string) => {+ const trimmed = newSearchValue.trim();+ onQueryChange(trimmed.length > 0 ? trimmed : undefined);+ },+ SEARCH_DEBOUNCE_DURATION,+ );++ useEffect(() => {+ handleSearchDebounce.cancel();+ setSearchValue((value) =>+ value.trim() === (query ?? "") ? value : (query ?? ""),+ );+ }, [handleSearchDebounce, query]);++ const handleSearchChange = (event: ChangeEvent<HTMLInputElement>) => {+ const newSearchValue = event.target.value;+ setSearchValue(newSearchValue);+ handleSearchDebounce(newSearchValue);+ };++ return (+ <TextInput+ value={searchValue}+ placeholder={t`Search…`}+ aria-label={t`Search`}+ flex={1}+ leftSection={<FixedSizeIcon name="search" />}+ onChange={handleSearchChange}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/index.tsnew file mode 100644index 00000000000..ec36584559d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSearchInput/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsSearchInput";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.module.css b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.module.cssnew file mode 100644index 00000000000..bff9d5554a1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.module.css@@ -0,0 +1,24 @@+.sidebar {+ flex: 1 1 auto;+ overflow: auto;+ border-left: 1px solid var(--mb-color-border-neutral);+}++.wrap {+ overflow-wrap: anywhere;+ word-break: break-word;+ white-space: normal;+}++.section:not(:last-child) {+ border-bottom: 1px solid var(--mb-color-border-neutral);+}++.titleLink {+ color: var(--mb-color-text-primary);++ &:hover {+ color: var(--mb-color-core-brand);+ text-decoration: none;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.tsxnew file mode 100644index 00000000000..7a374aa3657--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.tsx@@ -0,0 +1,253 @@+import cx from "classnames";+import type { ReactNode } from "react";+import { t } from "ttag";++import { DateTime } from "metabase/common/components/DateTime";+import { ForwardRefLink } from "metabase/common/components/Link";+import { useGetIcon } from "metabase/hooks/use-icon";+import { Link } from "metabase/router";+import {+ ActionIcon,+ Anchor,+ Box,+ Breadcrumbs,+ Card,+ FixedSizeIcon,+ Group,+ Stack,+ Text,+ Tooltip,+} from "metabase/ui";+import type { ContentDiagnosticsBaseFinding } from "metabase-types/api";++import {+ trackContentDiagnosticsEntityOpened,+ trackContentDiagnosticsLocationOpened,+} from "../../analytics";+import { DiagnosticsEntityIcon } from "../DiagnosticsEntityIcon";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";+import type { ContentDiagnosticsTab } from "../types";+import {+ getBreadcrumbLinks,+ getEntityName,+ getEntityTypeLabel,+ getEntityUrl,+ getEntityViewLabel,+ getUserName,+} from "../utils";++import S from "./DiagnosticsSidebar.module.css";++export type SidebarExtraInfo = {+ label: string;+ children: ReactNode;+};++type DiagnosticsSidebarProps<T extends ContentDiagnosticsBaseFinding> = {+ finding: T;+ tab: ContentDiagnosticsTab;+ extraInfo?: SidebarExtraInfo;+ children?: ReactNode;+ onClose: () => void;+};++export function DiagnosticsSidebar<T extends ContentDiagnosticsBaseFinding>({+ finding,+ tab,+ extraInfo,+ children,+ onClose,+}: DiagnosticsSidebarProps<T>) {+ const entityName = getEntityName(finding);++ return (+ <Stack+ className={S.sidebar}+ p="lg"+ gap="lg"+ bg="background_page-primary"+ role="region"+ aria-label={t`Details for ${entityName}`}+ data-testid="content-diagnostics-sidebar"+ >+ <Stack gap="lg" flex="0 0 auto">+ <SidebarHeader finding={finding} tab={tab} onClose={onClose} />+ <LocationSection finding={finding} tab={tab} />+ <InfoSection finding={finding} extraInfo={extraInfo} />+ {children}+ </Stack>+ </Stack>+ );+}++type SidebarHeaderProps = {+ finding: ContentDiagnosticsBaseFinding;+ tab: ContentDiagnosticsTab;+ onClose: () => void;+};++function SidebarHeader({ finding, tab, onClose }: SidebarHeaderProps) {+ const getIcon = useGetIcon();+ const entityUrl = getEntityUrl(finding);+ const viewLabel = getEntityViewLabel(finding);+ const trackEntityOpened = () =>+ trackContentDiagnosticsEntityOpened({+ tab,+ entityId: finding.entity_id,+ entityType: finding.entity_type,+ });++ return (+ <Group+ gap="0.75rem"+ wrap="nowrap"+ align="start"+ justify="space-between"+ data-testid="content-diagnostics-sidebar-header"+ >+ <Anchor+ className={cx(S.wrap, S.titleLink)}+ component={ForwardRefLink}+ fz="h3"+ fw="bold"+ lh="h3"+ miw={0}+ to={entityUrl}+ target="_blank"+ onClick={trackEntityOpened}+ >+ <Group component="span" gap="sm" wrap="nowrap" align="center" miw={0}>+ <DiagnosticsEntityIcon+ entity={finding}+ getIcon={getIcon}+ aria-hidden+ />+ <span className={S.wrap}>{getEntityName(finding)}</span>+ </Group>+ </Anchor>+ <Group gap="xs" wrap="nowrap">+ <Tooltip label={viewLabel} openDelay={TOOLTIP_OPEN_DELAY_MS}>+ <ActionIcon+ component={ForwardRefLink}+ to={entityUrl}+ target="_blank"+ aria-label={viewLabel}+ onClick={trackEntityOpened}+ >+ <FixedSizeIcon name="external" />+ </ActionIcon>+ </Tooltip>+ <ActionIcon aria-label={t`Close`} onClick={onClose}>+ <FixedSizeIcon name="close" />+ </ActionIcon>+ </Group>+ </Group>+ );+}++type LocationSectionProps = {+ finding: ContentDiagnosticsBaseFinding;+ tab: ContentDiagnosticsTab;+};++function LocationSection({ finding, tab }: LocationSectionProps) {+ const links = getBreadcrumbLinks(finding);++ return (+ <div role="region" aria-label={t`Location`}>+ <Breadcrumbs+ lh="1rem"+ separator={<FixedSizeIcon name="chevronright" size={12} />}+ >+ {links.map((link) => (+ <Anchor+ key={link.id}+ component={Link}+ className={S.wrap}+ lh="1rem"+ to={link.url}+ target="_blank"+ onClick={() => trackContentDiagnosticsLocationOpened(tab, link.id)}+ >+ <Group gap="sm" wrap="nowrap">+ {link.icon != null && (+ <FixedSizeIcon name={link.icon} aria-hidden />+ )}+ {link.label}+ </Group>+ </Anchor>+ ))}+ </Breadcrumbs>+ </div>+ );+}++type InfoSectionProps = {+ finding: ContentDiagnosticsBaseFinding;+ extraInfo?: SidebarExtraInfo;+};++function InfoSection({ finding, extraInfo }: InfoSectionProps) {+ const { description, owner, creator, view_count } = finding.details;+ const { created_at } = finding;++ return (+ <Card p={0} shadow="none" withBorder role="region" aria-label={t`Info`}>+ <InfoSectionItem label={t`Type`}>+ <Box className={S.wrap}>{getEntityTypeLabel(finding)}</Box>+ </InfoSectionItem>+ <InfoSectionItem label={t`Description`}>+ {description != null && description.length > 0 ? (+ <Box className={S.wrap}>{description}</Box>+ ) : (+ <Text c="text-secondary">{t`No description`}</Text>+ )}+ </InfoSectionItem>+ {creator != null && (+ <InfoSectionItem label={t`Created by`}>+ <Box className={S.wrap}>{getUserName(creator)}</Box>+ </InfoSectionItem>+ )}+ {owner != null && (+ <InfoSectionItem label={t`Owner`}>+ <Box className={S.wrap}>{getUserName(owner)}</Box>+ </InfoSectionItem>+ )}+ <InfoSectionItem label={t`Created at`}>+ {created_at != null ? (+ <DateTime value={created_at} unit="day" />+ ) : (+ <Text c="text-secondary">{t`Unknown`}</Text>+ )}+ </InfoSectionItem>+ {view_count != null && (+ <InfoSectionItem label={t`Views`}>+ <Box className={S.wrap}>{view_count}</Box>+ </InfoSectionItem>+ )}+ {extraInfo != null && (+ <InfoSectionItem label={extraInfo.label}>+ {extraInfo.children}+ </InfoSectionItem>+ )}+ </Card>+ );+}++type InfoSectionItemProps = {+ label: string;+ children?: ReactNode;+};++function InfoSectionItem({ label, children }: InfoSectionItemProps) {+ return (+ <Stack className={S.section} p="md" gap="xs">+ <Text className={S.wrap} c="text-secondary" fz="sm" lh="h5">+ {label}+ </Text>+ <Group lh="h4" justify="space-between" wrap="nowrap">+ {children}+ </Group>+ </Stack>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.unit.spec.tsxnew file mode 100644index 00000000000..e16e66aa964--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/DiagnosticsSidebar.unit.spec.tsx@@ -0,0 +1,146 @@+import userEvent from "@testing-library/user-event";++import { renderWithProviders, screen, within } from "__support__/ui";+import type { ContentDiagnosticsStaleFinding } from "metabase-types/api";+import {+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsStaleFinding,+ createMockContentDiagnosticsUser,+} from "metabase-types/api/mocks";++const { trackSimpleEvent } = jest.requireMock("metabase/analytics");++import {+ DiagnosticsSidebar,+ type SidebarExtraInfo,+} from "./DiagnosticsSidebar";++type SetupOpts = {+ finding?: ContentDiagnosticsStaleFinding;+ extraInfo?: SidebarExtraInfo;+};++function setup({+ finding = createMockContentDiagnosticsStaleFinding(),+ extraInfo = { label: "Last used", children: "3 months ago" },+}: SetupOpts = {}) {+ const onClose = jest.fn();++ renderWithProviders(+ <DiagnosticsSidebar+ finding={finding}+ tab="stale"+ extraInfo={extraInfo}+ onClose={onClose}+ />,+ { withRouter: true },+ );++ return { onClose };+}++describe("DiagnosticsSidebar", () => {+ it("renders the entity name and type", () => {+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ entity_type: "card",+ entity_display_name: "Sales overview",+ }),+ });++ expect(+ screen.getByRole("region", { name: "Details for Sales overview" }),+ ).toBeInTheDocument();+ expect(screen.getByText("Sales overview")).toBeInTheDocument();+ expect(screen.getByText("Question")).toBeInTheDocument();+ });++ it("renders the creator and view count", () => {+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ details: {+ creator: createMockContentDiagnosticsUser({ name: "Ada Lovelace" }),+ view_count: 42,+ },+ }),+ });++ expect(screen.getByText("Created by")).toBeInTheDocument();+ expect(screen.getByText("Ada Lovelace")).toBeInTheDocument();+ expect(screen.getByText("Views")).toBeInTheDocument();+ expect(screen.getByText("42")).toBeInTheDocument();+ });++ it("renders the location breadcrumb", () => {+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ details: {+ collection: createMockContentDiagnosticsCollection({+ name: "Marketing",+ }),+ },+ }),+ });++ const location = screen.getByRole("region", { name: "Location" });+ expect(within(location).getByText("Marketing")).toBeInTheDocument();+ });++ it("tracks opening a location breadcrumb", async () => {+ trackSimpleEvent.mockClear();+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 9,+ name: "Reports",+ }),+ },+ }),+ });++ await userEvent.click(screen.getByRole("link", { name: "Reports" }));+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_location_opened",+ triggered_from: "stale",+ event_detail: "9",+ }),+ );+ });++ it("shows a fallback when there is no description", () => {+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ details: { description: null },+ }),+ });++ expect(screen.getByText("No description")).toBeInTheDocument();+ });++ it("omits the owner row when there is no owner", () => {+ setup({+ finding: createMockContentDiagnosticsStaleFinding({+ details: { owner: null },+ }),+ });++ expect(screen.queryByText("Owner")).not.toBeInTheDocument();+ });++ it("renders the extra info slot after the built-in fields", () => {+ setup({ extraInfo: { label: "Last run", children: "Yesterday" } });++ expect(screen.getByText("Last run")).toBeInTheDocument();+ expect(screen.getByText("Yesterday")).toBeInTheDocument();+ });++ it("calls onClose when the close button is clicked", async () => {+ const { onClose } = setup();++ await userEvent.click(screen.getByRole("button", { name: "Close" }));++ expect(onClose).toHaveBeenCalledTimes(1);+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/index.tsnew file mode 100644index 00000000000..8de7fbb411a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsSidebar/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/DiagnosticsThresholdFilter.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/DiagnosticsThresholdFilter.tsxnew file mode 100644index 00000000000..cee6faf7960--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/DiagnosticsThresholdFilter.tsx@@ -0,0 +1,36 @@+import { Input, Select } from "metabase/ui";++type DiagnosticsThresholdFilterProps = {+ label: string;+ placeholder: string;+ options: { value: number; label: string }[];+ value: number | undefined;+ onChange: (value: number | undefined) => void;+};++export function DiagnosticsThresholdFilter({+ label,+ placeholder,+ options,+ value,+ onChange,+}: DiagnosticsThresholdFilterProps) {+ return (+ <Input.Wrapper label={label}>+ <Select+ mt="sm"+ data={options.map((option) => ({+ value: String(option.value),+ label: option.label,+ }))}+ value={value !== undefined ? String(value) : null}+ placeholder={placeholder}+ clearable+ comboboxProps={{ withinPortal: false, floatingStrategy: "fixed" }}+ onChange={(selected) =>+ onChange(selected !== null ? Number(selected) : undefined)+ }+ />+ </Input.Wrapper>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/index.tsnew file mode 100644index 00000000000..a5648d2c7a9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DiagnosticsThresholdFilter/index.ts@@ -0,0 +1 @@+export * from "./DiagnosticsThresholdFilter";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContent.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContent.tsxnew file mode 100644index 00000000000..2eace43c57e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContent.tsx@@ -0,0 +1,65 @@+import { useListDuplicatedFindingsQuery } from "metabase-enterprise/api";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";++import {+ type DuplicatedContentParams,+ duplicatedContentConfig,+} from "../config";++import { ContentDiagnosticsContent } from "./ContentDiagnosticsContent";+import { DuplicatedContentFilterBar } from "./DuplicatedContentFilterBar";+import { DuplicatedContentSidebar } from "./DuplicatedContentSidebar";+import { DuplicatedContentTable } from "./DuplicatedContentTable";+import type { ContentDiagnosticsParamsOptions } from "./types";++type DuplicatedContentProps = {+ params: DuplicatedContentParams;+ isLoadingParams: boolean;+ onParamsChange: (+ params: DuplicatedContentParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+};++export function DuplicatedContent({+ params,+ isLoadingParams,+ onParamsChange,+}: DuplicatedContentProps) {+ const config = duplicatedContentConfig;+ const { page = 0, query } = params;+ const filterOptions = config.getFilterOptions(params);+ const { data, isFetching, isLoading, error } = useListDuplicatedFindingsQuery(+ {+ query,+ "entity-types": config.getEntityTypesParam(filterOptions.entityTypes),+ "include-personal-collections": filterOptions.includePersonalCollections,+ "min-duplicate-count": filterOptions.minDuplicateCount,+ "sort-column": params.sortColumn,+ "sort-direction": params.sortDirection,+ limit: PAGE_SIZE,+ offset: page * PAGE_SIZE,+ },+ { skip: isLoadingParams },+ );++ return (+ <ContentDiagnosticsContent+ tab={"duplicated"}+ config={config}+ params={params}+ filterOptions={filterOptions}+ isLoadingParams={isLoadingParams}+ onParamsChange={onParamsChange}+ data={data}+ isFetchingFindings={isFetching}+ isLoadingFindings={isLoading}+ error={error}+ renderFilterBar={(props) => <DuplicatedContentFilterBar {...props} />}+ renderTable={(props) => <DuplicatedContentTable {...props} />}+ renderSidebar={(finding, onClose) => (+ <DuplicatedContentSidebar finding={finding} onClose={onClose} />+ )}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/DuplicatedContentFilterBar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/DuplicatedContentFilterBar.tsxnew file mode 100644index 00000000000..ed790d1bb59--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/DuplicatedContentFilterBar.tsx@@ -0,0 +1,38 @@+import { Group } from "metabase/ui";++import { duplicatedContentConfig } from "../../config";+import { DiagnosticsSearchInput } from "../DiagnosticsSearchInput";+import { DuplicatedContentFilterPicker } from "../DuplicatedContentFilterPicker";+import type {+ ContentDiagnosticsFilterBarProps,+ DuplicatedContentFilterOptions,+} from "../types";++export function DuplicatedContentFilterBar({+ query,+ filterOptions,+ isLoading,+ onQueryChange,+ onFilterOptionsChange,+ onReset,+}: ContentDiagnosticsFilterBarProps<DuplicatedContentFilterOptions>) {+ const hasDefaultFilterOptions = duplicatedContentConfig.areFilterOptionsEqual(+ filterOptions,+ duplicatedContentConfig.getDefaultFilterOptions(),+ );+ const canReset = !hasDefaultFilterOptions || query !== undefined;++ return (+ <Group gap="md" align="center" wrap="nowrap">+ <DiagnosticsSearchInput query={query} onQueryChange={onQueryChange} />+ <DuplicatedContentFilterPicker+ filterOptions={filterOptions}+ isDisabled={isLoading}+ hasDefaultOptions={hasDefaultFilterOptions}+ canReset={canReset}+ onFilterOptionsChange={onFilterOptionsChange}+ onReset={onReset}+ />+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/index.tsnew file mode 100644index 00000000000..bc2b05dd886--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterBar/index.ts@@ -0,0 +1 @@+export * from "./DuplicatedContentFilterBar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/DuplicatedContentFilterPicker.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/DuplicatedContentFilterPicker.tsxnew file mode 100644index 00000000000..86f563fd66b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/DuplicatedContentFilterPicker.tsx@@ -0,0 +1,36 @@+import { t } from "ttag";++import { DiagnosticsFilterPicker } from "../DiagnosticsFilterPicker";+import { DiagnosticsThresholdFilter } from "../DiagnosticsThresholdFilter";+import { getDuplicateCountFilterOptions } from "../duplicated-utils";+import type {+ ContentDiagnosticsFilterPickerProps,+ DuplicatedContentFilterOptions,+} from "../types";+import { ALL_FILTER_TYPES } from "../utils";++export function DuplicatedContentFilterPicker({+ filterOptions,+ onFilterOptionsChange,+ ...props+}: ContentDiagnosticsFilterPickerProps<DuplicatedContentFilterOptions>) {+ return (+ <DiagnosticsFilterPicker+ {...props}+ filterOptions={filterOptions}+ onFilterOptionsChange={onFilterOptionsChange}+ availableTypes={ALL_FILTER_TYPES}+ extraFilters={+ <DiagnosticsThresholdFilter+ label={t`Duplicates`}+ placeholder={t`Any number of duplicates`}+ options={getDuplicateCountFilterOptions()}+ value={filterOptions.minDuplicateCount}+ onChange={(minDuplicateCount) =>+ onFilterOptionsChange({ ...filterOptions, minDuplicateCount })+ }+ />+ }+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/index.tsnew file mode 100644index 00000000000..ee87d17713d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentFilterPicker/index.ts@@ -0,0 +1 @@+export * from "./DuplicatedContentFilterPicker";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.module.css b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.module.cssnew file mode 100644index 00000000000..84407542953--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.module.css@@ -0,0 +1,13 @@+.row:not(:last-child) {+ border-bottom: 1px solid var(--mb-color-border-neutral);+}++.nowrap {+ white-space: nowrap;+}++.wrap {+ overflow-wrap: anywhere;+ word-break: break-word;+ white-space: normal;+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.tsxnew file mode 100644index 00000000000..1a54aaab387--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.tsx@@ -0,0 +1,143 @@+import { c, msgid, ngettext, t } from "ttag";++import { useGetIcon } from "metabase/hooks/use-icon";+import { Link } from "metabase/router";+import { Anchor, Card, Group, Stack, Text, Tooltip } from "metabase/ui";+import type {+ ContentDiagnosticsDuplicateEntity,+ ContentDiagnosticsDuplicatedFinding,+} from "metabase-types/api";++import { trackContentDiagnosticsDuplicateOpened } from "../../analytics";+import { DiagnosticsEntityIcon } from "../DiagnosticsEntityIcon";+import { DiagnosticsSidebar } from "../DiagnosticsSidebar";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";+import {+ getDuplicateEntityName,+ getDuplicateEntityUrl,+ getEntityTypeLabel,+} from "../utils";++import S from "./DuplicatedContentSidebar.module.css";++type DuplicatedContentSidebarProps = {+ finding: ContentDiagnosticsDuplicatedFinding;+ onClose: () => void;+};++export function DuplicatedContentSidebar({+ finding,+ onClose,+}: DuplicatedContentSidebarProps) {+ return (+ <DiagnosticsSidebar finding={finding} tab="duplicated" onClose={onClose}>+ <DuplicatesSection+ duplicateCount={finding.duplicate_count}+ duplicateEntities={finding.details.duplicate_entities}+ />+ </DiagnosticsSidebar>+ );+}++type DuplicatesSectionProps = {+ duplicateCount: number;+ duplicateEntities: ContentDiagnosticsDuplicateEntity[];+};++function DuplicatesSection({+ duplicateCount,+ duplicateEntities,+}: DuplicatesSectionProps) {+ const title = c("{0} is the number of duplicates of an item")+ .t`Duplicates (${duplicateCount})`;+ // Duplicates are filtered by permission- and personal-collection- server-side, so the list can be+ // shorter than the count the heading shows.+ const hiddenCount = duplicateCount - duplicateEntities.length;++ return (+ <Stack gap="sm" role="region" aria-label={t`Duplicates`}>+ <Text c="text-secondary" fz="sm" lh="h5">+ {title}+ </Text>+ {duplicateEntities.length > 0 && (+ <Card p={0} shadow="none" withBorder>+ {duplicateEntities.map((entity) => (+ <DuplicateEntityRow+ key={`${entity.entity_type}-${entity.id}`}+ entity={entity}+ />+ ))}+ </Card>+ )}+ {hiddenCount > 0 && (+ <Text c="text-secondary">+ {duplicateEntities.length === 0+ ? t`None of these duplicates are visible to you.`+ : ngettext(+ msgid`${hiddenCount} duplicate isn't visible to you.`,+ `${hiddenCount} duplicates aren't visible to you.`,+ hiddenCount,+ )}+ </Text>+ )}+ </Stack>+ );+}++type DuplicateEntityRowProps = {+ entity: ContentDiagnosticsDuplicateEntity;+};++function DuplicateEntityRow({ entity }: DuplicateEntityRowProps) {+ const getIcon = useGetIcon();+ const name = getDuplicateEntityName(entity);+ const typeLabel = getEntityTypeLabel(entity);++ const trackEntityOpened = () =>+ trackContentDiagnosticsDuplicateOpened({+ tab: "duplicated",+ entityId: entity.id,+ entityType: entity.entity_type,+ });+ const linkLabel = `${name}, ${typeLabel}`;++ return (+ <Group+ className={S.row}+ p="md"+ gap="md"+ wrap="nowrap"+ justify="space-between"+ >+ <Anchor+ className={S.wrap}+ component={Link}+ miw={0}+ to={getDuplicateEntityUrl(entity)}+ target="_blank"+ aria-label={linkLabel}+ onClick={trackEntityOpened}+ >+ <Group component="span" gap="sm" wrap="nowrap" miw={0}>+ <Tooltip label={typeLabel} openDelay={TOOLTIP_OPEN_DELAY_MS}>+ <DiagnosticsEntityIcon+ entity={entity}+ getIcon={getIcon}+ aria-hidden+ />+ </Tooltip>+ <span className={S.wrap}>{name}</span>+ </Group>+ </Anchor>+ {entity.view_count != null && (+ <Text className={S.nowrap} c="text-secondary">+ {ngettext(+ msgid`${entity.view_count} view`,+ `${entity.view_count} views`,+ entity.view_count,+ )}+ </Text>+ )}+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.unit.spec.tsxnew file mode 100644index 00000000000..67d4cc4ccbd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/DuplicatedContentSidebar.unit.spec.tsx@@ -0,0 +1,176 @@+import userEvent from "@testing-library/user-event";++import { renderWithProviders, screen, within } from "__support__/ui";+import type { ContentDiagnosticsDuplicatedFinding } from "metabase-types/api";+import {+ createMockContentDiagnosticsDuplicateEntity,+ createMockContentDiagnosticsDuplicatedFinding,+} from "metabase-types/api/mocks";++import { DuplicatedContentSidebar } from "./DuplicatedContentSidebar";++const { trackSimpleEvent } = jest.requireMock("metabase/analytics");++function setup(+ finding: ContentDiagnosticsDuplicatedFinding = createMockContentDiagnosticsDuplicatedFinding(),+) {+ renderWithProviders(+ <DuplicatedContentSidebar finding={finding} onClose={jest.fn()} />,+ { withRouter: true },+ );+}++function getDuplicatesSection() {+ return screen.getByRole("region", { name: "Duplicates" });+}++describe("DuplicatedContentSidebar", () => {+ it("lists the duplicates with their count in the heading", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 2,+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 11,+ name: "Revenue",+ }),+ createMockContentDiagnosticsDuplicateEntity({+ id: 12,+ name: "revenue",+ }),+ ],+ },+ }),+ );++ const section = getDuplicatesSection();+ expect(within(section).getByText("Duplicates (2)")).toBeInTheDocument();+ expect(within(section).getByText("Revenue")).toBeInTheDocument();+ expect(within(section).getByText("revenue")).toBeInTheDocument();+ });++ it("links each duplicate and labels it with its type", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 1,+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 11,+ name: "Revenue",+ entity_type: "card",+ card_type: "model",+ }),+ ],+ },+ }),+ );++ const link = within(getDuplicatesSection()).getByRole("link", {+ name: "Revenue, Model",+ });+ expect(link).toHaveAttribute("href", expect.stringContaining("/model/11"));+ });++ it("tracks opening a duplicate peer", async () => {+ trackSimpleEvent.mockClear();+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 11,+ name: "Revenue",+ entity_type: "card",+ }),+ ],+ },+ }),+ );++ await userEvent.click(+ within(getDuplicatesSection()).getByRole("link", {+ name: "Revenue, Question",+ }),+ );+ expect(trackSimpleEvent).toHaveBeenCalledWith(+ expect.objectContaining({+ event: "content_diagnostics_duplicate_opened",+ triggered_from: "duplicated",+ target_id: 11,+ event_detail: "card",+ }),+ );+ });++ it("shows the view count of a duplicate", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 1,+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({ view_count: 12 }),+ ],+ },+ }),+ );++ expect(+ within(getDuplicatesSection()).getByText("12 views"),+ ).toBeInTheDocument();+ });++ it("omits the view count for duplicates that have none", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 1,+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ entity_type: "transform",+ name: "daily_rollup",+ view_count: undefined,+ }),+ ],+ },+ }),+ );++ const section = getDuplicatesSection();+ expect(within(section).getByText("daily_rollup")).toBeInTheDocument();+ expect(within(section).queryByText(/views?$/)).not.toBeInTheDocument();+ });++ it("reports how many duplicates aren't visible to the user", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 3,+ details: {+ duplicate_entities: [createMockContentDiagnosticsDuplicateEntity()],+ },+ }),+ );++ expect(+ within(getDuplicatesSection()).getByText(+ "2 duplicates aren't visible to you.",+ ),+ ).toBeInTheDocument();+ });++ it("reports when none of the duplicates are visible to the user", () => {+ setup(+ createMockContentDiagnosticsDuplicatedFinding({+ duplicate_count: 2,+ details: { duplicate_entities: [] },+ }),+ );++ expect(+ within(getDuplicatesSection()).getByText(+ "None of these duplicates are visible to you.",+ ),+ ).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/index.tsnew file mode 100644index 00000000000..566b6da3279--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentSidebar/index.ts@@ -0,0 +1 @@+export * from "./DuplicatedContentSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/DuplicatedContentTable.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/DuplicatedContentTable.tsxnew file mode 100644index 00000000000..af326c95d9b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/DuplicatedContentTable.tsx@@ -0,0 +1,120 @@+import type { OnChangeFn, Row, RowSelectionState } from "@tanstack/react-table";+import { useCallback, useMemo } from "react";+import { t } from "ttag";++import { useScrollToTop } from "metabase/common/hooks";+import { useGetIcon } from "metabase/hooks/use-icon";+import { MonitorEmptyState } from "metabase/monitor/components/MonitorEmptyState";+import {+ Card,+ LoadingOverlay,+ TreeTable,+ TreeTableSkeleton,+ useTreeTableInstance,+} from "metabase/ui";+import type { Sorting } from "metabase/utils/sorting";+import type {+ ContentDiagnosticsDuplicatedFinding,+ ContentDiagnosticsDuplicatedSortColumn,+} from "metabase-types/api";++import {+ type DuplicatedContentParams,+ duplicatedContentConfig,+} from "../../config";+import { useOptionalSortingState } from "../../hooks/use-optional-sorting-state";++import { SKELETON_COLUMN_WIDTHS, getColumns } from "./columns";++type DuplicatedContentTableProps = {+ findings: ContentDiagnosticsDuplicatedFinding[];+ params: DuplicatedContentParams;+ sortOptions: Sorting<ContentDiagnosticsDuplicatedSortColumn> | undefined;+ isFetching?: boolean;+ isLoading?: boolean;+ rowSelection: RowSelectionState;+ onSelect?: (finding: ContentDiagnosticsDuplicatedFinding) => void;+ onSortOptionsChange: (+ sortOptions: Sorting<ContentDiagnosticsDuplicatedSortColumn> | undefined,+ ) => void;+ onRowSelectionChange: OnChangeFn<RowSelectionState>;+};++export function DuplicatedContentTable({+ findings,+ params,+ sortOptions,+ isFetching = false,+ isLoading = false,+ rowSelection,+ onSelect,+ onSortOptionsChange,+ onRowSelectionChange,+}: DuplicatedContentTableProps) {+ const getIcon = useGetIcon();+ const columns = useMemo(() => getColumns(getIcon), [getIcon]);+ const { sortingState, onSortingChange } = useOptionalSortingState({+ sortOptions,+ columns: duplicatedContentConfig.sortColumns,+ onSortOptionsChange,+ });++ const handleRowActivate = useCallback(+ (row: Row<ContentDiagnosticsDuplicatedFinding>) => onSelect?.(row.original),+ [onSelect],+ );++ const treeTableInstance =+ useTreeTableInstance<ContentDiagnosticsDuplicatedFinding>({+ data: findings,+ columns,+ sorting: sortingState,+ manualSorting: true,+ getNodeId: (finding) => String(finding.id),+ enableRowSelection: true,+ rowSelection,+ onRowActivate: handleRowActivate,+ onRowSelectionChange,+ onSortingChange,+ });++ useScrollToTop({+ ref: treeTableInstance.containerRef,+ keys: [params],+ skip: isFetching,+ });++ return (+ <Card+ flex="0 1 auto"+ mih={0}+ p={0}+ pos="relative"+ withBorder+ data-testid="duplicated-content-list"+ >+ {isLoading ? (+ <TreeTableSkeleton+ showCheckboxes+ columnWidths={SKELETON_COLUMN_WIDTHS}+ />+ ) : (+ <>+ <LoadingOverlay visible={isFetching} data-testid="loading-overlay" />+ <TreeTable+ instance={treeTableInstance}+ showCheckboxes+ onHeaderCheckboxClick={() =>+ treeTableInstance.table.toggleAllRowsSelected()+ }+ headerCheckboxAriaLabel={t`Select all`}+ emptyState={+ <MonitorEmptyState label={t`No duplicated content found`} />+ }+ onRowClick={handleRowActivate}+ />+ </>+ )}+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/columns.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/columns.tsxnew file mode 100644index 00000000000..bfea0064ae1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/columns.tsx@@ -0,0 +1,41 @@+import { t } from "ttag";++import type { useGetIcon } from "metabase/hooks/use-icon";+import { Ellipsified, type TreeTableColumnDef } from "metabase/ui";+import type { ContentDiagnosticsDuplicatedFinding } from "metabase-types/api";++import { getCommonColumns } from "../common-columns";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";++export function getColumns(+ getIcon: ReturnType<typeof useGetIcon>,+): TreeTableColumnDef<ContentDiagnosticsDuplicatedFinding>[] {+ const { name, entityType, collectionName, createdBy, createdAt } =+ getCommonColumns<ContentDiagnosticsDuplicatedFinding>(getIcon);+ const duplicateCountColumn: TreeTableColumnDef<ContentDiagnosticsDuplicatedFinding> =+ {+ id: "duplicate-count",+ header: t`Duplicates`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 120,+ accessorFn: (finding) => finding.duplicate_count,+ cell: ({ row }) => (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {row.original.duplicate_count}+ </Ellipsified>+ ),+ };++ return [+ entityType,+ name,+ collectionName,+ duplicateCountColumn,+ createdBy,+ createdAt,+ ];+}++export const SKELETON_COLUMN_WIDTHS = [0.06, 0.34, 0.24, 0.11, 0.13, 0.12];diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/index.tsnew file mode 100644index 00000000000..db0fe81eafa--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/DuplicatedContentTable/index.ts@@ -0,0 +1 @@+export * from "./DuplicatedContentTable";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContent.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContent.tsxnew file mode 100644index 00000000000..23aaf35723f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContent.tsx@@ -0,0 +1,82 @@+import { useListImbalancedFindingsQuery } from "metabase-enterprise/api";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";+import type { ContentDiagnosticsImbalancedFindingType } from "metabase-types/api";++import {+ type ImbalancedContentParams,+ getImbalancedContentConfig,+} from "../config";++import { ContentDiagnosticsContent } from "./ContentDiagnosticsContent";+import { ImbalancedContentFilterBar } from "./ImbalancedContentFilterBar";+import { ImbalancedContentSidebar } from "./ImbalancedContentSidebar";+import { ImbalancedContentTable } from "./ImbalancedContentTable";+import { getImbalancedEmptyStateLabel } from "./imbalanced-utils";+import type { ContentDiagnosticsParamsOptions } from "./types";++type ImbalancedContentProps = {+ mode: ContentDiagnosticsImbalancedFindingType;+ params: ImbalancedContentParams;+ isLoadingParams: boolean;+ onParamsChange: (+ params: ImbalancedContentParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+};++export function ImbalancedContent({+ mode,+ params,+ isLoadingParams,+ onParamsChange,+}: ImbalancedContentProps) {+ const config = getImbalancedContentConfig(mode);+ const { page = 0, query } = params;+ const filterOptions = config.getFilterOptions(params);+ const { data, isFetching, isLoading, error } = useListImbalancedFindingsQuery(+ {+ query,+ "entity-types": config.getEntityTypesParam(filterOptions.entityTypes),+ "include-personal-collections": filterOptions.includePersonalCollections,+ "finding-types": [mode],+ "sort-column": params.sortColumn,+ "sort-direction": params.sortDirection,+ limit: PAGE_SIZE,+ offset: page * PAGE_SIZE,+ },+ { skip: isLoadingParams },+ );++ return (+ <ContentDiagnosticsContent+ tab={mode}+ config={config}+ params={params}+ filterOptions={filterOptions}+ isLoadingParams={isLoadingParams}+ onParamsChange={onParamsChange}+ data={data}+ isFetchingFindings={isFetching}+ isLoadingFindings={isLoading}+ error={error}+ enableBulkTrash={mode !== "crowded"}+ renderFilterBar={(props) => (+ <ImbalancedContentFilterBar {...props} mode={mode} />+ )}+ renderTable={(props) => (+ <ImbalancedContentTable+ {...props}+ mode={mode}+ emptyStateLabel={getImbalancedEmptyStateLabel(mode)}+ />+ )}+ renderSidebar={(finding, onClose) => (+ <ImbalancedContentSidebar+ finding={finding}+ tab={mode}+ onClose={onClose}+ />+ )}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/ImbalancedContentFilterBar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/ImbalancedContentFilterBar.tsxnew file mode 100644index 00000000000..7c06d393315--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/ImbalancedContentFilterBar.tsx@@ -0,0 +1,47 @@+import { Group } from "metabase/ui";+import type { ContentDiagnosticsImbalancedFindingType } from "metabase-types/api";++import { getImbalancedContentConfig } from "../../config";+import { DiagnosticsSearchInput } from "../DiagnosticsSearchInput";+import { ImbalancedContentFilterPicker } from "../ImbalancedContentFilterPicker";+import type {+ ContentDiagnosticsFilterBarProps,+ ImbalancedContentFilterOptions,+} from "../types";++type ImbalancedContentFilterBarProps =+ ContentDiagnosticsFilterBarProps<ImbalancedContentFilterOptions> & {+ mode: ContentDiagnosticsImbalancedFindingType;+ };++export function ImbalancedContentFilterBar({+ mode,+ query,+ filterOptions,+ isLoading,+ onQueryChange,+ onFilterOptionsChange,+ onReset,+}: ImbalancedContentFilterBarProps) {+ const config = getImbalancedContentConfig(mode);+ const hasDefaultFilterOptions = config.areFilterOptionsEqual(+ filterOptions,+ config.getDefaultFilterOptions(),+ );+ const canReset = !hasDefaultFilterOptions || query !== undefined;++ return (+ <Group gap="md" align="center" wrap="nowrap">+ <DiagnosticsSearchInput query={query} onQueryChange={onQueryChange} />+ <ImbalancedContentFilterPicker+ availableTypes={[...config.entityTypes]}+ filterOptions={filterOptions}+ isDisabled={isLoading}+ hasDefaultOptions={hasDefaultFilterOptions}+ canReset={canReset}+ onFilterOptionsChange={onFilterOptionsChange}+ onReset={onReset}+ />+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/index.tsnew file mode 100644index 00000000000..4a6ce10240b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterBar/index.ts@@ -0,0 +1 @@+export * from "./ImbalancedContentFilterBar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/ImbalancedContentFilterPicker.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/ImbalancedContentFilterPicker.tsxnew file mode 100644index 00000000000..bc46691523b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/ImbalancedContentFilterPicker.tsx@@ -0,0 +1,19 @@+import type { ContentDiagnosticsFilterType } from "metabase-types/api";++import { DiagnosticsFilterPicker } from "../DiagnosticsFilterPicker";+import type {+ ContentDiagnosticsFilterPickerProps,+ ImbalancedContentFilterOptions,+} from "../types";++type ImbalancedContentFilterPickerProps =+ ContentDiagnosticsFilterPickerProps<ImbalancedContentFilterOptions> & {+ availableTypes: ContentDiagnosticsFilterType[];+ };++export function ImbalancedContentFilterPicker({+ availableTypes,+ ...props+}: ImbalancedContentFilterPickerProps) {+ return <DiagnosticsFilterPicker {...props} availableTypes={availableTypes} />;+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/index.tsnew file mode 100644index 00000000000..f1f27e36c4a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentFilterPicker/index.ts@@ -0,0 +1 @@+export * from "./ImbalancedContentFilterPicker";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.tsxnew file mode 100644index 00000000000..bacf536f378--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.tsx@@ -0,0 +1,45 @@+import { msgid, ngettext, t } from "ttag";++import type {+ ContentDiagnosticsImbalancedFinding,+ ContentDiagnosticsImbalancedFindingType,+} from "metabase-types/api";++import { DiagnosticsSidebar } from "../DiagnosticsSidebar";+import { getContentCountLabel } from "../imbalanced-utils";++type ImbalancedContentSidebarProps = {+ finding: ContentDiagnosticsImbalancedFinding;+ tab: ContentDiagnosticsImbalancedFindingType;+ onClose: () => void;+};++export function ImbalancedContentSidebar({+ finding,+ tab,+ onClose,+}: ImbalancedContentSidebarProps) {+ const { content_count, details } = finding;+ const contentCountLabel =+ tab === "crowded" &&+ finding.entity_type === "dashboard" &&+ details.unit === "dashcards"+ ? ngettext(+ msgid`${content_count} dashcard on one tab`,+ `${content_count} dashcards on one tab`,+ content_count,+ )+ : getContentCountLabel(content_count, details.unit);++ return (+ <DiagnosticsSidebar+ finding={finding}+ tab={tab}+ onClose={onClose}+ extraInfo={{+ label: t`Content count`,+ children: contentCountLabel,+ }}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.unit.spec.tsxnew file mode 100644index 00000000000..cd983310fe3--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/ImbalancedContentSidebar.unit.spec.tsx@@ -0,0 +1,47 @@+import { renderWithProviders, screen } from "__support__/ui";+import type { ContentDiagnosticsImbalancedFinding } from "metabase-types/api";+import { createMockContentDiagnosticsImbalancedFinding } from "metabase-types/api/mocks";++import { ImbalancedContentSidebar } from "./ImbalancedContentSidebar";++function setup(+ finding: ContentDiagnosticsImbalancedFinding = createMockContentDiagnosticsImbalancedFinding(),+ tab: "empty" | "sparse" | "crowded" = "empty",+) {+ renderWithProviders(+ <ImbalancedContentSidebar+ finding={finding}+ tab={tab}+ onClose={jest.fn()}+ />,+ { withRouter: true },+ );+}++describe("ImbalancedContentSidebar", () => {+ it("renders the content count section", () => {+ setup(+ createMockContentDiagnosticsImbalancedFinding({+ content_count: 101,+ details: { unit: "items" },+ }),+ );++ expect(screen.getByText("Content count")).toBeInTheDocument();+ expect(screen.getByText("101 items")).toBeInTheDocument();+ });++ it("uses the unit the finding was measured in", () => {+ setup(+ createMockContentDiagnosticsImbalancedFinding({+ finding_type: "crowded",+ entity_type: "dashboard",+ content_count: 21,+ details: { unit: "dashcards" },+ }),+ "crowded",+ );++ expect(screen.getByText("21 dashcards on one tab")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/index.tsnew file mode 100644index 00000000000..0609cf56888--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentSidebar/index.ts@@ -0,0 +1 @@+export * from "./ImbalancedContentSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/ImbalancedContentTable.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/ImbalancedContentTable.tsxnew file mode 100644index 00000000000..40403f20d74--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/ImbalancedContentTable.tsx@@ -0,0 +1,123 @@+import type { OnChangeFn, Row, RowSelectionState } from "@tanstack/react-table";+import { useCallback, useMemo } from "react";+import { t } from "ttag";++import { useScrollToTop } from "metabase/common/hooks";+import { useGetIcon } from "metabase/hooks/use-icon";+import { MonitorEmptyState } from "metabase/monitor/components/MonitorEmptyState";+import {+ Card,+ LoadingOverlay,+ TreeTable,+ TreeTableSkeleton,+ useTreeTableInstance,+} from "metabase/ui";+import type { Sorting } from "metabase/utils/sorting";+import type {+ ContentDiagnosticsImbalancedFinding,+ ContentDiagnosticsImbalancedFindingType,+ ContentDiagnosticsImbalancedSortColumn,+} from "metabase-types/api";++import {+ type ImbalancedContentParams,+ getImbalancedContentConfig,+} from "../../config";+import { useOptionalSortingState } from "../../hooks/use-optional-sorting-state";++import { SKELETON_COLUMN_WIDTHS, getColumns } from "./columns";++type ImbalancedContentTableProps = {+ mode: ContentDiagnosticsImbalancedFindingType;+ findings: ContentDiagnosticsImbalancedFinding[];+ params: ImbalancedContentParams;+ sortOptions: Sorting<ContentDiagnosticsImbalancedSortColumn> | undefined;+ emptyStateLabel: string;+ isFetching?: boolean;+ isLoading?: boolean;+ rowSelection: RowSelectionState;+ onSelect?: (finding: ContentDiagnosticsImbalancedFinding) => void;+ onSortOptionsChange: (+ sortOptions: Sorting<ContentDiagnosticsImbalancedSortColumn> | undefined,+ ) => void;+ onRowSelectionChange: OnChangeFn<RowSelectionState>;+};++export function ImbalancedContentTable({+ mode,+ findings,+ params,+ sortOptions,+ emptyStateLabel,+ isFetching = false,+ isLoading = false,+ rowSelection,+ onSelect,+ onSortOptionsChange,+ onRowSelectionChange,+}: ImbalancedContentTableProps) {+ const getIcon = useGetIcon();+ const columns = useMemo(() => getColumns(mode, getIcon), [mode, getIcon]);+ const { sortingState, onSortingChange } = useOptionalSortingState({+ sortOptions,+ columns: getImbalancedContentConfig(mode).sortColumns,+ onSortOptionsChange,+ });++ const handleRowActivate = useCallback(+ (row: Row<ContentDiagnosticsImbalancedFinding>) => onSelect?.(row.original),+ [onSelect],+ );++ const treeTableInstance =+ useTreeTableInstance<ContentDiagnosticsImbalancedFinding>({+ data: findings,+ columns,+ sorting: sortingState,+ manualSorting: true,+ getNodeId: (finding) => String(finding.id),+ enableRowSelection: true,+ rowSelection,+ onRowActivate: handleRowActivate,+ onRowSelectionChange,+ onSortingChange,+ });++ useScrollToTop({+ ref: treeTableInstance.containerRef,+ keys: [params],+ skip: isFetching,+ });++ return (+ <Card+ flex="0 1 auto"+ mih={0}+ p={0}+ pos="relative"+ withBorder+ data-testid="imbalanced-content-list"+ >+ {isLoading ? (+ <TreeTableSkeleton+ showCheckboxes+ columnWidths={SKELETON_COLUMN_WIDTHS}+ />+ ) : (+ <>+ <LoadingOverlay visible={isFetching} data-testid="loading-overlay" />+ <TreeTable+ instance={treeTableInstance}+ showCheckboxes+ onHeaderCheckboxClick={() =>+ treeTableInstance.table.toggleAllRowsSelected()+ }+ headerCheckboxAriaLabel={t`Select all`}+ emptyState={<MonitorEmptyState label={emptyStateLabel} />}+ onRowClick={handleRowActivate}+ />+ </>+ )}+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/columns.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/columns.tsxnew file mode 100644index 00000000000..bbe9a1fdb7c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/columns.tsx@@ -0,0 +1,45 @@+import { t } from "ttag";++import type { useGetIcon } from "metabase/hooks/use-icon";+import { Ellipsified, type TreeTableColumnDef } from "metabase/ui";+import type {+ ContentDiagnosticsImbalancedFinding,+ ContentDiagnosticsImbalancedFindingType,+} from "metabase-types/api";++import { getCommonColumns } from "../common-columns";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";++export function getColumns(+ mode: ContentDiagnosticsImbalancedFindingType,+ getIcon: ReturnType<typeof useGetIcon>,+): TreeTableColumnDef<ContentDiagnosticsImbalancedFinding>[] {+ const { name, entityType, collectionName, createdBy, createdAt } =+ getCommonColumns<ContentDiagnosticsImbalancedFinding>(getIcon);+ const contentCountColumn: TreeTableColumnDef<ContentDiagnosticsImbalancedFinding> =+ {+ id: "content-count",+ header: t`Content count`,+ enableSorting: mode !== "crowded",+ sortDescFirst: false,+ width: "auto",+ minWidth: 120,+ accessorFn: (finding) => finding.content_count,+ cell: ({ row }) => (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {row.original.content_count}+ </Ellipsified>+ ),+ };++ return [+ entityType,+ name,+ collectionName,+ contentCountColumn,+ createdBy,+ createdAt,+ ];+}++export const SKELETON_COLUMN_WIDTHS = [0.06, 0.34, 0.24, 0.11, 0.13, 0.12];diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/index.tsnew file mode 100644index 00000000000..24368855609--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/ImbalancedContentTable/index.ts@@ -0,0 +1 @@+export * from "./ImbalancedContentTable";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContent.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContent.tsxnew file mode 100644index 00000000000..e0c4481d2d7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContent.tsx@@ -0,0 +1,62 @@+import { useListSlowFindingsQuery } from "metabase-enterprise/api";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";++import { type SlowContentParams, slowContentConfig } from "../config";++import { ContentDiagnosticsContent } from "./ContentDiagnosticsContent";+import { SlowContentFilterBar } from "./SlowContentFilterBar";+import { SlowContentSidebar } from "./SlowContentSidebar";+import { SlowContentTable } from "./SlowContentTable";+import type { ContentDiagnosticsParamsOptions } from "./types";++type SlowContentProps = {+ params: SlowContentParams;+ isLoadingParams: boolean;+ onParamsChange: (+ params: SlowContentParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+};++export function SlowContent({+ params,+ isLoadingParams,+ onParamsChange,+}: SlowContentProps) {+ const config = slowContentConfig;+ const { page = 0, query } = params;+ const filterOptions = config.getFilterOptions(params);+ const { data, isFetching, isLoading, error } = useListSlowFindingsQuery(+ {+ query,+ "entity-types": config.getEntityTypesParam(filterOptions.entityTypes),+ "include-personal-collections": filterOptions.includePersonalCollections,+ "min-duration-ms": filterOptions.minDurationMs,+ "sort-column": params.sortColumn,+ "sort-direction": params.sortDirection,+ limit: PAGE_SIZE,+ offset: page * PAGE_SIZE,+ },+ { skip: isLoadingParams },+ );++ return (+ <ContentDiagnosticsContent+ tab={"slow"}+ config={config}+ params={params}+ filterOptions={filterOptions}+ isLoadingParams={isLoadingParams}+ onParamsChange={onParamsChange}+ data={data}+ isFetchingFindings={isFetching}+ isLoadingFindings={isLoading}+ error={error}+ renderFilterBar={(props) => <SlowContentFilterBar {...props} />}+ renderTable={(props) => <SlowContentTable {...props} />}+ renderSidebar={(finding, onClose) => (+ <SlowContentSidebar finding={finding} onClose={onClose} />+ )}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/SlowContentFilterBar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/SlowContentFilterBar.tsxnew file mode 100644index 00000000000..0c9b8f8fcfe--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/SlowContentFilterBar.tsx@@ -0,0 +1,38 @@+import { Group } from "metabase/ui";++import { slowContentConfig } from "../../config";+import { DiagnosticsSearchInput } from "../DiagnosticsSearchInput";+import { SlowContentFilterPicker } from "../SlowContentFilterPicker";+import type {+ ContentDiagnosticsFilterBarProps,+ SlowContentFilterOptions,+} from "../types";++export function SlowContentFilterBar({+ query,+ filterOptions,+ isLoading,+ onQueryChange,+ onFilterOptionsChange,+ onReset,+}: ContentDiagnosticsFilterBarProps<SlowContentFilterOptions>) {+ const hasDefaultFilterOptions = slowContentConfig.areFilterOptionsEqual(+ filterOptions,+ slowContentConfig.getDefaultFilterOptions(),+ );+ const canReset = !hasDefaultFilterOptions || query !== undefined;++ return (+ <Group gap="md" align="center" wrap="nowrap">+ <DiagnosticsSearchInput query={query} onQueryChange={onQueryChange} />+ <SlowContentFilterPicker+ filterOptions={filterOptions}+ isDisabled={isLoading}+ hasDefaultOptions={hasDefaultFilterOptions}+ canReset={canReset}+ onFilterOptionsChange={onFilterOptionsChange}+ onReset={onReset}+ />+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/index.tsnew file mode 100644index 00000000000..6d00d0f0179--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterBar/index.ts@@ -0,0 +1 @@+export * from "./SlowContentFilterBar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/SlowContentFilterPicker.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/SlowContentFilterPicker.tsxnew file mode 100644index 00000000000..6ba5734ed6d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/SlowContentFilterPicker.tsx@@ -0,0 +1,36 @@+import { t } from "ttag";++import { DiagnosticsFilterPicker } from "../DiagnosticsFilterPicker";+import { DiagnosticsThresholdFilter } from "../DiagnosticsThresholdFilter";+import { getDurationFilterOptions } from "../slow-utils";+import type {+ ContentDiagnosticsFilterPickerProps,+ SlowContentFilterOptions,+} from "../types";+import { ALL_NON_COLLECTION_FILTER_TYPES } from "../utils";++export function SlowContentFilterPicker({+ filterOptions,+ onFilterOptionsChange,+ ...props+}: ContentDiagnosticsFilterPickerProps<SlowContentFilterOptions>) {+ return (+ <DiagnosticsFilterPicker+ {...props}+ filterOptions={filterOptions}+ onFilterOptionsChange={onFilterOptionsChange}+ availableTypes={ALL_NON_COLLECTION_FILTER_TYPES}+ extraFilters={+ <DiagnosticsThresholdFilter+ label={t`Duration`}+ placeholder={t`Any duration`}+ options={getDurationFilterOptions()}+ value={filterOptions.minDurationMs}+ onChange={(minDurationMs) =>+ onFilterOptionsChange({ ...filterOptions, minDurationMs })+ }+ />+ }+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/index.tsnew file mode 100644index 00000000000..746b373c95c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentFilterPicker/index.ts@@ -0,0 +1 @@+export * from "./SlowContentFilterPicker";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.tsxnew file mode 100644index 00000000000..de80e8c26dd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.tsx@@ -0,0 +1,60 @@+import { t } from "ttag";++import { Link } from "metabase/router";+import { Anchor, Card, Stack, Text } from "metabase/ui";+import { formatDurationLong } from "metabase/utils/formatting";+import type { ContentDiagnosticsSlowFinding } from "metabase-types/api";++import { trackContentDiagnosticsEntityOpened } from "../../analytics";+import { DiagnosticsSidebar } from "../DiagnosticsSidebar";+import { getDuplicateEntityName, getDuplicateEntityUrl } from "../utils";++type SlowContentSidebarProps = {+ finding: ContentDiagnosticsSlowFinding;+ onClose: () => void;+};++export function SlowContentSidebar({+ finding,+ onClose,+}: SlowContentSidebarProps) {+ return (+ <DiagnosticsSidebar+ finding={finding}+ tab="slow"+ onClose={onClose}+ extraInfo={{+ label: t`Duration`,+ children: formatDurationLong(finding.duration_ms),+ }}+ >+ {finding.details.slow_entities != null &&+ finding.details.slow_entities.length > 0 && (+ <Stack gap="sm" role="region" aria-label={t`Slow items`}>+ <Text c="text-secondary">{t`Slow items`}</Text>+ <Card withBorder p="md">+ <Stack gap="sm">+ {finding.details.slow_entities.map((entity) => (+ <Anchor+ key={entity.id}+ component={Link}+ to={getDuplicateEntityUrl(entity)}+ target="_blank"+ onClick={() =>+ trackContentDiagnosticsEntityOpened({+ tab: "slow",+ entityId: entity.id,+ entityType: entity.entity_type,+ })+ }+ >+ {getDuplicateEntityName(entity)}+ </Anchor>+ ))}+ </Stack>+ </Card>+ </Stack>+ )}+ </DiagnosticsSidebar>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.unit.spec.tsxnew file mode 100644index 00000000000..b62f13658e1--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/SlowContentSidebar.unit.spec.tsx@@ -0,0 +1,56 @@+import { renderWithProviders, screen } from "__support__/ui";+import type { ContentDiagnosticsSlowFinding } from "metabase-types/api";+import { createMockContentDiagnosticsSlowFinding } from "metabase-types/api/mocks";++import { SlowContentSidebar } from "./SlowContentSidebar";++function setup(+ finding: ContentDiagnosticsSlowFinding = createMockContentDiagnosticsSlowFinding(),+) {+ renderWithProviders(+ <SlowContentSidebar finding={finding} onClose={jest.fn()} />,+ { withRouter: true },+ );+}++describe("SlowContentSidebar", () => {+ it("renders the duration section", () => {+ setup(createMockContentDiagnosticsSlowFinding({ duration_ms: 5000 }));++ expect(screen.getByText("Duration")).toBeInTheDocument();+ expect(screen.getByText("5.0s")).toBeInTheDocument();+ });++ it("links to the slow cards behind a container finding", () => {+ setup(+ createMockContentDiagnosticsSlowFinding({+ entity_type: "dashboard",+ details: {+ slow_entities: [+ {+ id: 33,+ name: "Slow report",+ entity_type: "card",+ card_type: "model",+ view_count: 7,+ },+ ],+ },+ }),+ );++ expect(+ screen.getByRole("region", { name: "Slow items" }),+ ).toBeInTheDocument();+ expect(screen.getByRole("link", { name: "Slow report" })).toHaveAttribute(+ "href",+ expect.stringMatching(/^\/model\/33/),+ );+ });++ it("humanizes a duration spanning minutes", () => {+ setup(createMockContentDiagnosticsSlowFinding({ duration_ms: 65000 }));++ expect(screen.getByText("1m 5s")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/index.tsnew file mode 100644index 00000000000..ecfe29c1296--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentSidebar/index.ts@@ -0,0 +1 @@+export * from "./SlowContentSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/SlowContentTable.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/SlowContentTable.tsxnew file mode 100644index 00000000000..aa2dee70166--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/SlowContentTable.tsx@@ -0,0 +1,116 @@+import type { OnChangeFn, Row, RowSelectionState } from "@tanstack/react-table";+import { useCallback, useMemo } from "react";+import { t } from "ttag";++import { useScrollToTop } from "metabase/common/hooks";+import { useGetIcon } from "metabase/hooks/use-icon";+import { MonitorEmptyState } from "metabase/monitor/components/MonitorEmptyState";+import {+ Card,+ LoadingOverlay,+ TreeTable,+ TreeTableSkeleton,+ useTreeTableInstance,+} from "metabase/ui";+import type { Sorting } from "metabase/utils/sorting";+import type {+ ContentDiagnosticsSlowFinding,+ ContentDiagnosticsSlowSortColumn,+} from "metabase-types/api";++import { type SlowContentParams, slowContentConfig } from "../../config";+import { useOptionalSortingState } from "../../hooks/use-optional-sorting-state";++import { SKELETON_COLUMN_WIDTHS, getColumns } from "./columns";++type SlowContentTableProps = {+ findings: ContentDiagnosticsSlowFinding[];+ params: SlowContentParams;+ sortOptions: Sorting<ContentDiagnosticsSlowSortColumn> | undefined;+ isFetching?: boolean;+ isLoading?: boolean;+ rowSelection: RowSelectionState;+ onSelect?: (finding: ContentDiagnosticsSlowFinding) => void;+ onSortOptionsChange: (+ sortOptions: Sorting<ContentDiagnosticsSlowSortColumn> | undefined,+ ) => void;+ onRowSelectionChange: OnChangeFn<RowSelectionState>;+};++export function SlowContentTable({+ findings,+ params,+ sortOptions,+ isFetching = false,+ isLoading = false,+ rowSelection,+ onSelect,+ onSortOptionsChange,+ onRowSelectionChange,+}: SlowContentTableProps) {+ const getIcon = useGetIcon();+ const columns = useMemo(() => getColumns(getIcon), [getIcon]);+ const { sortingState, onSortingChange } = useOptionalSortingState({+ sortOptions,+ columns: slowContentConfig.sortColumns,+ onSortOptionsChange,+ });++ const handleRowActivate = useCallback(+ (row: Row<ContentDiagnosticsSlowFinding>) => onSelect?.(row.original),+ [onSelect],+ );++ const treeTableInstance = useTreeTableInstance<ContentDiagnosticsSlowFinding>(+ {+ data: findings,+ columns,+ sorting: sortingState,+ manualSorting: true,+ getNodeId: (finding) => String(finding.id),+ enableRowSelection: true,+ rowSelection,+ onRowActivate: handleRowActivate,+ onRowSelectionChange,+ onSortingChange,+ },+ );++ useScrollToTop({+ ref: treeTableInstance.containerRef,+ keys: [params],+ skip: isFetching,+ });++ return (+ <Card+ flex="0 1 auto"+ mih={0}+ p={0}+ pos="relative"+ withBorder+ data-testid="slow-content-list"+ >+ {isLoading ? (+ <TreeTableSkeleton+ showCheckboxes+ columnWidths={SKELETON_COLUMN_WIDTHS}+ />+ ) : (+ <>+ <LoadingOverlay visible={isFetching} data-testid="loading-overlay" />+ <TreeTable+ instance={treeTableInstance}+ showCheckboxes+ onHeaderCheckboxClick={() =>+ treeTableInstance.table.toggleAllRowsSelected()+ }+ headerCheckboxAriaLabel={t`Select all`}+ emptyState={<MonitorEmptyState label={t`No slow content found`} />}+ onRowClick={handleRowActivate}+ />+ </>+ )}+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/columns.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/columns.tsxnew file mode 100644index 00000000000..da96689e2a5--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/columns.tsx@@ -0,0 +1,34 @@+import { t } from "ttag";++import type { useGetIcon } from "metabase/hooks/use-icon";+import { Ellipsified, type TreeTableColumnDef } from "metabase/ui";+import { formatDurationLong } from "metabase/utils/formatting";+import type { ContentDiagnosticsSlowFinding } from "metabase-types/api";++import { getCommonColumns } from "../common-columns";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";++export function getColumns(+ getIcon: ReturnType<typeof useGetIcon>,+): TreeTableColumnDef<ContentDiagnosticsSlowFinding>[] {+ const { name, entityType, collectionName, createdBy, createdAt } =+ getCommonColumns<ContentDiagnosticsSlowFinding>(getIcon);+ const duration: TreeTableColumnDef<ContentDiagnosticsSlowFinding> = {+ id: "duration-ms",+ header: t`Duration`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 120,+ accessorFn: (finding) => finding.duration_ms,+ cell: ({ row }) => (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {formatDurationLong(row.original.duration_ms)}+ </Ellipsified>+ ),+ };++ return [entityType, name, collectionName, createdBy, createdAt, duration];+}++export const SKELETON_COLUMN_WIDTHS = [0.06, 0.34, 0.24, 0.13, 0.12, 0.11];diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/index.tsnew file mode 100644index 00000000000..a4f5cd838e8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/SlowContentTable/index.ts@@ -0,0 +1 @@+export * from "./SlowContentTable";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContent.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContent.tsxnew file mode 100644index 00000000000..27cee46252d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContent.tsx@@ -0,0 +1,62 @@+import { useListStaleFindingsQuery } from "metabase-enterprise/api";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";++import { type StaleContentParams, staleContentConfig } from "../config";++import { ContentDiagnosticsContent } from "./ContentDiagnosticsContent";+import { StaleContentFilterBar } from "./StaleContentFilterBar";+import { StaleContentSidebar } from "./StaleContentSidebar";+import { StaleContentTable } from "./StaleContentTable";+import type { ContentDiagnosticsParamsOptions } from "./types";++type StaleContentProps = {+ params: StaleContentParams;+ isLoadingParams: boolean;+ onParamsChange: (+ params: StaleContentParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+};++export function StaleContent({+ params,+ isLoadingParams,+ onParamsChange,+}: StaleContentProps) {+ const config = staleContentConfig;+ const { page = 0, query } = params;+ const filterOptions = config.getFilterOptions(params);+ const { data, isFetching, isLoading, error } = useListStaleFindingsQuery(+ {+ query,+ "entity-types": config.getEntityTypesParam(filterOptions.entityTypes),+ "include-personal-collections": filterOptions.includePersonalCollections,+ "threshold-days": filterOptions.thresholdDays,+ "sort-column": params.sortColumn,+ "sort-direction": params.sortDirection,+ limit: PAGE_SIZE,+ offset: page * PAGE_SIZE,+ },+ { skip: isLoadingParams },+ );++ return (+ <ContentDiagnosticsContent+ tab={"stale"}+ config={config}+ params={params}+ filterOptions={filterOptions}+ isLoadingParams={isLoadingParams}+ onParamsChange={onParamsChange}+ data={data}+ isFetchingFindings={isFetching}+ isLoadingFindings={isLoading}+ error={error}+ renderFilterBar={(props) => <StaleContentFilterBar {...props} />}+ renderTable={(props) => <StaleContentTable {...props} />}+ renderSidebar={(finding, onClose) => (+ <StaleContentSidebar finding={finding} onClose={onClose} />+ )}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/StaleContentFilterBar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/StaleContentFilterBar.tsxnew file mode 100644index 00000000000..d5b796d56f0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/StaleContentFilterBar.tsx@@ -0,0 +1,38 @@+import { Group } from "metabase/ui";++import { staleContentConfig } from "../../config";+import { DiagnosticsSearchInput } from "../DiagnosticsSearchInput";+import { StaleContentFilterPicker } from "../StaleContentFilterPicker";+import type {+ ContentDiagnosticsFilterBarProps,+ StaleContentFilterOptions,+} from "../types";++export function StaleContentFilterBar({+ query,+ filterOptions,+ isLoading,+ onQueryChange,+ onFilterOptionsChange,+ onReset,+}: ContentDiagnosticsFilterBarProps<StaleContentFilterOptions>) {+ const hasDefaultFilterOptions = staleContentConfig.areFilterOptionsEqual(+ filterOptions,+ staleContentConfig.getDefaultFilterOptions(),+ );+ const canReset = !hasDefaultFilterOptions || query !== undefined;++ return (+ <Group gap="md" align="center" wrap="nowrap">+ <DiagnosticsSearchInput query={query} onQueryChange={onQueryChange} />+ <StaleContentFilterPicker+ filterOptions={filterOptions}+ isDisabled={isLoading}+ hasDefaultOptions={hasDefaultFilterOptions}+ canReset={canReset}+ onFilterOptionsChange={onFilterOptionsChange}+ onReset={onReset}+ />+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/index.tsnew file mode 100644index 00000000000..0adf89806b9--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterBar/index.ts@@ -0,0 +1 @@+export * from "./StaleContentFilterBar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/StaleContentFilterPicker.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/StaleContentFilterPicker.tsxnew file mode 100644index 00000000000..87b3d1098fe--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/StaleContentFilterPicker.tsx@@ -0,0 +1,36 @@+import { t } from "ttag";++import { DiagnosticsFilterPicker } from "../DiagnosticsFilterPicker";+import { DiagnosticsThresholdFilter } from "../DiagnosticsThresholdFilter";+import { getThresholdDaysFilterOptions } from "../stale-utils";+import type {+ ContentDiagnosticsFilterPickerProps,+ StaleContentFilterOptions,+} from "../types";+import { ALL_NON_COLLECTION_FILTER_TYPES } from "../utils";++export function StaleContentFilterPicker({+ filterOptions,+ onFilterOptionsChange,+ ...props+}: ContentDiagnosticsFilterPickerProps<StaleContentFilterOptions>) {+ return (+ <DiagnosticsFilterPicker+ {...props}+ filterOptions={filterOptions}+ onFilterOptionsChange={onFilterOptionsChange}+ availableTypes={ALL_NON_COLLECTION_FILTER_TYPES}+ extraFilters={+ <DiagnosticsThresholdFilter+ label={t`Inactive for`}+ placeholder={t`Any length of time`}+ options={getThresholdDaysFilterOptions()}+ value={filterOptions.thresholdDays}+ onChange={(thresholdDays) =>+ onFilterOptionsChange({ ...filterOptions, thresholdDays })+ }+ />+ }+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/index.tsnew file mode 100644index 00000000000..b67c0e9ace7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentFilterPicker/index.ts@@ -0,0 +1 @@+export * from "./StaleContentFilterPicker";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.tsxnew file mode 100644index 00000000000..56a85fe5487--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.tsx@@ -0,0 +1,37 @@+import { t } from "ttag";++import { DateTime } from "metabase/common/components/DateTime";+import { Text } from "metabase/ui";+import type { ContentDiagnosticsStaleFinding } from "metabase-types/api";++import { DiagnosticsSidebar } from "../DiagnosticsSidebar";+import { getLastActiveLabel } from "../stale-utils";++type StaleContentSidebarProps = {+ finding: ContentDiagnosticsStaleFinding;+ onClose: () => void;+};++export function StaleContentSidebar({+ finding,+ onClose,+}: StaleContentSidebarProps) {+ const { last_active_at } = finding;++ return (+ <DiagnosticsSidebar+ finding={finding}+ tab="stale"+ onClose={onClose}+ extraInfo={{+ label: getLastActiveLabel(finding.entity_type),+ children:+ last_active_at != null ? (+ <DateTime value={last_active_at} unit="day" />+ ) : (+ <Text c="text-secondary">{t`Never`}</Text>+ ),+ }}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.unit.spec.tsxnew file mode 100644index 00000000000..af799036c7d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/StaleContentSidebar.unit.spec.tsx@@ -0,0 +1,42 @@+import { renderWithProviders, screen } from "__support__/ui";+import type { ContentDiagnosticsStaleFinding } from "metabase-types/api";+import { createMockContentDiagnosticsStaleFinding } from "metabase-types/api/mocks";++import { StaleContentSidebar } from "./StaleContentSidebar";++function setup(+ finding: ContentDiagnosticsStaleFinding = createMockContentDiagnosticsStaleFinding(),+) {+ renderWithProviders(+ <StaleContentSidebar finding={finding} onClose={jest.fn()} />,+ { withRouter: true },+ );+}++describe("StaleContentSidebar", () => {+ it("renders the last-activity section for a card", () => {+ setup(+ createMockContentDiagnosticsStaleFinding({+ entity_type: "card",+ last_active_at: "2026-03-01T00:00:00Z",+ }),+ );++ expect(screen.getByText("Last used")).toBeInTheDocument();+ expect(screen.queryByText("Never")).not.toBeInTheDocument();+ });++ it("uses the entity-specific activity label for a dashboard", () => {+ setup(+ createMockContentDiagnosticsStaleFinding({ entity_type: "dashboard" }),+ );++ expect(screen.getByText("Last viewed")).toBeInTheDocument();+ });++ it("falls back to Never when there is no last activity", () => {+ setup(createMockContentDiagnosticsStaleFinding({ last_active_at: null }));++ expect(screen.getByText("Never")).toBeInTheDocument();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/index.tsnew file mode 100644index 00000000000..8dd38b9de6b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentSidebar/index.ts@@ -0,0 +1 @@+export * from "./StaleContentSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/StaleContentTable.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/StaleContentTable.tsxnew file mode 100644index 00000000000..8d81edc1455--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/StaleContentTable.tsx@@ -0,0 +1,115 @@+import type { OnChangeFn, Row, RowSelectionState } from "@tanstack/react-table";+import { useCallback, useMemo } from "react";+import { t } from "ttag";++import { useScrollToTop } from "metabase/common/hooks";+import { useGetIcon } from "metabase/hooks/use-icon";+import { MonitorEmptyState } from "metabase/monitor/components/MonitorEmptyState";+import {+ Card,+ LoadingOverlay,+ TreeTable,+ TreeTableSkeleton,+ useTreeTableInstance,+} from "metabase/ui";+import type { Sorting } from "metabase/utils/sorting";+import type {+ ContentDiagnosticsStaleFinding,+ ContentDiagnosticsStaleSortColumn,+} from "metabase-types/api";++import { type StaleContentParams, staleContentConfig } from "../../config";+import { useOptionalSortingState } from "../../hooks/use-optional-sorting-state";++import { SKELETON_COLUMN_WIDTHS, getColumns } from "./columns";++type StaleContentTableProps = {+ findings: ContentDiagnosticsStaleFinding[];+ params: StaleContentParams;+ sortOptions: Sorting<ContentDiagnosticsStaleSortColumn> | undefined;+ isFetching?: boolean;+ isLoading?: boolean;+ rowSelection: RowSelectionState;+ onSelect?: (finding: ContentDiagnosticsStaleFinding) => void;+ onSortOptionsChange: (+ sortOptions: Sorting<ContentDiagnosticsStaleSortColumn> | undefined,+ ) => void;+ onRowSelectionChange: OnChangeFn<RowSelectionState>;+};++export function StaleContentTable({+ findings,+ params,+ sortOptions,+ isFetching = false,+ isLoading = false,+ rowSelection,+ onSelect,+ onSortOptionsChange,+ onRowSelectionChange,+}: StaleContentTableProps) {+ const getIcon = useGetIcon();+ const columns = useMemo(() => getColumns(getIcon), [getIcon]);+ const { sortingState, onSortingChange } = useOptionalSortingState({+ sortOptions,+ columns: staleContentConfig.sortColumns,+ onSortOptionsChange,+ });++ const handleRowActivate = useCallback(+ (row: Row<ContentDiagnosticsStaleFinding>) => onSelect?.(row.original),+ [onSelect],+ );++ const treeTableInstance =+ useTreeTableInstance<ContentDiagnosticsStaleFinding>({+ data: findings,+ columns,+ sorting: sortingState,+ manualSorting: true,+ getNodeId: (finding) => String(finding.id),+ enableRowSelection: true,+ rowSelection,+ onRowActivate: handleRowActivate,+ onRowSelectionChange,+ onSortingChange,+ });++ useScrollToTop({+ ref: treeTableInstance.containerRef,+ keys: [params],+ skip: isFetching,+ });++ return (+ <Card+ flex="0 1 auto"+ mih={0}+ p={0}+ pos="relative"+ withBorder+ data-testid="stale-content-list"+ >+ {isLoading ? (+ <TreeTableSkeleton+ showCheckboxes+ columnWidths={SKELETON_COLUMN_WIDTHS}+ />+ ) : (+ <>+ <LoadingOverlay visible={isFetching} data-testid="loading-overlay" />+ <TreeTable+ instance={treeTableInstance}+ showCheckboxes+ onHeaderCheckboxClick={() =>+ treeTableInstance.table.toggleAllRowsSelected()+ }+ headerCheckboxAriaLabel={t`Select all`}+ emptyState={<MonitorEmptyState label={t`No stale content found`} />}+ onRowClick={handleRowActivate}+ />+ </>+ )}+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/columns.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/columns.tsxnew file mode 100644index 00000000000..d53c17776d5--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/columns.tsx@@ -0,0 +1,40 @@+import { t } from "ttag";++import { DateTime } from "metabase/common/components/DateTime";+import type { useGetIcon } from "metabase/hooks/use-icon";+import { Ellipsified, Text, type TreeTableColumnDef } from "metabase/ui";+import type { ContentDiagnosticsStaleFinding } from "metabase-types/api";++import { getCommonColumns } from "../common-columns";+import { TOOLTIP_OPEN_DELAY_MS } from "../constants";++export function getColumns(+ getIcon: ReturnType<typeof useGetIcon>,+): TreeTableColumnDef<ContentDiagnosticsStaleFinding>[] {+ const { name, entityType, collectionName, createdBy, createdAt } =+ getCommonColumns<ContentDiagnosticsStaleFinding>(getIcon);+ const lastActiveAt: TreeTableColumnDef<ContentDiagnosticsStaleFinding> = {+ id: "last-active-at",+ header: t`Last active`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 150,+ accessorFn: (finding) => finding.last_active_at,+ cell: ({ row }) => {+ const { last_active_at } = row.original;+ if (last_active_at == null) {+ return <Text c="text-secondary">{t`Never`}</Text>;+ }+ return (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ <DateTime value={last_active_at} unit="day" />+ </Ellipsified>+ );+ },+ };++ return [entityType, name, collectionName, createdBy, createdAt, lastActiveAt];+}++export const SKELETON_COLUMN_WIDTHS = [0.06, 0.34, 0.24, 0.13, 0.12, 0.11];diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/index.tsnew file mode 100644index 00000000000..c153bd0b22d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/StaleContentTable/index.ts@@ -0,0 +1 @@+export * from "./StaleContentTable";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/common-columns.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/common-columns.tsxnew file mode 100644index 00000000000..4b55b3290fd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/common-columns.tsx@@ -0,0 +1,119 @@+import { t } from "ttag";++import { DateTime } from "metabase/common/components/DateTime";+import type { useGetIcon } from "metabase/hooks/use-icon";+import {+ Ellipsified,+ FixedSizeIcon,+ Group,+ Text,+ type TreeTableColumnDef,+} from "metabase/ui";+import { EMPTY_CELL_PLACEHOLDER } from "metabase/utils/constants";+import type { ContentDiagnosticsBaseFinding } from "metabase-types/api";++import { DiagnosticsEntityIcon } from "./DiagnosticsEntityIcon";+import { TOOLTIP_OPEN_DELAY_MS } from "./constants";+import {+ getCollectionName,+ getEntityName,+ getEntityTypeLabel,+ getUserName,+} from "./utils";++type CommonColumns<T extends ContentDiagnosticsBaseFinding> = Record<+ "name" | "entityType" | "collectionName" | "createdBy" | "createdAt",+ TreeTableColumnDef<T>+>;++export function getCommonColumns<T extends ContentDiagnosticsBaseFinding>(+ getIcon: ReturnType<typeof useGetIcon>,+): CommonColumns<T> {+ return {+ name: {+ id: "name",+ header: t`Name`,+ enableSorting: true,+ sortDescFirst: false,+ minWidth: "auto",+ maxAutoWidth: 520,+ accessorFn: getEntityName,+ cell: ({ row }) => (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {getEntityName(row.original)}+ </Ellipsified>+ ),+ },+ entityType: {+ id: "entity-type",+ header: t`Type`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ accessorFn: (finding) => getEntityTypeLabel(finding),+ cell: ({ row }) => (+ <DiagnosticsEntityIcon+ entity={row.original}+ getIcon={getIcon}+ mx="auto"+ />+ ),+ },+ collectionName: {+ id: "collection-name",+ header: t`Location`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 120,+ maxAutoWidth: 520,+ accessorFn: (finding) => getCollectionName(finding.details.collection),+ cell: ({ row }) => {+ const collectionName = getCollectionName(+ row.original.details.collection,+ );+ return (+ <Group align="center" gap="sm" miw={0} wrap="nowrap">+ <FixedSizeIcon name="folder" />+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {collectionName}+ </Ellipsified>+ </Group>+ );+ },+ },+ createdBy: {+ id: "created-by",+ header: t`Created by`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 120,+ accessorFn: (finding) => getUserName(finding.details.creator),+ cell: ({ row }) => (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ {getUserName(row.original.details.creator)}+ </Ellipsified>+ ),+ },+ createdAt: {+ id: "created-at",+ header: t`Created at`,+ enableSorting: true,+ sortDescFirst: false,+ width: "auto",+ minWidth: 150,+ accessorFn: (finding) => finding.created_at,+ cell: ({ row }) => {+ const { created_at } = row.original;+ return created_at != null ? (+ <Ellipsified tooltipProps={{ openDelay: TOOLTIP_OPEN_DELAY_MS }}>+ <DateTime value={created_at} unit="day" />+ </Ellipsified>+ ) : (+ <Text c="text-secondary">{EMPTY_CELL_PLACEHOLDER}</Text>+ );+ },+ },+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/constants.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/constants.tsnew file mode 100644index 00000000000..3ac528c7491--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/constants.ts@@ -0,0 +1,2 @@+export const DEFAULT_INCLUDE_PERSONAL_COLLECTIONS = true;+export const TOOLTIP_OPEN_DELAY_MS = 300;diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/duplicated-utils.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/duplicated-utils.tsnew file mode 100644index 00000000000..35ffc0b35f7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/duplicated-utils.ts@@ -0,0 +1,15 @@+import { t } from "ttag";++type DuplicateCountFilterOption = {+ value: number;+ label: string;+};++export function getDuplicateCountFilterOptions(): DuplicateCountFilterOption[] {+ return [+ { value: 2, label: t`2 or more` },+ { value: 3, label: t`3 or more` },+ { value: 5, label: t`5 or more` },+ { value: 10, label: t`10 or more` },+ ];+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/imbalanced-utils.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/imbalanced-utils.tsnew file mode 100644index 00000000000..17d7ae907a7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/imbalanced-utils.ts@@ -0,0 +1,36 @@+import { match } from "ts-pattern";+import { msgid, ngettext, t } from "ttag";++import type {+ ContentDiagnosticsImbalancedFindingType,+ ContentDiagnosticsImbalancedUnit,+} from "metabase-types/api";++export function getImbalancedEmptyStateLabel(+ mode: ContentDiagnosticsImbalancedFindingType,+): string {+ return match(mode)+ .with("empty", () => t`No empty content found`)+ .with("sparse", () => t`No sparse content found`)+ .with("crowded", () => t`No crowded content found`)+ .exhaustive();+}++export function getContentCountLabel(+ count: number,+ unit: ContentDiagnosticsImbalancedUnit,+): string {+ return match(unit)+ .with("items", () =>+ ngettext(msgid`${count} item`, `${count} items`, count),+ )+ .with("dashcards", () =>+ ngettext(msgid`${count} dashcard`, `${count} dashcards`, count),+ )+ .with("rows", () => ngettext(msgid`${count} row`, `${count} rows`, count))+ .with("cards", () =>+ ngettext(msgid`${count} card`, `${count} cards`, count),+ )+ .with("tabs", () => ngettext(msgid`${count} tab`, `${count} tabs`, count))+ .exhaustive();+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/index.tsnew file mode 100644index 00000000000..cd246110b19--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/index.ts@@ -0,0 +1,8 @@+export * from "./StaleContent";+export * from "./StaleContentSidebar";+export * from "./SlowContent";+export * from "./SlowContentSidebar";+export * from "./DuplicatedContent";+export * from "./DuplicatedContentSidebar";+export * from "./ImbalancedContent";+export * from "./ImbalancedContentSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/slow-utils.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/slow-utils.tsnew file mode 100644index 00000000000..5200fe37346--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/slow-utils.ts@@ -0,0 +1,15 @@+import { t } from "ttag";++type DurationFilterOption = {+ value: number;+ label: string;+};++export function getDurationFilterOptions(): DurationFilterOption[] {+ return [+ { value: 15000, label: t`15 seconds or more` },+ { value: 30000, label: t`30 seconds or more` },+ { value: 60000, label: t`1 minute or more` },+ { value: 300000, label: t`5 minutes or more` },+ ];+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/stale-utils.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/stale-utils.tsnew file mode 100644index 00000000000..dd0cb036886--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/stale-utils.ts@@ -0,0 +1,28 @@+import { match } from "ts-pattern";+import { t } from "ttag";++import type { ContentDiagnosticsNonCollectionEntityType } from "metabase-types/api";++export function getLastActiveLabel(+ entityType: ContentDiagnosticsNonCollectionEntityType,+): string {+ return match(entityType)+ .with("card", () => t`Last used`)+ .with("dashboard", () => t`Last viewed`)+ .with("document", () => t`Last viewed`)+ .with("transform", () => t`Last run`)+ .exhaustive();+}++type ThresholdDaysFilterOption = {+ value: number;+ label: string;+};++export function getThresholdDaysFilterOptions(): ThresholdDaysFilterOption[] {+ return [+ { value: 90, label: t`90 days or more` },+ { value: 180, label: t`6 months or more` },+ { value: 365, label: t`1 year or more` },+ ];+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/types.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/types.tsnew file mode 100644index 00000000000..42d618bc870--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/types.ts@@ -0,0 +1,65 @@+import {+ CONTENT_DIAGNOSTICS_IMBALANCED_FINDING_TYPES,+ type ContentDiagnosticsFilterType,+ type ContentDiagnosticsNonCollectionFilterType,+} from "metabase-types/api";++export type ContentDiagnosticsBaseFilterOptions<+ T extends ContentDiagnosticsFilterType =+ ContentDiagnosticsNonCollectionFilterType,+> = {+ entityTypes: T[];+ includePersonalCollections: boolean;+};++export type StaleContentFilterOptions = ContentDiagnosticsBaseFilterOptions & {+ thresholdDays?: number;+};++export type SlowContentFilterOptions = ContentDiagnosticsBaseFilterOptions & {+ minDurationMs?: number;+};++export type DuplicatedContentFilterOptions =+ ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType> & {+ minDuplicateCount?: number;+ };++export type ImbalancedContentFilterOptions =+ ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>;++export type ContentDiagnosticsParamsOptions = {+ withSetLastUsedParams?: boolean;+};++/** `duplicated` is an umbrella tab over the duplicated finding types. */+export const CONTENT_DIAGNOSTICS_TABS = [+ "stale",+ "slow",+ "duplicated",+ ...CONTENT_DIAGNOSTICS_IMBALANCED_FINDING_TYPES,+] as const;+export type ContentDiagnosticsTab = (typeof CONTENT_DIAGNOSTICS_TABS)[number];++export type ContentDiagnosticsFilterDimension =+ | "entity_type"+ | "personal_collections"+ | "threshold";++export type ContentDiagnosticsFilterPickerProps<TOptions> = {+ filterOptions: TOptions;+ isDisabled?: boolean;+ hasDefaultOptions?: boolean;+ canReset?: boolean;+ onFilterOptionsChange: (filterOptions: TOptions) => void;+ onReset: () => void;+};++export type ContentDiagnosticsFilterBarProps<TOptions> = {+ query?: string;+ filterOptions: TOptions;+ isLoading: boolean;+ onQueryChange: (query: string | undefined) => void;+ onFilterOptionsChange: (filterOptions: TOptions) => void;+ onReset: () => void;+};diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.tsnew file mode 100644index 00000000000..0e840a52a40--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.ts@@ -0,0 +1,260 @@+import { match } from "ts-pattern";+import { t } from "ttag";+import _ from "underscore";++import * as Urls from "metabase/urls";+import { EMPTY_CELL_PLACEHOLDER } from "metabase/utils/constants";+import {+ CONTENT_DIAGNOSTICS_FILTER_TYPES,+ CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES,+ type CollectionId,+ type CollectionNamespace,+ type ContentDiagnosticsBaseFinding,+ type ContentDiagnosticsCollection,+ type ContentDiagnosticsDuplicateEntity,+ type ContentDiagnosticsEntityId,+ type ContentDiagnosticsFilterType,+ type ContentDiagnosticsNonCollectionFilterType,+ type ContentDiagnosticsUser,+ type IconName,+} from "metabase-types/api";++import type {+ ContentDiagnosticsBaseFilterOptions,+ ContentDiagnosticsFilterDimension,+} from "./types";++export const ALL_NON_COLLECTION_FILTER_TYPES: ContentDiagnosticsNonCollectionFilterType[] =+ [...CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES];++export const ALL_FILTER_TYPES: ContentDiagnosticsFilterType[] = [+ ...CONTENT_DIAGNOSTICS_FILTER_TYPES,+];++type ContentDiagnosticsCollectionBreadcrumbEntry =+ | ContentDiagnosticsCollection+ | ContentDiagnosticsCollection["effective_ancestors"][number];++export type ContentDiagnosticsBreadcrumbLink = {+ id: CollectionId;+ label: string;+ url: string;+ icon?: IconName;+};++type ContentDiagnosticsEntityKind = Pick<+ ContentDiagnosticsBaseFinding,+ "entity_type" | "card_type"+>;++type ContentDiagnosticsEntityTarget = ContentDiagnosticsEntityKind & {+ id: ContentDiagnosticsEntityId;+ name: string;+ namespace?: CollectionNamespace;+};++export function getEntityTypeLabel(+ entity: ContentDiagnosticsEntityKind,+): string {+ return match(entity)+ .with({ entity_type: "card", card_type: "model" }, () => t`Model`)+ .with({ entity_type: "card", card_type: "metric" }, () => t`Metric`)+ .with({ entity_type: "card" }, () => t`Question`)+ .with({ entity_type: "dashboard" }, () => t`Dashboard`)+ .with({ entity_type: "document" }, () => t`Document`)+ .with({ entity_type: "transform" }, () => t`Transform`)+ .with({ entity_type: "collection" }, () => t`Collection`)+ .exhaustive();+}++export function getEntityViewLabel(+ entity: ContentDiagnosticsEntityKind,+): string {+ return match(entity)+ .with({ entity_type: "card", card_type: "model" }, () => t`View this model`)+ .with(+ { entity_type: "card", card_type: "metric" },+ () => t`View this metric`,+ )+ .with({ entity_type: "card" }, () => t`View this question`)+ .with({ entity_type: "dashboard" }, () => t`View this dashboard`)+ .with({ entity_type: "document" }, () => t`View this document`)+ .with({ entity_type: "transform" }, () => t`View this transform`)+ .with({ entity_type: "collection" }, () => t`View this collection`)+ .exhaustive();+}++function getDisplayName(name: string | null): string {+ return name ?? t`Untitled`;+}++export function getEntityName(finding: ContentDiagnosticsBaseFinding): string {+ return getDisplayName(finding.entity_display_name);+}++export function getDuplicateEntityName(+ entity: ContentDiagnosticsDuplicateEntity,+): string {+ return getDisplayName(entity.name);+}++function getTargetUrl(entity: ContentDiagnosticsEntityTarget): string {+ return match(entity)+ .with({ entity_type: "card" }, (entity) =>+ Urls.card({+ id: entity.id,+ name: entity.name,+ type: entity.card_type ?? undefined,+ }),+ )+ .with({ entity_type: "dashboard" }, (entity) =>+ Urls.dashboard({ id: entity.id, name: entity.name }),+ )+ .with({ entity_type: "document" }, (entity) =>+ Urls.document({ id: entity.id }),+ )+ .with({ entity_type: "transform" }, (entity) => Urls.transform(entity.id))+ .with({ entity_type: "collection" }, (entity) =>+ entity.namespace === "transforms"+ ? Urls.transformList({ collectionId: entity.id })+ : Urls.collection({ id: entity.id, name: entity.name }),+ )+ .exhaustive();+}++export function getEntityUrl(finding: ContentDiagnosticsBaseFinding): string {+ return getTargetUrl({+ entity_type: finding.entity_type,+ card_type: finding.card_type,+ id: finding.entity_id,+ name: getEntityName(finding),+ namespace: finding.details.collection?.namespace,+ });+}++export function getDuplicateEntityUrl(+ entity: ContentDiagnosticsDuplicateEntity,+): string {+ return getTargetUrl({+ entity_type: entity.entity_type,+ card_type: entity.card_type,+ id: entity.id,+ name: getDuplicateEntityName(entity),+ namespace: entity.namespace,+ });+}++export function getCollectionName(+ collection: ContentDiagnosticsCollection | null,+): string {+ return match(collection)+ .with(null, () => t`Our analytics`)+ .otherwise((collection) => collection.name);+}++function getCollectionBreadcrumbUrl(+ entry: ContentDiagnosticsCollectionBreadcrumbEntry,+ namespace: CollectionNamespace,+): string {+ return namespace === "transforms"+ ? Urls.transformList({ collectionId: entry.id })+ : Urls.collection({ id: entry.id, name: entry.name });+}++export function getBreadcrumbLinks(+ finding: ContentDiagnosticsBaseFinding,+): ContentDiagnosticsBreadcrumbLink[] {+ return match(finding.details.collection)+ .with(null, () => [+ {+ id: "root",+ label: t`Our analytics`,+ url: Urls.collection(),+ icon: "folder" as const,+ },+ ])+ .otherwise((collection) =>+ [...collection.effective_ancestors, collection].map((entry, index) => ({+ id: String(entry.id),+ label: entry.name,+ url: getCollectionBreadcrumbUrl(entry, collection.namespace),+ icon: index === 0 ? ("folder" as const) : undefined,+ })),+ );+}++export function getUserName(user: ContentDiagnosticsUser | null): string {+ return match(user)+ .with(null, () => EMPTY_CELL_PLACEHOLDER)+ .with(+ { type: "user" },+ (user) => user.name ?? user.email ?? EMPTY_CELL_PLACEHOLDER,+ )+ .with({ type: "external" }, (user) => user.email ?? EMPTY_CELL_PLACEHOLDER)+ .exhaustive();+}++export function getFilterTypeLabel(type: ContentDiagnosticsFilterType): string {+ return match(type)+ .with("question", () => t`Questions`)+ .with("model", () => t`Models`)+ .with("metric", () => t`Metrics`)+ .with("dashboard", () => t`Dashboards`)+ .with("document", () => t`Documents`)+ .with("transform", () => t`Transforms`)+ .with("collection", () => t`Collections`)+ .exhaustive();+}++export function areEntityTypesEqual(+ a: ContentDiagnosticsFilterType[],+ b: ContentDiagnosticsFilterType[],+): boolean {+ if (a.length !== b.length) {+ return false;+ }+ const setA = new Set(a);+ const setB = new Set(b);+ return setA.size === setB.size && a.every((type) => setB.has(type));+}++const BASE_FILTER_DIMENSIONS: Record<+ keyof ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+ ContentDiagnosticsFilterDimension+> = {+ entityTypes: "entity_type",+ includePersonalCollections: "personal_collections",+};++function getThresholds<+ T extends ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+>({ entityTypes, includePersonalCollections, ...thresholds }: T) {+ return thresholds;+}++export function getChangedFilterDimension<+ T extends ContentDiagnosticsBaseFilterOptions<ContentDiagnosticsFilterType>,+>(+ previousOptions: T,+ nextOptions: T,+): ContentDiagnosticsFilterDimension | null {+ if (+ !areEntityTypesEqual(previousOptions.entityTypes, nextOptions.entityTypes)+ ) {+ return BASE_FILTER_DIMENSIONS.entityTypes;+ }++ if (+ previousOptions.includePersonalCollections !==+ nextOptions.includePersonalCollections+ ) {+ return BASE_FILTER_DIMENSIONS.includePersonalCollections;+ }++ const hasThresholdChange = !_.isEqual(+ getThresholds(previousOptions),+ getThresholds(nextOptions),+ );++ return hasThresholdChange ? "threshold" : null;+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.unit.spec.tsnew file mode 100644index 00000000000..67cc93e8894--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/components/utils.unit.spec.ts@@ -0,0 +1,280 @@+import type { CollectionNamespace } from "metabase-types/api";+import {+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsDuplicateEntity,+ createMockContentDiagnosticsDuplicatedFinding,+ createMockContentDiagnosticsStaleFinding,+} from "metabase-types/api/mocks";++import type {+ ImbalancedContentFilterOptions,+ SlowContentFilterOptions,+ StaleContentFilterOptions,+} from "./types";+import {+ getBreadcrumbLinks,+ getChangedFilterDimension,+ getDuplicateEntityUrl,+ getEntityUrl,+} from "./utils";++describe("getEntityUrl", () => {+ it.each([+ ["question", /^\/question\/10\b/],+ ["model", /^\/model\/10\b/],+ ["metric", /^\/metric\/10\b/],+ ] as const)("points a %s at its own route", (cardType, expected) => {+ const url = getEntityUrl(+ createMockContentDiagnosticsStaleFinding({+ entity_type: "card",+ card_type: cardType,+ entity_id: 10,+ }),+ );++ expect(url).toMatch(expected);+ });++ it("falls back to the question route for a card of unknown kind", () => {+ const url = getEntityUrl(+ createMockContentDiagnosticsStaleFinding({+ entity_type: "card",+ card_type: null,+ entity_id: 10,+ }),+ );++ expect(url).toMatch(/^\/question\/10\b/);+ });++ it.each([+ ["dashboard", /^\/dashboard\/10\b/],+ ["document", /^\/document\/10\b/],+ ] as const)("points a %s at its own route", (entityType, expected) => {+ const url = getEntityUrl(+ createMockContentDiagnosticsStaleFinding({+ entity_type: entityType,+ card_type: null,+ entity_id: 10,+ }),+ );++ expect(url).toMatch(expected);+ });++ it("points a transforms collection at the transforms list", () => {+ expect(+ getEntityUrl(+ createMockContentDiagnosticsDuplicatedFinding({+ entity_type: "collection",+ entity_id: 10,+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 10,+ namespace: "transforms",+ }),+ },+ }),+ ),+ ).toBe("/data-studio/transforms?collectionId=10");+ });++ it("points a transform at the Data Studio transform route", () => {+ const url = getEntityUrl(+ createMockContentDiagnosticsStaleFinding({+ entity_type: "transform",+ card_type: null,+ entity_id: 10,+ }),+ );++ expect(url).toBe("/data-studio/transforms/10");+ });+});++describe("getDuplicateEntityUrl", () => {+ it("routes a transforms collection peer to the transforms list", () => {+ expect(+ getDuplicateEntityUrl(+ createMockContentDiagnosticsDuplicateEntity({+ id: 11,+ entity_type: "collection",+ namespace: "transforms",+ }),+ ),+ ).toBe("/data-studio/transforms?collectionId=11");+ });++ it("routes a peer by its own kind rather than the finding's", () => {+ const url = getDuplicateEntityUrl(+ createMockContentDiagnosticsDuplicateEntity({+ id: 11,+ entity_type: "card",+ card_type: "model",+ }),+ );++ expect(url).toMatch(/^\/model\/11\b/);+ });+});++describe("getBreadcrumbLinks", () => {+ it("shows the root collection when the entity has no collection", () => {+ const links = getBreadcrumbLinks(+ createMockContentDiagnosticsStaleFinding({+ details: { collection: null },+ }),+ );++ expect(links).toEqual([+ {+ id: "root",+ label: "Our analytics",+ url: "/collection/root",+ icon: "folder",+ },+ ]);+ });++ it("lists the ancestors before the collection itself, with a folder icon only on the first", () => {+ const links = getBreadcrumbLinks(+ createMockContentDiagnosticsStaleFinding({+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 3,+ name: "Reports",+ effective_ancestors: [+ { id: "root", name: "Our analytics" },+ { id: 2, name: "Finance" },+ ],+ }),+ },+ }),+ );++ expect(links.map((link) => link.label)).toEqual([+ "Our analytics",+ "Finance",+ "Reports",+ ]);+ expect(links.map((link) => link.icon)).toEqual([+ "folder",+ undefined,+ undefined,+ ]);+ expect(links[2].url).toMatch(/^\/collection\/3\b/);+ });++ it("links a collection by its namespace, not by the kind of entity in it", () => {+ const collection = createMockContentDiagnosticsCollection({+ id: 4,+ name: "Nightly",+ });+ const linkFor = (namespace: CollectionNamespace) =>+ getBreadcrumbLinks(+ createMockContentDiagnosticsStaleFinding({+ details: { collection: { ...collection, namespace } },+ }),+ )[0].url;++ expect(linkFor("transforms")).toBe(+ "/data-studio/transforms?collectionId=4",+ );+ expect(linkFor(null)).toMatch(/^\/collection\/4\b/);+ });+});++const BASE_OPTIONS: ImbalancedContentFilterOptions = {+ entityTypes: ["question", "dashboard"],+ includePersonalCollections: false,+};++const STALE_OPTIONS: StaleContentFilterOptions = {+ entityTypes: ["question", "dashboard"],+ includePersonalCollections: false,+ thresholdDays: 30,+};++const SLOW_OPTIONS: SlowContentFilterOptions = {+ entityTypes: ["question", "dashboard"],+ includePersonalCollections: false,+ minDurationMs: 1000,+};++describe("getChangedFilterDimension", () => {+ it("reports an entity type being dropped", () => {+ expect(+ getChangedFilterDimension(BASE_OPTIONS, {+ ...BASE_OPTIONS,+ entityTypes: ["question"],+ }),+ ).toBe("entity_type");+ });++ it("reports an entity type being added", () => {+ expect(+ getChangedFilterDimension(BASE_OPTIONS, {+ ...BASE_OPTIONS,+ entityTypes: ["question", "dashboard", "document"],+ }),+ ).toBe("entity_type");+ });++ it("reports the personal collections toggle", () => {+ expect(+ getChangedFilterDimension(BASE_OPTIONS, {+ ...BASE_OPTIONS,+ includePersonalCollections: true,+ }),+ ).toBe("personal_collections");+ });++ it("reports whichever numeric threshold the tab owns", () => {+ expect(+ getChangedFilterDimension(STALE_OPTIONS, {+ ...STALE_OPTIONS,+ thresholdDays: 90,+ }),+ ).toBe("threshold");++ expect(+ getChangedFilterDimension(SLOW_OPTIONS, {+ ...SLOW_OPTIONS,+ minDurationMs: 5000,+ }),+ ).toBe("threshold");+ });++ it("reports a threshold that was cleared", () => {+ expect(+ getChangedFilterDimension(STALE_OPTIONS, {+ ...STALE_OPTIONS,+ thresholdDays: undefined,+ }),+ ).toBe("threshold");+ });++ it("reports a threshold that was set where there was none", () => {+ expect(+ getChangedFilterDimension(+ { ...STALE_OPTIONS, thresholdDays: undefined },+ STALE_OPTIONS,+ ),+ ).toBe("threshold");+ });++ it("returns null when nothing changed, so no event is sent", () => {+ expect(getChangedFilterDimension(STALE_OPTIONS, { ...STALE_OPTIONS })).toBe(+ null,+ );+ });++ it("does not mistake entity type order for a change", () => {+ expect(+ getChangedFilterDimension(BASE_OPTIONS, {+ ...BASE_OPTIONS,+ entityTypes: ["dashboard", "question"],+ }),+ ).toBeNull();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.tsnew file mode 100644index 00000000000..83655d89702--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.ts@@ -0,0 +1,422 @@+import {+ type UrlStateConfig,+ type UrlStateQuery,+ getAllParamValues,+ getFirstParamValue,+ parsePage,+ parseSortColumn,+ parseSortDirection,+} from "metabase/common/hooks/use-url-state";+import * as Urls from "metabase/urls";+import {+ CONTENT_DIAGNOSTICS_DUPLICATED_SORT_COLUMNS,+ CONTENT_DIAGNOSTICS_FILTER_TYPES,+ CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS,+ CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES,+ CONTENT_DIAGNOSTICS_SLOW_SORT_COLUMNS,+ CONTENT_DIAGNOSTICS_STALE_SORT_COLUMNS,+ type ContentDiagnosticsFilterType,+ type ContentDiagnosticsImbalancedFindingType,+ type ContentDiagnosticsImbalancedSortColumn,+ type SortDirection,+} from "metabase-types/api";++import { DEFAULT_INCLUDE_PERSONAL_COLLECTIONS } from "./components/constants";+import { areEntityTypesEqual } from "./components/utils";++type ContentDiagnosticsParams<+ TEntityType extends ContentDiagnosticsFilterType,+ TSortColumn extends string,+ TThresholdParam extends string,+> = {+ page?: number;+ query?: string;+ entityTypes?: TEntityType[];+ includePersonalCollections?: boolean;+ sortColumn?: TSortColumn;+ sortDirection?: SortDirection;+} & Partial<Record<TThresholdParam, number>>;++type ContentDiagnosticsFilterOptions<+ TEntityType extends ContentDiagnosticsFilterType,+ TThresholdParam extends string,+> = {+ entityTypes: TEntityType[];+ includePersonalCollections: boolean;+} & Partial<Record<TThresholdParam, number>>;++type ContentDiagnosticsUserParams<+ TEntityType extends ContentDiagnosticsFilterType,+ TSortColumn extends string,+ TThresholdUserParam extends string,+> = {+ entity_types?: TEntityType[];+ include_personal_collections?: boolean;+ sort_column?: TSortColumn;+ sort_direction?: SortDirection;+} & Partial<Record<TThresholdUserParam, number>>;++type ThresholdConfig<+ TThresholdParam extends string,+ TThresholdUserParam extends string,+> = {+ paramKey: TThresholdParam;+ urlKey: string;+ userKey: TThresholdUserParam;+};++type CreateContentDiagnosticsConfigOptions<+ TKey extends string,+ TEntityType extends ContentDiagnosticsFilterType,+ TSortColumn extends string,+ TThresholdParam extends string,+ TThresholdUserParam extends string,+> = {+ key: TKey;+ entityTypes: readonly TEntityType[];+ sortColumns: readonly TSortColumn[];+ threshold?: ThresholdConfig<TThresholdParam, TThresholdUserParam>;+};++export function createContentDiagnosticsConfig<+ const TKey extends string,+ TEntityType extends ContentDiagnosticsFilterType,+ TSortColumn extends string,+ TThresholdParam extends string = never,+ TThresholdUserParam extends string = never,+>({+ key,+ entityTypes,+ sortColumns,+ threshold,+}: CreateContentDiagnosticsConfigOptions<+ TKey,+ TEntityType,+ TSortColumn,+ TThresholdParam,+ TThresholdUserParam+>) {+ type Params = ContentDiagnosticsParams<+ TEntityType,+ TSortColumn,+ TThresholdParam+ >;+ type FilterOptions = ContentDiagnosticsFilterOptions<+ TEntityType,+ TThresholdParam+ >;+ type UserParams = ContentDiagnosticsUserParams<+ TEntityType,+ TSortColumn,+ TThresholdUserParam+ >;++ const isEntityType = (value: unknown): value is TEntityType =>+ typeof value === "string" && entityTypes.some((type) => type === value);++ const parseEntityTypes = (value: unknown): TEntityType[] | undefined => {+ const values = Array.isArray(value) ? value : value == null ? [] : [value];+ return values.length === 0 ? undefined : values.filter(isEntityType);+ };++ const getThresholdParam = (params: Params | FilterOptions) =>+ threshold == null ? undefined : params[threshold.paramKey];++ const thresholdParam = <TKey extends string>(+ paramKey: TKey,+ value: number | undefined,+ ): Partial<Record<TKey, number>> => {+ // TypeScript widens computed object keys to `string`; the key comes from the config.+ return { [paramKey]: value } as Partial<Record<TKey, number>>;+ };++ const withThreshold = <T extends object>(+ params: T,+ value: number | undefined,+ ) =>+ Object.assign<T, Partial<Record<TThresholdParam, number>>>(+ params,+ threshold == null ? {} : thresholdParam(threshold.paramKey, value),+ );++ const getParamsWithoutDefaults = (params: Params): Params => {+ const normalizedParams: Params = {+ ...params,+ page: params.page === 0 ? undefined : params.page,+ entityTypes:+ params.entityTypes != null &&+ areEntityTypesEqual(params.entityTypes, [...entityTypes])+ ? undefined+ : params.entityTypes,+ includePersonalCollections:+ params.includePersonalCollections ===+ DEFAULT_INCLUDE_PERSONAL_COLLECTIONS+ ? undefined+ : params.includePersonalCollections,+ };+ return normalizedParams;+ };++ const parse = (query: UrlStateQuery): Params => {+ return withThreshold(+ {+ page: parsePage(query.page),+ query: Urls.parseStringParam(getFirstParamValue(query.query)),+ entityTypes: parseEntityTypes(getAllParamValues(query["entity-types"])),+ includePersonalCollections: Urls.parseBooleanParam(+ getFirstParamValue(query["include-personal-collections"]),+ ),+ sortColumn: parseSortColumn(query["sort-column"], sortColumns),+ sortDirection: parseSortDirection(query["sort-direction"]),+ },+ threshold == null+ ? undefined+ : Urls.parseNumberParam(getFirstParamValue(query[threshold.urlKey])),+ );+ };++ const serialize = (params: Params): UrlStateQuery => {+ const normalizedParams = getParamsWithoutDefaults(params);+ const query: UrlStateQuery = {+ page:+ normalizedParams.page == null+ ? undefined+ : String(normalizedParams.page),+ query: normalizedParams.query,+ "entity-types": normalizedParams.entityTypes,+ "include-personal-collections":+ normalizedParams.includePersonalCollections == null+ ? undefined+ : String(normalizedParams.includePersonalCollections),+ "sort-column": normalizedParams.sortColumn,+ "sort-direction": normalizedParams.sortDirection,+ };+ if (threshold != null) {+ const value = getThresholdParam(normalizedParams);+ query[threshold.urlKey] = value == null ? undefined : String(value);+ }+ return query;+ };++ const urlState: UrlStateConfig<Params> = { parse, serialize };++ const getDefaultFilterOptions = (): FilterOptions =>+ withThreshold(+ {+ entityTypes: [...entityTypes],+ includePersonalCollections: DEFAULT_INCLUDE_PERSONAL_COLLECTIONS,+ },+ undefined,+ );++ const getFilterOptions = (params: Params): FilterOptions =>+ withThreshold(+ {+ entityTypes: params.entityTypes ?? [...entityTypes],+ includePersonalCollections:+ params.includePersonalCollections ??+ DEFAULT_INCLUDE_PERSONAL_COLLECTIONS,+ },+ getThresholdParam(params),+ );++ const areFilterOptionsEqual = (a: FilterOptions, b: FilterOptions) =>+ areEntityTypesEqual(a.entityTypes, b.entityTypes) &&+ a.includePersonalCollections === b.includePersonalCollections &&+ getThresholdParam(a) === getThresholdParam(b);++ const getFilterParams = (filterOptions: FilterOptions): Partial<Params> => {+ return withThreshold(+ {+ entityTypes: areEntityTypesEqual(filterOptions.entityTypes, [+ ...entityTypes,+ ])+ ? undefined+ : filterOptions.entityTypes,+ includePersonalCollections:+ filterOptions.includePersonalCollections ===+ DEFAULT_INCLUDE_PERSONAL_COLLECTIONS+ ? undefined+ : filterOptions.includePersonalCollections,+ },+ getThresholdParam(filterOptions),+ );+ };++ const getEntityTypesParam = (values: TEntityType[]) =>+ areEntityTypesEqual(values, [...entityTypes]) ? undefined : values;++ const getUserParams = (params: Params): UserParams => {+ const userParams = {+ entity_types: params.entityTypes,+ include_personal_collections: params.includePersonalCollections,+ sort_column: params.sortColumn,+ sort_direction: params.sortDirection,+ };+ return Object.assign<+ typeof userParams,+ Partial<Record<TThresholdUserParam, number>>+ >(+ userParams,+ threshold == null+ ? {}+ : thresholdParam(threshold.userKey, getThresholdParam(params)),+ );+ };++ const isUserParams = (value: unknown): value is Record<string, unknown> =>+ typeof value === "object" && value != null && !Array.isArray(value);++ const parseUserParams = (value: unknown): Params => {+ if (!isUserParams(value)) {+ return {};+ }+ const userParams = value;+ const thresholdValue =+ threshold == null ? undefined : userParams[threshold.userKey];+ return withThreshold(+ {+ entityTypes: parseEntityTypes(userParams.entity_types),+ includePersonalCollections:+ typeof userParams.include_personal_collections === "boolean"+ ? userParams.include_personal_collections+ : undefined,+ sortColumn: parseSortColumn(+ typeof userParams.sort_column === "string"+ ? userParams.sort_column+ : undefined,+ sortColumns,+ ),+ sortDirection: parseSortDirection(+ typeof userParams.sort_direction === "string"+ ? userParams.sort_direction+ : undefined,+ ),+ },+ typeof thresholdValue === "number" && Number.isFinite(thresholdValue)+ ? thresholdValue+ : undefined,+ );+ };++ const urlParamKeys = [+ "page",+ "query",+ "entity-types",+ "include-personal-collections",+ ...(threshold == null ? [] : [threshold.urlKey]),+ "sort-column",+ "sort-direction",+ ];++ const hasUrlParams = (query: UrlStateQuery) =>+ urlParamKeys.some((key) => query[key] != null);++ return {+ key,+ entityTypes,+ sortColumns,+ urlState,+ getParamsWithoutDefaults,+ getDefaultFilterOptions,+ getFilterOptions,+ areFilterOptionsEqual,+ getFilterParams,+ getEntityTypesParam,+ getUserParams,+ parseUserParams,+ hasUrlParams,+ };+}++export const staleContentConfig = createContentDiagnosticsConfig({+ key: "stale",+ entityTypes: CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES,+ sortColumns: CONTENT_DIAGNOSTICS_STALE_SORT_COLUMNS,+ threshold: {+ paramKey: "thresholdDays",+ urlKey: "threshold-days",+ userKey: "threshold_days",+ },+});++export const slowContentConfig = createContentDiagnosticsConfig({+ key: "slow",+ entityTypes: CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES,+ sortColumns: CONTENT_DIAGNOSTICS_SLOW_SORT_COLUMNS,+ threshold: {+ paramKey: "minDurationMs",+ urlKey: "min-duration-ms",+ userKey: "min_duration_ms",+ },+});++export const duplicatedContentConfig = createContentDiagnosticsConfig({+ key: "duplicated",+ entityTypes: CONTENT_DIAGNOSTICS_FILTER_TYPES,+ sortColumns: CONTENT_DIAGNOSTICS_DUPLICATED_SORT_COLUMNS,+ threshold: {+ paramKey: "minDuplicateCount",+ urlKey: "min-duplicate-count",+ userKey: "min_duplicate_count",+ },+});++const EMPTY_ENTITY_TYPES: ContentDiagnosticsFilterType[] = [+ "question",+ "model",+ "metric",+ "dashboard",+ "document",+ "collection",+];+const SPARSE_ENTITY_TYPES: ContentDiagnosticsFilterType[] = [+ "dashboard",+ "collection",+];+const CROWDED_ENTITY_TYPES: ContentDiagnosticsFilterType[] = [+ "dashboard",+ "document",+ "collection",+];+const CROWDED_SORT_COLUMNS: ContentDiagnosticsImbalancedSortColumn[] =+ CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS.filter(+ (column) => column !== "content-count",+ );++const imbalancedContentConfigs = {+ empty: createContentDiagnosticsConfig({+ key: "empty",+ entityTypes: EMPTY_ENTITY_TYPES,+ sortColumns: CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS,+ }),+ sparse: createContentDiagnosticsConfig({+ key: "sparse",+ entityTypes: SPARSE_ENTITY_TYPES,+ sortColumns: CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS,+ }),+ crowded: createContentDiagnosticsConfig({+ key: "crowded",+ entityTypes: CROWDED_ENTITY_TYPES,+ sortColumns: CROWDED_SORT_COLUMNS,+ }),+};++export function getImbalancedContentConfig(+ mode: ContentDiagnosticsImbalancedFindingType,+) {+ return imbalancedContentConfigs[mode];+}++export type StaleContentParams = ReturnType<+ typeof staleContentConfig.urlState.parse+>;+export type SlowContentParams = ReturnType<+ typeof slowContentConfig.urlState.parse+>;+export type DuplicatedContentParams = ReturnType<+ typeof duplicatedContentConfig.urlState.parse+>;+export type ImbalancedContentParams = ReturnType<+ (typeof imbalancedContentConfigs)[ContentDiagnosticsImbalancedFindingType]["urlState"]["parse"]+>;diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.unit.spec.tsnew file mode 100644index 00000000000..f0e5c3479a4--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/config.unit.spec.ts@@ -0,0 +1,175 @@+import {+ type DuplicatedContentParams,+ duplicatedContentConfig,+ getImbalancedContentConfig,+ slowContentConfig,+ staleContentConfig,+} from "./config";++const configs = [+ [+ "stale",+ staleContentConfig,+ "threshold-days",+ "thresholdDays",+ "last-active-at",+ ],+ [+ "slow",+ slowContentConfig,+ "min-duration-ms",+ "minDurationMs",+ "duration-ms",+ ],+ [+ "duplicated",+ duplicatedContentConfig,+ "min-duplicate-count",+ "minDuplicateCount",+ "duplicate-count",+ ],+] as const;++describe.each(configs)(+ "%s content config",+ (_name, config, urlKey, paramKey, sortColumn) => {+ it("parses valid filters, sorting, and threshold", () => {+ const params = config.urlState.parse({+ page: "2",+ query: "sales",+ "entity-types": ["model", "invalid"],+ "include-personal-collections": "false",+ [urlKey]: "90",+ "sort-column": sortColumn,+ "sort-direction": "desc",+ });++ expect(params).toMatchObject({+ page: 2,+ query: "sales",+ entityTypes: ["model"],+ includePersonalCollections: false,+ [paramKey]: 90,+ sortColumn,+ sortDirection: "desc",+ });+ });++ it("rejects invalid page, threshold, sorting, and entity types", () => {+ const params = config.urlState.parse({+ page: "-2",+ "entity-types": ["collection", "bogus"],+ [urlKey]: "invalid",+ "sort-column": "unsupported",+ "sort-direction": "up",+ });++ expect(params.page).toBe(0);+ expect(+ Object.entries(params).find(([key]) => key === paramKey)?.[1],+ ).toBeUndefined();+ expect(params.sortColumn).toBeUndefined();+ expect(params.sortDirection).toBeUndefined();+ expect(params.entityTypes).toEqual(+ config === duplicatedContentConfig ? ["collection"] : [],+ );+ });++ it("recognizes only its own URL keys for last-used restoration", () => {+ expect(config.hasUrlParams({ unrelated: "1" })).toBe(false);+ expect(config.hasUrlParams({ page: "0" })).toBe(true);+ expect(config.hasUrlParams({ [urlKey]: "90" })).toBe(true);+ });+ },+);++describe("content diagnostics config", () => {+ it("omits default filters, page and absent sort from serialization", () => {+ expect(+ slowContentConfig.urlState.serialize({+ page: 0,+ entityTypes: [...slowContentConfig.entityTypes],+ includePersonalCollections: true,+ }),+ ).toMatchObject({+ page: undefined,+ "entity-types": undefined,+ "include-personal-collections": undefined,+ "sort-column": undefined,+ "sort-direction": undefined,+ });+ });++ it("does not erase a repeated single-type filter as if it selected all types", () => {+ const modelOnly = Array(staleContentConfig.entityTypes.length).fill(+ "model",+ );+ const params = staleContentConfig.urlState.parse({+ "entity-types": modelOnly,+ });++ expect(+ staleContentConfig.urlState.serialize(params)["entity-types"],+ ).toEqual(modelOnly);+ });++ it("uses the existing API sort default when the URL has no sort", () => {+ // The API defaults to detected-at ascending, which is not a visible column.+ expect(staleContentConfig.urlState.parse({}).sortColumn).toBeUndefined();+ expect(staleContentConfig.urlState.parse({}).sortDirection).toBeUndefined();+ });++ it("maps persisted user params without page or search query", () => {+ const params: DuplicatedContentParams = {+ page: 3,+ query: "ignored",+ entityTypes: ["dashboard"],+ includePersonalCollections: false,+ minDuplicateCount: 5,+ sortColumn: "duplicate-count" as const,+ sortDirection: "asc" as const,+ };++ const userParams = duplicatedContentConfig.getUserParams(params);+ expect(userParams).toEqual({+ entity_types: ["dashboard"],+ include_personal_collections: false,+ min_duplicate_count: 5,+ sort_column: "duplicate-count",+ sort_direction: "asc",+ });+ expect(duplicatedContentConfig.parseUserParams(userParams)).toEqual({+ entityTypes: ["dashboard"],+ includePersonalCollections: false,+ minDuplicateCount: 5,+ sortColumn: "duplicate-count",+ sortDirection: "asc",+ });+ });++ it.each([+ [+ "empty",+ ["question", "model", "metric", "dashboard", "document", "collection"],+ ],+ ["sparse", ["dashboard", "collection"]],+ ["crowded", ["dashboard", "document", "collection"]],+ ] as const)(+ "limits %s filters to entity types the endpoint can return",+ (mode, expected) => {+ const config = getImbalancedContentConfig(mode);++ expect(config.entityTypes).toEqual(expected);+ expect(+ config.urlState.parse({+ "entity-types": ["question", "dashboard", "document", "collection"],+ }).entityTypes,+ ).toEqual(+ expected.filter((type) =>+ ["question", "dashboard", "document", "collection"].includes(type),+ ),+ );+ expect(config.getFilterOptions({}).entityTypes).toEqual(expected);+ },+ );+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/hooks/use-optional-sorting-state.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/hooks/use-optional-sorting-state.tsnew file mode 100644index 00000000000..ff03e65348c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/hooks/use-optional-sorting-state.ts@@ -0,0 +1,41 @@+import type { SortingState, Updater } from "@tanstack/react-table";+import { useCallback, useMemo } from "react";++import {+ type Sorting,+ getNextOptionalSorting,+ getSortingState,+} from "metabase/utils/sorting";++type UseOptionalSortingStateProps<TColumn extends string> = {+ sortOptions: Sorting<TColumn> | undefined;+ columns: readonly TColumn[];+ onSortOptionsChange: (sortOptions: Sorting<TColumn> | undefined) => void;+};++/**+ * Adapter between TanStack Table sorting state and content diagnostics sorting.+ * Unlike `useSortingStateChange`, cycling to the unsorted state is allowed: the+ * API's default order (`detected-at`) has no visible column to show as sorted.+ */+export function useOptionalSortingState<TColumn extends string>({+ sortOptions,+ columns,+ onSortOptionsChange,+}: UseOptionalSortingStateProps<TColumn>) {+ const sortingState = useMemo(+ () => getSortingState(sortOptions),+ [sortOptions],+ );++ const onSortingChange = useCallback(+ (updater: Updater<SortingState>) => {+ const newSortingState =+ typeof updater === "function" ? updater(sortingState) : updater;+ onSortOptionsChange(getNextOptionalSorting(newSortingState, columns));+ },+ [sortingState, columns, onSortOptionsChange],+ );++ return { sortingState, onSortingChange };+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.tsnew file mode 100644index 00000000000..c116eca6e7a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.ts@@ -0,0 +1,11 @@+import { PLUGIN_MONITOR } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { getContentDiagnosticsRoutes } from "./routes";++export function initializePlugin() {+ if (hasPremiumFeature("content_diagnostics")) {+ PLUGIN_MONITOR.isContentDiagnosticsEnabled = true;+ PLUGIN_MONITOR.getContentDiagnosticsRoutes = getContentDiagnosticsRoutes;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.unit.spec.tsnew file mode 100644index 00000000000..4ddad0622f0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/index.unit.spec.ts@@ -0,0 +1,61 @@+import { PLUGIN_MONITOR, reinitialize } from "metabase/plugins";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { getContentDiagnosticsRoutes } from "./routes";++import { initializePlugin } from "./index";++jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn(),+}));++// jest.mock replaces the module factory; retype the import as its mock+const mockHasPremiumFeature = hasPremiumFeature as jest.MockedFunction<+ typeof hasPremiumFeature+>;++const mockContentDiagnosticsFeature = (enabled: boolean) => {+ mockHasPremiumFeature.mockImplementation((feature) =>+ feature === "content_diagnostics" ? enabled : false,+ );+};++describe("metabase-enterprise/monitor/content-diagnostics initializePlugin", () => {+ afterEach(() => {+ jest.clearAllMocks();+ reinitialize();+ });++ it("enables content diagnostics when the content_diagnostics feature is present", () => {+ mockContentDiagnosticsFeature(true);++ initializePlugin();++ expect(PLUGIN_MONITOR.isContentDiagnosticsEnabled).toBe(true);+ expect(PLUGIN_MONITOR.getContentDiagnosticsRoutes).toBe(+ getContentDiagnosticsRoutes,+ );+ expect(PLUGIN_MONITOR.getContentDiagnosticsRoutes()).not.toBeNull();+ });++ it("leaves the OSS defaults when the content_diagnostics feature is absent", () => {+ mockContentDiagnosticsFeature(false);++ initializePlugin();++ expect(PLUGIN_MONITOR.isContentDiagnosticsEnabled).toBe(false);+ expect(PLUGIN_MONITOR.getContentDiagnosticsRoutes()).toBeNull();+ });++ it("resets content diagnostics state on reinitialize()", () => {+ mockContentDiagnosticsFeature(true);+ initializePlugin();++ expect(PLUGIN_MONITOR.isContentDiagnosticsEnabled).toBe(true);++ reinitialize();++ expect(PLUGIN_MONITOR.isContentDiagnosticsEnabled).toBe(false);+ expect(PLUGIN_MONITOR.getContentDiagnosticsRoutes()).toBeNull();+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ContentDiagnosticsPage.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ContentDiagnosticsPage.tsxnew file mode 100644index 00000000000..ff3729c228d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ContentDiagnosticsPage.tsx@@ -0,0 +1,121 @@+import type { ComponentType } from "react";+import { useCallback, useEffect, useRef } from "react";++import {+ type UrlStateConfig,+ type UrlStateQuery,+ useUrlState,+} from "metabase/common/hooks/use-url-state";+import { useUserKeyValue } from "metabase/current-user";+import { useLocation } from "metabase/router";+import { parseSearchQuery } from "metabase/utils/browser";+import type {+ ContentDiagnosticsDuplicatedUserParams,+ ContentDiagnosticsImbalancedUserParams,+ ContentDiagnosticsSlowUserParams,+ ContentDiagnosticsStaleUserParams,+} from "metabase-types/api";++import type {+ ContentDiagnosticsParamsOptions,+ ContentDiagnosticsTab,+} from "../components/types";++type DiagnosticsUserParams =+ | ContentDiagnosticsDuplicatedUserParams+ | ContentDiagnosticsImbalancedUserParams+ | ContentDiagnosticsSlowUserParams+ | ContentDiagnosticsStaleUserParams;++type ContentDiagnosticsPageConfig<+ TParams extends Record<string, unknown>,+ TUserParams extends DiagnosticsUserParams,+> = {+ key: ContentDiagnosticsTab;+ urlState: UrlStateConfig<TParams>;+ getParamsWithoutDefaults: (params: TParams) => TParams;+ getUserParams: (params: TParams) => TUserParams;+ parseUserParams: (value: unknown) => TParams;+ hasUrlParams: (query: UrlStateQuery) => boolean;+};++type ContentDiagnosticsContentProps<TParams extends Record<string, unknown>> = {+ params: TParams;+ isLoadingParams: boolean;+ onParamsChange: (+ params: TParams,+ options?: ContentDiagnosticsParamsOptions,+ ) => void;+};++type ContentDiagnosticsPageProps<+ TParams extends Record<string, unknown>,+ TUserParams extends DiagnosticsUserParams,+> = {+ config: ContentDiagnosticsPageConfig<TParams, TUserParams>;+ component: ComponentType<ContentDiagnosticsContentProps<TParams>>;+};++export function ContentDiagnosticsPage<+ TParams extends Record<string, unknown>,+ TUserParams extends DiagnosticsUserParams,+>({+ config,+ component: Content,+}: ContentDiagnosticsPageProps<TParams, TUserParams>) {+ const location = useLocation();+ const [urlParams, { patchUrlState }] = useUrlState(location, config.urlState);+ const shouldRestoreLastUsedParamsRef = useRef(+ !config.hasUrlParams(parseSearchQuery(location.search)),+ );++ const {+ value: rawLastUsedParams,+ isLoading: isLoadingParams,+ setValue: setLastUsedParams,+ } = useUserKeyValue({+ namespace: "content_diagnostics",+ key: config.key,+ });++ const params = shouldRestoreLastUsedParamsRef.current+ ? config.parseUserParams(rawLastUsedParams)+ : urlParams;++ useEffect(() => {+ if (shouldRestoreLastUsedParamsRef.current && !isLoadingParams) {+ shouldRestoreLastUsedParamsRef.current = false;+ patchUrlState(+ config.getParamsWithoutDefaults(+ config.parseUserParams(rawLastUsedParams),+ ),+ { immediate: true },+ );+ }+ }, [config, isLoadingParams, patchUrlState, rawLastUsedParams]);++ const handleParamsChange = useCallback(+ (+ params: TParams,+ { withSetLastUsedParams = false }: ContentDiagnosticsParamsOptions = {},+ ) => {+ const normalizedParams = config.getParamsWithoutDefaults(params);+ if (withSetLastUsedParams) {+ setLastUsedParams(config.getUserParams(normalizedParams));+ }+ // Search input changes are already debounced at the control boundary; all+ // other diagnostics controls are discrete actions. Sync immediately so we+ // do not add a second debounce or leave navigation state temporarily stale.+ patchUrlState(normalizedParams, { immediate: true });+ },+ [config, patchUrlState, setLastUsedParams],+ );++ return (+ <Content+ params={params}+ isLoadingParams={isLoadingParams}+ onParamsChange={handleParamsChange}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.tsxnew file mode 100644index 00000000000..f12b0533274--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.tsx@@ -0,0 +1,13 @@+import { DuplicatedContent } from "../components";+import { duplicatedContentConfig } from "../config";++import { ContentDiagnosticsPage } from "./ContentDiagnosticsPage";++export function DuplicatedContentPage() {+ return (+ <ContentDiagnosticsPage+ config={duplicatedContentConfig}+ component={DuplicatedContent}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.unit.spec.tsxnew file mode 100644index 00000000000..801b341752f--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/DuplicatedContentPage.unit.spec.tsx@@ -0,0 +1,655 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupListDuplicatedFindingsEndpoint,+ setupUserKeyValueEndpoints,+} from "__support__/server-mocks";+import {+ type TestRouter,+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitFor,+ within,+} from "__support__/ui";+import type { UrlStateQuery } from "metabase/common/hooks/use-url-state";+import { MonitorContent } from "metabase/monitor/components/MonitorLayout/MonitorContent";+import { Route, queryToSearch } from "metabase/router";+import * as Urls from "metabase/urls";+import { parseSearchQuery } from "metabase/utils/browser";+import type {+ ContentDiagnosticsDuplicatedFinding,+ ContentDiagnosticsDuplicatedUserParams,+ ListDuplicatedFindingsResponse,+} from "metabase-types/api";+import {+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsDuplicateEntity,+ createMockContentDiagnosticsDuplicatedFinding,+ createMockContentDiagnosticsUser,+ createMockListDuplicatedFindingsResponse,+ createMockUser,+} from "metabase-types/api/mocks";++import { DuplicatedContentPage } from "./DuplicatedContentPage";++const FINDINGS: ContentDiagnosticsDuplicatedFinding[] = [+ createMockContentDiagnosticsDuplicatedFinding({+ id: 1,+ entity_type: "card",+ entity_display_name: "Sales overview",+ duplicate_count: 2,+ }),+ createMockContentDiagnosticsDuplicatedFinding({+ id: 2,+ entity_type: "dashboard",+ entity_display_name: "Marketing funnel",+ duplicate_count: 5,+ }),+];++type SetupOpts = {+ findings?: ContentDiagnosticsDuplicatedFinding[];+ total?: number;+ urlParams?: UrlStateQuery;+ lastUsedParams?: ContentDiagnosticsDuplicatedUserParams;+ error?: boolean;+ getResponse?: (url: string) => ListDuplicatedFindingsResponse;+};++function setup({+ findings = [],+ total,+ urlParams = {},+ lastUsedParams = {},+ error = false,+ getResponse,+}: SetupOpts = {}) {+ if (error) {+ fetchMock.get("path:/api/ee/content-diagnostics/duplicated", {+ status: 500,+ body: { message: "Duplicated scan failed" },+ });+ } else if (getResponse) {+ fetchMock.get("path:/api/ee/content-diagnostics/duplicated", ({ url }) =>+ getResponse(url),+ );+ } else {+ setupListDuplicatedFindingsEndpoint(+ createMockListDuplicatedFindingsResponse({+ data: findings,+ total: total ?? findings.length,+ }),+ );+ }++ setupUserKeyValueEndpoints({+ namespace: "content_diagnostics",+ key: "duplicated",+ value: lastUsedParams,+ });++ mockGetBoundingClientRect({ width: 100, height: 100 });++ const { router } = renderWithProviders(+ <Route+ path={Urls.duplicatedContent()}+ element={+ <MonitorContent>+ <DuplicatedContentPage />+ </MonitorContent>+ }+ />,+ {+ withRouter: true,+ initialRoute: `${Urls.duplicatedContent()}${queryToSearch(urlParams)}`,+ storeInitialState: {+ currentUser: createMockUser(),+ },+ },+ );++ return { router };+}++function getUrlQuery(router: TestRouter | undefined) {+ return parseSearchQuery(router?.location.search ?? "");+}++function getLastRequestUrl() {+ return new URL(+ String(+ fetchMock.callHistory.lastCall(+ "path:/api/ee/content-diagnostics/duplicated",+ )?.url,+ ),+ "http://localhost",+ );+}++async function waitForListToLoad() {+ expect(await screen.findByRole("treegrid")).toBeInTheDocument();+}++describe("DuplicatedContentPage", () => {+ it("renders duplicated findings with their duplicate counts in the table", async () => {+ setup({ findings: FINDINGS });++ const list = await screen.findByRole("treegrid");+ expect(await within(list).findByText("Sales overview")).toBeInTheDocument();+ expect(within(list).getByText("Marketing funnel")).toBeInTheDocument();+ expect(within(list).getByText("2")).toBeInTheDocument();+ expect(within(list).getByText("5")).toBeInTheDocument();+ });++ it("shows an empty state when there are no findings", async () => {+ setup({ findings: [] });++ expect(+ await screen.findByText("No duplicated content found"),+ ).toBeInTheDocument();+ });++ it("renders sidebar details for card and collection findings", async () => {+ const cardFinding = createMockContentDiagnosticsDuplicatedFinding({+ id: 1,+ entity_id: 42,+ entity_display_name: "Revenue by category",+ duplicate_count: 2,+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 20,+ name: "Executive dashboards",+ effective_ancestors: [{ id: "root", name: "Our analytics" }],+ }),+ description: "Shows revenue grouped by product category.",+ creator: createMockContentDiagnosticsUser({ name: "Grace Creator" }),+ view_count: 7,+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 43,+ name: "Revenue by Category",+ entity_type: "card",+ card_type: "model",+ view_count: 3,+ }),+ createMockContentDiagnosticsDuplicateEntity({+ id: 44,+ name: "revenue by category",+ entity_type: "card",+ view_count: 1,+ }),+ ],+ },+ });+ const collectionFinding = createMockContentDiagnosticsDuplicatedFinding({+ id: 2,+ entity_type: "collection",+ entity_id: 54,+ entity_display_name: "Reporting",+ duplicate_count: 1,+ details: {+ view_count: undefined,+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 55,+ name: "reporting",+ entity_type: "collection",+ view_count: undefined,+ }),+ ],+ },+ });+ setup({ findings: [cardFinding, collectionFinding] });++ const list = await screen.findByRole("treegrid");+ await userEvent.click(await within(list).findByText("Revenue by category"));++ const sidebarRegion = await screen.findByTestId("monitor-sidebar-region");+ expect(sidebarRegion).toHaveTextContent("Revenue by category");+ expect(sidebarRegion).toHaveTextContent("Executive dashboards");+ expect(sidebarRegion).toHaveTextContent(+ "Shows revenue grouped by product category.",+ );+ expect(sidebarRegion).toHaveTextContent("Grace Creator");++ const cardDuplicates = within(sidebarRegion).getByRole("region", {+ name: "Duplicates",+ });+ expect(+ within(cardDuplicates).getByText("Duplicates (2)"),+ ).toBeInTheDocument();+ expect(+ within(cardDuplicates).getByRole("link", {+ name: "Revenue by Category, Model",+ }),+ ).toHaveAttribute("href", expect.stringContaining("/model/43"));+ expect(+ within(cardDuplicates).getByRole("link", {+ name: "revenue by category, Question",+ }),+ ).toHaveAttribute("href", expect.stringContaining("/question/44"));+ expect(within(cardDuplicates).getByText("3 views")).toBeInTheDocument();+ expect(within(cardDuplicates).getByText("1 view")).toBeInTheDocument();++ const collectionRow = within(list).getByRole("row", { name: /Reporting/ });+ expect(+ within(collectionRow).getByLabelText("Collection"),+ ).toBeInTheDocument();+ await userEvent.click(within(collectionRow).getByText("Reporting"));++ const collectionDuplicates = within(sidebarRegion).getByRole("region", {+ name: "Duplicates",+ });+ expect(+ await within(collectionDuplicates).findByRole("link", {+ name: "reporting, Collection",+ }),+ ).toHaveAttribute("href", expect.stringContaining("/collection/55"));+ expect(+ within(collectionDuplicates).queryByText(/view/),+ ).not.toBeInTheDocument();+ });++ it("reports duplicates that aren't visible to the user", async () => {+ const noneVisible = createMockContentDiagnosticsDuplicatedFinding({+ id: 1,+ entity_display_name: "Hidden peers",+ duplicate_count: 3,+ details: { duplicate_entities: [] },+ });+ const someVisible = createMockContentDiagnosticsDuplicatedFinding({+ id: 2,+ entity_display_name: "Partially hidden peers",+ duplicate_count: 3,+ details: {+ duplicate_entities: [+ createMockContentDiagnosticsDuplicateEntity({+ id: 43,+ name: "Visible peer",+ }),+ ],+ },+ });+ setup({ findings: [noneVisible, someVisible] });++ const list = await screen.findByRole("treegrid");+ await userEvent.click(await within(list).findByText("Hidden peers"));++ const sidebarRegion = await screen.findByTestId("monitor-sidebar-region");+ expect(sidebarRegion).toHaveTextContent("Duplicates (3)");+ expect(sidebarRegion).toHaveTextContent(+ "None of these duplicates are visible to you.",+ );++ await userEvent.click(within(list).getByText("Partially hidden peers"));++ const duplicates = within(sidebarRegion).getByRole("region", {+ name: "Duplicates",+ });+ expect(+ await within(duplicates).findByRole("link", {+ name: "Visible peer, Question",+ }),+ ).toBeInTheDocument();+ expect(within(duplicates).getByText("Duplicates (3)")).toBeInTheDocument();+ expect(+ within(duplicates).getByText("2 duplicates aren't visible to you."),+ ).toBeInTheDocument();+ });++ it("sends table sort changes to the server and URL, resetting pagination", async () => {+ const { router } = setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { page: "1" },+ });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");++ await userEvent.click(+ screen.getByRole("columnheader", { name: /^Duplicates/ }),+ );++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ "duplicate-count",+ );+ });+ expect(getLastRequestUrl().searchParams.get("sort-direction")).toBe("asc");+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("0");+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "duplicate-count",+ "sort-direction": "asc",+ });+ });++ it("refetches the duplicated endpoint with the next offset and renders the next page", async () => {+ const secondPageFinding = createMockContentDiagnosticsDuplicatedFinding({+ id: 3,+ entity_display_name: "Second page question",+ });+ const { router } = setup({+ total: 50,+ getResponse: (url) =>+ createMockListDuplicatedFindingsResponse({+ data: url.includes("offset=25") ? [secondPageFinding] : FINDINGS,+ total: 50,+ }),+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ expect(await screen.findByText("Second page question")).toBeInTheDocument();+ expect(screen.queryByText("Sales overview")).not.toBeInTheDocument();+ expect(getUrlQuery(router)).toEqual({ page: "1" });+ expect(getLastRequestUrl().searchParams.get("limit")).toBe("25");+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");+ });++ it("filters by personal collections server-side", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("true");++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", {+ name: "Include items in personal collections",+ }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "include-personal-collections": "false",+ });+ });+ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("false");+ });++ it("reflects the minimum duplicate count from the URL and sends changes made in the Filter popover", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "min-duplicate-count": "3" },+ });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("min-duplicate-count")).toBe(+ "3",+ );++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ const input = within(popover).getByDisplayValue("3 or more");++ await userEvent.click(input);+ await userEvent.click(+ await within(popover).findByRole("option", { name: "5 or more" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "min-duplicate-count": "5",+ });+ });+ expect(getLastRequestUrl().searchParams.get("min-duplicate-count")).toBe(+ "5",+ );++ await userEvent.click(within(popover).getByLabelText("Clear"));++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(+ within(popover).getByPlaceholderText("Any number of duplicates"),+ ).toHaveValue("");+ });++ it("sends the query parameter to the server when searching", async () => {+ setup({+ getResponse: (url) =>+ createMockListDuplicatedFindingsResponse({+ data: url.includes("query=sales") ? [FINDINGS[0]] : FINDINGS,+ total: url.includes("query=sales") ? 1 : FINDINGS.length,+ }),+ });+ await waitForListToLoad();++ const input = screen.getByLabelText("Search");+ await userEvent.type(input, "sales");++ await waitFor(() => {+ expect(screen.queryByText("Marketing funnel")).not.toBeInTheDocument();+ });+ expect(screen.getByText("Sales overview")).toBeInTheDocument();+ expect(getLastRequestUrl().searchParams.get("query")).toBe("sales");+ });++ it("offers collections as an entity type and sends the selection to the server", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["question"] },+ });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "question",+ ]);++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ const collectionsCheckbox = within(popover).getByRole("checkbox", {+ name: "Collections",+ });+ expect(collectionsCheckbox).not.toBeChecked();++ await userEvent.click(collectionsCheckbox);++ const expectedTypes = ["question", "collection"];++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "entity-types": expectedTypes,+ });+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual(+ expectedTypes,+ );+ expect(collectionsCheckbox).toBeChecked();+ });++ it("shows the error state and doesn't render the table when request fails", async () => {+ setup({ error: true });++ expect(+ await screen.findByText("Duplicated scan failed"),+ ).toBeInTheDocument();+ expect(screen.queryByRole("treegrid")).not.toBeInTheDocument();+ });++ it("restores the last-used filter when the URL has no params", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: {},+ lastUsedParams: { min_duplicate_count: 5 },+ });++ await waitForListToLoad();++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "min-duplicate-count": "5",+ });+ });+ expect(getLastRequestUrl().searchParams.get("min-duplicate-count")).toBe(+ "5",+ );+ });++ it("lets an explicit default-valued URL win over the last-used filter", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { page: "0", "include-personal-collections": "true" },+ lastUsedParams: { min_duplicate_count: 5 },+ });++ await waitForListToLoad();++ expect(+ getLastRequestUrl().searchParams.get("min-duplicate-count"),+ ).toBeNull();+ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("true");+ expect(getUrlQuery(router)).toEqual({});+ });+ it("marks the filter button once non-default filters are applied", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ screen.queryByTestId("content-diagnostics-filter-indicator"),+ ).not.toBeInTheDocument();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(+ screen.getByTestId("content-diagnostics-filter-indicator"),+ ).toBeInTheDocument();+ });+ });++ it("keeps the active filters when moving to the next page", async () => {+ setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "model",+ ]);+ });++ it("resets to all entity types when the last selected type is deselected", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ const allEntityTypes = [+ "Questions",+ "Models",+ "Metrics",+ "Dashboards",+ "Documents",+ "Transforms",+ "Collections",+ ];+ allEntityTypes.forEach((label) => {+ expect(+ within(popover).getByRole("checkbox", { name: label }),+ ).toBeChecked();+ });+ });+ describe("sorting", () => {+ it.each([+ ["Name", "name"],+ ["Type", "entity-type"],+ ["Location", "collection-name"],+ ["Created by", "created-by"],+ ["Created at", "created-at"],+ ["Duplicates", "duplicate-count"],+ ])("sorts by %s", async (header, sortColumn) => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByRole("columnheader", { name: new RegExp(`^${header}`) }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": sortColumn,+ "sort-direction": "asc",+ });+ });+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ sortColumn,+ );+ });++ it("cycles a column through ascending, descending and unsorted", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ const header = () =>+ screen.getByRole("columnheader", { name: /^Created at/ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "ascending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "asc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "descending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "desc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(header()).not.toHaveAttribute("aria-sort");+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.tsxnew file mode 100644index 00000000000..1f144d0a8fb--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.tsx@@ -0,0 +1,43 @@+import type { ComponentProps, ComponentType } from "react";++import type { ContentDiagnosticsImbalancedFindingType } from "metabase-types/api";++import { ImbalancedContent } from "../components";+import { getImbalancedContentConfig } from "../config";++import { ContentDiagnosticsPage } from "./ContentDiagnosticsPage";++type ImbalancedContentPageProps = {+ mode: ContentDiagnosticsImbalancedFindingType;+};++type ContentProps = Omit<ComponentProps<typeof ImbalancedContent>, "mode">;+const contentByMode: Record<+ ContentDiagnosticsImbalancedFindingType,+ ComponentType<ContentProps>+> = {+ empty: (props) => <ImbalancedContent {...props} mode="empty" />,+ sparse: (props) => <ImbalancedContent {...props} mode="sparse" />,+ crowded: (props) => <ImbalancedContent {...props} mode="crowded" />,+};++function ImbalancedContentPage({ mode }: ImbalancedContentPageProps) {+ return (+ <ContentDiagnosticsPage+ config={getImbalancedContentConfig(mode)}+ component={contentByMode[mode]}+ />+ );+}++export function EmptyContentPage() {+ return <ImbalancedContentPage mode="empty" />;+}++export function SparseContentPage() {+ return <ImbalancedContentPage mode="sparse" />;+}++export function CrowdedContentPage() {+ return <ImbalancedContentPage mode="crowded" />;+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.unit.spec.tsxnew file mode 100644index 00000000000..9e77917879b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/ImbalancedContentPage.unit.spec.tsx@@ -0,0 +1,515 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";+import type { ComponentType } from "react";++import {+ setupInvalidateFindingsEndpoint,+ setupListImbalancedFindingsEndpoint,+ setupUserKeyValueEndpoints,+} from "__support__/server-mocks";+import {+ type TestRouter,+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitFor,+ within,+} from "__support__/ui";+import type { UrlStateQuery } from "metabase/common/hooks/use-url-state";+import { MonitorContent } from "metabase/monitor/components/MonitorLayout/MonitorContent";+import { Route, queryToSearch } from "metabase/router";+import * as Urls from "metabase/urls";+import { parseSearchQuery } from "metabase/utils/browser";+import type {+ ContentDiagnosticsImbalancedFinding,+ ContentDiagnosticsImbalancedFindingType,+ ContentDiagnosticsImbalancedUserParams,+ ListImbalancedFindingsResponse,+} from "metabase-types/api";+import {+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsImbalancedFinding,+ createMockContentDiagnosticsUser,+ createMockListImbalancedFindingsResponse,+ createMockUser,+} from "metabase-types/api/mocks";++import {+ CrowdedContentPage,+ EmptyContentPage,+ SparseContentPage,+} from "./ImbalancedContentPage";++const PAGE_BY_MODE: Record<+ ContentDiagnosticsImbalancedFindingType,+ ComponentType+> = {+ empty: EmptyContentPage,+ sparse: SparseContentPage,+ crowded: CrowdedContentPage,+};++const FINDINGS: ContentDiagnosticsImbalancedFinding[] = [+ createMockContentDiagnosticsImbalancedFinding({+ id: 1,+ finding_type: "crowded",+ entity_type: "collection",+ entity_display_name: "Crowded collection",+ content_count: 150,+ }),+ createMockContentDiagnosticsImbalancedFinding({+ id: 2,+ finding_type: "crowded",+ entity_type: "dashboard",+ entity_display_name: "Busy dashboard",+ content_count: 42,+ }),+];++type SetupOpts = {+ mode?: ContentDiagnosticsImbalancedFindingType;+ findings?: ContentDiagnosticsImbalancedFinding[];+ total?: number;+ urlParams?: UrlStateQuery;+ lastUsedParams?: ContentDiagnosticsImbalancedUserParams;+ error?: boolean;+ getResponse?: (url: string) => ListImbalancedFindingsResponse;+};++function setup({+ mode = "crowded",+ findings = [],+ total,+ urlParams = {},+ lastUsedParams = {},+ error = false,+ getResponse,+}: SetupOpts = {}) {+ if (error) {+ fetchMock.get("path:/api/ee/content-diagnostics/imbalanced", {+ status: 500,+ body: { message: "Imbalanced scan failed" },+ });+ } else if (getResponse) {+ fetchMock.get("path:/api/ee/content-diagnostics/imbalanced", ({ url }) =>+ getResponse(url),+ );+ } else {+ setupListImbalancedFindingsEndpoint(+ createMockListImbalancedFindingsResponse({+ data: findings,+ total: total ?? findings.length,+ }),+ );+ }++ setupUserKeyValueEndpoints({+ namespace: "content_diagnostics",+ key: mode,+ value: lastUsedParams,+ });++ mockGetBoundingClientRect({ width: 100, height: 100 });++ const Page = PAGE_BY_MODE[mode];+ const { router } = renderWithProviders(+ <Route+ path={Urls.imbalancedContent(mode)}+ element={+ <MonitorContent>+ <Page />+ </MonitorContent>+ }+ />,+ {+ withRouter: true,+ initialRoute: `${Urls.imbalancedContent(mode)}${queryToSearch(urlParams)}`,+ storeInitialState: {+ currentUser: createMockUser(),+ },+ },+ );++ return { router };+}++function getUrlQuery(router: TestRouter | undefined) {+ return parseSearchQuery(router?.location.search ?? "");+}++function getLastRequestUrl() {+ return new URL(+ String(+ fetchMock.callHistory.lastCall(+ "path:/api/ee/content-diagnostics/imbalanced",+ )?.url,+ ),+ "http://localhost",+ );+}++async function waitForListToLoad() {+ expect(await screen.findByRole("treegrid")).toBeInTheDocument();+}++describe("ImbalancedContentPage", () => {+ it("renders findings with their content counts in the table", async () => {+ setup({ findings: FINDINGS });++ const list = await screen.findByRole("treegrid");+ expect(+ await within(list).findByText("Crowded collection"),+ ).toBeInTheDocument();+ expect(within(list).getByText("Busy dashboard")).toBeInTheDocument();+ expect(within(list).getByText("150")).toBeInTheDocument();+ expect(within(list).getByText("42")).toBeInTheDocument();+ });++ it.each(["empty", "sparse"] as const)(+ "offers bulk-trash selection on the %s tab",+ async (mode) => {+ setup({+ mode,+ findings: [+ createMockContentDiagnosticsImbalancedFinding({ can_write: true }),+ ],+ });++ await screen.findByRole("treegrid");+ expect(await screen.findByLabelText("Select all")).toBeInTheDocument();+ },+ );++ it("offers dismissal without trash on the Crowded tab", async () => {+ setupInvalidateFindingsEndpoint({+ invalidated: [11],+ skipped: [],+ });+ setup({+ mode: "crowded",+ findings: [+ createMockContentDiagnosticsImbalancedFinding({+ id: 11,+ entity_id: 101,+ can_write: false,+ }),+ ],+ });++ await screen.findByRole("treegrid");+ await userEvent.click(screen.getByLabelText("Select all"));+ expect(+ screen.getByRole("button", { name: "Dismiss finding" }),+ ).toBeEnabled();+ expect(+ screen.queryByRole("button", { name: "Move to trash" }),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByRole("button", { name: "Delete" }),+ ).not.toBeInTheDocument();+ await userEvent.click(+ screen.getByRole("button", { name: "Dismiss finding" }),+ );+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Dismiss finding",+ }),+ );+ await waitFor(() =>+ expect(+ screen.queryByTestId("content-diagnostics-bulk-actions"),+ ).not.toBeInTheDocument(),+ );+ const calls = fetchMock.callHistory.calls(+ "path:/api/ee/content-diagnostics/invalidate",+ );+ expect(calls).toHaveLength(1);+ const [call] = calls;+ const body: unknown = JSON.parse(String(call.options.body));+ expect(body).toEqual({ ids: [11] });+ });++ it("pins the finding type to the tab's problem type", async () => {+ setup({ mode: "crowded", findings: FINDINGS });+ await waitForListToLoad();+ expect(getLastRequestUrl().searchParams.getAll("finding-types")).toEqual([+ "crowded",+ ]);+ });++ it("pins a different finding type for another tab", async () => {+ setup({ mode: "empty", findings: [] });+ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.getAll("finding-types")).toEqual([+ "empty",+ ]);+ });+ });++ it("shows a problem-specific empty state", async () => {+ setup({ mode: "sparse", findings: [] });++ expect(+ await screen.findByText("No sparse content found"),+ ).toBeInTheDocument();+ });++ it("renders the content count in the sidebar", async () => {+ const finding = createMockContentDiagnosticsImbalancedFinding({+ id: 1,+ finding_type: "crowded",+ entity_type: "collection",+ entity_id: 20,+ entity_display_name: "Executive dashboards",+ content_count: 150,+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 30,+ name: "Reporting",+ }),+ creator: createMockContentDiagnosticsUser({ name: "Grace Creator" }),+ threshold: 100,+ unit: "items",+ },+ });+ setup({ findings: [finding] });++ const list = await screen.findByRole("treegrid");+ await userEvent.click(+ await within(list).findByText("Executive dashboards"),+ );++ const sidebarRegion = await screen.findByTestId("monitor-sidebar-region");+ expect(sidebarRegion).toHaveTextContent("Grace Creator");+ expect(sidebarRegion).toHaveTextContent("150 items");+ });++ it("offers collections as an entity type and sends the selection to the server", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["dashboard"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ const collectionsCheckbox = within(popover).getByRole("checkbox", {+ name: "Collections",+ });+ expect(collectionsCheckbox).not.toBeChecked();++ await userEvent.click(collectionsCheckbox);++ const expectedTypes = ["dashboard", "collection"];+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({ "entity-types": expectedTypes });+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual(+ expectedTypes,+ );+ });++ it("sends the query parameter to the server when searching", async () => {+ setup({+ getResponse: (url) =>+ createMockListImbalancedFindingsResponse({+ data: url.includes("query=busy") ? [FINDINGS[1]] : FINDINGS,+ total: url.includes("query=busy") ? 1 : FINDINGS.length,+ }),+ });+ await waitForListToLoad();++ const input = screen.getByLabelText("Search");+ await userEvent.type(input, "busy");++ await waitFor(() => {+ expect(screen.queryByText("Crowded collection")).not.toBeInTheDocument();+ });+ expect(screen.getByText("Busy dashboard")).toBeInTheDocument();+ expect(getLastRequestUrl().searchParams.get("query")).toBe("busy");+ });++ it("shows the error state and doesn't render the table when the request fails", async () => {+ setup({ error: true });++ expect(+ await screen.findByText("Imbalanced scan failed"),+ ).toBeInTheDocument();+ expect(screen.queryByRole("treegrid")).not.toBeInTheDocument();+ });++ it("restores the last-used filter when the URL has no params", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: {},+ lastUsedParams: { sort_column: "created-at", sort_direction: "desc" },+ });++ await waitForListToLoad();++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "desc",+ });+ });+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ "created-at",+ );+ });+ it("marks the filter button once non-default filters are applied", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ screen.queryByTestId("content-diagnostics-filter-indicator"),+ ).not.toBeInTheDocument();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Dashboards" }),+ );++ await waitFor(() => {+ expect(+ screen.getByTestId("content-diagnostics-filter-indicator"),+ ).toBeInTheDocument();+ });+ });++ it("keeps the active filters when moving to the next page", async () => {+ setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { "entity-types": ["dashboard"] },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "dashboard",+ ]);+ });++ it("resets to all entity types when the last selected type is deselected", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["dashboard"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Dashboards" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ const allEntityTypes = ["Dashboards", "Documents", "Collections"];+ allEntityTypes.forEach((label) => {+ expect(+ within(popover).getByRole("checkbox", { name: label }),+ ).toBeChecked();+ });+ });+ describe("sorting", () => {+ it("does not offer sorting by mixed-unit crowded content counts", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "sort-column": "content-count" },+ });+ await waitForListToLoad();++ const header = screen.getByRole("columnheader", {+ name: /^Content count/,+ });+ expect(header).not.toHaveAttribute("aria-sort");+ await userEvent.click(header);+ expect(getUrlQuery(router)).toEqual({});+ });++ it("sorts empty content by its comparable count", async () => {+ const { router } = setup({ mode: "empty", findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByRole("columnheader", { name: /^Content count/ }),+ );+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "content-count",+ "sort-direction": "asc",+ });+ });+ });+ it.each([+ ["Name", "name"],+ ["Type", "entity-type"],+ ["Location", "collection-name"],+ ["Created by", "created-by"],+ ["Created at", "created-at"],+ ])("sorts by %s", async (header, sortColumn) => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByRole("columnheader", { name: new RegExp(`^${header}`) }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": sortColumn,+ "sort-direction": "asc",+ });+ });+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ sortColumn,+ );+ });++ it("cycles a column through ascending, descending and unsorted", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ const header = () =>+ screen.getByRole("columnheader", { name: /^Created at/ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "ascending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "asc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "descending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "desc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(header()).not.toHaveAttribute("aria-sort");+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.tsxnew file mode 100644index 00000000000..62ce999be39--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.tsx@@ -0,0 +1,13 @@+import { SlowContent } from "../components";+import { slowContentConfig } from "../config";++import { ContentDiagnosticsPage } from "./ContentDiagnosticsPage";++export function SlowContentPage() {+ return (+ <ContentDiagnosticsPage+ config={slowContentConfig}+ component={SlowContent}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.unit.spec.tsxnew file mode 100644index 00000000000..fee6591ace0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/SlowContentPage.unit.spec.tsx@@ -0,0 +1,427 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupListSlowFindingsEndpoint,+ setupUserKeyValueEndpoints,+} from "__support__/server-mocks";+import {+ type TestRouter,+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitFor,+ within,+} from "__support__/ui";+import type { UrlStateQuery } from "metabase/common/hooks/use-url-state";+import { MonitorContent } from "metabase/monitor/components/MonitorLayout/MonitorContent";+import { Route, queryToSearch } from "metabase/router";+import * as Urls from "metabase/urls";+import { parseSearchQuery } from "metabase/utils/browser";+import type {+ ContentDiagnosticsSlowFinding,+ ContentDiagnosticsSlowUserParams,+ ListSlowFindingsResponse,+} from "metabase-types/api";+import {+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsSlowFinding,+ createMockContentDiagnosticsUser,+ createMockListSlowFindingsResponse,+ createMockUser,+} from "metabase-types/api/mocks";++import { SlowContentPage } from "./SlowContentPage";++const FINDINGS: ContentDiagnosticsSlowFinding[] = [+ createMockContentDiagnosticsSlowFinding({+ id: 1,+ entity_type: "card",+ entity_display_name: "Sales overview",+ duration_ms: 5000,+ }),+ createMockContentDiagnosticsSlowFinding({+ id: 2,+ entity_type: "dashboard",+ entity_display_name: "Marketing funnel",+ duration_ms: 65000,+ }),+];++type SetupOpts = {+ findings?: ContentDiagnosticsSlowFinding[];+ total?: number;+ urlParams?: UrlStateQuery;+ lastUsedParams?: ContentDiagnosticsSlowUserParams;+ error?: boolean;+ getResponse?: (url: string) => ListSlowFindingsResponse;+};++function setup({+ findings = [],+ total,+ urlParams = {},+ lastUsedParams = {},+ error = false,+ getResponse,+}: SetupOpts = {}) {+ if (error) {+ fetchMock.get("path:/api/ee/content-diagnostics/slow", {+ status: 500,+ body: { message: "Slow scan failed" },+ });+ } else if (getResponse) {+ fetchMock.get("path:/api/ee/content-diagnostics/slow", ({ url }) =>+ getResponse(url),+ );+ } else {+ setupListSlowFindingsEndpoint(+ createMockListSlowFindingsResponse({+ data: findings,+ total: total ?? findings.length,+ }),+ );+ }++ setupUserKeyValueEndpoints({+ namespace: "content_diagnostics",+ key: "slow",+ value: lastUsedParams,+ });++ mockGetBoundingClientRect({ width: 100, height: 100 });++ const { router } = renderWithProviders(+ <Route+ path={Urls.slowContent()}+ element={+ <MonitorContent>+ <SlowContentPage />+ </MonitorContent>+ }+ />,+ {+ withRouter: true,+ initialRoute: `${Urls.slowContent()}${queryToSearch(urlParams)}`,+ storeInitialState: {+ currentUser: createMockUser(),+ },+ },+ );++ return { router };+}++function getUrlQuery(router: TestRouter | undefined) {+ return parseSearchQuery(router?.location.search ?? "");+}++function getLastRequestUrl() {+ return new URL(+ String(+ fetchMock.callHistory.lastCall("path:/api/ee/content-diagnostics/slow")+ ?.url,+ ),+ "http://localhost",+ );+}++async function waitForListToLoad() {+ expect(await screen.findByRole("treegrid")).toBeInTheDocument();+}++describe("SlowContentPage", () => {+ it("renders slow findings with humanized durations in the table", async () => {+ setup({ findings: FINDINGS });++ const list = await screen.findByRole("treegrid");+ expect(await within(list).findByText("Sales overview")).toBeInTheDocument();+ expect(within(list).getByText("Marketing funnel")).toBeInTheDocument();+ expect(within(list).getByText("5.0s")).toBeInTheDocument();+ expect(within(list).getByText("1m 5s")).toBeInTheDocument();+ });++ it("shows an empty state when there are no findings", async () => {+ setup({ findings: [] });++ expect(+ await screen.findByText("No slow content found"),+ ).toBeInTheDocument();+ });++ it("renders selected slow finding details in the sidebar", async () => {+ const finding = createMockContentDiagnosticsSlowFinding({+ entity_id: 42,+ entity_display_name: "Revenue by category",+ duration_ms: 12300,+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 20,+ name: "Executive dashboards",+ effective_ancestors: [{ id: "root", name: "Our analytics" }],+ }),+ description: "Shows revenue grouped by product category.",+ owner: createMockContentDiagnosticsUser({ name: "Ada Owner" }),+ creator: createMockContentDiagnosticsUser({ name: "Grace Creator" }),+ view_count: 7,+ },+ });+ setup({ findings: [finding] });++ const list = await screen.findByRole("treegrid");+ await userEvent.click(await within(list).findByText("Revenue by category"));++ const sidebarRegion = await screen.findByTestId("monitor-sidebar-region");+ expect(sidebarRegion).toHaveTextContent("Revenue by category");+ expect(sidebarRegion).toHaveTextContent("Executive dashboards");+ expect(sidebarRegion).toHaveTextContent(+ "Shows revenue grouped by product category.",+ );+ expect(sidebarRegion).toHaveTextContent("Grace Creator");+ expect(sidebarRegion).toHaveTextContent("12.3s");+ expect(sidebarRegion).toHaveTextContent("7");+ });++ it("sends the minimum duration filter to the server and reflects it in the Filter popover", async () => {+ setup({ findings: FINDINGS, urlParams: { "min-duration-ms": "30000" } });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("min-duration-ms")).toBe(+ "30000",+ );++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ expect(+ within(popover).getByDisplayValue("30 seconds or more"),+ ).toBeInTheDocument();+ });++ it("sends the query parameter to the server when searching", async () => {+ setup({+ getResponse: (url) =>+ createMockListSlowFindingsResponse({+ data: url.includes("query=sales") ? [FINDINGS[0]] : FINDINGS,+ total: url.includes("query=sales") ? 1 : FINDINGS.length,+ }),+ });+ await waitForListToLoad();++ const input = screen.getByLabelText("Search");+ await userEvent.type(input, "sales");++ await waitFor(() => {+ expect(screen.queryByText("Marketing funnel")).not.toBeInTheDocument();+ });+ expect(screen.getByText("Sales overview")).toBeInTheDocument();+ expect(getLastRequestUrl().searchParams.get("query")).toBe("sales");+ });++ it("sends the selected entity types to the server via the Filter popover", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Dashboards" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "entity-types": [+ "question",+ "model",+ "metric",+ "document",+ "transform",+ ],+ });+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "question",+ "model",+ "metric",+ "document",+ "transform",+ ]);+ });++ it("resets to all entity types when the last selected type is deselected", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ const allEntityTypes = [+ "Questions",+ "Models",+ "Metrics",+ "Dashboards",+ "Documents",+ "Transforms",+ ];+ allEntityTypes.forEach((label) => {+ expect(+ within(popover).getByRole("checkbox", { name: label }),+ ).toBeChecked();+ });+ });++ it("shows the error state and suppresses the table when the request fails", async () => {+ setup({ error: true });++ expect(await screen.findByText("Slow scan failed")).toBeInTheDocument();+ expect(screen.queryByRole("treegrid")).not.toBeInTheDocument();+ });++ it("restores the last-used filter when the URL has no params", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: {},+ lastUsedParams: { min_duration_ms: 3000 },+ });++ await waitForListToLoad();++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "min-duration-ms": "3000",+ });+ });+ expect(getLastRequestUrl().searchParams.get("min-duration-ms")).toBe(+ "3000",+ );+ });++ it("lets an explicit default-valued URL win over the last-used filter", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { page: "0", "include-personal-collections": "true" },+ lastUsedParams: { min_duration_ms: 3000 },+ });++ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("min-duration-ms")).toBeNull();+ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("true");+ expect(getUrlQuery(router)).toEqual({});+ });+ it("marks the filter button once non-default filters are applied", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ screen.queryByTestId("content-diagnostics-filter-indicator"),+ ).not.toBeInTheDocument();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(+ screen.getByTestId("content-diagnostics-filter-indicator"),+ ).toBeInTheDocument();+ });+ });++ it("keeps the active filters when moving to the next page", async () => {+ setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "model",+ ]);+ });+ describe("sorting", () => {+ it.each([+ ["Name", "name"],+ ["Type", "entity-type"],+ ["Location", "collection-name"],+ ["Created by", "created-by"],+ ["Created at", "created-at"],+ ["Duration", "duration-ms"],+ ])("sorts by %s", async (header, sortColumn) => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByRole("columnheader", { name: new RegExp(`^${header}`) }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": sortColumn,+ "sort-direction": "asc",+ });+ });+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ sortColumn,+ );+ });++ it("cycles a column through ascending, descending and unsorted", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ const header = () =>+ screen.getByRole("columnheader", { name: /^Created at/ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "ascending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "asc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "descending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "desc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(header()).not.toHaveAttribute("aria-sort");+ });+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.tsxnew file mode 100644index 00000000000..b9e273857e2--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.tsx@@ -0,0 +1,13 @@+import { StaleContent } from "../components";+import { staleContentConfig } from "../config";++import { ContentDiagnosticsPage } from "./ContentDiagnosticsPage";++export function StaleContentPage() {+ return (+ <ContentDiagnosticsPage+ config={staleContentConfig}+ component={StaleContent}+ />+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.unit.spec.tsxnew file mode 100644index 00000000000..804d13a7f23--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/StaleContentPage.unit.spec.tsx@@ -0,0 +1,1065 @@+import userEvent from "@testing-library/user-event";+import fetchMock from "fetch-mock";++import {+ setupCardEndpoints,+ setupInvalidateFindingsEndpoint,+ setupListStaleFindingsEndpoint,+ setupUpdateCardEndpointWithError,+ setupUserKeyValueEndpoints,+} from "__support__/server-mocks";+import {+ type TestRouter,+ mockGetBoundingClientRect,+ renderWithProviders,+ screen,+ waitFor,+ within,+} from "__support__/ui";+import type { UrlStateQuery } from "metabase/common/hooks/use-url-state";+import { MonitorContent } from "metabase/monitor/components/MonitorLayout/MonitorContent";+import { Route, queryToSearch } from "metabase/router";+import * as Urls from "metabase/urls";+import { parseSearchQuery } from "metabase/utils/browser";+import { defer } from "metabase/utils/promise";+import type {+ ContentDiagnosticsStaleFinding,+ ContentDiagnosticsStaleUserParams,+ InvalidateFindingsResponse,+ ListStaleFindingsResponse,+} from "metabase-types/api";+import {+ createMockCard,+ createMockContentDiagnosticsCollection,+ createMockContentDiagnosticsStaleFinding,+ createMockContentDiagnosticsUser,+ createMockListStaleFindingsResponse,+ createMockUser,+} from "metabase-types/api/mocks";++import { StaleContentPage } from "./StaleContentPage";++const { trackSimpleEvent } = jest.requireMock("metabase/analytics");++const FINDINGS: ContentDiagnosticsStaleFinding[] = [+ createMockContentDiagnosticsStaleFinding({+ id: 1,+ entity_type: "card",+ entity_display_name: "Sales overview",+ }),+ createMockContentDiagnosticsStaleFinding({+ id: 2,+ entity_type: "dashboard",+ entity_display_name: "Marketing funnel",+ }),+];++type SetupOpts = {+ findings?: ContentDiagnosticsStaleFinding[];+ total?: number;+ urlParams?: UrlStateQuery;+ lastUsedParams?: ContentDiagnosticsStaleUserParams;+ error?: boolean;+ getResponse?: (+ url: string,+ ) => ListStaleFindingsResponse | Promise<ListStaleFindingsResponse>;+ withUndos?: boolean;+};++function setup({+ findings = [],+ total,+ urlParams = {},+ lastUsedParams = {},+ error = false,+ getResponse,+ withUndos = false,+}: SetupOpts = {}) {+ if (error) {+ fetchMock.get("path:/api/ee/content-diagnostics/stale", {+ status: 500,+ body: { message: "Stale scan failed" },+ });+ } else if (getResponse) {+ setupListStaleFindingsEndpoint(({ url }) => getResponse(url));+ } else {+ setupListStaleFindingsEndpoint(+ createMockListStaleFindingsResponse({+ data: findings,+ total: total ?? findings.length,+ }),+ );+ }++ setupUserKeyValueEndpoints({+ namespace: "content_diagnostics",+ key: "stale",+ value: lastUsedParams,+ });++ mockGetBoundingClientRect({ width: 100, height: 100 });++ const { router, store } = renderWithProviders(+ <Route+ path={Urls.staleContent()}+ element={+ <MonitorContent>+ <StaleContentPage />+ </MonitorContent>+ }+ />,+ {+ withRouter: true,+ withUndos,+ initialRoute: `${Urls.staleContent()}${queryToSearch(urlParams)}`,+ storeInitialState: {+ currentUser: createMockUser(),+ },+ },+ );++ return { router, store };+}++describe("StaleContentPage", () => {+ beforeEach(() => {+ trackSimpleEvent.mockClear();+ });++ describe("analytics", () => {+ it("reports the tab as viewed", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_tab_viewed",+ event_detail: "stale",+ });+ });++ it("reports the finding a user opens, and the entity behind it", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(screen.getByText("Sales overview"));++ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_finding_selected",+ triggered_from: "stale",+ target_id: FINDINGS[0].entity_id,+ event_detail: "card",+ });+ });+ });++ it("renders stale findings in the table", async () => {+ setup({ findings: FINDINGS });++ const list = await screen.findByRole("treegrid");+ expect(await within(list).findByText("Sales overview")).toBeInTheDocument();+ expect(within(list).getByText("Marketing funnel")).toBeInTheDocument();+ });++ it("shows an empty state when there are no findings", async () => {+ setup({ findings: [] });++ expect(+ await screen.findByText("No stale content found"),+ ).toBeInTheDocument();+ });++ it("allows findings for read-only entities to be dismissed but not trashed", async () => {+ setup({+ findings: [+ createMockContentDiagnosticsStaleFinding({+ id: 1,+ entity_display_name: "Can trash",+ can_write: true,+ }),+ createMockContentDiagnosticsStaleFinding({+ id: 2,+ entity_display_name: "Read only",+ can_write: false,+ }),+ ],+ });++ const list = await screen.findByRole("treegrid");+ await within(list).findByText("Can trash");++ const rows = within(list).getAllByRole("row");+ const writableRow = rows.find((row) =>+ within(row).queryByText("Can trash"),+ );+ const readonlyRow = rows.find((row) =>+ within(row).queryByText("Read only"),+ );+ if (writableRow == null || readonlyRow == null) {+ throw new Error("expected both finding rows to render");+ }++ expect(within(writableRow).getByRole("checkbox")).toBeEnabled();+ expect(within(readonlyRow).getByRole("checkbox")).toBeEnabled();+ await userEvent.click(within(readonlyRow).getByRole("checkbox"));+ expect(+ screen.getByRole("button", { name: "Move to trash" }),+ ).toBeDisabled();+ expect(+ screen.getByRole("button", { name: "Dismiss finding" }),+ ).toBeEnabled();+ });++ it("dismisses findings, refetches the list, and clears selection", async () => {+ let findings = [+ createMockContentDiagnosticsStaleFinding({+ id: 11,+ entity_id: 101,+ entity_display_name: "Dismiss me",+ can_write: false,+ }),+ ];+ setupInvalidateFindingsEndpoint(() => {+ findings = [];+ return { invalidated: [11], skipped: [] };+ });+ setup({+ getResponse: () =>+ createMockListStaleFindingsResponse({+ data: findings,+ total: findings.length,+ }),+ });+ await screen.findByRole("treegrid");+ await userEvent.click(screen.getByLabelText("Select all"));+ await confirmBulkAction("Dismiss finding");+ expect(+ await screen.findByText("No stale content found"),+ ).toBeInTheDocument();+ expect(screen.queryByText("Dismiss me")).not.toBeInTheDocument();+ expect(+ screen.queryByTestId("content-diagnostics-bulk-actions"),+ ).not.toBeInTheDocument();+ expect(+ fetchMock.callHistory.calls("path:/api/ee/content-diagnostics/stale")+ .length,+ ).toBeGreaterThan(1);+ const calls = fetchMock.callHistory.calls(+ "path:/api/ee/content-diagnostics/invalidate",+ );+ expect(calls).toHaveLength(1);+ const [call] = calls;+ expect(JSON.parse(String(call.options.body))).toEqual({ ids: [11] });+ });++ it("keeps rows and pagination until dismissal refetches the authoritative list", async () => {+ const findings = [+ ...FINDINGS,+ ...Array.from({ length: 23 }, (_, index) =>+ createMockContentDiagnosticsStaleFinding({+ id: index + 3,+ entity_id: index + 3,+ entity_display_name: `Finding ${index + 3}`,+ }),+ ),+ ];+ const remainingFinding = createMockContentDiagnosticsStaleFinding({+ id: 26,+ entity_id: 26,+ entity_display_name: "Remaining finding",+ });+ const dismissal = defer<InvalidateFindingsResponse>();+ const refresh = defer<ListStaleFindingsResponse>();+ const dismissalResponse = {+ invalidated: findings.map(({ id }) => id),+ skipped: [],+ };+ const refreshResponse = createMockListStaleFindingsResponse({+ data: [remainingFinding],+ total: 1,+ });+ let listRequests = 0;+ setupInvalidateFindingsEndpoint(() => dismissal.promise);+ setup({+ withUndos: true,+ getResponse: () => {+ listRequests += 1;+ return listRequests === 1+ ? createMockListStaleFindingsResponse({ data: findings, total: 26 })+ : refresh.promise;+ },+ });++ try {+ await screen.findByText("Sales overview");+ await userEvent.click(screen.getByLabelText("Select all"));+ await confirmBulkAction("Dismiss findings");++ expect(screen.getByText("Sales overview")).toBeVisible();+ expect(screen.getByText("Marketing funnel")).toBeVisible();+ expect(screen.getByText(/^1 - 25/)).toHaveTextContent("1 - 25 of 26");+ expect(+ screen.queryByText("No stale content found"),+ ).not.toBeInTheDocument();+ const dialog = screen.getByRole("dialog");+ expect(+ within(dialog).getByRole("button", { name: "Dismiss findings" }),+ ).toBeDisabled();+ expect(+ within(dialog).getByRole("button", { name: "Cancel" }),+ ).toBeDisabled();+ expect(+ screen.queryByText("Dismissed 25 findings"),+ ).not.toBeInTheDocument();++ dismissal.resolve(dismissalResponse);+ await waitFor(() =>+ expect(screen.getByText("Dismissed 25 findings")).toBeVisible(),+ );+ await waitFor(() => expect(listRequests).toBe(2));+ expect(screen.getByTestId("loading-overlay")).toBeVisible();+ expect(screen.getByText("Sales overview")).toBeVisible();+ expect(screen.getByText(/^1 - 25/)).toHaveTextContent("1 - 25 of 26");+ expect(+ screen.queryByText("No stale content found"),+ ).not.toBeInTheDocument();++ refresh.resolve(refreshResponse);+ expect(await screen.findByText("Remaining finding")).toBeVisible();+ expect(screen.queryByText("Sales overview")).not.toBeInTheDocument();+ expect(screen.queryByTestId("loading-overlay")).not.toBeInTheDocument();+ } finally {+ dismissal.resolve(dismissalResponse);+ refresh.resolve(refreshResponse);+ }+ });++ it("keeps rows and selection when dismissal fails", async () => {+ const dismissal = defer<Response>();+ const failureResponse = new Response(+ JSON.stringify({ message: "Dismiss failed" }),+ { status: 500, headers: { "Content-Type": "application/json" } },+ );+ setupInvalidateFindingsEndpoint(() => dismissal.promise);+ setup({ findings: FINDINGS, withUndos: true });++ try {+ await screen.findByText("Sales overview");+ await userEvent.click(screen.getByLabelText("Select all"));+ await confirmBulkAction("Dismiss findings");+ for (const name of ["Sales overview", "Marketing funnel"]) {+ const row = getFindingRow(name);+ expect(row).toBeVisible();+ expect(+ within(row).getByRole("checkbox", { hidden: true }),+ ).toBeChecked();+ }+ expect(screen.queryByText("Dismiss failed")).not.toBeInTheDocument();++ dismissal.resolve(failureResponse);+ await waitFor(() =>+ expect(screen.getByText("Dismiss failed")).toBeVisible(),+ );+ for (const name of ["Sales overview", "Marketing funnel"]) {+ const row = getFindingRow(name);+ expect(row).toBeVisible();+ expect(within(row).getByRole("checkbox")).toBeChecked();+ }+ expect(screen.getByText("2 items selected")).toBeVisible();+ expect(+ screen.getByRole("button", { name: "Dismiss findings" }),+ ).toBeEnabled();+ expect(+ screen.queryByText("Dismissed 2 findings"),+ ).not.toBeInTheDocument();+ } finally {+ dismissal.resolve(failureResponse);+ }+ });++ it("returns to the first page when dismissal removes the last page", async () => {+ const finding = createMockContentDiagnosticsStaleFinding({+ id: 26,+ entity_display_name: "Last page finding",+ });+ let dismissed = false;+ setupInvalidateFindingsEndpoint(() => {+ dismissed = true;+ return { invalidated: [26], skipped: [] };+ });+ const { router } = setup({+ urlParams: { page: "1" },+ getResponse: (url) => {+ const lastPageData = dismissed ? [] : [finding];+ return createMockListStaleFindingsResponse({+ data: url.includes("offset=25") ? lastPageData : FINDINGS,+ total: dismissed ? 25 : 26,+ });+ },+ });+ await screen.findByText("Last page finding");+ await userEvent.click(screen.getByLabelText("Select all"));+ await confirmBulkAction("Dismiss finding");+ expect(await screen.findByText("Sales overview")).toBeInTheDocument();+ await waitFor(() =>+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("0"),+ );+ expect(getUrlQuery(router)).toEqual({});+ expect(+ screen.queryByTestId("content-diagnostics-bulk-actions"),+ ).not.toBeInTheDocument();+ });++ it("archives the selected findings and refetches the list", async () => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ setupCardEndpoints(createMockCard({ id: 2 }));+ const { store } = setup({+ findings: [+ createMockContentDiagnosticsStaleFinding({+ id: 1,+ entity_type: "card",+ entity_id: 1,+ entity_display_name: "First card",+ can_write: true,+ }),+ createMockContentDiagnosticsStaleFinding({+ id: 2,+ entity_type: "card",+ entity_id: 2,+ entity_display_name: "Second card",+ can_write: true,+ }),+ ],+ });++ await screen.findByRole("treegrid");+ await userEvent.click(screen.getByLabelText("Select all"));+ const bulkActions = screen.getByTestId("content-diagnostics-bulk-actions");+ expect(+ within(screen.getByTestId("monitor-main")).getByTestId(+ "content-diagnostics-bulk-actions",+ ),+ ).toBe(bulkActions);+ expect(bulkActions).toHaveStyle({+ position: "absolute",+ left: "50%",+ bottom: "var(--mantine-spacing-lg)",+ });+ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );++ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Move to trash" }),+ );++ await waitFor(() => {+ expect(fetchMock.callHistory.calls("path:/api/card/1")).toHaveLength(1);+ });+ expect(fetchMock.callHistory.calls("path:/api/card/2")).toHaveLength(1);+ // the two archive requests set archived+ for (const id of [1, 2]) {+ const [call] = fetchMock.callHistory.calls(`path:/api/card/${id}`);+ expect(JSON.parse(String(call.options?.body))).toMatchObject({+ archived: true,+ });+ }+ // archiving invalidates the findings cache, so the list refetches+ await waitFor(() => {+ expect(+ fetchMock.callHistory.calls("path:/api/ee/content-diagnostics/stale")+ .length,+ ).toBeGreaterThan(1);+ });++ await waitFor(() => {+ expect(+ store+ .getState()+ .undo.some((undo) => undo.message === "Moved 2 items to the trash"),+ ).toBe(true);+ });+ });++ it("keeps items that failed to trash selected", async () => {+ setupCardEndpoints(createMockCard({ id: 1 }));+ setupUpdateCardEndpointWithError(2);+ const { store } = setup({+ findings: [+ createMockContentDiagnosticsStaleFinding({+ id: 1,+ entity_type: "card",+ entity_id: 1,+ entity_display_name: "Ok card",+ can_write: true,+ }),+ createMockContentDiagnosticsStaleFinding({+ id: 2,+ entity_type: "card",+ entity_id: 2,+ entity_display_name: "Bad card",+ can_write: true,+ }),+ ],+ });++ await screen.findByRole("treegrid");+ await userEvent.click(screen.getByLabelText("Select all"));+ await userEvent.click(+ screen.getByRole("button", { name: "Move to trash" }),+ );+ const dialog = await screen.findByRole("dialog");+ await userEvent.click(+ within(dialog).getByRole("button", { name: "Move to trash" }),+ );++ // the failed item stays selected, so the bar reappears with just it+ expect(await screen.findByText("1 item selected")).toBeInTheDocument();+ await waitFor(() => {+ expect(+ store+ .getState()+ .undo.some((undo) => undo.message === "Couldn't remove 1 item"),+ ).toBe(true);+ });+ });++ it("renders selected stale finding details in the Monitor sidebar outlet", async () => {+ const finding = createMockContentDiagnosticsStaleFinding({+ entity_id: 42,+ entity_display_name: "Revenue by category",+ details: {+ collection: createMockContentDiagnosticsCollection({+ id: 20,+ name: "Executive dashboards",+ effective_ancestors: [{ id: "root", name: "Our analytics" }],+ }),+ description: "Shows revenue grouped by product category.",+ owner: createMockContentDiagnosticsUser({ name: "Ada Owner" }),+ creator: createMockContentDiagnosticsUser({ name: "Grace Creator" }),+ },+ });+ setup({ findings: [finding] });++ const list = await screen.findByRole("treegrid");+ await userEvent.click(await within(list).findByText("Revenue by category"));++ const sidebarRegion = await screen.findByTestId("monitor-sidebar-region");+ const sidebarHeader = within(sidebarRegion).getByTestId(+ "content-diagnostics-sidebar-header",+ );+ expect(sidebarRegion).toHaveTextContent("Revenue by category");+ expect(+ within(sidebarHeader).getByRole("link", {+ name: "Revenue by category",+ }),+ ).toHaveAttribute("href", "/question/42-revenue-by-category");+ const locationRegion = within(sidebarRegion).getByRole("region", {+ name: "Location",+ });+ expect(+ within(locationRegion).getByRole("link", {+ name: "Executive dashboards",+ }),+ ).toBeInTheDocument();+ expect(+ within(locationRegion).queryByRole("link", {+ name: "Revenue by category",+ }),+ ).not.toBeInTheDocument();+ expect(sidebarRegion).toHaveTextContent("Our analytics");+ expect(sidebarRegion).toHaveTextContent("Executive dashboards");+ expect(sidebarRegion).toHaveTextContent(+ "Shows revenue grouped by product category.",+ );+ expect(sidebarRegion).toHaveTextContent("Ada Owner");+ expect(sidebarRegion).toHaveTextContent("Grace Creator");+ expect(screen.getByTestId("monitor-main")).not.toContainElement(+ sidebarRegion,+ );+ });++ it("sets the page parameter when navigating to the next page", async () => {+ const { router } = setup({ findings: FINDINGS, total: 50 });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ expect(getUrlQuery(router)).toEqual({ page: "1" });+ });++ it("clamps a page that no longer exists after a refetch", async () => {+ const { router } = setup({+ urlParams: { page: "1" },+ getResponse: (url) =>+ createMockListStaleFindingsResponse({+ data:+ new URL(url, "http://localhost").searchParams.get("offset") === "25"+ ? []+ : FINDINGS,+ total: 2,+ }),+ });++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("0");+ });+ expect(await screen.findByText("Sales overview")).toBeInTheDocument();+ });++ it("clears the page parameter when navigating back to the first page", async () => {+ const { router } = setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { page: "1" },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Previous page"));++ expect(getUrlQuery(router)).toEqual({});+ });++ it("refetches the stale endpoint with the next offset and renders the next page", async () => {+ const secondPageFinding = createMockContentDiagnosticsStaleFinding({+ id: 3,+ entity_type: "card",+ entity_display_name: "Second page question",+ });+ setup({+ total: 50,+ getResponse: (url) => {+ const isSecondPage = url.includes("offset=25");+ return createMockListStaleFindingsResponse({+ data: isSecondPage ? [secondPageFinding] : FINDINGS,+ total: 50,+ });+ },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ expect(await screen.findByText("Second page question")).toBeInTheDocument();+ expect(screen.queryByText("Sales overview")).not.toBeInTheDocument();++ const lastUrl = getLastRequestUrl();+ expect(lastUrl.searchParams.get("limit")).toBe("25");+ expect(lastUrl.searchParams.get("offset")).toBe("25");+ });++ it("resets pagination when table sorting changes", async () => {+ const { router } = setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { page: "1" },+ });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");++ await userEvent.click(screen.getByRole("columnheader", { name: "Type" }));++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ "entity-type",+ );+ });+ expect(getLastRequestUrl().searchParams.get("sort-direction")).toBe("asc");+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("0");+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "entity-type",+ "sort-direction": "asc",+ });+ });++ it("shows the error state and suppresses the table when the stale request fails", async () => {+ setup({ error: true });++ expect(await screen.findByText("Stale scan failed")).toBeInTheDocument();+ expect(screen.queryByRole("treegrid")).not.toBeInTheDocument();+ expect(screen.queryByLabelText("Next page")).not.toBeInTheDocument();+ });++ it("sends the query parameter to the server when searching", async () => {+ setup({+ getResponse: (url) =>+ createMockListStaleFindingsResponse({+ data: url.includes("query=sales") ? [FINDINGS[0]] : FINDINGS,+ total: url.includes("query=sales") ? 1 : FINDINGS.length,+ }),+ });+ await waitForListToLoad();++ const input = screen.getByLabelText("Search");+ await userEvent.type(input, "sales");++ await waitFor(() => {+ expect(screen.queryByText("Marketing funnel")).not.toBeInTheDocument();+ });+ expect(screen.getByText("Sales overview")).toBeInTheDocument();+ expect(getLastRequestUrl().searchParams.get("query")).toBe("sales");+ });++ it("sends the selected entity types to the server via the Filter popover", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Dashboards" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "entity-types": [+ "question",+ "model",+ "metric",+ "document",+ "transform",+ ],+ });+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "question",+ "model",+ "metric",+ "document",+ "transform",+ ]);+ });++ it("clears the filters and the search box from the Filter popover", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { query: "revenue", "entity-types": ["dashboard"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Reset to defaults",+ }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();+ expect(screen.getByRole("textbox", { name: "Search" })).toHaveValue("");+ const searchParams = getLastRequestUrl().searchParams;+ expect(searchParams.get("query")).toBeNull();+ expect(searchParams.getAll("entity-types")).toEqual([]);+ expect(trackSimpleEvent).toHaveBeenCalledWith({+ event: "content_diagnostics_filters_reset",+ triggered_from: "stale",+ });+ });++ it("offers nothing to reset while the filters and search are untouched", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );++ expect(+ within(await screen.findByRole("dialog")).getByRole("button", {+ name: "Reset to defaults",+ }),+ ).toBeDisabled();+ });++ it("filters by personal collections server-side via the Location toggle", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("true");++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", {+ name: "Include items in personal collections",+ }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "include-personal-collections": "false",+ });+ });+ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("false");+ });++ it("reflects the staleness threshold from the URL and sends changes made in the Filter popover", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "threshold-days": "90" },+ });+ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.get("threshold-days")).toBe("90");++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ const input = within(popover).getByDisplayValue("90 days or more");++ await userEvent.click(input);+ await userEvent.click(+ await within(popover).findByRole("option", { name: "1 year or more" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "threshold-days": "365",+ });+ });+ expect(getLastRequestUrl().searchParams.get("threshold-days")).toBe("365");++ await userEvent.click(within(popover).getByLabelText("Clear"));++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(+ within(popover).getByPlaceholderText("Any length of time"),+ ).toHaveValue("");+ });++ it("restores the last-used filter when the URL has no params", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: {},+ lastUsedParams: { entity_types: ["model"] },+ });++ await waitForListToLoad();++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "entity-types": "model",+ });+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "model",+ ]);+ });++ it("prefers URL params over the last-used filter", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["dashboard"] },+ lastUsedParams: { entity_types: ["model"] },+ });++ await waitForListToLoad();++ expect(getUrlQuery(router)).toEqual({+ "entity-types": "dashboard",+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "dashboard",+ ]);+ });++ it("lets an explicit default-valued URL win over the last-used filter", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { page: "0", "include-personal-collections": "true" },+ lastUsedParams: { entity_types: ["model"] },+ });++ await waitForListToLoad();++ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([]);+ expect(+ getLastRequestUrl().searchParams.get("include-personal-collections"),+ ).toBe("true");+ expect(getUrlQuery(router)).toEqual({});+ });+ it("marks the filter button once non-default filters are applied", async () => {+ setup({ findings: FINDINGS });+ await waitForListToLoad();++ expect(+ screen.queryByTestId("content-diagnostics-filter-indicator"),+ ).not.toBeInTheDocument();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(+ screen.getByTestId("content-diagnostics-filter-indicator"),+ ).toBeInTheDocument();+ });+ });++ it("keeps the active filters when moving to the next page", async () => {+ setup({+ findings: FINDINGS,+ total: 50,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(screen.getByLabelText("Next page"));++ await waitFor(() => {+ expect(getLastRequestUrl().searchParams.get("offset")).toBe("25");+ });+ expect(getLastRequestUrl().searchParams.getAll("entity-types")).toEqual([+ "model",+ ]);+ });++ it("resets to all entity types when the last selected type is deselected", async () => {+ const { router } = setup({+ findings: FINDINGS,+ urlParams: { "entity-types": ["model"] },+ });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByTestId("content-diagnostics-filter-button"),+ );+ const popover = await screen.findByRole("dialog");+ await userEvent.click(+ within(popover).getByRole("checkbox", { name: "Models" }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ const allEntityTypes = [+ "Questions",+ "Models",+ "Metrics",+ "Dashboards",+ "Documents",+ "Transforms",+ ];+ allEntityTypes.forEach((label) => {+ expect(+ within(popover).getByRole("checkbox", { name: label }),+ ).toBeChecked();+ });+ });+ describe("sorting", () => {+ it.each([+ ["Name", "name"],+ ["Type", "entity-type"],+ ["Location", "collection-name"],+ ["Created by", "created-by"],+ ["Created at", "created-at"],+ ["Last active", "last-active-at"],+ ])("sorts by %s", async (header, sortColumn) => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ await userEvent.click(+ screen.getByRole("columnheader", { name: new RegExp(`^${header}`) }),+ );++ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({+ "sort-column": sortColumn,+ "sort-direction": "asc",+ });+ });+ expect(getLastRequestUrl().searchParams.get("sort-column")).toBe(+ sortColumn,+ );+ });++ it("cycles a column through ascending, descending and unsorted", async () => {+ const { router } = setup({ findings: FINDINGS });+ await waitForListToLoad();++ const header = () =>+ screen.getByRole("columnheader", { name: /^Created at/ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "ascending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "asc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(header()).toHaveAttribute("aria-sort", "descending");+ });+ expect(getUrlQuery(router)).toEqual({+ "sort-column": "created-at",+ "sort-direction": "desc",+ });++ await userEvent.click(header());+ await waitFor(() => {+ expect(getUrlQuery(router)).toEqual({});+ });+ expect(header()).not.toHaveAttribute("aria-sort");+ });+ });+});++function getUrlQuery(router: TestRouter | undefined) {+ return parseSearchQuery(router?.location.search ?? "");+}++function getLastRequestUrl() {+ return new URL(+ String(+ fetchMock.callHistory.lastCall("path:/api/ee/content-diagnostics/stale")+ ?.url,+ ),+ "http://localhost",+ );+}++async function waitForListToLoad() {+ expect(await screen.findByRole("treegrid")).toBeInTheDocument();+}++function getFindingRow(name: string) {+ const row = within(screen.getByRole("treegrid", { hidden: true }))+ .getAllByRole("row", { hidden: true })+ .find((row) => within(row).queryByText(name));+ if (row === undefined) {+ throw new Error(`Expected finding row: ${name}`);+ }+ return row;+}++async function confirmBulkAction(name: string) {+ await userEvent.click(screen.getByRole("button", { name }));+ await userEvent.click(+ within(await screen.findByRole("dialog")).getByRole("button", { name }),+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/index.ts b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/index.tsnew file mode 100644index 00000000000..fe5bdaad3bd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/pages/index.ts@@ -0,0 +1,4 @@+export * from "./SlowContentPage";+export * from "./StaleContentPage";+export * from "./DuplicatedContentPage";+export * from "./ImbalancedContentPage";diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/routes.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/routes.tsxnew file mode 100644index 00000000000..b2f6dad3765--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/monitor/content-diagnostics/routes.tsx@@ -0,0 +1,24 @@+import { Route, redirect } from "metabase/router";++import {+ CrowdedContentPage,+ DuplicatedContentPage,+ EmptyContentPage,+ SlowContentPage,+ SparseContentPage,+ StaleContentPage,+} from "./pages";++export function getContentDiagnosticsRoutes() {+ return (+ <>+ <Route index element={redirect("stale")} />+ <Route path="stale" element={<StaleContentPage />} />+ <Route path="duplicated" element={<DuplicatedContentPage />} />+ <Route path="slow" element={<SlowContentPage />} />+ <Route path="empty" element={<EmptyContentPage />} />+ <Route path="sparse" element={<SparseContentPage />} />+ <Route path="crowded" element={<CrowdedContentPage />} />+ </>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DependencyDiagnostics.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DependencyDiagnostics.tsxindex b7b4f00639b..8e68e407544 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DependencyDiagnostics.tsx+++ b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DependencyDiagnostics.tsx@@ -22,6 +22,7 @@ import {getDependencyTypes,isSameNode,} from "metabase-enterprise/dependencies/utils";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";import type { DependencyEntry } from "metabase-types/api";import { DiagnosticsFilterBar } from "./DiagnosticsFilterBar";@@ -29,7 +30,6 @@ import { DiagnosticsHeader } from "./DiagnosticsHeader";import { DiagnosticsPagination } from "./DiagnosticsPagination";import { DiagnosticsSidebar } from "./DiagnosticsSidebar";import { DiagnosticsTable } from "./DiagnosticsTable";-import { PAGE_SIZE } from "./constants";import type {DependencyDiagnosticsMode,DependencyDiagnosticsParamsOptions,diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsx b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsxindex 914e9c3108f..7a3b626df0e 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsx+++ b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/DiagnosticsPagination/DiagnosticsPagination.tsx@@ -1,7 +1,6 @@import { PaginationControls } from "metabase/common/components/PaginationControls";import { Flex } from "metabase/ui";--import { PAGE_SIZE } from "../constants";+import { PAGE_SIZE } from "metabase-enterprise/monitor/constants";type DiagnosticsPaginationProps = {page: number;diff --git a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/constants.ts b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/constants.tsindex 5fe7d18f895..e01451740b4 100644--- a/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/constants.ts+++ b/enterprise/frontend/src/metabase-enterprise/monitor/dependency-diagnostics/components/constants.ts@@ -34,5 +34,3 @@ export const UNREFERENCED_GROUP_TYPES: DependencyGroupType[] = ["measure","snippet",];--export const PAGE_SIZE = 25;diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.ts b/enterprise/frontend/src/metabase-enterprise/plugins.tsindex d165f26bcc8..6785dfabe48 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.ts+++ b/enterprise/frontend/src/metabase-enterprise/plugins.ts@@ -33,6 +33,7 @@ import { initializePlugin as initializeMetabot } from "./metabot";import { initializePlugin as initializeModelPersistence } from "./model_persistence";import { initializePlugin as initializeModeration } from "./moderation";import { initializePlugin as initializeMonitorApiKeyUsage } from "./monitor/api-key-usage";+import { initializePlugin as initializeMonitorContentDiagnostics } from "./monitor/content-diagnostics";import { initializePlugin as initializeMonitorDependencyDiagnostics } from "./monitor/dependency-diagnostics";import { initializePlugin as initializeMonitorSessionManagement } from "./monitor/session-management";import { initializePlugin as initializeTools } from "./monitor/tools";@@ -90,6 +91,7 @@ export function initializePlugins() {initializeModelPersistence();initializeModeration();initializeMonitorApiKeyUsage();+ initializeMonitorContentDiagnostics();initializeMonitorDependencyDiagnostics();initializeMonitorSessionManagement();initializeMultiFactorAuth();diff --git a/frontend/src/metabase-types/api/content-diagnostics.ts b/frontend/src/metabase-types/api/content-diagnostics.tsnew file mode 100644index 00000000000..9ba3292db01--- /dev/null+++ b/frontend/src/metabase-types/api/content-diagnostics.ts@@ -0,0 +1,357 @@+import type { CardId, CardType } from "./card";+import type { CollectionId, CollectionNamespace } from "./collection";+import type { DashboardId } from "./dashboard";+import type { DocumentId } from "./document";+import type { PaginationRequest } from "./pagination";+import type { SortDirection } from "./sorting";+import type { TransformId } from "./transform";+import type { UserId } from "./user";+import type { VisualizationDisplay } from "./visualization";++export const CONTENT_DIAGNOSTICS_IMBALANCED_FINDING_TYPES = [+ "empty",+ "sparse",+ "crowded",+] as const;+export type ContentDiagnosticsImbalancedFindingType =+ (typeof CONTENT_DIAGNOSTICS_IMBALANCED_FINDING_TYPES)[number];++export const CONTENT_DIAGNOSTICS_DUPLICATED_FINDING_TYPES = [+ "duplicate_name",+] as const;+export type ContentDiagnosticsDuplicatedFindingType =+ (typeof CONTENT_DIAGNOSTICS_DUPLICATED_FINDING_TYPES)[number];++export const CONTENT_DIAGNOSTICS_FINDING_TYPES = [+ "stale",+ "slow",+ ...CONTENT_DIAGNOSTICS_DUPLICATED_FINDING_TYPES,+ ...CONTENT_DIAGNOSTICS_IMBALANCED_FINDING_TYPES,+] as const;+export type ContentDiagnosticsFindingType =+ (typeof CONTENT_DIAGNOSTICS_FINDING_TYPES)[number];++export const CONTENT_DIAGNOSTICS_ENTITY_TYPES = [+ "card",+ "dashboard",+ "document",+ "transform",+ "collection",+] as const;+export type ContentDiagnosticsEntityType =+ (typeof CONTENT_DIAGNOSTICS_ENTITY_TYPES)[number];++export type ContentDiagnosticsNonCollectionEntityType = Exclude<+ ContentDiagnosticsEntityType,+ "collection"+>;++export const CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES = [+ "question",+ "model",+ "metric",+ "dashboard",+ "document",+ "transform",+] as const;++/** Every filter value any endpoint accepts; each endpoint pins its own subset. */+export const CONTENT_DIAGNOSTICS_FILTER_TYPES = [+ ...CONTENT_DIAGNOSTICS_NON_COLLECTION_FILTER_TYPES,+ "collection",+] as const;+export type ContentDiagnosticsFilterType =+ (typeof CONTENT_DIAGNOSTICS_FILTER_TYPES)[number];++export type ContentDiagnosticsNonCollectionFilterType = Exclude<+ ContentDiagnosticsFilterType,+ "collection"+>;++export const CONTENT_DIAGNOSTICS_STALE_SORT_COLUMNS = [+ "name",+ "entity-type",+ "collection-name",+ "created-by",+ "created-at",+ "last-active-at",+] as const;+export type ContentDiagnosticsStaleSortColumn =+ (typeof CONTENT_DIAGNOSTICS_STALE_SORT_COLUMNS)[number];++export const CONTENT_DIAGNOSTICS_SLOW_SORT_COLUMNS = [+ "name",+ "entity-type",+ "collection-name",+ "created-by",+ "created-at",+ "duration-ms",+] as const;+export type ContentDiagnosticsSlowSortColumn =+ (typeof CONTENT_DIAGNOSTICS_SLOW_SORT_COLUMNS)[number];++export const CONTENT_DIAGNOSTICS_DUPLICATED_SORT_COLUMNS = [+ "name",+ "entity-type",+ "collection-name",+ "created-by",+ "created-at",+ "duplicate-count",+] as const;+export type ContentDiagnosticsDuplicatedSortColumn =+ (typeof CONTENT_DIAGNOSTICS_DUPLICATED_SORT_COLUMNS)[number];++export const CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS = [+ "name",+ "entity-type",+ "collection-name",+ "created-by",+ "created-at",+ "content-count",+] as const;+export type ContentDiagnosticsImbalancedSortColumn =+ (typeof CONTENT_DIAGNOSTICS_IMBALANCED_SORT_COLUMNS)[number];++export const CONTENT_DIAGNOSTICS_IMBALANCED_UNITS = [+ "items",+ "dashcards",+ "rows",+ "cards",+ "tabs",+] as const;+export type ContentDiagnosticsImbalancedUnit =+ (typeof CONTENT_DIAGNOSTICS_IMBALANCED_UNITS)[number];++export type ContentDiagnosticsStaleUserParams = {+ entity_types?: ContentDiagnosticsNonCollectionFilterType[];+ include_personal_collections?: boolean;+ threshold_days?: number;+ sort_column?: ContentDiagnosticsStaleSortColumn;+ sort_direction?: SortDirection;+};++export type ContentDiagnosticsUser =+ | {+ type: "user";+ id: UserId;+ name: string | null;+ email: string | null;+ }+ | {+ type: "external";+ email: string | null;+ };++export type ContentDiagnosticsCollection = {+ id: CollectionId;+ name: string;+ namespace: CollectionNamespace;+ effective_ancestors: Array<{ id: CollectionId; name: string }>;+};++export type ContentDiagnosticsBaseFindingDetails = {+ collection: ContentDiagnosticsCollection | null;+ description: string | null;+ owner: ContentDiagnosticsUser | null;+ creator: ContentDiagnosticsUser | null;+ view_count?: number;+};++export type ContentDiagnosticsEntityId =+ | CardId+ | Extract<CollectionId, number>+ | Extract<DashboardId, number>+ | DocumentId+ | TransformId;++export type ContentDiagnosticsFindingId = number;++export type ContentDiagnosticsBaseFinding = {+ id: ContentDiagnosticsFindingId;+ finding_type: ContentDiagnosticsFindingType;+ entity_type: ContentDiagnosticsEntityType;+ card_type?: CardType | null;+ display?: VisualizationDisplay | null;+ entity_id: ContentDiagnosticsEntityId;+ detected_at: string;+ entity_display_name: string | null;+ created_at: string | null;+ can_write: boolean;+ details: ContentDiagnosticsBaseFindingDetails;+};++export type ContentDiagnosticsStaleFindingDetails =+ ContentDiagnosticsBaseFindingDetails & {+ threshold_days?: number;+ };++export type ContentDiagnosticsStaleFinding = ContentDiagnosticsBaseFinding & {+ finding_type: "stale";+ entity_type: ContentDiagnosticsNonCollectionEntityType;+ last_active_at: string | null;+ details: ContentDiagnosticsStaleFindingDetails;+};++export type ListStaleFindingsRequest = {+ query?: string;+ "entity-types"?: ContentDiagnosticsNonCollectionFilterType[];+ "include-personal-collections"?: boolean;+ "threshold-days"?: number;+ "sort-column"?: ContentDiagnosticsStaleSortColumn;+ "sort-direction"?: SortDirection;+} & PaginationRequest;++export type ListStaleFindingsResponse = {+ data: ContentDiagnosticsStaleFinding[];+ total: number;+ limit: number | null;+ offset: number | null;+ last_scan_at: string | null;+};++export type ContentDiagnosticsSlowUserParams = {+ entity_types?: ContentDiagnosticsNonCollectionFilterType[];+ include_personal_collections?: boolean;+ min_duration_ms?: number;+ sort_column?: ContentDiagnosticsSlowSortColumn;+ sort_direction?: SortDirection;+};++export type ContentDiagnosticsSlowEntity = {+ id: ContentDiagnosticsEntityId;+ name: string | null;+ entity_type: ContentDiagnosticsEntityType;+ card_type?: CardType | null;+ view_count: number;+};++export type ContentDiagnosticsSlowFindingDetails =+ ContentDiagnosticsBaseFindingDetails & {+ threshold_ms?: number;+ slow_entities?: ContentDiagnosticsSlowEntity[];+ };++export type ContentDiagnosticsSlowFinding = ContentDiagnosticsBaseFinding & {+ finding_type: "slow";+ entity_type: ContentDiagnosticsNonCollectionEntityType;+ duration_ms: number;+ details: ContentDiagnosticsSlowFindingDetails;+};++export type ListSlowFindingsRequest = {+ query?: string;+ "entity-types"?: ContentDiagnosticsNonCollectionFilterType[];+ "include-personal-collections"?: boolean;+ "min-duration-ms"?: number;+ "sort-column"?: ContentDiagnosticsSlowSortColumn;+ "sort-direction"?: SortDirection;+} & PaginationRequest;++export type ListSlowFindingsResponse = {+ data: ContentDiagnosticsSlowFinding[];+ total: number;+ limit: number | null;+ offset: number | null;+ last_scan_at: string | null;+};++export type ContentDiagnosticsDuplicatedUserParams = {+ entity_types?: ContentDiagnosticsFilterType[];+ include_personal_collections?: boolean;+ min_duplicate_count?: number;+ sort_column?: ContentDiagnosticsDuplicatedSortColumn;+ sort_direction?: SortDirection;+};++/**+ * Another kind of duplicate finding: entity of the same type sharing its normalized name.+ * Transforms and collections have no view concept, hence the optional `view_count`.+ */+export type ContentDiagnosticsDuplicateEntity = {+ id: ContentDiagnosticsEntityId;+ name: string | null;+ entity_type: ContentDiagnosticsEntityType;+ card_type?: CardType | null;+ display?: VisualizationDisplay | null;+ namespace?: CollectionNamespace;+ view_count?: number;+};++export type ContentDiagnosticsDuplicatedFindingDetails =+ ContentDiagnosticsBaseFindingDetails & {+ normalized_name: string;+ duplicate_entities: ContentDiagnosticsDuplicateEntity[];+ };++export type ContentDiagnosticsDuplicatedFinding =+ ContentDiagnosticsBaseFinding & {+ finding_type: ContentDiagnosticsDuplicatedFindingType;+ duplicate_count: number;+ details: ContentDiagnosticsDuplicatedFindingDetails;+ };++export type ListDuplicatedFindingsRequest = {+ query?: string;+ "entity-types"?: ContentDiagnosticsFilterType[];+ "include-personal-collections"?: boolean;+ "min-duplicate-count"?: number;+ "sort-column"?: ContentDiagnosticsDuplicatedSortColumn;+ "sort-direction"?: SortDirection;+} & PaginationRequest;++export type ListDuplicatedFindingsResponse = {+ data: ContentDiagnosticsDuplicatedFinding[];+ total: number;+ limit: number | null;+ offset: number | null;+ last_scan_at: string | null;+};++export type ContentDiagnosticsImbalancedUserParams = {+ entity_types?: ContentDiagnosticsFilterType[];+ include_personal_collections?: boolean;+ sort_column?: ContentDiagnosticsImbalancedSortColumn;+ sort_direction?: SortDirection;+};++export type ContentDiagnosticsImbalancedFindingDetails =+ ContentDiagnosticsBaseFindingDetails & {+ threshold: number;+ unit: ContentDiagnosticsImbalancedUnit;+ as_of?: string;+ };++export type ContentDiagnosticsImbalancedFinding =+ ContentDiagnosticsBaseFinding & {+ finding_type: ContentDiagnosticsImbalancedFindingType;+ content_count: number;+ details: ContentDiagnosticsImbalancedFindingDetails;+ };++// `finding-types` pins the problem type per tab (empty/sparse/crowded); it is not a+// user-facing filter, so it is absent from `ContentDiagnosticsImbalancedUserParams`.+export type ListImbalancedFindingsRequest = {+ query?: string;+ "entity-types"?: ContentDiagnosticsFilterType[];+ "finding-types"?: ContentDiagnosticsImbalancedFindingType[];+ "include-personal-collections"?: boolean;+ "sort-column"?: ContentDiagnosticsImbalancedSortColumn;+ "sort-direction"?: SortDirection;+} & PaginationRequest;++export type ListImbalancedFindingsResponse = {+ data: ContentDiagnosticsImbalancedFinding[];+ total: number;+ limit: number | null;+ offset: number | null;+ last_scan_at: string | null;+};++export type InvalidateFindingsRequest = {+ ids: readonly ContentDiagnosticsFindingId[];+};++export type InvalidateFindingsResponse = {+ invalidated: ContentDiagnosticsFindingId[];+ skipped: ContentDiagnosticsFindingId[];+};diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex dcabc5312a3..89c0f10d47e 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -13,6 +13,7 @@ export * from "./click-behavior";export * from "./cloud-add-ons";export * from "./collection";export * from "./comments";+export * from "./content-diagnostics";export * from "./content-translation";export * from "./custom-viz-plugin";export * from "./dashboard";diff --git a/frontend/src/metabase-types/api/mocks/content-diagnostics.ts b/frontend/src/metabase-types/api/mocks/content-diagnostics.tsnew file mode 100644index 00000000000..e358e72d07c--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/content-diagnostics.ts@@ -0,0 +1,219 @@+import type {+ ContentDiagnosticsCollection,+ ContentDiagnosticsDuplicateEntity,+ ContentDiagnosticsDuplicatedFinding,+ ContentDiagnosticsDuplicatedFindingDetails,+ ContentDiagnosticsImbalancedFinding,+ ContentDiagnosticsImbalancedFindingDetails,+ ContentDiagnosticsSlowFinding,+ ContentDiagnosticsSlowFindingDetails,+ ContentDiagnosticsStaleFinding,+ ContentDiagnosticsStaleFindingDetails,+ ContentDiagnosticsUser,+ ListDuplicatedFindingsResponse,+ ListImbalancedFindingsResponse,+ ListSlowFindingsResponse,+ ListStaleFindingsResponse,+} from "metabase-types/api";++export function createMockContentDiagnosticsUser(+ opts?: Partial<Extract<ContentDiagnosticsUser, { type: "user" }>>,+): ContentDiagnosticsUser {+ return {+ type: "user",+ id: 1,+ name: "Test User",+ email: "[email protected]",+ ...opts,+ };+}++export function createMockContentDiagnosticsCollection(+ opts?: Partial<ContentDiagnosticsCollection>,+): ContentDiagnosticsCollection {+ return {+ id: 1,+ name: "First collection",+ namespace: null,+ effective_ancestors: [],+ ...opts,+ };+}++export function createMockContentDiagnosticsStaleFinding(+ opts?: Partial<Omit<ContentDiagnosticsStaleFinding, "details">> & {+ details?: Partial<ContentDiagnosticsStaleFindingDetails>;+ },+): ContentDiagnosticsStaleFinding {+ return {+ id: 1,+ finding_type: "stale",+ entity_type: "card",+ entity_id: 10,+ detected_at: "2026-06-01T00:00:00Z",+ entity_display_name: "Stale question",+ created_at: "2026-01-01T00:00:00Z",+ can_write: true,+ last_active_at: "2026-03-01T00:00:00Z",+ ...opts,+ details: {+ collection: createMockContentDiagnosticsCollection(),+ description: null,+ owner: null,+ creator: createMockContentDiagnosticsUser(),+ view_count: 0,+ threshold_days: 90,+ ...opts?.details,+ },+ };+}++export function createMockListStaleFindingsResponse(+ opts?: Partial<ListStaleFindingsResponse>,+): ListStaleFindingsResponse {+ return {+ data: [createMockContentDiagnosticsStaleFinding()],+ total: 1,+ limit: 25,+ offset: 0,+ last_scan_at: "2026-06-01T00:00:00Z",+ ...opts,+ };+}++export function createMockContentDiagnosticsSlowFinding(+ opts?: Partial<Omit<ContentDiagnosticsSlowFinding, "details">> & {+ details?: Partial<ContentDiagnosticsSlowFindingDetails>;+ },+): ContentDiagnosticsSlowFinding {+ return {+ id: 1,+ finding_type: "slow",+ entity_type: "card",+ entity_id: 10,+ detected_at: "2026-06-01T00:00:00Z",+ entity_display_name: "Slow question",+ created_at: "2026-01-01T00:00:00Z",+ can_write: true,+ duration_ms: 5000,+ ...opts,+ details: {+ collection: createMockContentDiagnosticsCollection(),+ description: null,+ owner: null,+ creator: createMockContentDiagnosticsUser(),+ view_count: 0,+ threshold_ms: 1000,+ ...opts?.details,+ },+ };+}++export function createMockListSlowFindingsResponse(+ opts?: Partial<ListSlowFindingsResponse>,+): ListSlowFindingsResponse {+ return {+ data: [createMockContentDiagnosticsSlowFinding()],+ total: 1,+ limit: 25,+ offset: 0,+ last_scan_at: "2026-06-01T00:00:00Z",+ ...opts,+ };+}++export function createMockContentDiagnosticsDuplicateEntity(+ opts?: Partial<ContentDiagnosticsDuplicateEntity>,+): ContentDiagnosticsDuplicateEntity {+ return {+ id: 11,+ name: "Duplicated question",+ entity_type: "card",+ view_count: 0,+ ...opts,+ };+}++export function createMockContentDiagnosticsDuplicatedFinding(+ opts?: Partial<Omit<ContentDiagnosticsDuplicatedFinding, "details">> & {+ details?: Partial<ContentDiagnosticsDuplicatedFindingDetails>;+ },+): ContentDiagnosticsDuplicatedFinding {+ return {+ id: 1,+ finding_type: "duplicate_name",+ entity_type: "card",+ entity_id: 10,+ detected_at: "2026-06-01T00:00:00Z",+ entity_display_name: "Duplicated question",+ created_at: "2026-01-01T00:00:00Z",+ can_write: true,+ duplicate_count: 1,+ ...opts,+ details: {+ collection: createMockContentDiagnosticsCollection(),+ description: null,+ owner: null,+ creator: createMockContentDiagnosticsUser(),+ view_count: 0,+ normalized_name: "duplicated question",+ duplicate_entities: [createMockContentDiagnosticsDuplicateEntity()],+ ...opts?.details,+ },+ };+}++export function createMockListDuplicatedFindingsResponse(+ opts?: Partial<ListDuplicatedFindingsResponse>,+): ListDuplicatedFindingsResponse {+ return {+ data: [createMockContentDiagnosticsDuplicatedFinding()],+ total: 1,+ limit: 25,+ offset: 0,+ last_scan_at: "2026-06-01T00:00:00Z",+ ...opts,+ };+}++export function createMockContentDiagnosticsImbalancedFinding(+ opts?: Partial<Omit<ContentDiagnosticsImbalancedFinding, "details">> & {+ details?: Partial<ContentDiagnosticsImbalancedFindingDetails>;+ },+): ContentDiagnosticsImbalancedFinding {+ return {+ id: 1,+ finding_type: "crowded",+ entity_type: "collection",+ entity_id: 10,+ detected_at: "2026-06-01T00:00:00Z",+ entity_display_name: "Crowded collection",+ created_at: "2026-01-01T00:00:00Z",+ can_write: true,+ content_count: 101,+ ...opts,+ details: {+ collection: createMockContentDiagnosticsCollection(),+ description: null,+ owner: null,+ creator: createMockContentDiagnosticsUser(),+ view_count: 0,+ threshold: 100,+ unit: "items",+ ...opts?.details,+ },+ };+}++export function createMockListImbalancedFindingsResponse(+ opts?: Partial<ListImbalancedFindingsResponse>,+): ListImbalancedFindingsResponse {+ return {+ data: [createMockContentDiagnosticsImbalancedFinding()],+ total: 1,+ limit: 25,+ offset: 0,+ last_scan_at: "2026-06-01T00:00:00Z",+ ...opts,+ };+}diff --git a/frontend/src/metabase-types/api/mocks/index.ts b/frontend/src/metabase-types/api/mocks/index.tsindex 1e19f6f7ba1..6f9289cbe1c 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -5,6 +5,7 @@ export * from "./automagic-dashboards";export * from "./bookmark";export * from "./card";export * from "./collection";+export * from "./content-diagnostics";export * from "./custom-viz";export * from "./dashboard";export * from "./data-app";diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.tsindex bb1a95ead85..178c6ab9616 100644--- a/frontend/src/metabase-types/api/mocks/settings.ts+++ b/frontend/src/metabase-types/api/mocks/settings.ts@@ -104,6 +104,7 @@ export const createMockTokenFeatures = ("schema-viewer": false,disable_password_login: false,cloud_custom_smtp: false,+ content_diagnostics: false,content_translation: false,content_verification: false,"custom-viz": false,diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.tsindex 251a58760ec..8df824029c2 100644--- a/frontend/src/metabase-types/api/settings.ts+++ b/frontend/src/metabase-types/api/settings.ts@@ -332,6 +332,7 @@ export const tokenFeatures = ["audit_app","cache_granular_controls","cloud_custom_smtp",+ "content_diagnostics","content_translation","content_verification","custom-viz",diff --git a/frontend/src/metabase-types/api/user.ts b/frontend/src/metabase-types/api/user.tsindex 638f582449c..78b3fa3b465 100644--- a/frontend/src/metabase-types/api/user.ts+++ b/frontend/src/metabase-types/api/user.ts@@ -1,4 +1,10 @@import type { CollectionId } from "./collection";+import type {+ ContentDiagnosticsDuplicatedUserParams,+ ContentDiagnosticsImbalancedUserParams,+ ContentDiagnosticsSlowUserParams,+ ContentDiagnosticsStaleUserParams,+} from "./content-diagnostics";import type { DashboardId } from "./dashboard";import type { DatabaseId } from "./database";import type { DependencyDiagnosticsUserParams } from "./dependencies";@@ -213,6 +219,36 @@ export type UserKeyValue =key: string;value: DependencyDiagnosticsUserParams;}+ | {+ namespace: "content_diagnostics";+ key: "stale";+ value: ContentDiagnosticsStaleUserParams;+ }+ | {+ namespace: "content_diagnostics";+ key: "slow";+ value: ContentDiagnosticsSlowUserParams;+ }+ | {+ namespace: "content_diagnostics";+ key: "duplicated";+ value: ContentDiagnosticsDuplicatedUserParams;+ }+ | {+ namespace: "content_diagnostics";+ key: "empty";+ value: ContentDiagnosticsImbalancedUserParams;+ }+ | {+ namespace: "content_diagnostics";+ key: "sparse";+ value: ContentDiagnosticsImbalancedUserParams;+ }+ | {+ namespace: "content_diagnostics";+ key: "crowded";+ value: ContentDiagnosticsImbalancedUserParams;+ }| {namespace: "schema_viewer";key: "last_database";diff --git a/frontend/src/metabase/api/card.ts b/frontend/src/metabase/api/card.tsindex 7644deebd0d..443824ef14d 100644--- a/frontend/src/metabase/api/card.ts+++ b/frontend/src/metabase/api/card.ts@@ -205,6 +205,8 @@ export const cardApi = Api.injectEndpoints({idTag("table", `card__${payload.id}`),listTag("revision"),listTag("table"), // table listings include information about published models+ // updating a card resolves any content-diagnostics finding for it+ listTag("content-diagnostics-finding"),];if (payload.dashboard_id != null) {diff --git a/frontend/src/metabase/api/collection.ts b/frontend/src/metabase/api/collection.tsindex dba7db66b0c..ed799fd8be5 100644--- a/frontend/src/metabase/api/collection.ts+++ b/frontend/src/metabase/api/collection.ts@@ -166,6 +166,8 @@ export const collectionApi = Api.injectEndpoints({listTag("collection"),idTag("collection", payload.id),idTag("collection", payload.parent_id ?? "root"),+ // archiving a collection resolves content-diagnostics findings in its subtree+ listTag("content-diagnostics-finding"),];// When archiving/restoring a collection, invalidate bookmarksdiff --git a/frontend/src/metabase/api/dashboard.ts b/frontend/src/metabase/api/dashboard.tsindex 494c6b29060..b381ca4ddbc 100644--- a/frontend/src/metabase/api/dashboard.ts+++ b/frontend/src/metabase/api/dashboard.ts@@ -215,6 +215,8 @@ export const dashboardApi = Api.injectEndpoints({// Archiving the dashboard the user has as their homepage clears the homepage server-side.// getCurrentUser provides this tag, so invalidating it refetches the user.idTag("user-homepage-dashboard", id),+ // archiving a dashboard resolves any content-diagnostics finding for it+ listTag("content-diagnostics-finding"),]),}),deleteDashboard: builder.mutation<void, DashboardId>({diff --git a/frontend/src/metabase/api/document.ts b/frontend/src/metabase/api/document.tsindex ccccdb22f0c..d428f2aac58 100644--- a/frontend/src/metabase/api/document.ts+++ b/frontend/src/metabase/api/document.ts@@ -46,7 +46,13 @@ export const documentApi = Api.injectEndpoints({}),invalidatesTags: (_, error, { id }) =>!error- ? [listTag("document"), idTag("document", id), listTag("revision")]+ ? [+ listTag("document"),+ idTag("document", id),+ listTag("revision"),+ // archiving a document resolves any content-diagnostics finding for it+ listTag("content-diagnostics-finding"),+ ]: [],async onQueryStarted(_props, { dispatch, queryFulfilled }) {const { data } = await queryFulfilled;diff --git a/frontend/src/metabase/api/tags/constants.ts b/frontend/src/metabase/api/tags/constants.tsindex 2b01987ff9d..f0c3822574f 100644--- a/frontend/src/metabase/api/tags/constants.ts+++ b/frontend/src/metabase/api/tags/constants.ts@@ -12,6 +12,7 @@ export const TAG_TYPES = ["cloud-migration","collection","collection-tree",+ "content-diagnostics-finding","content-translation","dashboard","dashboard-question-candidates",diff --git a/frontend/src/metabase/api/transform.ts b/frontend/src/metabase/api/transform.tsindex 2f6e360ffee..85fe982a1f1 100644--- a/frontend/src/metabase/api/transform.ts+++ b/frontend/src/metabase/api/transform.ts@@ -290,6 +290,8 @@ export const transformApi = Api.injectEndpoints({idTag("transform", id),listTag("transform-run"),listTag("transform-dag-run"),+ // deleting a transform resolves any content-diagnostics finding for it+ listTag("content-diagnostics-finding"),]),}),deleteTransformTarget: builder.mutation<void, TransformId>({diff --git a/frontend/src/metabase/archive/analytics.ts b/frontend/src/metabase/archive/analytics.tsindex 26002373653..0692430a246 100644--- a/frontend/src/metabase/archive/analytics.ts+++ b/frontend/src/metabase/archive/analytics.ts@@ -20,6 +20,7 @@ type MoveToTrashTriggeredFrom =| "collection"| "detail_page"| "cleanup_modal"+ | "content_diagnostics"| "drag_and_drop";export const archiveAndTrack = async ({diff --git a/frontend/src/metabase/common/hooks/use-url-state/utils.ts b/frontend/src/metabase/common/hooks/use-url-state/utils.tsindex a766a90de18..6b96225bd57 100644--- a/frontend/src/metabase/common/hooks/use-url-state/utils.ts+++ b/frontend/src/metabase/common/hooks/use-url-state/utils.ts@@ -24,7 +24,16 @@ export function parseSortColumn<TColumn extends string>(param: QueryParam,columns: readonly TColumn[],defaultColumn: TColumn,-): TColumn {+): TColumn;+export function parseSortColumn<TColumn extends string>(+ param: QueryParam,+ columns: readonly TColumn[],+): TColumn | undefined;+export function parseSortColumn<TColumn extends string>(+ param: QueryParam,+ columns: readonly TColumn[],+ defaultColumn?: TColumn,+): TColumn | undefined {const value = getFirstParamValue(param);return value && isSortColumn(value, columns) ? value : defaultColumn;}@@ -32,7 +41,14 @@ export function parseSortColumn<TColumn extends string>(export function parseSortDirection(param: QueryParam,defaultDirection: SortDirection,-): SortDirection {+): SortDirection;+export function parseSortDirection(+ param: QueryParam,+): SortDirection | undefined;+export function parseSortDirection(+ param: QueryParam,+ defaultDirection?: SortDirection,+): SortDirection | undefined {const value = getFirstParamValue(param);return value === "asc" || value === "desc" ? value : defaultDirection;}diff --git a/frontend/src/metabase/common/hooks/use-url-state/utils.unit.spec.ts b/frontend/src/metabase/common/hooks/use-url-state/utils.unit.spec.tsnew file mode 100644index 00000000000..8a66a89a6b8--- /dev/null+++ b/frontend/src/metabase/common/hooks/use-url-state/utils.unit.spec.ts@@ -0,0 +1,29 @@+import { parseSortColumn, parseSortDirection } from "./utils";++describe("parseSortColumn", () => {+ const columns = ["name", "created_at"] as const;++ it("uses the default column when the value is missing or invalid", () => {+ expect(parseSortColumn(undefined, columns, "name")).toBe("name");+ expect(parseSortColumn("invalid", columns, "name")).toBe("name");+ });++ it("returns undefined when the default column is omitted", () => {+ expect(parseSortColumn(undefined, columns)).toBeUndefined();+ expect(parseSortColumn("invalid", columns)).toBeUndefined();+ expect(parseSortColumn("created_at", columns)).toBe("created_at");+ });+});++describe("parseSortDirection", () => {+ it("uses the default direction when the value is missing or invalid", () => {+ expect(parseSortDirection(undefined, "asc")).toBe("asc");+ expect(parseSortDirection("invalid", "asc")).toBe("asc");+ });++ it("returns undefined when the default direction is omitted", () => {+ expect(parseSortDirection(undefined)).toBeUndefined();+ expect(parseSortDirection("invalid")).toBeUndefined();+ expect(parseSortDirection("desc")).toBe("desc");+ });+});diff --git a/frontend/src/metabase/common/monitor/analytics.ts b/frontend/src/metabase/common/monitor/analytics.tsindex 07b7436c364..c6cc9a49def 100644--- a/frontend/src/metabase/common/monitor/analytics.ts+++ b/frontend/src/metabase/common/monitor/analytics.ts@@ -2,6 +2,7 @@ import { trackSimpleEvent } from "metabase/analytics";export type MonitorSection =| "diagnostics"+ | "content-diagnostics"| "erroring-questions"| "alerts"| "tasks"diff --git a/frontend/src/metabase/common/monitor/selectors.ts b/frontend/src/metabase/common/monitor/selectors.tsindex 5f45e9d8e82..0c133030dba 100644--- a/frontend/src/metabase/common/monitor/selectors.ts+++ b/frontend/src/metabase/common/monitor/selectors.ts@@ -6,25 +6,37 @@ import {import type { State } from "metabase/redux/store";import { isWithinIframe } from "metabase/utils/iframe";+function getUserHasMonitoringPermission(state: State) {+ return getUser(state)?.permissions?.can_access_monitoring ?? false;+}+// Like Data Studio, diagnostics come with the Data Analyst role rather than a// permissions graph, so they pause while the plan lacks the feature.-export function canAccessMonitorDiagnostics(state: State) {+export function canAccessDependencyDiagnostics(state: State) {if (isWithinIframe()) {return false;}return getUserIsAdmin(state) || getUserIsEntitledAnalyst(state);}-export function canAccessMonitoringTools(state: State) {+export function canAccessContentDiagnostics(state: State) {if (isWithinIframe()) {return false;}return (getUserIsAdmin(state) ||- (getUser(state)?.permissions?.can_access_monitoring ?? false)+ getUserIsEntitledAnalyst(state) ||+ getUserHasMonitoringPermission(state));}+export function canAccessMonitoringTools(state: State) {+ if (isWithinIframe()) {+ return false;+ }+ return getUserIsAdmin(state) || getUserHasMonitoringPermission(state);+}+export function canAccessAlertsManagement(state: State) {if (isWithinIframe()) {return false;@@ -57,7 +69,8 @@ export function canAccessApiKeyUsage(state: State) {export function canAccessMonitor(state: State) {return (- canAccessMonitorDiagnostics(state) ||+ canAccessDependencyDiagnostics(state) ||+ canAccessContentDiagnostics(state) ||canAccessMonitoringTools(state) ||canAccessAiAuditing(state));diff --git a/frontend/src/metabase/common/monitor/selectors.unit.spec.ts b/frontend/src/metabase/common/monitor/selectors.unit.spec.tsindex 4d3722e78ae..1dd05008eaa 100644--- a/frontend/src/metabase/common/monitor/selectors.unit.spec.ts+++ b/frontend/src/metabase/common/monitor/selectors.unit.spec.ts@@ -9,8 +9,9 @@ import {canAccessAiAuditing,canAccessAlertsManagement,canAccessApiKeyUsage,+ canAccessContentDiagnostics,+ canAccessDependencyDiagnostics,canAccessMonitor,- canAccessMonitorDiagnostics,canAccessMonitoringTools,canAccessSessionManagement,} from "./selectors";@@ -72,7 +73,7 @@ describe("canAccessMonitor", () => {expect(canAccessMonitor(state)).toBe(false);});- it("returns true for a monitoring-only user (tools access)", () => {+ it("returns true for a monitoring-only user (content diagnostics and tools access)", () => {const state = createMockState({currentUser: createMockUser({is_superuser: false,@@ -97,7 +98,7 @@ describe("canAccessMonitor", () => {});});-describe("canAccessMonitorDiagnostics", () => {+describe("canAccessDependencyDiagnostics", () => {beforeEach(() => {jest.clearAllMocks();isWithinIframe.mockReturnValue(false);@@ -109,7 +110,7 @@ describe("canAccessMonitorDiagnostics", () => {currentUser: createMockUser({ is_superuser: true }),});- expect(canAccessMonitorDiagnostics(state)).toBe(false);+ expect(canAccessDependencyDiagnostics(state)).toBe(false);});it("returns true when user is admin", () => {@@ -117,20 +118,20 @@ describe("canAccessMonitorDiagnostics", () => {currentUser: createMockUser({ is_superuser: true }),});- expect(canAccessMonitorDiagnostics(state)).toBe(true);+ expect(canAccessDependencyDiagnostics(state)).toBe(true);});it("returns true when user is analyst", () => {- expect(canAccessMonitorDiagnostics(createAnalystState())).toBe(true);+ expect(canAccessDependencyDiagnostics(createAnalystState())).toBe(true);});it("returns false for an analyst whose plan lost the feature", () => {const state = createAnalystState({ hasAdvancedPermissions: false });- expect(canAccessMonitorDiagnostics(state)).toBe(false);+ expect(canAccessDependencyDiagnostics(state)).toBe(false);});- it("returns false for a monitoring-only user (no diagnostics access)", () => {+ it("returns false for a non-admin with only the monitoring application permission", () => {const state = createMockState({currentUser: createMockUser({is_superuser: false,@@ -139,7 +140,65 @@ describe("canAccessMonitorDiagnostics", () => {}),});- expect(canAccessMonitorDiagnostics(state)).toBe(false);+ expect(canAccessDependencyDiagnostics(state)).toBe(false);+ });+});++describe("canAccessContentDiagnostics", () => {+ beforeEach(() => {+ jest.clearAllMocks();+ isWithinIframe.mockReturnValue(false);+ });++ it("returns false when in embedding iframe", () => {+ isWithinIframe.mockReturnValue(true);+ const state = createMockState({+ currentUser: createMockUser({ is_superuser: true }),+ });++ expect(canAccessContentDiagnostics(state)).toBe(false);+ });++ it("returns true when user is admin", () => {+ const state = createMockState({+ currentUser: createMockUser({ is_superuser: true }),+ });++ expect(canAccessContentDiagnostics(state)).toBe(true);+ });++ it("returns true when user is entitled analyst", () => {+ expect(canAccessContentDiagnostics(createAnalystState())).toBe(true);+ });++ it("returns false for an analyst whose plan lost the feature", () => {+ const state = createAnalystState({ hasAdvancedPermissions: false });++ expect(canAccessContentDiagnostics(state)).toBe(false);+ });++ it("returns true for a non-admin with the monitoring application permission", () => {+ const state = createMockState({+ currentUser: createMockUser({+ is_superuser: false,+ is_data_analyst: false,+ permissions: { can_access_monitoring: true },+ }),+ });++ expect(canAccessContentDiagnostics(state)).toBe(true);+ });++ it("returns false without admin, analyst or the monitoring permission", () => {+ const state = createMockState({+ currentUser: createMockUser({+ is_superuser: false,+ is_data_analyst: false,+ permissions: { can_access_monitoring: false },+ }),+ });++ expect(canAccessContentDiagnostics(state)).toBe(false);});});diff --git a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.tsx b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.tsxindex 6a12d67c674..522092af14e 100644--- a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.tsx+++ b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.tsx@@ -10,7 +10,8 @@ import {canAccessAiAuditing,canAccessAlertsManagement,canAccessApiKeyUsage,- canAccessMonitorDiagnostics,+ canAccessContentDiagnostics,+ canAccessDependencyDiagnostics,canAccessMonitoringTools,canAccessSessionManagement,} from "metabase/common/monitor/selectors";@@ -36,6 +37,10 @@ function getActiveSection(pathname: string): ActiveSection {P.string.startsWith(Urls.dependencyDiagnostics()),() => "diagnostics",)+ .with(+ P.string.startsWith(Urls.contentDiagnostics()),+ () => "content-diagnostics",+ ).with(P.string.startsWith(Urls.monitorErroringQuestions()),() => "erroring-questions",@@ -85,10 +90,18 @@ export function MonitorLayout() {const { pathname } = useLocation();const hasDependenciesFeature = useHasTokenFeature("dependencies");+ const hasContentDiagnosticsFeature = useHasTokenFeature(+ "content_diagnostics",+ );const hasAuditAppFeature = useHasTokenFeature("audit_app");const hasAiControlsFeature = useHasTokenFeature("ai_controls");const hasSessionManagementFeature = useHasTokenFeature("session-management");- const canAccessDiagnostics = useSelector(canAccessMonitorDiagnostics);+ const canAccessDependencyDiagnosticsPage = useSelector(+ canAccessDependencyDiagnostics,+ );+ const canAccessContentDiagnosticsPage = useSelector(+ canAccessContentDiagnostics,+ );const canAccessTools = useSelector(canAccessMonitoringTools);const canAccessAlerts = useSelector(canAccessAlertsManagement);const canAccessSessions = useSelector(canAccessSessionManagement);@@ -98,7 +111,10 @@ export function MonitorLayout() {const activeSection = getActiveSection(pathname);const hasContentManagement =- canAccessDiagnostics || canAccessTools || canAccessAlerts;+ canAccessDependencyDiagnosticsPage ||+ canAccessContentDiagnosticsPage ||+ canAccessTools ||+ canAccessAlerts;const hasLogsAndActivity = canAccessTools;const upperNav = (@@ -109,7 +125,7 @@ export function MonitorLayout() {showLabel={isNavbarOpened}mb="lg">- {canAccessDiagnostics && (+ {canAccessDependencyDiagnosticsPage && (<AreaTablabel={t`Dependency diagnostics`}icon="search_check"@@ -120,6 +136,17 @@ export function MonitorLayout() {onClick={() => trackMonitorSectionClicked("diagnostics")}/>)}+ {canAccessContentDiagnosticsPage && (+ <AreaTab+ label={t`Content diagnostics`}+ icon="document"+ to={Urls.contentDiagnostics()}+ isSelected={activeSection === "content-diagnostics"}+ showLabel={isNavbarOpened}+ isGated={!hasContentDiagnosticsFeature}+ onClick={() => trackMonitorSectionClicked("content-diagnostics")}+ />+ )}{canAccessTools && (<AreaTablabel={t`Erroring questions`}diff --git a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsxindex 129e72d3770..6edc3c5798c 100644--- a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx+++ b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx@@ -146,6 +146,7 @@ describe("MonitorLayout", () => {const expectedTabs: [string, string][] = [["Dependency diagnostics", Urls.dependencyDiagnostics()],+ ["Content diagnostics", Urls.contentDiagnostics()],["Erroring questions", Urls.monitorErroringQuestions()],["Alerts management", Urls.monitorNotifications()],["Background tasks", Urls.monitorTasks()],@@ -167,6 +168,11 @@ describe("MonitorLayout", () => {route: Urls.dependencyDiagnostics(),section: "diagnostics",},+ {+ label: "Content diagnostics",+ route: Urls.contentDiagnostics(),+ section: "content-diagnostics",+ },{label: "Erroring questions",route: Urls.monitorErroringQuestions(),@@ -317,9 +323,9 @@ describe("MonitorLayout", () => {screen.queryByRole("heading", { name: LOGS_AND_ACTIVITY_GROUP }),).not.toBeInTheDocument();- expect(- screen.getByRole("link", { name: "Dependency diagnostics" }),- ).toBeInTheDocument();+ ["Dependency diagnostics", "Content diagnostics"].forEach((name) => {+ expect(screen.getByRole("link", { name })).toBeInTheDocument();+ });["Background tasks","Scheduled jobs",@@ -345,10 +351,8 @@ describe("MonitorLayout", () => {expect(screen.getByTestId("monitor-nav")).toBeInTheDocument();});- expect(- screen.queryByRole("link", { name: "Dependency diagnostics" }),- ).not.toBeInTheDocument();[+ "Content diagnostics","Background tasks","Scheduled jobs","Application logs",@@ -356,9 +360,9 @@ describe("MonitorLayout", () => {].forEach((name) => {expect(screen.getByRole("link", { name })).toBeInTheDocument();});- expect(- screen.queryByRole("link", { name: "Alerts management" }),- ).not.toBeInTheDocument();+ ["Alerts management", "Dependency diagnostics"].forEach((name) => {+ expect(screen.queryByRole("link", { name })).not.toBeInTheDocument();+ });// the page loads GET /api/api-key, which is superuser-onlyexpect(screen.queryByRole("link", { name: "API key usage" }),@@ -473,7 +477,13 @@ describe("MonitorLayout", () => {within(screen.getByRole("link", { name })).queryByTestId("upsell-gem");it("gates only Erroring questions when audit_app is unavailable", async () => {- setup({ tokenFeatures: { dependencies: true, audit_app: false } });+ setup({+ tokenFeatures: {+ dependencies: true,+ content_diagnostics: true,+ audit_app: false,+ },+ });await waitFor(() => {expect(screen.getByTestId("monitor-nav")).toBeInTheDocument();@@ -481,27 +491,60 @@ describe("MonitorLayout", () => {expect(getTabGem("Erroring questions")).toBeInTheDocument();expect(getTabGem("Dependency diagnostics")).not.toBeInTheDocument();+ expect(getTabGem("Content diagnostics")).not.toBeInTheDocument();});it("gates only Dependency diagnostics when dependencies is unavailable", async () => {- setup({ tokenFeatures: { dependencies: false, audit_app: true } });+ setup({+ tokenFeatures: {+ dependencies: false,+ content_diagnostics: true,+ audit_app: true,+ },+ });await waitFor(() => {expect(screen.getByTestId("monitor-nav")).toBeInTheDocument();});expect(getTabGem("Dependency diagnostics")).toBeInTheDocument();+ expect(getTabGem("Content diagnostics")).not.toBeInTheDocument();expect(getTabGem("Erroring questions")).not.toBeInTheDocument();});- it("gates neither when both features are available", async () => {- setup({ tokenFeatures: { dependencies: true, audit_app: true } });+ it("gates only Content diagnostics when content_diagnostics is unavailable", async () => {+ setup({+ tokenFeatures: {+ dependencies: true,+ content_diagnostics: false,+ audit_app: true,+ },+ });++ await waitFor(() => {+ expect(screen.getByTestId("monitor-nav")).toBeInTheDocument();+ });++ expect(getTabGem("Dependency diagnostics")).not.toBeInTheDocument();+ expect(getTabGem("Content diagnostics")).toBeInTheDocument();+ expect(getTabGem("Erroring questions")).not.toBeInTheDocument();+ });++ it("gates none of the token-feature tabs when all features are available", async () => {+ setup({+ tokenFeatures: {+ dependencies: true,+ content_diagnostics: true,+ audit_app: true,+ },+ });await waitFor(() => {expect(screen.getByTestId("monitor-nav")).toBeInTheDocument();});expect(getTabGem("Dependency diagnostics")).not.toBeInTheDocument();+ expect(getTabGem("Content diagnostics")).not.toBeInTheDocument();expect(getTabGem("Erroring questions")).not.toBeInTheDocument();});diff --git a/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/ContentDiagnosticsSectionLayout.tsx b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/ContentDiagnosticsSectionLayout.tsxnew file mode 100644index 00000000000..0acb7b1fbe1--- /dev/null+++ b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/ContentDiagnosticsSectionLayout.tsx@@ -0,0 +1,15 @@+import { t } from "ttag";++import { usePageTitle } from "metabase/hooks/use-page-title";+import { Outlet } from "metabase/router";+import { Stack } from "metabase/ui";++export function ContentDiagnosticsSectionLayout() {+ usePageTitle(t`Content diagnostics`);++ return (+ <Stack h="100%" gap={0} bg="background_page-secondary">+ <Outlet />+ </Stack>+ );+}diff --git a/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/index.ts b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/index.tsnew file mode 100644index 00000000000..ee188721d87--- /dev/null+++ b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout/index.ts@@ -0,0 +1 @@+export * from "./ContentDiagnosticsSectionLayout";diff --git a/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsUpsellPage.tsx b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsUpsellPage.tsxnew file mode 100644index 00000000000..75cf91b9042--- /dev/null+++ b/frontend/src/metabase/monitor/content-diagnostics/ContentDiagnosticsUpsellPage.tsx@@ -0,0 +1,19 @@+import { t } from "ttag";++import { usePageTitle } from "metabase/hooks/use-page-title";+import { BaseUpsellPage } from "metabase/monitor/upsells";++export function ContentDiagnosticsUpsellPage() {+ usePageTitle(t`Content diagnostics`);++ return (+ <BaseUpsellPage+ campaign="monitor-content-diagnostics"+ location="monitor-content-diagnostics-page"+ header={t`Content diagnostics`}+ title={t`Find and clean up stale content without hunting it down`}+ description={t`Surface questions and dashboards that have gone stale so you can keep your instance clean and tidy.`}+ variant="image-card"+ />+ );+}diff --git a/frontend/src/metabase/monitor/route-guards.tsx b/frontend/src/metabase/monitor/route-guards.tsxindex a25f30dff9b..6aefda36adf 100644--- a/frontend/src/metabase/monitor/route-guards.tsx+++ b/frontend/src/metabase/monitor/route-guards.tsx@@ -2,8 +2,9 @@ import {canAccessAiAuditing,canAccessAlertsManagement,canAccessApiKeyUsage,+ canAccessContentDiagnostics,+ canAccessDependencyDiagnostics,canAccessMonitor,- canAccessMonitorDiagnostics,canAccessMonitoringTools,canAccessSessionManagement,} from "metabase/common/monitor/selectors";@@ -19,8 +20,13 @@ const UserCanAccessMonitor = createRedirectGuard("/unauthorized",);-const UserCanAccessMonitorDiagnostics = createRedirectGuard(- (state) => canAccessMonitorDiagnostics(state),+const UserCanAccessDependencyDiagnostics = createRedirectGuard(+ (state) => canAccessDependencyDiagnostics(state),+ "/unauthorized",+);++const UserCanAccessContentDiagnostics = createRedirectGuard(+ (state) => canAccessContentDiagnostics(state),"/unauthorized",);@@ -59,10 +65,16 @@ export const CanAccessMonitor = () => (</MetabaseIsSetup>);-export const CanAccessMonitorDiagnostics = () => (- <UserCanAccessMonitorDiagnostics>+export const CanAccessDependencyDiagnostics = () => (+ <UserCanAccessDependencyDiagnostics>+ <Outlet />+ </UserCanAccessDependencyDiagnostics>+);++export const CanAccessContentDiagnostics = () => (+ <UserCanAccessContentDiagnostics><Outlet />- </UserCanAccessMonitorDiagnostics>+ </UserCanAccessContentDiagnostics>);export const CanAccessMonitoringTools = () => (diff --git a/frontend/src/metabase/monitor/route-guards.unit.spec.tsx b/frontend/src/metabase/monitor/route-guards.unit.spec.tsxindex 543bda189d9..65d66315884 100644--- a/frontend/src/metabase/monitor/route-guards.unit.spec.tsx+++ b/frontend/src/metabase/monitor/route-guards.unit.spec.tsx@@ -12,6 +12,8 @@ import {CanAccessAiAuditing,CanAccessAlertsManagement,CanAccessApiKeyUsage,+ CanAccessContentDiagnostics,+ CanAccessDependencyDiagnostics,CanAccessMonitor,} from "./route-guards";@@ -99,6 +101,137 @@ describe("monitor route-guards", () => {});});+ describe("the diagnostics guards", () => {+ const DEPENDENCY_ROUTE = "/monitor/dependency-diagnostics";+ const CONTENT_ROUTE = "/monitor/content-diagnostics";++ const DEPENDENCY_PAGE = "dependency diagnostics page";+ const CONTENT_PAGE = "content diagnostics page";++ const BOTH_SECTIONS = [+ [DEPENDENCY_ROUTE, DEPENDENCY_PAGE],+ [CONTENT_ROUTE, CONTENT_PAGE],+ ];++ interface SetupOpts {+ currentUser?: ReturnType<typeof createMockUser>;+ initialRoute: string;+ tokenFeatures?: Partial<TokenFeatures>;+ }++ const setup = ({ currentUser, initialRoute, tokenFeatures }: SetupOpts) => {+ return renderWithProviders(+ <>+ <Route element={<CanAccessDependencyDiagnostics />}>+ <Route+ path={DEPENDENCY_ROUTE}+ element={<div>{DEPENDENCY_PAGE}</div>}+ />+ </Route>+ <Route element={<CanAccessContentDiagnostics />}>+ <Route path={CONTENT_ROUTE} element={<div>{CONTENT_PAGE}</div>} />+ </Route>+ <Route path="/unauthorized" element={<div>unauthorized</div>} />+ </>,+ {+ storeInitialState: createMockState({+ currentUser,+ settings: createMockSettingsState({+ "has-user-setup": true,+ "token-features": createMockTokenFeatures(tokenFeatures),+ }),+ }),+ withRouter: true,+ initialRoute,+ },+ );+ };++ const monitoringOnlyUser = () =>+ createMockUser({+ is_superuser: false,+ is_data_analyst: false,+ permissions: { can_access_monitoring: true },+ });++ it.each(BOTH_SECTIONS)(+ "renders %s for an analyst",+ async (initialRoute, pageText) => {+ setup({+ currentUser: createMockUser({+ is_superuser: false,+ is_data_analyst: true,+ }),+ initialRoute,+ tokenFeatures: { advanced_permissions: true },+ });++ expect(await screen.findByText(pageText)).toBeInTheDocument();+ },+ );++ it.each(BOTH_SECTIONS)(+ "redirects an analyst whose plan lost the feature away from %s",+ async (initialRoute) => {+ const { router } = setup({+ currentUser: createMockUser({+ is_superuser: false,+ is_data_analyst: true,+ }),+ initialRoute,+ tokenFeatures: { advanced_permissions: false },+ });++ await waitFor(() => {+ expect(router?.location.pathname).toBe("/unauthorized");+ });+ },+ );++ it("renders content diagnostics for a monitoring-only user", async () => {+ setup({+ currentUser: monitoringOnlyUser(),+ initialRoute: CONTENT_ROUTE,+ });++ expect(await screen.findByText(CONTENT_PAGE)).toBeInTheDocument();+ });++ it("redirects a monitoring-only user away from dependency diagnostics", async () => {+ const { router } = setup({+ currentUser: monitoringOnlyUser(),+ initialRoute: DEPENDENCY_ROUTE,+ });++ await waitFor(() => {+ expect(router?.location.pathname).toBe("/unauthorized");+ });++ expect(router?.location.search).toBe("");+ expect(screen.queryByText(DEPENDENCY_PAGE)).not.toBeInTheDocument();+ });++ it.each(BOTH_SECTIONS)(+ "redirects a user with none of the three away from %s",+ async (initialRoute, pageText) => {+ const { router } = setup({+ currentUser: createMockUser({+ is_superuser: false,+ is_data_analyst: false,+ permissions: { can_access_monitoring: false },+ }),+ initialRoute,+ });++ await waitFor(() => {+ expect(router?.location.pathname).toBe("/unauthorized");+ });++ expect(screen.queryByText(pageText)).not.toBeInTheDocument();+ },+ );+ });+describe("CanAccessAlertsManagement", () => {interface SetupOpts {currentUser?: ReturnType<typeof createMockUser>;diff --git a/frontend/src/metabase/monitor/routes.tsx b/frontend/src/metabase/monitor/routes.tsxindex 265d7eb115c..121086ee0ae 100644--- a/frontend/src/metabase/monitor/routes.tsx+++ b/frontend/src/metabase/monitor/routes.tsx@@ -6,7 +6,7 @@ import {lazyModalRouteElement,modalRoute,} from "metabase/common/components/ModalRoute";-import { canAccessMonitorDiagnostics } from "metabase/common/monitor/selectors";+import { canAccessDependencyDiagnostics } from "metabase/common/monitor/selectors";// From the file rather than the barrel beside it: the barrel also re-exports// the page this file loads lazily, so importing the modal through it would hold// the page in the initial bundle.@@ -30,8 +30,9 @@ import {CanAccessAiAuditing,CanAccessAlertsManagement,CanAccessApiKeyUsage,+ CanAccessContentDiagnostics,+ CanAccessDependencyDiagnostics,CanAccessMonitor,- CanAccessMonitorDiagnostics,CanAccessMonitoringTools,CanAccessSessionManagement,} from "./route-guards";@@ -68,6 +69,20 @@ const dependencyDiagnosticsUpsellPage = () =>Component: DependencyDiagnosticsUpsellPage,}));+const contentDiagnosticsSectionLayout = () =>+ import(+ /* webpackChunkName: "monitor" */ "metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout"+ ).then(({ ContentDiagnosticsSectionLayout }) => ({+ Component: ContentDiagnosticsSectionLayout,+ }));++const contentDiagnosticsUpsellPage = () =>+ import(+ /* webpackChunkName: "monitor" */ "metabase/monitor/content-diagnostics/ContentDiagnosticsUpsellPage"+ ).then(({ ContentDiagnosticsUpsellPage }) => ({+ Component: ContentDiagnosticsUpsellPage,+ }));+const apiKeyUsageUpsellPage = () =>import(/* webpackChunkName: "monitor" */ "metabase/monitor/api-key-usage/ApiKeyUsageUpsellPage"@@ -119,7 +134,7 @@ export function getMonitorRoutes() {<Route element={<CanAccessMonitor />}><Route path="monitor" lazy={monitorLayout}><Route index element={<MonitorIndexRedirect />} />- <Route element={<CanAccessMonitorDiagnostics />}>+ <Route element={<CanAccessDependencyDiagnostics />}>{PLUGIN_MONITOR.isDependencyDiagnosticsEnabled ? (<Routepath="dependency-diagnostics"@@ -135,6 +150,22 @@ export function getMonitorRoutes() {)}</Route>+ <Route element={<CanAccessContentDiagnostics />}>+ {PLUGIN_MONITOR.isContentDiagnosticsEnabled ? (+ <Route+ path="content-diagnostics"+ lazy={contentDiagnosticsSectionLayout}+ >+ {PLUGIN_MONITOR.getContentDiagnosticsRoutes()}+ </Route>+ ) : (+ <Route path="content-diagnostics">+ <Route index lazy={contentDiagnosticsUpsellPage} />+ <Route path="*" lazy={contentDiagnosticsUpsellPage} />+ </Route>+ )}+ </Route>+<Route element={<CanAccessMonitoringTools />}><Route path="tasks">{getTasksRoutes()}</Route><Route path="jobs" lazy={jobInfoApp}>@@ -201,10 +232,10 @@ export function getMonitorRoutes() {);}-// Diagnostics for analysts/admins; otherwise the Tools pages for users who only-// hold the monitoring application permission.+// Content diagnostics requires an additional feature token, so Tasks remains+// the accessible fallback for users who lack dependency diagnostics access.function getMonitorIndexPath(state: State) {- return canAccessMonitorDiagnostics(state)+ return canAccessDependencyDiagnostics(state)? Urls.dependencyDiagnostics(): Urls.monitorTasks();}diff --git a/frontend/src/metabase/monitor/routes.unit.spec.tsx b/frontend/src/metabase/monitor/routes.unit.spec.tsxindex 6072be3bda7..229e92a3ded 100644--- a/frontend/src/metabase/monitor/routes.unit.spec.tsx+++ b/frontend/src/metabase/monitor/routes.unit.spec.tsx@@ -1,15 +1,506 @@+import { setupEnterpriseOnlyPlugin } from "__support__/enterprise";import { lazyLoaders } from "__support__/lazy-routes";+import { createMockSettingsState, createMockState } from "__support__/state";+import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { PLUGIN_AUDIT, reinitialize } from "metabase/plugins";+import { Route } from "metabase/router";+import * as Urls from "metabase/urls";+import type { TokenFeatures } from "metabase-types/api";+import {+ createMockTokenFeatures,+ createMockUser,+} from "metabase-types/api/mocks";-import { getMonitorRoutes } from "./routes";+import { getMonitorRedirects, getMonitorRoutes } from "./routes";++type MonitorGuard =+ | "CanAccessMonitor"+ | "CanAccessDependencyDiagnostics"+ | "CanAccessContentDiagnostics"+ | "CanAccessMonitoringTools"+ | "CanAccessAlertsManagement"+ | "CanAccessAiAuditing";++/**+ * These specs assert route-tree structure, not access policy, so the guards are+ * stubbed to allow by default. Adding a guard here makes it deny instead, which+ * is how a single section gets blocked without touching permissions state.+ */+const mockDeniedGuards = new Set<MonitorGuard>();++jest.mock("./route-guards", () => {+ const { Outlet } = jest.requireActual("metabase/router");+ const stubGuard = (name: MonitorGuard) => {+ const Guard = () =>+ mockDeniedGuards.has(name) ? (+ <div data-testid="unauthorized-marker">{"Unauthorized"}</div>+ ) : (+ <Outlet />+ );+ return Guard;+ };++ return {+ CanAccessMonitor: stubGuard("CanAccessMonitor"),+ CanAccessDependencyDiagnostics: stubGuard("CanAccessDependencyDiagnostics"),+ CanAccessContentDiagnostics: stubGuard("CanAccessContentDiagnostics"),+ CanAccessMonitoringTools: stubGuard("CanAccessMonitoringTools"),+ CanAccessAlertsManagement: stubGuard("CanAccessAlertsManagement"),+ CanAccessAiAuditing: stubGuard("CanAccessAiAuditing"),+ };+});++jest.mock("metabase-enterprise/settings", () => ({+ hasPremiumFeature: jest.fn().mockReturnValue(true),+}));++jest.mock("./components/MonitorLayout", () => {+ const { Outlet } = jest.requireActual("metabase/router");+ return { MonitorLayout: () => <Outlet /> };+});+jest.mock(+ "metabase/monitor/dependency-diagnostics/DependencyDiagnosticsSectionLayout",+ () => {+ const { Outlet } = jest.requireActual("metabase/router");+ return {+ DependencyDiagnosticsSectionLayout: () => (+ <div data-testid="diagnostics-section">+ <Outlet />+ </div>+ ),+ };+ },+);+jest.mock(+ "metabase/monitor/content-diagnostics/ContentDiagnosticsSectionLayout",+ () => {+ const { Outlet } = jest.requireActual("metabase/router");+ return {+ ContentDiagnosticsSectionLayout: () => (+ <div data-testid="content-diagnostics-section">+ <Outlet />+ </div>+ ),+ };+ },+);++jest.mock("metabase-enterprise/monitor/dependency-diagnostics/pages", () => ({+ BrokenDependencyDiagnosticsPage: () => <div>{"Broken page"}</div>,+ UnreferencedDependencyDiagnosticsPage: () => <div>{"Unreferenced page"}</div>,+}));+jest.mock("metabase-enterprise/monitor/content-diagnostics/pages", () => ({+ StaleContentPage: () => <div>{"Stale content page"}</div>,+}));++jest.mock("metabase/monitor/tools/components/Logs", () => {+ const { Outlet } = jest.requireActual("metabase/router");+ return {+ Logs: () => (+ <div data-testid="logs-page">+ {"Logs"}+ <Outlet />+ </div>+ ),+ };+});+jest.mock("metabase/monitor/tools/components/JobInfoApp", () => ({+ JobInfoApp: () => <div data-testid="jobs-page">{"Jobs"}</div>,+}));+jest.mock(+ "metabase/monitor/tools/components/ModelPersistenceLogJobs/ModelPersistenceLogJobs",+ () => ({+ ModelPersistenceLogPage: () => (+ <div data-testid="model-persistence-log-page">+ {"Model persistence log"}+ </div>+ ),+ }),+);+jest.mock(+ "metabase/monitor/tools/components/ModelPersistenceLogJobs/ModelPersistenceLogJobModal",+ () => ({ ModelPersistenceLogJobModal: () => null }),+);+jest.mock("metabase/monitor/tools/components/LogLevelsModal", () => ({+ LogLevelsModal: () => null,+}));+jest.mock("metabase/monitor/tools/components/MonitorUpsell", () => ({+ MonitorUpsell: () => (+ <div data-testid="errors-upsell">{"Erroring questions"}</div>+ ),+}));++jest.mock("metabase/monitor/tools/components/TaskListPage", () => ({+ TaskListPage: () => <div data-testid="task-list-page">{"Task list"}</div>,+}));+jest.mock("metabase/monitor/tools/components/TaskDetailsPage", () => ({+ TaskDetailsPage: () => (+ <div data-testid="task-details-page">{"Task details"}</div>+ ),+}));+jest.mock("metabase/monitor/tools/components/TaskRunsPage", () => ({+ TaskRunsPage: () => <div data-testid="task-runs-page">{"Task runs"}</div>,+}));+jest.mock("metabase/monitor/tools/components/TaskRunDetailsPage", () => ({+ TaskRunDetailsPage: () => (+ <div data-testid="task-run-details-page">{"Task run details"}</div>+ ),+}));+jest.mock(+ "metabase/monitor/tools/notifications/NotificationsAdminPage",+ () => ({+ NotificationsAdminPage: () => (+ <div data-testid="notifications-page">{"Notifications"}</div>+ ),+ }),+);++const UPSELL_TITLE =+ "Find and fix broken dependencies without hunting them down";+const CONTENT_DIAGNOSTICS_UPSELL_TITLE =+ "Find and clean up stale content without hunting it down";++type SetupOpts = {+ initialRoute: string;+ user?: ReturnType<typeof createMockUser>;+ /** Guards to make deny access, so a section can be blocked in isolation. */+ deny?: MonitorGuard[];+ tokenFeatures?: Partial<TokenFeatures>;+};++const setup = ({+ initialRoute,+ user = createMockUser({ is_superuser: true }),+ deny = [],+ tokenFeatures,+}: SetupOpts) => {+ deny.forEach((guard) => mockDeniedGuards.add(guard));++ return renderWithProviders(+ <Route path="/">+ {getMonitorRedirects()}+ {getMonitorRoutes()}+ </Route>,+ {+ withRouter: true,+ initialRoute,+ storeInitialState: createMockState({+ currentUser: user,+ settings: createMockSettingsState({+ "token-features": createMockTokenFeatures(tokenFeatures),+ }),+ }),+ },+ );+};++const enableAiAuditingRoutes = () => {+ PLUGIN_AUDIT.isAiAuditingEnabled = true;+ PLUGIN_AUDIT.getAiAuditingRoutes = () => (+ <Route+ path="usage"+ element={<div data-testid="ai-auditing-page">AI Auditing</div>}+ />+ );+};describe("monitor routes", () => {- it("resolves every page", async () => {+ afterEach(() => {+ reinitialize();+ mockDeniedGuards.clear();+ });++ // Route factories name their page in an `import()`, so nothing type-checks the+ // path or the export. Resolving every lazy loader is the cheap guard against a+ // typo that would otherwise surface as a blank page. Runs against default (OSS)+ // plugin state, so both diagnostics sections are on their upsell branch.+ it("resolves every lazy page", async () => {const loaders = lazyLoaders(getMonitorRoutes());- expect(loaders).toHaveLength(17);+ expect(loaders).toHaveLength(19);for (const load of loaders) {expect((await load()).Component).toBeDefined();}});++ describe("getMonitorRoutes", () => {+ describe("OSS (Dependency Diagnostics disabled)", () => {+ it("renders the upsell at /monitor/dependency-diagnostics", async () => {+ setup({ initialRoute: "/monitor/dependency-diagnostics" });++ expect(await screen.findByText(UPSELL_TITLE)).toBeInTheDocument();+ expect(+ screen.queryByTestId("diagnostics-section"),+ ).not.toBeInTheDocument();+ });++ it("renders the upsell for child paths (e.g. /broken)", async () => {+ setup({ initialRoute: "/monitor/dependency-diagnostics/broken" });++ expect(await screen.findByText(UPSELL_TITLE)).toBeInTheDocument();+ expect(screen.queryByText("Broken page")).not.toBeInTheDocument();+ });+ });++ describe("OSS (Content Diagnostics disabled)", () => {+ it("renders the upsell for content diagnostics child paths", async () => {+ setup({ initialRoute: "/monitor/content-diagnostics/stale" });++ expect(+ await screen.findByText(CONTENT_DIAGNOSTICS_UPSELL_TITLE),+ ).toBeInTheDocument();+ expect(+ screen.queryByTestId("content-diagnostics-section"),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByText("Stale content page"),+ ).not.toBeInTheDocument();+ });+ });++ describe("EE (Dependency Diagnostics enabled)", () => {+ it("renders the diagnostics section and child routes instead of the upsell", async () => {+ setupEnterpriseOnlyPlugin("monitor_dependency_diagnostics");++ setup({ initialRoute: "/monitor/dependency-diagnostics/broken" });++ expect(+ await screen.findByTestId("diagnostics-section"),+ ).toBeInTheDocument();+ expect(screen.getByText("Broken page")).toBeInTheDocument();+ expect(screen.queryByText(UPSELL_TITLE)).not.toBeInTheDocument();+ });++ it("redirects the diagnostics index to the broken route", async () => {+ setupEnterpriseOnlyPlugin("monitor_dependency_diagnostics");++ setup({ initialRoute: "/monitor/dependency-diagnostics" });++ expect(await screen.findByText("Broken page")).toBeInTheDocument();+ });+ });++ describe("EE (Content Diagnostics enabled)", () => {+ it("renders the content diagnostics section and child routes instead of the upsell", async () => {+ setupEnterpriseOnlyPlugin("monitor_content_diagnostics");++ setup({ initialRoute: "/monitor/content-diagnostics/stale" });++ expect(+ await screen.findByTestId("content-diagnostics-section"),+ ).toBeInTheDocument();+ expect(screen.getByText("Stale content page")).toBeInTheDocument();+ expect(+ screen.queryByText(CONTENT_DIAGNOSTICS_UPSELL_TITLE),+ ).not.toBeInTheDocument();+ });++ it("redirects the content diagnostics index to the stale route", async () => {+ setupEnterpriseOnlyPlugin("monitor_content_diagnostics");++ setup({ initialRoute: "/monitor/content-diagnostics" });++ expect(+ await screen.findByText("Stale content page"),+ ).toBeInTheDocument();+ });+ });++ describe("index redirect (/monitor)", () => {+ it("sends analysts to dependency diagnostics", async () => {+ const { router } = setup({+ initialRoute: "/monitor",+ user: createMockUser({+ is_superuser: false,+ is_data_analyst: true,+ }),+ tokenFeatures: { advanced_permissions: true },+ });++ await waitFor(() =>+ expect(router?.location.pathname).toBe(+ "/monitor/dependency-diagnostics",+ ),+ );+ });++ it("sends monitoring-only users to background tasks", async () => {+ const { router } = setup({+ initialRoute: "/monitor",+ user: createMockUser({+ is_superuser: false,+ is_data_analyst: false,+ permissions: { can_access_monitoring: true },+ }),+ });++ await waitFor(() =>+ expect(router?.location.pathname).toBe(Urls.monitorTasksList()),+ );+ });+ });++ describe("unknown routes", () => {+ it("renders the NotFound page inside the Monitor layout", async () => {+ setup({ initialRoute: "/monitor/does-not-exist" });++ expect(await screen.findByLabelText("error page")).toBeInTheDocument();+ });++ it("blocks section routes when the section guard denies", async () => {+ setup({+ initialRoute: "/monitor/logs",+ deny: ["CanAccessMonitoringTools"],+ });++ expect(+ await screen.findByTestId("unauthorized-marker"),+ ).toBeInTheDocument();+ expect(screen.queryByTestId("logs-page")).not.toBeInTheDocument();+ });++ it("blocks the notifications route when its own guard denies, independent of the Tools guard", async () => {+ setup({+ initialRoute: "/monitor/notifications",+ deny: ["CanAccessAlertsManagement"],+ });++ expect(+ await screen.findByTestId("unauthorized-marker"),+ ).toBeInTheDocument();+ expect(+ screen.queryByTestId("notifications-page"),+ ).not.toBeInTheDocument();+ });++ it("blocks the AI Auditing route when its own guard denies", async () => {+ enableAiAuditingRoutes();++ setup({+ initialRoute: Urls.monitorAiAuditingUsage(),+ deny: ["CanAccessAiAuditing"],+ });++ expect(+ await screen.findByTestId("unauthorized-marker"),+ ).toBeInTheDocument();+ expect(+ screen.queryByTestId("ai-auditing-page"),+ ).not.toBeInTheDocument();+ });++ it("renders NotFound for unknown paths even when every section guard denies (catch-all sits outside the guards)", async () => {+ setup({+ initialRoute: "/monitor/does-not-exist",+ deny: [+ "CanAccessDependencyDiagnostics",+ "CanAccessContentDiagnostics",+ "CanAccessMonitoringTools",+ ],+ });++ expect(await screen.findByLabelText("error page")).toBeInTheDocument();+ expect(+ screen.queryByTestId("unauthorized-marker"),+ ).not.toBeInTheDocument();+ });+ });+ });++ describe("Tools sections (migrated from /admin/tools)", () => {+ it.each([["/monitor/logs"], ["/monitor/logs/levels"]])(+ "renders the Logs section at %s",+ async (initialRoute) => {+ setup({ initialRoute });++ expect(await screen.findByTestId("logs-page")).toBeInTheDocument();+ },+ );++ it.each([["/monitor/jobs"], ["/monitor/jobs/sync"]])(+ "renders the Jobs section at %s",+ async (initialRoute) => {+ setup({ initialRoute });++ expect(await screen.findByTestId("jobs-page")).toBeInTheDocument();+ },+ );++ it.each([+ ["/monitor/model-persistence-log"],+ ["/monitor/model-persistence-log/9"],+ ])(+ "renders the Model persistence log section at %s",+ async (initialRoute) => {+ setup({ initialRoute });++ expect(+ await screen.findByTestId("model-persistence-log-page"),+ ).toBeInTheDocument();+ },+ );++ it("renders the Erroring questions upsell at /monitor/errors without the audit_app feature", async () => {+ setup({ initialRoute: "/monitor/errors" });++ expect(await screen.findByTestId("errors-upsell")).toBeInTheDocument();+ });+ });++ describe("Tasks section route branches", () => {+ it.each([+ ["/monitor/tasks", "task-list-page"],+ ["/monitor/tasks/list", "task-list-page"],+ ["/monitor/tasks/list/42", "task-details-page"],+ ["/monitor/tasks/runs", "task-runs-page"],+ ["/monitor/tasks/runs/7", "task-run-details-page"],+ ])("mounts %s", async (route, testId) => {+ setup({ initialRoute: route });++ expect(await screen.findByTestId(testId)).toBeInTheDocument();+ });+ });++ describe("Notifications section route branches", () => {+ it.each([["/monitor/notifications"], ["/monitor/notifications/13"]])(+ "mounts %s",+ async (route) => {+ setup({ initialRoute: route });++ expect(+ await screen.findByTestId("notifications-page"),+ ).toBeInTheDocument();+ },+ );+ });++ describe("getMonitorRedirects (legacy Admin Tools URLs)", () => {+ it.each([+ ["/admin/tools/tasks", "task-list-page"],+ ["/admin/tools/tasks/list", "task-list-page"],+ ["/admin/tools/tasks/list/42", "task-details-page"],+ ["/admin/tools/tasks/runs", "task-runs-page"],+ ["/admin/tools/tasks/runs/7", "task-run-details-page"],+ ["/admin/tools/jobs", "jobs-page"],+ ["/admin/tools/jobs/sync", "jobs-page"],+ ["/admin/tools/logs", "logs-page"],+ ["/admin/tools/logs/levels", "logs-page"],+ ["/admin/tools/errors", "errors-upsell"],+ ["/admin/tools/model-caching", "model-persistence-log-page"],+ ["/admin/tools/model-caching/9", "model-persistence-log-page"],+ ["/admin/tools/notifications", "notifications-page"],+ ["/admin/tools/notifications/13", "notifications-page"],+ ])("redirects %s into the Monitor space", async (route, testId) => {+ setup({ initialRoute: route });++ expect(await screen.findByTestId(testId)).toBeInTheDocument();+ });++ it("redirects the legacy /admin/tools index into the Monitor space", async () => {+ setup({ initialRoute: "/admin/tools" });++ expect(await screen.findByText(UPSELL_TITLE)).toBeInTheDocument();+ });+ });});diff --git a/frontend/src/metabase/plugins/oss/monitor.ts b/frontend/src/metabase/plugins/oss/monitor.tsindex afc9aa3904d..fe334739192 100644--- a/frontend/src/metabase/plugins/oss/monitor.ts+++ b/frontend/src/metabase/plugins/oss/monitor.ts@@ -3,6 +3,8 @@ import type { ComponentType, ReactNode } from "react";import { definePluginSlot } from "../slot";type MonitorPlugin = {+ isContentDiagnosticsEnabled: boolean;+ getContentDiagnosticsRoutes: () => ReactNode;isDependencyDiagnosticsEnabled: boolean;getDependencyDiagnosticsRoutes: () => ReactNode;isSessionManagementEnabled: boolean;@@ -12,6 +14,8 @@ type MonitorPlugin = {};const getDefaultPluginMonitor = (): MonitorPlugin => ({+ isContentDiagnosticsEnabled: false,+ getContentDiagnosticsRoutes: () => null,isDependencyDiagnosticsEnabled: false,getDependencyDiagnosticsRoutes: () => null,isSessionManagementEnabled: false,diff --git a/frontend/src/metabase/transforms/analytics.ts b/frontend/src/metabase/transforms/analytics.tsindex 8e9c75d7fde..6b0376c71ac 100644--- a/frontend/src/metabase/transforms/analytics.ts+++ b/frontend/src/metabase/transforms/analytics.ts@@ -11,6 +11,38 @@ import type {/** Lens ID concatenated with params */type LensKey = string;+export type TransformDeletedTriggeredFrom =+ | "transform_page"+ | "content_diagnostics";++export async function deleteTransformAndTrack({+ deleteTransform,+ transformId,+ triggeredFrom,+}: {+ deleteTransform: () => Promise<unknown>;+ transformId: TransformId;+ triggeredFrom: TransformDeletedTriggeredFrom;+}): Promise<void> {+ const startTime = performance.now();+ const track = (result: "success" | "failure") =>+ trackSimpleEvent({+ event: "transform_deleted",+ target_id: transformId,+ triggered_from: triggeredFrom,+ duration_ms: Math.trunc(performance.now() - startTime),+ result,+ });++ try {+ await deleteTransform();+ track("success");+ } catch (error) {+ track("failure");+ throw error;+ }+}+export function trackTransformTriggerManualRun({transformId,}: {diff --git a/frontend/src/metabase/transforms/components/TransformHeader/TransformMoreMenu/DeleteTransformModal/TransformDeleteModal.tsx b/frontend/src/metabase/transforms/components/TransformHeader/TransformMoreMenu/DeleteTransformModal/TransformDeleteModal.tsxindex 60556d85099..4e457c85a31 100644--- a/frontend/src/metabase/transforms/components/TransformHeader/TransformMoreMenu/DeleteTransformModal/TransformDeleteModal.tsx+++ b/frontend/src/metabase/transforms/components/TransformHeader/TransformMoreMenu/DeleteTransformModal/TransformDeleteModal.tsx@@ -13,6 +13,7 @@ import {FormProvider,FormSubmitButton,} from "metabase/forms";+import { deleteTransformAndTrack } from "metabase/transforms/analytics";import {Box,Button,@@ -82,10 +83,16 @@ function DeleteTransformForm({if (transform == null) {return;}- if (shouldDeleteTarget) {- await deleteTransformTarget(transform.id).unwrap();- }- await deleteTransform(transform.id).unwrap();+ await deleteTransformAndTrack({+ deleteTransform: async () => {+ if (shouldDeleteTarget) {+ await deleteTransformTarget(transform.id).unwrap();+ }+ await deleteTransform(transform.id).unwrap();+ },+ transformId: transform.id,+ triggeredFrom: "transform_page",+ });onDelete();};diff --git a/frontend/src/metabase/urls/content-diagnostics.ts b/frontend/src/metabase/urls/content-diagnostics.tsnew file mode 100644index 00000000000..9a84e70b44b--- /dev/null+++ b/frontend/src/metabase/urls/content-diagnostics.ts@@ -0,0 +1,26 @@+import type { ContentDiagnosticsImbalancedFindingType } from "metabase-types/api";++const CONTENT_DIAGNOSTICS_URL = `/monitor/content-diagnostics`;++export function contentDiagnostics() {+ return CONTENT_DIAGNOSTICS_URL;+}++export function staleContent() {+ return `${contentDiagnostics()}/stale`;+}++export function slowContent() {+ return `${contentDiagnostics()}/slow`;+}++export function duplicatedContent() {+ return `${contentDiagnostics()}/duplicated`;+}++// The problem type is the route segment and pins the `finding-types` value.+export function imbalancedContent(+ mode: ContentDiagnosticsImbalancedFindingType,+) {+ return `${contentDiagnostics()}/${mode}`;+}diff --git a/frontend/src/metabase/urls/index.ts b/frontend/src/metabase/urls/index.tsindex 989a2d345a5..c2727e2d955 100644--- a/frontend/src/metabase/urls/index.ts+++ b/frontend/src/metabase/urls/index.ts@@ -5,6 +5,7 @@ export * from "./bookmarks";export * from "./browse";export * from "./cards";export * from "./collections";+export * from "./content-diagnostics";export * from "./dashboards";export * from "./data-apps";export * from "./data-studio";diff --git a/frontend/test/__support__/enterprise-typed.ts b/frontend/test/__support__/enterprise-typed.tsindex 3f92c2347df..7682bff5165 100644--- a/frontend/test/__support__/enterprise-typed.ts+++ b/frontend/test/__support__/enterprise-typed.ts@@ -9,6 +9,8 @@ export const ENTERPRISE_PLUGIN_NAME_MAPPING = {sandboxes: "metabase-enterprise/sandboxes",clean_up: "metabase-enterprise/clean_up",tools: "metabase-enterprise/monitor/tools",+ monitor_content_diagnostics:+ "metabase-enterprise/monitor/content-diagnostics",monitor_dependency_diagnostics:"metabase-enterprise/monitor/dependency-diagnostics",advanced_permissions: "metabase-enterprise/advanced_permissions",diff --git a/frontend/test/__support__/server-mocks/card.ts b/frontend/test/__support__/server-mocks/card.tsindex 6c5e1b24c97..80f5d02f80c 100644--- a/frontend/test/__support__/server-mocks/card.ts+++ b/frontend/test/__support__/server-mocks/card.ts@@ -31,6 +31,10 @@ export function setupCardEndpoints(card: Card) {});}+export function setupUpdateCardEndpointWithError(cardId: CardId) {+ fetchMock.put(`path:/api/card/${cardId}`, { status: 500, body: {} });+}+export function setupCardByEntityIdEndpoints(card: Card) {fetchMock.get(`path:/api/card/${card.entity_id}`, card, {name: `card-entity-${card.entity_id}-get`,diff --git a/frontend/test/__support__/server-mocks/content-diagnostics.ts b/frontend/test/__support__/server-mocks/content-diagnostics.tsnew file mode 100644index 00000000000..b99084c3515--- /dev/null+++ b/frontend/test/__support__/server-mocks/content-diagnostics.ts@@ -0,0 +1,51 @@+import fetchMock, { type CallLog } from "fetch-mock";++import type {+ InvalidateFindingsResponse,+ ListDuplicatedFindingsResponse,+ ListImbalancedFindingsResponse,+ ListSlowFindingsResponse,+ ListStaleFindingsResponse,+} from "metabase-types/api";++type MockResponse<T> = T | ((call: CallLog) => T | Promise<T>);++export function setupInvalidateFindingsEndpoint(+ response: MockResponse<InvalidateFindingsResponse | Response>,+) {+ fetchMock.post("path:/api/ee/content-diagnostics/invalidate", response, {+ name: "invalidate-findings",+ });+}++export function setupInvalidateFindingsEndpointWithError(error: object) {+ fetchMock.post(+ "path:/api/ee/content-diagnostics/invalidate",+ { status: 500, body: error },+ { name: "invalidate-findings" },+ );+}++export function setupListStaleFindingsEndpoint(+ response: MockResponse<ListStaleFindingsResponse>,+) {+ fetchMock.get("path:/api/ee/content-diagnostics/stale", response);+}++export function setupListSlowFindingsEndpoint(+ response: MockResponse<ListSlowFindingsResponse>,+) {+ fetchMock.get("path:/api/ee/content-diagnostics/slow", response);+}++export function setupListDuplicatedFindingsEndpoint(+ response: MockResponse<ListDuplicatedFindingsResponse>,+) {+ fetchMock.get("path:/api/ee/content-diagnostics/duplicated", response);+}++export function setupListImbalancedFindingsEndpoint(+ response: MockResponse<ListImbalancedFindingsResponse>,+) {+ fetchMock.get("path:/api/ee/content-diagnostics/imbalanced", response);+}diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.tsindex 6a0ec45daf8..561d09083bf 100644--- a/frontend/test/__support__/server-mocks/index.ts+++ b/frontend/test/__support__/server-mocks/index.ts@@ -12,6 +12,7 @@ export * from "./cloud-proxy";export * from "./collection";export * from "./comment";export * from "./constants";+export * from "./content-diagnostics";export * from "./custom-viz-plugin";export * from "./dashboard";export * from "./data-studio";diff --git a/frontend/test/__support__/server-mocks/transform.ts b/frontend/test/__support__/server-mocks/transform.tsindex 3563d4e44f1..d7de2410524 100644--- a/frontend/test/__support__/server-mocks/transform.ts+++ b/frontend/test/__support__/server-mocks/transform.ts@@ -38,6 +38,10 @@ export function setupGetTransformEndpoint(transform: Transform) {fetchMock.get(`path:/api/transform/${transform.id}`, transform);}+export function setupDeleteTransformEndpoint(transformId: TransformId) {+ fetchMock.delete(`path:/api/transform/${transformId}`, 204);+}+export function setupListTransformTagsEndpoint(tags: TransformTag[]) {fetchMock.get(`path:/api/transform-tag`, tags);}diff --git a/resources/migrations/065/20260911_content_diagnostics.yaml b/resources/migrations/065/20260911_content_diagnostics.yamlnew file mode 100644index 00000000000..3cc44f98b8b--- /dev/null+++ b/resources/migrations/065/20260911_content_diagnostics.yaml@@ -0,0 +1,316 @@+## Content Diagnostics — detected findings snapshot (scan-snapshot, latest-wins).+## Persisted table: `content_diagnostics_finding`.+##+## `entity_name` and `entity_creator_name` carry no index: both sort through `lower()`, which a plain B-tree+## can't serve. `scan_id` has no index of its own: its only predicate is a `!=`, and it leads+## `idx_uniq_cd_finding`. `idx_cd_finding_detected` serves `last-detected-at` (an unfiltered+## `ORDER BY detected_at DESC LIMIT 1` on every list request) and the default sort.+##+## Every changeset is marked ran when its object already exists.++databaseChangeLog:+ - changeSet:+ id: v65.pux7zh+ author: yb+ comment: Content Diagnostics — content_diagnostics_finding (scan snapshot)+ preConditions:+ - onFail: MARK_RAN+ - not:+ - tableExists:+ tableName: content_diagnostics_finding+ changes:+ - createTable:+ tableName: content_diagnostics_finding+ remarks: One detected problem for one entity, from one scan run.+ columns:+ - column:+ name: id+ type: int+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: scan_id+ type: varchar(36)+ remarks: Groups all rows from one scan run (UUID). API serves the newest scan.+ constraints:+ nullable: false+ - column:+ name: entity_type+ type: varchar(32)+ remarks: Polymorphic — card/dashboard/document/collection/transform. No FK.+ constraints:+ nullable: false+ - column:+ name: entity_id+ type: int+ remarks: Polymorphic id of the flagged entity (no FK; paired with entity_type).+ constraints:+ nullable: false+ - column:+ name: finding_type+ type: varchar(32)+ remarks: stale/duplicated/empty/crowded/sparse/slow/trash-not-emptied+ constraints:+ nullable: false+ - column:+ name: scope_collection_id+ type: int+ remarks: collection the entity lived in at scan time (filter/sort); no FK+ - column:+ name: details+ type: ${text.type}+ remarks: JSON evidence — e.g. {threshold_days, last_active_at}+ - column:+ name: detected_at+ type: ${timestamp_type}+ defaultValueComputed: current_timestamp+ remarks: When this finding was detected (per-finding freshness signal).+ constraints:+ nullable: false+ - column:+ name: invalidated_at+ type: ${timestamp_type}+ remarks: stamped when invalidated/superseded; null = active (drives the served-set filter)+ - column:+ name: last_active_at+ type: ${timestamp_type}+ remarks: Entity's last activity at scan time (card last_used_at / dashboard last_viewed_at); null = never used/ran. Frozen verdict evidence; powers the threshold-days serve filter.+ - column:+ name: entity_name+ type: ${text.type}+ remarks: Entity's name at scan time; served as entity_display_name and used as the sort/search key. text because upstream names are unbounded.+ - column:+ name: entity_created_at+ type: ${timestamp_type}+ remarks: Entity's created_at (immutable). Denormalized for sort + display.+ - column:+ name: entity_creator_id+ type: int+ remarks: Entity's creator_id at scan time; no FK (polymorphic, mirrors entity_id). Sort key (groups by creator).+ - column:+ name: entity_creator_name+ type: ${text.type}+ remarks: Creator's common_name at scan time (resolved from creator_id). Sort key for created-by, served in details.creator. text because common_name can be an email, which is unbounded on Postgres.+ - column:+ name: duration_ms+ type: bigint+ remarks: Verdict magnitude for slow findings - median query ms (card) / last-run ms (transform) / slowest-child ms (dashboard/document roll-up); null on all other finding types (per-type named magnitude column). bigint, not int - a pathological run duration can exceed 2^31-1 ms.+ - column:+ name: content_count+ type: bigint+ remarks: Verdict magnitude for imbalanced (empty/sparse/crowded) findings - items (collection) / dashcards or tabs (dashboard) / cards (document) / rows (card, transform); 0 on every empty finding, null on all other finding types (per-type named magnitude column). bigint - house choice for magnitude columns.+ - column:+ name: duplicate_count+ type: bigint+ remarks: Verdict magnitude for duplicated findings - the number of other same-type entities sharing the normalized name (cluster size minus 1); null on all other finding types (per-type named magnitude column).+ - column:+ name: card_type+ type: varchar(32)+ remarks: report_card.type (question/model/metric) at scan time, card findings only; null on all other entity types. Denormalized filter key for the card sub-kind values of the entity-types param (mutable upstream - drift until the next scan, like entity_name).+ - column:+ name: entity_collection_name+ type: ${text.type}+ remarks: Scan-time name of the scope_collection_id collection (root rows store the root label); sort/display value for the collection column.+ - column:+ name: entity_kind+ type: varchar(32)+ remarks: Flat entity kind at scan time - card_type for cards, else entity_type; serves the entity-types filter and entity-type sort.++ - changeSet:+ id: v65.jmeicl+ author: yb+ comment: Content Diagnostics — unique finding per scan (dedup)+ preConditions:+ - onFail: MARK_RAN+ - not:+ # MySQL/MariaDB implement the constraint as an index of the same name, which uniqueConstraintExists misses+ - or:+ - uniqueConstraintExists:+ tableName: content_diagnostics_finding+ constraintName: idx_uniq_cd_finding+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_uniq_cd_finding+ changes:+ - addUniqueConstraint:+ tableName: content_diagnostics_finding+ columnNames: scan_id, entity_type, entity_id, finding_type+ constraintName: idx_uniq_cd_finding++ - changeSet:+ id: v65.3ynvmr+ author: yb+ comment: Content Diagnostics — index findings by entity+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_entity+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_entity+ columns:+ - column:+ name: entity_type+ - column:+ name: entity_id++ - changeSet:+ id: v65.qznubo+ author: yb+ comment: Content Diagnostics — index findings by detected_at+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_detected+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_detected+ columns:+ - column:+ name: detected_at++ - changeSet:+ id: v65.9pq42l+ author: yb+ comment: Content Diagnostics — index findings by finding_type + entity_created_at+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_created+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_created+ columns:+ - column:+ name: finding_type+ - column:+ name: entity_created_at++ - changeSet:+ id: v65.xy6d2y+ author: yb+ comment: Content Diagnostics — index findings by finding_type + last_active_at+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_active+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_active+ columns:+ - column:+ name: finding_type+ - column:+ name: last_active_at++ - changeSet:+ id: v65.5hg5yl+ author: yb+ comment: Content Diagnostics — index findings by finding_type + duration_ms+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_duration+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_duration+ columns:+ - column:+ name: finding_type+ - column:+ name: duration_ms++ - changeSet:+ id: v65.1l7sd5+ author: yb+ comment: Content Diagnostics — index findings by finding_type + content_count+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_content_count+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_content_count+ columns:+ - column:+ name: finding_type+ - column:+ name: content_count++ - changeSet:+ id: v65.2bp272+ author: yb+ comment: Content Diagnostics — index findings by finding_type + duplicate_count+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_duplicate_count+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_duplicate_count+ columns:+ - column:+ name: finding_type+ - column:+ name: duplicate_count++ - changeSet:+ id: v65.i4ropf+ author: yb+ comment: Content Diagnostics — index for the entity-types filter and entity-type sort+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_entity_kind+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_ftype_entity_kind+ columns:+ - column:+ name: finding_type+ - column:+ name: entity_kind++ - changeSet:+ id: v65.mmndvn+ author: yb+ comment: Content Diagnostics — index findings by invalidated_at+ preConditions:+ - onFail: MARK_RAN+ - not:+ - indexExists:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_invalidated+ changes:+ - createIndex:+ tableName: content_diagnostics_finding+ indexName: idx_cd_finding_invalidated+ columns:+ - column:+ name: invalidated_atdiff --git a/resources/user_key_value_types/content_diagnostics.edn b/resources/user_key_value_types/content_diagnostics.ednnew file mode 100644index 00000000000..905991e71fd--- /dev/null+++ b/resources/user_key_value_types/content_diagnostics.edn@@ -0,0 +1,9 @@+[:map+ [:value [:map {:closed true}+ [:entity_types {:optional true} [:sequential [:enum "question" "model" "metric" "dashboard" "document" "transform" "collection"]]]+ [:include_personal_collections {:optional true} :boolean]+ [:threshold_days {:optional true} [:int {:min 1}]]+ [:min_duration_ms {:optional true} [:int {:min 1}]]+ [:min_duplicate_count {:optional true} [:int {:min 1}]]+ [:sort_column {:optional true} [:enum "detected-at" "name" "entity-type" "collection-name" "created-by" "created-at" "finding-type" "last-active-at" "duration-ms" "duplicate-count" "content-count"]]+ [:sort_direction {:optional true} [:enum "asc" "desc"]]]]]diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.cljindex 9d031f47a9d..26beaff37b3 100644--- a/src/metabase/analytics/prometheus.clj+++ b/src/metabase/analytics/prometheus.clj@@ -532,6 +532,24 @@{:description "Duration (ms) of one stage (`enumerate` = DB fetch, `score` = in-memory scoring, `publish` = Snowplow emit) for one catalog within a Data Complexity Score run.":labels [:stage :catalog]:buckets [1 10 50 100 500 1000 5000 10000 30000 60000]})+ ;; content diagnostics scan (enterprise/content-diagnostics): one run/day/instance where the feature is on.+ ;; Counters throughout, no histogram: at one scan a day an instance contributes a single sample, so its+ ;; own bucket distribution is degenerate. The distribution worth having is the one across instances, and+ ;; Prometheus already keeps a series per instance - increase(scan_duration_ms_total[7d]) divided by+ ;; increase(scan_runs_total[7d]) is that instance's mean scan, which topk() ranks (naming the slow+ ;; instances, which buckets cannot) and quantile() aggregates into a fleet percentile.+ (prometheus/counter :metabase-content-diagnostics/scan-duration-ms+ {:description "Total milliseconds spent running Content Diagnostics scans, by outcome; over scan-runs it gives the mean scan."+ :labels [:status]})+ (prometheus/counter :metabase-content-diagnostics/scan-runs+ {:description "Number of Content Diagnostics scans, by outcome and, on failure, the stage that was running (none = succeeded, unknown = threw outside any stage)."+ :labels [:status :stage]})+ (prometheus/counter :metabase-content-diagnostics/scan-stage-ms+ {:description "Total milliseconds Content Diagnostics scans spent in each stage (checker.<name>, enrich, insert, invalidate); bumped on stage success only."+ :labels [:stage]})+ (prometheus/counter :metabase-content-diagnostics/findings-persisted+ {:description "Number of findings persisted by a Content Diagnostics scan that completed its insert stage, by finding type."+ :labels [:finding-type]});; explorations(prometheus/gauge :metabase-explorations/pending-queue-depth{:description "Number of exploration_query rows currently in 'pending' status (awaiting execution by the explorations background runner)."})diff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.cljindex 6d01b759364..abe371c9dcc 100644--- a/src/metabase/cmd/copy.clj+++ b/src/metabase/cmd/copy.clj@@ -171,6 +171,7 @@:model/Tenant:model/ConnectionImpersonation:model/CustomVizPlugin+ :model/ContentDiagnosticsFinding:model/TransformTest:model/TransformTestRun])))diff --git a/src/metabase/dashboards/models/dashboard.clj b/src/metabase/dashboards/models/dashboard.cljindex b9644c02588..c999db3b42a 100644--- a/src/metabase/dashboards/models/dashboard.clj+++ b/src/metabase/dashboards/models/dashboard.clj@@ -547,7 +547,8 @@^:allow-subquery {:select [:report_dashboard.id[(h2x/literal "Dashboard") :model][:report_dashboard.name :name]- [:last_viewed_at :last_used_at]]+ [:last_viewed_at :last_used_at]+ :report_dashboard.collection_id]:from :report_dashboard:left-join [:pulse [:and[:= :pulse.archived false]@@ -569,7 +570,4 @@[:= :report_dashboard.enable_embedding false])(when (setting/get :enable-public-sharing)[:= :report_dashboard.public_uuid nil])- [:or- (when (contains? (:collection-ids args) nil)- [:is :report_dashboard.collection_id nil])- [:in :report_dashboard.collection_id (-> args :collection-ids)]]]})+ (staleness/collection-filter :report_dashboard.collection_id args)]})diff --git a/src/metabase/documents/models/document.clj b/src/metabase/documents/models/document.cljindex 201eaace134..266ee63b7d2 100644--- a/src/metabase/documents/models/document.clj+++ b/src/metabase/documents/models/document.clj@@ -19,7 +19,10 @@[metabase.query-permissions.core :as query-perms][metabase.search.config :as search.config][metabase.search.spec :as search.spec]+ [metabase.settings.core :as setting]+ [metabase.staleness.core :as staleness][metabase.util :as u]+ [metabase.util.honey-sql-2 :as h2x][metabase.util.i18n :refer [deferred-tru]][metabase.util.json :as json][metabase.util.log :as log]@@ -549,6 +552,35 @@:collection-type :collection.type:archived-directly true}})+;;; ------------------------------------------------- Staleness ---------------------------------------------------++(defmethod staleness/find-stale-query :model/Document+ [_model args]+ ^:allow-subquery+ {:select [:document.id+ [(h2x/literal "Document") :model]+ [:document.name :name]+ ;; last_viewed_at is NOT NULL (default current_timestamp), so it's storage noise for a+ ;; never-viewed doc — null the anchor when view_count = 0 to keep "never used" distinguishable.+ [[:case [:= :document.view_count [:inline 0]] nil :else :document.last_viewed_at] :last_used_at]+ :document.collection_id]+ :from :document+ :left-join [:collection [:= :collection.id :document.collection_id]]+ :where [:and+ [:= :document.archived false]+ ;; stale = not viewed since the cutoff, OR never viewed and created before the cutoff.+ [:or+ [:<= :document.last_viewed_at (-> args :cutoff-date)]+ [:and+ [:= :document.view_count 0]+ [:<= :document.created_at (-> args :cutoff-date)]]]+ ;; only regular user collections (type nil), not system collections like+ ;; `instance-analytics`, `trash`, or the `library` collections.+ [:= :collection.type nil]+ (when (setting/get :enable-public-sharing)+ [:= :document.public_uuid nil])+ (staleness/collection-filter :document.collection_id args)]})+;;; ---------------------------------------------- Serialization --------------------------------------------------(defn- ast-model->entitydiff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex 3fb1f8d0f40..67bc917b9f5 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -34,6 +34,7 @@:model/Comment metabase.comments.models.comment:model/CommentReaction metabase.comments.models.comment-reaction:model/ConnectionImpersonation metabase-enterprise.impersonation.models+ :model/ContentDiagnosticsFinding metabase-enterprise.content-diagnostics.models.finding:model/ContentTranslation metabase.content-translation.models:model/CustomVizPlugin metabase-enterprise.custom-viz-plugin.models.custom-viz-plugin:model/DataApp metabase-enterprise.data-apps.models.data-appdiff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.cljindex f64e13d1a1b..458bad5f0b5 100644--- a/src/metabase/premium_features/settings.clj+++ b/src/metabase/premium_features/settings.clj@@ -248,6 +248,10 @@"Should we enable Collection Cleanup?":collection-cleanup)+(define-premium-feature ^{:added "0.65.0"} enable-content-diagnostics?+ "Should we enable Content Diagnostics?"+ :content-diagnostics)+(define-premium-feature ^{:added "0.51.0"} enable-database-auth-providers?"Should we enable database auth-providers?":database-auth-providers)@@ -399,6 +403,7 @@:cloud_custom_smtp (cloud-custom-smtp?):collection_cleanup (enable-collection-cleanup?):config_text_file (enable-config-text-file?)+ :content_diagnostics (enable-content-diagnostics?):content_translation (enable-content-translation?):content_verification (enable-content-verification?):custom-viz (enable-custom-viz?)diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex 0f02d5a598f..27d6e02cb5b 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -1887,7 +1887,8 @@^:allow-subquery {:select [:report_card.id[(h2x/literal "Card") :model][:report_card.name :name]- :last_used_at]+ :last_used_at+ :report_card.collection_id]:from :report_card:left-join [:moderation_review [:and[:= :moderation_review.moderated_item_id :report_card.id]@@ -1912,7 +1913,4 @@[:= :report_card.enable_embedding false])(when (setting/get :enable-public-sharing)[:= :report_card.public_uuid nil])- [:or- (when (contains? (:collection-ids args) nil)- [:is :report_card.collection_id nil])- [:in :report_card.collection_id (-> args :collection-ids)]]]})+ (staleness/collection-filter :report_card.collection_id args)]})diff --git a/src/metabase/staleness/core.clj b/src/metabase/staleness/core.cljindex f370df44bdf..ece1700e6da 100644--- a/src/metabase/staleness/core.clj+++ b/src/metabase/staleness/core.clj@@ -23,7 +23,20 @@[:cat :keyword [:* [:maybe [:ref ::predicate]]]]])(mr/def ::select-entry- [:or :keyword [:tuple [:or :keyword h2x/Literal] :keyword]])+ "A `:select` element: a bare column keyword, or an aliased expression paired with its output column.+ The expression is a [[predicate]] - which covers a column, a literal, and any operator applied to+ nested values, such as the `:case` a model uses to null out a recency column."+ [:or :keyword [:tuple ::predicate :keyword]])++(mr/def ::derived-table+ "A sub-select joined as a derived table, paired with its alias."+ [:tuple+ [:map {:closed true}+ [:select [:sequential [:ref ::select-entry]]]+ [:from [:or :keyword [:sequential :keyword]]]+ [:group-by {:optional true} [:sequential :keyword]]+ [:where {:optional true} ::predicate]]+ :keyword])(mr/def ::query"The shape [[find-stale-query]] must return, and [[metabase-enterprise.stale.db]] consumes: a HoneySQL@@ -31,7 +44,7 @@[:map {:closed true}[:select [:sequential ::select-entry]][:from :keyword]- [:left-join {:optional true} [:sequential [:or :keyword ::predicate]]]+ [:left-join {:optional true} [:sequential [:or :keyword ::derived-table ::predicate]]][:where {:optional true} ::predicate]])(defmulti find-stale-query@@ -45,3 +58,13 @@`id`, `model`, `name`, `last_used_at`."{:arglists '([model args])}(fn [model _args] model))++(defn collection-filter+ "HoneySQL condition scoping `collection-id-column` to `(:collection-ids args)`, or nil (no filtering)+ when `:collection-ids` is `:all`. A nil member of the set selects root-level content."+ [collection-id-column {:keys [collection-ids]}]+ (when (set? collection-ids)+ [:or+ (when (contains? collection-ids nil)+ [:is collection-id-column nil])+ [:in collection-id-column collection-ids]]))diff --git a/src/metabase/task_history/schema.clj b/src/metabase/task_history/schema.cljindex fb3e4c90d14..0853ea9733e 100644--- a/src/metabase/task_history/schema.clj+++ b/src/metabase/task_history/schema.clj@@ -56,6 +56,12 @@[:run-method [:= :cron]][:skipped-reason {:optional true} :string]])+(mr/def ::task-details.content-diagnostics-scan+ [:map {:closed true}+ [:scan-id {:optional true} :string]+ [:skipped-reason {:optional true} :string]+ [:run-method {:optional true} [:enum :cron :upgrade-backfill]]])+(mr/def ::task-details.remote-sync-auto-import[:map {:closed true}[:task-id ms/PositiveInt]])@@ -136,6 +142,7 @@::task-details.notification-trigger::task-details.send-pulse::task-details.run-transforms+ ::task-details.content-diagnostics-scan::task-details.remote-sync-auto-import::task-details.persist-refresh::task-details.sync-stepdiff --git a/src/metabase/testing_api/api.clj b/src/metabase/testing_api/api.cljindex 8e06ebea365..9697de1335e 100644--- a/src/metabase/testing_api/api.clj+++ b/src/metabase/testing_api/api.clj@@ -222,7 +222,9 @@;; use our API + we will need it when we make auto-TypeScript-signature generation happen;;(api.macros/defendpoint :post "/mark-stale"- "Mark the card or dashboard as stale"+ "Backdate an entity's activity so the staleness checks treat it as stale. Cards and dashboards carry+ the timestamp directly, a document is stale once it has not been viewed since the cutoff, and a+ transform is stale once it was created before the cutoff with no later run. Intended only for E2E tests."[_route-params_query-params{:keys [id model date-str]} :- [:map {:closed true}@@ -235,11 +237,18 @@(throw (ex-info (str "invalid date: '"date-str"' expected format: 'yyyy-MM-dd'")- {:status 400}))))+ {:status-code 400}))))(t/minus (t/local-date) (t/months 7)))](case model"card" (testing-api.db/set-card-last-used-at! id date)- "dashboard" (testing-api.db/set-dashboard-last-viewed-at! id date))))+ "dashboard" (testing-api.db/set-dashboard-last-viewed-at! id date)+ "document" (testing-api.db/set-document-last-viewed-at! id date)+ "transform" (do+ (testing-api.db/set-transform-created-at! id date)+ ;; collapsed to a zero-length run so backdating staleness can't also make the+ ;; transform look slow+ (testing-api.db/set-transform-run-times! id date))+ (throw (ex-info (str "unknown model: '" model "'") {:status-code 400})))));; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to;; use our API + we will need it when we make auto-TypeScript-signature generation happen@@ -261,6 +270,22 @@metabase-enterprise.metabot.usage[])+(defenterprise run-content-diagnostics-scan!+ "Runs a content diagnostics scan on EE and returns its topline. No-op on OSS."+ metabase-enterprise.content-diagnostics.scan+ [])++(api.macros/defendpoint :post "/content-diagnostics/scan"+ :- [:maybe [:map+ [:scan_id :string]+ [:finding_count :int]+ [:duration_ms :int]]]+ "Run a content diagnostics scan synchronously and return its topline. Findings only reach the UI+ through a scan, and the production trigger is a nightly job, so E2E tests need a way to run one on+ demand. Intended only for E2E tests."+ []+ (run-content-diagnostics-scan!))+(defenterprise reset-mfa-throttlers-for-testing!"Clears the accumulated MFA management throttle state (enroll/disable/regenerate) on EE.No-op on OSS."diff --git a/src/metabase/testing_api/db.clj b/src/metabase/testing_api/db.cljindex 92047a8d85e..a635f26c53b 100644--- a/src/metabase/testing_api/db.clj+++ b/src/metabase/testing_api/db.clj@@ -21,6 +21,24 @@last-viewed-at :- ms/TemporalInstant](t2/update! :model/Dashboard :id dashboard-id {:last_viewed_at last-viewed-at}))+(mu/defn set-document-last-viewed-at!+ "Set `last_viewed_at` of the Document with `document-id`."+ [document-id :- ms/PositiveInt+ last-viewed-at :- ms/TemporalInstant]+ (t2/update! :model/Document :id document-id {:last_viewed_at last-viewed-at}))++(mu/defn set-transform-created-at!+ "Set `created_at` of the Transform with `transform-id`."+ [transform-id :- ::lib.schema.id/transform+ created-at :- ms/TemporalInstant]+ (t2/update! :model/Transform :id transform-id {:created_at created-at}))++(mu/defn set-transform-run-times!+ "Set `start_time` and `end_time` of every TransformRun of the Transform with `transform-id`."+ [transform-id :- ::lib.schema.id/transform+ run-time :- ms/TemporalInstant]+ (t2/update! :model/TransformRun :transform_id transform-id {:start_time run-time :end_time run-time}))+(mu/defn delete-all-security-advisories!"Delete every SecurityAdvisory."[]diff --git a/src/metabase/transforms/db.clj b/src/metabase/transforms/db.cljindex 244709e6107..b8c346e82be 100644--- a/src/metabase/transforms/db.clj+++ b/src/metabase/transforms/db.clj@@ -128,17 +128,30 @@[tag-ids :- [:or [:set ms/PositiveInt] [:sequential ms/PositiveInt]]](t2/select-fn-set :transform_id :model/TransformTransformTag :tag_id [:in tag-ids]))-(mu/defn active-job-schedules-for-transforms- "Rows of Transform ID and the schedule of each active TransformJob that runs it through a shared tag."- [transform-ids :- [:set ::lib.schema.id/transform]]+(defn- job-schedule-rows+ "Rows of Transform ID and the schedule of the TransformJobs that run it through a shared tag, narrowed+ by `where`."+ [where](t2/select :model/TransformTransformTag{:select [:ttt.transform_id [:job.schedule :schedule]]:from [[:transform_transform_tag :ttt]]:join [[:transform_job_transform_tag :jtt] [:= :ttt.tag_id :jtt.tag_id][:transform_job :job] [:= :jtt.job_id :job.id]]- :where [:and- [:in :ttt.transform_id transform-ids]- [:= :job.active true]]}))+ :where where}))++(mu/defn active-job-schedules+ "Rows of Transform ID and the schedule of each active TransformJob that runs it through a shared tag,+ for every Transform such a job runs."+ []+ (job-schedule-rows [:= :job.active true]))++(mu/defn active-job-schedules-for-transforms+ "Rows of Transform ID and the schedule of each active TransformJob that runs it through a shared tag,+ for `transform-ids`."+ [transform-ids :- [:set ::lib.schema.id/transform]]+ (job-schedule-rows [:and+ [:in :ttt.transform_id transform-ids]+ [:= :job.active true]]))(mu/defn insert-transform-tag-links!"Insert the TransformTransformTag `rows`."@@ -430,6 +443,31 @@[:= :status "succeeded"]]:group-by [:transform_id]}))+(mu/defn latest-run-start-times-query+ "HoneySQL map selecting each transform's most recent run `start_time` (any status) as+ `:last_start`, one row per `:transform_id`; optionally restricted to `transform-ids`. The single+ definition of the \"most recent run\" anchor - embedded as the staleness method's join subquery+ ([[metabase.staleness.core/find-stale-query]] `:model/Transform`) and realized by+ [[last-start-times]] for the schedule-freshness check, so the two can't drift apart."+ ([]+ (latest-run-start-times-query nil))+ ([transform-ids :- [:maybe [:or+ [:set ::lib.schema.id/transform]+ [:sequential ::lib.schema.id/transform]]]]+ (cond-> ^:allow-subquery {:select [:transform_id [[:max :start_time] :last_start]]+ :from [:transform_run]+ :group-by [:transform_id]}+ (seq transform-ids) (assoc :where [:in :transform_id transform-ids]))))++(mu/defn last-start-times+ "Rows of Transform ID and the latest `start_time` of its runs, any status, restricted to+ `transform-ids` when non-empty - an empty collection means every Transform (see+ [[latest-run-start-times-query]])."+ [transform-ids :- [:maybe [:or+ [:set ::lib.schema.id/transform]+ [:sequential ::lib.schema.id/transform]]]]+ (t2/select :model/TransformRun (latest-run-start-times-query transform-ids)))+(mu/defn insert-run!"Insert `run` and return the new instance."[run :- ::transforms.schema/transform-run.update]diff --git a/src/metabase/transforms/freshness.clj b/src/metabase/transforms/freshness.cljindex fc01b69e0da..345c73e6263 100644--- a/src/metabase/transforms/freshness.clj+++ b/src/metabase/transforms/freshness.clj@@ -1,10 +1,12 @@(ns metabase.transforms.freshness- "Decides which transforms pulled into a job's plan only as dependencies are already fresh — no- scheduled fire time has passed since their last successful run — and can be skipped."+ "Schedule-aware freshness checks. Decides which transforms pulled into a job's plan only as+ dependencies are already fresh — no scheduled fire time has passed since their last successful+ run — and can be skipped ([[fresh-dep-ids]]); also which transforms are simply between fires of+ a slow schedule and so should not be treated as inactive ([[schedule-fresh-transform-ids]])."(:require[java-time.api :as t][metabase.transforms.models.transform-run :as transform-run]- [metabase.transforms.models.transform-tag :as transform-tag]+ [metabase.transforms.models.transform-schedule :as transform-schedule][metabase.util.log :as log])(:import(java.util Date)@@ -23,20 +25,37 @@(log/warnf "Ignoring unparseable transform job schedule %s: %s" (pr-str cron) (ex-message e))false)))+(defn- fresh-ids+ "Subset of `ids` that have a timestamp in `id->last-run` and whose schedules have not fired+ based on `schedules-by-id` since the `now` timestamp.+ IDs with no schedules are fresh once they have a timestamp; IDs with no timestamp are never fresh."+ [now ids schedules-by-id id->last-run]+ (let [now-date (Date/from (t/instant now))]+ (into #{}+ (for [id ids+ :let [ran (id->last-run id)]+ :when ran+ :let [ran-date (Date/from (t/instant ran))]+ :when (not-any? #(fired-since? % ran-date now-date) (schedules-by-id id))]+ id))))++(defn schedule-fresh-transform-ids+ "IDs of transforms that are between fires of an active job schedule as of `now`.+ Covered transforms have ≥1 active scheduled job and no schedule fire time has elapsed since+ their most recent run (any status) - e.g. a six-month cadence whose last run was two months+ ago - and should not be treated as inactive by recency checks like staleness. Transforms that+ have never run are never schedule-fresh."+ [now]+ (let [schedules-by-id (transform-schedule/schedules-for-transforms)+ ids (keys schedules-by-id)]+ (fresh-ids now ids schedules-by-id (transform-run/last-run-start-times ids))))+(defn fresh-dep-ids"Subset of `dep-ids` safe to skip as of `now`: a dep that has never succeeded is never fresh; ascheduled one is fresh while none of its schedules has fired since its last success; an unscheduledone is fresh once it has succeeded."[now dep-ids](when (seq dep-ids)- (let [schedules-by-id (transform-tag/schedules-for-transforms dep-ids)- last-success (transform-run/last-successful-run-times dep-ids)- now-date (Date/from (t/instant now))]- (into #{}- (keep (fn [id]- (when-let [ran (last-success id)]- (let [ran-date (Date/from (t/instant ran))]- (when (not-any? #(fired-since? % ran-date now-date)- (schedules-by-id id))- id)))))- dep-ids))))+ (fresh-ids now dep-ids+ (transform-schedule/schedules-for-transforms dep-ids)+ (transform-run/last-successful-run-times dep-ids))))diff --git a/src/metabase/transforms/jobs.clj b/src/metabase/transforms/jobs.cljindex 0fa371a9c9c..57302a1e0d0 100644--- a/src/metabase/transforms/jobs.clj+++ b/src/metabase/transforms/jobs.clj@@ -19,7 +19,7 @@[metabase.transforms.instrumentation :as transforms.instrumentation][metabase.transforms.models.job-run :as transforms.job-run][metabase.transforms.models.transform-run :as transform-run]- [metabase.transforms.models.transform-tag :as transform-tag]+ [metabase.transforms.models.transform-schedule :as transform-schedule][metabase.transforms.settings :as transforms.settings][metabase.transforms.usage :as transforms.usage][metabase.transforms.util :as transforms.u]@@ -417,7 +417,7 @@(let [tagged (job-transform-ids job-id)plan (:order (get-plan tagged))dep-ids (into #{} (comp (map :id) (remove tagged)) plan)- scheduled (set (keys (transform-tag/schedules-for-transforms dep-ids)))]+ scheduled (set (keys (transform-schedule/schedules-for-transforms dep-ids)))](map (fn [{:keys [id] :as transform}](cond-> transform(contains? dep-ids id) (assoc :dependency truediff --git a/src/metabase/transforms/models/transform.clj b/src/metabase/transforms/models/transform.cljindex 47146acadc9..27f63257cec 100644--- a/src/metabase/transforms/models/transform.clj+++ b/src/metabase/transforms/models/transform.clj@@ -15,13 +15,16 @@[metabase.remote-sync.core :as remote-sync][metabase.search.core :as search.core][metabase.search.spec :as search.spec]+ [metabase.staleness.core :as staleness][metabase.transforms-base.interface :as transforms-base.i][metabase.transforms-base.util :as transforms-base.u][metabase.transforms.db :as transforms.db]+ [metabase.transforms.freshness :as freshness][metabase.transforms.models.transform-run :as transform-run][metabase.transforms.schema][metabase.transforms.util :as transforms.u][metabase.util :as u]+ [metabase.util.honey-sql-2 :as h2x][metabase.util.log :as log][methodical.core :as methodical][toucan2.core :as t2]@@ -582,3 +585,30 @@:search-terms [:name :description]:render-terms {:transform-name :name:transform-id :id}})++;;; ------------------------------------------------- Staleness ------------------------------------------------++(defmethod staleness/find-stale-query :model/Transform+ [_model args]+ ;; Run-based: a transform is stale when it has never run and was created on/before the cutoff,+ ;; or when its most recent run started on/before the cutoff (failed runs still count). Transforms+ ;; between fires of a slower-than-threshold schedule are not stale.+ (let [schedule-fresh-ids (freshness/schedule-fresh-transform-ids (java.time.Instant/now))]+ ^:allow-subquery+ {:select [:transform.id+ [(h2x/literal "Transform") :model]+ [:transform.name :name]+ [:latest_run.last_start :last_used_at]+ :transform.collection_id]+ :from :transform+ :left-join [[(transforms.db/latest-run-start-times-query) :latest_run]+ [:= :latest_run.transform_id :transform.id]]+ :where [:and+ [:or+ [:and+ [:= :latest_run.last_start nil]+ [:<= :transform.created_at (:cutoff-date args)]]+ [:<= :latest_run.last_start (:cutoff-date args)]]+ (when (seq schedule-fresh-ids)+ [:not [:in :transform.id schedule-fresh-ids]])+ (staleness/collection-filter :transform.collection_id args)]}))diff --git a/src/metabase/transforms/models/transform_run.clj b/src/metabase/transforms/models/transform_run.cljindex 40a0ec843de..5ae42270ed4 100644--- a/src/metabase/transforms/models/transform_run.clj+++ b/src/metabase/transforms/models/transform_run.clj@@ -219,6 +219,15 @@(map (juxt :transform_id :last_success))(transforms.db/last-success-times transform-ids))))+(defn last-run-start-times+ "Map each id in `transform-ids` to its most recent run's `start_time`, regardless of run status.+ Ids with no runs are absent."+ [transform-ids]+ (when (seq transform-ids)+ (into {}+ (map (juxt :transform_id :last_start))+ (transforms.db/last-start-times transform-ids))))+(defn- status-labels"Display labels for TransformRun status values."[]diff --git a/src/metabase/transforms/models/transform_schedule.clj b/src/metabase/transforms/models/transform_schedule.cljnew file mode 100644index 00000000000..dc62558c893--- /dev/null+++ b/src/metabase/transforms/models/transform_schedule.clj@@ -0,0 +1,22 @@+(ns metabase.transforms.models.transform-schedule+ "Cron schedules attached to transforms."+ (:require+ [metabase.transforms.db :as transforms.db]))++(set! *warn-on-reflection* true)++(defn- schedules-by-transform-id+ [rows]+ (reduce (fn [m {:keys [transform_id schedule]}]+ (update m transform_id (fnil conj #{}) schedule))+ {}+ rows))++(defn schedules-for-transforms+ "Map each id in `transform-ids` — or every transform, in the 0-arity — to the cron schedules of the+ active jobs that run it via shared tags. Ids with no such job are absent."+ ([]+ (schedules-by-transform-id (transforms.db/active-job-schedules)))+ ([transform-ids]+ (when (seq transform-ids)+ (schedules-by-transform-id (transforms.db/active-job-schedules-for-transforms transform-ids)))))diff --git a/src/metabase/transforms/models/transform_tag.clj b/src/metabase/transforms/models/transform_tag.cljindex e60ea7f0134..d34a7f52313 100644--- a/src/metabase/transforms/models/transform_tag.clj+++ b/src/metabase/transforms/models/transform_tag.clj@@ -38,16 +38,6 @@[_model _instance](api/entitled-data-analyst?))-(defn schedules-for-transforms- "Map each id in `transform-ids` to the cron schedules of the active jobs that run it via shared tags.- Ids with no such job are absent."- [transform-ids]- (when (seq transform-ids)- (reduce (fn [m {:keys [transform_id schedule]}]- (update m transform_id (fnil conj #{}) schedule))- {}- (transforms.db/active-job-schedules-for-transforms transform-ids))))-(defn tag-name-exists?"Check if a tag with the given name already exists"[tag-name]diff --git a/test/metabase/analytics/stats_test.clj b/test/metabase/analytics/stats_test.cljindex f0c0b7617d9..7e846ddd115 100644--- a/test/metabase/analytics/stats_test.clj+++ b/test/metabase/analytics/stats_test.clj@@ -540,6 +540,7 @@or to this set, so that [[every-feature-is-accounted-for-test]] passes."#{:audit-app ;; tracked under :mb-analytics:collection-cleanup+ :content-diagnostics:data-apps:data-complexity-score:development-modediff --git a/test/metabase/documents/models/document_test.clj b/test/metabase/documents/models/document_test.cljindex 2cd73c098ab..69556d94912 100644--- a/test/metabase/documents/models/document_test.clj+++ b/test/metabase/documents/models/document_test.clj@@ -9,6 +9,8 @@[metabase.models.interface :as mi][metabase.models.serialization :as serdes][metabase.permissions.core :as perms]+ [metabase.stale-test :as stale-test]+ [metabase.staleness.core :as staleness][metabase.test :as mt][metabase.test.fixtures :as fixtures][metabase.test.util :as tu]@@ -806,6 +808,24 @@(t2/update! :model/Document doc-id {:name "Deserialized Name"}))(is (empty? @events-published))))))))+(deftest never-viewed-document-reports-null-activity-anchor+ (testing "a never-viewed document's activity anchor is NULL, not its last_viewed_at storage default+ (NOT NULL default current_timestamp) — the never-used arm (view_count = 0) stays+ distinguishable downstream"+ (mt/with-temp [:model/Document {viewed-id :id} (stale-test/stale-document {:name "viewed long ago"+ :view_count 5})+ :model/Document {never-id :id} {:name "never viewed"+ :created_at (stale-test/datetime-months-ago 7)}]+ (let [rows (t2/query (staleness/find-stale-query+ :model/Document+ {:collection-ids #{nil}+ :cutoff-date (stale-test/date-months-ago 6)}))+ anchor (fn [id] (:last_used_at (first (filter #(= id (:id %)) rows))))]+ (testing "a viewed-but-stale document keeps its real anchor"+ (is (some? (anchor viewed-id))))+ (testing "a never-viewed document (flagged via the never-used arm) reports NULL"+ (is (nil? (anchor never-id))))))))+(defn- export-document-ast"Run a document row through the serdes `:document` export transform and return the exported AST."[document]diff --git a/test/metabase/stale_test.clj b/test/metabase/stale_test.cljindex 848f27f9bd5..fa9d2753bbb 100644--- a/test/metabase/stale_test.clj+++ b/test/metabase/stale_test.clj@@ -28,13 +28,18 @@;; assoc the card with the current time minus 7 months(assoc card :last_used_at (datetime-months-ago 7)))+(defn stale-document [document]+ ;; assoc the document with the current time minus 7 months+ (assoc document :last_viewed_at (datetime-months-ago 7)))+(defmacro with-stale-items [inputs & body](let [processed-inputs(map (fn [[model binding args]](let [column (case model:model/Card :last_used_at:model/Dashboard :last_viewed_at- (throw (ex-info "`with-stale` only works for cards or dashboards." {})))]+ :model/Document :last_viewed_at+ (throw (ex-info "`with-stale` only works for cards, dashboards, or documents." {})))][model binding `(assoc ~args ~column (datetime-months-ago 7))]))(partition-all 3 inputs))]`(mt/with-temp ~(vec (apply concat processed-inputs))diff --git a/test/metabase/transforms/freshness_test.clj b/test/metabase/transforms/freshness_test.cljindex d7d92400764..a71b657af38 100644--- a/test/metabase/transforms/freshness_test.clj+++ b/test/metabase/transforms/freshness_test.clj@@ -107,3 +107,76 @@:is_active nil:end_time (t/minus now (t/minutes 1))})(is (= #{} (freshness/fresh-dep-ids now #{(:id t)}))))))))++(defn- insert-run-started-at!+ "Insert a transform run with explicit `status` and `start-time` — schedule-freshness anchors on+ the most recent run's start_time, regardless of status."+ [transform-id status start-time]+ (t2/insert! :model/TransformRun {:transform_id transform-id+ :run_method :cron+ :status status+ :is_active nil+ :start_time start-time+ :end_time start-time}))++(deftest schedule-fresh-transform-ids-test+ ;; pinned to the same Wednesday at 10:30 (local) as above: the hourly cron last fired at 10:00,+ ;; the daily one at midnight. Assertions are membership-based because the fn scans every+ ;; transform in the (shared) test DB, not a caller-supplied id set.+ (let [now (t/offset-date-time 2025 6 18 10 30)]+ (testing "with a daily schedule, freshness follows the most recent run's start_time, any status"+ (mt/with-temp [:model/TransformTag tag {:name "sf-daily-tag"}+ :model/TransformJob job {:name "sf-daily-job" :schedule daily-cron}+ :model/TransformJobTransformTag _ {:job_id (:id job) :tag_id (:id tag) :position 0}+ :model/Transform fresh {:name "sf-fresh"}+ :model/TransformTransformTag _ {:transform_id (:id fresh) :tag_id (:id tag) :position 0}+ :model/Transform stale {:name "sf-stale"}+ :model/TransformTransformTag _ {:transform_id (:id stale) :tag_id (:id tag) :position 0}+ :model/Transform failed {:name "sf-failed-recently"}+ :model/TransformTransformTag _ {:transform_id (:id failed) :tag_id (:id tag) :position 0}+ :model/Transform never {:name "sf-never-run"}+ :model/TransformTransformTag _ {:transform_id (:id never) :tag_id (:id tag) :position 0}]+ ;; 08:30 — after today's midnight fire → fresh; two days ago → two midnights since → not+ (insert-run-started-at! (:id fresh) :succeeded (t/minus now (t/hours 2)))+ (insert-run-started-at! (:id stale) :succeeded (t/minus now (t/days 2)))+ ;; a FAILED run still anchors freshness (unlike fresh-dep-ids, which needs a success)+ (insert-run-started-at! (:id failed) :failed (t/minus now (t/hours 2)))+ (let [ids (freshness/schedule-fresh-transform-ids now)]+ (is (contains? ids (:id fresh)))+ (is (not (contains? ids (:id stale))))+ (is (contains? ids (:id failed)))+ (testing "a transform that has never run is never schedule-fresh"+ (is (not (contains? ids (:id never))))))+ (testing "the most recent run governs: a late failed run re-freshens a stale transform"+ (insert-run-started-at! (:id stale) :failed (t/minus now (t/minutes 30)))+ (is (contains? (freshness/schedule-fresh-transform-ids now) (:id stale))))))+ (testing "only transforms covered by an active scheduled job can be schedule-fresh"+ (mt/with-temp [:model/TransformTag tag {:name "sf-inactive-tag"}+ :model/TransformJob job {:name "sf-inactive-job" :schedule daily-cron :active false}+ :model/TransformJobTransformTag _ {:job_id (:id job) :tag_id (:id tag) :position 0}+ :model/Transform inactive {:name "sf-inactively-scheduled"}+ :model/TransformTransformTag _ {:transform_id (:id inactive) :tag_id (:id tag) :position 0}+ :model/Transform unscheduled {:name "sf-unscheduled"}]+ ;; both ran recently enough to be fresh under the daily cadence — but neither has an+ ;; active schedule, so neither is schedule-fresh (they're plain recency cases instead)+ (insert-run-started-at! (:id inactive) :succeeded (t/minus now (t/hours 2)))+ (insert-run-started-at! (:id unscheduled) :succeeded (t/minus now (t/hours 2)))+ (let [ids (freshness/schedule-fresh-transform-ids now)]+ (is (not (contains? ids (:id inactive))))+ (is (not (contains? ids (:id unscheduled)))))))+ (testing "with multiple schedules, any one firing since the last run breaks freshness"+ (mt/with-temp [:model/TransformTag hourly-tag {:name "sf-hourly-tag"}+ :model/TransformTag daily-tag {:name "sf-daily-tag-2"}+ :model/TransformJob hourly-job {:name "sf-hourly-job" :schedule hourly-cron}+ :model/TransformJob daily-job {:name "sf-daily-job-2" :schedule daily-cron}+ :model/TransformJobTransformTag _ {:job_id (:id hourly-job) :tag_id (:id hourly-tag) :position 0}+ :model/TransformJobTransformTag _ {:job_id (:id daily-job) :tag_id (:id daily-tag) :position 0}+ :model/Transform t {:name "sf-hourly-and-daily"}+ :model/TransformTransformTag _ {:transform_id (:id t) :tag_id (:id hourly-tag) :position 0}+ :model/TransformTransformTag _ {:transform_id (:id t) :tag_id (:id daily-tag) :position 1}]+ (testing "a run 2h ago (08:30): the hourly job has fired since (09:00, 10:00)"+ (insert-run-started-at! (:id t) :succeeded (t/minus now (t/hours 2)))+ (is (not (contains? (freshness/schedule-fresh-transform-ids now) (:id t)))))+ (testing "a run 25m ago (10:05): no schedule has fired since"+ (insert-run-started-at! (:id t) :succeeded (t/minus now (t/minutes 25)))+ (is (contains? (freshness/schedule-fresh-transform-ids now) (:id t))))))))diff --git a/test/metabase/transforms/models/transform_schedule_test.clj b/test/metabase/transforms/models/transform_schedule_test.cljnew file mode 100644index 00000000000..e411ff326c5--- /dev/null+++ b/test/metabase/transforms/models/transform_schedule_test.clj@@ -0,0 +1,21 @@+(ns metabase.transforms.models.transform-schedule-test+ (:require+ [clojure.test :refer :all]+ [metabase.test :as mt]+ [metabase.transforms.models.transform-schedule :as transform-schedule]))++(deftest schedules-for-transforms-test+ (testing "returns the schedules of the active jobs that run a transform via shared tags"+ (mt/with-temp [:model/TransformTag tag {}+ :model/TransformJob active {:schedule "0 0 * * * ? *" :active true}+ :model/TransformJob inactive {:schedule "0 0 0 * * ? *" :active false}+ :model/TransformJobTransformTag _ {:job_id (:id active) :tag_id (:id tag) :position 0}+ :model/TransformJobTransformTag _ {:job_id (:id inactive) :tag_id (:id tag) :position 1}+ :model/Transform t {}+ :model/TransformTransformTag _ {:transform_id (:id t) :tag_id (:id tag) :position 0}+ :model/Transform untagged {}]+ (is (= {(:id t) #{"0 0 * * * ? *"}}+ (transform-schedule/schedules-for-transforms #{(:id t)}))+ "only the active job's schedule is included")+ (is (= {} (transform-schedule/schedules-for-transforms #{(:id untagged)}))+ "a transform with no scheduling job is absent"))))diff --git a/test/metabase/transforms/models/transform_test.clj b/test/metabase/transforms/models/transform_test.cljindex ef8cfaf42ab..7f2aff0125b 100644--- a/test/metabase/transforms/models/transform_test.clj+++ b/test/metabase/transforms/models/transform_test.clj@@ -9,11 +9,15 @@[metabase.permissions.core :as perms][metabase.permissions.models.data-permissions :as data-perms][metabase.permissions.models.permissions-group :as perms-group]+ [metabase.stale-test :as stale-test]+ [metabase.staleness.core :as staleness][metabase.test :as mt][metabase.transforms-base.query :as transforms-base.query][metabase.transforms.query-test-util :as query-test-util][toucan2.core :as t2]))+(set! *warn-on-reflection* true)+(deftest source-database-id-set-test(testing "inserting a transform correctly sets the source-database-id column"(mt/with-temp [:model/Transform transform@@ -220,6 +224,110 @@:query {:source-table (mt/id :people)}}}})(is (nil? (stored-deps id))))))+(defn- insert-run! [transform-id status months-ago]+ (t2/insert! :model/TransformRun+ {:transform_id transform-id+ :status status+ :run_method :cron+ :start_time (stale-test/datetime-months-ago months-ago)+ :end_time (stale-test/datetime-months-ago months-ago)}))++(deftest find-stale-query-test+ (testing "a transform is stale when never run past a created_at grace period, or its most recent run predates the cutoff"+ (let [cutoff-months 6+ old-months (+ cutoff-months 2) ; predates the cutoff+ recent-months 1 ; within the cutoff+ ;; fires once a year, ~2 months from now — so its previous fire was ~10 months ago,+ ;; before the old runs: nothing has fired since them+ schedule-anchor (-> (java.time.LocalDate/now) (.plusMonths 2) (.withDayOfMonth 15))+ yearly-cron (format "0 0 0 15 %d ?" (.getMonthValue schedule-anchor))+ daily-cron "0 0 0 * * ?"]+ (mt/with-temp [:model/Transform {never-new-id :id} {:name "never-new"}+ :model/Transform {never-old-id :id} {:name "never-old"+ :created_at (stale-test/datetime-months-ago old-months)}+ :model/Transform {old-id :id} {:name "old-run"}+ :model/Transform {fresh-id :id} {:name "fresh-run"}+ :model/Transform {recent-failed-id :id} {:name "recent-failed"}+ :model/Transform {old-failed-id :id} {:name "old-failed"}+ :model/Transform {old-then-recent-id :id} {:name "old-then-recent"}+ :model/Transform {on-schedule-id :id} {:name "on-schedule"}+ :model/Transform {missed-schedule-id :id} {:name "missed-schedule"}+ :model/Transform {never-scheduled-id :id} {:name "never-run-scheduled"+ :created_at (stale-test/datetime-months-ago old-months)}+ :model/TransformTag {slow-tag-id :id} {}+ :model/TransformTag {fast-tag-id :id} {}+ :model/TransformJob {slow-job-id :id} {:name "yearly job" :schedule yearly-cron}+ :model/TransformJob {fast-job-id :id} {:name "daily job" :schedule daily-cron}+ :model/TransformJobTransformTag _ {:job_id slow-job-id :tag_id slow-tag-id :position 0}+ :model/TransformJobTransformTag _ {:job_id fast-job-id :tag_id fast-tag-id :position 0}+ :model/TransformTransformTag _ {:transform_id on-schedule-id :tag_id slow-tag-id :position 0}+ :model/TransformTransformTag _ {:transform_id missed-schedule-id :tag_id fast-tag-id :position 0}+ :model/TransformTransformTag _ {:transform_id never-scheduled-id :tag_id slow-tag-id :position 0}]+ (insert-run! old-id :succeeded old-months)+ (insert-run! fresh-id :succeeded recent-months)+ ;; status is irrelevant — only the most recent run's start_time matters+ (insert-run! recent-failed-id :failed recent-months)+ (insert-run! old-failed-id :failed old-months)+ ;; two runs: the most recent (recent-months) governs, not the earlier one (old-months)+ (insert-run! old-then-recent-id :succeeded old-months)+ (insert-run! old-then-recent-id :succeeded recent-months)+ ;; both scheduled transforms last ran old-months ago; the yearly schedule hasn't fired since+ ;; (previous fire ~10 months ago), the daily schedule has fired thousands of times since+ (insert-run! on-schedule-id :succeeded old-months)+ (insert-run! missed-schedule-id :succeeded old-months)+ (let [stale-ids (set (map :id (t2/query (staleness/find-stale-query+ :model/Transform+ {:collection-ids :all+ :cutoff-date (stale-test/date-months-ago cutoff-months)}))))]+ (testing "a never-run transform is stale only once its created_at passes the cutoff"+ (is (contains? stale-ids never-old-id))+ (is (not (contains? stale-ids never-new-id))))+ (testing "a transform whose most recent run started before the cutoff is stale, regardless of status"+ (is (contains? stale-ids old-id))+ (is (contains? stale-ids old-failed-id)))+ (testing "a transform with a recent run is not stale, regardless of status"+ (is (not (contains? stale-ids fresh-id)))+ (is (not (contains? stale-ids recent-failed-id))))+ (testing "the most recent run governs: an old run followed by a recent one is not stale"+ (is (not (contains? stale-ids old-then-recent-id))))+ (testing "an old run is not stale while a slower-than-threshold schedule has not fired since it"+ (is (not (contains? stale-ids on-schedule-id))))+ (testing "an old run is stale when its schedule has fired since it (missed fires)"+ (is (contains? stale-ids missed-schedule-id)))+ (testing "the schedule exception does not shield never-run transforms"+ (is (contains? stale-ids never-scheduled-id))))))))++(deftest find-stale-query-collection-scoping-test+ (testing "find-stale-query scopes transforms to :collection-ids, like the card and dashboard arms"+ (let [cutoff-months 6+ ;; never run and created before the cutoff, so each one is stale on the created_at arm alone+ long-ago (stale-test/datetime-months-ago (+ cutoff-months 2))]+ (mt/with-temp [:model/Collection {coll-a :id} {:namespace "transforms"}+ :model/Collection {coll-b :id} {:namespace "transforms"}+ :model/Transform {in-a :id} {:name "in-a" :collection_id coll-a :created_at long-ago}+ :model/Transform {in-b :id} {:name "in-b" :collection_id coll-b :created_at long-ago}+ :model/Transform {at-root :id} {:name "at-root" :created_at long-ago}]+ (let [mine #{in-a in-b at-root}+ ;; the table carries transforms from elsewhere in the suite, so narrow to this test's rows+ stale-ids (fn [collection-ids]+ (into #{}+ (comp (map :id) (filter mine))+ (t2/query (staleness/find-stale-query+ :model/Transform+ {:collection-ids collection-ids+ :cutoff-date (stale-test/date-months-ago cutoff-months)}))))]+ (testing "a named collection returns its own stale transforms and no others"+ (is (= #{in-a} (stale-ids #{coll-a})))+ (is (= #{in-b} (stale-ids #{coll-b}))))+ (testing "several collections return the union"+ (is (= #{in-a in-b} (stale-ids #{coll-a coll-b}))))+ (testing "a nil member selects root-level transforms alongside the named collection"+ (is (= #{in-a at-root} (stale-ids #{coll-a nil}))))+ (testing "an empty set means no collections, not every collection"+ (is (= #{} (stale-ids #{}))))+ (testing ":all searches instance-wide"+ (is (= mine (stale-ids :all)))))))))+(deftest last-run-hydration-omits-heartbeat-test(testing "the `:last_run` hydrated onto a transform leaves out the run's internal `last_heartbeat`"(mt/with-temp [:model/Transform {transform-id :id} {}diff --git a/test/metabase/transforms/models_test.clj b/test/metabase/transforms/models_test.cljindex 5486baf4175..317d8d3bf7d 100644--- a/test/metabase/transforms/models_test.clj+++ b/test/metabase/transforms/models_test.clj@@ -378,22 +378,6 @@(get-in (t2/select-one :model/Transform transform-id)[:source :source-incremental-strategy :lookback])))))))-(deftest schedules-for-transforms-test- (testing "returns the schedules of the active jobs that run a transform via shared tags"- (mt/with-temp [:model/TransformTag tag {}- :model/TransformJob active {:schedule "0 0 * * * ? *" :active true}- :model/TransformJob inactive {:schedule "0 0 0 * * ? *" :active false}- :model/TransformJobTransformTag _ {:job_id (:id active) :tag_id (:id tag) :position 0}- :model/TransformJobTransformTag _ {:job_id (:id inactive) :tag_id (:id tag) :position 1}- :model/Transform t {}- :model/TransformTransformTag _ {:transform_id (:id t) :tag_id (:id tag) :position 0}- :model/Transform untagged {}]- (is (= {(:id t) #{"0 0 * * * ? *"}}- (transform-tag/schedules-for-transforms #{(:id t)}))- "only the active job's schedule is included")- (is (= {} (transform-tag/schedules-for-transforms #{(:id untagged)}))- "a transform with no scheduling job is absent"))))-(deftest start-run-stores-job-run-id-test(testing "start-run! stores job_run_id when provided in properties"(mt/with-premium-features #{:transforms-basic}diff --git a/test_resources/serialization_baseline/settings.yaml b/test_resources/serialization_baseline/settings.yamlindex be0e15b075f..cfd879d4e76 100644--- a/test_resources/serialization_baseline/settings.yaml+++ b/test_resources/serialization_baseline/settings.yaml@@ -23,6 +23,17 @@ custom-geojson-enabled: nullcustom-homepage: nullcustom-homepage-dashboard: nullcustom-viz-enabled: null+content-diagnostics-crowded-collection-threshold-items: null+content-diagnostics-crowded-dashboard-threshold-dashcards-per-tab: null+content-diagnostics-crowded-dashboard-threshold-tabs: null+content-diagnostics-crowded-document-threshold-cards: null+content-diagnostics-empty-card-lookback-days: null+content-diagnostics-finding-retention-days: null+content-diagnostics-slow-card-threshold-seconds: null+content-diagnostics-slow-transform-threshold-seconds: null+content-diagnostics-sparse-collection-threshold-items: null+content-diagnostics-sparse-dashboard-threshold-dashcards: null+content-diagnostics-stale-threshold-days: nulldefault-maps-enabled: nulldisable-cors-on-localhost: nullemail-max-recipients-per-message: null