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
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;
}