66import * as vscode from 'vscode'
77import { getLogger } from '../../shared/logger/logger'
88import * as CodeWhispererConstants from '../models/constants'
9- import globals from '../../shared/extensionGlobals'
109import { vsCodeState } from '../models/model'
11- import { CodewhispererLanguage , telemetry } from '../../shared/telemetry/telemetry'
1210import { runtimeLanguageContext } from '../util/runtimeLanguageContext'
1311import { TelemetryHelper } from '../util/telemetryHelper'
1412import { AuthUtil } from '../util/authUtil'
@@ -27,6 +25,7 @@ export class QCodeGenTracker {
2725 private _serviceInvocationCount : number
2826
2927 static #instance: QCodeGenTracker
28+ static copySnippetThreshold = 50
3029
3130 private constructor ( ) {
3231 this . _totalNewCodeLineCount = 0
@@ -50,39 +49,21 @@ export class QCodeGenTracker {
5049 this . _serviceInvocationCount += 1
5150 }
5251
53- public flush ( ) {
54- if ( ! this . isActive ( ) ) {
55- this . _totalNewCodeLineCount = 0
56- this . _totalNewCodeCharacterCount = 0
57- this . closeTimer ( )
58- return
59- }
60- try {
61- this . emitCodeContribution ( )
62- } catch ( error ) {
63- getLogger ( ) . error ( `Encountered ${ error } when emitting code contribution metric` )
64- }
65- }
66-
6752 public emitCodeContribution ( ) {
6853 const selectedCustomization = getSelectedCustomization ( )
69- if ( this . _serviceInvocationCount <= 0 ) {
70- getLogger ( ) . debug ( `Skip emiting code contribution metric. There is no Amazon Q active usage. ` )
71- return
72- }
7354 client
7455 . sendTelemetryEvent ( {
7556 telemetryEvent : {
7657 codeCoverageEvent : {
7758 customizationArn : selectedCustomization . arn === '' ? undefined : selectedCustomization . arn ,
7859 programmingLanguage : {
79- languageName : runtimeLanguageContext . toRuntimeLanguage ( this . _language ) ,
60+ languageName : 'plaintext' ,
8061 } ,
8162 acceptedCharacterCount : 0 ,
8263 totalCharacterCount : 0 ,
8364 timestamp : new Date ( Date . now ( ) ) ,
84- totalNewCodeCharacterCount : 0 ,
85- totalNewCodeLineCount : 0 ,
65+ totalNewCodeCharacterCount : this . _totalNewCodeCharacterCount ,
66+ totalNewCodeLineCount : this . _totalNewCodeLineCount ,
8667 } ,
8768 } ,
8869 } )
@@ -92,11 +73,8 @@ export class QCodeGenTracker {
9273 if ( isAwsError ( error ) ) {
9374 requestId = error . requestId
9475 }
95-
9676 getLogger ( ) . debug (
97- `Failed to sendTelemetryEvent to CodeWhisperer, requestId: ${ requestId ?? '' } , message: ${
98- error . message
99- } `
77+ `Failed to sendTelemetryEvent, requestId: ${ requestId ?? '' } , message: ${ error . message } `
10078 )
10179 } )
10280 }
@@ -105,24 +83,31 @@ export class QCodeGenTracker {
10583 if ( this . _timer !== undefined ) {
10684 return
10785 }
108- const currentDate = new globals . clock . Date ( )
86+ if ( ! this . isActive ( ) ) {
87+ getLogger ( ) . debug ( `Skip emiting code contribution metric. Telemetry disabled or not logged in. ` )
88+ this . resetTracker ( )
89+ this . closeTimer ( )
90+ return
91+ }
10992 const startTime = performance . now ( )
11093 this . _timer = setTimeout ( ( ) => {
11194 try {
112- const currentTime = new globals . clock . Date ( ) . getTime ( )
95+ const currentTime = performance . now ( )
11396 const delay : number = CodeWhispererConstants . defaultCheckPeriodMillis
11497 const diffTime : number = startTime + delay
11598 if ( diffTime <= currentTime ) {
116- if ( this . _totalNewCodeCharacterCount > 0 ) {
117- this . flush ( )
118- } else {
119- getLogger ( ) . debug (
120- `CodeWhispererCodeCoverageTracker: skipped telemetry due to empty tokens array`
121- )
99+ if ( this . _serviceInvocationCount <= 0 ) {
100+ getLogger ( ) . debug ( `Skip emiting code contribution metric. There is no active Amazon Q usage. ` )
101+ return
102+ }
103+ if ( this . _totalNewCodeCharacterCount === 0 ) {
104+ getLogger ( ) . debug ( `Skip emiting code contribution metric. There is no new code added. ` )
105+ return
122106 }
107+ this . emitCodeContribution ( )
123108 }
124109 } catch ( e ) {
125- getLogger ( ) . verbose ( `Exception Thrown from CodeWhispererCodeCoverageTracker : ${ e } ` )
110+ getLogger ( ) . verbose ( `Exception Thrown from QCodeGenTracker : ${ e } ` )
126111 } finally {
127112 this . resetTracker ( )
128113 this . closeTimer ( )
@@ -131,9 +116,8 @@ export class QCodeGenTracker {
131116 }
132117
133118 private resetTracker ( ) {
134- this . _totalTokens = { }
135- this . _acceptedTokens = { }
136- this . _startTime = 0
119+ this . _totalNewCodeLineCount = 0
120+ this . _totalNewCodeCharacterCount = 0
137121 this . _serviceInvocationCount = 0
138122 }
139123
@@ -153,10 +137,13 @@ export class QCodeGenTracker {
153137 return
154138 }
155139 const contentChange = e . contentChanges [ 0 ]
156- if ( contentChange . text . length > 50 ) {
140+ // if user copies code into the editor for more than 50 characters
141+ // do not count this as total new code, this will skew the data.
142+ if ( contentChange . text . length > QCodeGenTracker . copySnippetThreshold ) {
157143 return
158144 }
159145 this . _totalNewCodeCharacterCount += contentChange . text . length
146+ this . _totalNewCodeLineCount += contentChange . text . split ( '\n' ) . length - 1
160147 // start 5 min data reporting once valid user input is detected
161148 this . tryStartTimer ( )
162149 }
0 commit comments