1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.stats; 18 19 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_BATTERY_NOT_LOW; 20 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_CHARGING; 21 import static com.android.adservices.service.stats.AdsRelevanceStatusUtils.TOPICS_RESCHEDULE_EPOCH_JOB_STATUS_RESCHEDULE_SUCCESS; 22 23 import com.android.adservices.concurrency.AdServicesExecutors; 24 import com.android.adservices.service.Flags; 25 import com.android.adservices.service.FlagsFactory; 26 import com.android.internal.annotations.VisibleForTesting; 27 28 import java.util.Objects; 29 import java.util.concurrent.Future; 30 31 /** The class for logging {@link TopicsScheduleEpochJobSettingReportedStats}. */ 32 public class TopicsScheduleEpochJobSettingReportedStatsLogger { 33 private final AdServicesLogger mAdServicesLogger; 34 35 private final TopicsScheduleEpochJobSettingReportedStats.Builder mBuilder; 36 37 private final boolean mTopicsEpochJobBatteryConstraintLoggingEnabled; 38 39 private final boolean mTopicsEpochJobBatteryNotLowInsteadOfCharging; 40 41 @VisibleForTesting TopicsScheduleEpochJobSettingReportedStatsLogger( AdServicesLogger adServicesLogger, Flags flags)42 public TopicsScheduleEpochJobSettingReportedStatsLogger( 43 AdServicesLogger adServicesLogger, 44 Flags flags) { 45 Objects.requireNonNull(adServicesLogger); 46 Objects.requireNonNull(flags); 47 mAdServicesLogger = adServicesLogger; 48 mBuilder = TopicsScheduleEpochJobSettingReportedStats.builder(); 49 mTopicsEpochJobBatteryConstraintLoggingEnabled = 50 flags.getTopicsEpochJobBatteryConstraintLoggingEnabled(); 51 mTopicsEpochJobBatteryNotLowInsteadOfCharging = 52 flags.getTopicsEpochJobBatteryNotLowInsteadOfCharging(); 53 } 54 getInstance()55 public static TopicsScheduleEpochJobSettingReportedStatsLogger getInstance() { 56 return new TopicsScheduleEpochJobSettingReportedStatsLogger( 57 AdServicesLoggerImpl.getInstance(), 58 FlagsFactory.getFlags()); 59 } 60 61 /** 62 * Logs {@link TopicsScheduleEpochJobSettingReportedStats} in 63 * {@code EpochJobService.scheduleIfNeeded()}. 64 */ 65 @SuppressWarnings("FutureReturnValueIgnored") // TODO(b/331285831): fix this logScheduleIfNeeded()66 public void logScheduleIfNeeded() { 67 if (!mTopicsEpochJobBatteryConstraintLoggingEnabled) { 68 return; 69 } 70 int scheduleIfNeededEpochJobStatus = 71 mTopicsEpochJobBatteryNotLowInsteadOfCharging 72 ? TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_BATTERY_NOT_LOW 73 : TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_CHARGING; 74 mBuilder.setScheduleIfNeededEpochJobStatus(scheduleIfNeededEpochJobStatus); 75 76 Future<?> unusedLogScheduleIfNeededFuture = 77 AdServicesExecutors.getBackgroundExecutor().submit( 78 () -> mAdServicesLogger 79 .logTopicsScheduleEpochJobSettingReportedStats( 80 mBuilder.build())); 81 } 82 83 /** 84 * Logs {@link TopicsScheduleEpochJobSettingReportedStats} when 85 * {@code EpochJobService.rescheduleEpochJob} is skipped because of some reasons. 86 * 87 * @param skipReason The reason of skipping reschedule epoch job. 88 */ 89 @SuppressWarnings("FutureReturnValueIgnored") // TODO(b/331285831): fix this logSkipRescheduleEpochJob( @dsRelevanceStatusUtils.TopicsRescheduleEpochJobStatus int skipReason)90 public void logSkipRescheduleEpochJob( 91 @AdsRelevanceStatusUtils.TopicsRescheduleEpochJobStatus int skipReason) { 92 if (!mTopicsEpochJobBatteryConstraintLoggingEnabled) { 93 return; 94 } 95 mBuilder.setRescheduleEpochJobStatus(skipReason); 96 97 Future<?> unusedLogSkipRescheduleEpochJobFuture = 98 AdServicesExecutors.getBackgroundExecutor().submit( 99 () -> mAdServicesLogger 100 .logTopicsScheduleEpochJobSettingReportedStats( 101 mBuilder.build())); 102 } 103 104 /** 105 * Sets the previous epoch job configuration when the epoch job is rescheduled successfully in 106 * {@code EpochJobService.rescheduleEpochJob}. 107 * 108 * @param previousScheduledEpochJobRequireBatteryNotLow The battery not low configuration of 109 * previous epoch job. 110 */ setPreviousEpochJobStatus( boolean previousScheduledEpochJobRequireBatteryNotLow)111 public void setPreviousEpochJobStatus( 112 boolean previousScheduledEpochJobRequireBatteryNotLow) { 113 if (!mTopicsEpochJobBatteryConstraintLoggingEnabled) { 114 return; 115 } 116 int previousEpochJobSetting = 117 previousScheduledEpochJobRequireBatteryNotLow 118 ? TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_BATTERY_NOT_LOW 119 : TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_CHARGING; 120 int currentEpochJobSetting = 121 mTopicsEpochJobBatteryNotLowInsteadOfCharging 122 ? TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_BATTERY_NOT_LOW 123 : TOPICS_EPOCH_JOB_BATTERY_CONSTRAINT_REQUIRES_CHARGING; 124 mBuilder.setRescheduleEpochJobStatus( 125 TOPICS_RESCHEDULE_EPOCH_JOB_STATUS_RESCHEDULE_SUCCESS); 126 mBuilder.setPreviousEpochJobSetting(previousEpochJobSetting); 127 mBuilder.setCurrentEpochJobSetting(currentEpochJobSetting); 128 } 129 } 130