1717package com.duckduckgo.pir.impl.pixels
1818
1919import com.duckduckgo.common.utils.CurrentTimeProvider
20+ import com.duckduckgo.common.utils.DispatcherProvider
2021import com.duckduckgo.di.scopes.AppScope
2122import com.duckduckgo.pir.impl.store.PirRepository
23+ import com.duckduckgo.pir.impl.store.PirSchedulingRepository
2224import com.squareup.anvil.annotations.ContributesBinding
25+ import kotlinx.coroutines.withContext
2326import logcat.logcat
2427import java.util.concurrent.TimeUnit
2528import 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 }
0 commit comments