forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathdataScienceSurveyBanner.ts
117 lines (100 loc) · 3.92 KB
/
dataScienceSurveyBanner.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import { IApplicationShell } from '../common/application/types';
import '../common/extensions';
import { IBrowserService, IPersistentStateFactory, IPythonExtensionBanner } from '../common/types';
import * as localize from '../common/utils/localize';
export enum DSSurveyStateKeys {
ShowBanner = 'ShowDSSurveyBanner',
ShowAttemptCounter = 'DSSurveyShowAttempt'
}
enum DSSurveyLabelIndex {
Yes,
No
}
@injectable()
export class DataScienceSurveyBanner implements IPythonExtensionBanner {
private disabledInCurrentSession: boolean = false;
private isInitialized: boolean = false;
private bannerMessage: string = localize.DataScienceSurveyBanner.bannerMessage();
private bannerLabels: string[] = [
localize.DataScienceSurveyBanner.bannerLabelYes(),
localize.DataScienceSurveyBanner.bannerLabelNo()
];
private readonly commandThreshold: number;
private readonly surveyLink: string;
constructor(
@inject(IApplicationShell) private appShell: IApplicationShell,
@inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory,
@inject(IBrowserService) private browserService: IBrowserService,
commandThreshold: number = 500,
surveyLink: string = 'https://aka.ms/pyaisurvey'
) {
this.commandThreshold = commandThreshold;
this.surveyLink = surveyLink;
this.initialize();
}
public initialize(): void {
if (this.isInitialized) {
return;
}
this.isInitialized = true;
}
public get enabled(): boolean {
return this.persistentState.createGlobalPersistentState<boolean>(DSSurveyStateKeys.ShowBanner, true).value;
}
public async showBanner(): Promise<void> {
if (!this.enabled || this.disabledInCurrentSession) {
return;
}
const launchCounter: number = await this.incrementPythonDataScienceCommandCounter();
const show = await this.shouldShowBanner(launchCounter);
if (!show) {
return;
}
const response = await this.appShell.showInformationMessage(this.bannerMessage, ...this.bannerLabels);
switch (response) {
case this.bannerLabels[DSSurveyLabelIndex.Yes]: {
await this.launchSurvey();
await this.disable();
break;
}
case this.bannerLabels[DSSurveyLabelIndex.No]: {
await this.disable();
break;
}
default: {
// Disable for the current session.
this.disabledInCurrentSession = true;
}
}
}
public async shouldShowBanner(launchCounter?: number): Promise<boolean> {
if (!this.enabled || this.disabledInCurrentSession) {
return false;
}
if (!launchCounter) {
launchCounter = await this.getPythonDSCommandCounter();
}
return launchCounter >= this.commandThreshold;
}
public async disable(): Promise<void> {
await this.persistentState
.createGlobalPersistentState<boolean>(DSSurveyStateKeys.ShowBanner, false)
.updateValue(false);
}
public async launchSurvey(): Promise<void> {
this.browserService.launch(this.surveyLink);
}
private async getPythonDSCommandCounter(): Promise<number> {
const state = this.persistentState.createGlobalPersistentState<number>(DSSurveyStateKeys.ShowAttemptCounter, 0);
return state.value;
}
private async incrementPythonDataScienceCommandCounter(): Promise<number> {
const state = this.persistentState.createGlobalPersistentState<number>(DSSurveyStateKeys.ShowAttemptCounter, 0);
await state.updateValue(state.value + 1);
return state.value;
}
}