diff --git a/app/Controllers/Admin/TrafficController.php b/app/Controllers/Admin/TrafficController.php index e734212..72167db 100644 --- a/app/Controllers/Admin/TrafficController.php +++ b/app/Controllers/Admin/TrafficController.php @@ -225,7 +225,7 @@ class TrafficController extends AdminController /** * AJAX 요청을 처리하고, 모델에서 집계된 데이터를 JSON 형식으로 반환합니다. */ - public function getAggregatedData($uid): ResponseInterface + public function getAggregatedData($uid): string|ResponseInterface { $entity = $this->service->getEntity($uid); if (!$entity instanceof TrafficEntity) { @@ -247,11 +247,18 @@ class TrafficController extends AdminController $start_timestamp = $startDate . ' 00:00:00'; $end_timestamp = $endDate . ' 23:59:59'; $aggregateDatas = $collectorService->getAggregateDatas($entity, $start_timestamp, $end_timestamp); + $aggregateCalculates = $collectorService->get95PercentileBoth($entity, $start_timestamp, $end_timestamp); + + // 디버깅 로그 추가 + // log_message('debug', 'aggregateDatas: ' . print_r($aggregateDatas, true)); + log_message('debug', 'aggregateCalculates: ' . print_r($aggregateCalculates, true)); + // 데이터를 JSON 형식으로 반환 return $this->response->setJSON([ 'message' => sprintf("[%s~%s] %s 트랙픽차트 ", $startDate, $endDate, $entity->getCustomTitle()), 'status' => 'success', - 'data' => $aggregateDatas // 이 데이터는 이제 사용자가 제공한 필드 구조를 가집니다. + 'data' => $aggregateDatas, // 이 데이터는 이제 사용자가 제공한 필드 구조를 가집니다. + 'calculates' => $aggregateCalculates ]); } catch (\Exception $e) { // 예외 처리 diff --git a/app/Models/CollectorModel.php b/app/Models/CollectorModel.php index ac6fd44..0a8eadd 100644 --- a/app/Models/CollectorModel.php +++ b/app/Models/CollectorModel.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Entities\CollectorEntity; +use App\Entities\TrafficEntity; class CollectorModel extends CommonModel { @@ -34,4 +35,43 @@ class CollectorModel extends CommonModel ->orderBy('created_at', 'DESC') ->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(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) + 1 + $index = floor($total * 0.95) + 1; + // 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()->getRowArray(); + return $row[$field] ?? null; + } } diff --git a/app/Services/CollectorService.php b/app/Services/CollectorService.php index 880ed12..38a8458 100644 --- a/app/Services/CollectorService.php +++ b/app/Services/CollectorService.php @@ -103,8 +103,16 @@ class CollectorService extends CommonService } return $entities; } - - + /** + * in_kbits과 out_kbits 두 값을 한꺼번에 구하는 함수 + */ + public function get95PercentileBoth(TrafficEntity $trafficEntity, string $startDate, string $endDate): array + { + return [ + 'in_kbits' => $this->model->get95PercentileValue($trafficEntity, 'in', $startDate, $endDate), + 'out_kbits' => $this->model->get95PercentileValue($trafficEntity, 'out', $startDate, $endDate) + ]; + } public function getCalculatedData(TrafficEntity $trafficEntity): array { diff --git a/app/Views/admin/traffic/dashboard.php b/app/Views/admin/traffic/dashboard.php index 8986abd..559cb66 100644 --- a/app/Views/admin/traffic/dashboard.php +++ b/app/Views/admin/traffic/dashboard.php @@ -10,53 +10,39 @@
-
- -

getCustomTitle() ?>트래픽 속도 추이 (IN/OUT Kbit/s)

- -
+

getCustomTitle() ?> 트래픽 속도 추이 (IN/OUT Kbit/s)

+
- -
- + + +