1 /*
2  * Copyright (C) 2022 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.server.healthconnect.logging;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.health.HealthFitnessStatsLog;
22 
23 import com.android.server.healthconnect.storage.datatypehelpers.DatabaseStatsCollector;
24 
25 import java.util.Objects;
26 
27 /**
28  * Class to log Health Connect database stats.
29  *
30  * @hide
31  */
32 class DatabaseStatsLogger {
33 
34     /** Write Health Connect database stats to statsd. */
log(@onNull Context context)35     static void log(@NonNull Context context) {
36 
37         long numberOfInstantRecords = DatabaseStatsCollector.getNumberOfInstantRecordRows();
38         long numberOfIntervalRecords = DatabaseStatsCollector.getNumberOfIntervalRecordRows();
39         long numberOfSeriesRecords = DatabaseStatsCollector.getNumberOfSeriesRecordRows();
40         long numberOfChangeLogs = DatabaseStatsCollector.getNumberOfChangeLogs();
41 
42         // If this condition is true then the user does not uses HC and we should not collect data.
43         // This will reduce the load on logging service otherwise we will get daily data from
44         // billions of Android devices.
45         if (numberOfInstantRecords == 0
46                 && numberOfIntervalRecords == 0
47                 && numberOfSeriesRecords == 0
48                 && numberOfChangeLogs == 0) {
49             return;
50         }
51 
52         Objects.requireNonNull(context);
53         HealthFitnessStatsLog.write(
54                 HealthFitnessStatsLog.HEALTH_CONNECT_STORAGE_STATS,
55                 DatabaseStatsCollector.getDatabaseSize(context),
56                 numberOfInstantRecords,
57                 numberOfIntervalRecords,
58                 numberOfSeriesRecords,
59                 numberOfChangeLogs);
60     }
61 }
62