Skip to content

Commit

Permalink
fix: queries in collection and geolocation stats inline with app cach…
Browse files Browse the repository at this point in the history
…e refresh
  • Loading branch information
sriramkanakam87 committed May 28, 2024
1 parent bfadb89 commit 9506316
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,26 @@ class CollectionStats extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Entries', Cache::rememberForever('stats.collections'.$this->record->id.'entries.count', function () {
return DB::table('entries')->selectRaw('count(*)')->whereRaw('collection_id='.$this->record->id)->get()[0]->count;
}))
// Commented is the model query that we can use in case we decide not to use app level caching as the app scales up.

// Stat::make('Entries', Cache::rememberForever('stats.collections'.$this->record->id.'entries.count', function () {
// return DB::table('entries')->selectRaw('count(*)')->whereRaw('collection_id='.$this->record->id)->get()[0]->count;
// }))
// ->description('Total count')
// ->color('primary'),
Stat::make('Entries', Cache::get('stats.collections'.$this->record->id.'entries.count'))
->description('Total count')
->color('primary'),
Stat::make('Passed Entries', Cache::rememberForever('stats.collections'.$this->record->id.'passed_entries.count', function () {
return DB::table('entries')->selectRaw('count(*)')->whereRaw("status = 'PASSED'")->get()[0]->count;
}))
Stat::make('Passed Entries', Cache::get('stats.collections'.$this->record->id.'passed_entries.count'))
->description('Successful count')
->color('success'),
Stat::make('Entries', Cache::rememberForever('stats.collections'.$this->record->id.'rejected_entries.count', function () {
return DB::table('entries')->selectRaw('count(*)')->whereRaw("status = 'REJECTED'")->get()[0]->count;
}))
Stat::make('Entries', Cache::get('stats.collections'.$this->record->id.'rejected_entries.count'))
->description('Failed entries')
->color('danger'),
Stat::make('Total Molecules', Cache::rememberForever('stats.collections'.$this->record->id.'molecules.count', function () {
return DB::table('collection_molecule')->selectRaw('count(*)')->whereRaw('collection_id ='.$this->record->id)->get()[0]->count;
})),
Stat::make('Total Citations', Cache::rememberForever('stats.collections'.$this->record->id.'citations.count', function () {
return DB::table('citables')->selectRaw('count(*)')->whereRaw("citable_type='App\Models\Collection' and citable_id=".$this->record->id)->get()[0]->count;
})),
Stat::make('Total Organisms', Cache::rememberForever('stats.collections'.$this->record->id.'organisms.count', function () {
return DB::table('collection_molecule')->selectRaw('count(*)')->whereRaw('collection_id='.$this->record->id)->Join('molecule_organism', 'collection_molecule.molecule_id', '=', 'molecule_organism.molecule_id')->get()[0]->count;
})),
Stat::make('Total Geo Locations', Cache::rememberForever('stats.collections'.$this->record->id.'geo_locations.count', function () {
return DB::table('collection_molecule')->selectRaw('count(*)')->whereRaw('collection_id='.$this->record->id)->Join('geo_location_molecule', 'collection_molecule.molecule_id', '=', 'geo_location_molecule.molecule_id')->get()[0]->count;
})),
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')),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class GeoLocationStats extends BaseWidget
{
Expand All @@ -15,12 +14,8 @@ class GeoLocationStats extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Total Molecules', Cache::rememberForever('stats.geo_locations'.$this->record->id.'molecules.count', function () {
return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$this->record->id)->get()[0]->count;
})),
Stat::make('Total Organisms', Cache::rememberForever('stats.geo_locations'.$this->record->id.'organisms.count', function () {
return DB::table('geo_location_molecule')->selectRaw('count(*)')->whereRaw('geo_location_id='.$this->record->id)->Join('molecule_organism', 'geo_location_molecule.molecule_id', '=', 'molecule_organism.molecule_id')->get()[0]->count;
})),
Stat::make('Total Molecules', Cache::get('stats.geo_locations'.$this->record->id.'molecules.count')),
Stat::make('Total Organisms', Cache::get('stats.geo_locations'.$this->record->id.'organisms.count')),
];
}
}
20 changes: 5 additions & 15 deletions app/Filament/Dashboard/Widgets/DashboardStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,12 @@ public function getColumns(): int
protected function getStats(): array
{
return [
Stat::make('Total Collections', Cache::rememberForever('stats.collections', function () {
return DB::table('collections')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Citations', Cache::rememberForever('stats.citations', function () {
return DB::table('citations')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Collections', Cache::get('stats.collections')),
Stat::make('Total Citations', Cache::get('stats.citations')),

Stat::make('Total Organisms', Cache::rememberForever('stats.organisms', function () {
return DB::table('organisms')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Geo Locations', Cache::rememberForever('stats.geo_locations', function () {
return DB::table('geo_locations')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Reports', Cache::rememberForever('stats.reports', function () {
return DB::table('reports')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Organisms', Cache::get('stats.organisms')),
Stat::make('Total Geo Locations', Cache::get('stats.geo_locations')),
Stat::make('Total Reports', Cache::get('stats.reports')),
];
}
}
16 changes: 4 additions & 12 deletions app/Filament/Dashboard/Widgets/DashboardStatsMid.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@ class DashboardStatsMid extends BaseWidget
protected function getStats(): array
{
return [
Stat::make('Total Molecules', Cache::rememberForever('stats.molecules', function () {
return DB::table('molecules')->selectRaw('count(*)')->get()[0]->count;
})),
Stat::make('Total Non-Stereo Molecules', Cache::rememberForever('stats.molecules.non_stereo', function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=false and is_parent=false')->get()[0]->count;
})),
Stat::make('Total Stereo Molecules', Cache::rememberForever('stats.molecules.stereo', function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=true')->get()[0]->count;
}))
Stat::make('Total Molecules', Cache::get('stats.molecules')),
Stat::make('Total Non-Stereo Molecules', Cache::get('stats.molecules.non_stereo')),
Stat::make('Total Stereo Molecules', Cache::get('stats.molecules.stereo'))
->description(
'Total parent molecules: '.Cache::rememberForever('stats.molecules.parent', function () {
return DB::table('molecules')->selectRaw('count(*)')->whereRaw('has_stereo=false and is_parent=true')->get()[0]->count;
})
'Total parent molecules: '.Cache::get('stats.molecules.parent')
)
->color('primary'),
];
Expand Down

0 comments on commit 9506316

Please sign in to comment.