Skip to content

Commit

Permalink
adopt to application scope (#152286)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 authored Jun 16, 2022
1 parent 8559324 commit 6f7c824
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class ExtensionStorageService extends Disposable implements IExtensionSto
return undefined;
}

/* TODO @sandy081: This has to be done across all profiles */
static async removeOutdatedExtensionVersions(extensionManagementService: IExtensionManagementService, storageService: IStorageService): Promise<void> {
const extensions = await extensionManagementService.getInstalled();
const extensionVersionsToRemove: string[] = [];
Expand Down Expand Up @@ -193,7 +194,7 @@ export class ExtensionStorageService extends Disposable implements IExtensionSto
}

private get migrationList(): [string, string][] {
const value = this.storageService.get('extensionStorage.migrationList', StorageScope.GLOBAL, '[]');
const value = this.storageService.get('extensionStorage.migrationList', StorageScope.APPLICATION, '[]');
try {
const migrationList = JSON.parse(value);
if (isArray(migrationList)) {
Expand All @@ -205,9 +206,9 @@ export class ExtensionStorageService extends Disposable implements IExtensionSto

private set migrationList(migrationList: [string, string][]) {
if (migrationList.length) {
this.storageService.store('extensionStorage.migrationList', JSON.stringify(migrationList), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store('extensionStorage.migrationList', JSON.stringify(migrationList), StorageScope.APPLICATION, StorageTarget.MACHINE);
} else {
this.storageService.remove('extensionStorage.migrationList', StorageScope.GLOBAL);
this.storageService.remove('extensionStorage.migrationList', StorageScope.APPLICATION);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/vs/platform/userDataSync/common/globalStateSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ function stringify(globalState: IGlobalState, format: boolean): string {
const GLOBAL_STATE_DATA_VERSION = 1;

/**
* TODO: @sandy081: Sync only global state of default profile
*
* Synchronises global state that includes
* - Global storage with user scope
* - Locale from argv properties
Expand Down
40 changes: 20 additions & 20 deletions src/vs/workbench/contrib/experiments/common/experimentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ export class ExperimentService extends Disposable implements IExperimentService

public markAsCompleted(experimentId: string): void {
const storageKey = 'experiments.' + experimentId;
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.GLOBAL), {});
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.APPLICATION), {});
experimentState.state = ExperimentState.Complete;
this.storageService.store(storageKey, JSON.stringify(experimentState), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(storageKey, JSON.stringify(experimentState), StorageScope.APPLICATION, StorageTarget.MACHINE);
}

protected async getExperiments(): Promise<IRawExperiment[] | null> {
Expand Down Expand Up @@ -255,11 +255,11 @@ export class ExperimentService extends Disposable implements IExperimentService
return this.getExperiments().then(rawExperiments => {
// Offline mode
if (!rawExperiments) {
const allExperimentIdsFromStorage = safeParse(this.storageService.get('allExperiments', StorageScope.GLOBAL), []);
const allExperimentIdsFromStorage = safeParse(this.storageService.get('allExperiments', StorageScope.APPLICATION), []);
if (Array.isArray(allExperimentIdsFromStorage)) {
allExperimentIdsFromStorage.forEach(experimentId => {
const storageKey = 'experiments.' + experimentId;
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.GLOBAL), null);
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.APPLICATION), null);
if (experimentState) {
this._experiments.push({
id: experimentId,
Expand All @@ -278,19 +278,19 @@ export class ExperimentService extends Disposable implements IExperimentService
rawExperiments = rawExperiments.filter(e => (e.schemaVersion || 0) <= currentSchemaVersion);

// Clear disbaled/deleted experiments from storage
const allExperimentIdsFromStorage = safeParse(this.storageService.get('allExperiments', StorageScope.GLOBAL), []);
const allExperimentIdsFromStorage = safeParse(this.storageService.get('allExperiments', StorageScope.APPLICATION), []);
const enabledExperiments = rawExperiments.filter(experiment => !!experiment.enabled).map(experiment => experiment.id.toLowerCase());
if (Array.isArray(allExperimentIdsFromStorage)) {
allExperimentIdsFromStorage.forEach(experiment => {
if (enabledExperiments.indexOf(experiment) === -1) {
this.storageService.remove(`experiments.${experiment}`, StorageScope.GLOBAL);
this.storageService.remove(`experiments.${experiment}`, StorageScope.APPLICATION);
}
});
}
if (enabledExperiments.length) {
this.storageService.store('allExperiments', JSON.stringify(enabledExperiments), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store('allExperiments', JSON.stringify(enabledExperiments), StorageScope.APPLICATION, StorageTarget.MACHINE);
} else {
this.storageService.remove('allExperiments', StorageScope.GLOBAL);
this.storageService.remove('allExperiments', StorageScope.APPLICATION);
}

const activationEvents = new Set(rawExperiments.map(exp => exp.condition?.activationEvent?.event)
Expand Down Expand Up @@ -348,7 +348,7 @@ export class ExperimentService extends Disposable implements IExperimentService
}

const storageKey = 'experiments.' + experiment.id;
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.GLOBAL), {});
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.APPLICATION), {});
if (!experimentState.hasOwnProperty('enabled')) {
experimentState.enabled = processedExperiment.enabled;
}
Expand All @@ -360,7 +360,7 @@ export class ExperimentService extends Disposable implements IExperimentService

return this.shouldRunExperiment(experiment, processedExperiment).then((state: ExperimentState) => {
experimentState.state = processedExperiment.state = state;
this.storageService.store(storageKey, JSON.stringify(experimentState), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(storageKey, JSON.stringify(experimentState), StorageScope.APPLICATION, StorageTarget.MACHINE);

if (state === ExperimentState.Run) {
this.fireRunExperiment(processedExperiment);
Expand All @@ -372,22 +372,22 @@ export class ExperimentService extends Disposable implements IExperimentService

private fireRunExperiment(experiment: IExperiment) {
this._onExperimentEnabled.fire(experiment);
const runExperimentIdsFromStorage: string[] = safeParse(this.storageService.get('currentOrPreviouslyRunExperiments', StorageScope.GLOBAL), []);
const runExperimentIdsFromStorage: string[] = safeParse(this.storageService.get('currentOrPreviouslyRunExperiments', StorageScope.APPLICATION), []);
if (runExperimentIdsFromStorage.indexOf(experiment.id) === -1) {
runExperimentIdsFromStorage.push(experiment.id);
}

// Ensure we dont store duplicates
const distinctExperiments = distinct(runExperimentIdsFromStorage);
if (runExperimentIdsFromStorage.length !== distinctExperiments.length) {
this.storageService.store('currentOrPreviouslyRunExperiments', JSON.stringify(distinctExperiments), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store('currentOrPreviouslyRunExperiments', JSON.stringify(distinctExperiments), StorageScope.APPLICATION, StorageTarget.MACHINE);
}
}

private checkExperimentDependencies(experiment: IRawExperiment): boolean {
const experimentsPreviouslyRun = experiment.condition?.experimentsPreviouslyRun;
if (experimentsPreviouslyRun) {
const runExperimentIdsFromStorage: string[] = safeParse(this.storageService.get('currentOrPreviouslyRunExperiments', StorageScope.GLOBAL), []);
const runExperimentIdsFromStorage: string[] = safeParse(this.storageService.get('currentOrPreviouslyRunExperiments', StorageScope.APPLICATION), []);
let includeCheck = true;
let excludeCheck = true;
const includes = experimentsPreviouslyRun.includes;
Expand All @@ -407,9 +407,9 @@ export class ExperimentService extends Disposable implements IExperimentService

private recordActivatedEvent(event: string) {
const key = experimentEventStorageKey(event);
const record = getCurrentActivationRecord(safeParse(this.storageService.get(key, StorageScope.GLOBAL), undefined));
const record = getCurrentActivationRecord(safeParse(this.storageService.get(key, StorageScope.APPLICATION), undefined));
record.count[0]++;
this.storageService.store(key, JSON.stringify(record), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(key, JSON.stringify(record), StorageScope.APPLICATION, StorageTarget.MACHINE);

this._experiments
.filter(e => {
Expand All @@ -434,7 +434,7 @@ export class ExperimentService extends Disposable implements IExperimentService

const events = typeof setting.event === 'string' ? [setting.event] : setting.event;
for (const event of events) {
const { count } = getCurrentActivationRecord(safeParse(this.storageService.get(experimentEventStorageKey(event), StorageScope.GLOBAL), undefined));
const { count } = getCurrentActivationRecord(safeParse(this.storageService.get(experimentEventStorageKey(event), StorageScope.APPLICATION), undefined));

for (const entry of count) {
if (entry > 0) {
Expand Down Expand Up @@ -532,7 +532,7 @@ export class ExperimentService extends Disposable implements IExperimentService
}

const storageKey = 'experiments.' + experiment.id;
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.GLOBAL), {});
const experimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.APPLICATION), {});

return extensionsCheckPromise.then(success => {
const fileEdits = condition.fileEdits;
Expand All @@ -549,7 +549,7 @@ export class ExperimentService extends Disposable implements IExperimentService
// Process model-save event every 250ms to reduce load
const onModelsSavedWorker = this._register(new RunOnceWorker<ITextFileEditorModel>(models => {
const date = new Date().toDateString();
const latestExperimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.GLOBAL), {});
const latestExperimentState: IExperimentStorageState = safeParse(this.storageService.get(storageKey, StorageScope.APPLICATION), {});
if (latestExperimentState.state !== ExperimentState.Evaluating) {
onSaveHandler.dispose();
onModelsSavedWorker.dispose();
Expand Down Expand Up @@ -579,12 +579,12 @@ export class ExperimentService extends Disposable implements IExperimentService
if (filePathCheck && workspaceCheck) {
latestExperimentState.editCount = (latestExperimentState.editCount || 0) + 1;
latestExperimentState.lastEditedDate = date;
this.storageService.store(storageKey, JSON.stringify(latestExperimentState), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(storageKey, JSON.stringify(latestExperimentState), StorageScope.APPLICATION, StorageTarget.MACHINE);
}
});
if (typeof latestExperimentState.editCount === 'number' && latestExperimentState.editCount >= fileEdits.minEditCount) {
processedExperiment.state = latestExperimentState.state = (typeof condition.userProbability === 'number' && Math.random() < condition.userProbability && this.checkExperimentDependencies(experiment)) ? ExperimentState.Run : ExperimentState.NoRun;
this.storageService.store(storageKey, JSON.stringify(latestExperimentState), StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(storageKey, JSON.stringify(latestExperimentState), StorageScope.APPLICATION, StorageTarget.MACHINE);
if (latestExperimentState.state === ExperimentState.Run && processedExperiment.action && ExperimentActionType[processedExperiment.action.type] === ExperimentActionType.Prompt) {
this.fireRunExperiment(processedExperiment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ suite('Experiment Service', () => {
didGetCall = true;
assert.strictEqual(key, 'experimentEventRecord-my-event');
assert.deepStrictEqual(JSON.parse(value).count, [1, 0, 10, 0, 0, 0, 0]);
assert.strictEqual(scope, StorageScope.GLOBAL);
assert.strictEqual(scope, StorageScope.APPLICATION);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ export class ExtensionsWorkbenchService extends Disposable implements IExtension
}).then(undefined, error => this.onError(error));
}


/* TODO: @sandy081 Extension version shall be moved to extensions.json file */
private _ignoredAutoUpdateExtensions: string[] | undefined;
private get ignoredAutoUpdateExtensions(): string[] {
if (!this._ignoredAutoUpdateExtensions) {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/userDataSync/browser/userDataSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class UserDataSyncWorkbenchContribution extends Disposable implements IWo
if (requiresInitialization && !this.userDataSyncEnablementService.isEnabled()) {
this.updateSyncAfterInitializationContext(true);
} else {
this.updateSyncAfterInitializationContext(this.storageService.getBoolean(CONTEXT_SYNC_AFTER_INITIALIZATION.key, StorageScope.GLOBAL, false));
this.updateSyncAfterInitializationContext(this.storageService.getBoolean(CONTEXT_SYNC_AFTER_INITIALIZATION.key, StorageScope.APPLICATION, false));
}
const disposable = this._register(this.userDataSyncEnablementService.onDidChangeEnablement(() => {
if (this.userDataSyncEnablementService.isEnabled()) {
Expand All @@ -201,7 +201,7 @@ export class UserDataSyncWorkbenchContribution extends Disposable implements IWo
}

private async updateSyncAfterInitializationContext(value: boolean): Promise<void> {
this.storageService.store(CONTEXT_SYNC_AFTER_INITIALIZATION.key, value, StorageScope.GLOBAL, StorageTarget.MACHINE);
this.storageService.store(CONTEXT_SYNC_AFTER_INITIALIZATION.key, value, StorageScope.APPLICATION, StorageTarget.MACHINE);
this.syncAfterInitializationContext.set(value);
this.updateGlobalActivityBadge();
}
Expand Down

0 comments on commit 6f7c824

Please sign in to comment.