From 23812308d8299507dc4d33d564663f052ec6d3ff Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 13 Aug 2024 09:44:43 +0800 Subject: [PATCH 1/2] Telemetry: Disable save-from-controls telemetry for example stories --- code/core/src/core-server/utils/save-story/save-story.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code/core/src/core-server/utils/save-story/save-story.ts b/code/core/src/core-server/utils/save-story/save-story.ts index 5369d99a4cba..0cdc5b5bd0db 100644 --- a/code/core/src/core-server/utils/save-story/save-story.ts +++ b/code/core/src/core-server/utils/save-story/save-story.ts @@ -49,6 +49,8 @@ const removeExtraNewlines = (code: string, name: string) => { : code; }; +const IS_CLI_EXAMPLE = /^example-(button|header|page)--/; + export function initializeSaveStory(channel: Channel, options: Options, coreConfig: CoreConfig) { channel.on(SAVE_STORY_REQUEST, async ({ id, payload }: RequestData) => { const { csfId, importPath, args, name } = payload; @@ -120,7 +122,9 @@ export function initializeSaveStory(channel: Channel, options: Options, coreConf error: null, } satisfies ResponseData); - if (!coreConfig.disableTelemetry) { + // don't take credit for save-from-controls actions against CLI example stories + const isCLIExample = IS_CLI_EXAMPLE.test(newStoryId ?? ''); + if (!coreConfig.disableTelemetry && !isCLIExample) { await telemetry('save-story', { action: name ? 'createStory' : 'updateStory', success: true, From 96b185d3cf42b0b3d60796bbfb4596c32e901249 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Tue, 13 Aug 2024 11:26:21 +0800 Subject: [PATCH 2/2] Refactor to keep example story test DRY --- code/core/src/core-server/utils/doTelemetry.ts | 2 +- .../src/core-server/utils/save-story/save-story.ts | 6 ++---- code/core/src/core-server/utils/summarizeIndex.ts | 12 ++---------- code/core/src/telemetry/index.ts | 9 +++++++++ 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/code/core/src/core-server/utils/doTelemetry.ts b/code/core/src/core-server/utils/doTelemetry.ts index be9479cf3348..5738f1aad560 100644 --- a/code/core/src/core-server/utils/doTelemetry.ts +++ b/code/core/src/core-server/utils/doTelemetry.ts @@ -1,5 +1,5 @@ import invariant from 'tiny-invariant'; -import type { CoreConfig, Options, StoryIndex } from '@storybook/core/types'; +import type { CoreConfig, Options } from '@storybook/core/types'; import { telemetry, getPrecedingUpgrade } from '@storybook/core/telemetry'; import { useStorybookMetadata } from './metadata'; import type { StoryIndexGenerator } from './StoryIndexGenerator'; diff --git a/code/core/src/core-server/utils/save-story/save-story.ts b/code/core/src/core-server/utils/save-story/save-story.ts index 0cdc5b5bd0db..8931aa2054e8 100644 --- a/code/core/src/core-server/utils/save-story/save-story.ts +++ b/code/core/src/core-server/utils/save-story/save-story.ts @@ -16,7 +16,7 @@ import { storyNameFromExport, toId } from '@storybook/csf'; import { printCsf, readCsf } from '@storybook/core/csf-tools'; import { logger } from '@storybook/core/node-logger'; import type { CoreConfig, Options } from '@storybook/core/types'; -import { telemetry } from '@storybook/core/telemetry'; +import { telemetry, isExampleStoryId } from '@storybook/core/telemetry'; import { basename, join } from 'node:path'; import { updateArgsInCsfFile } from './update-args-in-csf-file'; @@ -49,8 +49,6 @@ const removeExtraNewlines = (code: string, name: string) => { : code; }; -const IS_CLI_EXAMPLE = /^example-(button|header|page)--/; - export function initializeSaveStory(channel: Channel, options: Options, coreConfig: CoreConfig) { channel.on(SAVE_STORY_REQUEST, async ({ id, payload }: RequestData) => { const { csfId, importPath, args, name } = payload; @@ -123,7 +121,7 @@ export function initializeSaveStory(channel: Channel, options: Options, coreConf } satisfies ResponseData); // don't take credit for save-from-controls actions against CLI example stories - const isCLIExample = IS_CLI_EXAMPLE.test(newStoryId ?? ''); + const isCLIExample = isExampleStoryId(newStoryId ?? csfId); if (!coreConfig.disableTelemetry && !isCLIExample) { await telemetry('save-story', { action: name ? 'createStory' : 'updateStory', diff --git a/code/core/src/core-server/utils/summarizeIndex.ts b/code/core/src/core-server/utils/summarizeIndex.ts index 56b4b27ad929..2f4f8c14d9b8 100644 --- a/code/core/src/core-server/utils/summarizeIndex.ts +++ b/code/core/src/core-server/utils/summarizeIndex.ts @@ -1,4 +1,5 @@ import type { IndexEntry, StoryIndex } from '@storybook/core/types'; +import { isExampleStoryId } from '@storybook/core/telemetry'; import { isMdxEntry, AUTODOCS_TAG, PLAY_FN_TAG } from './StoryIndexGenerator'; @@ -25,15 +26,6 @@ const isCLIExampleEntry = (entry: IndexEntry) => 'example-page--logged-out', ].includes(entry.id); -/** - * Is this story part of the CLI generated examples, - * including user-created stories in those files - */ -const isAnyExampleEntry = (entry: IndexEntry) => - entry.id.startsWith('example-button--') || - entry.id.startsWith('example-header--') || - entry.id.startsWith('example-page--'); - export function summarizeIndex(storyIndex: StoryIndex) { let storyCount = 0; const componentTitles = new Set(); @@ -49,7 +41,7 @@ export function summarizeIndex(storyIndex: StoryIndex) { if (isCLIExampleEntry(entry)) { if (entry.type === 'story') exampleStoryCount += 1; if (entry.type === 'docs') exampleDocsCount += 1; - } else if (isAnyExampleEntry(entry)) { + } else if (isExampleStoryId(entry.id)) { if (entry.type === 'story') onboardingStoryCount += 1; if (entry.type === 'docs') onboardingDocsCount += 1; } else if (entry.type === 'story') { diff --git a/code/core/src/telemetry/index.ts b/code/core/src/telemetry/index.ts index d8286d0b7535..8fe61e441011 100644 --- a/code/core/src/telemetry/index.ts +++ b/code/core/src/telemetry/index.ts @@ -15,6 +15,15 @@ export { getPrecedingUpgrade } from './event-cache'; export { addToGlobalContext } from './telemetry'; +/** + * Is this story part of the CLI generated examples, + * including user-created stories in those files + */ +export const isExampleStoryId = (storyId: string) => + storyId.startsWith('example-button--') || + storyId.startsWith('example-header--') || + storyId.startsWith('example-page--'); + export const telemetry = async ( eventType: EventType, payload: Payload = {},