Skip to content

Commit 5767a98

Browse files
authored
Merge pull request #15649 from bryanlopezinc/ImproveImporting
Improve import performance
2 parents 0c820cb + 524a442 commit 5767a98

File tree

2 files changed

+28
-54
lines changed

2 files changed

+28
-54
lines changed

app/Console/Commands/ObjectImportCommand.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputOption;
88
use Illuminate\Support\Facades\Log;
9+
use Symfony\Component\Console\Helper\ProgressIndicator;
910

1011
ini_set('max_execution_time', env('IMPORT_TIME_LIMIT', 600)); //600 seconds = 10 minutes
1112
ini_set('memory_limit', env('IMPORT_MEMORY_LIMIT', '500M'));
@@ -29,6 +30,11 @@ class ObjectImportCommand extends Command
2930
*/
3031
protected $description = 'Import Items from CSV';
3132

33+
/**
34+
* The progress indicator instance.
35+
*/
36+
protected ProgressIndicator $progressIndicator;
37+
3238
/**
3339
* Create a new command instance.
3440
*
@@ -39,15 +45,15 @@ public function __construct()
3945
parent::__construct();
4046
}
4147

42-
private $bar;
43-
4448
/**
4549
* Execute the console command.
4650
*
4751
* @return mixed
4852
*/
4953
public function handle()
5054
{
55+
$this->progressIndicator = new ProgressIndicator($this->output);
56+
5157
$filename = $this->argument('filename');
5258
$class = title_case($this->option('item-type'));
5359
$classString = "App\\Importer\\{$class}Importer";
@@ -61,46 +67,25 @@ public function handle()
6167
// This $logFile/useFiles() bit is currently broken, so commenting it out for now
6268
// $logFile = $this->option('logfile');
6369
// Log::useFiles($logFile);
64-
$this->comment('======= Importing Items from '.$filename.' =========');
65-
$importer->import();
70+
$this->progressIndicator->start('======= Importing Items from '.$filename.' =========');
6671

67-
$this->bar = null;
72+
$importer->import();
6873

69-
if (! empty($this->errors)) {
70-
$this->comment('The following Errors were encountered.');
71-
foreach ($this->errors as $asset => $error) {
72-
$this->comment('Error: Item: '.$asset.' failed validation: '.json_encode($error));
73-
}
74-
} else {
75-
$this->comment('All Items imported successfully!');
76-
}
77-
$this->comment('');
74+
$this->progressIndicator->finish('Import finished.');
7875
}
7976

80-
public function errorCallback($item, $field, $errorString)
77+
public function errorCallback($item, $field, $error)
8178
{
82-
$this->errors[$item->name][$field] = $errorString;
79+
$this->output->write("\x0D\x1B[2K");
80+
81+
$this->warn('Error: Item: '.$item->name.' failed validation: '.json_encode($error));
8382
}
8483

85-
public function progress($count)
84+
public function progress($importedItemsCount)
8685
{
87-
if (! $this->bar) {
88-
$this->bar = $this->output->createProgressBar($count);
89-
}
90-
static $index = 0;
91-
$index++;
92-
if ($index < $count) {
93-
$this->bar->advance();
94-
} else {
95-
$this->bar->finish();
96-
}
86+
$this->progressIndicator->advance();
9787
}
9888

99-
// Tracks the current item for error messages
100-
private $updating;
101-
// An array of errors encountered while parsing
102-
private $errors;
103-
10489
/**
10590
* Log a message to file, configurable by the --log-file parameter.
10691
* If a warning message is passed, we'll spit it to the console as well.

app/Importer/Importer.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ abstract class Importer
2121
* Id of User performing import
2222
* @var
2323
*/
24-
2524
protected $created_by;
2625
/**
2726
* Are we updating items in the import
@@ -149,17 +148,23 @@ public function import()
149148
{
150149
$headerRow = $this->csv->fetchOne();
151150
$this->csv->setHeaderOffset(0); //explicitly sets the CSV document header record
152-
$results = $this->normalizeInputArray($this->csv->getRecords($headerRow));
153151

154152
$this->populateCustomFields($headerRow);
155153

156-
DB::transaction(function () use (&$results) {
154+
DB::transaction(function () use ($headerRow) {
155+
$importedItemsCount = 0;
157156
Model::unguard();
158-
$resultsCount = count($results);
159-
foreach ($results as $row) {
157+
158+
foreach ($this->csv->getRecords($headerRow) as $row) {
159+
//Lowercase header values to ensure we're comparing values properly.
160+
$row = array_change_key_case($row, CASE_LOWER);
161+
160162
$this->handle($row);
163+
164+
$importedItemsCount++;
165+
161166
if ($this->progressCallback) {
162-
call_user_func($this->progressCallback, $resultsCount);
167+
call_user_func($this->progressCallback, $importedItemsCount);
163168
}
164169

165170
$this->log('------------- Action Summary ----------------');
@@ -237,22 +242,6 @@ public function lookupCustomKey($key)
237242
return $key;
238243
}
239244

240-
/**
241-
* Used to lowercase header values to ensure we're comparing values properly.
242-
*
243-
* @param $results
244-
* @return array
245-
*/
246-
public function normalizeInputArray($results)
247-
{
248-
$newArray = [];
249-
foreach ($results as $index => $arrayToNormalize) {
250-
$newArray[$index] = array_change_key_case($arrayToNormalize);
251-
}
252-
253-
return $newArray;
254-
}
255-
256245
/**
257246
* Figure out the fieldname of the custom field
258247
*

0 commit comments

Comments
 (0)