Skip to content

Commit 6e7a238

Browse files
committed
Replace CLI import/export progress display
- Remove per-row dot output. renderProgressDescription() now writes a single update line to STDERR every ~5s. - Print a final summary to STDOUT.
1 parent 15308df commit 6e7a238

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

lib/QubitFlatfileImport.class.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public function csv($fh, $skipRows = 0)
413413
++$this->status['rows'];
414414

415415
if ($this->displayProgress) {
416-
echo $this->renderProgressDescription();
416+
$this->renderProgressDescription();
417417
}
418418
} else {
419419
++$this->status['rows'];
@@ -424,6 +424,19 @@ public function csv($fh, $skipRows = 0)
424424
$this->stopTimer();
425425
}
426426

427+
// Final summary to STDOUT for logs
428+
$rowsProcessed = $this->getStatus('rows') - $this->getStatus('skippedRows');
429+
$totalDuration = $this->getTimeElapsed();
430+
// Ensure total duration is never zero (avoid div by zero in rate calc below)
431+
$finalRate = $rowsProcessed / max($totalDuration, 1e-9);
432+
$msg = sprintf(
433+
"Processed %d rows total in %.2fs (%.1f/s)\n",
434+
$rowsProcessed,
435+
$totalDuration,
436+
$finalRate
437+
);
438+
echo $this->logError($msg, false);
439+
427440
if ($this->status['duplicates']) {
428441
$msg = sprintf('Duplicates found: %d', $this->status['duplicates']);
429442
echo $this->logError($msg, false);
@@ -547,30 +560,30 @@ public function isUpdating()
547560
*/
548561
public function renderProgressDescription()
549562
{
550-
$output = '.';
563+
// Periodic single-line summaries to STDERR.
564+
static $startTime = null;
565+
static $lastLogTime = null;
566+
static $processedCount = 0;
551567

552-
// return empty string if no intermittant progress display
553-
if (
554-
!isset($this->rowsUntilProgressDisplay)
555-
|| !$this->rowsUntilProgressDisplay
556-
) {
557-
return $output;
568+
if (null === $startTime) {
569+
$startTime = microtime(true);
570+
$lastLogTime = $startTime;
558571
}
559-
// row count isn't incremented until after this is displayed, so add one to reflect reality
560-
$rowsProcessed = $this->getStatus('rows') - $this->getStatus('skippedRows');
561-
$memoryUsageMB = round(memory_get_usage() / (1024 * 1024), 2);
562572

563-
// if this show should be displayed, display it
564-
if (!($rowsProcessed % $this->rowsUntilProgressDisplay)) {
565-
$elapsed = $this->getTimeElapsed();
566-
$elapsedMinutes = round($elapsed / 60, 2);
567-
$averageTime = round($elapsed / $rowsProcessed, 2);
573+
++$processedCount;
568574

569-
$output .= "\n".$rowsProcessed.' rows processed in '.$elapsedMinutes
570-
.' minutes ('.$averageTime.' second/row average, '.$memoryUsageMB." MB used).\n";
575+
$now = microtime(true);
576+
if ($now - $lastLogTime >= 5) {
577+
// Ensure elapsed is never zero (avoid div by zero in rate calc below)
578+
$elapsed = max($now - $startTime, 1e-9);
579+
$rate = $processedCount / $elapsed;
580+
$memoryUsageMB = round(memory_get_usage() / (1024 * 1024), 2);
581+
fwrite(STDERR, sprintf("\rProcessed %d rows (%.1f/s, %.2f MB)", $processedCount, $rate, $memoryUsageMB));
582+
fflush(STDERR);
583+
$lastLogTime = $now;
571584
}
572585

573-
return $output;
586+
return '';
574587
}
575588

576589
/*
@@ -1498,7 +1511,8 @@ protected function startTimer()
14981511
*/
14991512
protected function stopTimer()
15001513
{
1501-
$this->timer->stop();
1514+
// Record elapsed time into the timer's total so elapsed() reports correctly.
1515+
$this->timer->add(false);
15021516
}
15031517

15041518
/**

0 commit comments

Comments
 (0)