-
Notifications
You must be signed in to change notification settings - Fork 1k
PIR: Add support for multiple profile queries in PirScan and PirOptOut #6413
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
Merged
landomen
merged 6 commits into
develop
from
feature/domen/pir_scan_multiple_profile_queries
Jul 21, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
207758b
Add support for multiple profile queries in PirScan and PirOptOut
landomen 575be3a
Add additional default profile to PirOptOut
landomen 7daa1ef
Fix code formatting
landomen 8e40765
Optimize the runner work distribution
landomen a8a7496
Update debug opt-out
landomen 2133953
Simplify parse steps function to use profile id
landomen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,21 +101,58 @@ class RealPirOptOut @Inject constructor( | |
private val dispatcherProvider: DispatcherProvider, | ||
callbacks: PluginPoint<PirCallbacks>, | ||
) : PirOptOut, PirJob(callbacks) { | ||
private var profileQuery: ProfileQuery = ProfileQuery( | ||
firstName = "William", | ||
lastName = "Smith", | ||
city = "Chicago", | ||
state = "IL", | ||
addresses = listOf( | ||
Address( | ||
city = "Chicago", | ||
state = "IL", | ||
private var profileQueries: List<ProfileQuery> = listOf( | ||
ProfileQuery( | ||
id = -1, | ||
firstName = "William", | ||
lastName = "Smith", | ||
city = "Chicago", | ||
state = "IL", | ||
addresses = listOf( | ||
Address( | ||
city = "Chicago", | ||
state = "IL", | ||
), | ||
), | ||
birthYear = 1993, | ||
fullName = "William Smith", | ||
age = 32, | ||
deprecated = false, | ||
), | ||
ProfileQuery( | ||
id = -2, | ||
firstName = "Jane", | ||
lastName = "Doe", | ||
city = "New York", | ||
state = "NY", | ||
addresses = listOf( | ||
Address( | ||
city = "New York", | ||
state = "NY", | ||
), | ||
), | ||
birthYear = 1990, | ||
fullName = "Jane Doe", | ||
age = 35, | ||
deprecated = false, | ||
), | ||
ProfileQuery( | ||
id = -3, | ||
firstName = "Alicia", | ||
lastName = "West", | ||
city = "Los Angeles", | ||
state = "CA", | ||
addresses = listOf( | ||
Address( | ||
city = "Los Angeles", | ||
state = "CA", | ||
), | ||
), | ||
birthYear = 1985, | ||
fullName = "Alicia West", | ||
age = 40, | ||
deprecated = false, | ||
), | ||
birthYear = 1993, | ||
fullName = "William Smith", | ||
age = 32, | ||
deprecated = false, | ||
) | ||
|
||
private val runners: MutableList<PirActionsRunner> = mutableListOf() | ||
|
@@ -131,9 +168,9 @@ class RealPirOptOut @Inject constructor( | |
cleanRunners() | ||
runners.clear() | ||
} | ||
obtainProfile() | ||
obtainProfiles() | ||
|
||
logcat { "PIR-OPT-OUT: Running opt-out on profile: $profileQuery on ${Thread.currentThread().name}" } | ||
logcat { "PIR-OPT-OUT: Running debug opt-out for $brokers on profiles: $profileQueries on ${Thread.currentThread().name}" } | ||
|
||
runners.add( | ||
pirActionsRunnerFactory.create( | ||
|
@@ -143,21 +180,28 @@ class RealPirOptOut @Inject constructor( | |
), | ||
) | ||
|
||
// Start each runner on a subset of the broker steps | ||
// Load opt-out steps jsons for each broker | ||
val brokerOptOutStepsJsons = brokers.mapNotNull { broker -> | ||
repository.getBrokerOptOutSteps(broker)?.let { broker to it } | ||
} | ||
|
||
brokers.mapNotNull { broker -> | ||
repository.getBrokerOptOutSteps(broker)?.run { | ||
brokerStepsParser.parseStep(broker, this) | ||
} | ||
}.filter { | ||
it.isNotEmpty() | ||
// Map broker steps with their associated profile queries | ||
val allSteps = profileQueries.map { profileQuery -> | ||
brokerOptOutStepsJsons.map { (broker, stepsJson) -> | ||
brokerStepsParser.parseStep(broker, stepsJson, profileQuery.id) | ||
}.flatten().map { step -> profileQuery to step } | ||
}.flatten() | ||
.also { list -> | ||
runners[0].startOn(webView, profileQuery, list) | ||
runners[0].stop() | ||
} | ||
|
||
logcat { "PIR-OPT-OUT: Optout completed for all runners" } | ||
// Execute each steps sequentially on the single runner | ||
allSteps.forEach { (profileQuery, step) -> | ||
logcat { "PIR-OPT-OUT: Start thread=${Thread.currentThread().name}, profile=$profileQuery and step=$step" } | ||
runners[0].startOn(webView, profileQuery, listOf(step)) | ||
runners[0].stop() | ||
logcat { "PIR-OPT-OUT: Finish thread=${Thread.currentThread().name}, profile=$profileQuery and step=$step" } | ||
} | ||
|
||
logcat { "PIR-OPT-OUT: Opt-out completed for all runners and profiles" } | ||
|
||
emitCompletedPixel() | ||
onJobCompleted() | ||
return@withContext Result.success(Unit) | ||
|
@@ -173,25 +217,28 @@ class RealPirOptOut @Inject constructor( | |
cleanRunners() | ||
runners.clear() | ||
} | ||
obtainProfile() | ||
obtainProfiles() | ||
|
||
logcat { "PIR-OPT-OUT: Running opt-out on profile: $profileQuery on ${Thread.currentThread().name}" } | ||
logcat { "PIR-OPT-OUT: Running opt-out on profiles: $profileQueries on ${Thread.currentThread().name}" } | ||
|
||
val script = pirCssScriptLoader.getScript() | ||
|
||
val brokerSteps = brokers.mapNotNull { broker -> | ||
repository.getBrokerOptOutSteps(broker)?.run { | ||
brokerStepsParser.parseStep(broker, this) | ||
} | ||
}.filter { | ||
it.isNotEmpty() | ||
// Load opt-out steps jsons for each broker | ||
val brokerOptOutStepsJsons = brokers.mapNotNull { broker -> | ||
repository.getBrokerOptOutSteps(broker)?.let { broker to it } | ||
} | ||
|
||
// Map broker steps with their associated profile queries | ||
val allSteps = profileQueries.map { profileQuery -> | ||
brokerOptOutStepsJsons.map { (broker, stepsJson) -> | ||
brokerStepsParser.parseStep(broker, stepsJson, profileQuery.id) | ||
}.flatten().map { step -> profileQuery to step } | ||
}.flatten() | ||
|
||
maxWebViewCount = if (brokerSteps.size <= MAX_DETACHED_WEBVIEW_COUNT) { | ||
brokerSteps.size | ||
} else { | ||
MAX_DETACHED_WEBVIEW_COUNT | ||
} | ||
maxWebViewCount = minOf(allSteps.size, MAX_DETACHED_WEBVIEW_COUNT) | ||
|
||
// Assign steps to runners based on the maximum number of WebViews we can use | ||
val stepsPerRunner = allSteps.splitIntoParts(maxWebViewCount) | ||
Comment on lines
+226
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now split the work between the maximum number of runners to max utilization. |
||
|
||
logcat { "PIR-OPT-OUT: Attempting to create $maxWebViewCount parallel runners on ${Thread.currentThread().name}" } | ||
|
||
|
@@ -208,45 +255,49 @@ class RealPirOptOut @Inject constructor( | |
createCount++ | ||
} | ||
|
||
// Start each runner on a subset of the broker steps | ||
brokerSteps.splitIntoParts(maxWebViewCount) | ||
.mapIndexed { index, part -> | ||
async { | ||
runners[index].start(profileQuery, part) | ||
// Execute the steps on all runners in parallel | ||
stepsPerRunner.mapIndexed { index, partSteps -> | ||
async { | ||
partSteps.map { (profileQuery, step) -> | ||
logcat { "PIR-OPT-OUT: Start opt-out on runner=$index, profile=$profileQuery and step=$step" } | ||
runners[index].start(profileQuery, listOf(step)) | ||
runners[index].stop() | ||
logcat { "PIR-OPT-OUT: Finish opt-out on runner=$index, profile=$profileQuery and step=$step" } | ||
} | ||
}.awaitAll() | ||
} | ||
}.awaitAll() | ||
|
||
logcat { "PIR-OPT-OUT: Optout completed for all runners" } | ||
logcat { "PIR-OPT-OUT: Opt-out completed for all runners and profiles" } | ||
emitCompletedPixel() | ||
onJobCompleted() | ||
return@withContext Result.success(Unit) | ||
} | ||
|
||
private suspend fun obtainProfile() { | ||
repository.getUserProfiles().also { | ||
if (it.isNotEmpty()) { | ||
// Temporarily taking the first profile only for the PoC. In the reality, more than 1 should be allowed. | ||
val storedProfile = it[0] | ||
profileQuery = ProfileQuery( | ||
firstName = storedProfile.userName.firstName, | ||
lastName = storedProfile.userName.lastName, | ||
city = storedProfile.addresses.city, | ||
state = storedProfile.addresses.state, | ||
addresses = listOf( | ||
Address( | ||
city = storedProfile.addresses.city, | ||
state = storedProfile.addresses.state, | ||
private suspend fun obtainProfiles() { | ||
repository.getUserProfiles().also { profiles -> | ||
if (profiles.isNotEmpty()) { | ||
profileQueries = profiles.map { storedProfile -> | ||
ProfileQuery( | ||
id = storedProfile.id, | ||
firstName = storedProfile.userName.firstName, | ||
lastName = storedProfile.userName.lastName, | ||
city = storedProfile.addresses.city, | ||
state = storedProfile.addresses.state, | ||
addresses = listOf( | ||
Address( | ||
city = storedProfile.addresses.city, | ||
state = storedProfile.addresses.state, | ||
), | ||
), | ||
), | ||
birthYear = storedProfile.birthYear, | ||
fullName = storedProfile.userName.middleName?.run { | ||
"${storedProfile.userName.firstName} $this ${storedProfile.userName.lastName}" | ||
} | ||
?: "${storedProfile.userName.firstName} ${storedProfile.userName.lastName}", | ||
age = LocalDate.now().year - storedProfile.birthYear, | ||
deprecated = false, | ||
) | ||
birthYear = storedProfile.birthYear, | ||
fullName = storedProfile.userName.middleName?.run { | ||
"${storedProfile.userName.firstName} $this ${storedProfile.userName.lastName}" | ||
} | ||
?: "${storedProfile.userName.firstName} ${storedProfile.userName.lastName}", | ||
age = LocalDate.now().year - storedProfile.birthYear, | ||
deprecated = false, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that the previous implementation would not use all 20 runners in some cases. Example:
The new implementation results in 1 list of 2 elements + 19 lists of 1 element = 20 runners