Skip to content

Commit

Permalink
Change db transaction place
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanakg committed Feb 13, 2024
1 parent a6c80e8 commit 88ce9f1
Showing 1 changed file with 38 additions and 52 deletions.
90 changes: 38 additions & 52 deletions app/Jobs/ImportCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,34 @@ private function handleNew(CSVImportReader $reader)
$mismatch_attrs = (new Mismatch())->getFillable();
$fileLines = [];
$whereClause = [];
//
DB::transaction(function () use ($reader, $filepath, $mismatch_attrs, &$fileLines, &$whereClause) {
$mismatches_per_upload_user = DB::table('mismatches')
->select($mismatch_attrs)
->join('import_meta', 'mismatches.import_id', '=', 'import_meta.id')
->where('import_meta.user_id', '=', $this->meta->user->id);
$reader->lines($filepath)->each(function ($mismatchLine) use (
$mismatch_attrs,
&$fileLines,
$mismatches_per_upload_user,
&$whereClause
) {

$new_mismatch = Mismatch::make($mismatchLine);
$fileLines[] = $new_mismatch;
$collection = collect($new_mismatch->getAttributes());
$collection->forget('review_status');
$newArray = [['review_status', '!=', 'pending']];

// $start = hrtime(true);

$mismatches_per_upload_user = DB::table('mismatches')
->select($mismatch_attrs)
->join('import_meta', 'mismatches.import_id', '=', 'import_meta.id')
->where('import_meta.user_id', '=', $this->meta->user->id);
$reader->lines($filepath)->each(function ($mismatchLine) use (
$mismatch_attrs,
&$fileLines,
$mismatches_per_upload_user,
&$whereClause
) {

$new_mismatch = Mismatch::make($mismatchLine);
$fileLines[] = $new_mismatch;
$collection = collect($new_mismatch->getAttributes());
$collection->forget('review_status');
$newArray = [['review_status', '!=', 'pending']];
// dd($mismatch_attrs, $collection, $new_mismatch->getAttributes());
$collection->map(function ($item, $key) use (&$newArray) {
if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
$newArray[] = [$key, $item];
}
});
$collection->map(function ($item, $key) use (&$newArray) {
if ($key != 'type') { // key can be empty in the file but in the db always has statement by default
$newArray[] = [$key, $item];
}
});


$whereClause[] = $newArray;
$whereClause[] = $newArray;

// $count = $mismatches_per_upload_user->count();
// $row_in_db = $mismatches_per_upload_user->orWhere(function ($query) use ($newArray) {
Expand All @@ -139,23 +140,21 @@ private function handleNew(CSVImportReader $reader)
// $timespan = (hrtime(true) - $start) / 1000000;
// Log::info("DB save timespan:\t {$timespan}ms");
// }
});

// dd($whereClause);

$mismatches_per_upload_user->where(function ($query) use ($whereClause) {
foreach ($whereClause as $where) {
$query->orWhere(function ($query) use ($where) {
$query->where($where);
});
}
});
});

$start = hrtime(true);
$result = $mismatches_per_upload_user->get();
$timespan = (hrtime(true) - $start) / 1000000;
Log::info("DB check timespan:\t {$timespan}ms");
$mismatches_per_upload_user->where(function ($query) use ($whereClause) {
foreach ($whereClause as $where) {
$query->orWhere(function ($query) use ($where) {
$query->where($where);
});
}
});
$result = $mismatches_per_upload_user->get();
//
// $timespan = (hrtime(true) - $start) / 1000000;
// Log::info("DB check timespan:\t {$timespan}ms");

DB::transaction(function () use ($fileLines, $result) {
foreach ($fileLines as $fileLine) {
if ($result->contains(function ($value, $key) use ($fileLine) {
$metaAttrs = $fileLine->getAttributes();
Expand All @@ -165,25 +164,12 @@ private function handleNew(CSVImportReader $reader)
return false;
}
}
// dd($value, $key, $fileLine);
return true;
})) {
$this->saveMismatch($fileLine);
// dd($fileLine);
}
}

// if ($mismatches_per_upload_user->get()) {
// $timespan = (hrtime(true) - $start) / 1000000;
// Log::info("DB check timespan:\t {$timespan}ms");
// $start = hrtime(true);
//// $this->saveMismatch($new_mismatch);
// $timespan = (hrtime(true) - $start) / 1000000;
// Log::info("DB save timespan:\t {$timespan}ms");
// }

// dd($mismatches_per_upload_user->toSql());

$this->meta->status = 'completed';
$this->meta->save();
});
Expand Down

0 comments on commit 88ce9f1

Please sign in to comment.