|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.background.healthreport; |
|
6 |
|
7 import org.json.JSONObject; |
|
8 |
|
9 import android.database.Cursor; |
|
10 import android.util.SparseArray; |
|
11 |
|
12 /** |
|
13 * Abstraction over storage for Firefox Health Report on Android. |
|
14 */ |
|
15 public interface HealthReportStorage { |
|
16 // Right now we only care about the name of the field. |
|
17 public interface MeasurementFields { |
|
18 public class FieldSpec { |
|
19 public final String name; |
|
20 public final int type; |
|
21 public FieldSpec(String name, int type) { |
|
22 this.name = name; |
|
23 this.type = type; |
|
24 } |
|
25 } |
|
26 Iterable<FieldSpec> getFields(); |
|
27 } |
|
28 |
|
29 public abstract class Field { |
|
30 protected static final int UNKNOWN_TYPE_OR_FIELD_ID = -1; |
|
31 |
|
32 protected static final int FLAG_INTEGER = 1 << 0; |
|
33 protected static final int FLAG_STRING = 1 << 1; |
|
34 protected static final int FLAG_JSON = 1 << 2; |
|
35 |
|
36 protected static final int FLAG_DISCRETE = 1 << 8; |
|
37 protected static final int FLAG_LAST = 1 << 9; |
|
38 protected static final int FLAG_COUNTER = 1 << 10; |
|
39 |
|
40 protected static final int FLAG_COUNTED = 1 << 14; |
|
41 |
|
42 public static final int TYPE_INTEGER_DISCRETE = FLAG_INTEGER | FLAG_DISCRETE; |
|
43 public static final int TYPE_INTEGER_LAST = FLAG_INTEGER | FLAG_LAST; |
|
44 public static final int TYPE_INTEGER_COUNTER = FLAG_INTEGER | FLAG_COUNTER; |
|
45 |
|
46 public static final int TYPE_STRING_DISCRETE = FLAG_STRING | FLAG_DISCRETE; |
|
47 public static final int TYPE_STRING_LAST = FLAG_STRING | FLAG_LAST; |
|
48 |
|
49 public static final int TYPE_JSON_DISCRETE = FLAG_JSON | FLAG_DISCRETE; |
|
50 public static final int TYPE_JSON_LAST = FLAG_JSON | FLAG_LAST; |
|
51 |
|
52 public static final int TYPE_COUNTED_STRING_DISCRETE = FLAG_COUNTED | TYPE_STRING_DISCRETE; |
|
53 |
|
54 protected int fieldID = UNKNOWN_TYPE_OR_FIELD_ID; |
|
55 protected int flags; |
|
56 |
|
57 protected final String measurementName; |
|
58 protected final String measurementVersion; |
|
59 protected final String fieldName; |
|
60 |
|
61 public Field(String mName, int mVersion, String fieldName, int type) { |
|
62 this.measurementName = mName; |
|
63 this.measurementVersion = Integer.toString(mVersion, 10); |
|
64 this.fieldName = fieldName; |
|
65 this.flags = type; |
|
66 } |
|
67 |
|
68 /** |
|
69 * @return the ID for this <code>Field</code> |
|
70 * @throws IllegalStateException if this field is not found in storage |
|
71 */ |
|
72 public abstract int getID() throws IllegalStateException; |
|
73 |
|
74 public boolean isIntegerField() { |
|
75 return (this.flags & FLAG_INTEGER) > 0; |
|
76 } |
|
77 |
|
78 public boolean isStringField() { |
|
79 return (this.flags & FLAG_STRING) > 0; |
|
80 } |
|
81 |
|
82 public boolean isJSONField() { |
|
83 return (this.flags & FLAG_JSON) > 0; |
|
84 } |
|
85 |
|
86 public boolean isStoredAsString() { |
|
87 return (this.flags & (FLAG_JSON | FLAG_STRING)) > 0; |
|
88 } |
|
89 |
|
90 public boolean isDiscreteField() { |
|
91 return (this.flags & FLAG_DISCRETE) > 0; |
|
92 } |
|
93 |
|
94 /** |
|
95 * True if the accrued values are intended to be bucket-counted. For strings, |
|
96 * each discrete value will name a bucket, with the number of instances per |
|
97 * day being the value in the bucket. |
|
98 */ |
|
99 public boolean isCountedField() { |
|
100 return (this.flags & FLAG_COUNTED) > 0; |
|
101 } |
|
102 } |
|
103 |
|
104 /** |
|
105 * Close open storage handles and otherwise finish up. |
|
106 */ |
|
107 public void close(); |
|
108 |
|
109 /** |
|
110 * Return the day integer corresponding to the provided time. |
|
111 * |
|
112 * @param time |
|
113 * milliseconds since Unix epoch. |
|
114 * @return an integer day. |
|
115 */ |
|
116 public int getDay(long time); |
|
117 |
|
118 /** |
|
119 * Return the day integer corresponding to the current time. |
|
120 * |
|
121 * @return an integer day. |
|
122 */ |
|
123 public int getDay(); |
|
124 |
|
125 /** |
|
126 * Return a new {@link Environment}, suitable for being populated, hashed, and |
|
127 * registered. |
|
128 * |
|
129 * @return a new {@link Environment} instance. |
|
130 */ |
|
131 public Environment getEnvironment(); |
|
132 |
|
133 /** |
|
134 * @return a mapping from environment IDs to hashes, suitable for use in |
|
135 * payload generation. |
|
136 */ |
|
137 public SparseArray<String> getEnvironmentHashesByID(); |
|
138 |
|
139 /** |
|
140 * @return a mapping from environment IDs to registered {@link Environment} |
|
141 * records, suitable for use in payload generation. |
|
142 */ |
|
143 public SparseArray<Environment> getEnvironmentRecordsByID(); |
|
144 |
|
145 /** |
|
146 * @param id |
|
147 * the environment ID, as returned by {@link Environment#register()}. |
|
148 * @return a cursor for the record. |
|
149 */ |
|
150 public Cursor getEnvironmentRecordForID(int id); |
|
151 |
|
152 /** |
|
153 * @param measurement |
|
154 * the name of a measurement, such as "org.mozilla.appInfo.appInfo". |
|
155 * @param measurementVersion |
|
156 * the version of a measurement, such as '3'. |
|
157 * @param fieldName |
|
158 * the name of a field, such as "platformVersion". |
|
159 * |
|
160 * @return a {@link Field} instance corresponding to the provided values. |
|
161 */ |
|
162 public Field getField(String measurement, int measurementVersion, |
|
163 String fieldName); |
|
164 |
|
165 /** |
|
166 * @return a mapping from field IDs to {@link Field} instances, suitable for |
|
167 * use in payload generation. |
|
168 */ |
|
169 public SparseArray<Field> getFieldsByID(); |
|
170 |
|
171 public void recordDailyLast(int env, int day, int field, JSONObject value); |
|
172 public void recordDailyLast(int env, int day, int field, String value); |
|
173 public void recordDailyLast(int env, int day, int field, int value); |
|
174 public void recordDailyDiscrete(int env, int day, int field, JSONObject value); |
|
175 public void recordDailyDiscrete(int env, int day, int field, String value); |
|
176 public void recordDailyDiscrete(int env, int day, int field, int value); |
|
177 public void incrementDailyCount(int env, int day, int field, int by); |
|
178 public void incrementDailyCount(int env, int day, int field); |
|
179 |
|
180 /** |
|
181 * Return true if events exist that were recorded on or after <code>time</code>. |
|
182 */ |
|
183 boolean hasEventSince(long time); |
|
184 |
|
185 /** |
|
186 * Obtain a cursor over events that were recorded since <code>time</code>. |
|
187 * This cursor exposes 'raw' events, with integer identifiers for values. |
|
188 */ |
|
189 public Cursor getRawEventsSince(long time); |
|
190 |
|
191 /** |
|
192 * Obtain a cursor over events that were recorded since <code>time</code>. |
|
193 * |
|
194 * This cursor exposes 'friendly' events, with string names and full |
|
195 * measurement metadata. |
|
196 */ |
|
197 public Cursor getEventsSince(long time); |
|
198 |
|
199 /** |
|
200 * Ensure that a measurement and all of its fields are registered with the DB. |
|
201 * No fields will be processed if the measurement exists with the specified |
|
202 * version. |
|
203 * |
|
204 * @param measurement |
|
205 * a measurement name, such as "org.mozila.appInfo.appInfo". |
|
206 * @param version |
|
207 * a version number, such as '3'. |
|
208 * @param fields |
|
209 * a {@link MeasurementFields} instance, consisting of a collection |
|
210 * of field names. |
|
211 */ |
|
212 public void ensureMeasurementInitialized(String measurement, |
|
213 int version, |
|
214 MeasurementFields fields); |
|
215 public Cursor getMeasurementVersions(); |
|
216 public Cursor getFieldVersions(); |
|
217 public Cursor getFieldVersions(String measurement, int measurementVersion); |
|
218 |
|
219 public void deleteEverything(); |
|
220 public void deleteEnvironments(); |
|
221 public void deleteMeasurements(); |
|
222 /** |
|
223 * Deletes all environments, addons, and events from the database before the given time. |
|
224 * |
|
225 * @param time milliseconds since epoch. |
|
226 * @param curEnv The ID of the current environment. |
|
227 * @return The number of environments and addon entries deleted. |
|
228 */ |
|
229 public int deleteDataBefore(final long time, final int curEnv); |
|
230 |
|
231 public int getEventCount(); |
|
232 public int getEnvironmentCount(); |
|
233 |
|
234 public void pruneEvents(final int num); |
|
235 public void pruneEnvironments(final int num); |
|
236 |
|
237 public void enqueueOperation(Runnable runnable); |
|
238 } |