117 lines
4.4 KiB
PHP
117 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\DTOs\TrafficDTO;
|
|
use App\Entities\TrafficEntity;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class TrafficController extends AdminController
|
|
{
|
|
public const PATH = 'traffic';
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
if ($this->service === null) {
|
|
$this->service = service('trafficservice');
|
|
}
|
|
$this->addActionPaths($this::PATH);
|
|
}
|
|
protected function createDTO(array $formDatas): TrafficDTO
|
|
{
|
|
return new TrafficDTO($formDatas);
|
|
}
|
|
protected function action_init_process(string $action): void
|
|
{
|
|
$fields = ['client', 'server', 'switch', 'server_ip', 'interface', 'ip', 'status'];
|
|
$filters = ['status'];
|
|
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($filters);
|
|
parent::action_init_process($action);
|
|
}
|
|
protected function create_process(): TrafficEntity
|
|
{
|
|
return parent::create_process();
|
|
}
|
|
protected function modify_form_process($uid): TrafficEntity
|
|
{
|
|
return parent::modify_form_process($uid);
|
|
}
|
|
protected function modify_process($uid): TrafficEntity
|
|
{
|
|
return parent::modify_process($uid);
|
|
}
|
|
protected function delete_process($uid): TrafficEntity
|
|
{
|
|
return parent::delete_process($uid);
|
|
}
|
|
protected function view_process($uid): TrafficEntity
|
|
{
|
|
return parent::view_process($uid);
|
|
}
|
|
public function dashboard(): string
|
|
{
|
|
$this->action_init_process(__FUNCTION__);
|
|
return $this->action_render_process($this->getActionPaths(), __FUNCTION__, $this->getViewDatas());
|
|
}
|
|
/**
|
|
* AJAX 요청을 처리하고, 모델에서 집계된 데이터를 JSON 형식으로 반환합니다.
|
|
*/
|
|
public function getAggregatedData(): ResponseInterface
|
|
{
|
|
$uid = $this->request->getPost('uid');
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof TrafficEntity) {
|
|
throw new \Exception(__METHOD__ . "에서 오류발생:{$uid}에 해당하는 트래픽정보를 찾을수없습니다.");
|
|
}
|
|
$startDate = $this->request->getPost('startDate');
|
|
$endDate = $this->request->getPost('endDate');
|
|
// 입력값 검증 (실제 프로덕션에서는 반드시 수행해야 함)
|
|
if (empty($startDate) || empty($endDate)) {
|
|
return $this->response->setStatusCode(400)->setJSON([
|
|
'status' => 'error',
|
|
'message' => '필수 파라미터(날짜 또는 집계 기준)가 누락되었습니다.',
|
|
]);
|
|
}
|
|
try {
|
|
// 모델 로드 및 데이터 조회
|
|
$collectorService = service('collectorservice');
|
|
$data = $collectorService->getAggregated($entity, $startDate, $endDate);
|
|
// 데이터를 JSON 형식으로 반환
|
|
return $this->response->setJSON([
|
|
'status' => 'success',
|
|
'data' => $data // 이 데이터는 이제 사용자가 제공한 필드 구조를 가집니다.
|
|
]);
|
|
} catch (\Exception $e) {
|
|
// 예외 처리
|
|
log_message('error', 'Traffic data aggregation error: ' . $e->getMessage());
|
|
return $this->response->setStatusCode(500)->setJSON([
|
|
'status' => 'error',
|
|
'message' => '데이터 처리 중 서버 오류가 발생했습니다.',
|
|
]);
|
|
}
|
|
}
|
|
}
|