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
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
5 changed files with 29 additions and 6 deletions

View File

@ -52,7 +52,7 @@ class UserController extends Controller
public function dashboard()
{
$user = Auth::user();
$user->load('systems.addresses.zone.domain');
$user->load('systems.addresses.zone.domain.echoareas');
return view('dashboard')
->with('user',$user);

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'));

View File

@ -6,6 +6,7 @@
namespace App\Traits;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
trait UseMongo
{
@ -56,11 +57,21 @@ trait UseMongo
if (! is_array($values))
throw new \Exception('Where values must be an array.');
switch ($x=Arr::get($values,0)) {
case '$gt':
case '$gte':
$where_condition[$key] = [ $x => Arr::get($values,1)];
break;
case '$in':
default:
$where_condition[$key] = ['$in' => $values];
}
}
$query->push([
'$match' => $where_condition
'$match' => [ '$and'=> [$where_condition]]
]);
}

View File

@ -117,7 +117,7 @@ If you have more than 1 BBS, then the Clearing House can receive all your mail f
}
},
series: [
@foreach (\App\Models\Domain::active()->public()->get() as $o)
@foreach (\App\Models\Domain::active()->public()->with(['echoareas'])->get() as $o)
{
name: '{{ $o->name }}',
data: {!! $o->daily_area_stats() !!},

View File

@ -1,4 +1,7 @@
@extends('layouts.app')
@section('htmlheader_title')
Message View
@endsection
@section('content')
<h3>Echomail</h3>