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