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

Add Privacy Stats module for collecting stats about blocked trackers #1097

Merged
merged 52 commits into from
Nov 29, 2024

Conversation

ayoy
Copy link
Contributor

@ayoy ayoy commented Nov 26, 2024

Please review the release process for BrowserServicesKit here.

Required:

Task/Issue URL: https://app.asana.com/0/72649045549333/1208246350498754/f
iOS PR: duckduckgo/iOS#3651
macOS PR: duckduckgo/macos-browser#3611
What kind of version bump will this require?: Major

Optional:

Tech Design URL: https://app.asana.com/0/481882893211075/1208848285586302
CC: @bwaresiak

Description:
This change adds PrivacyStats module that will be used in macOS HTML New Tab Page.
Privacy Stats uses Core Data for storage where it keeps blocked trackers counts per tracker
company name per day, for past 7 days.
PrivacyStats class is the main (and only) public interface. It provides simple API to fetch current
stats, clear them (for Fire button use) and record blocked trackers, as well as a publisher
that notifies about changes to stats.

Steps to test this PR:
TBD in the macOS PR

OS Testing:

  • iOS 14
  • iOS 15
  • iOS 16
  • macOS 10.15
  • macOS 11
  • macOS 12

Internal references:

Software Engineering Expectations
Technical Design Template

@ayoy ayoy changed the title [WIP] Privacy Stats Add Privacy Stats module for collecting stats about blocked trackers Nov 28, 2024
@ayoy ayoy requested a review from aataraxiaa November 28, 2024 17:15
Copy link
Contributor

@aataraxiaa aataraxiaa left a comment

Choose a reason for hiding this comment

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

Left a few comments @ayoy. Overall looks good. Nice tests. 👍🏼

Sources/Common/Extensions/DateExtension.swift Outdated Show resolved Hide resolved
Sources/Common/Extensions/DateExtension.swift Outdated Show resolved Hide resolved
Sources/PrivacyStats/PrivacyStats.swift Outdated Show resolved Hide resolved
Sources/PrivacyStats/PrivacyStats.swift Outdated Show resolved Hide resolved
Sources/PrivacyStats/internal/PrivacyStatsUtils.swift Outdated Show resolved Hide resolved
Copy link
Contributor

@aataraxiaa aataraxiaa left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes @ayoy , looks great!

@ayoy ayoy assigned ayoy and unassigned aataraxiaa Nov 29, 2024
fetchRequest.predicate = NSPredicate(format: "%K < %@", #keyPath(DailyBlockedTrackersEntity.timestamp), oldestValidTimestamp as NSDate)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

try context.execute(deleteRequest)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As we are using batch delete request, I'd reset the context after this to invalidate any object that could be still cached in the object graph by the context.

Sources/PrivacyStats/PrivacyStats.swift Outdated Show resolved Hide resolved

private let db: CoreDataDatabase
private let context: NSManagedObjectContext
private var currentPack: CurrentPack?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be non optional let?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can, it was just initialized using self.initializeCurrentPack() which was causing the "self used before all properties are initialized". But if I make it lazy var then it works fine as non-optional :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could make it static and pass context and errorEvents as params to avoid this warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was my initial idea actually. Agreed that making it a let has a benefit to the reader so let me update it 👍

* It sets a new empty pack with the current timestamp.
*/
func resetPack() {
resetStats(andSet: Date.currentPrivacyStatsPackTimestamp)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious - shouldn't that notifyChanges ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great point - it actually should. It works because it's used by the Fire button which closes everything and opens a new window in which case new data is pulled, but overall this should trigger a notification. Thanks for spotting it!

@ayoy ayoy merged commit dfd266a into main Nov 29, 2024
7 checks passed
@ayoy ayoy deleted the dominik/privacy-stats branch November 29, 2024 14:38
samsymons added a commit that referenced this pull request Nov 29, 2024
* main:
  Add Privacy Stats module for collecting stats about blocked trackers (#1097)
  experiment framework (#1100)
  Client displays correct subscription (#1088)
  Bump github.com/duckduckgo/privacy-dashboard from 7.2.0 to 7.2.1 (#1085)
samsymons added a commit that referenced this pull request Nov 30, 2024
* main:
  Add `locale` parameter to RMF survey builder (#1105)
  Add Privacy Stats module for collecting stats about blocked trackers (#1097)
  experiment framework (#1100)
  Client displays correct subscription (#1088)
  Bump github.com/duckduckgo/privacy-dashboard from 7.2.0 to 7.2.1 (#1085)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants