Skip to content

Commit b20e196

Browse files
committed
Minor refactor
1 parent 474a878 commit b20e196

File tree

5 files changed

+223
-239
lines changed

5 files changed

+223
-239
lines changed

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/pixels/OptOut24HourSubmissionSuccessRateReporter.kt

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package com.duckduckgo.pir.impl.pixels
1818

1919
import com.duckduckgo.common.utils.CurrentTimeProvider
20+
import com.duckduckgo.common.utils.DispatcherProvider
2021
import com.duckduckgo.di.scopes.AppScope
2122
import com.duckduckgo.pir.impl.store.PirRepository
23+
import com.duckduckgo.pir.impl.store.PirSchedulingRepository
2224
import com.squareup.anvil.annotations.ContributesBinding
25+
import kotlinx.coroutines.withContext
2326
import logcat.logcat
2427
import java.util.concurrent.TimeUnit
2528
import javax.inject.Inject
@@ -35,37 +38,46 @@ class RealOptOut24HourSubmissionSuccessRateReporter @Inject constructor(
3538
private val pirRepository: PirRepository,
3639
private val currentTimeProvider: CurrentTimeProvider,
3740
private val pirPixelSender: PirPixelSender,
41+
private val pirSchedulingRepository: PirSchedulingRepository,
42+
private val dispatcherProvider: DispatcherProvider,
3843
) : OptOut24HourSubmissionSuccessRateReporter {
3944
override suspend fun attemptFirePixel() {
40-
logcat { "PIR-CUSTOM-STATS: Attempt to fire 24hour submission pixels" }
41-
val startDate = pirRepository.getCustomStatsPixelsLastSentMs()
42-
val now = currentTimeProvider.currentTimeMillis()
45+
withContext(dispatcherProvider.io()) {
46+
logcat { "PIR-CUSTOM-STATS: Attempt to fire 24hour submission pixels" }
47+
val startDate = pirRepository.getCustomStatsPixelsLastSentMs()
48+
val now = currentTimeProvider.currentTimeMillis()
4349

44-
if (shouldFirePixel(startDate, now)) {
45-
logcat { "PIR-CUSTOM-STATS: Should fire pixel - 24hrs passed since last send" }
46-
val endDate = now - TimeUnit.HOURS.toMillis(24)
47-
val activeBrokers = pirRepository.getAllActiveBrokerObjects()
48-
val hasUserProfiles = pirRepository.getAllUserProfileQueries().isNotEmpty()
50+
if (shouldFirePixel(startDate, now)) {
51+
logcat { "PIR-CUSTOM-STATS: Should fire pixel - 24hrs passed since last send" }
52+
val endDate = now - TimeUnit.HOURS.toMillis(24)
53+
val activeBrokers = pirRepository.getAllActiveBrokerObjects()
54+
val hasUserProfiles = pirRepository.getAllUserProfileQueries().isNotEmpty()
55+
val activeOptOutJobRecords = pirSchedulingRepository.getAllValidOptOutJobRecords()
4956

50-
if (activeBrokers.isNotEmpty() && hasUserProfiles) {
51-
activeBrokers.forEach {
52-
val successRate = optOutSubmitRateCalculator.calculateOptOutSubmitRate(
53-
it.name,
54-
startDate,
55-
endDate,
56-
)
57+
if (activeBrokers.isNotEmpty() && activeOptOutJobRecords.isNotEmpty() && hasUserProfiles) {
58+
activeBrokers.forEach { broker ->
59+
val activeJobRecordsForBroker = activeOptOutJobRecords.filter { it.brokerName == broker.name }
5760

58-
logcat { "PIR-CUSTOM-STATS: 24hr submission ${it.name} : $successRate" }
59-
if (successRate != null) {
60-
pirPixelSender.reportBrokerCustomStateOptOutSubmitRate(
61-
brokerUrl = it.url,
62-
optOutSuccessRate = successRate,
61+
if (activeJobRecordsForBroker.isEmpty()) return@forEach
62+
63+
val successRate = optOutSubmitRateCalculator.calculateOptOutSubmitRate(
64+
activeJobRecordsForBroker,
65+
startDate,
66+
endDate,
6367
)
68+
69+
logcat { "PIR-CUSTOM-STATS: 24hr submission ${broker.name} : $successRate" }
70+
if (successRate != null) {
71+
pirPixelSender.reportBrokerCustomStateOptOutSubmitRate(
72+
brokerUrl = broker.url,
73+
optOutSuccessRate = successRate,
74+
)
75+
}
6476
}
65-
}
6677

67-
logcat { "PIR-CUSTOM-STATS: Updating last send date to $endDate" }
68-
pirRepository.setCustomStatsPixelsLastSentMs(endDate)
78+
logcat { "PIR-CUSTOM-STATS: Updating last send date to $endDate" }
79+
pirRepository.setCustomStatsPixelsLastSentMs(endDate)
80+
}
6981
}
7082
}
7183
}

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/pixels/OptOutConfirmationReporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class RealOptOutConfirmationReporter @Inject constructor(
120120
it.daysPassedSinceSubmission(now, confirmationDay) && jobRecordFilter(it)
121121
}
122122

123-
logcat { "PIR-CUSTOM-STATS: Firing 7day confirmation pixels for ${optOutsForPixel.size} jobs" }
123+
logcat { "PIR-CUSTOM-STATS: Firing $confirmationDay day confirmation pixels for ${optOutsForPixel.size} jobs" }
124124
optOutsForPixel.forEach { optOutJobRecord ->
125125
val broker = activeBrokers[optOutJobRecord.brokerName] ?: return@forEach
126126

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/pixels/OptOutSubmitRateCalculator.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ package com.duckduckgo.pir.impl.pixels
1818

1919
import com.duckduckgo.common.utils.DispatcherProvider
2020
import com.duckduckgo.di.scopes.AppScope
21+
import com.duckduckgo.pir.impl.models.scheduling.JobRecord
2122
import com.duckduckgo.pir.impl.models.scheduling.JobRecord.OptOutJobRecord.OptOutJobStatus.REMOVED
2223
import com.duckduckgo.pir.impl.models.scheduling.JobRecord.OptOutJobRecord.OptOutJobStatus.REQUESTED
23-
import com.duckduckgo.pir.impl.store.PirSchedulingRepository
2424
import com.squareup.anvil.annotations.ContributesBinding
2525
import kotlinx.coroutines.withContext
2626
import java.util.concurrent.TimeUnit
@@ -31,12 +31,12 @@ interface OptOutSubmitRateCalculator {
3131
/**
3232
* Calculates the opt-out 24h submit rate for a given broker within the specified date range.
3333
*
34-
* @param brokerName name of the broker to calculate the opt-out submit rate for.
34+
* @param allActiveOptOutJobsForBroker all active opt-out job records for the broker.
3535
* @param startDateMs The opt-out records to include should be created on or after this date. Default is 0L (epoch).
3636
* @param endDateMs tThe opt-out records to include should be created on or before this date. Default is 0L (epoch).
3737
*/
3838
suspend fun calculateOptOutSubmitRate(
39-
brokerName: String,
39+
allActiveOptOutJobsForBroker: List<JobRecord.OptOutJobRecord>,
4040
startDateMs: Long = 0L,
4141
endDateMs: Long,
4242
): Double?
@@ -45,16 +45,15 @@ interface OptOutSubmitRateCalculator {
4545
@ContributesBinding(AppScope::class)
4646
class RealOptOutSubmitRateCalculator @Inject constructor(
4747
private val dispatcherProvider: DispatcherProvider,
48-
private val schedulingRepository: PirSchedulingRepository,
4948
) : OptOutSubmitRateCalculator {
5049
override suspend fun calculateOptOutSubmitRate(
51-
brokerName: String,
50+
allActiveOptOutJobsForBroker: List<JobRecord.OptOutJobRecord>,
5251
startDateMs: Long,
5352
endDateMs: Long,
5453
): Double? = withContext(dispatcherProvider.io()) {
5554
// Get all opt out job records created within the given range for the specified broker
56-
val recordsCreatedWithinRange = schedulingRepository.getAllValidOptOutJobRecordsForBroker(brokerName).filter {
57-
it.brokerName == brokerName && it.dateCreatedInMillis in startDateMs..endDateMs
55+
val recordsCreatedWithinRange = allActiveOptOutJobsForBroker.filter {
56+
it.dateCreatedInMillis in startDateMs..endDateMs
5857
}
5958

6059
// We don't need to calculate the rate if there are no records
@@ -68,7 +67,8 @@ class RealOptOutSubmitRateCalculator @Inject constructor(
6867
)
6968
}
7069

71-
val optOutSuccessRate = requestedRecordsWithinRange.size.toDouble() / recordsCreatedWithinRange.size.toDouble()
70+
val optOutSuccessRate =
71+
requestedRecordsWithinRange.size.toDouble() / recordsCreatedWithinRange.size.toDouble()
7272
val roundedOptOutSuccessRate = round(optOutSuccessRate * 100) / 100
7373
return@withContext roundedOptOutSuccessRate
7474
}

0 commit comments

Comments
 (0)