diff --git a/app/Entities/CollectorEntity.php b/app/Entities/CollectorEntity.php index dd41dfc..91945e8 100644 --- a/app/Entities/CollectorEntity.php +++ b/app/Entities/CollectorEntity.php @@ -9,4 +9,24 @@ class CollectorEntity extends CommonEntity const PK = CollectorModel::PK; const TITLE = CollectorModel::TITLE; //기본기능용 + public function getTrafficInfoUID(): int + { + return $this->attributes['trafficinfo_uid']; + } + public function getIn(): int + { + return $this->attributes['in']; + } + public function getOut(): int + { + return $this->attributes['out']; + } + public function getRawIn(): int + { + return $this->attributes['raw_in']; + } + public function getRawOut(): int + { + return $this->attributes['raw_out']; + } } diff --git a/app/Models/CollectorModel.php b/app/Models/CollectorModel.php index d66c29f..5f65462 100644 --- a/app/Models/CollectorModel.php +++ b/app/Models/CollectorModel.php @@ -26,4 +26,12 @@ class CollectorModel extends CommonModel { parent::__construct(); } + + public function getLastEntity($uid): CollectorEntity + { + return $this + ->where('trafficinfo_uid', $uid) + ->orderBy('created_at', 'DESC') + ->first(); // 첫 번째 레코드(=가장 최신) + } } diff --git a/app/Services/CollectorService.php b/app/Services/CollectorService.php index e78d481..6c61ab9 100644 --- a/app/Services/CollectorService.php +++ b/app/Services/CollectorService.php @@ -108,18 +108,18 @@ class CollectorService extends CommonService log_message('warning', $message); throw new \Exception($message); } - // 이전 데이터를 조회하여 Rate 계산에 사용 (trafficModel 사용) - $lastEntry = $this->model->getLastEntryByInfoUid($trafficEntity->getPK()); + // 이전 데이터를 조회하여 Rate 계산에 사용 + $lastEntity = $this->model->getLastEntity($trafficEntity->getPK()); $inKbitsSec = 0.0; $outKbitsSec = 0.0; // 이전 데이터가 있어야만 Rate 계산 가능 - if ($lastEntry !== null) { - $lastTime = Time::parse($lastEntry['created_at'])->getTimestamp(); + if ($lastEntity !== null) { + $lastTime = Time::parse($lastEntity->getCreatedAt())->getTimestamp(); $deltaTime = Time::now()->getTimestamp() - $lastTime; if ($deltaTime > 0) { // Raw Octets 값의 차분 계산 - $deltaInOctets = $currentInOctets - $lastEntry['raw_in_octets']; - $deltaOutOctets = $currentOutOctets - $lastEntry['raw_out_octets']; + $deltaInOctets = $currentInOctets - $lastEntity->getRawIn(); + $deltaOutOctets = $currentOutOctets - $lastEntity->getRawOut(); // Kbit/s 계산: (Delta_Octets * 8 bits) / Delta_Time_Seconds / 1000 (-> Kbit/s) $inKbitsSec = ($deltaInOctets * 8) / $deltaTime / 1000;