Skip to content

Commit 0a43005

Browse files
committed
ai code gen
1 parent f5e4144 commit 0a43005

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

packages/core/src/codewhisperer/activation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import { logAndShowError, logAndShowWebviewError } from '../shared/utilities/log
7171
import { openSettings } from '../shared/settings'
7272
import { telemetry } from '../shared/telemetry'
7373
import { FeatureConfigProvider } from '../shared/featureConfig'
74+
import { QCodeGenTracker } from './tracker/qCodeGenTracker'
7475

7576
let localize: nls.LocalizeFunc
7677

@@ -491,7 +492,7 @@ export async function activate(context: ExtContext): Promise<void> {
491492
SecurityIssueCodeActionProvider.instance.handleDocumentChange(e)
492493

493494
CodeWhispererCodeCoverageTracker.getTracker(e.document.languageId)?.countTotalTokens(e)
494-
495+
QCodeGenTracker.instance.onTextDocumentChange(e)
495496
/**
496497
* Handle this keystroke event only when
497498
* 1. It is not a backspace

packages/core/src/codewhisperer/client/user-service-2.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,9 @@
624624
"acceptedCharacterCount": { "shape": "PrimitiveInteger" },
625625
"totalCharacterCount": { "shape": "PrimitiveInteger" },
626626
"timestamp": { "shape": "Timestamp" },
627-
"unmodifiedAcceptedCharacterCount": { "shape": "PrimitiveInteger" }
627+
"unmodifiedAcceptedCharacterCount": { "shape": "PrimitiveInteger" },
628+
"totalNewCodeCharacterCount": { "shape": "PrimitiveInteger" },
629+
"totalNewCodeLineCount": { "shape": "PrimitiveInteger" }
628630
}
629631
},
630632
"CodeFixAcceptanceEvent": {

packages/core/src/codewhisperer/service/recommendationHandler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { openUrl } from '../../shared/utilities/vsCodeUtils'
4444
import { indent } from '../../shared/utilities/textUtilities'
4545
import path from 'path'
4646
import { isIamConnection } from '../../auth/connection'
47+
import { QCodeGenTracker } from '../tracker/qCodeGenTracker'
4748

4849
/**
4950
* This class is for getRecommendation/listRecommendation API calls and its states
@@ -316,6 +317,7 @@ export class RecommendationHandler {
316317
getLogger().debug(msg)
317318
if (invocationResult === 'Succeeded') {
318319
CodeWhispererCodeCoverageTracker.getTracker(session.language)?.incrementServiceInvocationCount()
320+
QCodeGenTracker.instance.onQFeatureInvoked()
319321
} else {
320322
if (
321323
(errorMessage?.includes(invalidCustomizationMessage) && errorCode === 'AccessDeniedException') ||

packages/core/src/codewhisperer/tracker/qCodeGenTracker.ts

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import * as vscode from 'vscode'
77
import { getLogger } from '../../shared/logger/logger'
88
import * as CodeWhispererConstants from '../models/constants'
9-
import globals from '../../shared/extensionGlobals'
109
import { vsCodeState } from '../models/model'
11-
import { CodewhispererLanguage, telemetry } from '../../shared/telemetry/telemetry'
1210
import { runtimeLanguageContext } from '../util/runtimeLanguageContext'
1311
import { TelemetryHelper } from '../util/telemetryHelper'
1412
import { 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
}

packages/core/src/codewhispererChat/clients/chat/v0/chat.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as vscode from 'vscode'
99
import { ToolkitError } from '../../../../shared/errors'
1010
import { createCodeWhispererChatStreamingClient } from '../../../../shared/clients/codewhispererChatClient'
1111
import { createQDeveloperStreamingClient } from '../../../../shared/clients/qDeveloperChatClient'
12+
import { QCodeGenTracker } from '../../../../codewhisperer/tracker/qCodeGenTracker'
1213

1314
export class ChatSession {
1415
private sessionId?: string
@@ -67,6 +68,8 @@ export class ChatSession {
6768

6869
this.sessionId = response.conversationId
6970

71+
QCodeGenTracker.instance.onQFeatureInvoked()
72+
7073
return response
7174
}
7275
}

0 commit comments

Comments
 (0)