From c0e3dc577bc46ccfd422ef5038ea4c48bb21322a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 28 Nov 2025 09:38:24 +0900 Subject: [PATCH] trafficmonitor init...2 --- app/Models/CollectorModel.php | 50 +++++---------------------- app/Views/admin/traffic/dashboard.php | 2 +- 2 files changed, 10 insertions(+), 42 deletions(-) diff --git a/app/Models/CollectorModel.php b/app/Models/CollectorModel.php index 4ff9d57..3cd2201 100644 --- a/app/Models/CollectorModel.php +++ b/app/Models/CollectorModel.php @@ -36,56 +36,24 @@ class CollectorModel extends CommonModel ->first(); // 첫 번째 레코드(=가장 최신) } - /** - * DESC 기준 95% + 1번째 값을 가져오는 함수 - * - * @param string $field in_kbits 또는 out_kbits - * @param string $startDate '2024-10-01' - * @param string $endDate '2024-10-30' - * @return mixed 해당 자리의 수치값 - */ - public function get95PercentileValue_OLD(TrafficEntity $trafficEntity, string $field, string $startDate, string $endDate) - { - // 1) 해당 조건의 전체 개수 구하기 - $total = - $this->where("trafficinfo_uid", $trafficEntity->getPK()) - ->where("created_at >=", $startDate) - ->where("created_at <=", $endDate) - ->countAllResults(true); // true해야 queryu reset 함 - // false = 쿼리 빌더 초기화 X - if ($total === 0) { - return null; - } - // 2) index = floor(total * 0.95) - $index = floor($total * 0.95); - // 3) 95% + 1 번째 값 조회 - $builder = $this->builder(); - $builder->select($field) - ->where("trafficinfo_uid", $trafficEntity->getPK()) - ->where("created_at >=", $startDate) - ->where("created_at <=", $endDate) - ->orderBy($field, "DESC") - ->limit(1, $index - 1); // LIMIT offset, 1 - - // 🔥 실행되기 전에 SQL 확인 - $sql = $builder->getCompiledSelect(false); //반드시 false해야 queryu reset 않함 - log_message('debug', '[95PERCENTILE QUERY] ' . $sql); - $row = $builder->get()->getFirstRow('array'); - return $row[$field] ?? null; - } - public function get95PercentileValue(TrafficEntity $trafficEntity, string $field, string $startDate, string $endDate) { $sql = "WITH ranked AS ( - SELECT `{$field}`,`created_at`, ROW_NUMBER() OVER (ORDER BY `in` DESC) AS rn,COUNT(*) OVER () AS total + 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) + 1 +WHERE rn = FLOOR(total * 0.05) LIMIT 1"; - return $this->query($sql, [$trafficEntity->getPK(), $startDate, $endDate])->getRowArray()[$field]; + $result = $this->query($sql, [ + $trafficEntity->getPK(), + $startDate, + $endDate + ]); + log_message('debug', '[95PERCENTILE SQL] :' . $this->db->getLastQuery()); + return $result->getRow()->$field ?? null; } } diff --git a/app/Views/admin/traffic/dashboard.php b/app/Views/admin/traffic/dashboard.php index 559cb66..b2ff4b6 100644 --- a/app/Views/admin/traffic/dashboard.php +++ b/app/Views/admin/traffic/dashboard.php @@ -118,7 +118,7 @@ const out95 = calculates.out_kbits ?? '-'; // 차트 제목 업데이트 - chartTitleElement.textContent = `트래픽 속도 추이 (IN: ${in95} Kbit/s, OUT: ${out95} Kbit/s)`; + chartTitleElement.textContent = `트래픽 속도 추이 , 상위5%를 제외한 최대값=>(IN: ${in95} Kbit/s, OUT: ${out95} Kbit/s)`; const data = result.data || []; if (data.length === 0) {