Cache some calls to Mongo for performance
This commit is contained in:
parent
be886d9e4b
commit
6acc8ee407
@ -5,6 +5,7 @@ namespace App\Models;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
use App\Traits\ScopeActive;
|
use App\Traits\ScopeActive;
|
||||||
@ -12,6 +13,7 @@ use App\Traits\ScopeActive;
|
|||||||
class Domain extends Model
|
class Domain extends Model
|
||||||
{
|
{
|
||||||
use HasFactory,ScopeActive;
|
use HasFactory,ScopeActive;
|
||||||
|
private const CACHE_TIME = 3600;
|
||||||
|
|
||||||
/* SCOPES */
|
/* SCOPES */
|
||||||
|
|
||||||
@ -59,6 +61,9 @@ class Domain extends Model
|
|||||||
if (! $this->echoareas->count())
|
if (! $this->echoareas->count())
|
||||||
return collect();
|
return collect();
|
||||||
|
|
||||||
|
$key = sprintf('%s_%d','daily_echoarea_stats',$this->id);
|
||||||
|
|
||||||
|
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() {
|
||||||
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
|
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
|
||||||
|
|
||||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||||
@ -67,6 +72,7 @@ class Domain extends Model
|
|||||||
->sortBy(function($item) { return $item->id->datetime; })
|
->sortBy(function($item) { return $item->id->datetime; })
|
||||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||||
->values();
|
->values();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function daily_echoarea_stats(Echoarea $o): Collection
|
public function daily_echoarea_stats(Echoarea $o): Collection
|
||||||
@ -74,6 +80,9 @@ class Domain extends Model
|
|||||||
if (! $this->echoareas->count())
|
if (! $this->echoareas->count())
|
||||||
return collect();
|
return collect();
|
||||||
|
|
||||||
|
$key = sprintf('%s_%d-%d','daily_echoarea_stats',$this->id,$o->id);
|
||||||
|
|
||||||
|
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($o) {
|
||||||
$where = ['echoarea_id'=>[$o->id]];
|
$where = ['echoarea_id'=>[$o->id]];
|
||||||
|
|
||||||
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
|
||||||
@ -82,6 +91,7 @@ class Domain extends Model
|
|||||||
->sortBy(function($item) { return $item->id->datetime; })
|
->sortBy(function($item) { return $item->id->datetime; })
|
||||||
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
->map(function($item) { return ['x'=>Carbon::createFromFormat('Y-m-d',$item->id->datetime)->timestamp*1000,'y'=>$item->count]; })
|
||||||
->values();
|
->values();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stats(System $o=NULL): Collection
|
public function stats(System $o=NULL): Collection
|
||||||
|
@ -2,15 +2,18 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
use App\Traits\{ScopeActive,UsePostgres};
|
use App\Traits\{ScopeActive,UsePostgres};
|
||||||
|
|
||||||
class Echoarea extends Model
|
class Echoarea extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes,ScopeActive,UsePostgres;
|
use SoftDeletes,ScopeActive,UsePostgres;
|
||||||
|
|
||||||
|
private const CACHE_TIME = 3600;
|
||||||
|
|
||||||
/* RELATIONS */
|
/* RELATIONS */
|
||||||
|
|
||||||
public function addresses()
|
public function addresses()
|
||||||
@ -28,4 +31,35 @@ class Echoarea extends Model
|
|||||||
return Echomail::select('*')
|
return Echomail::select('*')
|
||||||
->where('echoarea_id',$this->id);
|
->where('echoarea_id',$this->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ATTRIBUTES */
|
||||||
|
|
||||||
|
public function getLastMessageAttribute(): ?Carbon
|
||||||
|
{
|
||||||
|
$key = sprintf('%s_%d','echo_last_message',$this->id);
|
||||||
|
|
||||||
|
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() {
|
||||||
|
return ($x=$this->echomail()->orderBy('datetime','DESC')->first()) ? $x->datetime : NULL;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* METHODS */
|
||||||
|
|
||||||
|
public function messages_count(int $period): int
|
||||||
|
{
|
||||||
|
$key = sprintf('%s_%d_%d','echo_mesages_count',$this->id,$period);
|
||||||
|
|
||||||
|
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($period) {
|
||||||
|
switch ($period) {
|
||||||
|
case 1: // day
|
||||||
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subDay())->count();
|
||||||
|
case 7: // week
|
||||||
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subWeek())->count();
|
||||||
|
case 30: // month
|
||||||
|
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subMonth())->count();
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -50,10 +50,10 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td style="width: 15%;"><a href="{{ url('ftn/echoarea/addedit',[$oo->id]) }}">{{ $oo->name }}</a></td>
|
<td style="width: 15%;"><a href="{{ url('ftn/echoarea/addedit',[$oo->id]) }}">{{ $oo->name }}</a></td>
|
||||||
<td>{{ $oo->description }}</td>
|
<td>{{ $oo->description }}</td>
|
||||||
<td style="width: 15%;">{{ ($x=$oo->echomail()->orderBy('datetime','DESC')->first()) ? $x->datetime->format('Y-m-d H:i') : '-' }}</td>
|
<td style="width: 15%;">{{ $oo->last_message ? $oo->last_message->format('Y-m-d H:i') : '-' }}</td>
|
||||||
<td class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subDay())->count()) }}</td>
|
<td class="text-end">{{ number_format($oo->messages_count(1)) }}</td>
|
||||||
<td class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subWeek())->count()) }}</td>
|
<td class="text-end">{{ number_format($oo->messages_count(7)) }}</td>
|
||||||
<td class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subMonth())->count()) }}</td>
|
<td class="text-end">{{ number_format($oo->messages_count(30)) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -276,7 +276,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
series: [
|
series: [
|
||||||
@foreach($o->echoareas as $oo)
|
@foreach($o->echoareas->sortBy('name') as $oo)
|
||||||
{
|
{
|
||||||
name: '{{ $oo->name }}',
|
name: '{{ $oo->name }}',
|
||||||
data: {!! $o->daily_echoarea_stats($oo) !!},
|
data: {!! $o->daily_echoarea_stats($oo) !!},
|
||||||
|
Loading…
Reference in New Issue
Block a user