From 4e43736dcc65bd536cace3a808f666167fe60436 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 20 Apr 2024 22:03:47 +1000 Subject: [PATCH] More optimisations for users dashboard --- app/Models/Domain.php | 18 ++++++++++++++++++ resources/views/dashboard.blade.php | 8 ++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Models/Domain.php b/app/Models/Domain.php index e648d6d..bab8b41 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use App\Casts\CompressedString; @@ -154,4 +155,21 @@ class Domain extends Model { return our_address($this)->count() > 0; } + + public function total_echomail(Collection $systems=NULL): Collection + { + return Cache::remember(sprintf('%d-%s',$this->id,$systems?->pluck('id')->join(',')),self::CACHE_TIME,function() use ($systems) { + return DB::query() + ->select(['echoareas.name',DB::raw('count(*) as count')]) + ->from($this->getTable()) + ->join('echoareas',['echoareas.domain_id'=>'domains.id']) + ->join('echomails',['echomails.echoarea_id'=>'echoareas.id']) + ->where('domains.id',$this->id) + ->where('echomails.datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth()) + ->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('echomails.fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')); }) + ->groupBy(['echoareas.id']) + ->orderBy('echoareas.name') + ->get(); + }); + } } \ No newline at end of file diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 246412a..0241815 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -186,7 +186,7 @@ @foreach($dl as $do) { name: '{{ $do->name }}', - y: {{ $do->daily_area_stats()->sum('y') }}, + y: {{ $do->total_echomail()->sum('count') }}, drilldown: 'n-{{ $do->name }}', }, @endforeach @@ -200,7 +200,7 @@ @foreach($dl as $do) { name: '{{ $do->name }}', - y: {{ $do->daily_area_stats(FALSE,$user->systems)->sum('y') }}, + y: {{ $do->total_echomail($user->systems)->sum('count') }}, drilldown: 'ny-{{ $do->name }}', color: Highcharts.color(Highcharts.getOptions().colors[{{$loop->index}}]).brighten(-0.2).get() }, @@ -220,7 +220,7 @@ { name: '{{ $do->name }}', id: 'n-{{ $do->name }}', - data: {!! $do->daily_area_stats(TRUE)->sortBy('name')->map(function($item) { return ['name'=>$item['name'],'y'=>$item['data']->sum('y'),'drilldown'=>'e-'.$item['name']]; })->values() !!} + data: {!! $do->total_echomail()->map(function($item) { return ['name'=>$item->name,'y'=>$item->count,'drilldown'=>'e-'.$item->name]; })->values() !!} }, @endforeach @@ -228,7 +228,7 @@ { name: '{{ $do->name }}', id: 'ny-{{ $do->name }}', - data: {!! $do->daily_area_stats(TRUE,$user->systems)->sortBy('name')->map(function($item) { return ['name'=>$item['name'],'y'=>$item['data']->sum('y'),'drilldown'=>'ey-'.$item['name']]; })->values() !!} + data: {!! $do->total_echomail($user->systems)->map(function($item) { return ['name'=>$item->name,'y'=>$item->count,'drilldown'=>'ey-'.$item->name]; })->values() !!} }, @endforeach ]