diff --git a/app/Http/Controllers/DomainController.php b/app/Http/Controllers/DomainController.php index c0ea05e..d147516 100644 --- a/app/Http/Controllers/DomainController.php +++ b/app/Http/Controllers/DomainController.php @@ -19,8 +19,15 @@ class DomainController extends Controller */ public function api_daily_stats(Request $request): Collection { - $do = Domain::where('name',$request->name)->firstOrFail(); - return $do->daily_area_stats(); + $o = Domain::where('name',$request->name)->firstOrFail(); + + return $o->echoarea_total_daily() + ->sortBy('date') + ->groupBy('date') + ->transform(function($item,$key) { return [ + 'x'=>\Carbon\Carbon::createFromFormat('Y-m-d',$key)->timestamp, + 'y'=>$item->sum('count')]; } ) + ->values(); } /** diff --git a/app/Models/Domain.php b/app/Models/Domain.php index e648d6d..fa33b90 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; @@ -69,55 +70,6 @@ class Domain extends Model /* METHODS */ - /** - * Get some message area stats for this domain - * - * @param bool $byarea - * @param Collection|NULL $systems - * @return Collection - */ - public function daily_area_stats(bool $byarea=FALSE,Collection $systems=NULL): Collection - { - if (! $this->echoareas->count()) - return collect(); - - $echostats = Echomail::cacheFor(self::CACHE_TIME)->select([DB::raw('datetime::date as date'),'echoarea_id','echoareas.name',DB::raw('COUNT(*)')]) - ->join('echoareas',['echoareas.id'=>'echomails.echoarea_id']) - ->join('domains',['domains.id'=>'echoareas.domain_id']) - ->where('domain_id',$this->id) - ->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')->toArray()); }) - ->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth()) - ->groupBy(['echoarea_id','echoareas.name','date']) - ->orderBy('date') - ->orderBy('echoareas.name') - ->with(['echoarea']) - ->get(); - - if ($byarea) - return $echostats - ->sortBy('name') - ->groupBy(['echoarea_id']) - ->map(function($item,$key) { - return [ - 'name' => $item->first()->echoarea->name, - 'data' => $item->groupby('date')->map(function($item) { - return [ - 'x' => Carbon::create($item->first()->date)->timestamp*1000, - 'y' => $item->sum('count') - ]; - })->values(), - 'dashStyle' => 'ShortDot', - 'visible'=>(bool)$item->first()->echoarea->show, - ]; - })->values(); - - else - return $echostats - ->groupBy('date') - ->map(function($item) { return ['x'=>Carbon::create($item->first()->date)->timestamp*1000,'y'=>$item->sum('count')]; }) - ->values(); - } - public function echoarea_stats(): Collection { $dt = Carbon::now()->startOfday(); @@ -139,6 +91,24 @@ class Domain extends Model ->get(); } + public function echoarea_total_daily(Collection $systems=NULL): Collection + { + return Cache::remember(md5(sprintf('%d-%s',$this->id,$systems?->pluck('id')->join(','))),self::CACHE_TIME,function() use ($systems) { + return DB::query() + ->select(['echoareas.name','echoareas.show',DB::raw('COUNT(*) AS count'),DB::raw('datetime::date AS date')]) + ->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.name','echoareas.show','date']) + ->orderBy('echoareas.name') + ->orderBy('date') + ->get(); + }); + } + public function isManaged(): bool { return our_address()->pluck('zone.domain')->pluck('id')->contains($this->id); diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 246412a..4e0116b 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->echoarea_total_daily()->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->echoarea_total_daily($user->systems)->sum('count') }}, drilldown: 'ny-{{ $do->name }}', color: Highcharts.color(Highcharts.getOptions().colors[{{$loop->index}}]).brighten(-0.2).get() }, @@ -220,7 +220,10 @@ { 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->echoarea_total_daily() + ->groupBy('name') + ->map(function($item,$key) { return ['name'=>$key,'y'=>$item->sum('count'),'drilldown'=>'e-'.$key]; }) + ->values() !!} }, @endforeach @@ -228,7 +231,10 @@ { 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->echoarea_total_daily($user->systems) + ->groupBy('name') + ->map(function($item,$key) { return ['name'=>$key,'y'=>$item->sum('count'),'drilldown'=>'e-'.$key]; }) + ->values() !!} }, @endforeach ] diff --git a/resources/views/domain/view.blade.php b/resources/views/domain/view.blade.php index 1570ba5..9867179 100644 --- a/resources/views/domain/view.blade.php +++ b/resources/views/domain/view.blade.php @@ -305,7 +305,15 @@ cursor: 'pointer' } }, - series: {!! $o->daily_area_stats('true') !!} + series: {!! $o->echoarea_total_daily() + ->groupBy('name') + ->transform(function($item,$key) { return [ + 'name'=>$key, + 'dashStyle'=>'ShortDot', + 'visible'=>$item->first()->show, + 'data'=>$item->map(function($item) { return ['x'=>\Carbon\Carbon::createFromFormat('Y-m-d',$item->date)->timestamp,'y'=>$item->count]; }) + ]; }) + ->values() !!} }); @append \ No newline at end of file