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.cobalt.data;
18 
19 import androidx.annotation.NonNull;
20 import androidx.room.ColumnInfo;
21 import androidx.room.Embedded;
22 import androidx.room.Entity;
23 import androidx.room.ForeignKey;
24 import androidx.room.Index;
25 
26 import com.google.auto.value.AutoValue;
27 import com.google.auto.value.AutoValue.CopyAnnotations;
28 import com.google.cobalt.AggregateValue;
29 
30 /**
31  * Stores aggregate values of unique reports for a given event vector, day, and system profile.
32  *
33  * <p>References the Reports and SystemProfiles tables to ensure necessary data are kept in sync.
34  */
35 @AutoValue
36 @CopyAnnotations
37 @Entity(
38         tableName = "AggregateStore",
39         primaryKeys = {
40             "customer_id",
41             "project_id",
42             "metric_id",
43             "report_id",
44             "day_index",
45             "system_profile_hash",
46             "event_vector"
47         },
48         foreignKeys = {
49             @ForeignKey(
50                     entity = ReportEntity.class,
51                     parentColumns = {"customer_id", "project_id", "metric_id", "report_id"},
52                     childColumns = {"customer_id", "project_id", "metric_id", "report_id"},
53                     onDelete = ForeignKey.CASCADE),
54             @ForeignKey(
55                     entity = SystemProfileEntity.class,
56                     parentColumns = {"system_profile_hash"},
57                     childColumns = {"system_profile_hash"})
58         },
59         indices = {@Index(value = {"system_profile_hash"})})
60 abstract class AggregateStoreEntity {
61     /** The values uniquely identifying the report. */
62     @CopyAnnotations
63     @Embedded
64     @NonNull
reportKey()65     abstract ReportKey reportKey();
66 
67     /** The day the value is being aggregated on. */
68     @CopyAnnotations
69     @ColumnInfo(name = "day_index")
70     @NonNull
dayIndex()71     abstract int dayIndex();
72 
73     /** The event being aggregated. */
74     @CopyAnnotations
75     @ColumnInfo(name = "event_vector")
76     @NonNull
eventVector()77     abstract EventVector eventVector();
78 
79     /** The system profile hash of the value being aggregated. */
80     @CopyAnnotations
81     @ColumnInfo(name = "system_profile_hash")
82     @NonNull
systemProfileHash()83     abstract long systemProfileHash();
84 
85     /** The aggregated value. */
86     @CopyAnnotations
87     @ColumnInfo(name = "aggregate_value")
88     @NonNull
aggregateValue()89     abstract AggregateValue aggregateValue();
90 
91     /**
92      * Creates an {@link AggregateStoreEntity}.
93      *
94      * <p>Used by Room to instantiate objects.
95      */
96     @NonNull
create( ReportKey reportKey, int dayIndex, EventVector eventVector, long systemProfileHash, AggregateValue aggregateValue)97     static AggregateStoreEntity create(
98             ReportKey reportKey,
99             int dayIndex,
100             EventVector eventVector,
101             long systemProfileHash,
102             AggregateValue aggregateValue) {
103         return new AutoValue_AggregateStoreEntity(
104                 reportKey, dayIndex, eventVector, systemProfileHash, aggregateValue);
105     }
106 }
107