• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 package com.android.loganalysis.item;
17 
18 import org.json.JSONArray;
19 import org.json.JSONException;
20 import org.json.JSONObject;
21 
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 
29 /**
30  * An {@link IItem} used to store monkey log info.
31  */
32 public class SmartMonkeyLogItem extends GenericItem {
33 
34     @SuppressWarnings("serial")
35     private class DateSet extends HashSet<Date> {}
36 
37     /** Constant for JSON output */
38     public static final String START_TIME = "START_TIME";
39     /** Constant for JSON output */
40     public static final String STOP_TIME = "STOP_TIME";
41     /** Constant for JSON output */
42     public static final String APPLICATIONS = "APPS";
43     /** Constant for JSON output */
44     public static final String PACKAGES = "PACKAGES";
45     /** Constant for JSON output */
46     public static final String THROTTLE = "THROTTLE";
47     /** Constant for JSON output */
48     public static final String TARGET_INVOCATIONS = "TARGET_INVOCATIONS";
49     /** Constant for JSON output */
50     public static final String TOTAL_DURATION = "TOTAL_TIME";
51     /** Constant for JSON output */
52     public static final String START_UPTIME_DURATION = "START_UPTIME";
53     /** Constant for JSON output */
54     public static final String STOP_UPTIME_DURATION = "STOP_UPTIME";
55     /** Constant for JSON output */
56     public static final String IS_FINISHED = "IS_FINISHED";
57     /** Constant for JSON output */
58     public static final String ABORTED = "ABORTED";
59     /** Constant for JSON output */
60     public static final String INTERMEDIATE_COUNT = "INTERMEDIATE_COUNT";
61     /** Constant for JSON output */
62     public static final String FINAL_COUNT = "FINAL_COUNT";
63     /** Constant for JSON output */
64     public static final String ANR_TIMES = "ANR_TIMES";
65     /** Constant for JSON output */
66     public static final String CRASH_TIMES = "CRASH_TIMES";
67     /** Constant for JSON output */
68     public static final String INTERMEDIATE_TIME = "INTERMEDIATE_TIME";
69 
70     private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
71             START_TIME, STOP_TIME, PACKAGES, THROTTLE, TARGET_INVOCATIONS, ABORTED,
72             TOTAL_DURATION, START_UPTIME_DURATION, STOP_UPTIME_DURATION, APPLICATIONS,
73             IS_FINISHED, INTERMEDIATE_COUNT, FINAL_COUNT, ANR_TIMES, CRASH_TIMES,
74             INTERMEDIATE_TIME));
75 
76     /**
77      * The constructor for {@link MonkeyLogItem}.
78      */
SmartMonkeyLogItem()79     public SmartMonkeyLogItem() {
80         super(ATTRIBUTES);
81 
82         setAttribute(APPLICATIONS, new ArrayList<String>());
83         setAttribute(PACKAGES, new ArrayList<String>());
84         setAttribute(CRASH_TIMES, new DateSet());
85         setAttribute(ANR_TIMES, new DateSet());
86         setAttribute(INTERMEDIATE_TIME, new DateSet());
87         setAttribute(THROTTLE, 0);
88         setAttribute(FINAL_COUNT, 0);
89         setAttribute(IS_FINISHED, false);
90         setAttribute(ABORTED, false);
91         setAttribute(INTERMEDIATE_COUNT, 0);
92         setAttribute(START_UPTIME_DURATION, 0L);
93         setAttribute(STOP_UPTIME_DURATION, 0L);
94     }
95 
96     /**
97      * Get the start time of the monkey log.
98      */
getStartTime()99     public Date getStartTime() {
100         return (Date) getAttribute(START_TIME);
101     }
102 
103     /**
104      * Set the start time of the monkey log.
105      */
setStartTime(Date time)106     public void setStartTime(Date time) {
107         setAttribute(START_TIME, time);
108     }
109 
110     /**
111      * Set the last time reported for a monkey event
112      */
setIntermediateTime(Date time)113     public void setIntermediateTime(Date time) {
114         setAttribute(INTERMEDIATE_TIME, time);
115     }
116 
117     /**
118      * Get the last time reported for a monkey event
119      */
getIntermediateTime()120     public Date getIntermediateTime() {
121         return (Date) getAttribute(INTERMEDIATE_TIME);
122     }
123 
124     /**
125      * Get the stop time of the monkey log.
126      */
getStopTime()127     public Date getStopTime() {
128         return (Date) getAttribute(STOP_TIME);
129     }
130 
131     /**
132      * Set the stop time of the monkey log.
133      */
setStopTime(Date time)134     public void setStopTime(Date time) {
135         setAttribute(STOP_TIME, time);
136     }
137 
138     /**
139      * Get the set of packages that the monkey is run on.
140      */
141     @SuppressWarnings("unchecked")
getPackages()142     public List<String> getPackages() {
143         return (List<String>) getAttribute(PACKAGES);
144     }
145 
146     /**
147      * Add a package to the set that the monkey is run on.
148      */
149     @SuppressWarnings("unchecked")
addPackage(String thePackage)150     public void addPackage(String thePackage) {
151         ((List<String>) getAttribute(PACKAGES)).add(thePackage);
152     }
153 
154     /**
155      * Get the set of packages that the monkey is run on.
156      */
157     @SuppressWarnings("unchecked")
getApplications()158     public List<String> getApplications() {
159         return (List<String>) getAttribute(APPLICATIONS);
160     }
161 
162     /**
163      * Add a package to the set that the monkey is run on.
164      */
165     @SuppressWarnings("unchecked")
addApplication(String theApp)166     public void addApplication(String theApp) {
167         ((List<String>) getAttribute(APPLICATIONS)).add(theApp);
168     }
169 
170     /**
171      * Get the throttle for the monkey run.
172      */
getThrottle()173     public int getThrottle() {
174         return (Integer) getAttribute(THROTTLE);
175     }
176 
177     /**
178      * Set the throttle for the monkey run.
179      */
setThrottle(int throttle)180     public void setThrottle(int throttle) {
181         setAttribute(THROTTLE, throttle);
182     }
183 
184     /**
185      * Get the target sequence invocations for the monkey run.
186      */
getTargetInvocations()187     public int getTargetInvocations() {
188         return (Integer) getAttribute(TARGET_INVOCATIONS);
189     }
190 
191     /**
192      * Set the target sequence invocations for the monkey run.
193      */
setTargetInvocations(int count)194     public void setTargetInvocations(int count) {
195         setAttribute(TARGET_INVOCATIONS, count);
196     }
197 
198     /**
199      * Get the total duration of the monkey run in milliseconds.
200      */
getTotalDuration()201     public long getTotalDuration() {
202         if (getIsFinished() || getIsAborted())
203             return (Long) getAttribute(TOTAL_DURATION);
204         // else it crashed
205         Date startTime = getStartTime();
206         Date endTime = getIntermediateTime();
207         return endTime.getTime() - startTime.getTime() / 1000;
208     }
209 
210     /**
211      * Set the total duration of the monkey run in milliseconds.
212      */
setTotalDuration(long time)213     public void setTotalDuration(long time) {
214         setAttribute(TOTAL_DURATION, time);
215     }
216 
217     /**
218      * Get the start uptime duration of the monkey run in milliseconds.
219      */
getStartUptimeDuration()220     public long getStartUptimeDuration() {
221         return (Long) getAttribute(START_UPTIME_DURATION);
222     }
223 
224     /**
225      * Set the start uptime duration of the monkey run in milliseconds.
226      */
setStartUptimeDuration(long uptime)227     public void setStartUptimeDuration(long uptime) {
228         setAttribute(START_UPTIME_DURATION, uptime);
229     }
230 
231     /**
232      * Get the stop uptime duration of the monkey run in milliseconds.
233      */
getStopUptimeDuration()234     public long getStopUptimeDuration() {
235         return (Long) getAttribute(STOP_UPTIME_DURATION);
236     }
237 
238     /**
239      * Set the stop uptime duration of the monkey run in milliseconds.
240      */
setStopUptimeDuration(long uptime)241     public void setStopUptimeDuration(long uptime) {
242         setAttribute(STOP_UPTIME_DURATION, uptime);
243     }
244 
245     /**
246      * Get if the monkey run finished without crashing.
247      */
getIsFinished()248     public boolean getIsFinished() {
249         return (Boolean) getAttribute(IS_FINISHED);
250     }
251 
252     /**
253      * Set if the monkey run finished without crashing.
254      */
setIsFinished(boolean finished)255     public void setIsFinished(boolean finished) {
256         setAttribute(IS_FINISHED, finished);
257     }
258 
259     /**
260      * Get the intermediate count for the monkey run.
261      * <p>
262      * This count starts at 0 and increments every 100 events. This number should be within 100 of
263      * the final count.
264      * </p>
265      */
getIntermediateCount()266     public int getIntermediateCount() {
267         return (Integer) getAttribute(INTERMEDIATE_COUNT);
268     }
269 
270     /**
271      * Set the intermediate count for the monkey run.
272      * <p>
273      * This count starts at 0 and increments every 100 events. This number should be within 100 of
274      * the final count.
275      * </p>
276      */
setIntermediateCount(int count)277     public void setIntermediateCount(int count) {
278         setAttribute(INTERMEDIATE_COUNT, count);
279     }
280 
281     /**
282      * Get the final count for the monkey run.
283      */
getFinalCount()284     public int getFinalCount() {
285         if (getIsFinished())
286             return (Integer) getAttribute(FINAL_COUNT);
287         return getIntermediateCount();
288     }
289 
290     /**
291      * Set the final count for the monkey run.
292      */
setFinalCount(int count)293     public void setFinalCount(int count) {
294         setAttribute(FINAL_COUNT, count);
295     }
296 
297     /**
298      * Get ANR times
299      */
getAnrTimes()300     public Set<Date> getAnrTimes() {
301         return (DateSet) getAttribute(ANR_TIMES);
302     }
303 
304     /**
305      * Add ANR time
306      */
addAnrTime(Date time)307     public void addAnrTime(Date time) {
308         ((DateSet) getAttribute(ANR_TIMES)).add(time);
309     }
310 
311     /**
312      * Get Crash times
313      */
getCrashTimes()314     public Set<Date> getCrashTimes() {
315         return (DateSet) getAttribute(CRASH_TIMES);
316     }
317 
318     /**
319      * Add Crash time
320      */
addCrashTime(Date time)321     public void addCrashTime(Date time) {
322         ((DateSet) getAttribute(CRASH_TIMES)).add(time);
323     }
324 
325     /**
326      * Get the status of no sequences abort
327      */
getIsAborted()328     public boolean getIsAborted() {
329         return (Boolean) getAttribute(ABORTED);
330     }
331 
332     /**
333      * Set the status of no sequences abort
334      * @param noSeq
335      */
setIsAborted(boolean noSeq)336     public void setIsAborted(boolean noSeq) {
337         setAttribute(ABORTED, noSeq);
338     }
339 
340     /**
341      * {@inheritDoc}
342      */
343     @Override
toJson()344     public JSONObject toJson() {
345         JSONObject object = super.toJson();
346 
347         // Override application, packages, and ANR and crash times.
348         put(object, APPLICATIONS, new JSONArray(getApplications()));
349         put(object, PACKAGES, new JSONArray(getPackages()));
350         put(object, ANR_TIMES, new JSONArray(getAnrTimes()));
351         put(object, CRASH_TIMES, new JSONArray(getCrashTimes()));
352 
353         return object;
354     }
355 
356     /**
357      * Try to put an {@link Object} in a {@link JSONObject} and remove the existing key if it fails.
358      */
put(JSONObject object, String key, Object value)359     private static void put(JSONObject object, String key, Object value) {
360         try {
361             object.put(key, value);
362         } catch (JSONException e) {
363             object.remove(key);
364         }
365     }
366 }
367