Skip to content

Commit

Permalink
fix: replaced the or with and condition
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Nov 20, 2024
1 parent 905f092 commit 885c2db
Showing 1 changed file with 92 additions and 84 deletions.
176 changes: 92 additions & 84 deletions app/Console/Commands/DedupeFixCollectionSourceLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use App\Models\Molecule;
use DB;
use Illuminate\Console\Command;
use \OwenIt\Auditing\Events\AuditCustom;
use Illuminate\Support\Facades\Event;
use OwenIt\Auditing\Events\AuditCustom;

class DedupeFixCollectionSourceLinks extends Command
{
Expand All @@ -24,17 +24,16 @@ class DedupeFixCollectionSourceLinks extends Command
*/
protected $description = 'Dedupes and fixes source links for specific collections mentioned in the command';


public function handle()
{
$batchSize = 1000; // Number of molecules to process in each batch
$data = []; // Array to store data for batch updating
$db_links = [
30 => "https://bidd.group/NPASS/compound.php?compoundID=",
42 => "http://langelabtools.wsu.edu/nmr/record/",
43 => "http://132.230.102.198:8000/streptomedb/get_drugcard/",
54 => "https://go.drugbank.com/drugs/",
58 => "https://www.way2drug.com/phyto4health/compound_card.php?compound_id="
30 => 'https://bidd.group/NPASS/compound.php?compoundID=',
42 => 'http://langelabtools.wsu.edu/nmr/record/',
43 => 'http://132.230.102.198:8000/streptomedb/get_drugcard/',
54 => 'https://go.drugbank.com/drugs/',
58 => 'https://www.way2drug.com/phyto4health/compound_card.php?compound_id=',
];
$batchCount = 1;

Expand All @@ -44,62 +43,64 @@ public function handle()
->orderBy('id')
->chunk($batchSize, function ($collection_molecules) use (&$data, $db_links, &$batchCount) {
$this->info('started batch ');
foreach ($collection_molecules as $collection_molecule) {
$url = null;
$reference = null;

// Get the source URL and reference for the molecule
$entries = DB::select("SELECT link, reference_id FROM entries WHERE collection_id = {$collection_molecule->collection_id} and molecule_id = {$collection_molecule->molecule_id};");

foreach ($entries as $index => $entry) {

if ($index == 0) {
switch ($collection_molecule->collection_id) {
case 30:
case 42:
case 43:
case 54:
case 58:
$url = $db_links[$collection_molecule->collection_id] . $entry->reference_id;
break;
default:
$url = $entry->link;
break;
if ($batchCount >= 29) {
foreach ($collection_molecules as $collection_molecule) {
$url = null;
$reference = null;

// Get the source URL and reference for the molecule
$entries = DB::select("SELECT link, reference_id FROM entries WHERE collection_id = {$collection_molecule->collection_id} and molecule_id = {$collection_molecule->molecule_id};");

foreach ($entries as $index => $entry) {

if ($index == 0) {
switch ($collection_molecule->collection_id) {
case 30:
case 42:
case 43:
case 54:
case 58:
$url = $db_links[$collection_molecule->collection_id].$entry->reference_id;
break;
default:
$url = $entry->link;
break;
}
$reference = $entry->reference_id;
} else {
switch ($collection_molecule->collection_id) {
case 30:
case 42:
case 43:
case 54:
case 58:
$url .= '|'.$db_links[$collection_molecule->collection_id].$entry->reference_id;
break;
default:
$url .= '|'.$entry->link;
break;
}
$reference .= '|'.$entry->reference_id;
}
$reference = $entry->reference_id;
} else {
switch ($collection_molecule->collection_id) {
case 30:
case 42:
case 43:
case 54:
case 58:
$url .= '|' . $db_links[$collection_molecule->collection_id] . $entry->reference_id;
break;
default:
$url .= '|' . $entry->link;
break;
}
$reference .= '|' . $entry->reference_id;
}
}

// Prepare data for batch update
array_push($data, [
'collection_id' => $collection_molecule->collection_id,
'molecule_id' => $collection_molecule->molecule_id,
'url' => $url,
'reference' => $reference,
]);
}
// Prepare data for batch update
array_push($data, [
'collection_id' => $collection_molecule->collection_id,
'molecule_id' => $collection_molecule->molecule_id,
'url' => $url,
'reference' => $reference,
]);
}

// Update the database with the calculated scores in batch
if (! empty($data)) {
$this->info('Updating batch '. $batchCount);
$this->updateBatch($data);
$batchCount = $batchCount + 1;
$data = []; // Reset the data array for the next batch
// Update the database with the calculated scores in batch
if (! empty($data)) {
$this->info('Updating batch '.$batchCount);
$this->updateBatch($data);
$data = []; // Reset the data array for the next batch
}
}
$batchCount = $batchCount + 1;
});

// Ensure any remaining data is updated after the last chunk
Expand Down Expand Up @@ -128,12 +129,12 @@ public function handle()
$children_pivot_rows = DB::select("SELECT collection_id, molecule_id, url, reference FROM collection_molecule WHERE collection_id = {$parent_pivot_row->collection_id} and molecule_id in ({$children_ids_string});");

foreach ($children_pivot_rows as $children_pivot_row) {
if (!$parent_pivot_row->url) {
if (! $parent_pivot_row->url) {
$url = $children_pivot_row->url;
$reference = $children_pivot_row->reference;
} else {
$url .= '|' . $children_pivot_row->url;
$reference .= '|' . $children_pivot_row->reference;
$url .= '|'.$children_pivot_row->url;
$reference .= '|'.$children_pivot_row->reference;
}

// push each parent data for update
Expand All @@ -148,13 +149,13 @@ public function handle()

// Update the database with the calculated scores in batch
if (! empty($data)) {
$this->info('Updating parent batch '. $batchCount);
$this->info('Updating parent batch '.$batchCount);
$this->updateBatch($data);
$batchCount = $batchCount + 1;
$data = []; // Reset the data array for the next batch
}
});

$batchCount = $batchCount + 1;
});

// Ensure any remaining data is updated after the last chunk
if (! empty($data)) {
Expand All @@ -164,28 +165,35 @@ public function handle()
$this->info('Updated successfully.');
}


private function updateBatch(array $data)
{
DB::transaction(function () use ($data) {
foreach ($data as $row) {
if($row['molecule_id']!=293023 || $row['molecule_id']!=427142 || $row['molecule_id']!=395531 || $row['molecule_id']!=292092 || $row['molecule_id']!=186065 || $row['molecule_id']!=23452 || $row['molecule_id']!=33176) {
$molecule = Molecule::find($row['molecule_id']);
$currentPivotData = $molecule->collections()->wherePivot('collection_id', $row['collection_id'])->first()->pivot->toArray();
$molecule->collections()->updateExistingPivot($row['collection_id'], ['url' => $row['url'], 'reference' => $row['reference']]);
$molecule->auditEvent = 'moleculeCollectionUpdate';
$molecule->isCustomEvent = true;
$molecule->auditCustomOld = [
'url' => $currentPivotData['url'],
'reference' => $currentPivotData['reference']
];
$molecule->auditCustomNew = [
'url' => $row['url'],
'reference' => $row['reference']
];
Event::dispatch(AuditCustom::class, [$molecule]);
try {
DB::transaction(function () use ($data) {
foreach ($data as $row) {
if ($row['molecule_id'] != 293023 && $row['molecule_id'] != 427142 && $row['molecule_id'] != 395531 && $row['molecule_id'] != 292092 && $row['molecule_id'] != 186065 && $row['molecule_id'] != 23452 && $row['molecule_id'] != 33176) {
$molecule = Molecule::find($row['molecule_id']);
if (! $molecule) {
dd($row);
}
$currentPivotData = $molecule->collections()->wherePivot('collection_id', $row['collection_id'])->first()->pivot->toArray();
$molecule->collections()->updateExistingPivot($row['collection_id'], ['url' => $row['url'], 'reference' => $row['reference']]);
$molecule->auditEvent = 'moleculeCollectionUpdate';
$molecule->isCustomEvent = true;
$molecule->auditCustomOld = [
'url' => $currentPivotData['url'],
'reference' => $currentPivotData['reference'],
];
$molecule->auditCustomNew = [
'url' => $row['url'],
'reference' => $row['reference'],
];
Event::dispatch(AuditCustom::class, [$molecule]);
}
}
}
});
});
} catch (\Exception $e) {
exec('afplay /System/Library/Sounds/Ping.aiff');
dd($e);
}
}
}

0 comments on commit 885c2db

Please sign in to comment.