Limit graphic stats to 6 months by default, some performance query improvements

This commit is contained in:
Deon George
2021-12-03 11:24:23 +11:00
parent bf57f151d5
commit 10afd6f3a4
5 changed files with 29 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use MongoDB\BSON\UTCDateTime;
use App\Traits\ScopeActive;
@@ -14,6 +15,7 @@ class Domain extends Model
{
use HasFactory,ScopeActive;
private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6;
/* SCOPES */
@@ -64,7 +66,10 @@ class Domain extends Model
$key = sprintf('%s_%d','daily_area_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(),
'datetime' => ['$gte',new UTCDateTime(Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())],
];
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
@@ -83,7 +88,10 @@ class Domain extends Model
$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],
'datetime' => ['$gte',new UTCDateTime(Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())],
];
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
@@ -103,6 +111,7 @@ class Domain extends Model
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($o) {
$where = collect(['echoarea_id'=>$this->echoareas->pluck('id')->toArray()]);
$where->put('datetime',['$gte',new UTCDateTime(Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())]);
if ($o)
$where->put('fftn_id',$o->addresses()->pluck('id'));