1
1
package javaxt .express .utils ;
2
2
3
- import java .util .concurrent .Executors ;
4
- import java .util .concurrent .ScheduledExecutorService ;
5
- import java .util .concurrent .TimeUnit ;
3
+ import java .util .TimeZone ;
4
+ import java .util .concurrent .*;
6
5
import java .util .concurrent .atomic .AtomicLong ;
7
6
8
7
13
12
* Used to print status messages to the standard output stream. Status
14
13
* messages are written every second and appear in the following format:
15
14
* <pre>0 records processed (0 records per second)</pre>
16
- * A percent completion is appended to the status message if a "totalRecords"
17
- * counter is given.<br/>
15
+ * A percent completion and an estimated time to completion (ETC) is appended
16
+ * to the status message if a "totalRecords" counter is given.<br/>
18
17
* The status logger is run in a separate thread. The "recordCounter" is
19
18
* updated by the caller. Example:
20
19
<pre>
@@ -37,6 +36,7 @@ public class StatusLogger {
37
36
private ScheduledExecutorService executor ;
38
37
private Runnable r ;
39
38
private boolean separateMessages = false ;
39
+ private TimeZone tz ;
40
40
41
41
42
42
//**************************************************************************
@@ -63,12 +63,13 @@ public void run() {
63
63
AtomicLong totalRecords = me .totalRecords ;
64
64
65
65
String rate = "0" ;
66
+ long recordsPerSecond = 0 ;
66
67
try {
67
- long r = Math .round (x /elapsedTime );
68
+ recordsPerSecond = Math .round (x /elapsedTime );
68
69
if (totalRecords !=null && totalRecords .get ()>0 ){
69
- if (r >totalRecords .get ()) r = totalRecords .get ();
70
+ if (recordsPerSecond >totalRecords .get ()) recordsPerSecond = totalRecords .get ();
70
71
}
71
- rate = StringUtils .format (r );
72
+ rate = StringUtils .format (recordsPerSecond );
72
73
}
73
74
catch (Exception e ){}
74
75
@@ -84,8 +85,22 @@ public void run() {
84
85
85
86
if (totalRecords !=null && totalRecords .get ()>0 ){
86
87
double p = ((double ) x / (double ) totalRecords .get ());
87
- int currPercent = (int ) Math .round (p *100 );
88
- statusText += " " + x + "/" + totalRecords .get () + " " + currPercent + "%" ;
88
+ int percentComplete = (int ) Math .round (p *100 );
89
+
90
+ String _etc = "---------- --:-- --" ;
91
+ if (elapsedTime >0 && recordsPerSecond >0 ){
92
+ int timeRemaining = (int ) Math .round (((totalRecords .get ()-x )/recordsPerSecond )/60 );
93
+
94
+ javaxt .utils .Date etc = new javaxt .utils .Date ();
95
+ etc .add (timeRemaining , "minutes" );
96
+
97
+ if (percentComplete ==100 ) etc = new javaxt .utils .Date ();
98
+ if (tz !=null ) etc .setTimeZone (tz );
99
+
100
+ _etc = etc .toString ("yyyy-MM-dd HH:mm a" );
101
+ }
102
+
103
+ statusText += " " + x + "/" + totalRecords .get () + " " + percentComplete + "% ETC: " + _etc ;
89
104
}
90
105
91
106
while (statusText .length ()<len ) statusText += " " ;
@@ -124,6 +139,41 @@ public Long getTotalRecords(){
124
139
}
125
140
126
141
142
+ //**************************************************************************
143
+ //** setTimeZone
144
+ //**************************************************************************
145
+ /** Used to set the timezone when reporting ETC (estimated time to
146
+ * completion). ETC is rendered only if the total record count is known
147
+ * (see setTotalRecords). If no timezone is specified, ETC will default
148
+ * to the system timezone.
149
+ * @param timezone Name of a timezone (e.g. "America/New York", "UTC", etc)
150
+ */
151
+ public void setTimeZone (String timezone ){
152
+ tz = javaxt .utils .Date .getTimeZone (timezone );
153
+ }
154
+
155
+
156
+ //**************************************************************************
157
+ //** setTimeZone
158
+ //**************************************************************************
159
+ /** Used to set the timezone when reporting ETC (see above)
160
+ */
161
+ public void setTimeZone (TimeZone timezone ){
162
+ tz = timezone ;
163
+ }
164
+
165
+
166
+ //**************************************************************************
167
+ //** getTimeZone
168
+ //**************************************************************************
169
+ /** Returns the timezone used to report ETC (see setTimeZone). Will return
170
+ * null if a timezone has not been set.
171
+ */
172
+ public TimeZone getTimeZone (){
173
+ return tz ;
174
+ }
175
+
176
+
127
177
//**************************************************************************
128
178
//** separateMessages
129
179
//**************************************************************************
0 commit comments