1 | package org.expeditee.stats;
|
---|
2 |
|
---|
3 | import java.awt.event.KeyEvent;
|
---|
4 | import java.awt.event.MouseEvent;
|
---|
5 | import java.sql.Time;
|
---|
6 | import java.text.SimpleDateFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Date;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.expeditee.gui.DisplayIO;
|
---|
12 | import org.expeditee.gui.FrameUtils;
|
---|
13 | import org.expeditee.gui.TimeKeeper;
|
---|
14 | import org.expeditee.io.Logger;
|
---|
15 |
|
---|
16 | public class SessionStats {
|
---|
17 |
|
---|
18 | private static final long MILLISECONDS_PER_MINUTE = 6000;
|
---|
19 |
|
---|
20 | private static Date _FrameAccessTime = new Date();
|
---|
21 |
|
---|
22 | private static List<String> _FrameEvents = new ArrayList<String>();
|
---|
23 |
|
---|
24 | // statistics counters
|
---|
25 | private static final int MOUSE_BUTTONS = 3;
|
---|
26 |
|
---|
27 | private static StringBuffer _FramesEdited = new StringBuffer();
|
---|
28 |
|
---|
29 | private static int[] _MouseCounters = new int[MOUSE_BUTTONS];
|
---|
30 |
|
---|
31 | private static int _AccessedFrames = -1;
|
---|
32 |
|
---|
33 | private static int _SavedFrames = 0;
|
---|
34 |
|
---|
35 | // the number of frames created via TDFC
|
---|
36 | private static int _CreatedFrames = 0;
|
---|
37 |
|
---|
38 | // character counts
|
---|
39 | private static int[] _CharCounts = new int[255];
|
---|
40 |
|
---|
41 | // how many items have been created thus far
|
---|
42 | private static int _CreatedItems = 0;
|
---|
43 |
|
---|
44 | private static Date _StartTime = new Date();
|
---|
45 |
|
---|
46 | private static long _LastEvent = new Date().getTime();
|
---|
47 |
|
---|
48 | private static Time _DarkTime = new Time(0);
|
---|
49 |
|
---|
50 | private static final long DARK_TIME_THRESHOLD = 60000;
|
---|
51 |
|
---|
52 | private static int _EscapeCount = 0;
|
---|
53 |
|
---|
54 | private static boolean _StatsEnabled = true;
|
---|
55 |
|
---|
56 | public static String getCurrentStats() {
|
---|
57 | StringBuffer stats = new StringBuffer("Date: ");
|
---|
58 |
|
---|
59 | stats.append(Logger.EasyDateFormat("ddMMMyyyy")).append("\n");
|
---|
60 | stats.append("SessionTime: ").append(getTimeElapsed()).append("\n");
|
---|
61 | stats.append("DarkTime: ").append( new SimpleDateFormat("m").format(_DarkTime) ).append("\n");
|
---|
62 |
|
---|
63 | stats.append(getFrameStats());
|
---|
64 | stats.append(getResponseStats()).append("\n");
|
---|
65 |
|
---|
66 | stats.append(getCharacterStats());
|
---|
67 |
|
---|
68 | stats.append("Version: ").append(DisplayIO.TITLE);
|
---|
69 |
|
---|
70 | return stats.toString();
|
---|
71 | }
|
---|
72 |
|
---|
73 | private static String getTimeElapsed() {
|
---|
74 | SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
|
---|
75 | Date currentTime = new Date();
|
---|
76 | Time elapsedTime = new Time(currentTime.getTime()-_StartTime.getTime());
|
---|
77 | String time = formatter.format(_StartTime);
|
---|
78 | time += " --> " + formatter.format(currentTime);
|
---|
79 | time += " = " + (new SimpleDateFormat("m").format(elapsedTime));
|
---|
80 |
|
---|
81 | return time;
|
---|
82 | }
|
---|
83 |
|
---|
84 | private static String getResponseStats() {
|
---|
85 | String stats = "ResponseTime: "
|
---|
86 | + TimeKeeper.Formatter.format(FrameUtils.getLastResponseTime())
|
---|
87 | + "sec, ";
|
---|
88 |
|
---|
89 | if (_AccessedFrames > 0)
|
---|
90 | stats += TimeKeeper.Formatter.format(FrameUtils
|
---|
91 | .getResponseTimeTotal()
|
---|
92 | / _AccessedFrames)
|
---|
93 | + "avg";
|
---|
94 | else
|
---|
95 | stats += "--avg";
|
---|
96 |
|
---|
97 | return stats;
|
---|
98 | }
|
---|
99 |
|
---|
100 | private static String getFrameStats() {
|
---|
101 | return getFrameStats(true);
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static String getFrameStats(boolean newline) {
|
---|
105 | StringBuffer stats = new StringBuffer();
|
---|
106 | appendStat(stats, "FramesAccessed", _AccessedFrames, newline);
|
---|
107 | appendStat(stats, "FramesEdited", _SavedFrames, newline);
|
---|
108 | return stats.toString();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private static int getMinutesUsed() {
|
---|
112 | long elapsedTime = new Date().getTime() - _StartTime.getTime() - _DarkTime.getTime();
|
---|
113 |
|
---|
114 | return (int) (elapsedTime / MILLISECONDS_PER_MINUTE);
|
---|
115 | }
|
---|
116 |
|
---|
117 | private static String getCharacterStats() {
|
---|
118 | int[] counts = _CharCounts;
|
---|
119 |
|
---|
120 | int chars = 0;
|
---|
121 |
|
---|
122 | for (int i = 'A'; i <= 'Z'; i++)
|
---|
123 | chars += counts[i];
|
---|
124 |
|
---|
125 | for (int i = 'a'; i <= 'z'; i++)
|
---|
126 | chars += counts[i];
|
---|
127 |
|
---|
128 | for (int i = '0'; i <= '9'; i++)
|
---|
129 | chars += counts[i];
|
---|
130 |
|
---|
131 | chars -= counts[KeyEvent.VK_BACK_SPACE];
|
---|
132 | chars -= counts[KeyEvent.VK_DELETE];
|
---|
133 |
|
---|
134 | int EOS = counts['.'] + counts[','] + counts['!'] + counts['?'];
|
---|
135 |
|
---|
136 | int punct = counts[' '] + counts[';'] + counts[':'];
|
---|
137 | chars += counts['('] + counts[')'] + counts['\''] + counts['"']
|
---|
138 | + counts['+'] + counts['='] + counts['-'];
|
---|
139 |
|
---|
140 | StringBuffer stats = new StringBuffer();
|
---|
141 | appendStat(stats, "Chars", chars + punct + EOS);
|
---|
142 | appendStat(stats, "Words", punct + EOS);
|
---|
143 | appendStat(stats, "Sentences", EOS);
|
---|
144 | appendStat(stats, "Items", _CreatedItems);
|
---|
145 | appendStat(stats, "Frames", _CreatedFrames);
|
---|
146 | appendStat(stats, "Escape", _EscapeCount);
|
---|
147 | appendStat(stats, "Left", _MouseCounters[MouseEvent.BUTTON1 - 1]);
|
---|
148 | appendStat(stats, "Middle", _MouseCounters[MouseEvent.BUTTON2 - 1]);
|
---|
149 | appendStat(stats, "Right", _MouseCounters[MouseEvent.BUTTON3 - 1]);
|
---|
150 |
|
---|
151 | return stats.toString();
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Appends a single stats to a string buffer containing a collection of
|
---|
156 | * stats.
|
---|
157 | *
|
---|
158 | * @param stats
|
---|
159 | * The string buffer to append the stat onto
|
---|
160 | * @param name
|
---|
161 | * The name of the stat
|
---|
162 | * @param value
|
---|
163 | * The new value for the stat
|
---|
164 | */
|
---|
165 | private static void appendStat(StringBuffer stats, String name, int value,
|
---|
166 | boolean newline) {
|
---|
167 | // prevent divide by zero errors
|
---|
168 | if (value < 0)
|
---|
169 | value = 0;
|
---|
170 |
|
---|
171 | int perHour = value * 60 / Math.max(1, getMinutesUsed());
|
---|
172 |
|
---|
173 | stats.append(value).append(" ").append(name).append(" @ ").append(perHour).append("/hour");
|
---|
174 | if (newline)
|
---|
175 | stats.append("\n");
|
---|
176 | else
|
---|
177 | stats.append(" ");
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | private static void appendStat(StringBuffer stats, String name, int value) {
|
---|
182 | appendStat(stats, name, value, true);
|
---|
183 | }
|
---|
184 |
|
---|
185 | public static void resetStats() {
|
---|
186 | _StartTime = new Date();
|
---|
187 | _AccessedFrames = 0;
|
---|
188 | _SavedFrames = 0;
|
---|
189 | _CreatedFrames = 0;
|
---|
190 | _CharCounts = new int[255];
|
---|
191 | _CreatedItems = 0;
|
---|
192 | _EscapeCount = 0;
|
---|
193 | _DarkTime.setTime(0);
|
---|
194 | _MouseCounters = new int[MOUSE_BUTTONS];
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Called signal that a frame has been accessed.
|
---|
199 | *
|
---|
200 | */
|
---|
201 | public static void AccessedFrame() {
|
---|
202 | if (_StatsEnabled) {
|
---|
203 | _AccessedFrames++;
|
---|
204 | _FrameEvents.clear();
|
---|
205 | _FrameAccessTime = new Date();
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | public static void SavedFrame(String frameName) {
|
---|
210 | FrameEdited(frameName);
|
---|
211 | _SavedFrames++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static void Escape() {
|
---|
215 | _EscapeCount++;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public static void CreatedFrame() {
|
---|
219 | _CreatedFrames++;
|
---|
220 | }
|
---|
221 |
|
---|
222 | public static void CreatedItem() {
|
---|
223 | _CreatedItems++;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Increments the count for a character that is typed.
|
---|
228 | *
|
---|
229 | * @param ch
|
---|
230 | * ascii value for the typed character
|
---|
231 | */
|
---|
232 | public static void TypedChar(int ch) {
|
---|
233 | UserEvent();
|
---|
234 | _CharCounts[ch]++;
|
---|
235 | }
|
---|
236 |
|
---|
237 | private static void UserEvent() {
|
---|
238 | long thisEvent = new Date().getTime();
|
---|
239 | long elapsedTime = thisEvent - _LastEvent;
|
---|
240 | if (elapsedTime > DARK_TIME_THRESHOLD)
|
---|
241 | _DarkTime.setTime(_DarkTime.getTime() + elapsedTime);
|
---|
242 | _LastEvent = thisEvent;
|
---|
243 | }
|
---|
244 |
|
---|
245 | public static void AddFrameEvent(String description) {
|
---|
246 | Date elapsedTime = new Date((new Date()).getTime()
|
---|
247 | - _FrameAccessTime.getTime());
|
---|
248 |
|
---|
249 | _FrameEvents.add(Logger.EasyDateFormat("mm:ss", elapsedTime) + " "
|
---|
250 | + DisplayIO.getMouseX() + " " + DisplayIO.getMouseY() + " "
|
---|
251 | + description);
|
---|
252 | }
|
---|
253 |
|
---|
254 | public static String getFrameEventList() {
|
---|
255 | StringBuilder eventList = new StringBuilder();
|
---|
256 | for (String s : _FrameEvents)
|
---|
257 | eventList.append(s + '\n');
|
---|
258 |
|
---|
259 | return eventList.toString();
|
---|
260 | }
|
---|
261 |
|
---|
262 | public static void MouseClicked(int button) {
|
---|
263 | UserEvent();
|
---|
264 | _MouseCounters[button - 1]++;
|
---|
265 | }
|
---|
266 |
|
---|
267 | public static void setEnabled(boolean value) {
|
---|
268 | _StatsEnabled = true;
|
---|
269 | }
|
---|
270 |
|
---|
271 | private static void FrameEdited(String name) {
|
---|
272 | _FramesEdited.append(Logger.EasyDateFormat("ddMMMyyyy-HHmm:ss"))
|
---|
273 | .append("[").append(name).append("]\n");
|
---|
274 | }
|
---|
275 |
|
---|
276 | public static String getFramesEdited() {
|
---|
277 | return _FramesEdited.toString();
|
---|
278 | }
|
---|
279 | }
|
---|