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

@@ -93,7 +93,7 @@ class Message extends FTNBase
private string $subject; // Message subject
private string $msgid; // MSG ID
private string $reply; // Reply ID
private string $replyid; // Reply ID
private string $echoarea; // FTS-0004.001
private string $intl; // Netmail details
@@ -182,7 +182,7 @@ class Message extends FTNBase
$this->message = '';
$this->msgid = '';
$this->reply = '';
$this->replyid = '';
$this->echoarea = '';
$this->intl = '';
@@ -372,7 +372,7 @@ class Message extends FTNBase
case 'echoarea':
case 'msgid':
case 'reply':
case 'replyid':
case 'message':
case 'message_src':
@@ -412,7 +412,7 @@ class Message extends FTNBase
case 'subject':
case 'msgid':
case 'reply':
case 'replyid':
case 'echoarea':
case 'intl':
@@ -486,8 +486,8 @@ class Message extends FTNBase
// Add some kludges
$return .= sprintf("\01MSGID: %s\r",$this->msgid);
if ($this->reply)
$return .= sprintf("\01REPLY: %s\r",$this->reply);
if ($this->replyid)
$return .= sprintf("\01REPLY: %s\r",$this->replyid);
foreach ($this->_kludge as $k=>$v) {
if ($x=$this->kludge->get($k))

View File

@@ -26,6 +26,7 @@ final class Test extends Process
private const testing = ['test','testing'];
// @todo add path and other kludges
public static function handle(Message $msg): bool
{
if ((strtolower($msg->user_to) !== 'all') || ! in_array(strtolower($msg->subject),self::testing))
@@ -66,7 +67,7 @@ final class Test extends Process
$o->datetime = Carbon::now();
$o->tzoffset = $o->datetime->utcOffset();
$o->echoarea_id = $eo?->id;
$o->reply = $msg->msgid;
$o->replyid = $msg->msgid;
$o->fftn_id = $ftns->id;
$o->flags = Message::FLAG_LOCAL;

View File

@@ -50,7 +50,7 @@ final class Ping extends Process
$o->datetime = Carbon::now();
$o->tzoffset = $o->datetime->utcOffset();
$o->reply = $msg->msgid;
$o->replyid = $msg->msgid;
$o->fftn_id = $ftns->id;
$o->tftn_id = ($x=$msg->fftn_o) ? $x->id : NULL;
$o->flags = Message::FLAG_LOCAL;

View File

@@ -0,0 +1,160 @@
<?php
namespace App\Console\Commands;
use App\Models\Echoarea;
use App\Models\Echomail;
use App\Models\Netmail;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
class ConvertMongo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'convert:mongo {start=0}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Convert Mongo to Cockroach';
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
/*
// Netmails
foreach (DB::connection('mongodb')->collection('netmails')->cursor() as $o) {
$o['mid'] = (string)$o['_id'];
foreach([
'reply'=>'replyid',
'packet'=>'sent_pkt',
] as $key => $newkey) {
if (array_key_exists($key,$o)) {
$o[$newkey] = $o[$key];
unset($o[$key]);
}
}
unset($o['_id'],$o['sent'],$o['reply']);
foreach (['created_at','updated_at','sent_at','datetime','deleted_at'] as $key) {
if (Arr::get($o,$key))
$o[$key] = Carbon::create($o[$key]->toDatetime());
}
if (! Arr::get($o,'datetime'))
$o['datetime'] = $o['created_at'];
$oo = Netmail::withTrashed()->where('mid',$o['mid'])->firstOrNew();
$oo->forceFill($o);
try {
$oo->save(['timestamps' => false]);
} catch (\Exception $e) {
$this->warn(sprintf('Netmail didnt move over: %s (%s)',$o['mid'],$e->getMessage()));
}
}
*/
/**/
// Echomail
$c = 0;
foreach (DB::connection('mongodb')->collection('echomails')->cursor() as $o) {
if (++$c < $this->argument('start'))
continue;
if (! ($c%100))
Log::debug(sprintf('Processed : %d Echomails',$c));
$o['mid'] = (string)$o['_id'];
foreach([
'reply'=>'replyid',
'rogue_seen'=>'rogue_seenby',
] as $key => $newkey) {
if (array_key_exists($key,$o)) {
$o[$newkey] = $o[$key];
unset($o[$key]);
}
}
$path = (array_key_exists('path',$o) && $o['path']) ? $o['path'] : NULL;
$seenby = (array_key_exists('seenby',$o) && $o['seenby']) ? $o['seenby'] : NULL;
$packet = (array_key_exists('packet',$o) && $o['packet']) ? $o['packet'] : NULL;
unset($o['_id'],$o['reply'],$o['path'],$o['seenby'],$o['toexport'],$o['sent_at'],$o['packet'],$o['sent']);
foreach (['created_at','updated_at','datetime','deleted_at'] as $key) {
if (Arr::get($o,$key))
$o[$key] = Carbon::create($o[$key]->toDatetime());
}
if (! Arr::get($o,'datetime'))
$o['datetime'] = $o['created_at'];
if (Arr::get($o,'echoarea') && ! Arr::get($o,'echoarea_id')) {
$ea = Echoarea::where('name',$o['echoarea'])->single();
$o['echoarea_id'] = $ea->id;
unset($o['echoarea']);
}
if (! $o['echoarea_id']) {
Log::error(sprintf('Echomail didnt move over: %s [%d] - has no echoarea_id',$o['mid'],$c));
continue;
}
if (Arr::get($o,'msg_src') && ! Arr::get($o,'msg_crc')) {
$o['msg_crc'] = 'x'.md5($o['msg_src']);
}
$oo = Echomail::withTrashed()->where('mid',$o['mid'])->firstOrNew();
$oo->forceFill($o);
$oo->set_path = $path ? array_filter($path) : [];
$oo->set_seenby = $seenby ? array_filter($seenby): [];
$oo->set_packet = $packet;
try {
$oo->save(['timestamps'=>FALSE]);
} catch (\Exception $e) {
Log::error(sprintf('Echomail didnt move over: %s [%d] (%s@%d|%s)',$o['mid'],$c,$e->getFile(),$e->getLine(),$e->getMessage()));
dd(['e'=>$e,'o'=>$o,'oo'=>$oo]);
}
DB::connection('mongodb')->collection('echomails')->delete($o['mid']);
}
/**/
// Update old MID seenby with proper ID
foreach (DB::connection('cockroach')->table('echomail_seenby')->whereNotNull('mid')->whereNull('echomail_id')->cursor() as $o)
{
$eo = Echomail::where('mid',$o->mid)->get();
if ($eo->count() && $eo->count() == 1) {
DB::update('UPDATE echomail_seenby set echomail_id = ?, mid=NULL where echomail_id IS NULL AND mid = ? ',[
$eo->first()->id,
$o->mid,
]);
} elseif ($eo->count() > 1) {
Log::error(sprintf('Echomail [%s] has more than 1 record [%d] - skipped',$o->mid,$eo->count()));
}
}
}
}

View File

@@ -121,6 +121,7 @@ class HomeController extends Controller
# Look for Messages
foreach (Echomail::select(['id','fftn_id','from'])
->where('msgid','like','%'.$request->query('term').'%')
->orWhere('replyid','like','%'.$request->query('term').'%')
->get() as $o)
{
$result->push(['id'=>$o->id,'name'=>sprintf('%s (%s)',$o->from,$o->fftn->ftn3d),'value'=>url('echomail/view',[$o->id]),'category'=>'Echomail']);

View File

@@ -112,7 +112,7 @@ class MessageProcess implements ShouldQueue
$o->fftn_id = ($x=$this->msg->tftn_o) ? $x->id : NULL;
$o->tftn_id = ($x=$this->msg->fftn_o) ? $x->id : NULL;
$o->reply = $this->msg->msgid;
$o->replyid = $this->msg->msgid;
$o->msg = Process::format_msg($reply,$reject);
$o->tagline = 'Do you think it was fate which brought us together? Nah, bad luck :(';

View File

@@ -11,7 +11,7 @@ use Illuminate\Support\Facades\DB;
use App\Classes\FTN\Packet;
use App\Http\Controllers\DomainController;
use App\Traits\{ScopeActive,UsePostgres};
use App\Traits\ScopeActive;
/**
* @todo Need to stop this from happening:
@@ -24,7 +24,9 @@ use App\Traits\{ScopeActive,UsePostgres};
*/
class Address extends Model
{
use ScopeActive,SoftDeletes,UsePostgres;
use ScopeActive,SoftDeletes;
protected $with = ['zone'];
/* SCOPES */
@@ -337,6 +339,21 @@ class Address extends Model
return ($o && $o->system->active) ? $o : NULL;
}
/**
* Netmail waiting to be sent to this system
*
* @return Collection
*/
public function echomailWaiting(): Collection
{
return Echomail::select(['echomails.*'])
->join('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
->where('echomail_seenby.address_id',$this->id)
->whereNull('echomail_seenby.sent_at')
->whereNotNull('echomail_seenby.export_at')
->get();
}
/**
* Get echomail for this node
*
@@ -346,23 +363,17 @@ class Address extends Model
{
$pkt = NULL;
$echomail = DB::table('address_echomail')
->select('echomail_id')
->where('address_id',$this->id)
->whereNull('sent_date')
->get();
if (($x=Echomail::select('*')
->whereIn('_id',$echomail->pluck('echomail_id')))
if (($x=$this->echomailWaiting())
->count())
{
$pkt = $this->getPacket($x->get());
$pkt = $this->getPacket($x);
DB::table('address_echomail')
->whereIn('echomail_id',$echomail->pluck('echomail_id'))
DB::table('echomail_seenby')
->whereIn('echomail_id',$x->pluck('id'))
->where('address_id',$this->id)
->whereNull('sent_date')
->update(['sent_date'=>Carbon::now()]);
->whereNull('sent_at')
->whereNotNull('echomail_seenby.export_at')
->update(['sent_at'=>Carbon::now(),'packet'=>$pkt->name]);
}
return $pkt;
@@ -375,18 +386,19 @@ class Address extends Model
*/
public function getNetmail(): ?Packet
{
if (($x=Netmail::whereIn('tftn_id',(($x=$this->children) ? $x->pluck('id') : collect())->push($this->id))
->where(function($q) {
return $q->whereNull('sent')
->orWhere('sent',FALSE);
}))
->whereNull('local')
$pkt = NULL;
if (($x=$this->netmailWaiting())
->count())
{
return $this->getPacket($x->get());
$pkt = $this->getPacket($x);
DB::table('netmails')
->whereIn('id',$x->pluck('id'))
->update(['sent_at'=>Carbon::now(),'sent_pkt'=>$pkt->name]);
}
return NULL;
return $pkt;
}
/**
@@ -411,6 +423,18 @@ class Address extends Model
return $o;
}
/**
* Netmail waiting to be sent to this system
*
* @return Collection
*/
public function netmailWaiting(): Collection
{
return Netmail::whereIn('tftn_id',(($x=$this->children) ? $x->pluck('id') : collect())->push($this->id))
->whereNull('sent_at')
->get();
}
/**
* Parse a string and split it out as an FTN array
*

View File

@@ -7,13 +7,15 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\UTCDateTime;
use App\Traits\ScopeActive;
use App\Traits\{QueryCacheableConfig,ScopeActive};
class Domain extends Model
{
use HasFactory,ScopeActive;
use HasFactory,ScopeActive,QueryCacheableConfig;
private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6;
@@ -65,17 +67,19 @@ 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(),
'datetime' => ['$gte',new UTCDateTime(Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())],
];
Cache::forget($key);
return Cache::remember($key,self::CACHE_TIME,function() {
$gb ="CONCAT(EXTRACT('year',datetime)::string,'-',LPAD(EXTRACT('month',datetime)::string,2,'0'),'-',LPAD(EXTRACT('day',datetime)::string,2,'0')) AS datetime";
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
$echostats = Echomail::select([DB::raw($gb),DB::raw('COUNT(*)')])
->whereIn('id',$this->echoareas->pluck('id')->toArray())
->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
->groupBy('datetime')
->orderBy('datetime')
->get();
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]; })
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
}
@@ -87,17 +91,19 @@ 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],
'datetime' => ['$gte',new UTCDateTime(Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())],
];
Cache::forget($key);
return Cache::remember($key,self::CACHE_TIME,function() use ($o) {
$gb ="CONCAT(EXTRACT('year',datetime)::string,'-',LPAD(EXTRACT('month',datetime)::string,2,'0'),'-',LPAD(EXTRACT('day',datetime)::string,2,'0')) AS datetime";
$echostats = Echomail::countGroupBy(['datetime',['datetime'=>'%Y-%m-%d']],$where);
$echostats = Echomail::select([DB::raw($gb),DB::raw('COUNT(*)')])
->whereIn('echoarea_id',[$o->id])
->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
->groupBy('datetime')
->orderBy('datetime')
->get();
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]; })
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
}

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();
}
}

View File

@@ -3,25 +3,29 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,MsgID,UseMongo};
use App\Traits\{EncodeUTF8,MsgID};
final class Echomail extends Model implements Packet
{
use SoftDeletes,MsgID,UseMongo,EncodeUTF8;
use SoftDeletes,EncodeUTF8,MsgID;
private const LOGKEY = 'ME-';
private array $set_seenby = [];
private array $set_path = [];
private ?string $set_packet = NULL;
protected $collection = FALSE;
protected $casts = [ 'kludges' => 'json' ];
protected $casts = [
'kludges' => 'json',
'rogue_seenby' => 'json',
'rogue_path' => 'json',
];
private const cast_utf8 = [
'to',
@@ -35,6 +39,20 @@ final class Echomail extends Model implements Packet
protected $dates = ['datetime'];
public function __set($key, $value)
{
switch ($key) {
case 'set_path':
case 'set_packet':
case 'set_seenby':
$this->{$key} = $value;
break;
default:
parent::__set($key,$value);
}
}
public static function boot()
{
parent::boot();
@@ -46,29 +64,43 @@ final class Echomail extends Model implements Packet
return;
}
// See if we need to export this message.
$exportto = $model->echoarea->addresses->pluck('system')->diff($model->seenby->pluck('system'));
$export_ao = collect();
foreach ($model->echoarea->domain->zones as $zo) {
foreach ($exportto as $so) {
$export_ao = $export_ao->merge($so->match($zo));
}
}
// Add to export
foreach ($export_ao as $ao) {
Log::info(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$ao->ftn));
DB::table('address_echomail')->insert([
'address_id'=>$ao->id,
'echomail_id'=>$model->id,
'export_date'=>Carbon::now()
// Save the seenby
foreach ($model->set_seenby as $aoid) {
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,packet) VALUES (?,?,?)',[
$model->id,
$aoid,
$model->set_packet,
]);
}
$model->seenby = $model->seenby->merge($export_ao)->pluck('id')->toArray();
$model->save();
// Save the Path
$ppoid = NULL;
foreach ($model->set_path as $aoid) {
$po = DB::select('INSERT INTO echomail_path (echomail_id,address_id,parent_id) VALUES (?,?,?) RETURNING id',[
$model->id,
$aoid,
$ppoid,
]);
$ppoid = $po[0]->id;
}
// See if we need to export this message.
$exportto = $model->echoarea->addresses->pluck('id')->diff($model->set_seenby);
if ($exportto->count()) {
Log::debug(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$exportto->join(',')));
// Save the seenby for the exported systems
$export_at = Carbon::now();
foreach ($exportto as $aoid) {
DB::insert('INSERT INTO echomail_seenby (echomail_id,address_id,export_at) VALUES (?,?,?)',[
$model->id,
$aoid,
$export_at,
]);
}
}
});
}
@@ -81,35 +113,19 @@ final class Echomail extends Model implements Packet
public function fftn()
{
return $this
->setConnection('pgsql')
->belongsTo(Address::class)
return $this->belongsTo(Address::class)
->withTrashed();
}
/* ATTRIBUTES */
public function getKludgesAttribute(?string $value): Collection
public function seenby()
{
return collect($this->castAttribute('kludges',$value));
return $this->belongsToMany(Address::class,'echomail_seenby')
->ftnOrder();
}
public function getPathAttribute(?array $value): Collection
public function path()
{
if (is_null($value))
return collect();
return Address::whereIn('id',$value)
->orderBy(DB::raw(sprintf("position (id::text in '(%s)')",join(',',$value))))
->get();
}
public function getSeenByAttribute(?array $value): Collection
{
if (is_null($value))
return collect();
return Address::whereIn('id',$value)->get();
return $this->belongsToMany(Address::class,'echomail_path');
}
/* METHODS */
@@ -149,11 +165,11 @@ final class Echomail extends Model implements Packet
if ($this->kludges)
$o->kludge = $this->kludges;
$o->kludge->put('mid',$this->id);
$o->kludge->put('dbid',$this->id);
$o->msgid = $this->msgid;
if ($this->reply)
$o->reply = $this->reply;
if ($this->replyid)
$o->replyid = $this->replyid;
$o->message = $this->msg;

View File

@@ -3,19 +3,19 @@
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,UseMongo};
use App\Traits\EncodeUTF8;
final class Netmail extends Model implements Packet
{
private const LOGKEY = 'MN-';
use SoftDeletes,UseMongo,EncodeUTF8;
use SoftDeletes,EncodeUTF8;
private const cast_utf8 = [
'to',
@@ -80,8 +80,8 @@ final class Netmail extends Model implements Packet
$o->flags = $this->flags;
$o->msgid = sprintf('%s %08x',$this->fftn->ftn3d,crc32($this->id));
if ($this->reply)
$o->reply = $this->reply;
if ($this->replyid)
$o->replyid = $this->replyid;
$o->message = $this->msg;
$o->tagline = $this->tagline;

View File

@@ -8,10 +8,11 @@ use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\DomainController;
use App\Traits\QueryCacheableConfig;
class System extends Model
{
use HasFactory;
use HasFactory,QueryCacheableConfig;
protected $dates = ['last_session'];
@@ -37,10 +38,7 @@ class System extends Model
public function addresses()
{
return $this->hasMany(Address::class)
->orderBy('region_id')
->orderBy('host_id')
->orderBy('node_id')
->orderBy('point_id');
->FTNorder();
}
/**

View File

@@ -4,11 +4,11 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ScopeActive;
use App\Traits\{QueryCacheableConfig,ScopeActive};
class Zone extends Model
{
use ScopeActive;
use ScopeActive,QueryCacheableConfig;
/* SCOPES */

View File

@@ -7,13 +7,15 @@ namespace App\Traits;
use Illuminate\Support\Arr;
use App\Classes\FTN\Message;
use App\Models\Setup;
trait MsgID
{
public function save(array $options = [])
{
if (! $this->exists) {
// Only create a MSGID for locally generated conetnt
if ((! $this->exists) && ($this->flags & Message::FLAG_LOCAL)) {
$ftns = Setup::findOrFail(config('app.id'))->system->match($this->fftn->zone)->first();
if (is_null(Arr::get($this->attributes,'msgid')))

View File

@@ -0,0 +1,17 @@
<?php
/**
* Set defaults of QueryCacheable
*/
namespace App\Traits;
use Rennokki\QueryCache\Traits\QueryCacheable;
trait QueryCacheableConfig
{
use QueryCacheable;
public $cacheFor = 900; // cache time, in seconds
protected static $flushCacheOnUpdate = TRUE;
public $cacheDriver = 'memcached';
}

View File

@@ -1,104 +0,0 @@
<?php
/**
* Models that store data in Mongo
*/
namespace App\Traits;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
trait UseMongo
{
/**
* Resolve a connection instance.
* We need to do this, because our relations are in pgsql and somehow loading a relation changes this models
* connection information. Using protected $connection is not enough.
*
* @param string|null $connection
*/
public static function resolveConnection($connection = null)
{
return static::$resolver->connection('mongodb');
}
/* ATTRIBUTES */
public function getMsgAttribute($value): string
{
return utf8_decode($value);
}
public function setMsgAttribute($value): void
{
$this->attributes['msg'] = utf8_encode($value);
}
public function getSubjectAttribute($value): string
{
return utf8_decode($value);
}
public function setSubjectAttribute($value): void
{
$this->attributes['subject'] = utf8_encode($value);
}
/* METHODS */
public static function countGroupBy(array $fields,array $where=[]): Collection
{
$query = collect();
if (count($where)) {
$where_condition = [];
foreach ($where as $key => $values) {
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' => [ '$and'=> [$where_condition]]
]);
}
$gb = collect();
foreach ($fields as $field)
if (is_array($field)) {
foreach ($field as $k=>$v) {
$gb->put('datetime',['$dateToString'=>['format'=>$v,'date'=>'$'.$k]]);
}
} else {
$gb->put($field,'$'.$field);
}
$query->push([
'$group' => [
'_id' => $gb->toArray(),
'count' => ['$sum' => 1]
]
]);
return (new self)
->groupBy($field)
->raw(function($collection) use ($query) {
return $collection->aggregate(
$query->toArray()
);
});
}
}

View File

@@ -1,21 +0,0 @@
<?php
/**
* Models that store data in Postgres
*/
namespace App\Traits;
trait UsePostgres
{
/**
* Resolve a connection instance.
* We need to do this, because our relations are in pgsql and somehow loading a relation changes this models
* connection information. Using protected $connection is not enough.
*
* @param string|null $connection
*/
public static function resolveConnection($connection = null)
{
return static::$resolver->connection('pgsql');
}
}