Skip to content

Commit c24794a

Browse files
committed
Address dots in CsvImportValidator
1 parent 6e7a238 commit c24794a

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

lib/CsvImportValidator.class.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function validate()
152152
// Iterate csv rows, calling each test/row.
153153
foreach ($this->rows as $row) {
154154
if ($this->showDisplayProgress) {
155-
echo $this->renderProgressDescription();
155+
$this->renderProgressDescription();
156156
}
157157

158158
$this->validatorCollection->testRow($this->header, $row);
@@ -167,7 +167,7 @@ public function validate()
167167
}
168168

169169
if ($this->showDisplayProgress) {
170-
echo $this->renderProgressDescription(true);
170+
$this->renderProgressDescription(true);
171171
}
172172

173173
return $this->resultCollection;
@@ -365,13 +365,51 @@ public static function validateFilename($filename)
365365

366366
public function renderProgressDescription(bool $complete = false)
367367
{
368-
$output = '.';
368+
// Periodic single-line summaries to STDERR; final summary to STDOUT when complete.
369+
static $startTime = null;
370+
static $lastLogTime = null;
371+
static $processedCount = 0;
369372

370373
if ($complete) {
371-
return "\nAnalysis complete.\n";
374+
if (null === $startTime) {
375+
return ''; // Nothing processed
376+
}
377+
378+
$totalDuration = microtime(true) - $startTime;
379+
$finalRate = $processedCount / max($totalDuration, 1e-9);
380+
echo sprintf(
381+
"\rProcessed %d rows total in %.2fs (%.1f/s)\n",
382+
$processedCount,
383+
$totalDuration,
384+
$finalRate
385+
);
386+
387+
// Reset for potential reuse; no stdout/stderr output
388+
$startTime = null;
389+
$lastLogTime = null;
390+
$processedCount = 0;
391+
392+
return '';
393+
}
394+
395+
if (null === $startTime) {
396+
$startTime = microtime(true);
397+
$lastLogTime = $startTime;
398+
}
399+
400+
++$processedCount;
401+
402+
$now = microtime(true);
403+
if ($now - $lastLogTime >= 5) {
404+
// Ensure elapsed is never zero (avoid div by zero in rate calc below)
405+
$elapsed = max($now - $startTime, 1e-9);
406+
$rate = $processedCount / $elapsed;
407+
fwrite(STDERR, sprintf("\rProcessed %d rows (%.1f/s)", $processedCount, $rate));
408+
fflush(STDERR);
409+
$lastLogTime = $now;
372410
}
373411

374-
return $output;
412+
return '';
375413
}
376414

377415
protected function getLongestRow(): int

0 commit comments

Comments
 (0)