use of password.pwm.util.ProgressInfo in project pwm by pwm-project.
the class PasswordChangeProgressChecker method figureEstimatedCompletion.
private Instant figureEstimatedCompletion(final ProgressTracker tracker, final Collection<ProgressRecord> progressRecords) {
final Instant minCompletionTime = minCompletionTime(tracker);
final Instant maxCompletionTime = maxCompletionTime(tracker);
final Instant estimatedCompletion;
{
final BigDecimal pctComplete = figureAverageProgress(progressRecords);
LOGGER.trace(pwmSession, "percent complete: " + pctComplete);
final ProgressInfo progressInfo = new ProgressInfo(tracker.beginTime, 100, pctComplete.longValue());
final Instant actualEstimate = progressInfo.estimatedCompletion();
if (actualEstimate.isBefore(minCompletionTime)) {
estimatedCompletion = minCompletionTime;
} else if (actualEstimate.isAfter(maxCompletionTime)) {
estimatedCompletion = maxCompletionTime;
} else {
estimatedCompletion = actualEstimate;
}
}
return estimatedCompletion;
}
use of password.pwm.util.ProgressInfo in project pwm by pwm-project.
the class LocalDBUtility method importLocalDB.
private void importLocalDB(final InputStream inputStream, final Appendable out, final long totalBytes) throws PwmOperationalException, IOException {
this.prepareForImport();
importLineCounter = 0;
if (totalBytes > 0) {
writeStringToOut(out, "total bytes in localdb import source: " + totalBytes);
}
writeStringToOut(out, "beginning localdb import...");
final Instant startTime = Instant.now();
final TransactionSizeCalculator transactionCalculator = new TransactionSizeCalculator(new TransactionSizeCalculator.SettingsBuilder().setDurationGoal(new TimeDuration(100, TimeUnit.MILLISECONDS)).setMinTransactions(50).setMaxTransactions(5 * 1000).createSettings());
final Map<LocalDB.DB, Map<String, String>> transactionMap = new HashMap<>();
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
transactionMap.put(loopDB, new TreeMap<>());
}
final CountingInputStream countingInputStream = new CountingInputStream(inputStream);
final EventRateMeter eventRateMeter = new EventRateMeter(TimeDuration.MINUTE);
final Timer statTimer = new Timer(true);
statTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
String output = "";
if (totalBytes > 0) {
final ProgressInfo progressInfo = new ProgressInfo(startTime, totalBytes, countingInputStream.getByteCount());
output += progressInfo.debugOutput();
} else {
output += "recordsImported=" + importLineCounter;
}
output += ", avgTransactionSize=" + transactionCalculator.getTransactionSize() + ", recordsPerMinute=" + eventRateMeter.readEventRate().setScale(2, BigDecimal.ROUND_DOWN);
writeStringToOut(out, output);
}
}, 30 * 1000, 30 * 1000);
Reader csvReader = null;
try {
csvReader = new InputStreamReader(new GZIPInputStream(countingInputStream, GZIP_BUFFER_SIZE), PwmConstants.DEFAULT_CHARSET);
for (final CSVRecord record : PwmConstants.DEFAULT_CSV_FORMAT.parse(csvReader)) {
importLineCounter++;
eventRateMeter.markEvents(1);
final String dbNameRecordStr = record.get(0);
final LocalDB.DB db = JavaHelper.readEnumFromString(LocalDB.DB.class, null, dbNameRecordStr);
final String key = record.get(1);
final String value = record.get(2);
if (db == null) {
writeStringToOut(out, "ignoring localdb import record #" + importLineCounter + ", invalid DB name '" + dbNameRecordStr + "'");
} else {
transactionMap.get(db).put(key, value);
int cachedTransactions = 0;
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
cachedTransactions += transactionMap.get(loopDB).size();
}
if (cachedTransactions >= transactionCalculator.getTransactionSize()) {
final long startTxnTime = System.currentTimeMillis();
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
localDB.putAll(loopDB, transactionMap.get(loopDB));
transactionMap.get(loopDB).clear();
}
transactionCalculator.recordLastTransactionDuration(TimeDuration.fromCurrent(startTxnTime));
}
}
}
} finally {
LOGGER.trace("import process completed");
statTimer.cancel();
IOUtils.closeQuietly(csvReader);
IOUtils.closeQuietly(countingInputStream);
}
for (final LocalDB.DB loopDB : LocalDB.DB.values()) {
localDB.putAll(loopDB, transactionMap.get(loopDB));
transactionMap.get(loopDB).clear();
}
this.markImportComplete();
writeStringToOut(out, "restore complete, restored " + importLineCounter + " records in " + TimeDuration.fromCurrent(startTime).asLongString());
statTimer.cancel();
}
Aggregations