From 85d4235f1f7e01cae899fcfa0428c87f40dec952 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 27 Oct 2022 20:34:52 +0800 Subject: [PATCH 1/3] Telemetry: Track docs usage --- code/lib/core-server/src/build-static.ts | 6 ++-- code/lib/core-server/src/dev-server.ts | 10 ++++--- .../core-server/src/utils/summarizeIndex.ts | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 code/lib/core-server/src/utils/summarizeIndex.ts diff --git a/code/lib/core-server/src/build-static.ts b/code/lib/core-server/src/build-static.ts index 9b89a440cc21..b9567b6d4530 100644 --- a/code/lib/core-server/src/build-static.ts +++ b/code/lib/core-server/src/build-static.ts @@ -32,6 +32,7 @@ import { getBuilders } from './utils/get-builders'; import { extractStoriesJson, convertToIndexV3 } from './utils/stories-json'; import { extractStorybookMetadata } from './utils/metadata'; import { StoryIndexGenerator } from './utils/StoryIndexGenerator'; +import { summarizeIndex } from './utils/summarizeIndex'; export async function buildStaticStandalone( options: CLIOptions & LoadOptions & BuilderOptions & { outputDir: string } @@ -174,10 +175,7 @@ export async function buildStaticStandalone( const storyIndex = await generator?.getIndex(); const payload = storyIndex ? { - storyIndex: { - storyCount: Object.keys(storyIndex.entries).length, - version: storyIndex.v, - }, + storyIndex: summarizeIndex(storyIndex), } : undefined; await telemetry('build', payload, { configDir: options.configDir }); diff --git a/code/lib/core-server/src/dev-server.ts b/code/lib/core-server/src/dev-server.ts index c2c9e6c5c49e..42fa8ca5e356 100644 --- a/code/lib/core-server/src/dev-server.ts +++ b/code/lib/core-server/src/dev-server.ts @@ -17,6 +17,7 @@ import { getServerChannel } from './utils/get-server-channel'; import { openInBrowser } from './utils/open-in-browser'; import { getBuilders } from './utils/get-builders'; import { StoryIndexGenerator } from './utils/StoryIndexGenerator'; +import { summarizeIndex } from './utils/summarizeIndex'; // @ts-expect-error (Converted from ts-ignore) export const router: Router = new Router(); @@ -66,12 +67,13 @@ export async function storybookDevServer(options: Options) { if (!core?.disableTelemetry) { initializedStoryIndexGenerator.then(async (generator) => { const storyIndex = await generator?.getIndex(); + const entries = Object.values(storyIndex.entries); + if (storyIndex) { + console.dir(entries.filter((e) => e.type === 'docs')); // .map((e) => ({ id: e.id, type: e.type }))); + } const payload = storyIndex ? { - storyIndex: { - storyCount: Object.keys(storyIndex.entries).length, - version: storyIndex.v, - }, + storyIndex: summarizeIndex(storyIndex), } : undefined; telemetry('dev', payload, { configDir: options.configDir }); diff --git a/code/lib/core-server/src/utils/summarizeIndex.ts b/code/lib/core-server/src/utils/summarizeIndex.ts new file mode 100644 index 000000000000..7093abab55ae --- /dev/null +++ b/code/lib/core-server/src/utils/summarizeIndex.ts @@ -0,0 +1,28 @@ +import { Store_StoryIndex } from '@storybook/types'; + +export function summarizeIndex(storyIndex: Store_StoryIndex) { + let storyCount = 0; + let docsPageCount = 0; + let storiesMdxCount = 0; + let mdxCount = 0; + Object.values(storyIndex.entries).forEach((entry) => { + if (entry.type === 'story') { + storyCount += 1; + } else if (entry.type === 'docs') { + if (entry.standalone) { + mdxCount += 1; + } else if (entry.importPath.endsWith('.mdx')) { + storiesMdxCount += 1; + } else { + docsPageCount += 1; + } + } + }); + return { + storyCount, + docsPageCount, + storiesMdxCount, + mdxCount, + version: storyIndex.v, + }; +} From a6499d6190ac6d496774bb3d9b95e351849ca0ca Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 27 Oct 2022 20:55:30 +0800 Subject: [PATCH 2/3] Fix boot notification --- code/lib/telemetry/src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code/lib/telemetry/src/index.ts b/code/lib/telemetry/src/index.ts index f49b77fb7a4d..6a07d9ad407f 100644 --- a/code/lib/telemetry/src/index.ts +++ b/code/lib/telemetry/src/index.ts @@ -14,7 +14,11 @@ export const telemetry = async ( payload: Payload = {}, options: Partial = {} ) => { - await notify(); + // Don't notify on boot since it can lead to double notification in `sb init`. + // The notification will happen when the actual command runs. + if (eventType !== 'boot') { + await notify(); + } const telemetryData: TelemetryData = { eventType, payload, From e3986596d855c647382650d4487344d2648af200 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Thu, 27 Oct 2022 20:57:35 +0800 Subject: [PATCH 3/3] Remove accidental debug code --- code/lib/core-server/src/dev-server.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/code/lib/core-server/src/dev-server.ts b/code/lib/core-server/src/dev-server.ts index 42fa8ca5e356..b3023f4d422a 100644 --- a/code/lib/core-server/src/dev-server.ts +++ b/code/lib/core-server/src/dev-server.ts @@ -67,10 +67,6 @@ export async function storybookDevServer(options: Options) { if (!core?.disableTelemetry) { initializedStoryIndexGenerator.then(async (generator) => { const storyIndex = await generator?.getIndex(); - const entries = Object.values(storyIndex.entries); - if (storyIndex) { - console.dir(entries.filter((e) => e.type === 'docs')); // .map((e) => ({ id: e.id, type: e.type }))); - } const payload = storyIndex ? { storyIndex: summarizeIndex(storyIndex),