125 lines
3.1 KiB
PHP
125 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
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);
|
|
}
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
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 */
|
|
|
|
/**
|
|
* Get some message area stats for this domain
|
|
*
|
|
* @param bool $byarea
|
|
* @param Collection|NULL $systems
|
|
* @return Collection
|
|
*/
|
|
public function daily_area_stats(bool $byarea=FALSE,Collection $systems=NULL): Collection
|
|
{
|
|
if (! $this->echoareas->count())
|
|
return collect();
|
|
|
|
$echostats = Echomail::cacheFor(self::CACHE_TIME)->select([DB::raw('datetime::date as date'),'echoarea_id','echoareas.name',DB::raw('COUNT(*)')])
|
|
->join('echoareas',['echoareas.id'=>'echomails.echoarea_id'])
|
|
->join('domains',['domains.id'=>'echoareas.domain_id'])
|
|
->where('domain_id',$this->id)
|
|
->when($systems?->count(),function($query) use ($systems) { return $query->whereIn('fftn_id',$systems->pluck('addresses')->flatten()->pluck('id')->toArray()); })
|
|
->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
|
|
->groupBy(['echoarea_id','echoareas.name','date'])
|
|
->orderBy('echoareas.name')
|
|
->orderBy('date')
|
|
->with(['echoarea'])
|
|
->get();
|
|
|
|
if ($byarea)
|
|
return $echostats
|
|
->groupBy(['echoarea_id'])
|
|
->map(function($item,$key) {
|
|
return [
|
|
'name' => $item->first()->echoarea->name,
|
|
'data' => $item->groupby('date')->map(function($item) {
|
|
return [
|
|
'x' => Carbon::create($item->first()->date)->timestamp*1000,
|
|
'y' => $item->sum('count')
|
|
];
|
|
})->values(),
|
|
'dashStyle' => 'ShortDot',
|
|
];
|
|
})->values();
|
|
|
|
else
|
|
return $echostats
|
|
->groupBy('date')
|
|
->map(function($item) { return ['x'=>Carbon::create($item->first()->date)->timestamp*1000,'y'=>$item->sum('count')]; })
|
|
->values();
|
|
}
|
|
|
|
/**
|
|
* Get the latest message in each echomail area
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function latest_echomail_message(): Collection
|
|
{
|
|
return Echoarea::cacheFor(self::CACHE_TIME)
|
|
->select([
|
|
'echoareas.*',DB::raw('max(datetime) as last_message')
|
|
])
|
|
->leftJoin('echomails',['echomails.echoarea_id'=>'echoareas.id'])
|
|
->where('domain_id',$this->id)
|
|
->groupBy('echoareas.id')
|
|
->orderBy('echoareas.name')
|
|
->get();
|
|
}
|
|
} |