1 /*
2  * Copyright (C) 2023 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.request;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 
22 import com.android.server.healthconnect.storage.utils.SqlJoin;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Objects;
29 
30 /**
31  * Collection of parameters of {@link AggregateTableRequest}.
32  *
33  * @hide
34  */
35 public final class AggregateParams {
36     private final String mTableName;
37 
38     /** Column used for time filtering. Start time for interval records. */
39     private String mTimeColumnName;
40 
41     private final List<String> mColumnsToFetch;
42     private SqlJoin mJoin;
43 
44     // Additional column used for time filtering. End time for interval records,
45     // null for other records.
46     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
47     private String mExtraTimeColumnName = null;
48 
49     private String mTimeOffsetColumnName;
50 
51     private PriorityAggregationExtraParams mPriorityAggregationExtraParams;
52 
53     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
AggregateParams(String tableName, List<String> columnsToFetch)54     public AggregateParams(String tableName, List<String> columnsToFetch) {
55         this(tableName, columnsToFetch, null);
56     }
57 
58     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
AggregateParams( String tableName, List<String> columnsToFetch, Class<?> priorityColumnDataType)59     public AggregateParams(
60             String tableName, List<String> columnsToFetch, Class<?> priorityColumnDataType) {
61         mTableName = tableName;
62         mColumnsToFetch = new ArrayList<>();
63         mColumnsToFetch.addAll(columnsToFetch);
64 
65         // TODO(b/277776749): remove dependency on columns orders
66         mPriorityAggregationExtraParams =
67                 new PriorityAggregationExtraParams(columnsToFetch.get(0), priorityColumnDataType);
68     }
69 
getJoin()70     public SqlJoin getJoin() {
71         return mJoin;
72     }
73 
getTableName()74     public String getTableName() {
75         return mTableName;
76     }
77 
getTimeColumnName()78     public String getTimeColumnName() {
79         return mTimeColumnName;
80     }
81 
getExtraTimeColumnName()82     public String getExtraTimeColumnName() {
83         return mExtraTimeColumnName;
84     }
85 
getColumnsToFetch()86     public List<String> getColumnsToFetch() {
87         return mColumnsToFetch;
88     }
89 
getTimeOffsetColumnName()90     public String getTimeOffsetColumnName() {
91         return mTimeOffsetColumnName;
92     }
93 
94     /** Sets join type. */
setJoin(SqlJoin join)95     public AggregateParams setJoin(SqlJoin join) {
96         mJoin = join;
97         return this;
98     }
99 
setTimeColumnName(String columnName)100     public AggregateParams setTimeColumnName(String columnName) {
101         mTimeColumnName = columnName;
102         return this;
103     }
104 
105     /** Appends additional columns to fetch. */
appendAdditionalColumns(List<String> additionColumns)106     public AggregateParams appendAdditionalColumns(List<String> additionColumns) {
107         mColumnsToFetch.addAll(additionColumns);
108         return this;
109     }
110 
111     /** Sets params for priority aggregation. */
setPriorityAggregationExtraParams( PriorityAggregationExtraParams extraParams)112     public AggregateParams setPriorityAggregationExtraParams(
113             PriorityAggregationExtraParams extraParams) {
114         mPriorityAggregationExtraParams = extraParams;
115         return this;
116     }
117 
118     /** Returns params for priority aggregation. */
getPriorityAggregationExtraParams()119     public PriorityAggregationExtraParams getPriorityAggregationExtraParams() {
120         return mPriorityAggregationExtraParams;
121     }
122 
setExtraTimeColumn(String extraTimeColumn)123     public AggregateParams setExtraTimeColumn(String extraTimeColumn) {
124         mExtraTimeColumnName = extraTimeColumn;
125         return this;
126     }
127 
setOffsetColumnToFetch(@onNull String mainTimeColumnOffset)128     public AggregateParams setOffsetColumnToFetch(@NonNull String mainTimeColumnOffset) {
129         Objects.requireNonNull(mainTimeColumnOffset);
130         mTimeOffsetColumnName = mainTimeColumnOffset;
131         return this;
132     }
133 
134     /** Collections of parameters of priority AggregationRequest. */
135     public static class PriorityAggregationExtraParams {
136 
137         public static final int VALUE_TYPE_LONG = 0;
138         public static final int VALUE_TYPE_DOUBLE = 1;
139 
140         /** @hide */
141         @IntDef({
142             VALUE_TYPE_LONG,
143             VALUE_TYPE_DOUBLE,
144         })
145         @Retention(RetentionPolicy.SOURCE)
146         public @interface ValueColumnType {}
147 
148         @ValueColumnType private int mColumnToAggregateType;
149 
150         private String mColumnToAggregateName;
151         private String mExcludeIntervalEndColumnName;
152         private String mExcludeIntervalStartColumnName;
153 
154         @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
PriorityAggregationExtraParams( String excludeIntervalStartColumnName, String excludeIntervalEndColumnName)155         public PriorityAggregationExtraParams(
156                 String excludeIntervalStartColumnName, String excludeIntervalEndColumnName) {
157             mExcludeIntervalStartColumnName = excludeIntervalStartColumnName;
158             mExcludeIntervalEndColumnName = excludeIntervalEndColumnName;
159         }
160 
161         @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
PriorityAggregationExtraParams( String columnToAggregateName, Class<?> aggregationType)162         public PriorityAggregationExtraParams(
163                 String columnToAggregateName, Class<?> aggregationType) {
164             mColumnToAggregateName = columnToAggregateName;
165             // TODO(b/277776749): use intdef instead of unlimited Class<?>
166             mColumnToAggregateType =
167                     (aggregationType == Long.class ? VALUE_TYPE_LONG : VALUE_TYPE_DOUBLE);
168         }
169 
getExcludeIntervalStartColumnName()170         public String getExcludeIntervalStartColumnName() {
171             return mExcludeIntervalStartColumnName;
172         }
173 
getExcludeIntervalEndColumnName()174         public String getExcludeIntervalEndColumnName() {
175             return mExcludeIntervalEndColumnName;
176         }
177 
178         @ValueColumnType
getColumnToAggregateType()179         public int getColumnToAggregateType() {
180             return mColumnToAggregateType;
181         }
182 
getColumnToAggregateName()183         public String getColumnToAggregateName() {
184             return mColumnToAggregateName;
185         }
186     }
187 }
188