Merge "Block navigation until reply is sent"
diff --git a/polygerrit-ui/app/constants/reporting.ts b/polygerrit-ui/app/constants/reporting.ts
index ef2f63e..f57afca 100644
--- a/polygerrit-ui/app/constants/reporting.ts
+++ b/polygerrit-ui/app/constants/reporting.ts
@@ -136,4 +136,14 @@
   LINK_CLICK = 'link-click',
   USER_ACTIVE = 'user-active',
   USER_PASSIVE = 'user-passive',
+  // User added generated suggestion to comment
+  GENERATE_SUGGESTION_ADDED = 'generate_suggestion_added',
+  // Request for generating suggestion (usually after user typed draft comment)
+  GENERATE_SUGGESTION_REQUEST = 'generate_suggestion_request',
+  // Response with suggestions
+  GENERATE_SUGGESTION_RESPONSE = 'generate_suggestion_response',
+  // User enabled generating suggestions (enabled is default)
+  GENERATE_SUGGESTION_ENABLED = 'generate_suggestion_enabled',
+  // User disabled generating suggestions
+  GENERATE_SUGGESTION_DISABLED = 'generate_suggestion_disabled',
 }
diff --git a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
index 9661547..0266610 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
@@ -52,7 +52,7 @@
   ValueChangedEvent,
 } from '../../../types/events';
 import {fire} from '../../../utils/event-util';
-import {assertIsDefined, assert} from '../../../utils/common-util';
+import {assertIsDefined, assert, uuid} from '../../../utils/common-util';
 import {Key, Modifier, whenVisible} from '../../../utils/dom-util';
 import {commentsModelToken} from '../../../models/comments/comments-model';
 import {sharedStyles} from '../../../styles/shared-styles';
@@ -75,6 +75,7 @@
   commentModelToken,
 } from '../gr-comment-model/gr-comment-model';
 import {formStyles} from '../../../styles/form-styles';
+import {Interaction} from '../../../constants/reporting';
 
 // visible for testing
 export const AUTO_SAVE_DEBOUNCE_DELAY_MS = 2000;
@@ -187,9 +188,6 @@
   autoSaving?: Promise<DraftInfo>;
 
   @state()
-  generatedReplacement?: string;
-
-  @state()
   changeNum?: NumericChangeId;
 
   @state()
@@ -209,6 +207,12 @@
   @state()
   generateSuggestion = true;
 
+  @state()
+  generatedReplacement?: string;
+
+  @state()
+  generatedReplacementId?: string;
+
   @property({type: Boolean, attribute: 'show-patchset'})
   showPatchset = false;
 
@@ -576,6 +580,7 @@
               html`<gr-suggestion-diff-preview
                 .showAddSuggestionButton=${true}
                 .suggestion=${this.generatedReplacement}
+                .uuid=${this.generatedReplacementId}
               ></gr-suggestion-diff-preview>`
           )}
         </div>
@@ -943,6 +948,11 @@
               } else {
                 this.generateSuggestionTrigger$.next();
               }
+              this.reporting.reportInteraction(
+                this.generateSuggestion
+                  ? Interaction.GENERATE_SUGGESTION_ENABLED
+                  : Interaction.GENERATE_SUGGESTION_DISABLED
+              );
             }}
           />
           Generate Suggestion${numberOfSuggestions}
@@ -964,6 +974,10 @@
     if (suggestionsPlugins.length === 0) return;
     if (!this.changeNum || !this.comment?.patch_set || !this.comments?.[0].path)
       return;
+    this.generatedReplacementId = uuid();
+    this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_REQUEST, {
+      uuid: this.generatedReplacementId,
+    });
     const suggestion = await suggestionsPlugins[0].provider.suggestCode({
       prompt: this.messageText,
       changeNumber: this.changeNum,
@@ -972,6 +986,10 @@
       range: this.comments?.[0].range,
       lineNumber: this.comments?.[0].line,
     });
+    this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_RESPONSE, {
+      uuid: this.generatedReplacementId,
+      response: suggestion.responseCode,
+    });
     const replacement = suggestion.suggestions?.[0]?.replacement;
     if (!replacement) return;
     this.generatedReplacement = replacement;
diff --git a/polygerrit-ui/app/elements/shared/gr-suggestion-diff-preview/gr-suggestion-diff-preview.ts b/polygerrit-ui/app/elements/shared/gr-suggestion-diff-preview/gr-suggestion-diff-preview.ts
index 00b5ba3..64be38b 100644
--- a/polygerrit-ui/app/elements/shared/gr-suggestion-diff-preview/gr-suggestion-diff-preview.ts
+++ b/polygerrit-ui/app/elements/shared/gr-suggestion-diff-preview/gr-suggestion-diff-preview.ts
@@ -28,6 +28,7 @@
 import {KnownExperimentId} from '../../../services/flags/flags';
 import {commentModelToken} from '../gr-comment-model/gr-comment-model';
 import {fire} from '../../../utils/event-util';
+import {Interaction, Timing} from '../../../constants/reporting';
 
 declare global {
   interface HTMLElementEventMap {
@@ -49,6 +50,9 @@
   @property({type: Boolean})
   showAddSuggestionButton = false;
 
+  @property({type: String})
+  uuid?: string;
+
   @state()
   comment?: Comment;
 
@@ -77,6 +81,8 @@
     hide_line_length_indicator: true,
   };
 
+  private readonly reporting = getAppContext().reportingService;
+
   private readonly getChangeModel = resolve(this, changeModelToken);
 
   private readonly restApiService = getAppContext().restApiService;
@@ -210,12 +216,12 @@
       !this.commentedText
     )
       return;
-
     const fixSuggestions = createUserFixSuggestion(
       this.comment,
       this.commentedText,
       this.suggestion
     );
+    this.reporting.time(Timing.PREVIEW_FIX_LOAD);
     const res = await this.restApiService.getFixPreview(
       this.changeNum,
       this.comment?.patch_set,
@@ -225,6 +231,9 @@
     const currentPreviews = Object.keys(res).map(key => {
       return {filepath: key, preview: res[key]};
     });
+    this.reporting.timeEnd(Timing.PREVIEW_FIX_LOAD, {
+      uuid: this.uuid,
+    });
     if (currentPreviews.length > 0) {
       this.preview = currentPreviews[0];
       this.previewLoadedFor = this.suggestion;
@@ -245,6 +254,9 @@
 
   handleAddGeneratedSuggestion() {
     if (!this.suggestion) return;
+    this.reporting.reportInteraction(Interaction.GENERATE_SUGGESTION_ADDED, {
+      uuid: this.uuid,
+    });
     fire(this, 'add-generated-suggestion', {code: this.suggestion});
   }
 }