Switchout DB to CockroachDB

This commit is contained in:
Deon George
2022-01-01 16:59:35 +11:00
parent afaa7d8bc7
commit 424d6ef39d
28 changed files with 1342 additions and 865 deletions

View File

@@ -3,14 +3,16 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use App\Traits\{ScopeActive,UsePostgres};
use App\Traits\ScopeActive;
class Echoarea extends Model
{
use SoftDeletes,ScopeActive,UsePostgres;
use SoftDeletes,ScopeActive;
private const CACHE_TIME = 3600;
@@ -28,28 +30,24 @@ class Echoarea extends Model
public function echomail()
{
return Echomail::select('*')
->where('echoarea_id',$this->id);
return $this->hasMany(Echomail::class)
->orderBy('datetime','ASC');
}
/* 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;
});
return $this->echomail?->last()->datetime;
}
/* METHODS */
public function messages_count(int $period): int
{
$key = sprintf('%s_%d_%d','echo_mesages_count',$this->id,$period);
$key = sprintf('%s_%d_%d','echo_messages_count',$this->id,$period);
return Cache::driver('redis')->remember($key,self::CACHE_TIME,function() use ($period) {
return Cache::remember($key,self::CACHE_TIME,function() use ($period) {
switch ($period) {
case 1: // day
return $this->echomail()->where('datetime','>=',Carbon::now()->startOfday()->subDay())->count();
@@ -62,4 +60,20 @@ class Echoarea extends Model
}
});
}
/**
* Number of messages waiting for address
*
* @param Address $ao
* @return Collection
*/
public function waiting(Address $ao): Collection
{
return $this->echomail()
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
->whereNull('sent_at')
->whereNotNull('export_at')
->where('address_id',$ao->id)
->get();
}
}