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-11-26 05:58:50 +00:00
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 ;
2021-12-03 00:24:23 +00:00
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 ;
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
/* 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 ();
2021-11-26 05:58:50 +00:00
return $echostats
2022-01-01 05:59:35 +00:00
-> map ( function ( $item ) { return [ 'x' => $item -> datetime -> timestamp * 1000 , 'y' => $item -> count ]; })
2021-11-26 05:58:50 +00:00
-> values ();
});
2021-11-20 06:58:46 +00:00
}
2021-11-26 05:16:33 +00:00
public function daily_echoarea_stats ( Echoarea $o ) : Collection
{
if ( ! $this -> echoareas -> count ())
return collect ();
2021-11-26 05:58:50 +00:00
$key = sprintf ( '%s_%d-%d' , 'daily_echoarea_stats' , $this -> id , $o -> id );
2021-11-26 05:16:33 +00:00
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 " ;
2021-11-26 05:16:33 +00:00
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 ();
2021-11-26 05:58:50 +00:00
return $echostats
2022-01-01 05:59:35 +00:00
-> map ( function ( $item ) { return [ 'x' => $item -> datetime -> timestamp * 1000 , 'y' => $item -> count ]; })
2021-11-26 05:58:50 +00:00
-> values ();
});
2021-11-26 05:16:33 +00:00
}
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 ()]);
2021-12-03 00:24:23 +00:00
$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
}