trafficmonitor init...1

This commit is contained in:
choi.jh 2025-11-13 10:24:15 +09:00
parent f7123ffb8b
commit 7037cd98f0
9 changed files with 140 additions and 75 deletions

View File

@ -305,4 +305,63 @@ abstract class AdminController extends CommonController
$this->getAuthContext()->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""));
return $this->action_render_process($this->getActionPaths(), $action, $this->getViewDatas());
}
//OUPUT Document 관련
protected function download_process(string $document_type, mixed $loaded_data): array
{
$full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel";
switch ($document_type) {
case 'excel':
$file_name = sprintf("%s_%s.xlsx", $this->service->getClassPaths(false, "_"), date('Y-m-d_Hm'));
$writer = IOFactory::createWriter($loaded_data, 'Xlsx');
$writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
break;
case 'pdf':
$file_name = sprintf("%s_%s.pdf", $this->service->getClassPaths(false, "_"), date('Y-m-d_Hm'));
$writer = new Mpdf($loaded_data);
$writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
break;
}
return array($full_path, $file_name);
}
// Download
public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string
{
$action = __FUNCTION__;
try {
//초기화
$this->action_init_process($action);
switch ($output_type) {
case 'excel':
case 'pdf':
helper(['form']);
foreach ($this->getService()->getEntities() as $entity) {
$entities[] = $entity;
}
$this->entities = $entities;
$html = $this->getResultSuccess();
//data loading
$reader = new Html();
$loaded_data = $reader->loadFromString($html);
list($full_path, $file_name) = $this->download_process($output_type, $loaded_data);
$full_path .= DIRECTORY_SEPARATOR . $file_name;
break;
default:
if (!$uid) {
throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다.");
}
$entity = $this->getService()->getEntity($uid);
if (!$entity) {
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
}
$this->entity = $entity;
list($file_name, $uploaded_filename) = $entity->getDownlaodFile();
$full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename;
break;
}
return $this->response->download($full_path, null)->setFileName($file_name);
} catch (\Exception $e) {
return $this->getResultFail($e->getMessage());
}
}
}

View File

@ -16,12 +16,10 @@ use CodeIgniter\Validation\Exceptions\ValidationException;
*/
abstract class AuthService
{
protected $title = null;
private $_helper = null;
private ?AuthContext $_authContext = null;
private array $_classPaths = [];
protected $formServiceInstance = null;
protected ?CommonModel $model = null;
protected ?AuthHelper $helperInstance = null;
protected function __construct(CommonModel $model)
{
@ -31,16 +29,16 @@ abstract class AuthService
abstract public function getFormService(): mixed;
final public function getHelper(): AuthHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new AuthHelper();
if ($this->_helper === null) {
$this->_helper = new AuthHelper();
// AuthHelper에 필요한 기본 메타데이터만 설정합니다. (CRUD 제거)
$this->helperInstance->setAttributes([
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->helperInstance;
return $this->_helper;
}
//인증세션용
final public function getAuthContext(): AuthContext

View File

@ -13,6 +13,7 @@ use RuntimeException;
class GoogleService extends AuthService
{
private $_form = null;
public function __construct(UserModel $model, private CURL $socket)
{
parent::__construct($model);
@ -20,17 +21,17 @@ class GoogleService extends AuthService
}
public function getFormService(): GoogleForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new GoogleForm();
$this->formServiceInstance->setAttributes([
if ($this->_form === null) {
$this->_form = new GoogleForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
'class_path' => $this->getClassPaths(false),
]);
}
return $this->formServiceInstance;
return $this->_form;
}
//기본기능
protected function login_process(array $formDatas): UserEntity
@ -44,9 +45,9 @@ class GoogleService extends AuthService
throw new PageNotFoundException("회원[{$sns_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다.");
}
return $entity;
} catch (\Google_Service_Exception $e) {
log_message('error', '구글 서비스 예외발생: ' . $e->getMessage());
throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요.");
// } catch (\Google_Service_Exception $e) {
// log_message('error', '구글 서비스 예외발생: ' . $e->getMessage());
// throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요.");
} catch (\Exception $e) {
log_message('error', $e->getMessage());
throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.\n{$e->getMessage()}");

View File

@ -12,6 +12,7 @@ use RuntimeException;
class LocalService extends AuthService
{
private $_form = null;
public function __construct(UserModel $model)
{
parent::__construct($model);
@ -20,9 +21,9 @@ class LocalService extends AuthService
public function getFormService(): LocalForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new LocalForm();
$this->formServiceInstance->setAttributes([
if ($this->_form === null) {
$this->_form = new LocalForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -30,7 +31,7 @@ class LocalService extends AuthService
'class_path' => $this->getClassPaths(false),
]);
}
return $this->formServiceInstance;
return $this->_form;
}
protected function login_process(array $formDatas): UserEntity
{

View File

@ -13,9 +13,11 @@ use RuntimeException;
class CollectorService extends CommonService
{
const OID_IF_IN_OCTETS = '1.3.6.1.2.1.2.2.1.10.'; // ifInOctets (Raw Octets)
private $_form = null;
private $_helper = null;
const OID_IF_IN_OCTETS = '1.3.6.1.2.1.2.2.1.10.'; // ifInOctets (Raw Octets)
const OID_IF_OUT_OCTETS = '1.3.6.1.2.1.2.2.1.16.'; // ifOutOctets (Raw Octets)
const SNMP_VERSION = '2c';
const SNMP_VERSION = '2c';
// Counter32의 최대값 + 1 (오버플로우 보정을 위해 사용)
const MAX_COUNTER_32BIT = 4294967296;
public function __construct(CollectorModel $model)
@ -25,31 +27,31 @@ class CollectorService extends CommonService
}
public function getFormService(): CollectorForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new CollectorForm();
$this->formServiceInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
if ($this->_form === null) {
$this->_form = new CollectorForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false),
'class_path' => $this->getClassPaths(false),
]);
}
return $this->formServiceInstance;
return $this->_form;
}
public function getHelper(): CollectorHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new CollectorHelper();
$this->helperInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
if ($this->_helper === null) {
$this->_helper = new CollectorHelper();
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false),
'class_path' => $this->getClassPaths(false),
]);
}
return $this->helperInstance;
return $this->_helper;
}
//기본 기능부분
public function create(object $dto): CollectorEntity
@ -77,9 +79,9 @@ class CollectorService extends CommonService
//SNMP연결
private function getSNMPOctets(TrafficEntity $trafficEntity, string $oid): ?int
{
$fullOid = $oid . $trafficEntity->getInterface();
$fullOid = $oid . $trafficEntity->getInterface();
$community = $trafficEntity->getCommunity();
$ip = $trafficEntity->getIP();
$ip = $trafficEntity->getIP();
// snmp2_get을 사용하여 SNMP v2c로 요청
// 💡 snmp2_get() 함수가 존재하지 않는다는 LSP 오류를 무시하기 위해 @suppress 태그 사용
/** @phpstan-ignore-next-line */
@ -98,7 +100,7 @@ class CollectorService extends CommonService
}
public function getCalculatedData(TrafficEntity $trafficEntity): array
{
$currentInOctets = $this->getSNMPOctets($trafficEntity, self::OID_IF_IN_OCTETS);
$currentInOctets = $this->getSNMPOctets($trafficEntity, self::OID_IF_IN_OCTETS);
$currentOutOctets = $this->getSNMPOctets($trafficEntity, self::OID_IF_OUT_OCTETS);
if ($currentInOctets === null || $currentOutOctets === null) {
@ -111,16 +113,16 @@ class CollectorService extends CommonService
// $this->model은 TrafficDataModel의 인스턴스라고 가정
$lastEntity = $this->model->getLastEntity($trafficEntity->getPK());
$inKbitsSec = 0.0;
$inKbitsSec = 0.0;
$outKbitsSec = 0.0;
// 이전 데이터가 있어야만 Rate 계산 가능
if ($lastEntity !== null) {
$lastTime = Time::parse($lastEntity->getCreatedAt())->getTimestamp();
$lastTime = Time::parse($lastEntity->getCreatedAt())->getTimestamp();
$deltaTime = Time::now()->getTimestamp() - $lastTime;
if ($deltaTime > 0) {
$lastIn = $lastEntity->getRawIn();
$lastIn = $lastEntity->getRawIn();
$lastOut = $lastEntity->getRawOut();
// 💡 1. 인바운드 Octets 차분 계산 (오버플로우 처리)
@ -145,7 +147,7 @@ class CollectorService extends CommonService
// Kbit/s 계산: (Delta_Octets * 8 bits) / Delta_Time_Seconds / 1000 (-> Kbit/s)
// 실수(float) 연산으로 정확도를 높입니다.
$inKbitsSec = ($deltaInOctets * 8.0) / $deltaTime / 1000.0;
$inKbitsSec = ($deltaInOctets * 8.0) / $deltaTime / 1000.0;
$outKbitsSec = ($deltaOutOctets * 8.0) / $deltaTime / 1000.0;
} else {
log_message('error', "시간 차이 오류 발생: {$trafficEntity->getIP()} - {$deltaTime}초 (UID: {$trafficEntity->getPK()})");
@ -154,12 +156,12 @@ class CollectorService extends CommonService
// DB에 저장할 데이터를 배열로 반환
return [
// 💡 정수 캐스팅 대신 반올림을 사용하여 정밀도 유지 및 데이터 형식 일치
// 💡 요청에 따라 'in'과 'out' 값을 정수형으로 캐스팅하여 소수점 이하를 버림
'trafficinfo_uid' => (int)$trafficEntity->getPK(),
'in' => round($inKbitsSec, 2), // 소수점 2자리까지 반올림
'out' => round($outKbitsSec, 2), // 소수점 2자리까지 반올림
'raw_in' => (int)$currentInOctets,
'raw_out' => (int)$currentOutOctets,
'in' => (int)$inKbitsSec, // 정수형으로 반환
'out' => (int)$outKbitsSec, // 정수형으로 반환
'raw_in' => (int)$currentInOctets,
'raw_out' => (int)$currentOutOctets,
];
}
}

View File

@ -12,8 +12,6 @@ abstract class CommonService
{
private array $_classPaths = [];
protected $title = null;
protected $formServiceInstance = null;
protected $helperInstance = null;
protected function __construct(protected CommonModel $model) {}
abstract public function getFormService(): mixed;
abstract public function getHelper(): mixed;

View File

@ -11,6 +11,8 @@ use RuntimeException;
class MylogService extends CommonService
{
private $_form = null;
private $_helper = null;
public function __construct(MylogModel $model)
{
parent::__construct($model);
@ -18,9 +20,9 @@ class MylogService extends CommonService
}
public function getFormService(): MylogForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new MylogForm();
$this->formServiceInstance->setAttributes([
if ($this->_form === null) {
$this->_form = new MylogForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -28,13 +30,13 @@ class MylogService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
return $this->_form;
}
public function getHelper(): MylogHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new MylogHelper();
$this->helperInstance->setAttributes([
if ($this->_helper === null) {
$this->_helper = new MylogHelper();
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -42,7 +44,7 @@ class MylogService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->helperInstance;
return $this->_helper;
}
//기본 기능부분
public function create(object $dto): MylogEntity

View File

@ -11,6 +11,8 @@ use RuntimeException;
class TrafficService extends CommonService
{
private $_form = null;
private $_helper = null;
public function __construct(TrafficModel $model)
{
parent::__construct($model);
@ -18,9 +20,9 @@ class TrafficService extends CommonService
}
public function getFormService(): TrafficForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new TrafficForm();
$this->formServiceInstance->setAttributes([
if ($this->_form === null) {
$this->_form = new TrafficForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -28,13 +30,13 @@ class TrafficService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
return $this->_form;
}
public function getHelper(): TrafficHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new TrafficHelper();
$this->helperInstance->setAttributes([
if ($this->_helper === null) {
$this->_helper = new TrafficHelper();
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -42,7 +44,7 @@ class TrafficService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->helperInstance;
return $this->_helper;
}
//기본 기능부분
public function create(object $dto): TrafficEntity

View File

@ -11,6 +11,8 @@ use RuntimeException;
class UserService extends CommonService
{
private $_form = null;
private $_helper = null;
public function __construct(UserModel $model)
{
parent::__construct($model);
@ -18,9 +20,9 @@ class UserService extends CommonService
}
public function getFormService(): UserForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new UserForm();
$this->formServiceInstance->setAttributes([
if ($this->_form === null) {
$this->_form = new UserForm();
$this->_form->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -28,13 +30,13 @@ class UserService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
return $this->_form;
}
public function getHelper(): UserHelper
{
if ($this->helperInstance === null) {
$this->helperInstance = new UserHelper();
$this->helperInstance->setAttributes([
if ($this->_helper === null) {
$this->_helper = new UserHelper();
$this->_helper->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
@ -42,7 +44,7 @@ class UserService extends CommonService
'class_path' => $this->getClassPaths(false)
]);
}
return $this->helperInstance;
return $this->_helper;
}
//기본 기능부분
protected function create_process(array $formDatas): UserEntity