2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-11-20 06:58:46 +00:00
|
|
|
use Carbon\Carbon;
|
2021-06-15 12:19:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2021-05-13 12:40:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-10-26 12:19:55 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-01-01 05:59:35 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-05-13 12:40:21 +00:00
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
use App\Traits\{QueryCacheableConfig,ScopeActive};
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
class Domain extends Model
|
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
use HasFactory,ScopeActive,QueryCacheableConfig;
|
|
|
|
|
2021-11-26 05:58:50 +00:00
|
|
|
private const CACHE_TIME = 3600;
|
2021-12-03 00:24:23 +00:00
|
|
|
private const STATS_MONTHS = 6;
|
2021-06-14 05:46:18 +00:00
|
|
|
|
2021-06-14 11:33:18 +00:00
|
|
|
/* SCOPES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only query active records
|
|
|
|
*/
|
|
|
|
public function scopePublic($query)
|
|
|
|
{
|
|
|
|
return $query->where('public',TRUE);
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2021-08-11 13:45:30 +00:00
|
|
|
public function echoareas()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Echoarea::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fileareas()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Filearea::class);
|
|
|
|
}
|
|
|
|
|
2021-06-14 05:46:18 +00:00
|
|
|
public function zones()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Zone::class);
|
|
|
|
}
|
2021-06-14 11:33:18 +00:00
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/* ATTRIBUTES */
|
2021-06-14 11:33:18 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
2021-10-26 12:19:55 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-11-20 06:58:46 +00:00
|
|
|
{
|
|
|
|
if (! $this->echoareas->count())
|
|
|
|
return collect();
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
$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()); })
|
2022-01-01 05:59:35 +00:00
|
|
|
->where('datetime','>=',Carbon::now()->subMonths(self::STATS_MONTHS)->startOfMonth())
|
2022-01-05 13:19:57 +00:00
|
|
|
->groupBy(['echoarea_id','echoareas.name','date'])
|
|
|
|
->orderBy('echoareas.name')
|
|
|
|
->orderBy('date')
|
|
|
|
->with(['echoarea'])
|
2022-01-01 05:59:35 +00:00
|
|
|
->get();
|
2021-11-26 05:58:50 +00:00
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
if ($byarea)
|
2021-11-26 05:58:50 +00:00
|
|
|
return $echostats
|
2022-01-05 13:19:57 +00:00
|
|
|
->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
|
2021-11-26 05:58:50 +00:00
|
|
|
return $echostats
|
2022-01-05 13:19:57 +00:00
|
|
|
->groupBy('date')
|
|
|
|
->map(function($item) { return ['x'=>Carbon::create($item->first()->date)->timestamp*1000,'y'=>$item->sum('count')]; })
|
2021-11-26 05:58:50 +00:00
|
|
|
->values();
|
2021-11-26 05:16:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 13:19:57 +00:00
|
|
|
/**
|
|
|
|
* Get the latest message in each echomail area
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function latest_echomail_message(): Collection
|
2021-10-26 12:19:55 +00:00
|
|
|
{
|
2022-01-05 13:19:57 +00:00
|
|
|
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();
|
2021-10-26 12:19:55 +00:00
|
|
|
}
|
2021-05-13 12:40:21 +00:00
|
|
|
}
|