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
Fix module stats counting (#78184)
Fraser1591e74409eb
diff --git a/.github/workflows/module-boundaries-stats.yml b/.github/workflows/module-boundaries-stats.ymlindex 6a3784487d7..0e08de4ff81 100644--- a/.github/workflows/module-boundaries-stats.yml+++ b/.github/workflows/module-boundaries-stats.yml@@ -19,40 +19,7 @@ jobs:run: bun install --frozen-lockfile- name: get module boundary statsid: get-stats- run: |- NAMED_MODULES=$(node --input-type=module -e "- import { elements } from './frontend/lint/module-boundaries.mjs';- const named = elements.filter(e => !['shared/other', 'app/misc', 'other'].includes(e.type));- console.log(named.length);- ")-- ENFORCED_MODULES=$(node --input-type=module -e "- import { elements } from './frontend/lint/module-boundaries.mjs';- const enforced = new Set(elements.filter(e => e.enforceOutgoing).map(e => e.type));- console.log(enforced.size);- ")-- NAMED_DIRS=$(node --input-type=module -e "- import { elements } from './frontend/lint/module-boundaries.mjs';- const dirs = new Set();- for (const el of elements) {- const m = el.pattern.match(/^frontend\/src\/metabase\/(\w[\w-]*)\//)- if (m) dirs.add(m[1]);- }- for (const d of dirs) console.log(d);- ")- TOTAL_DIRS=$(ls -d frontend/src/metabase/*/ | xargs -I{} basename {} | wc -l | tr -d ' ')- NAMED_COUNT=$(echo "$NAMED_DIRS" | grep -c . || true)- UNMODULED=$(($TOTAL_DIRS - $NAMED_COUNT))-- VIOLATIONS=$(bun run module-boundaries 2>&1 | grep -o '✖ [0-9]* problems' | grep -o '[0-9]*' || echo "0")-- echo "$NAMED_MODULES modules, $ENFORCED_MODULES enforced, $UNMODULED unmoduled folders, $VIOLATIONS violations"-- echo "NAMED_MODULES=$NAMED_MODULES" >> $GITHUB_OUTPUT- echo "ENFORCED_MODULES=$ENFORCED_MODULES" >> $GITHUB_OUTPUT- echo "UNMODULED=$UNMODULED" >> $GITHUB_OUTPUT- echo "VIOLATIONS=$VIOLATIONS" >> $GITHUB_OUTPUT+ run: node frontend/lint/module-boundaries-stats.mjs | tee -a "$GITHUB_OUTPUT"- name: Upload statsuses: actions/github-script@v9env:diff --git a/eslint.config.mjs b/eslint.config.mjsindex b80ede697f7..7506231b868 100644--- a/eslint.config.mjs+++ b/eslint.config.mjs@@ -1086,7 +1086,7 @@ const configs = [},},{- files: ["frontend/lint/**/*.js"],+ files: ["frontend/lint/**/*.js", "frontend/lint/**/*.mjs"],languageOptions: {globals: {...globals.node,diff --git a/frontend/lint/module-boundaries-stats.mjs b/frontend/lint/module-boundaries-stats.mjsnew file mode 100644index 00000000000..323796fa7c0--- /dev/null+++ b/frontend/lint/module-boundaries-stats.mjs@@ -0,0 +1,77 @@+/**+ * Computes the weekly module-boundary stats uploaded to stats.metabase.com by+ * .github/workflows/module-boundaries-stats.yml. Prints KEY=value lines+ * suitable for appending to $GITHUB_OUTPUT.+ */+import { execFileSync } from "child_process";+import { readdirSync } from "fs";+import { fileURLToPath } from "url";++import { elements } from "./module-boundaries.mjs";++// catch-all buckets that aren't real modules+const EXCLUDED_TYPES = ["shared/other", "app/misc", "other"];++export function getNamedModules(elements) {+ return new Set(+ elements+ .filter((element) => !EXCLUDED_TYPES.includes(element.type))+ .map((element) => element.type),+ );+}++export function getEnforcedModules(elements) {+ return new Set(+ elements+ .filter(+ (element) =>+ element.enforceOutgoing && !EXCLUDED_TYPES.includes(element.type),+ )+ .map((element) => element.type),+ );+}++export function getUnmoduledFolders(elements, folders) {+ const namedDirs = new Set();+ for (const element of elements) {+ const match = element.pattern.match(+ /^frontend\/src\/metabase\/(\w[\w-]*)\//,+ );+ if (match) {+ namedDirs.add(match[1]);+ }+ }+ return folders.filter((folder) => !namedDirs.has(folder));+}++function getViolationCount() {+ let output;+ try {+ output = execFileSync("bun", ["run", "module-boundaries"], {+ encoding: "utf8",+ stdio: ["ignore", "pipe", "pipe"],+ });+ } catch (error) {+ // eslint exits non-zero when there are violations+ output = `${error.stdout ?? ""}${error.stderr ?? ""}`;+ }+ const match = output.match(/✖ (\d+) problems?/);+ return match ? Number(match[1]) : 0;+}++function main() {+ const folders = readdirSync("frontend/src/metabase", {+ withFileTypes: true,+ })+ .filter((entry) => entry.isDirectory())+ .map((entry) => entry.name);++ console.log(`NAMED_MODULES=${getNamedModules(elements).size}`);+ console.log(`ENFORCED_MODULES=${getEnforcedModules(elements).size}`);+ console.log(`UNMODULED=${getUnmoduledFolders(elements, folders).length}`);+ console.log(`VIOLATIONS=${getViolationCount()}`);+}++if (process.argv[1] === fileURLToPath(import.meta.url)) {+ main();+}diff --git a/frontend/lint/tests/module-boundaries-stats.unit.spec.js b/frontend/lint/tests/module-boundaries-stats.unit.spec.jsnew file mode 100644index 00000000000..5908081c525--- /dev/null+++ b/frontend/lint/tests/module-boundaries-stats.unit.spec.js@@ -0,0 +1,119 @@+import {+ getEnforcedModules,+ getNamedModules,+ getUnmoduledFolders,+} from "../module-boundaries-stats.mjs";++function element({ type, pattern = "frontend/src/metabase/x/**", ...rest }) {+ return { type, pattern, ...rest };+}++describe("getNamedModules", () => {+ it("counts a module with multiple pattern entries once", () => {+ const elements = [+ element({ type: "app/mcp-app" }),+ element({ type: "app/mcp-app" }),+ element({ type: "app/mcp-app" }),+ element({ type: "shared/nav" }),+ ];+ expect(getNamedModules(elements)).toEqual(+ new Set(["app/mcp-app", "shared/nav"]),+ );+ });++ it("excludes the catch-all buckets", () => {+ const elements = [+ element({ type: "shared/other" }),+ element({ type: "app/misc" }),+ element({ type: "other" }),+ element({ type: "shared/nav" }),+ ];+ expect(getNamedModules(elements)).toEqual(new Set(["shared/nav"]));+ });+});++describe("getEnforcedModules", () => {+ it("only counts modules with enforceOutgoing", () => {+ const elements = [+ element({ type: "shared/nav", enforceOutgoing: true }),+ element({ type: "shared/home" }),+ ];+ expect(getEnforcedModules(elements)).toEqual(new Set(["shared/nav"]));+ });++ it("counts a module with multiple enforced entries once", () => {+ const elements = [+ element({ type: "app/mcp-app", enforceOutgoing: true }),+ element({ type: "app/mcp-app", enforceOutgoing: true }),+ ];+ expect(getEnforcedModules(elements)).toEqual(new Set(["app/mcp-app"]));+ });++ it("excludes the catch-all buckets even when enforced", () => {+ const elements = [+ element({ type: "app/misc", enforceOutgoing: true }),+ element({ type: "shared/other", enforceOutgoing: true }),+ element({ type: "shared/nav", enforceOutgoing: true }),+ ];+ expect(getEnforcedModules(elements)).toEqual(new Set(["shared/nav"]));+ });++ it("is a subset of the named modules", () => {+ const elements = [+ element({ type: "shared/nav", enforceOutgoing: true }),+ element({ type: "shared/home" }),+ element({ type: "app/misc", enforceOutgoing: true }),+ ];+ const named = getNamedModules(elements);+ for (const type of getEnforcedModules(elements)) {+ expect(named).toContain(type);+ }+ });+});++describe("getUnmoduledFolders", () => {+ const folders = ["nav", "home", "hoc", "rich_text_editing"];++ it("returns folders no pattern points into", () => {+ const elements = [+ element({ type: "shared/nav", pattern: "frontend/src/metabase/nav/**" }),+ element({+ type: "shared/home",+ pattern: "frontend/src/metabase/home/**",+ }),+ ];+ expect(getUnmoduledFolders(elements, folders)).toEqual([+ "hoc",+ "rich_text_editing",+ ]);+ });++ it("ignores patterns outside frontend/src/metabase", () => {+ const elements = [+ element({+ type: "shared/embedding-sdk",+ pattern: "enterprise/frontend/src/embedding-sdk-ee/**",+ }),+ element({ type: "shared/nav", pattern: "frontend/src/nav/**" }),+ ];+ expect(getUnmoduledFolders(elements, folders)).toEqual(folders);+ });++ it("matches a folder from a pattern into a subdirectory", () => {+ const elements = [+ element({+ type: "app/nav-part",+ pattern: "frontend/src/metabase/nav/components/**",+ }),+ ];+ expect(getUnmoduledFolders(elements, folders)).toEqual([+ "home",+ "hoc",+ "rich_text_editing",+ ]);+ });++ it("returns every folder when there are no elements", () => {+ expect(getUnmoduledFolders([], folders)).toEqual(folders);+ });+});