Skip to content

Commit

Permalink
fix(@angular/cli): add workspace information as part of analytics col…
Browse files Browse the repository at this point in the history
…lection

With this change we collect 3 additional metrics
- `all_projects_count` Count of all project in a workspace
- `libs_projects_count` Count of library projects in a workspace
- `apps_projects_count` Count of application projects in a workspace

(cherry picked from commit c59c1e7)
  • Loading branch information
alan-agius4 committed Oct 14, 2022
1 parent de467f4 commit af07aa3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
3 changes: 3 additions & 0 deletions docs/design/analytics.md
Expand Up @@ -79,6 +79,9 @@ PROJECT NAME TO BUILD OR A MODULE NAME.**
| CssSizeInBytes | `epn.ng_css_size_bytes` | `number` |
| JsSizeInBytes | `epn.ng_js_size_bytes` | `number` |
| NgComponentCount | `epn.ng_component_count` | `number` |
| AllProjectsCount | `epn.all_projects_count` | `number` |
| LibraryProjectsCount | `epn.libs_projects_count` | `number` |
| ApplicationProjectsCount | `epn.apps_projects_count` | `number` |
<!--METRICS_TABLE_END-->

## Debugging
Expand Down
6 changes: 6 additions & 0 deletions packages/angular/cli/src/analytics/analytics-collector.ts
Expand Up @@ -72,6 +72,12 @@ export class AnalyticsCollector {
};
}

reportWorkspaceInfoEvent(
parameters: Partial<Record<EventCustomMetric, string | boolean | number | undefined>>,
): void {
this.event('workspace_info', parameters);
}

reportRebuildRunEvent(
parameters: Partial<
Record<EventCustomMetric & EventCustomDimension, string | boolean | number | undefined>
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/cli/src/analytics/analytics-parameters.ts
Expand Up @@ -97,4 +97,7 @@ export enum EventCustomMetric {
CssSizeInBytes = 'epn.ng_css_size_bytes',
JsSizeInBytes = 'epn.ng_js_size_bytes',
NgComponentCount = 'epn.ng_component_count',
AllProjectsCount = 'epn.all_projects_count',
LibraryProjectsCount = 'epn.libs_projects_count',
ApplicationProjectsCount = 'epn.apps_projects_count',
}
60 changes: 45 additions & 15 deletions packages/angular/cli/src/command-builder/command-module.ts
Expand Up @@ -146,22 +146,9 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI

let exitCode: number | void | undefined;
try {
// Run and time command.
if (analytics) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const internalMethods = (yargs as any).getInternalMethods();
// $0 generate component [name] -> generate_component
// $0 add <collection> -> add
const fullCommand = (internalMethods.getUsageInstance().getUsage()[0][0] as string)
.split(' ')
.filter((x) => {
const code = x.charCodeAt(0);

return code >= 97 && code <= 122;
})
.join('_');

analytics.reportCommandRunEvent(fullCommand);
this.reportCommandRunAnalytics(analytics);
this.reportWorkspaceInfoAnalytics(analytics);
}

exitCode = await this.run(camelCasedOptions as Options<T> & OtherOptions);
Expand Down Expand Up @@ -307,6 +294,49 @@ export abstract class CommandModule<T extends {} = {}> implements CommandModuleI

return parameters;
}

private reportCommandRunAnalytics(analytics: AnalyticsCollector): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const internalMethods = (yargs as any).getInternalMethods();
// $0 generate component [name] -> generate_component
// $0 add <collection> -> add
const fullCommand = (internalMethods.getUsageInstance().getUsage()[0][0] as string)
.split(' ')
.filter((x) => {
const code = x.charCodeAt(0);

return code >= 97 && code <= 122;
})
.join('_');

analytics.reportCommandRunEvent(fullCommand);
}

private reportWorkspaceInfoAnalytics(analytics: AnalyticsCollector): void {
const { workspace } = this.context;
if (!workspace) {
return;
}

let applicationProjectsCount = 0;
let librariesProjectsCount = 0;
for (const project of workspace.projects.values()) {
switch (project.extensions['projectType']) {
case 'application':
applicationProjectsCount++;
break;
case 'library':
librariesProjectsCount++;
break;
}
}

analytics.reportWorkspaceInfoEvent({
[EventCustomMetric.AllProjectsCount]: librariesProjectsCount + applicationProjectsCount,
[EventCustomMetric.ApplicationProjectsCount]: applicationProjectsCount,
[EventCustomMetric.LibraryProjectsCount]: librariesProjectsCount,
});
}
}

/**
Expand Down

0 comments on commit af07aa3

Please sign in to comment.