|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, named } from 'inversify'; |
| 7 | +import { Memento } from 'vscode'; |
| 8 | +import { getExperimentationService, IExperimentationService, TargetPopulation } from 'vscode-tas-client'; |
| 9 | +import { sendTelemetryEvent } from '../../telemetry'; |
| 10 | +import { EventName } from '../../telemetry/constants'; |
| 11 | +import { IApplicationEnvironment } from '../application/types'; |
| 12 | +import { GLOBAL_MEMENTO, IConfigurationService, IExperimentService, IMemento, IPythonSettings } from '../types'; |
| 13 | +import { ExperimentationTelemetry } from './telemetry'; |
| 14 | + |
| 15 | +export class ExperimentService implements IExperimentService { |
| 16 | + /** |
| 17 | + * Experiments the user requested to opt into manually. |
| 18 | + */ |
| 19 | + public _optInto: string[] = []; |
| 20 | + /** |
| 21 | + * Experiments the user requested to opt out from manually. |
| 22 | + */ |
| 23 | + public _optOutFrom: string[] = []; |
| 24 | + |
| 25 | + private readonly experimentationService?: IExperimentationService; |
| 26 | + private readonly settings: IPythonSettings; |
| 27 | + |
| 28 | + constructor( |
| 29 | + @inject(IConfigurationService) readonly configurationService: IConfigurationService, |
| 30 | + @inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment, |
| 31 | + @inject(IMemento) @named(GLOBAL_MEMENTO) readonly globalState: Memento |
| 32 | + ) { |
| 33 | + this.settings = configurationService.getSettings(undefined); |
| 34 | + |
| 35 | + // Users can only opt in or out of experiment groups, not control groups. |
| 36 | + const optInto = this.settings.experiments.optInto; |
| 37 | + const optOutFrom = this.settings.experiments.optOutFrom; |
| 38 | + this._optInto = optInto.filter((exp) => !exp.endsWith('control')); |
| 39 | + this._optOutFrom = optOutFrom.filter((exp) => !exp.endsWith('control')); |
| 40 | + |
| 41 | + // Don't initialize the experiment service if the extension's experiments setting is disabled. |
| 42 | + const enabled = this.settings.experiments.enabled; |
| 43 | + if (!enabled) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + let targetPopulation: TargetPopulation; |
| 48 | + |
| 49 | + if (this.appEnvironment.channel === 'insiders') { |
| 50 | + targetPopulation = TargetPopulation.Insiders; |
| 51 | + } else { |
| 52 | + targetPopulation = TargetPopulation.Public; |
| 53 | + } |
| 54 | + |
| 55 | + const telemetryReporter = new ExperimentationTelemetry(); |
| 56 | + |
| 57 | + this.experimentationService = getExperimentationService( |
| 58 | + this.appEnvironment.extensionName, |
| 59 | + this.appEnvironment.packageJson.version!, |
| 60 | + targetPopulation, |
| 61 | + telemetryReporter, |
| 62 | + globalState |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public async inExperiment(experiment: string): Promise<boolean> { |
| 67 | + if (!this.experimentationService) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // Currently the service doesn't support opting in and out of experiments, |
| 72 | + // so we need to perform these checks and send the corresponding telemetry manually. |
| 73 | + if (this._optOutFrom.includes('All') || this._optOutFrom.includes(experiment)) { |
| 74 | + sendTelemetryEvent(EventName.PYTHON_EXPERIMENTS_OPT_IN_OUT, undefined, { |
| 75 | + expNameOptedOutOf: experiment |
| 76 | + }); |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (this._optInto.includes('All') || this._optInto.includes(experiment)) { |
| 82 | + sendTelemetryEvent(EventName.PYTHON_EXPERIMENTS_OPT_IN_OUT, undefined, { |
| 83 | + expNameOptedInto: experiment |
| 84 | + }); |
| 85 | + |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + return this.experimentationService.isCachedFlightEnabled(experiment); |
| 90 | + } |
| 91 | +} |
0 commit comments