Skip to content

Commit

Permalink
fix: delete organisms with no molecules and enables audit trail
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Aug 26, 2024
1 parent d405d60 commit 7ca9c65
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
17 changes: 12 additions & 5 deletions app/Console/Commands/OrganismDedupeOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Console\Commands;

use App\Models\Organism;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\Organism;

use function Laravel\Prompts\select;

class OrganismDedupeOptions extends Command
Expand Down Expand Up @@ -37,7 +38,6 @@ public function handle()
WHERE slug = '' or slug is null;"
);


$this->info('Finding duplicate records...');

// Query to find duplicates case-insensitively
Expand All @@ -50,6 +50,7 @@ public function handle()

if (count($duplicates) === 0) {
$this->info('No duplicates found.');

return 0;
}

Expand All @@ -59,6 +60,8 @@ public function handle()
// Group records by the lowercase version of the duplicate column for easier processing
$groupedRecords = $records->groupBy('slug');

$this->info('Found '.count($groupedRecords).' duplicate records.');

foreach ($groupedRecords as $columnValue => $group) {
$this->info("Duplicate records found for: {$columnValue}");

Expand All @@ -82,11 +85,14 @@ public function handle()
try {
$moleculeIds = $removableOrganism->molecules->pluck('id')->toArray();

$removableOrganism->molecules()->detach($moleculeIds);
$selectedOrganism->molecules()->syncWithoutDetaching($moleculeIds);
// $removableOrganism->molecules()->detach($moleculeIds);
// $selectedOrganism->molecules()->syncWithoutDetaching($moleculeIds);

$removableOrganism->auditDetach('molecules', $moleculeIds);
$selectedOrganism->auditSyncWithoutDetaching('molecules', $moleculeIds);

$removableOrganism->molecule_count = $removableOrganism->molecules()->count();
$removableOrganism->save();
$removableOrganism->delete();
$selectedOrganism->molecule_count = $selectedOrganism->molecules()->count();
$selectedOrganism->save();

Expand All @@ -105,6 +111,7 @@ public function handle()
}

$this->info('Duplicate handling complete.');

return 0;
}
}
3 changes: 1 addition & 2 deletions app/Console/Commands/OrganismMoleculeCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public function handle()
}

$this->info('Updatin the residual organisms.');

DB::table('organisms')
->whereNotIn('id', $moleculeCounts->pluck('id'))
->update(['molecule_count' => 0]);


$this->info('Update process completed.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function table(Table $table): Table
ImageColumn::make('structure')->square()
->label('Structure')
->state(function ($record) {
return env('CM_API', 'https://api.cheminf.studio/latest/') . 'depict/2D?smiles=' . urlencode($record->canonical_smiles) . '&height=300&width=300&CIP=true&toolkit=cdk';
return env('CM_API', 'https://api.cheminf.studio/latest/').'depict/2D?smiles='.urlencode($record->canonical_smiles).'&height=300&width=300&CIP=true&toolkit=cdk';
})
->width(200)
->height(200)
Expand All @@ -50,9 +50,9 @@ public function table(Table $table): Table
Tables\Columns\TextColumn::make('identifier')->searchable()->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('name')->searchable()
->formatStateUsing(
fn(Molecule $molecule): HtmlString => new HtmlString("<strong>ID:</strong> {$molecule->id}<br><strong>Identifier:</strong> {$molecule->identifier}<br><strong>Name:</strong> {$molecule->name}")
fn (Molecule $molecule): HtmlString => new HtmlString("<strong>ID:</strong> {$molecule->id}<br><strong>Identifier:</strong> {$molecule->identifier}<br><strong>Name:</strong> {$molecule->name}")
)
->description(fn(Molecule $molecule): string => $molecule->standard_inchi)
->description(fn (Molecule $molecule): string => $molecule->standard_inchi)
->wrap(),
Tables\Columns\TextColumn::make('synonyms')
->searchable()
Expand Down
2 changes: 1 addition & 1 deletion config/audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,5 @@
|
*/

'console' => false,
'console' => true,
];

0 comments on commit 7ca9c65

Please sign in to comment.