1 | /**
|
---|
2 | * Stats.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.stats;
|
---|
20 |
|
---|
21 | import java.sql.Time;
|
---|
22 | import java.util.Date;
|
---|
23 |
|
---|
24 |
|
---|
25 | public abstract class Stats {
|
---|
26 | protected static Time _DarkTime = new Time(0);
|
---|
27 | protected static Date _StartTime = new Date();
|
---|
28 |
|
---|
29 | /**The time the current frame was accessed (displayed)*/
|
---|
30 | protected static Date _FrameAccessTime = new Date();
|
---|
31 |
|
---|
32 | /**The session dark time when the current frame was accessed (displayed)*/
|
---|
33 | protected static Date _FrameAccessDarkTime = new Date();
|
---|
34 |
|
---|
35 | protected static final long MILLISECONDS_PER_MINUTE = 60000;
|
---|
36 |
|
---|
37 | public static final char ROW_SEPARATOR_CHAR = '\u2500';
|
---|
38 |
|
---|
39 | public static final char COLUMN_SEPARATOR_CHAR = '\u2502';
|
---|
40 |
|
---|
41 | public static final char ROW_COLUMN_SEPARATOR_CHAR = '\u253C';
|
---|
42 |
|
---|
43 | public static final char BOTTOM_COLUMN_SEPARATOR_CHAR = '\u2534';
|
---|
44 |
|
---|
45 | public static final char TOP_COLUMN_SEPARATOR_CHAR = '\u252C';
|
---|
46 |
|
---|
47 | public static final String COLUMN_SEPARATOR = " " + COLUMN_SEPARATOR_CHAR
|
---|
48 | + ' ';
|
---|
49 |
|
---|
50 | public static final String ROW_COLUMN_SEPARATOR = "" + ROW_SEPARATOR_CHAR + ROW_COLUMN_SEPARATOR_CHAR + ROW_SEPARATOR_CHAR;
|
---|
51 |
|
---|
52 | public static final String DARK_TIME_ATTRIBUTE = "DarkTime:";
|
---|
53 |
|
---|
54 | public static final String ACTIVE_TIME_ATTRIBUTE = "ActiveTime:";
|
---|
55 |
|
---|
56 | protected static final int DEFAULT_RATE_WIDTH = 4;
|
---|
57 |
|
---|
58 | protected static final int DEFAULT_VALUE_WIDTH = 3;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Appends a single stats to a string buffer containing a collection of
|
---|
62 | * stats.
|
---|
63 | *
|
---|
64 | * @param stats
|
---|
65 | * The string buffer to append the stat onto
|
---|
66 | * @param name
|
---|
67 | * The name of the stat
|
---|
68 | * @param value
|
---|
69 | * The new value for the stat
|
---|
70 | */
|
---|
71 | protected static void appendStat(StringBuffer stats, String name, int value,
|
---|
72 | boolean newline, boolean ignoreZero, int minValueWidth,
|
---|
73 | int minRateWidth) {
|
---|
74 | // prevent divide by zero errors
|
---|
75 | if (ignoreZero && value <= 0)
|
---|
76 | return;
|
---|
77 |
|
---|
78 | String perHour = getRate(value);
|
---|
79 | while (perHour.length() < minRateWidth)
|
---|
80 | perHour = " " + perHour;
|
---|
81 | String valueString = "" + value;
|
---|
82 | while (valueString.length() < minValueWidth)
|
---|
83 | valueString = " " + valueString;
|
---|
84 | stats.append(valueString).append(" @ ").append(perHour.toString())
|
---|
85 | .append("/hour ").append(name);
|
---|
86 | if (newline)
|
---|
87 | stats.append("\n");
|
---|
88 | else
|
---|
89 | stats.append(" ");
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected static StringBuffer getCompactStat(String name, int value,
|
---|
94 | int minValueWidth, int minTotalWidth) {
|
---|
95 | StringBuffer stats = new StringBuffer();
|
---|
96 | // prevent divide by zero errors
|
---|
97 | if (value > 0) {
|
---|
98 | String perHour = getRate(value);
|
---|
99 | String valueString = "" + value;
|
---|
100 | while (valueString.length() < minValueWidth)
|
---|
101 | valueString = " " + valueString;
|
---|
102 | stats.append(valueString).append("@").append(perHour.toString())
|
---|
103 | .append("/h");
|
---|
104 | }
|
---|
105 | while (stats.length() < minTotalWidth) {
|
---|
106 | stats.append(' ');
|
---|
107 | }
|
---|
108 | return stats.append(COLUMN_SEPARATOR);
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected static String getRate(int value) {
|
---|
112 | return "" + Math.round(value * 60 / getMinutesUsed());
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected static void appendStat(StringBuffer stats, String name, int value) {
|
---|
116 | appendStat(stats, name, value, true, false, DEFAULT_VALUE_WIDTH,
|
---|
117 | DEFAULT_RATE_WIDTH);
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected static double getMinutesUsed() {
|
---|
121 | long elapsedTime = new Date().getTime() - _StartTime.getTime()
|
---|
122 | - _DarkTime.getTime();
|
---|
123 |
|
---|
124 | return (double) elapsedTime / MILLISECONDS_PER_MINUTE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static StringBuffer getDate() {
|
---|
128 | StringBuffer stats = new StringBuffer("@Date: ");
|
---|
129 | stats.append(Formatter.getDateTime()).append("\n");
|
---|
130 | return stats;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public static StringBuffer getBufferedString(String string, int width) {
|
---|
134 | return getBufferedString(string, width, ' ');
|
---|
135 | }
|
---|
136 |
|
---|
137 | public static StringBuffer getBufferedString(int width, char bufferChar) {
|
---|
138 | return getBufferedString("", width, bufferChar);
|
---|
139 | }
|
---|
140 |
|
---|
141 | public static StringBuffer getBufferedString(int width) {
|
---|
142 | return getBufferedString("", width);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public static StringBuffer getBufferedString(String start, int width,
|
---|
146 | char bufferChar) {
|
---|
147 | StringBuffer sb = new StringBuffer(start);
|
---|
148 | while (sb.length() < width) {
|
---|
149 | sb.append(bufferChar);
|
---|
150 | }
|
---|
151 | return sb;
|
---|
152 | }
|
---|
153 |
|
---|
154 | public static Time getFrameDarkTime() {
|
---|
155 | return new Time(_DarkTime.getTime() - _FrameAccessDarkTime.getTime());
|
---|
156 | }
|
---|
157 |
|
---|
158 | public static Time getFrameActiveTime() {
|
---|
159 | return new Time(getFrameTotalTime().getTime()
|
---|
160 | - getFrameDarkTime().getTime());
|
---|
161 | }
|
---|
162 |
|
---|
163 | public static Time getFrameTotalTime() {
|
---|
164 | return new Time((new Date()).getTime() - _FrameAccessTime.getTime());
|
---|
165 | }
|
---|
166 | }
|
---|