You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.1 KiB

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Clientlogsecond extends Model
{
use HasFactory;
protected $table = 'clientlogsecond';
protected $fillable = [
'to_ip',
'to_time',
'this_ip',
'this_time',
'diffsecond',
'name',
'pushed',
];
protected $appends = ['before_time','force_time', 'timeout'];
public function getBeforeTimeAttribute()
{
return substr(Carbon::parse($this->this_time)->subMilliseconds($this->diffsecond)->format('Y-m-d H:i:s.u'), 0, -3);
}
public function getForceTimeAttribute()
{
$forceTime = UserLog::where('action', 'forceTime')->pluck('created_at')->toArray();
// 將資料轉換為 Y-m-d H:i 格式
$forceTime = array_map(function ($item) {
return Carbon::parse($item)->format('Y-m-d H:i');
}, $forceTime);
// 將this_time 轉換為 Y-m-d H:i 格式 並比對是否存在於 forceTime 陣列中
$thisTime = Carbon::parse($this->this_time)->format('Y-m-d H:i');
// 如果存在於陣列中 則回傳 1 代表強制校時 不存在則回傳 0
return in_array($thisTime, $forceTime) ? 1 : 0;
}
public function getTimeoutAttribute()
{
// this_time 與 serverlog 最新一筆,差超過150秒則回傳1 代表超時
$serverLog = Serverlog::orderBy('this_time', 'desc')->first();
if (!$serverLog) {
return 1;
}
return Carbon::parse($this->this_time)->diffInSeconds($serverLog->this_time) > 150 ? 1 : 0;
// return Carbon::parse($this->this_time)->diffInSeconds() > 150 ? 1 : 0;
}
// 修改現有的diffsecond格式
public function getDiffsecondAttribute($value)
{
return floatval($value) * 1000;
}
public static function cachedDistinctName()
{
return Cache::remember('client_log2_distinct_name', 3600, function () {
return self::select('name')->distinct()->get();
});
}
}