1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.internal.logging; 17 18 import com.google.errorprone.annotations.CheckReturnValue; 19 import com.google.mobiledatadownload.LogEnumsProto.MddClientEvent; 20 import com.google.mobiledatadownload.LogProto.DataDownloadFileGroupStats; 21 import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupBookkeeping; 22 import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal; 23 24 /** Helper logger to log the events associated with an MDD Download or Import operation. */ 25 @CheckReturnValue 26 public final class DownloadStateLogger { 27 private static final String TAG = "FileGroupStatusLogger"; 28 29 /** The type of operation for which the logger will log events. */ 30 public enum Operation { 31 DOWNLOAD, 32 IMPORT, 33 }; 34 35 private final EventLogger eventLogger; 36 private final Operation operation; 37 DownloadStateLogger(EventLogger eventLogger, Operation operation)38 private DownloadStateLogger(EventLogger eventLogger, Operation operation) { 39 this.eventLogger = eventLogger; 40 this.operation = operation; 41 } 42 forDownload(EventLogger eventLogger)43 public static DownloadStateLogger forDownload(EventLogger eventLogger) { 44 return new DownloadStateLogger(eventLogger, Operation.DOWNLOAD); 45 } 46 forImport(EventLogger eventLogger)47 public static DownloadStateLogger forImport(EventLogger eventLogger) { 48 return new DownloadStateLogger(eventLogger, Operation.IMPORT); 49 } 50 51 /** Gets the operation associated with this logger. */ getOperation()52 public Operation getOperation() { 53 return operation; 54 } 55 logStarted(DataFileGroupInternal fileGroup)56 public void logStarted(DataFileGroupInternal fileGroup) { 57 switch (operation) { 58 case DOWNLOAD: 59 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 60 break; 61 case IMPORT: 62 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 63 break; 64 } 65 } 66 logPending(DataFileGroupInternal fileGroup)67 public void logPending(DataFileGroupInternal fileGroup) { 68 switch (operation) { 69 case DOWNLOAD: 70 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 71 break; 72 case IMPORT: 73 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 74 break; 75 } 76 } 77 logFailed(DataFileGroupInternal fileGroup)78 public void logFailed(DataFileGroupInternal fileGroup) { 79 switch (operation) { 80 case DOWNLOAD: 81 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 82 break; 83 case IMPORT: 84 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 85 break; 86 } 87 } 88 logComplete(DataFileGroupInternal fileGroup)89 public void logComplete(DataFileGroupInternal fileGroup) { 90 switch (operation) { 91 case DOWNLOAD: 92 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 93 logDownloadLatency(fileGroup); 94 break; 95 case IMPORT: 96 logEventWithDataFileGroup(MddClientEvent.Code.EVENT_CODE_UNSPECIFIED, fileGroup); 97 break; 98 } 99 } 100 logDownloadLatency(DataFileGroupInternal fileGroup)101 private void logDownloadLatency(DataFileGroupInternal fileGroup) { 102 // This operation only makes sense for download operation, exit early if it's not the download 103 // operation. 104 if (operation != Operation.DOWNLOAD) { 105 return; 106 } 107 108 DataDownloadFileGroupStats fileGroupDetails = 109 DataDownloadFileGroupStats.newBuilder() 110 .setOwnerPackage(fileGroup.getOwnerPackage()) 111 .setFileGroupName(fileGroup.getGroupName()) 112 .setFileGroupVersionNumber(fileGroup.getFileGroupVersionNumber()) 113 .setFileCount(fileGroup.getFileCount()) 114 .setBuildId(fileGroup.getBuildId()) 115 .setVariantId(fileGroup.getVariantId()) 116 .build(); 117 118 DataFileGroupBookkeeping bookkeeping = fileGroup.getBookkeeping(); 119 long newFilesReceivedTimestamp = bookkeeping.getGroupNewFilesReceivedTimestamp(); 120 long downloadStartedTimestamp = bookkeeping.getGroupDownloadStartedTimestampInMillis(); 121 long downloadCompleteTimestamp = bookkeeping.getGroupDownloadedTimestampInMillis(); 122 123 Void downloadLatency = null; 124 125 eventLogger.logMddDownloadLatency(fileGroupDetails, downloadLatency); 126 } 127 logEventWithDataFileGroup( MddClientEvent.Code code, DataFileGroupInternal fileGroup)128 private void logEventWithDataFileGroup( 129 MddClientEvent.Code code, DataFileGroupInternal fileGroup) { 130 eventLogger.logEventSampled( 131 code, 132 fileGroup.getGroupName(), 133 fileGroup.getFileGroupVersionNumber(), 134 fileGroup.getBuildId(), 135 fileGroup.getVariantId()); 136 } 137 } 138