Cache some calls to Mongo for performance

This commit is contained in:
Deon George 2021-11-26 16:58:50 +11:00
parent be886d9e4b
commit 6acc8ee407
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
3 changed files with 62 additions and 18 deletions

View File

@ -5,6 +5,7 @@ namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use App\Traits\ScopeActive;
@ -12,6 +13,7 @@ use App\Traits\ScopeActive;
class Domain extends Model
{
use HasFactory,ScopeActive;
private const CACHE_TIME = 3600;
/* SCOPES */
@ -59,14 +61,18 @@ class Domain extends Model
if (! $this->echoareas->count())
return collect();
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
$key = sprintf('%s_%d','daily_echoarea_stats',$this->id);
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() {
$where = ['echoarea_id'=>$this->echoareas->pluck('id')->toArray()];
return $echostats
->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]; })
->values();
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
return $echostats
->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]; })
->values();
});
}
public function daily_echoarea_stats(Echoarea $o): Collection
@ -74,14 +80,18 @@ class Domain extends Model
if (! $this->echoareas->count())
return collect();
$where = ['echoarea_id'=>[$o->id]];
$key = sprintf('%s_%d-%d','daily_echoarea_stats',$this->id,$o->id);
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($o) {
$where = ['echoarea_id'=>[$o->id]];
return $echostats
->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]; })
->values();
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
return $echostats
->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]; })
->values();
});
}
public function stats(System $o=NULL): Collection

View File

@ -2,15 +2,18 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use App\Traits\{ScopeActive,UsePostgres};
class Echoarea extends Model
{
use SoftDeletes,ScopeActive,UsePostgres;
private const CACHE_TIME = 3600;
/* RELATIONS */
public function addresses()
@ -28,4 +31,35 @@ class Echoarea extends Model
return Echomail::select('*')
->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;
}
});
}
}

View File

@ -50,10 +50,10 @@
<tr>
<td style="width: 15%;"><a href="{{ url('ftn/echoarea/addedit',[$oo->id]) }}">{{ $oo->name }}</a></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 class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subDay())->count()) }}</td>
<td class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subWeek())->count()) }}</td>
<td class="text-end">{{ number_format($oo->echomail()->where('datetime','>=',Carbon::now()->subMonth())->count()) }}</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->messages_count(1)) }}</td>
<td class="text-end">{{ number_format($oo->messages_count(7)) }}</td>
<td class="text-end">{{ number_format($oo->messages_count(30)) }}</td>
</tr>
@endforeach
</tbody>
@ -276,7 +276,7 @@
}
},
series: [
@foreach($o->echoareas as $oo)
@foreach($o->echoareas->sortBy('name') as $oo)
{
name: '{{ $oo->name }}',
data: {!! $o->daily_echoarea_stats($oo) !!},