Skip to content

Commit

Permalink
Merge pull request #1935 from rosahbruno/1894064-remove-migration-code
Browse files Browse the repository at this point in the history
Bug 1894064: remove migration code
  • Loading branch information
rosahbruno committed May 6, 2024
2 parents da7a7fd + a5c41e9 commit f265d15
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 148 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.1...main)

* [#1935](https://github.com/mozilla/glean.js/pull/1935): **BREAKING CHANGE**: Remove `migrateFromLegacyStorage` capability and configuration option. If your project currently sets the `migrateFromLegacyStorage` value, this will no longer work.

# v5.0.1 (2024-04-30)

[Full changelog](https://github.com/mozilla/glean.js/compare/v5.0.0...v5.0.1)
Expand Down
5 changes: 0 additions & 5 deletions glean/src/core/config.ts
Expand Up @@ -44,9 +44,6 @@ export interface ConfigurationInterface {
httpClient?: Uploader,
// The build date, provided by glean_parser
readonly buildDate?: Date,
// Migrate from legacy storage (IndexedDB) to the new one (LocalStorage).
// This should only be true for older projects that have existing data in IndexedDB.
readonly migrateFromLegacyStorage?: boolean,
// Allow the client to explicitly specify whether they want page load events to be
// collected automatically.
readonly enableAutoPageLoadEvents?: boolean,
Expand All @@ -67,7 +64,6 @@ export class Configuration implements ConfigurationInterface {
readonly serverEndpoint: string;
readonly buildDate?: Date;
readonly maxEvents: number;
readonly migrateFromLegacyStorage?: boolean;
readonly enableAutoPageLoadEvents?: boolean;
readonly enableAutoElementClickEvents?: boolean;
experimentationId?: string;
Expand All @@ -84,7 +80,6 @@ export class Configuration implements ConfigurationInterface {
this.appDisplayVersion = config?.appDisplayVersion;
this.buildDate = config?.buildDate;
this.maxEvents = config?.maxEvents || DEFAULT_MAX_EVENTS;
this.migrateFromLegacyStorage = config?.migrateFromLegacyStorage;
this.enableAutoPageLoadEvents = config?.enableAutoPageLoadEvents;
this.enableAutoElementClickEvents = config?.enableAutoElementClickEvents;
this.experimentationId = config?.experimentationId;
Expand Down
13 changes: 2 additions & 11 deletions glean/src/core/glean.ts
Expand Up @@ -95,15 +95,6 @@ namespace Glean {
pingUploader.resumeUploads();
}

/**
* Initialize core metrics: client_id, first_run_date, os, etc.
*
* @param migrateFromLegacy Whether or not to migrate data from legacy storage.
*/
function initializeCoreMetrics(migrateFromLegacy?: boolean): void {
Context.coreMetrics.initialize(migrateFromLegacy);
}

/**
* Handles the changing of state from upload enabled to disabled.
*
Expand Down Expand Up @@ -333,7 +324,7 @@ namespace Glean {
// If upload is enabled,
// just follow the normal code path to instantiate the core metrics.
onUploadEnabled();
initializeCoreMetrics(config?.migrateFromLegacyStorage);
Context.coreMetrics.initialize();

// Record a page load event if the client has auto page-load events enabled.
if (config?.enableAutoPageLoadEvents) {
Expand Down Expand Up @@ -419,7 +410,7 @@ namespace Glean {
if (Context.uploadEnabled !== flag) {
if (flag) {
onUploadEnabled();
initializeCoreMetrics(Context.config.migrateFromLegacyStorage);
Context.coreMetrics.initialize();
} else {
onUploadDisabled(false);
}
Expand Down
97 changes: 3 additions & 94 deletions glean/src/core/internal_metrics.ts
Expand Up @@ -142,28 +142,13 @@ export class CoreMetrics {
});
}

initialize(migrateFromLegacyStorage?: boolean): void {
initialize(): void {
if (!Context.testing && isWindowObjectUnavailable()) {
return;
}

// If the client had used previous versions of Glean.js before we moved
// to LocalStorage as the data store, then we need to move important
// user data from IndexedDB to LocalStorage.
//
// Currently we are interested in migrating two things
// 1. The client_id - consistent clientId across all sessions.
// 2. The first_run_date - the date when the client was first run.
if (!!migrateFromLegacyStorage && localStorage.getItem("GLEAN_MIGRATION_FLAG") !== "1") {
// The migration is done only once per client. The flag is set in
// LocalStorage to indicate that the migration has been completed.
this.migrateCoreMetricsFromIdb();
localStorage.setItem("GLEAN_MIGRATION_FLAG", "1");
} else {
// If we do not need to migrate anything, then we can set the metrics
// for the first time.
this.initializeUserLifetimeMetrics();
}
this.initializeClientId();
this.initializeFirstRunDate();

this.updateSessionInfo();

Expand Down Expand Up @@ -275,80 +260,4 @@ export class CoreMetrics {
this.sessionId.generateAndSet();
this.sessionCount.add();
}

/**
* Initializes the Glean internal user-lifetime metrics.
*/
private initializeUserLifetimeMetrics(): void {
this.initializeClientId();
this.initializeFirstRunDate();
}

/**
* Migrates the core metrics from the old IDB storage to LocalStorage
* on first launch IF the client had used previous versions of Glean.js -
* pre LocalStorage.
*
* There is no way to access IDB data synchronously, so we rely on listeners
* for when specific actions complete. This means that we need to be careful
* and ensure that the clientId and firstRunDate are always set.
*
* Once the migration is complete, running the initialize functions for the
* clientId and firstRunDate equate to no-ops. If there is an error anywhere
* along the way and the migration is not complete, then the initialize the
* two metrics as usual.
*/
private migrateCoreMetricsFromIdb(): void {
const dbRequest = window.indexedDB.open("Glean");
dbRequest.onerror = () => {
this.initializeUserLifetimeMetrics();
};

dbRequest.onsuccess = () => {
try {
const db = dbRequest.result;
const transaction = db?.transaction("Main", "readwrite");
const store = transaction.objectStore("Main");

// All the core metrics are stored in the userLifetimeMetrics object.
const req = store.get("userLifetimeMetrics");
req.onsuccess = () => {
// Pull and set the existing clientId.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const clientId = req.result?.["glean_client_info"]?.["uuid"]?.["client_id"] as string;
if (!!clientId) {
this.clientId.set(clientId);
} else {
this.initializeClientId();
}

// Pull and set the existing firstRunDate.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const firstRunDate = req.result?.["glean_client_info"]?.["datetime"]?.[
"first_run_date"
] as {
date: string;
timezone: number;
timeUnit: TimeUnit;
};
if (!!firstRunDate) {
this.firstRunDate.setRaw(
firstRunDate.date,
firstRunDate.timezone,
firstRunDate.timeUnit
);
} else {
this.initializeFirstRunDate();
}
};

req.onerror = () => {
this.initializeUserLifetimeMetrics();
};
} catch (e) {
// Fail-safe in case any generic error occurs.
this.initializeUserLifetimeMetrics();
}
};
}
}
38 changes: 0 additions & 38 deletions glean/src/core/metrics/types/datetime.ts
Expand Up @@ -58,18 +58,6 @@ export class DatetimeMetric extends Metric<DatetimeInternalRepresentation, strin
});
}

static fromRawDatetime(
isoString: string,
timezoneOffset: number,
timeUnit: TimeUnit
): DatetimeMetric {
return new DatetimeMetric({
timeUnit,
timezone: timezoneOffset,
date: isoString
});
}

/**
* Gets the datetime data as a Date object.
*
Expand Down Expand Up @@ -249,32 +237,6 @@ export class InternalDatetimeMetricType extends MetricType {
return truncatedDate;
}

/**
* Set a datetime metric from raw values.
*
* # Important
* This method should **never** be exposed to users. This is used solely
* for migrating IDB data to LocalStorage.
*
* @param isoString Raw isoString.
* @param timezone Raw timezone.
* @param timeUnit Raw timeUnit.
*/
setRaw(isoString: string, timezone: number, timeUnit: TimeUnit) {
if (!this.shouldRecord(Context.uploadEnabled)) {
return;
}

try {
const metric = DatetimeMetric.fromRawDatetime(isoString, timezone, timeUnit);
Context.metricsDatabase.record(this, metric);
} catch (e) {
if (e instanceof MetricValidationError) {
e.recordError(this);
}
}
}

/// TESTING ///
/**
* Test-only API
Expand Down

0 comments on commit f265d15

Please sign in to comment.