clrghouz/app/Models/Domain.php
2022-01-15 14:58:41 +11:00

141 lines
3.7 KiB
PHP

<?php
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 Illuminate\Support\Facades\DB;
use MongoDB\BSON\UTCDateTime;
use App\Traits\{QueryCacheableConfig,ScopeActive};
class Domain extends Model
{
use HasFactory,ScopeActive,QueryCacheableConfig;
private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6;
/* SCOPES */
/**
* Only query active records
*/
public function scopePublic($query)
{
return $query->where('public',TRUE);
}
/* RELATIONS */
public function echoareas()
{
return $this->hasMany(Echoarea::class);
}
public function fileareas()
{
return $this->hasMany(Filearea::class);
}
public function zones()
{
return $this->hasMany(Zone::class);
}
/* CASTS */
public function getHomePageAttribute($value)
{
return $value ? gzuncompress(base64_decode($value)) : 'No available information at the moment.';
}
public function setHomePageAttribute($value)
{
$this->attributes['homepage'] = base64_encode(gzcompress($value,9));
}
/* METHODS */
public function daily_area_stats(): Collection
{
if (! $this->echoareas->count())
return collect();
$key = sprintf('%s_%d','daily_area_stats',$this->id);
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::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
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
}
public function daily_echoarea_stats(Echoarea $o): Collection
{
if (! $this->echoareas->count())
return collect();
$key = sprintf('%s_%d-%d','daily_echoarea_stats',$this->id,$o->id);
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::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
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
}
public function stats(System $o=NULL): Collection
{
if (! $this->echoareas->count())
return collect();
$key = sprintf('%s_%d_%d','stats',$this->id,$o?->id);
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'));
$echostats = Echomail::countGroupBy(['echoarea_id'],$where->toArray());
return $this->echoareas->map(function($item) use ($echostats) {
$stats = $echostats->filter(function($x) use ($item) {
return $x->id->echoarea_id == $item->id;
});
$item->count = 0;
foreach ($stats as $o)
$item->count += $o->count;
return $item;
});
});
}
}