1 /* 2 * Copyright (C) 2023 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 android.annotation.NonNull; 20 21 import com.android.adservices.LoggerFactory; 22 import com.android.adservices.service.customaudience.BackgroundFetchWorker; 23 import com.android.adservices.shared.util.Clock; 24 import com.android.internal.annotations.VisibleForTesting; 25 26 import java.util.Objects; 27 28 /** 29 * Class for logging the background fetch process when {@link 30 * BackgroundFetchWorker#runBackgroundFetch} is called. 31 */ 32 public class BackgroundFetchExecutionLogger extends ApiServiceLatencyCalculator { 33 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 34 35 @VisibleForTesting 36 static final String MISSING_START_TIMESTAMP = 37 "The logger should set the start of the background fetch process."; 38 39 @VisibleForTesting 40 static final String REPEATED_END_TIMESTAMP = 41 "The logger has already set the end of the background fetch process."; 42 43 private final AdServicesLogger mAdServicesLogger; 44 private long mRunBackgroundFetchStartTimestamp; 45 private long mRunBackgroundFetchEndTimestamp; 46 private int mNumOfEligibleToUpdateCAs; 47 BackgroundFetchExecutionLogger( @onNull Clock clock, @NonNull AdServicesLogger adServicesLogger)48 public BackgroundFetchExecutionLogger( 49 @NonNull Clock clock, @NonNull AdServicesLogger adServicesLogger) { 50 super(clock); 51 Objects.requireNonNull(clock); 52 Objects.requireNonNull(adServicesLogger); 53 this.mAdServicesLogger = adServicesLogger; 54 sLogger.v("BackgroundFetchExecutionLogger starts."); 55 } 56 57 /** Start the background fetch process. */ start()58 public void start() { 59 sLogger.v("Start logging the BackgroundFetch process."); 60 this.mRunBackgroundFetchStartTimestamp = getServiceElapsedTimestamp(); 61 } 62 63 /** Close and log the background fetch process into AdServicesLogger. */ close(int numOfEligibleToUpdateCAs, int resultCode)64 public void close(int numOfEligibleToUpdateCAs, int resultCode) { 65 if (mRunBackgroundFetchStartTimestamp == 0L) { 66 throw new IllegalStateException(MISSING_START_TIMESTAMP); 67 } 68 if (mRunBackgroundFetchEndTimestamp > 0L) { 69 throw new IllegalStateException(REPEATED_END_TIMESTAMP); 70 } 71 sLogger.v("Close BackgroundFetchExecutionLogger."); 72 getApiServiceInternalFinalLatencyInMs(); 73 this.mRunBackgroundFetchEndTimestamp = getServiceElapsedTimestamp(); 74 int runBackgroundFetchLatencyInMs = 75 (int) (mRunBackgroundFetchEndTimestamp - mRunBackgroundFetchStartTimestamp); 76 77 BackgroundFetchProcessReportedStats backgroundFetchProcessReportedStats = 78 BackgroundFetchProcessReportedStats.builder() 79 .setLatencyInMillis(runBackgroundFetchLatencyInMs) 80 .setNumOfEligibleToUpdateCas(numOfEligibleToUpdateCAs) 81 .setResultCode(resultCode) 82 .build(); 83 sLogger.v("BackgroundFetch process has been logged into AdServicesLogger."); 84 mAdServicesLogger.logBackgroundFetchProcessReportedStats( 85 backgroundFetchProcessReportedStats); 86 } 87 88 /** 89 * Getter for number of eligible to update CAs. 90 * 91 * @return the number of eligible to update CAs. 92 */ getNumOfEligibleToUpdateCAs()93 public int getNumOfEligibleToUpdateCAs() { 94 return mNumOfEligibleToUpdateCAs; 95 } 96 97 /** 98 * Setter for number of eligible to update CAs 99 * 100 * @param numOfEligibleToUpdateCAs the number of eligible to update CAs. 101 */ setNumOfEligibleToUpdateCAs(int numOfEligibleToUpdateCAs)102 public void setNumOfEligibleToUpdateCAs(int numOfEligibleToUpdateCAs) { 103 mNumOfEligibleToUpdateCAs = numOfEligibleToUpdateCAs; 104 } 105 } 106