Skip to content

Commit 4e4617c

Browse files
committed
Tweaked print() logic in the StatusLogger
1 parent 407f41d commit 4e4617c

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

src/javaxt/express/utils/StatusLogger.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public void run() {
8080
* setting the total record count, the status logger will print a percent
8181
* completion status update.
8282
*/
83-
public void setTotalRecords(long n){
83+
public synchronized void setTotalRecords(long n){
84+
if (n<0) return;
8485
totalRecords.set(n);
8586
r.run();
8687
}
@@ -91,7 +92,7 @@ public void setTotalRecords(long n){
9192
//**************************************************************************
9293
/** Returns the total number of records expected to be processed.
9394
*/
94-
public Long getTotalRecords(){
95+
public synchronized Long getTotalRecords(){
9596
return totalRecords.get();
9697
}
9798

@@ -166,16 +167,28 @@ private synchronized void print(AtomicLong recordCounter){
166167
long currTime = System.currentTimeMillis();
167168
double elapsedTime = (currTime-startTime)/1000; //seconds
168169
long x = recordCounter.get();
170+
long total = totalRecords.get();
169171

170172

171-
String rate = "0";
173+
String rateText = "0 records per second";
172174
long recordsPerSecond = 0;
175+
double recordsPerSecondDouble = 0;
173176
try{
174-
recordsPerSecond = Math.round(x/elapsedTime);
175-
if (totalRecords!=null && totalRecords.get()>0){
176-
if (recordsPerSecond>totalRecords.get()) recordsPerSecond = totalRecords.get();
177+
if (elapsedTime > 0 && x > 0) {
178+
recordsPerSecondDouble = x / elapsedTime;
179+
recordsPerSecond = Math.round(recordsPerSecondDouble);
180+
}
181+
if (total>0){
182+
if (recordsPerSecond>total) recordsPerSecond = total;
183+
}
184+
185+
// Display seconds per record if rate is less than 1 record per second
186+
if (recordsPerSecondDouble < 1.0 && x > 0) {
187+
double secondsPerRecord = elapsedTime / x;
188+
rateText = StringUtils.format(secondsPerRecord) + " seconds per record";
189+
} else {
190+
rateText = StringUtils.format(recordsPerSecond) + " records per second";
177191
}
178-
rate = StringUtils.format(recordsPerSecond);
179192
}
180193
catch(Exception e){}
181194

@@ -186,32 +199,36 @@ private synchronized void print(AtomicLong recordCounter){
186199
}
187200
}
188201

189-
statusText = StringUtils.format(x) + " records processed (" + rate + " records per second)";
202+
statusText = StringUtils.format(x) + " records processed (" + rateText + ")";
190203

191204

192-
if (totalRecords!=null && totalRecords.get()>0){
193-
double p = ((double) x / (double) totalRecords.get());
205+
if (total>0){
206+
// Ensure x doesn't exceed total (can happen due to timing)
207+
long displayX = Math.min(x, total);
208+
double p = ((double) displayX / (double) total);
194209
int percentComplete = (int) Math.round(p*100);
195210

196211
String _etc = "---------- --:-- --";
197212
if (elapsedTime>0 && recordsPerSecond>0){
198-
int timeRemaining = (int) Math.round(((totalRecords.get()-x)/recordsPerSecond)/60);
199-
200-
javaxt.utils.Date etc = new javaxt.utils.Date();
201-
etc.add(timeRemaining, "minutes");
213+
long recordsRemaining = Math.max(0, total - displayX);
214+
if (recordsRemaining > 0) {
215+
int timeRemaining = (int) Math.round(((double) recordsRemaining / recordsPerSecond) / 60);
202216

203-
if (percentComplete==100) etc = new javaxt.utils.Date();
204-
if (tz!=null) etc.setTimeZone(tz);
217+
javaxt.utils.Date etc = new javaxt.utils.Date();
218+
etc.add(timeRemaining, "minutes");
205219

206-
_etc = etc.toString("yyyy-MM-dd HH:mm a");
220+
if (tz!=null) etc.setTimeZone(tz);
221+
_etc = etc.toString("yyyy-MM-dd HH:mm a");
222+
}
207223
}
208224

209-
statusText += " " + x + "/" + totalRecords.get() + " " + percentComplete + "% ETC: " + _etc;
225+
statusText += " " + displayX + "/" + total + " " + percentComplete + "% ETC: " + _etc;
210226
}
211227

212228
while (statusText.length()<len) statusText += " ";
213229

214230

215231
System.out.print(statusText + (separateMessages ? "\r\n" : ""));
232+
System.out.flush(); // Ensure output is written immediately
216233
}
217234
}

0 commit comments

Comments
 (0)