Skip to content

Commit d9aed5c

Browse files
committed
Add parameters for approach selection and iteration count
1 parent c43de6d commit d9aed5c

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

app/Http/Controllers/ImportController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ public function store(Request $request, StatsdAPIClient $statsd): JsonResource
8787
'expires' => $expires
8888
])->user()->associate($request->user());
8989

90+
$iterationCount = $request->iteration_count ?? 10;
91+
92+
$useOldApproach = $request->boolean('old_approach') ?? false;
9093
$meta->save();
9194

9295
Bus::chain([
9396
new ValidateCSV($meta),
94-
new ImportCSV($meta)
97+
new ImportCSV($meta, $useOldApproach, $iterationCount)
9598
])->dispatch();
9699

97100
$this->trackImportStats();

app/Jobs/ImportCSV.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ class ImportCSV implements ShouldQueue
2727
*/
2828
protected $meta;
2929

30+
protected $iterationCount;
31+
32+
protected $useOldApproach;
33+
3034
/**
3135
* Create a new job instance.
3236
*
3337
* @return void
3438
*/
35-
public function __construct(ImportMeta $meta)
39+
public function __construct(ImportMeta $meta, bool $useOldApproach = false, int $iterationCount = 10)
3640
{
3741
$this->meta = $meta;
42+
$this->useOldApproach = $useOldApproach;
43+
$this->iterationCount = $iterationCount;
3844
}
3945

4046
/**
@@ -45,15 +51,23 @@ public function __construct(ImportMeta $meta)
4551
public function handle(CSVImportReader $reader)
4652
{
4753
Log::info("======= Start import CSV =======");
54+
$filepath = Storage::disk('local')
55+
->path('mismatch-files/' . $this->meta->filename);
56+
$lineCount = $reader->lines($filepath)->count();
57+
4858
$start = hrtime(true);
4959
$maxTimespan = 0.0;
5060
$minTimespan = 999999999.0;
5161

5262
$totalTimespan = 0.0;
53-
$iterationCount = 10;
63+
$iterationCount = $this->iterationCount;
5464
for ($i = 0; $i < $iterationCount; $i++) {
5565
$startIteration = hrtime(true);
56-
$this->handleOld($reader);
66+
if (!$this->useOldApproach) {
67+
$this->handleNew($reader);
68+
} else {
69+
$this->handleOld($reader);
70+
}
5771
$endIteration = (hrtime(true) - $startIteration) / 1000000;
5872
if ($endIteration > $maxTimespan) {
5973
$maxTimespan = $endIteration;
@@ -65,14 +79,17 @@ public function handle(CSVImportReader $reader)
6579
}
6680
$meanTimespan = $totalTimespan / $iterationCount;
6781
$timespan = (hrtime(true) - $start) / 1000000;
68-
Log::info("Iteration count:\t {$iterationCount}");
82+
$approach = $this->useOldApproach ? 'Old' : 'New';
83+
Log::info("Approach:\t\t $approach");
84+
Log::info("Mismatch count:\t $lineCount");
85+
Log::info("Iteration count:\t $iterationCount");
6986
Log::info("Shortest timespan:\t {$minTimespan}ms");
7087
Log::info("Longest timespan:\t {$maxTimespan}ms");
7188
Log::info("Average timespan:\t {$meanTimespan}ms");
7289
Log::info("======= End import CSV in {$timespan}ms =======");
7390
}
7491

75-
private function handleOld(CSVImportReader $reader)
92+
private function handleNew(CSVImportReader $reader)
7693
{
7794
$filepath = Storage::disk('local')
7895
->path('mismatch-files/' . $this->meta->filename);
@@ -111,6 +128,27 @@ private function handleOld(CSVImportReader $reader)
111128
});
112129
}
113130

131+
132+
private function handleOld(CSVImportReader $reader)
133+
{
134+
$filepath = Storage::disk('local')
135+
->path('mismatch-files/' . $this->meta->filename);
136+
137+
DB::transaction(function () use ($reader, $filepath) {
138+
$reader->lines($filepath)->each(function ($mismatchLine) {
139+
$mismatch = Mismatch::make($mismatchLine);
140+
if ($mismatch->type == null) {
141+
$mismatch->type = 'statement';
142+
}
143+
$mismatch->importMeta()->associate($this->meta);
144+
$mismatch->save();
145+
});
146+
147+
$this->meta->status = 'completed';
148+
$this->meta->save();
149+
});
150+
}
151+
114152
/**
115153
* Handle a job failure.
116154
*

0 commit comments

Comments
 (0)