File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package javaxt.express.utils;
22

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.*;
65
import java.util.concurrent.atomic.AtomicLong;
76

87

@@ -13,8 +12,8 @@
1312
* Used to print status messages to the standard output stream. Status
1413
* messages are written every second and appear in the following format:
1514
* <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/>
1817
* The status logger is run in a separate thread. The "recordCounter" is
1918
* updated by the caller. Example:
2019
<pre>
@@ -37,6 +36,7 @@ public class StatusLogger {
3736
private ScheduledExecutorService executor;
3837
private Runnable r;
3938
private boolean separateMessages = false;
39+
private TimeZone tz;
4040

4141

4242
//**************************************************************************
@@ -63,12 +63,13 @@ public void run() {
6363
AtomicLong totalRecords = me.totalRecords;
6464

6565
String rate = "0";
66+
long recordsPerSecond = 0;
6667
try{
67-
long r = Math.round(x/elapsedTime);
68+
recordsPerSecond = Math.round(x/elapsedTime);
6869
if (totalRecords!=null && totalRecords.get()>0){
69-
if (r>totalRecords.get()) r = totalRecords.get();
70+
if (recordsPerSecond>totalRecords.get()) recordsPerSecond = totalRecords.get();
7071
}
71-
rate = StringUtils.format(r);
72+
rate = StringUtils.format(recordsPerSecond);
7273
}
7374
catch(Exception e){}
7475

@@ -84,8 +85,22 @@ public void run() {
8485

8586
if (totalRecords!=null && totalRecords.get()>0){
8687
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;
89104
}
90105

91106
while (statusText.length()<len) statusText += " ";
@@ -124,6 +139,41 @@ public Long getTotalRecords(){
124139
}
125140

126141

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+
127177
//**************************************************************************
128178
//** separateMessages
129179
//**************************************************************************

0 commit comments

Comments
 (0)