60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\CollectorEntity;
|
|
use App\Entities\TrafficEntity;
|
|
|
|
class CollectorModel extends CommonModel
|
|
{
|
|
const TABLE = "collectorinfo";
|
|
const PK = "uid";
|
|
const TITLE = "uid";
|
|
protected $table = self::TABLE;
|
|
// protected $useAutoIncrement = false;
|
|
protected $primaryKey = self::PK;
|
|
protected $returnType = CollectorEntity::class;
|
|
protected $allowedFields = [
|
|
"uid",
|
|
"trafficinfo_uid",
|
|
"in",
|
|
"out",
|
|
"raw_in",
|
|
"raw_out",
|
|
"updated_at"
|
|
];
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getLastEntity($uid): CollectorEntity|null
|
|
{
|
|
return $this
|
|
->where('trafficinfo_uid', $uid)
|
|
->orderBy('created_at', 'DESC')
|
|
->first(); // 첫 번째 레코드(=가장 최신)
|
|
}
|
|
|
|
public function get95PercentileValue(TrafficEntity $trafficEntity, string $field, string $startDate, string $endDate)
|
|
{
|
|
$sql = "WITH ranked AS (
|
|
SELECT `{$field}`,`created_at`, ROW_NUMBER() OVER (ORDER BY `{$field}` DESC) AS rn,COUNT(*) OVER () AS total
|
|
FROM {$this->table}
|
|
WHERE trafficinfo_uid = ?
|
|
AND created_at BETWEEN ? AND ?
|
|
)
|
|
SELECT `{$field}`
|
|
FROM ranked
|
|
WHERE rn = FLOOR(total * 0.05)
|
|
LIMIT 1";
|
|
$result = $this->query($sql, [
|
|
$trafficEntity->getPK(),
|
|
$startDate,
|
|
$endDate
|
|
]);
|
|
log_message('debug', '[95PERCENTILE SQL] :' . $this->db->getLastQuery());
|
|
return $result->getRow()->$field ?? null;
|
|
}
|
|
}
|