clrghouz/app/Models/Domain.php

141 lines
3.7 KiB
PHP
Raw Normal View History

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;
use Illuminate\Support\Facades\Cache;
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;
use MongoDB\BSON\UTCDateTime;
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;
private const CACHE_TIME = 3600;
private const STATS_MONTHS = 6;
2021-06-14 11:33:18 +00:00
/* SCOPES */
/**
* Only query active records
*/
public function scopePublic($query)
{
return $query->where('public',TRUE);
}
/* 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);
}
public function zones()
{
return $this->hasMany(Zone::class);
}
2021-06-14 11:33:18 +00:00
/* 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));
}
2021-10-26 12:19:55 +00:00
/* METHODS */
2021-11-20 06:58:46 +00:00
public function daily_area_stats(): Collection
{
if (! $this->echoareas->count())
return collect();
2021-11-26 06:19:55 +00:00
$key = sprintf('%s_%d','daily_area_stats',$this->id);
2021-11-20 06:58:46 +00:00
2022-01-01 05:59:35 +00:00
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";
2021-11-20 06:58:46 +00:00
2022-01-01 05:59:35 +00:00
$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
2022-01-01 05:59:35 +00:00
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
2021-11-20 06:58:46 +00:00
}
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);
2022-01-01 05:59:35 +00:00
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";
2022-01-01 05:59:35 +00:00
$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
2022-01-01 05:59:35 +00:00
->map(function($item) { return ['x'=>$item->datetime->timestamp*1000,'y'=>$item->count]; })
->values();
});
}
2021-10-26 12:19:55 +00:00
public function stats(System $o=NULL): Collection
{
if (! $this->echoareas->count())
return collect();
2021-11-26 06:19:55 +00:00
$key = sprintf('%s_%d_%d','stats',$this->id,$o?->id);
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
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())]);
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
if ($o)
$where->put('fftn_id',$o->addresses()->pluck('id'));
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
$echostats = Echomail::countGroupBy(['echoarea_id'],$where->toArray());
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
return $this->echoareas->map(function($item) use ($echostats) {
$stats = $echostats->filter(function($x) use ($item) {
return $x->id->echoarea_id == $item->id;
});
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
$item->count = 0;
2021-10-26 12:19:55 +00:00
2021-11-26 06:19:55 +00:00
foreach ($stats as $o)
$item->count += $o->count;
return $item;
});
2021-10-26 12:19:55 +00:00
});
}
2021-05-13 12:40:21 +00:00
}