Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding a new controller for Metametrics Data Deletion #24503

Draft
wants to merge 37 commits into
base: develop
Choose a base branch
from

Conversation

NiranjanaBinoy
Copy link
Contributor

@NiranjanaBinoy NiranjanaBinoy commented May 13, 2024

Description

We are adding a new controller, MetaMetricsDataDeletionController, and a service class for MetaMetrics data deletion. The service class will have the API interaction for the data deletion, which will create the data deletion regulation and check the status of an existing relation. The controller handles the state update and service class interactions.
The PR also initiates the controller by updating the metamask-controller and updating actions.ts with the new functions.

Open in GitHub Codespaces

Related issues

Fixes:
fixes #24405

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@NiranjanaBinoy NiranjanaBinoy changed the title Adding a new controller for Metametrics Data Deletion feat : Adding a new controller for Metametrics Data Deletion May 13, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [614ccfd]
Page Load Metrics (880 ± 596 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6912288168
domContentLoaded95619126
load5729188801242596
domInteractive95619126
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link

codecov bot commented May 13, 2024

Codecov Report

Attention: Patch coverage is 64.65517% with 41 lines in your changes are missing coverage. Please review.

Project coverage is 65.78%. Comparing base (0dc23ab) to head (4a07c1a).

Current head 4a07c1a differs from pull request most recent head 9276b65

Please upload reports for the commit 9276b65 to get more accurate results.

Files Patch % Lines
app/scripts/services/data-deletion-service.ts 52.73% 26 Missing ⚠️
...metrics-data-deletion/metametrics-data-deletion.ts 81.40% 8 Missing ⚠️
app/scripts/metamask-controller.js 41.67% 7 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24503      +/-   ##
===========================================
+ Coverage    65.70%   65.78%   +0.08%     
===========================================
  Files         1366     1368       +2     
  Lines        54361    54478     +117     
  Branches     14141    14162      +21     
===========================================
+ Hits         35715    35838     +123     
+ Misses       18646    18640       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@NiranjanaBinoy NiranjanaBinoy changed the title feat : Adding a new controller for Metametrics Data Deletion feat: Adding a new controller for Metametrics Data Deletion May 14, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [d1cdd73]
Page Load Metrics (786 ± 599 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint60143882411
domContentLoaded84515105
load4831237861247599
domInteractive84515105
Bundle size diffs
  • background: 0 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@@ -5085,6 +5086,42 @@ export function setName(
}) as any;
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a unit test for each of these?

@@ -0,0 +1,138 @@
import { ObservableStore } from '@metamask/obs-store';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some unit tests for this file.

}

async createMetaMetricsDataDeletionTask(): Promise<DataDeletionResponse> {
const segmentSourceId = process.env.SEGMENT_DELETE_API_SOURCE_ID;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition of these constant variables can happen outside of this method and outside of the class. We don't need to re-define them every time the method is called.

);

this.store.updateState({
metaMetricsDataDeletionId: data?.data?.regulateId as string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data?.data?.regulateId is undefined, should we return an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the api will return a regulate ID if the request did not error. So I have made the type to just string than string | undefined, and initiated the state variable to empty string.

const { data } = await fetchDeletionRegulationStatus(
this.store.getState().metaMetricsDataDeletionId as string,
);
const regulation = data?.data?.regulation as Record<string, string>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data?.data?.regulation is undefined, should we return an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the api will return a regulation if we have a valid Id, otherwise, it would probably error. I have added an empty check for deleteRegulateId to make sure we have a value before we call the api..

const segmentSourceId = process.env.SEGMENT_DELETE_API_SOURCE_ID;
const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT;

if (!segmentSourceId || !segmentRegulationEndpoint) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this validation is unneeded here, and can just happen within the services?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

);
return response as DeleteRegulationAPIResponse;
} catch (error: unknown) {
throw new Error('Analytics Deletion Task Error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to throw the error object received in the catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the try-catch in the service class, and it is being handled in the controller.

const segmentRegulationEndpoint = process.env.SEGMENT_REGULATIONS_ENDPOINT;

try {
const response: unknown = await fetchWithTimeout(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can type the response in a way other that unknown 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type was explicitly mentioned as it is erroring while converting the Response type returned from fetchWithTimeout to DeleteRegulationAPIResponse. We can also typecast while returning the response like this response as unknown as DeleteRegulationAPIResponse

Copy link
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First review complete. I have left some comments

.metamaskrc.dist Outdated
@@ -6,6 +6,8 @@ INFURA_PROJECT_ID=00000000000

;PASSWORD=METAMASK PASSWORD
;SEGMENT_WRITE_KEY=
;SEGMENT_DELETE_API_SOURCE_ID=
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what these environment variables are?

It was my understanding that we're using our own API for this, not using Segment directly. Our API will also be requesting deletion from more than just Segment. If that's the case, making these environment variables named SEGMENT_ seems inaccurate

data: RegulationId | CurrentRegulationStatus;
};

export enum DeleteRegulationStatus {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there API docs somewhere that can explain what status exist, and what they mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the comment to reference these docs?

fetchDeletionRegulationStatus,
} from './services/services';

export type DataDeleteDate = string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Ideally each type would have a TSDoc block that explains what the type is for. This is especially useful because the descriptions show up in Intellisense tooltips in supporting editors such as VSCode

@NiranjanaBinoy NiranjanaBinoy force-pushed the delete-metametrics-data-controller branch from 00838c9 to b540dfc Compare May 27, 2024 20:28
@Gudahtt Gudahtt mentioned this pull request May 27, 2024
7 tasks
Gudahtt added a commit that referenced this pull request May 27, 2024
The `nock` library is not compatible with the fake timers we use in
unit tests because it uses the Node.js `timers` API. This API is not
mocked correctly by the version of Jest we are using.

Jest uses `@sinon/fake-timers` internally, which didn't support mocking
the Node.js `timers` API until v11.0.0 (see sinonjs/fake-timers#467)
This package is updated in Jest as part of the v30 release, which is
currently under development.

To workaround this problem in the meantime, the `nock` package has been
updated and patched to use global timers rather than the `timers` API.
Global timers are mocked correctly.

This was required for some unit tests that I will be submitting in a
different PR, intended for #24503
Gudahtt added a commit that referenced this pull request May 28, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

The `nock` library is not compatible with the fake timers we use in unit
tests because it uses the Node.js `timers` API. This API is not mocked
correctly by the version of Jest we are using.

Jest uses `@sinon/fake-timers` internally, which didn't support mocking
the Node.js `timers` API until v11.0.0 (see
sinonjs/fake-timers#467) This package is updated
in Jest as part of the v30 release, which is currently under
development.

To workaround this problem in the meantime, the `nock` package has been
updated and patched to use global timers rather than the `timers` API.
Global timers are mocked correctly.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24805?quickstart=1)

## **Related issues**

This was required for some unit tests that I will be submitting in a
different PR, intended for #24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Gudahtt and others added 3 commits May 28, 2024 14:01
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

The `circuitBreakDuration`, `degradedThreshold`, and `timeout` are now
configurable for the `DataDeletionService`. This configuration is relied
upon in the unit tests, to be added in a later PR.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24806?quickstart=1)

## **Related issues**

This is an improvement for the PR #24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
@metamaskbot
Copy link
Collaborator

Builds ready [3ef9147]
Page Load Metrics (945 ± 578 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint682461114622
domContentLoaded10501594
load5632949451203578
domInteractive10501494
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 7.83 KiB (0.22%)
  • ui: 0 Bytes (0.00%)
  • common: -388 Bytes (-0.01%)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

The `DataDeletionService` has been moved to a `services` directory,
independent of controllers. This was done to reinforce that services are
independent of controllers, and can be used outside of them.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24863?quickstart=1)

## **Related issues**

This is an improvement for the PR
#24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Gudahtt added a commit that referenced this pull request May 29, 2024
The `DataDeletionController` has been decoupled from the
`MetaMetricsController` by updating the constructor parameters to
expect just a function for getting the MetaMetrics ID, rather than the
entire `MetaMetricsController` state. This vastly simplifies the API
surface, making it easier to audit and test. It also prevents the
`DataDeletionController` from having the capability to make changes to
the `MetaMetricsController` state.

This is an improvement for #24503
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

The `DataDeletionController` has been decoupled from the
`MetaMetricsController` by updating the constructor parameters to expect
just a function for getting the MetaMetrics ID, rather than the entire
`MetaMetricsController` state. This vastly simplifies the API surface,
making it easier to audit and test. It also prevents the
`DataDeletionController` from having the capability to make changes to
the `MetaMetricsController` state.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24870?quickstart=1)

## **Related issues**

This is an improvement for #24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
Gudahtt and others added 2 commits May 31, 2024 10:55
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

A single test has been added, along with a helper setup function that is
designed to be used for further unit tests as well.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24879?quickstart=1)

## **Related issues**

This is an improvement for the PR
#24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
@metamaskbot
Copy link
Collaborator

Builds ready [4a07c1a]
Page Load Metrics (119 ± 139 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6910985105
domContentLoaded9141111
load421379119289139
domInteractive9141111

legobeat and others added 2 commits June 1, 2024 19:35
## **Description**

Add unit tests for the `createDataDeletionRegulationTask` method of
`DataDeletionService`.

One of the two service methods is now fully tested. The remaining method
will be tested in a later PR.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24816?quickstart=1)

## **Related issues**

This is an improvement for the PR #24503

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: Niranjana Binoy <[email protected]>
@NiranjanaBinoy NiranjanaBinoy marked this pull request as draft June 3, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs more work from the author
Development

Successfully merging this pull request may close these issues.

Add a new controller for MetaMetrics Data Deletion
6 participants