Skip to content

Commit

Permalink
Merge pull request #217 from Steinbeck-Lab/fix-command-collection-counts
Browse files Browse the repository at this point in the history
Fix command collection counts
  • Loading branch information
CS76 authored Aug 29, 2024
2 parents 7fabdd4 + c71ccc8 commit 0998a4d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 152 deletions.
170 changes: 45 additions & 125 deletions app/Console/Commands/DashWidgetsRefresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace App\Console\Commands;

use App\Models\Collection;
use App\Models\GeoLocation;
use App\Models\Molecule;
use App\Models\Organism;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
Expand All @@ -32,7 +29,7 @@ class DashWidgetsRefresh extends Command
public function handle()
{
// Clear the cache for all widgets
// Cache::flush();
Cache::flush();

// Create the cache for all DashboardStats widgets
Cache::rememberForever('stats.collections', function () {
Expand Down Expand Up @@ -84,128 +81,51 @@ public function handle()

// Create the cache for all Collection widgets

$this->info('Cache for each collection is being refreshed.');

$collection_ids = Collection::pluck('id')->toArray();

foreach ($collection_ids as $collection_id) {
$this->info('Refreshing Cache: CollectionID - '.$collection_id);
Cache::rememberForever('stats.collections'.$collection_id.'entries.count', function () use ($collection_id) {
return DB::table('entries')->selectRaw('count(*)')->whereRaw('collection_id='.$collection_id)->get()[0]->count;
});

Cache::rememberForever('stats.collections'.$collection_id.'passed_entries.count', function () use ($collection_id) {
return DB::table('entries')->selectRaw('count(*)')->whereRaw('collection_id='.$collection_id)->whereRaw("status = 'PASSED'")->get()[0]->count;
});

Cache::rememberForever('stats.collections'.$collection_id.'rejected_entries.count', function () use ($collection_id) {
return DB::table('entries')->selectRaw('count(*)')->whereRaw('collection_id='.$collection_id)->whereRaw("status = 'REJECTED'")->get()[0]->count;
});

// Cache::rememberForever('stats.collections'.$collection_id.'citations.count', function () use ($collection_id) {
// $entries = DB::table('entries')
// ->select('entries.doi')
// ->join('collections', 'collections.id', '=', 'entries.collection_id')
// ->where('collections.id', $collection_id)
// ->pluck('doi');

// $dois = collect();

// foreach ($entries as $entry) {
// $dois = $dois->merge(explode('|', $entry));
// }

// $uniqueDois = $dois->filter(function ($doi) {
// return ! empty($doi);
// })->unique();

// return $uniqueDois->count();
// });

Cache::rememberForever('stats.collections'.$collection_id.'molecules.count', function () use ($collection_id) {
return DB::table('collection_molecule')->selectRaw('count(*)')->whereRaw('collection_id ='.$collection_id)->get()[0]->count;
});

// Cache::rememberForever('stats.collections'.$collection_id.'organisms.count', function () use ($collection_id) {
// $entries = DB::table('entries')
// ->select('entries.organism')
// ->join('collections', 'collections.id', '=', 'entries.collection_id')
// ->where('collections.id', $collection_id)
// ->pluck('organism');

// $organisms = collect();

// foreach ($entries as $entry) {
// $organisms = $organisms->merge(explode('|', $entry));
// }

// $uniqueOrgs = $organisms->filter(function ($organism) {
// return ! empty($organism);
// })->unique();

// return $uniqueOrgs->count();
// });

// Cache::rememberForever('stats.collections'.$collection_id.'geo_locations.count', function () use ($collection_id) {
// $entries = DB::table('entries')
// ->select('entries.geo_location')
// ->join('collections', 'collections.id', '=', 'entries.collection_id')
// ->where('collections.id', $collection_id)
// ->pluck('geo_location');

// $geo_locations = collect();

// foreach ($entries as $entry) {
// $geo_locations = $geo_locations->merge(explode('|', $entry));
// }

// $uniqueGeos = $geo_locations->filter(function ($geo_location) {
// return ! empty($geo_location);
// })->unique();

// return $uniqueGeos->count();
// });

$this->info('Processing collection wiget counts');
$collection_ids = DB::select('SELECT id FROM collections order by id;');

foreach ($collection_ids as $collection) {
$this->info('Processing collection '.$collection->id);

$entries = DB::select("SELECT doi, organism, geo_location FROM entries WHERE collection_id = {$collection->id};");

$dois = [];
$organisms = [];
$geo_locations = [];

foreach ($entries as $entry) {
foreach (explode('|', $entry->doi) as $doi) {
if ($doi !== '') {
array_push($dois, $doi);
}
}
foreach (explode('|', $entry->organism) as $organism) {
if ($organism !== '') {
array_push($organisms, $organism);
}
}
foreach (explode('|', $entry->geo_location) as $geo_location) {
if ($geo_location !== '') {
array_push($geo_locations, $geo_location);
}
}
}
$unique_dois_count = count(array_unique($dois));
$unique_organisms_count = count(array_unique($organisms));
$unique_geo_locations_count = count(array_unique($geo_locations));

$total_entries = DB::select("SELECT count(*) FROM entries WHERE collection_id = {$collection->id};");
$successful_entries = DB::select("SELECT count(*) FROM entries WHERE collection_id = {$collection->id} AND status = 'PASSED';");
$failed_entries = DB::select("SELECT count(*) FROM entries WHERE collection_id = {$collection->id} AND status = 'REJECTED';");
$molecules_count = DB::select("SELECT count(*) FROM collection_molecule WHERE collection_id = {$collection->id};");

DB::statement(
"UPDATE collections
SET (total_entries, successful_entries, failed_entries, molecules_count, citations_count, organisms_count, geo_count) = ({$total_entries[0]->count}, {$successful_entries[0]->count}, {$failed_entries[0]->count}, {$molecules_count[0]->count}, {$unique_dois_count}, {$unique_organisms_count}, {$unique_geo_locations_count})
WHERE id = {$collection->id};"
);
}

$this->info('Cache for each collection refreshed.');

// Create the cache for all Molecule widgets
// $molecule_ids = Molecule::pluck('id')->toArray();
// foreach ($molecule_ids as $molecule_id) {
// Cache::rememberForever('stats.molecules'.$molecule_id.'organisms.count', function () use ($molecule_id){
// return DB::table('molecule_organism')->selectRaw('count(*)')->whereRaw('molecule_id='.$molecule_id)->get()[0]->count;
// });
// Cache::rememberForever('stats.molecules'.$molecule_id.'geo_locations.count', function () use ($molecule_id){
// return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('molecule_id='.$molecule_id)->get()[0]->count;
// });
// }

// // Create the cache for all Organism widgets

// $organism_ids = Organism::pluck('id')->toArray();

// foreach ($organism_ids as $organism_id) {
// Cache::rememberForever('stats.organisms'.$organism_id.'molecules.count', function () use ($organism_id){
// return DB::table('molecule_organism')->selectRaw('count(*)')->whereRaw('organism_id='.$organism_id)->get()[0]->count;
// });
// Cache::rememberForever('stats.organisms'.$organism_id.'geo_locations.count', function () use ($organism_id){
// return DB::table('molecule_organism')->selectRaw('count(*)')->whereRaw('organism_id='.$organism_id)->Join('geo_location_molecule', 'molecule_organism.molecule_id', '=', 'geo_location_molecule.molecule_id')->get()[0]->count;
// });
// }

// Create the cache for all Geo Location widgets

// $geo_location_ids = GeoLocation::pluck('id')->toArray();

// foreach ($geo_location_ids as $geo_location_id) {
// Cache::rememberForever('stats.geo_locations'.$geo_location_id.'molecules.count', function () use ($geo_location_id) {
// return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$geo_location_id)->get()[0]->count;
// });
// Cache::rememberForever('stats.geo_locations'.$geo_location_id.'organisms.count', function () use ($geo_location_id) {
// return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$geo_location_id)->Join('molecule_organism', 'geo_location_molecule.molecule_id', '=', 'molecule_organism.molecule_id')->get()[0]->count;
// });
// }

$this->info('Processing collection wiget counts complete');
}
}
22 changes: 5 additions & 17 deletions app/Filament/Dashboard/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,12 @@ public static function table(Table $table): Table
->wrap(),
Tables\Columns\TextColumn::make('entries')
->state(function (Model $record) {
return Cache::get('stats.collections'.$record->id.'entries.count').'/'.Cache::get('stats.collections'.$record->id.'rejected_entries.count');
}),
Tables\Columns\TextColumn::make('molecules')
->state(function (Model $record) {
return Cache::get('stats.collections'.$record->id.'molecules.count');
}),
Tables\Columns\TextColumn::make('citations')
->state(function (Model $record) {
return Cache::get('stats.collections'.$record->id.'citations.count');
}),
Tables\Columns\TextColumn::make('organisms')
->state(function (Model $record) {
return Cache::get('stats.collections'.$record->id.'organisms.count');
}),
Tables\Columns\TextColumn::make('geo_locations')
->state(function (Model $record) {
return Cache::get('stats.collections'.$record->id.'geo_locations.count');
return $record->total_entries.'/'.$record->failed_entries;
}),
Tables\Columns\TextColumn::make('molecules_count')->label('Molecules'),
Tables\Columns\TextColumn::make('citations_count')->label('Citations'),
Tables\Columns\TextColumn::make('organisms_count')->label('Organisms'),
Tables\Columns\TextColumn::make('geo_count')->label('Geo Locations'),
Tables\Columns\TextColumn::make('status')
->badge()
->color(fn (string $state): string => match ($state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ protected function getStats(): array
// }))
// ->description('Total count')
// ->color('primary'),
Stat::make('Entries', Cache::get('stats.collections'.$this->record->id.'entries.count'))
Stat::make('Entries', $this->record->total_entries)
->description('Total count')
->color('primary'),
Stat::make('Passed Entries', Cache::get('stats.collections'.$this->record->id.'passed_entries.count'))
Stat::make('Passed Entries', $this->record->successful_entries)
->description('Successful count')
->color('success'),
Stat::make('Entries', Cache::get('stats.collections'.$this->record->id.'rejected_entries.count'))
Stat::make('Entries', $this->record->failed_entries)
->description('Failed entries')
->color('danger'),
Stat::make('Total Molecules', Cache::get('stats.collections'.$this->record->id.'molecules.count')),
Stat::make('Total Citations', Cache::get('stats.collections'.$this->record->id.'citations.count')),
Stat::make('Total Organisms', Cache::get('stats.collections'.$this->record->id.'organisms.count')),
Stat::make('Total Geo Locations', Cache::get('stats.collections'.$this->record->id.'geo_locations.count')),
Stat::make('Total Molecules', $this->record->molecules_count),
Stat::make('Total Citations', $this->record->citations_count),
Stat::make('Total Organisms', $this->record->organisms_count),
Stat::make('Total Geo Locations', $this->record->geo_count),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class EntriesOverview extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Entries', $this->record->entries->count())
Stat::make('Entries', $this->record->total_entries)
->description('Total count')
->color('primary'),
Stat::make('Passed Entries', $this->record->entries->where('status', 'PASSED')->count())
Stat::make('Passed Entries', $this->record->successful_entries)
->description('Successful count')
->color('success'),
Stat::make('Entries', $this->record->entries->where('status', 'REJECTED')->count())
Stat::make('Entries', $this->record->failed_entries)
->description('Failed entries')
->color('danger'),
];
Expand Down

0 comments on commit 0998a4d

Please sign in to comment.