Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry: Measure docs usage #19648

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 });
Expand Down
6 changes: 2 additions & 4 deletions code/lib/core-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -68,10 +69,7 @@ export async function storybookDevServer(options: Options) {
const storyIndex = await generator?.getIndex();
const payload = storyIndex
? {
storyIndex: {
storyCount: Object.keys(storyIndex.entries).length,
version: storyIndex.v,
},
storyIndex: summarizeIndex(storyIndex),
}
: undefined;
telemetry('dev', payload, { configDir: options.configDir });
Expand Down
28 changes: 28 additions & 0 deletions code/lib/core-server/src/utils/summarizeIndex.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
6 changes: 5 additions & 1 deletion code/lib/telemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const telemetry = async (
payload: Payload = {},
options: Partial<Options> = {}
) => {
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,
Expand Down