summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/item/SmartMonkeyLogItem.java
blob: a8645e1d2774bb49a1253e4f8a5d062becad45df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.loganalysis.item;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * An {@link IItem} used to store monkey log info.
 */
public class SmartMonkeyLogItem extends GenericItem {

    @SuppressWarnings("serial")
    private class DateSet extends HashSet<Date> {}

    /** Constant for JSON output */
    public static final String START_TIME = "START_TIME";
    /** Constant for JSON output */
    public static final String STOP_TIME = "STOP_TIME";
    /** Constant for JSON output */
    public static final String APPLICATIONS = "APPS";
    /** Constant for JSON output */
    public static final String PACKAGES = "PACKAGES";
    /** Constant for JSON output */
    public static final String THROTTLE = "THROTTLE";
    /** Constant for JSON output */
    public static final String TARGET_INVOCATIONS = "TARGET_INVOCATIONS";
    /** Constant for JSON output */
    public static final String TOTAL_DURATION = "TOTAL_TIME";
    /** Constant for JSON output */
    public static final String START_UPTIME_DURATION = "START_UPTIME";
    /** Constant for JSON output */
    public static final String STOP_UPTIME_DURATION = "STOP_UPTIME";
    /** Constant for JSON output */
    public static final String IS_FINISHED = "IS_FINISHED";
    /** Constant for JSON output */
    public static final String ABORTED = "ABORTED";
    /** Constant for JSON output */
    public static final String INTERMEDIATE_COUNT = "INTERMEDIATE_COUNT";
    /** Constant for JSON output */
    public static final String FINAL_COUNT = "FINAL_COUNT";
    /** Constant for JSON output */
    public static final String ANR_TIMES = "ANR_TIMES";
    /** Constant for JSON output */
    public static final String CRASH_TIMES = "CRASH_TIMES";
    /** Constant for JSON output */
    public static final String INTERMEDIATE_TIME = "INTERMEDIATE_TIME";

    private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
            START_TIME, STOP_TIME, PACKAGES, THROTTLE, TARGET_INVOCATIONS, ABORTED,
            TOTAL_DURATION, START_UPTIME_DURATION, STOP_UPTIME_DURATION, APPLICATIONS,
            IS_FINISHED, INTERMEDIATE_COUNT, FINAL_COUNT, ANR_TIMES, CRASH_TIMES,
            INTERMEDIATE_TIME));

    /**
     * The constructor for {@link MonkeyLogItem}.
     */
    public SmartMonkeyLogItem() {
        super(ATTRIBUTES);

        setAttribute(APPLICATIONS, new ArrayList<String>());
        setAttribute(PACKAGES, new ArrayList<String>());
        setAttribute(CRASH_TIMES, new DateSet());
        setAttribute(ANR_TIMES, new DateSet());
        setAttribute(INTERMEDIATE_TIME, new DateSet());
        setAttribute(THROTTLE, 0);
        setAttribute(FINAL_COUNT, 0);
        setAttribute(IS_FINISHED, false);
        setAttribute(ABORTED, false);
        setAttribute(INTERMEDIATE_COUNT, 0);
        setAttribute(START_UPTIME_DURATION, 0L);
        setAttribute(STOP_UPTIME_DURATION, 0L);
    }

    /**
     * Get the start time of the monkey log.
     */
    public Date getStartTime() {
        return (Date) getAttribute(START_TIME);
    }

    /**
     * Set the start time of the monkey log.
     */
    public void setStartTime(Date time) {
        setAttribute(START_TIME, time);
    }

    /**
     * Set the last time reported for a monkey event
     */
    public void setIntermediateTime(Date time) {
        setAttribute(INTERMEDIATE_TIME, time);
    }

    /**
     * Get the last time reported for a monkey event
     */
    public Date getIntermediateTime() {
        return (Date) getAttribute(INTERMEDIATE_TIME);
    }

    /**
     * Get the stop time of the monkey log.
     */
    public Date getStopTime() {
        return (Date) getAttribute(STOP_TIME);
    }

    /**
     * Set the stop time of the monkey log.
     */
    public void setStopTime(Date time) {
        setAttribute(STOP_TIME, time);
    }

    /**
     * Get the set of packages that the monkey is run on.
     */
    @SuppressWarnings("unchecked")
    public List<String> getPackages() {
        return (List<String>) getAttribute(PACKAGES);
    }

    /**
     * Add a package to the set that the monkey is run on.
     */
    @SuppressWarnings("unchecked")
    public void addPackage(String thePackage) {
        ((List<String>) getAttribute(PACKAGES)).add(thePackage);
    }

    /**
     * Get the set of packages that the monkey is run on.
     */
    @SuppressWarnings("unchecked")
    public List<String> getApplications() {
        return (List<String>) getAttribute(APPLICATIONS);
    }

    /**
     * Add a package to the set that the monkey is run on.
     */
    @SuppressWarnings("unchecked")
    public void addApplication(String theApp) {
        ((List<String>) getAttribute(APPLICATIONS)).add(theApp);
    }

    /**
     * Get the throttle for the monkey run.
     */
    public int getThrottle() {
        return (Integer) getAttribute(THROTTLE);
    }

    /**
     * Set the throttle for the monkey run.
     */
    public void setThrottle(int throttle) {
        setAttribute(THROTTLE, throttle);
    }

    /**
     * Get the target sequence invocations for the monkey run.
     */
    public int getTargetInvocations() {
        return (Integer) getAttribute(TARGET_INVOCATIONS);
    }

    /**
     * Set the target sequence invocations for the monkey run.
     */
    public void setTargetInvocations(int count) {
        setAttribute(TARGET_INVOCATIONS, count);
    }

    /**
     * Get the total duration of the monkey run in milliseconds.
     */
    public long getTotalDuration() {
        if (getIsFinished() || getIsAborted())
            return (Long) getAttribute(TOTAL_DURATION);
        // else it crashed
        Date startTime = getStartTime();
        Date endTime = getIntermediateTime();
        return endTime.getTime() - startTime.getTime() / 1000;
    }

    /**
     * Set the total duration of the monkey run in milliseconds.
     */
    public void setTotalDuration(long time) {
        setAttribute(TOTAL_DURATION, time);
    }

    /**
     * Get the start uptime duration of the monkey run in milliseconds.
     */
    public long getStartUptimeDuration() {
        return (Long) getAttribute(START_UPTIME_DURATION);
    }

    /**
     * Set the start uptime duration of the monkey run in milliseconds.
     */
    public void setStartUptimeDuration(long uptime) {
        setAttribute(START_UPTIME_DURATION, uptime);
    }

    /**
     * Get the stop uptime duration of the monkey run in milliseconds.
     */
    public long getStopUptimeDuration() {
        return (Long) getAttribute(STOP_UPTIME_DURATION);
    }

    /**
     * Set the stop uptime duration of the monkey run in milliseconds.
     */
    public void setStopUptimeDuration(long uptime) {
        setAttribute(STOP_UPTIME_DURATION, uptime);
    }

    /**
     * Get if the monkey run finished without crashing.
     */
    public boolean getIsFinished() {
        return (Boolean) getAttribute(IS_FINISHED);
    }

    /**
     * Set if the monkey run finished without crashing.
     */
    public void setIsFinished(boolean finished) {
        setAttribute(IS_FINISHED, finished);
    }

    /**
     * Get the intermediate count for the monkey run.
     * <p>
     * This count starts at 0 and increments every 100 events. This number should be within 100 of
     * the final count.
     * </p>
     */
    public int getIntermediateCount() {
        return (Integer) getAttribute(INTERMEDIATE_COUNT);
    }

    /**
     * Set the intermediate count for the monkey run.
     * <p>
     * This count starts at 0 and increments every 100 events. This number should be within 100 of
     * the final count.
     * </p>
     */
    public void setIntermediateCount(int count) {
        setAttribute(INTERMEDIATE_COUNT, count);
    }

    /**
     * Get the final count for the monkey run.
     */
    public int getFinalCount() {
        if (getIsFinished())
            return (Integer) getAttribute(FINAL_COUNT);
        return getIntermediateCount();
    }

    /**
     * Set the final count for the monkey run.
     */
    public void setFinalCount(int count) {
        setAttribute(FINAL_COUNT, count);
    }

    /**
     * Get ANR times
     */
    public Set<Date> getAnrTimes() {
        return (DateSet) getAttribute(ANR_TIMES);
    }

    /**
     * Add ANR time
     */
    public void addAnrTime(Date time) {
        ((DateSet) getAttribute(ANR_TIMES)).add(time);
    }

    /**
     * Get Crash times
     */
    public Set<Date> getCrashTimes() {
        return (DateSet) getAttribute(CRASH_TIMES);
    }

    /**
     * Add Crash time
     */
    public void addCrashTime(Date time) {
        ((DateSet) getAttribute(CRASH_TIMES)).add(time);
    }

    /**
     * Get the status of no sequences abort
     */
    public boolean getIsAborted() {
        return (Boolean) getAttribute(ABORTED);
    }

    /**
     * Set the status of no sequences abort
     * @param noSeq
     */
    public void setIsAborted(boolean noSeq) {
        setAttribute(ABORTED, noSeq);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JSONObject toJson() {
        JSONObject object = super.toJson();

        // Override application, packages, and ANR and crash times.
        put(object, APPLICATIONS, new JSONArray(getApplications()));
        put(object, PACKAGES, new JSONArray(getPackages()));
        put(object, ANR_TIMES, new JSONArray(getAnrTimes()));
        put(object, CRASH_TIMES, new JSONArray(getCrashTimes()));

        return object;
    }

    /**
     * Try to put an {@link Object} in a {@link JSONObject} and remove the existing key if it fails.
     */
    private static void put(JSONObject object, String key, Object value) {
        try {
            object.put(key, value);
        } catch (JSONException e) {
            object.remove(key);
        }
    }
}