|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { IApplicationShell } from '../common/application/types'; |
| 8 | +import '../common/extensions'; |
| 9 | +import { IBrowserService, IPersistentStateFactory, |
| 10 | + IPythonExtensionBanner } from '../common/types'; |
| 11 | +import { getRandomBetween } from '../common/utils'; |
| 12 | + |
| 13 | +// persistent state names, exported to make use of in testing |
| 14 | +export enum LSSurveyStateKeys { |
| 15 | + ShowBanner = 'ShowLSSurveyBanner', |
| 16 | + ShowAttemptCounter = 'LSSurveyShowAttempt', |
| 17 | + ShowAfterCompletionCount = 'LSSurveyShowCount' |
| 18 | +} |
| 19 | + |
| 20 | +enum LSSurveyLabelIndex { |
| 21 | + Yes, |
| 22 | + No |
| 23 | +} |
| 24 | + |
| 25 | +/* |
| 26 | +This class represents a popup that will ask our users for some feedback after |
| 27 | +a specific event occurs N times. |
| 28 | +*/ |
| 29 | +@injectable() |
| 30 | +export class LanguageServerSurveyBanner implements IPythonExtensionBanner { |
| 31 | + private disabledInCurrentSession: boolean = false; |
| 32 | + private minCompletionsBeforeShow: number; |
| 33 | + private maxCompletionsBeforeShow: number; |
| 34 | + private isInitialized: boolean = false; |
| 35 | + private bannerMessage: string = 'Can you please take 2 minutes to tell us how the Experimental Debugger is working for you?'; |
| 36 | + private bannerLabels: string [] = [ 'Yes, take survey now', 'No, thanks']; |
| 37 | + |
| 38 | + constructor( |
| 39 | + @inject(IApplicationShell) private appShell: IApplicationShell, |
| 40 | + @inject(IPersistentStateFactory) private persistentState: IPersistentStateFactory, |
| 41 | + @inject(IBrowserService) private browserService: IBrowserService, |
| 42 | + showAfterMinimumEventsCount: number = 100, |
| 43 | + showBeforeMaximumEventsCount: number = 500) |
| 44 | + { |
| 45 | + this.minCompletionsBeforeShow = showAfterMinimumEventsCount; |
| 46 | + this.maxCompletionsBeforeShow = showBeforeMaximumEventsCount; |
| 47 | + this.initialize(); |
| 48 | + } |
| 49 | + |
| 50 | + public initialize(): void { |
| 51 | + if (this.isInitialized) { |
| 52 | + return; |
| 53 | + } |
| 54 | + this.isInitialized = true; |
| 55 | + |
| 56 | + if (this.minCompletionsBeforeShow >= this.maxCompletionsBeforeShow) { |
| 57 | + this.disable().ignoreErrors(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public get optionLabels(): string[] { |
| 62 | + return this.bannerLabels; |
| 63 | + } |
| 64 | + |
| 65 | + public get shownCount(): Promise<number> { |
| 66 | + return this.getPythonLSLaunchCounter(); |
| 67 | + } |
| 68 | + |
| 69 | + public get enabled(): boolean { |
| 70 | + return this.persistentState.createGlobalPersistentState<boolean>(LSSurveyStateKeys.ShowBanner, true).value; |
| 71 | + } |
| 72 | + |
| 73 | + public async showBanner(): Promise<void> { |
| 74 | + if (!this.enabled || this.disabledInCurrentSession) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const launchCounter: number = await this.incrementPythonLanguageServiceLaunchCounter(); |
| 79 | + const show = await this.shouldShowBanner(launchCounter); |
| 80 | + if (!show) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + const response = await this.appShell.showInformationMessage(this.bannerMessage, ...this.bannerLabels); |
| 85 | + switch (response) { |
| 86 | + case this.bannerLabels[LSSurveyLabelIndex.Yes]: |
| 87 | + { |
| 88 | + await this.launchSurvey(); |
| 89 | + await this.disable(); |
| 90 | + break; |
| 91 | + } |
| 92 | + case this.bannerLabels[LSSurveyLabelIndex.No]: { |
| 93 | + await this.disable(); |
| 94 | + break; |
| 95 | + } |
| 96 | + default: { |
| 97 | + // Disable for the current session. |
| 98 | + this.disabledInCurrentSession = true; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public async shouldShowBanner(launchCounter?: number): Promise<boolean> { |
| 104 | + if (!this.enabled || this.disabledInCurrentSession) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if (! launchCounter) { |
| 109 | + launchCounter = await this.getPythonLSLaunchCounter(); |
| 110 | + } |
| 111 | + const threshold: number = await this.getPythonLSLaunchThresholdCounter(); |
| 112 | + |
| 113 | + return launchCounter >= threshold; |
| 114 | + } |
| 115 | + |
| 116 | + public async disable(): Promise<void> { |
| 117 | + await this.persistentState.createGlobalPersistentState<boolean>(LSSurveyStateKeys.ShowBanner, false).updateValue(false); |
| 118 | + } |
| 119 | + |
| 120 | + public async launchSurvey(): Promise<void> { |
| 121 | + const launchCounter = await this.getPythonLSLaunchCounter(); |
| 122 | + this.browserService.launch(`https://www.research.net/r/LJZV9BZ?n=${launchCounter}`); |
| 123 | + } |
| 124 | + |
| 125 | + private async incrementPythonLanguageServiceLaunchCounter(): Promise<number> { |
| 126 | + const state = this.persistentState.createGlobalPersistentState<number>(LSSurveyStateKeys.ShowAttemptCounter, 0); |
| 127 | + await state.updateValue(state.value + 1); |
| 128 | + return state.value; |
| 129 | + } |
| 130 | + |
| 131 | + private async getPythonLSLaunchCounter(): Promise<number> { |
| 132 | + const state = this.persistentState.createGlobalPersistentState<number>(LSSurveyStateKeys.ShowAttemptCounter, 0); |
| 133 | + return state.value; |
| 134 | + } |
| 135 | + |
| 136 | + private async getPythonLSLaunchThresholdCounter(): Promise<number> { |
| 137 | + const state = this.persistentState.createGlobalPersistentState<number | undefined>(LSSurveyStateKeys.ShowAfterCompletionCount, undefined); |
| 138 | + if (state.value === undefined) { |
| 139 | + await state.updateValue(getRandomBetween(this.minCompletionsBeforeShow, this.maxCompletionsBeforeShow)); |
| 140 | + } |
| 141 | + return state.value!; |
| 142 | + } |
| 143 | +} |
0 commit comments