microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/services/experimentService/experimentService.ts
150lines · modeblame
88671796RedMickey5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
| 4 | import * as vscode from "vscode"; | |
ab0238b7RedMickey4 years ago | 5 | import { TelemetryHelper } from "../../../common/telemetryHelper"; |
| 6 | import { Telemetry } from "../../../common/telemetry"; | |
| 7 | import { ExtensionConfigManager } from "../../extensionConfigManager"; | |
f338085detatanova4 years ago | 8 | import { IConfig, retryDownloadConfig } from "../remoteConfigHelper"; |
09f6024fHeniker4 years ago | 9 | import { IExperiment } from "./IExperiment"; |
88671796RedMickey5 years ago | 10 | |
| 11 | export enum ExperimentStatuses { | |
| 12 | ENABLED = "enabled", | |
| 13 | DISABLED = "disabled", | |
| 14 | FAILED = "failed", | |
| 15 | } | |
| 16 | | |
f338085detatanova4 years ago | 17 | export interface ExperimentConfig extends IConfig { |
88671796RedMickey5 years ago | 18 | experimentName: string; |
| 19 | popCoveragePercent: number; | |
| 20 | enabled: boolean; | |
| 21 | } | |
| 22 | | |
| 23 | export interface ExperimentParameters extends ExperimentConfig { | |
| 24 | [key: string]: any; | |
| 25 | extensionId?: string; | |
| 26 | } | |
| 27 | | |
| 28 | export interface ExperimentResult { | |
| 29 | resultStatus: ExperimentStatuses; | |
| 30 | updatedExperimentParameters: ExperimentParameters; | |
| 31 | error?: Error; | |
| 32 | } | |
| 33 | | |
| 34 | export class ExperimentService implements vscode.Disposable { | |
| 35 | private static instance: ExperimentService; | |
| 36 | | |
| 37 | private readonly endpointURL: string; | |
| 38 | private downloadedExperimentsConfig: Array<ExperimentConfig> | null; | |
| 39 | private experimentsInstances: Map<string, IExperiment>; | |
| 40 | private downloadConfigRequest: Promise<ExperimentConfig[]>; | |
| 41 | private cancellationTokenSource: vscode.CancellationTokenSource; | |
| 42 | | |
34472878RedMickey5 years ago | 43 | public static create(): ExperimentService { |
88671796RedMickey5 years ago | 44 | if (!ExperimentService.instance) { |
| 45 | ExperimentService.instance = new ExperimentService(); | |
| 46 | } | |
| 47 | | |
| 48 | return ExperimentService.instance; | |
| 49 | } | |
| 50 | | |
| 51 | public async runExperiments(): Promise<void> { | |
| 52 | if (!this.downloadedExperimentsConfig) { | |
| 53 | this.downloadedExperimentsConfig = await this.downloadConfigRequest; | |
| 54 | this.experimentsInstances = await this.initializeExperimentsInstances(); | |
| 55 | } | |
| 56 | | |
09f6024fHeniker4 years ago | 57 | const experimentResults: Array<ExperimentResult> = await Promise.all( |
34472878RedMickey5 years ago | 58 | this.downloadedExperimentsConfig.map(expConfig => this.executeExperiment(expConfig)), |
88671796RedMickey5 years ago | 59 | ); |
| 60 | | |
| 61 | this.sendExperimentTelemetry(experimentResults); | |
| 62 | } | |
| 63 | | |
34472878RedMickey5 years ago | 64 | public dispose(): void { |
88671796RedMickey5 years ago | 65 | this.cancellationTokenSource.cancel(); |
| 66 | this.cancellationTokenSource.dispose(); | |
| 67 | } | |
| 68 | | |
| 69 | private constructor() { | |
34472878RedMickey5 years ago | 70 | this.endpointURL = |
| 71 | "https://microsoft.github.io/vscode-react-native/experiments/experimentsConfig.json"; | |
88671796RedMickey5 years ago | 72 | this.cancellationTokenSource = new vscode.CancellationTokenSource(); |
| 73 | this.downloadedExperimentsConfig = null; | |
| 74 | | |
f338085detatanova4 years ago | 75 | this.downloadConfigRequest = retryDownloadConfig<ExperimentConfig[]>( |
| 76 | this.endpointURL, | |
| 77 | this.cancellationTokenSource, | |
| 78 | ); | |
88671796RedMickey5 years ago | 79 | } |
| 80 | | |
| 81 | private async executeExperiment(expConfig: ExperimentConfig): Promise<ExperimentResult> { | |
09f6024fHeniker4 years ago | 82 | const curExperimentParameters = ExtensionConfigManager.config.get(expConfig.experimentName); |
| 83 | const expInstance = this.experimentsInstances.get(expConfig.experimentName); | |
88671796RedMickey5 years ago | 84 | |
| 85 | let expResult: ExperimentResult; | |
| 86 | if (expInstance && expConfig.enabled) { | |
| 87 | try { | |
| 88 | expResult = await expInstance.run(expConfig, curExperimentParameters); | |
155e1318JiglioNero5 years ago | 89 | ExtensionConfigManager.config.set( |
| 90 | expConfig.experimentName, | |
| 91 | expResult.updatedExperimentParameters, | |
| 92 | ); | |
88671796RedMickey5 years ago | 93 | } catch (err) { |
| 94 | expResult = { | |
| 95 | resultStatus: ExperimentStatuses.FAILED, | |
| 96 | updatedExperimentParameters: expConfig, | |
| 97 | error: err, | |
| 98 | }; | |
| 99 | } | |
| 100 | } else { | |
| 101 | expResult = { | |
| 102 | resultStatus: ExperimentStatuses.DISABLED, | |
| 103 | updatedExperimentParameters: expConfig, | |
| 104 | }; | |
| 105 | } | |
| 106 | | |
| 107 | return expResult; | |
| 108 | } | |
| 109 | | |
| 110 | private async initializeExperimentsInstances(): Promise<Map<string, IExperiment>> { | |
09f6024fHeniker4 years ago | 111 | const expInstances = new Map<string, IExperiment>(); |
88671796RedMickey5 years ago | 112 | |
| 113 | if (this.downloadedExperimentsConfig) { | |
09f6024fHeniker4 years ago | 114 | for (const expConfig of this.downloadedExperimentsConfig) { |
88671796RedMickey5 years ago | 115 | try { |
09f6024fHeniker4 years ago | 116 | const expClass = await import(`./experiments/${expConfig.experimentName}`); |
34472878RedMickey5 years ago | 117 | expInstances.set(expConfig.experimentName, new expClass.default()); |
88671796RedMickey5 years ago | 118 | } catch (err) { |
| 119 | expConfig.enabled = false; | |
| 120 | } | |
| 121 | } | |
| 122 | } | |
| 123 | | |
| 124 | return expInstances; | |
| 125 | } | |
| 126 | | |
| 127 | private sendExperimentTelemetry(experimentsResults: ExperimentResult[]): void { | |
| 128 | const runExperimentsEvent = TelemetryHelper.createTelemetryEvent("runExperiments"); | |
| 129 | | |
| 130 | experimentsResults.forEach(expResult => { | |
34472878RedMickey5 years ago | 131 | if (expResult.resultStatus === ExperimentStatuses.FAILED && expResult.error) { |
88671796RedMickey5 years ago | 132 | TelemetryHelper.addTelemetryEventErrorProperty( |
| 133 | runExperimentsEvent, | |
| 134 | expResult.error, | |
| 135 | undefined, | |
34472878RedMickey5 years ago | 136 | `${expResult.updatedExperimentParameters.experimentName}.`, |
88671796RedMickey5 years ago | 137 | ); |
| 138 | } else { | |
| 139 | TelemetryHelper.addTelemetryEventProperty( | |
| 140 | runExperimentsEvent, | |
| 141 | expResult.updatedExperimentParameters.experimentName, | |
| 142 | expResult.resultStatus, | |
34472878RedMickey5 years ago | 143 | false, |
88671796RedMickey5 years ago | 144 | ); |
| 145 | } | |
| 146 | }); | |
| 147 | | |
| 148 | Telemetry.send(runExperimentsEvent); | |
| 149 | } | |
| 150 | } |