Skip to content

Commit 8f42d26

Browse files
mikespositodanroc
andauthored
feat: add ProfileMetricsController to @metamask/profile-metrics-controller (#7196)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> This PR adds `ProfileMetricsController` to `@metamask/profile-metrics-controller`. The new controller manages a queue of addresses that need to be sent to `ProfileMetricsService` The queue is updated in the following scenarios: - When the wallet is being unlocked _AND_ `firstSyncCompleted` is `false` (i.e., first sync has not been completed yet) - When a new account is added - When an account is removed The queue is processed at regular intervals (every 10 seconds by default). At each polling interval, if there are items in the queue, the controller will attempt to call `ProfileMetricsService` through the messenger to sync addresses in batches, grouped by their entropy source. Accounts with no entropy source will be batched together. The queue is persisted to storage, so that if the app is closed and reopened, the sync process can continue from where it left off. If one of the batch syncs fails, the same batch will be retried in the next interval. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> * Related to https://consensyssoftware.atlassian.net/browse/WPC-179 ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds `ProfileMetricsController` that queues accounts and periodically submits grouped metrics via messenger, with persistence, mutexing, and comprehensive tests. > > - **New Controller**: `src/ProfileMetricsController.ts` > - Polling via `StaticIntervalPollingController` (default 10s) to call `ProfileMetricsService:submitMetrics`. > - Maintains persistent `syncQueue` grouped by entropy source (`null` for none); guarded by `async-mutex`. > - Subscribes to `KeyringController:unlock/lock` and `AccountsController:accountAdded/accountRemoved` to enqueue/dequeue; initial enqueue on unlock if not completed. > - Batches submissions per entropy source; logs and skips failed batches while continuing others. > - Exposes state with metadata (`initialEnqueueCompleted`, `syncQueue`). > - **Tests**: `src/ProfileMetricsController.test.ts` covering subscriptions, polling behavior, batching, failure handling, and state metadata. > - **Exports**: Update `src/index.ts` to export controller types and helpers. > - **Project Setup**: > - Add dependencies: `@metamask/accounts-controller`, `@metamask/keyring-controller`, `@metamask/polling-controller`, `async-mutex` (and dev `@metamask/keyring-internal-api`). > - Update TS references in `tsconfig.json` and `tsconfig.build.json`. > - Update `CHANGELOG.md` entry for initial release. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit bc80feb. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Daniel Rocha <[email protected]>
1 parent a779d4d commit 8f42d26

File tree

8 files changed

+984
-2
lines changed

8 files changed

+984
-2
lines changed

packages/profile-metrics-controller/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Initial release ([#7194](https://github.com/MetaMask/core/pull/7194))
12+
- Initial release ([#7194](https://github.com/MetaMask/core/pull/7194), [#7196](https://github.com/MetaMask/core/pull/7196))
1313

1414
[Unreleased]: https://github.com/MetaMask/core/

packages/profile-metrics-controller/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,19 @@
4848
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
4949
},
5050
"dependencies": {
51+
"@metamask/accounts-controller": "^35.0.0",
5152
"@metamask/base-controller": "^9.0.0",
5253
"@metamask/controller-utils": "^11.16.0",
54+
"@metamask/keyring-controller": "^25.0.0",
5355
"@metamask/messenger": "^0.3.0",
56+
"@metamask/polling-controller": "^16.0.0",
5457
"@metamask/profile-sync-controller": "^27.0.0",
55-
"@metamask/utils": "^11.8.1"
58+
"@metamask/utils": "^11.8.1",
59+
"async-mutex": "^0.5.0"
5660
},
5761
"devDependencies": {
5862
"@metamask/auto-changelog": "^3.4.4",
63+
"@metamask/keyring-internal-api": "^9.0.0",
5964
"@ts-bridge/cli": "^0.6.4",
6065
"@types/jest": "^27.4.1",
6166
"deepmerge": "^4.2.2",

0 commit comments

Comments
 (0)