@@ -11,12 +11,12 @@ import {
11
11
NodeModulesBuilderInfo ,
12
12
WorkspaceNodeModulesArchitectHost ,
13
13
} from '@angular-devkit/architect/node' ;
14
- import { json } from '@angular-devkit/core' ;
14
+ import { json , JsonValue } from '@angular-devkit/core' ;
15
15
import { spawnSync } from 'child_process' ;
16
16
import { existsSync } from 'fs' ;
17
17
import { resolve } from 'path' ;
18
18
import { isPackageNameSafeForAnalytics } from '../analytics/analytics' ;
19
- import { EventCustomDimension } from '../analytics/analytics-collector ' ;
19
+ import { EventCustomDimension , EventCustomMetric } from '../analytics/analytics-parameters ' ;
20
20
import { assertIsError } from '../utilities/error' ;
21
21
import { askConfirmation , askQuestion } from '../utilities/prompt' ;
22
22
import { isTTY } from '../utilities/tty' ;
@@ -62,18 +62,73 @@ export abstract class ArchitectBaseCommandModule<T extends object>
62
62
? await this . getAnalytics ( )
63
63
: undefined ;
64
64
65
- analytics ?. reportArchitectRunEvent ( {
66
- [ EventCustomDimension . BuilderTarget ] : builderName ,
67
- } ) ;
65
+ let outputSubscription ;
66
+ if ( analytics ) {
67
+ analytics . reportArchitectRunEvent ( {
68
+ [ EventCustomDimension . BuilderTarget ] : builderName ,
69
+ } ) ;
70
+
71
+ let firstRun = true ;
72
+ outputSubscription = run . output . subscribe ( ( { stats } ) => {
73
+ const parameters = this . builderStatsToAnalyticsParameters ( stats , builderName ) ;
74
+ if ( ! parameters ) {
75
+ return ;
76
+ }
68
77
69
- const { error, success } = await run . output . toPromise ( ) ;
70
- await run . stop ( ) ;
78
+ if ( firstRun ) {
79
+ firstRun = false ;
80
+ analytics . reportBuildRunEvent ( parameters ) ;
81
+ } else {
82
+ analytics . reportRebuildRunEvent ( parameters ) ;
83
+ }
84
+ } ) ;
85
+ }
86
+
87
+ try {
88
+ const { error, success } = await run . output . toPromise ( ) ;
71
89
72
- if ( error ) {
73
- logger . error ( error ) ;
90
+ if ( error ) {
91
+ logger . error ( error ) ;
92
+ }
93
+
94
+ return success ? 0 : 1 ;
95
+ } finally {
96
+ await run . stop ( ) ;
97
+ outputSubscription ?. unsubscribe ( ) ;
98
+ }
99
+ }
100
+
101
+ private builderStatsToAnalyticsParameters (
102
+ stats : JsonValue ,
103
+ builderName : string ,
104
+ ) : Partial <
105
+ | Record < EventCustomDimension & EventCustomMetric , string | number | undefined | boolean >
106
+ | undefined
107
+ > {
108
+ if ( ! stats || typeof stats !== 'object' || ! ( 'durationInMs' in stats ) ) {
109
+ return undefined ;
74
110
}
75
111
76
- return success ? 0 : 1 ;
112
+ const {
113
+ optimization,
114
+ allChunksCount,
115
+ aot,
116
+ lazyChunksCount,
117
+ initialChunksCount,
118
+ durationInMs,
119
+ changedChunksCount,
120
+ } = stats ;
121
+
122
+ return {
123
+ [ EventCustomDimension . BuilderTarget ] : builderName ,
124
+ [ EventCustomDimension . Aot ] : aot ,
125
+ [ EventCustomDimension . Optimization ] : optimization ,
126
+ [ EventCustomMetric . AllChunksCount ] : allChunksCount ,
127
+ [ EventCustomMetric . LazyChunksCount ] : lazyChunksCount ,
128
+ [ EventCustomMetric . InitialChunksCount ] : initialChunksCount ,
129
+ [ EventCustomMetric . ChangedChunksCount ] : changedChunksCount ,
130
+ [ EventCustomMetric . DurationInMs ] : durationInMs ,
131
+ } ;
77
132
}
78
133
79
134
private _architectHost : WorkspaceNodeModulesArchitectHost | undefined ;
0 commit comments