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.storage.datatypehelpers;
18 
19 import android.content.Context;
20 
21 import com.android.server.healthconnect.storage.TransactionManager;
22 import com.android.server.healthconnect.storage.utils.RecordHelperProvider;
23 
24 /**
25  * Helper class to collect Health Connect database stats for logging.
26  *
27  * @hide
28  */
29 public class DatabaseStatsCollector {
30 
31     /** Get the size of Health Connect database. */
getDatabaseSize(Context context)32     public static long getDatabaseSize(Context context) {
33         final TransactionManager transactionManager = TransactionManager.getInitialisedInstance();
34         return transactionManager.getDatabaseSize(context);
35     }
36 
37     /** Get the number of interval record entries in Health Connect database. */
getNumberOfIntervalRecordRows()38     public static long getNumberOfIntervalRecordRows() {
39         final TransactionManager transactionManager = TransactionManager.getInitialisedInstance();
40         long count = 0L;
41         for (RecordHelper<?> recordHelper : RecordHelperProvider.getRecordHelpers().values()) {
42             if (recordHelper instanceof IntervalRecordHelper
43                     && !(recordHelper instanceof SeriesRecordHelper)) {
44                 count +=
45                         transactionManager.getNumberOfEntriesInTheTable(
46                                 recordHelper.getMainTableName());
47             }
48         }
49         return count;
50     }
51 
52     /** Get the number of series record entries in Health Connect database. */
getNumberOfSeriesRecordRows()53     public static long getNumberOfSeriesRecordRows() {
54         final TransactionManager transactionManager = TransactionManager.getInitialisedInstance();
55         long count = 0L;
56         for (RecordHelper<?> recordHelper : RecordHelperProvider.getRecordHelpers().values()) {
57             if (recordHelper instanceof SeriesRecordHelper) {
58                 count +=
59                         transactionManager.getNumberOfEntriesInTheTable(
60                                 recordHelper.getMainTableName());
61             }
62         }
63         return count;
64     }
65 
66     /** Get the number of instant record entries in Health Connect database. */
getNumberOfInstantRecordRows()67     public static long getNumberOfInstantRecordRows() {
68         final TransactionManager transactionManager = TransactionManager.getInitialisedInstance();
69         long count = 0L;
70         for (RecordHelper<?> recordHelper : RecordHelperProvider.getRecordHelpers().values()) {
71             if (recordHelper instanceof InstantRecordHelper) {
72                 count +=
73                         transactionManager.getNumberOfEntriesInTheTable(
74                                 recordHelper.getMainTableName());
75             }
76         }
77         return count;
78     }
79 
80     /** Get the number of change log entries in Health Connect database. */
getNumberOfChangeLogs()81     public static long getNumberOfChangeLogs() {
82         return TransactionManager.getInitialisedInstance()
83                 .getNumberOfEntriesInTheTable(ChangeLogsHelper.TABLE_NAME);
84     }
85 }
86