forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabTesting.ts
More file actions
110 lines (95 loc) · 5.06 KB
/
Copy pathabTesting.ts
File metadata and controls
110 lines (95 loc) · 5.06 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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as util from './common';
import * as Telemetry from './telemetry';
import { PersistentState } from './LanguageServer/persistentState';
import * as fs from 'fs';
const userBucketMax: number = 100;
const userBucketString: string = "CPP.UserBucket";
const localConfigFile: string = "cpptools.json";
interface Settings {
defaultIntelliSenseEngine?: number;
recursiveIncludes?: number;
gotoDefIntelliSense?: number;
}
export class ABTestSettings {
private settings: Settings;
private intelliSenseEngineDefault: PersistentState<number>;
private recursiveIncludesDefault: PersistentState<number>;
private gotoDefIntelliSenseDefault: PersistentState<number>;
private bucket: PersistentState<number>;
constructor() {
this.intelliSenseEngineDefault = new PersistentState<number>("ABTest.1", 100);
this.recursiveIncludesDefault = new PersistentState<number>("ABTest.2", 100);
this.gotoDefIntelliSenseDefault = new PersistentState<number>("ABTest.3", 100);
this.settings = {
defaultIntelliSenseEngine: this.intelliSenseEngineDefault.Value,
recursiveIncludes: this.recursiveIncludesDefault.Value,
gotoDefIntelliSense: this.gotoDefIntelliSenseDefault.Value
};
this.bucket = new PersistentState<number>(userBucketString, -1);
if (this.bucket.Value === -1) {
this.bucket.Value = Math.floor(Math.random() * userBucketMax) + 1; // Range is [1, userBucketMax].
}
this.updateSettings();
// Redownload cpptools.json after initialization so it's not blocked.
// It'll be used the next time the extension reloads.
this.downloadCpptoolsJsonPkgAsync();
// Redownload occasionally to prevent an extra reload during long sessions.
setInterval(() => { this.downloadCpptoolsJsonPkgAsync(); }, 30 * 60 * 1000); // 30 minutes.
}
public get UseDefaultIntelliSenseEngine(): boolean {
return util.isNumber(this.settings.defaultIntelliSenseEngine) ? this.settings.defaultIntelliSenseEngine >= this.bucket.Value : true;
}
public get UseRecursiveIncludes(): boolean {
return util.isNumber(this.settings.recursiveIncludes) ? this.settings.recursiveIncludes >= this.bucket.Value : true;
}
public get UseGoToDefIntelliSense(): boolean {
return util.isNumber(this.settings.gotoDefIntelliSense) ? this.settings.gotoDefIntelliSense >= this.bucket.Value : true;
}
private updateSettings(): void {
const cpptoolsJsonFile: string = util.getExtensionFilePath(localConfigFile);
try {
const exists: boolean = fs.existsSync(cpptoolsJsonFile);
if (exists) {
const fileContent: string = fs.readFileSync(cpptoolsJsonFile).toString();
let newSettings: Settings = <Settings>JSON.parse(fileContent);
this.intelliSenseEngineDefault.Value = util.isNumber(newSettings.defaultIntelliSenseEngine) ? newSettings.defaultIntelliSenseEngine : this.intelliSenseEngineDefault.DefaultValue;
this.recursiveIncludesDefault.Value = util.isNumber(newSettings.recursiveIncludes) ? newSettings.recursiveIncludes : this.recursiveIncludesDefault.DefaultValue;
this.gotoDefIntelliSenseDefault.Value = util.isNumber(newSettings.gotoDefIntelliSense) ? newSettings.gotoDefIntelliSense : this.gotoDefIntelliSenseDefault.DefaultValue;
this.settings = {
defaultIntelliSenseEngine: this.intelliSenseEngineDefault.Value,
recursiveIncludes: this.recursiveIncludesDefault.Value,
gotoDefIntelliSense: this.gotoDefIntelliSenseDefault.Value
};
}
} catch (error) {
// Ignore any cpptoolsJsonFile errors
}
}
private downloadCpptoolsJsonPkgAsync(): Promise<void> {
let hasError: boolean = false;
let telemetryProperties: { [key: string]: string } = {};
const localConfigPath: string = util.getExtensionFilePath(localConfigFile);
return util.downloadFileToDestination("https://go.microsoft.com/fwlink/?linkid=2026205", localConfigPath)
.catch((error) => {
// More specific error info is not likely to be helpful, and we get detailed download data from the initial install.
hasError = true;
})
.then(() => {
this.updateSettings();
telemetryProperties['success'] = (!hasError).toString();
Telemetry.logDebuggerEvent("cpptoolsJsonDownload", telemetryProperties);
});
}
}
let settings: ABTestSettings;
export function getABTestSettings(): ABTestSettings {
if (!settings) {
settings = new ABTestSettings();
}
return settings;
}