forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedbackService.ts
More file actions
131 lines (124 loc) · 5.07 KB
/
feedbackService.ts
File metadata and controls
131 lines (124 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as child_process from 'child_process';
import * as os from 'os';
import { window } from 'vscode';
import { commands, Disposable, TextDocument, workspace } from 'vscode';
import { PythonLanguage } from '../common/constants';
import { IPersistentStateFactory, PersistentState } from '../common/persistentState';
import { FEEDBACK } from '../telemetry/constants';
import { captureTelemetry, sendTelemetryEvent } from '../telemetry/index';
import { FeedbackCounters } from './counters';
const FEEDBACK_URL = 'https://www.surveymonkey.com/r/293C9HY';
export class FeedbackService implements Disposable {
private counters?: FeedbackCounters;
private showFeedbackPrompt: PersistentState<boolean>;
private userResponded: PersistentState<boolean>;
private promptDisplayed: boolean;
private disposables: Disposable[] = [];
private get canShowPrompt(): boolean {
return this.showFeedbackPrompt.value && !this.userResponded.value &&
!this.promptDisplayed && this.counters !== undefined;
}
constructor(persistentStateFactory: IPersistentStateFactory) {
this.showFeedbackPrompt = persistentStateFactory.createGlobalPersistentState('SHOW_FEEDBACK_PROMPT', true);
this.userResponded = persistentStateFactory.createGlobalPersistentState('RESPONDED_TO_FEEDBACK', false);
this.initialize();
}
public dispose() {
this.counters = undefined;
this.disposables.forEach(disposable => {
// tslint:disable-next-line:no-unsafe-any
disposable.dispose();
});
this.disposables = [];
}
private initialize() {
// tslint:disable-next-line:no-void-expression
let commandDisable = commands.registerCommand('python.updateFeedbackCounter', (telemetryEventName: string) => this.updateFeedbackCounter(telemetryEventName));
this.disposables.push(commandDisable);
if (!this.showFeedbackPrompt.value || this.userResponded.value) {
return;
}
// tslint:disable-next-line:no-void-expression
commandDisable = workspace.onDidChangeTextDocument(changeEvent => this.handleChangesToTextDocument(changeEvent.document), this, this.disposables);
this.disposables.push(commandDisable);
this.counters = new FeedbackCounters();
this.counters.on('thresholdReached', () => {
this.thresholdHandler();
});
}
private handleChangesToTextDocument(textDocument: TextDocument) {
if (textDocument.languageId !== PythonLanguage.language) {
return;
}
if (!this.canShowPrompt) {
return;
}
// tslint:disable-next-line:no-non-null-assertion
this.counters!.incrementEditCounter();
}
private updateFeedbackCounter(telemetryEventName: string): void {
// Ignore feedback events.
if (telemetryEventName === FEEDBACK) {
return;
}
if (!this.canShowPrompt) {
return;
}
// tslint:disable-next-line:no-non-null-assertion
this.counters!.incrementFeatureUsageCounter();
}
private thresholdHandler() {
if (!this.canShowPrompt) {
return;
}
this.showPrompt();
}
private showPrompt() {
this.promptDisplayed = true;
const message = 'Would you tell us how likely you are to recommend the Microsoft Python extension for VS Code to a friend or colleague?';
const yesButton = 'Yes';
const dontShowAgainButton = 'Don\'t Show Again';
window.showInformationMessage(message, yesButton, dontShowAgainButton).then((value) => {
switch (value) {
case yesButton: {
this.displaySurvey();
break;
}
case dontShowAgainButton: {
this.doNotShowFeedbackAgain();
break;
}
default: {
sendTelemetryEvent(FEEDBACK, undefined, { action: 'dismissed' });
break;
}
}
// Stop everything for this session.
this.dispose();
});
}
@captureTelemetry(FEEDBACK, { action: 'accepted' })
private displaySurvey() {
this.userResponded.value = true;
let openCommand: string | undefined;
if (os.platform() === 'win32') {
openCommand = 'explorer';
} else if (os.platform() === 'darwin') {
openCommand = '/usr/bin/open';
} else {
openCommand = '/usr/bin/xdg-open';
}
if (!openCommand) {
console.error(`Unable to determine platform to capture user feedback in Python extension ${os.platform()}`);
console.error(`Survey link is: ${FEEDBACK_URL}`);
}
child_process.spawn(openCommand, [FEEDBACK_URL]);
}
@captureTelemetry(FEEDBACK, { action: 'doNotShowAgain' })
private doNotShowFeedbackAgain() {
this.showFeedbackPrompt.value = false;
}
}