• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.networkstack.metrics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.captiveportal.CaptivePortalProbeResult;
22 import android.util.Log;
23 
24 import com.android.internal.util.HexDump;
25 import com.android.server.connectivity.nano.DataStallEventProto;
26 
27 /**
28  * Collection of utilities for data stall metrics.
29  *
30  * To see if the logs are properly sent to statsd, execute following command.
31  *
32  * $ adb shell cmd stats print-logs
33  * $ adb logcat | grep statsd  OR $ adb logcat -b stats
34  *
35  * @hide
36  */
37 public class DataStallStatsUtils {
38     private static final String TAG = DataStallStatsUtils.class.getSimpleName();
39     private static final boolean DBG = false;
40 
probeResultToEnum(@ullable final CaptivePortalProbeResult result)41     private static int probeResultToEnum(@Nullable final CaptivePortalProbeResult result) {
42         if (result == null) return DataStallEventProto.INVALID;
43 
44         if (result.isSuccessful()) {
45             return DataStallEventProto.VALID;
46         } else if (result.isPortal()) {
47             return DataStallEventProto.PORTAL;
48         } else if (result.isPartialConnectivity()) {
49             return DataStallEventProto.PARTIAL;
50         } else {
51             return DataStallEventProto.INVALID;
52         }
53     }
54 
55     /**
56      * Write the metric to {@link StatsLog}.
57      */
write(@onNull final DataStallDetectionStats stats, @NonNull final CaptivePortalProbeResult result)58     public static void write(@NonNull final DataStallDetectionStats stats,
59             @NonNull final CaptivePortalProbeResult result) {
60         int validationResult = probeResultToEnum(result);
61         if (DBG) {
62             Log.d(TAG, "write: " + stats + " with result: " + validationResult
63                     + ", dns: " + HexDump.toHexString(stats.mDns));
64         }
65         NetworkStackStatsLog.write(NetworkStackStatsLog.DATA_STALL_EVENT,
66                 stats.mEvaluationType,
67                 validationResult,
68                 stats.mNetworkType,
69                 stats.mWifiInfo,
70                 stats.mCellularInfo,
71                 stats.mDns);
72     }
73 }
74