266 lines
12 KiB
PHP
266 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Entities\TrafficEntity;
|
|
use CodeIgniter\HTTP\DownloadResponse;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\Validation\Exceptions\ValidationException;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class TrafficController extends AdminController
|
|
{
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
if ($this->service === null) {
|
|
$this->service = service('trafficservice');
|
|
}
|
|
}
|
|
protected function action_init_process(string $action): void
|
|
{
|
|
$fields = ['client', 'server', 'switch', 'server_ip', 'interface', 'ip', 'status'];
|
|
$filters = ['status'];
|
|
$batchjobFilters = $filters;
|
|
$actionButtons = ['view' => ICONS['SEARCH'], 'dashboard' => ICONS['CHART']];
|
|
switch ($action) {
|
|
case 'create':
|
|
case 'create_form':
|
|
case 'modify':
|
|
case 'modify_form':
|
|
break;
|
|
case 'view':
|
|
$fields = [...$fields, 'created_at'];
|
|
break;
|
|
case 'index':
|
|
case 'download':
|
|
$fields = [...$fields, 'created_at'];
|
|
break;
|
|
default:
|
|
throw new \Exception("[{$action}] 지원하지 않는 action입니다.");
|
|
// break;
|
|
}
|
|
$this->service->getFormService()->setFormFields($fields);
|
|
$this->service->getFormService()->setFormRules($action, $fields);
|
|
$this->service->getFormService()->setFormFilters($filters);
|
|
$this->service->getFormService()->setFormOptions($filters);
|
|
$this->service->getFormService()->setBatchjobFilters($batchjobFilters);
|
|
$this->service->getFormService()->setActionButtons($actionButtons);
|
|
parent::action_init_process($action);
|
|
}
|
|
public function create_form(): string|RedirectResponse
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$this->addViewDatas('formDatas', $this->create_form_process());
|
|
return $this->create_form_result_process($action);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성폼 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function create(): string|RedirectResponse
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$entity = $this->create_process();
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 TrafficEntity만 가능");
|
|
}
|
|
return $this->create_result_process($entity);
|
|
} catch (ValidationException $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 검증오류:" . $e->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 생성 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function modify_form($uid): string|RedirectResponse
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$entity = $this->modify_form_process($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 TrafficEntity만 가능");
|
|
}
|
|
$this->addViewDatas('entity', $entity);
|
|
return $this->modify_form_result_process($action);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정폼 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function modify($uid): string|RedirectResponse
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$entity = $this->modify_process($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 TrafficEntity만 가능");
|
|
}
|
|
$this->addViewDatas('entity', $entity);
|
|
return $this->modify_result_process($entity);
|
|
} catch (ValidationException $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage());
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function delete($uid): RedirectResponse
|
|
{
|
|
try {
|
|
$entity = $this->delete_process($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 TrafficEntity만 가능");
|
|
}
|
|
return $this->delete_result_process($entity);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function view($uid): string|RedirectResponse
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$entity = $this->view_process($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \RuntimeException(__METHOD__ . "에서 오류발생:Return Type은 TrafficEntity만 가능");
|
|
}
|
|
$this->addViewDatas('entity', $entity);
|
|
return $this->view_result_process($action);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 {$uid} 상세보기 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
public function batchjob(): string|RedirectResponse
|
|
{
|
|
try {
|
|
// 사전작업 및 데이터 추출 초기화
|
|
list($uids, $selectedFields, $formDatas) = $this->batchjob_pre_process();
|
|
$this->service->getFormService()->setFormFields($selectedFields);
|
|
$this->service->getFormService()->setFormRules(__FUNCTION__, $selectedFields);
|
|
$this->service->getFormService()->setFormFilters($selectedFields);
|
|
$this->service->getFormService()->setFormOptions($selectedFields);
|
|
$entities = [];
|
|
$errors = [];
|
|
foreach ($uids as $uid) {
|
|
try {
|
|
$entities[] = $this->batchjob_process($uid, $formDatas);
|
|
} catch (ValidationException $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 수정 검증오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
} catch (\Exception $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 수정 오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
}
|
|
}
|
|
return $this->batchjob_result_process($uids, $entities, $errors);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 일괄작업처리 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function batchjob_delete(): string|RedirectResponse
|
|
{
|
|
try {
|
|
$uids = $this->batchjob_delete_pre_process();
|
|
$entities = [];
|
|
$errors = [];
|
|
foreach ($uids as $uid) {
|
|
try {
|
|
$entities[] = $this->batchjob_delete_process($uid);
|
|
} catch (\Exception $e) {
|
|
log_message('error', "{$this->getTitle()}에서 {$uid} 삭제 오류:" . $e->getMessage());
|
|
$errors[] = $e->getMessage();
|
|
}
|
|
}
|
|
return $this->batchjob_delete_result_process($uids, $entities, $errors);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 일괄삭제 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function index(): string|ResponseInterface
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
$this->index_process($action);
|
|
$this->index_result_process($action);
|
|
} catch (\Exception $e) {
|
|
session()->setFlashdata('message', $e->getMessage());
|
|
}
|
|
return $this->index_result_process($action);
|
|
}
|
|
|
|
public function download(string $output_type, mixed $uid = false): DownloadResponse|RedirectResponse|string
|
|
{
|
|
try {
|
|
$action = __FUNCTION__;
|
|
$this->action_init_process($action);
|
|
return $this->download_process($action, $output_type, $uid);
|
|
} catch (\Exception $e) {
|
|
return $this->action_redirect_process('error', "{$this->getTitle()}에서 오류:" . $e->getMessage());
|
|
}
|
|
}
|
|
//기본 함수 작업
|
|
|
|
//Custom 추가 함수
|
|
public function dashboard($uid): string
|
|
{
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \Exception(__METHOD__ . "에서 오류발생:{$uid}에 해당하는 트래픽정보를 찾을수없습니다.");
|
|
}
|
|
return $this->action_render_process(
|
|
$this->getActionPaths(),
|
|
'traffic' . DIRECTORY_SEPARATOR . __FUNCTION__,
|
|
['entity' => $entity]
|
|
);
|
|
}
|
|
/**
|
|
* AJAX 요청을 처리하고, 모델에서 집계된 데이터를 JSON 형식으로 반환합니다.
|
|
*/
|
|
public function getAggregatedData($uid): ResponseInterface
|
|
{
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \Exception(__METHOD__ . "에서 오류발생:{$uid}에 해당하는 트래픽정보를 찾을수없습니다.");
|
|
}
|
|
$startDate = $this->request->getVar('startDate');
|
|
$endDate = $this->request->getVar('endDate');
|
|
// 입력값 검증 (실제 프로덕션에서는 반드시 수행해야 함)
|
|
if (empty($startDate) || empty($endDate)) {
|
|
return $this->response->setStatusCode(400)->setJSON([
|
|
'status' => 'error',
|
|
'message' => '필수 파라미터(날짜 또는 집계 기준)가 누락되었습니다.',
|
|
]);
|
|
}
|
|
try {
|
|
// 모델 로드 및 데이터 조회
|
|
$collectorService = service('collectorservice');
|
|
//기간의 시작 시점 (YYYY-MM-DD 00:00:00) 설정~기간의 종료 시점 (YYYY-MM-DD 23:59:59) 설정
|
|
$start_timestamp = $startDate . ' 00:00:00';
|
|
$end_timestamp = $endDate . ' 23:59:59';
|
|
$aggregateDatas = $collectorService->getAggregateDatas($entity, $start_timestamp, $end_timestamp);
|
|
// 데이터를 JSON 형식으로 반환
|
|
return $this->response->setJSON([
|
|
'message' => sprintf("[%s~%s] %s 트랙픽차트 ", $startDate, $endDate, $entity->getCustomTitle()),
|
|
'status' => 'success',
|
|
'data' => $aggregateDatas // 이 데이터는 이제 사용자가 제공한 필드 구조를 가집니다.
|
|
]);
|
|
} catch (\Exception $e) {
|
|
// 예외 처리
|
|
log_message('error', 'Traffic data aggregation error: ' . $e->getMessage());
|
|
return $this->response->setStatusCode(500)->setJSON([
|
|
'status' => 'error',
|
|
'message' => '데이터 처리 중 서버 오류가 발생했습니다.',
|
|
]);
|
|
}
|
|
}
|
|
}
|