dbmsv4/app/Libraries/DBMigration/Process/ServerCodeProcess.php
2025-12-23 16:25:24 +09:00

142 lines
4.3 KiB
PHP

<?php
namespace App\Libraries\DBMigration\Process;
use CodeIgniter\Database\BaseConnection;
class ServerCodeProcess implements MigrationProcessInterface
{
private $db;
public function __construct(BaseConnection $db)
{
$this->db = $db;
}
/**
* 서버 하드웨어 사양 정제 도구 (2025 최적화 버전)
*/
/**
* 1. CPU 사양 정제 (모델명 * 수량)
*/
function refineCPU(string $raw): string
{
$raw = strtoupper(trim($raw));
if (empty($raw) || $raw === '0' || $raw === 'NULL') return '0';
// E5504*2 또는 E5504 * 2 형태 처리
if (preg_match('/(.+?)\s*\*\s*(\d+)/', $raw, $matches)) {
return trim($matches[1]) . " * " . $matches[2];
}
// 단일 모델명인 경우 수량 1 추가
return $raw . " * 1";
}
/**
* 2. RAM 및 HDD 사양 정제 (용량G * 수량)
* HDD의 경우 [타입 용량G * 수량] 패턴 및 콤마 구분 적용
*/
function refineSpec(string $raw, bool $isHDD = false): string
{
$raw = trim($raw);
if (empty($raw) || $raw === '0' || strtoupper($raw) === 'NULL') return '0';
// 대문자 변환 및 단위 통일
$raw = strtoupper($raw);
$raw = str_replace(['RAM', 'GB'], ['', 'G'], $raw);
// 패턴 분석 및 데이터 추출
// 1단계: 괄호 안의 상세 사양 우선 추출 (예: 32G(4G*8) -> 4G*8)
if (preg_match('/\(\s*(\d+)\s*G\s*\*\s*(\d+)\s*\)/', $raw, $matches)) {
$raw = $matches[1] . "G * " . $matches[2];
}
// 2단계: 타입별 분리 및 수량 합산
// HDD 타입들 (SSD, SATA, SAS)
$types = ['SSD', 'SATA', 'SAS'];
$resultParts = [];
if ($isHDD) {
foreach ($types as $type) {
// 해당 타입이 포함된 문구만 추출 (예: SSD 128G SSD 128G)
if (str_contains($raw, $type)) {
preg_match_all('/' . $type . '\s*(\d+)\s*G/', $raw, $matches);
if (!empty($matches[1])) {
$counts = array_count_values($matches[1]);
foreach ($counts as $size => $qty) {
$resultParts[] = "{$type} {$size}G * {$qty}";
}
}
}
}
} else {
// RAM 처리 (타입 없이 용량만 추출)
preg_match_all('/(\d+)\s*G/', $raw, $matches);
if (!empty($matches[1])) {
$counts = array_count_values($matches[1]);
foreach ($counts as $size => $qty) {
$resultParts[] = "{$size}G * {$qty}";
}
}
}
// 3단계: 결과 조립 (HDD는 콤마로 구분)
if (empty($resultParts)) {
// 매칭되는 패턴이 없을 경우 기본 숫자 추출 시도
if (preg_match('/(\d+)/', $raw, $m)) return $m[1] . "G * 1";
return $raw;
}
return implode(', ', $resultParts);
}
/**
* 3. [활용 예시] 실 데이터 변환 테스트
*/
// $testData = [
// ['cpu' => 'E5504', 'ram' => '4g 4g', 'hdd' => 'SSD 128G SSD 128G SATA 500G SATA 500G'],
// ['cpu' => 'E5504*2', 'ram' => '32G(4G*8)', 'hdd' => 'SAS 146G SAS 146G SAS 146G SAS 146G'],
// ['cpu' => 'X5570', 'ram' => '16g', 'hdd' => 'SSD 128G * 4'],
// ];
// echo "--- 마이그레이션 데이터 변환 결과 ---\n";
// foreach ($testData as $row) {
// echo "CPU: " . refineCPU($row['cpu']) . "\n";
// echo "RAM: " . refineSpec($row['ram'], false) . "\n";
// echo "HDD: " . refineSpec($row['hdd'], true) . "\n";
// echo "-------------------------------------\n";
// }
/*
[출력 예상 결과]
CPU: E5504 * 1
RAM: 4G * 2
HDD: SSD 128G * 2, SATA 500G * 2
-------------------------------------
CPU: E5504 * 2
RAM: 4G * 8
HDD: SAS 146G * 4
-------------------------------------
CPU: X5570 * 1
RAM: 16G * 1
HDD: SSD 128G * 4
*/
public function process(string $site, array $row, int $cnt): void
{
//server_use_status => y이면 사용중
$temps = [];
$temps['code'] = trim($row['server_code']);
$temps['type'] = SERVER['TYPES']['NORMAL'];
$temps['chassisinfo_uid'] = 1;
$temps['price'] = trim($row['server_use_status']) === 'y' ? 0 : intval($row['server_cost']);
$temps['status'] = trim($row['server_use_status']) === 'y' ? STATUS['AVAILABLE'] : STATUS['OCCUPIED'];
$temps['manufactur_at'] = trim($row['server_release_date']);
if (!$this->db->table('serverinfo')->insert($temps)) {
throw new \Exception($this->db->error()['message']);
}
echo "{$cnt} -> {$temps['code']} SERVERCODE 완료.\n";
}
}