summaryrefslogtreecommitdiff
path: root/java/com/google/android/libraries/mobiledatadownload/internal/logging/NetworkLogger.java
blob: 3109bf26e4835ec5b9698fd14a7467c00c4e5fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.libraries.mobiledatadownload.internal.logging;

import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import android.content.Context;

import com.google.android.libraries.mobiledatadownload.Flags;
import com.google.android.libraries.mobiledatadownload.annotations.InstanceId;
import com.google.android.libraries.mobiledatadownload.internal.ApplicationContext;
import com.google.android.libraries.mobiledatadownload.tracing.PropagatedFutures;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.mobiledatadownload.LogProto.DataDownloadFileGroupStats;
import com.google.mobiledatadownload.LogProto.MddNetworkStats;
import com.google.mobiledatadownload.internal.MetadataProto.FileGroupLoggingState;

import java.util.List;

import javax.inject.Inject;

/**
 * Log MDD network stats at daily maintenance. For each file group, it will log the total bytes
 * downloaded on Wifi and Cellular and also total bytes downloaded by MDD on Wifi and Cellular.
 */
public class NetworkLogger {

  private final EventLogger eventLogger;
  private final Flags flags;
  private final LoggingStateStore loggingStateStore;

  @Inject
  public NetworkLogger(
      @ApplicationContext Context context,
      EventLogger eventLogger,
      @InstanceId Optional<String> instanceIdOptional,
      Flags flags,
      LoggingStateStore loggingStateStore) {
    this.eventLogger = eventLogger;
    this.flags = flags;
    this.loggingStateStore = loggingStateStore;
  }

  public ListenableFuture<Void> log() {
    if (!flags.logNetworkStats()) {
      return immediateVoidFuture();
    }

    // Clear the accumulated network usage even if the device isn't logging, otherwise with 1%
    // sampling, we could potentially log network usage for up to 100 days.
    ListenableFuture<List<FileGroupLoggingState>> allDataUsageFuture =
        loggingStateStore.getAndResetAllDataUsage();

    return eventLogger.logMddNetworkStats(
        () ->
            PropagatedFutures.transform(
                allDataUsageFuture, this::buildNetworkStats, directExecutor()));
  }

  private MddNetworkStats buildNetworkStats(List<FileGroupLoggingState> allDataUsage) {
    long totalMddWifiCount = 0;
    long totalMddCellularCount = 0;
    MddNetworkStats.Builder networkStatsBuilder = MddNetworkStats.newBuilder();

    for (FileGroupLoggingState fileGroupLoggingState : allDataUsage) {
      networkStatsBuilder.addGroupStats(
          MddNetworkStats.GroupStats.newBuilder()
              .setDataDownloadFileGroupStats(
                  DataDownloadFileGroupStats.newBuilder()
                      .setOwnerPackage(fileGroupLoggingState.getGroupKey().getOwnerPackage())
                      .setFileGroupName(fileGroupLoggingState.getGroupKey().getGroupName())
                      .setFileGroupVersionNumber(fileGroupLoggingState.getFileGroupVersionNumber())
                      .setBuildId(fileGroupLoggingState.getBuildId())
                      .setVariantId(fileGroupLoggingState.getVariantId())
                      .build())
              .setTotalWifiBytes(fileGroupLoggingState.getWifiUsage())
              .setTotalCellularBytes(fileGroupLoggingState.getCellularUsage()));

      totalMddWifiCount += fileGroupLoggingState.getWifiUsage();
      totalMddCellularCount += fileGroupLoggingState.getCellularUsage();
    }

    networkStatsBuilder
        .setTotalMddWifiBytes(totalMddWifiCount)
        .setTotalMddCellularBytes(totalMddCellularCount);
    return networkStatsBuilder.build();
  }
}