chore: .gitignore에 Python 캐시 및 가상환경 무시 규칙 추가

This commit is contained in:
windpacer
2026-05-03 03:58:23 +09:00
parent f71ec310e4
commit a0404b1fee
254 changed files with 33 additions and 5083599 deletions

View File

@@ -1,12 +0,0 @@
{
"mcpServers": {
"iiot-rag": {
"command": "/home/windpacer/projects/ExperionCrawler/mcp-server/.venv/bin/python",
"args": [
"/home/windpacer/projects/ExperionCrawler/mcp-server/server.py"
],
"env": {},
"description": "ExperionCrawler RAG — Qdrant(코드베이스+OPC UA 문서) + GLM-4.7-Flash"
}
}
}

33
.gitignore vendored
View File

@@ -25,3 +25,36 @@ data/
# Temporary files # Temporary files
*.tmp *.tmp
*.log *.log
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual Environment
.venv/
venv/
ENV/
env/
# IDE
.pytest_cache/
.mypy_cache/
.ipynb_checkpoints/

61
.roo.md
View File

@@ -1,61 +0,0 @@
# 🎯 PROJECT CONTEXT
- **이름**: ExperionCrawler
- **스택**: .NET 8 (C#), PostgreSQL (TimescaleDB), OPC UA
- **아키텍처**: Clean Architecture (`src/Core`, `src/Infrastructure`, `src/Web`)
- **주요 DB**:
- 도커 컨테이너`iiot-timescaledb` 의 (localhost:5432/iiot_platform 테이블): 시계열 저장, Text-to-SQL
# 📋 MANDATORY WORKFLOW (최우선 준수)
1. **시작 전 상태 파악**: 프로젝트 루트의 `todo.md`를 먼저 읽고 이력/미완료 작업 확인
2. **Todo List 생성**: 복잡도 ≥2 단계인 작업은 반드시 더 작은 단위로 `todo list` 를 만들것
3. 'todo list'를 완료시 까지, 작은 단위 작업 완료시에는 다음 작은 단위 작업은 반드시 새 작업으로 작업할 것
4. **수정전 백업**: 파일을 수정할 시에는 반드시 파일명에 현재 날짜와 시간을 붙여서 /.rooBackup 폴더에 복사후 수정
5. **안전한 파일 수정**: `apply_diff` 사용 전 무조건 `read_file`로 현재 내용 검증. 정확한 검색/교체 블록만 사용
6. **단계 완료 처리**: 각 Todo 항목 완료 시 즉시 `completed` 표시
# 🔄 CONTEXT MANAGEMENT & TASK MIGRATION (핵심 규칙)
## 1. 이관 트리거 (하단 조건 중 하나라도 충족 시 즉시 중단)
- 논리적 모듈/기능 단위 완료
- 자가 평가 기준: 컨텍스트 누적 부하가 약 70% 이상으로 판단될 때
## 이관 실행 프로토콜
- 현재 상태를 압축 요약하고 **반드시 아래 형식으로 응답을 종료**
- 이관 신호 출력 후 추가 코딩/분석/설명 절대 금지.
## 3. 이관 직전 필수 저장 항목
이관 신호 출력 전 반드시:
1. `task_state.md` 최신화 (미완료 파일 목록, 발견된 문제 전체)
2. 다음 작업자(새 컨텍스트)를 위한 첫 문장 명시:
> "task_state.md를 읽고 [미완료 파일명]부터 이어서 분석하세요"
3. 이관 후 첫 응답에서 task_state.md 확인 없이 작업 시작 금지
# 🧠 LARGE TASK ANTI-CORRUPTION RULES
## 대규모 작업 (파일 5개 이상 분석/수정) 필수 규칙
### 반드시 외부 파일을 진행 상태 저장소로 사용
- 분석/수정 작업 시작 즉시 `task_state.md` 생성
- 각 파일 처리 완료마다 즉시 결과를 `task_state.md`에 기록
- 컨텍스트 압축/이관 발생 시 **첫 번째 행동은 `task_state.md` 읽기**
- 기억(컨텍스트)을 절대 진실 소스로 사용 금지
### task_state.md 형식
```
## 작업명:
## 시작시각:
## 전체 대상: [파일 목록]
### 완료된 파일
- [x] src/Core/xxx.cs → 문제없음
- [x] src/Infrastructure/yyy.cs → HIGH: DB연결 미해제
### 미완료 파일
- [ ] src/Web/zzz.cs
### 발견된 문제 누적
| 파일 | 심각도 | 내용 |
|------|--------|------|
```
# 🚫 COMMAND LOOP PREVENTION
- 명령 실행 후 결과가 이전과 동일하면 → 재시도 금지, 원인 분석 먼저
- --no-build 옵션은 빌드 완료 확인 후에만 사용
- 테스트 0개 실행 시 → 테스트 프로젝트/필터 조건 재확인, 재실행 금지

View File

@@ -1,3 +0,0 @@
{
"mcpServers": {}
}

View File

@@ -1,101 +0,0 @@
# GLM-4.7-Flash 코드 작업 규칙 (ExperionCrawler)
## 필수 준수 사항
### 작업 전
- 반드시 `task_state.md`를 먼저 읽어 진행 상태 파악
- 파일 수정 전 반드시 `read_file`로 전체 내용 확인 후 수정
### 코드 수정 원칙
- 요청된 범위만 수정 — 관련 없는 코드 리팩토링 금지
- 빌드 검증: 각 파일 수정 후 `dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q` 실행
- 빌드 실패 시 즉시 원인 수정 후 재빌드, 다음 항목으로 넘어가지 않음
### issues.md 작성 형식
```
| # | 파일 | 심각도 | 분류 | 내용 | 상태 |
|---|------|--------|------|------|------|
| 1 | src/... | HIGH/MED/LOW | bug/perf/quality/security | 설명 | pending/fixed |
```
심각도 기준:
- HIGH: 런타임 예외, 데이터 손실, 보안 취약점
- MED: 성능 저하, 잘못된 동작, 예외 미처리
- LOW: 코드 품질, 불필요한 코드, 명명 불일치
### 클로드 검수를 위한 커밋 규칙
- 각 이슈 수정 완료 시: `git add -p` 후 이슈 번호 포함 커밋
예: `fix(#3): ExperionDbContext null 참조 예외 방어 처리`
- 전체 완료 후 `REVIEW_REQUEST.md` 생성하여 검수 요청
### MCP 도구 활용
- `search_codebase`: 관련 코드 패턴 검색에 적극 활용
- `ask_iiot_llm`: IIoT/OPC UA 도메인 판단이 필요할 때 사용
---
## [CRITICAL] ASP.NET Core 컨트롤러 JSON 직렬화 규칙
### 배경 (반드시 숙지)
`src/Web/Program.cs`에 다음 설정이 있다:
```csharp
opt.JsonSerializerOptions.PropertyNamingPolicy = null; // PascalCase 그대로 직렬화
```
이로 인해 C# 속성명이 **그대로** JSON 키가 된다.
프론트엔드(`app.js`)는 **모든 JSON 필드를 camelCase**로 접근하므로,
PascalCase 키가 오면 **모든 값이 `undefined`** 가 된다.
### 금지 패턴 (절대 사용 금지)
```csharp
// ❌ shorthand 익명 객체 — C# 속성명(PascalCase)이 JSON 키로 그대로 사용됨
return Ok(new { x.Id, x.TagName, x.NodeId, x.LiveValue });
// ❌ typed 객체를 Ok()에 직접 전달 — PascalCase 직렬화됨
return Ok(result);
return Ok(new MyDto { Id = 1, TagName = "abc" });
```
### 올바른 패턴 (항상 명시적 camelCase 매핑)
```csharp
// ✅ 항상 명시적으로 소문자 키 지정
return Ok(new
{
id = x.Id,
tagName = x.TagName,
nodeId = x.NodeId,
liveValue = x.LiveValue,
timestamp = x.Timestamp
});
// ✅ 컬렉션 포함 응답
return Ok(new
{
total = r.Total,
items = r.Items.Select(x => new
{
id = x.Id,
tagName = x.TagName,
nodeId = x.NodeId
})
});
```
### C# 예약어 처리
`class`는 C# 예약어이므로 `@class`를 사용한다. System.Text.Json이 `"class"`로 정상 직렬화한다:
```csharp
// ✅ @class → JSON "class"
return Ok(new { id = x.Id, @class = x.Class, name = x.Name });
```
### 점검 체크리스트
컨트롤러에서 `Ok(...)` 또는 `return` 사용 시:
- [ ] 익명 객체의 모든 키가 소문자(camelCase)인가?
- [ ] `new { x.SomeProp }` 형태(shorthand)가 없는가?
- [ ] typed record/class를 그대로 반환하지 않는가?
- [ ] C# 예약어(`class`, `string` 등)에 `@` 접두사를 붙였는가?

View File

@@ -1,97 +0,0 @@
# roo-rules.md
Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.
## 1. Think Before Coding
**Don't assume. Don't hide confusion. Surface tradeoffs.**
Before implementing:
- State your assumptions explicitly. If uncertain, ask.
- If multiple interpretations exist, present them - don't pick silently.
- If a simpler approach exists, say so. Push back when warranted.
- If something is unclear, stop. Name what's confusing. Ask.
## 2. Simplicity First
**Minimum code that solves the problem. Nothing speculative.**
- No features beyond what was asked.
- No abstractions for single-use code.
- No "flexibility" or "configurability" that wasn't requested.
- No error handling for impossible scenarios.
- If you write 200 lines and it could be 50, rewrite it.
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
## 3. Surgical Changes
**Touch only what you must. Clean up only your own mess.**
When editing existing code:
- Don't "improve" adjacent code, comments, or formatting.
- Don't refactor things that aren't broken.
- Match existing style, even if you'd do it differently.
- If you notice unrelated dead code, mention it - don't delete it.
When your changes create orphans:
- Remove imports/variables/functions that YOUR changes made unused.
- Don't remove pre-existing dead code unless asked.
The test: Every changed line should trace directly to the user's request.
## 4. Goal-Driven Execution
**Define success criteria. Loop until verified.**
Transform tasks into verifiable goals:
- "Add validation" → "Write tests for invalid inputs, then make them pass"
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
- "Refactor X" → "Ensure tests pass before and after"
For multi-step tasks, state a brief plan:
```
1. [Step] → verify: [check]
2. [Step] → verify: [check]
3. [Step] → verify: [check]
```
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
## 5. Save the token & time - Roo code must keep this rule not API
- "Do not summarize the code or changes after completing a task"
- "Once the code is written, do not repeat the explanation"
- "Only output the final file content if necessary"
## 6. Backup + Diff Before Edit
**기존 파일을 수정하기 전에 반드시 다음 두 단계를 수행할 것.**
### Step 1 — 백업
수정 대상 파일을 `.rooBackup/` 폴더에 현재날짜와 시간으로 폴더를 만들고 그 폴더에 수정전 원본 그대로 저장한다.
- 저장 경로: `.rooBackup/<날짜-시간>/<원본경로>/<파일명>`
- 예: `src/Web/wwwroot/js/app.js``.rooBackup/src/Web/wwwroot/js/app.js`
- 백업 후 "백업 완료: `.rooBackup/...`" 를 출력할 것
### Step 2 — Diff 제시
변경 내용을 diff 형식으로 보여주고, 사용자 확인 후 실제 수정 진행.
```diff
- 기존 코드
+ 변경된 코드
```
변경 이유를 한 줄로 함께 설명할 것.
### 예외 (백업/diff 생략 가능)
- 신규 파일 생성
- 공백/포맷팅만 바뀌는 경우
**위반 사례 (금지):** 백업·diff 없이 바로 파일을 덮어쓰는 것 — roo가 이전에 fastRecord 섹션 전체를 날린 것이 이 케이스에 해당.
---
**These guidelines are working if:** fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.

View File

@@ -1,103 +0,0 @@
using System.Diagnostics;
namespace ExperionCrawler.Infrastructure.Mcp;
public class McpServerHostedService : IHostedService
{
private readonly McpClient _mcpClient;
private readonly ILogger<McpServerHostedService> _logger;
private readonly string _workingDirectory;
private Process? _process;
public McpServerHostedService(
McpClient mcpClient,
ILogger<McpServerHostedService> logger,
IConfiguration config)
{
_mcpClient = mcpClient;
_logger = logger;
var dir = config["McpServer:WorkingDirectory"] ?? "../../mcp-server";
_workingDirectory = Path.IsPathRooted(dir)
? dir
: Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), dir));
}
public async Task StartAsync(CancellationToken cancellationToken)
{
// 이미 외부에서 실행 중이면 새 프로세스 띄우지 않음
if (await _mcpClient.PingAsync())
{
_logger.LogInformation("[McpServer] 이미 실행 중 (localhost:5001) — 기존 프로세스 사용");
return;
}
if (!Directory.Exists(_workingDirectory))
{
_logger.LogWarning("[McpServer] 디렉터리 없음: {Dir} — MCP 서버 시작 스킵", _workingDirectory);
return;
}
_logger.LogInformation("[McpServer] Python MCP 서버 시작 중... ({Dir})", _workingDirectory);
_process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "uv",
Arguments = "run server.py --http",
WorkingDirectory = _workingDirectory,
UseShellExecute = false,
}
};
try
{
_process.Start();
}
catch (Exception ex)
{
_logger.LogError(ex, "[McpServer] 프로세스 시작 실패 (uv 설치 여부 확인)");
return;
}
// 최대 30초 대기 (1초 간격 health check)
for (int i = 0; i < 30; i++)
{
try { await Task.Delay(1000, cancellationToken); } catch { return; }
if (_process.HasExited)
{
_logger.LogWarning("[McpServer] 프로세스가 예기치 않게 종료됨 (exit code: {Code})", _process.ExitCode);
return;
}
if (await _mcpClient.PingAsync())
{
_logger.LogInformation("[McpServer] 준비 완료 (localhost:5001, {Sec}초 소요)", i + 1);
return;
}
}
_logger.LogWarning("[McpServer] 30초 내 응답 없음 — 백그라운드에서 계속 기다림");
}
public Task StopAsync(CancellationToken cancellationToken)
{
try
{
if (_process is { HasExited: false })
{
_process.Kill(entireProcessTree: true);
_process.WaitForExit(3000);
_logger.LogInformation("[McpServer] Python MCP 서버 종료됨");
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "[McpServer] 종료 중 오류");
}
finally
{
_process?.Dispose();
_process = null;
}
return Task.CompletedTask;
}
}

View File

@@ -1,137 +0,0 @@
# 🛠️ Graph Pipeline Phase 1: 기하학적 데이터 추출 (Geometric Extraction)
이 문서는 P&ID Graph Pipeline의 첫 번째 단계인 **기하학적 데이터 추출**의 상세 구현 계획을 다룹니다. 목표는 단순한 텍스트 추출을 넘어, 도면 내 모든 객체의 **물리적 위치(좌표)**와 **기하학적 속성**을 보존하여 이후 위상 모델링(Topology Modeling)이 가능하도록 하는 것입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `ezdxf` | DXF 파일 파싱 및 엔티티 추출 | 핵심 라이브러리 |
| `shapely` | 기하학적 연산 (Intersection, Distance, Bounding Box) | 좌표 기반 분석 필수 |
| `numpy` | 대량의 좌표 데이터 계산 및 행렬 연산 | 성능 최적화 |
| `pandas` | 추출된 객체 데이터의 구조화 및 CSV/JSON 저장 | 데이터 관리 |
| `pydantic` | 추출 데이터의 스키마 정의 및 유효성 검증 | 데이터 무결성 보장 |
| `pytesseract` / `pdf2image` | PDF 도면의 영역 기반 OCR 추출 | PDF 처리 시 필요 |
### 1.2 설치 명령어
```bash
pip install ezdxf shapely numpy pandas pydantic pytesseract pdf2image
```
---
## 📐 2. 상세 설계 구조
### 2.1 데이터 모델 (Schema)
모든 추출 객체는 다음과 같은 공통 속성을 갖는 `GeometricEntity` 모델을 따릅니다.
```python
from pydantic import BaseModel
from typing import List, Optional, Union, Tuple
class BoundingBox(BaseModel):
min_x: float
min_y: float
max_x: float
max_y: float
center: Tuple[float, float]
class GeometricEntity(BaseModel):
entity_id: str
entity_type: str # TEXT, LINE, CIRCLE, POLYLINE, ARC
layer: str
bbox: BoundingBox
properties: dict # 텍스트 값, 색상, 선 굵기 등
coordinates: List[Tuple[float, float]] # 시작점, 끝점 또는 정점 리스트
```
### 2.2 처리 파이프라인 흐름
1. **DXF Load:** `ezdxf.readfile()`을 통해 도면 로드.
2. **Entity Iteration:** 모든 레이어의 엔티티를 순회하며 타입별 분류.
3. **Coordinate Extraction:**
* `TEXT`: 삽입점(Insertion Point) 및 텍스트 길이를 이용한 BBox 계산.
* `LINE`: 시작점(Start)과 끝점(End) 추출.
* `POLYLINE`: 모든 정점(Vertices) 리스트 추출.
* `CIRCLE/ARC`: 중심점(Center)과 반지름(Radius) 추출.
4. **Spatial Normalization:** 도면 좌표계를 분석 시스템 좌표계로 정규화.
5. **Structured Export:** JSON 또는 DB(PostgreSQL/PostGIS)에 저장.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 DXF 기하학적 추출 핵심 코드
```python
import ezdxf
from shapely.geometry import box, LineString, Point
from typing import List
class PidGeometricExtractor:
def __init__(self, file_path: str):
self.doc = ezdxf.readfile(file_path)
self.msp = self.doc.modelspace()
def get_bbox(self, entity):
"""엔티티의 Bounding Box를 계산하여 shapely box 객체로 반환"""
if entity.dxftype() == 'TEXT':
# 텍스트의 경우 삽입점과 텍스트 길이를 기반으로 단순화된 BBox 생성
p = entity.dxf.insert
return box(p.x, p.y, p.x + 10, p.y + 5) # 실제로는 폰트 크기 반영 필요
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
return box(min(start.x, end.x), min(start.y, end.y),
max(start.x, end.x), max(start.y, end.y))
# ... 기타 타입 구현
return None
def extract_all(self) -> List[dict]:
results = []
for entity in self.msp:
bbox_obj = self.get_bbox(entity)
if bbox_obj:
results.append({
"id": entity.dxf.handle,
"type": entity.dxftype(),
"layer": entity.dxf.layer,
"bbox": {
"min_x": bbox_obj.bounds[0],
"min_y": bbox_obj.bounds[1],
"max_x": bbox_obj.bounds[2],
"max_y": bbox_obj.bounds[3]
},
"value": getattr(entity.dxf, 'text', None)
})
return results
# 사용 예시
extractor = PidGeometricExtractor("plant_drawing.dxf")
geometric_data = extractor.extract_all()
```
### 3.2 유틸리티 함수: 인접성 체크 (Proximity Utility)
추후 2단계(위상 모델링)에서 사용할 핵심 유틸리티입니다.
```python
from shapely.geometry import Point
def is_near(entity_a_bbox, entity_b_bbox, threshold=5.0):
"""두 객체의 Bounding Box 간의 최단 거리가 임계값 이내인지 확인"""
return entity_a_bbox.distance(entity_b_bbox) <= threshold
def is_inside(point, bbox):
"""특정 점이 Bounding Box 내부에 있는지 확인"""
return bbox.contains(Point(point))
```
---
## 🚀 4. Phase 1 완료 기준 (Definition of Done)
- [ ] DXF 파일 내 모든 `TEXT`, `LINE`, `POLYLINE`의 좌표 데이터가 누락 없이 추출되는가?
- [ ] 각 객체별로 정확한 `Bounding Box`가 계산되어 저장되는가?
- [ ] 추출된 데이터가 `GeometricEntity` 스키마에 맞게 JSON 형태로 저장되는가?
- [ ] (선택 사항) PDF 도면의 경우 OCR을 통해 텍스트의 좌표값이 추출되는가?

View File

@@ -1,126 +0,0 @@
# 🕸️ Graph Pipeline Phase 2: 위상 모델링 (Topology Modeling)
이 문서는 P&ID Graph Pipeline의 두 번째 단계인 **위상 모델링**의 상세 구현 계획을 다룹니다. 1단계에서 추출한 기하학적 객체(좌표, BBox)를 기반으로, 설비 간의 **연결성(Connectivity)**과 **흐름(Flow)**을 정의하는 지식 그래프(Knowledge Graph)를 구축하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `networkx` | 그래프 데이터 구조 생성 및 알고리즘 분석 | 핵심 라이브러리 |
| `shapely` | 객체 간 거리 계산 및 포함 관계 분석 | 1단계와 연계 |
| `scikit-learn` | (선택) KD-Tree를 이용한 고속 근접 이웃 검색 | 대규모 도면 최적화 |
| `matplotlib` | 생성된 그래프의 위상 구조 시각화 검증 | 디버깅용 |
### 1.2 설치 명령어
```bash
pip install networkx shapely scikit-learn matplotlib
```
---
## 📐 2. 상세 설계 구조
### 2.1 그래프 정의 (Graph Definition)
* **노드 (Nodes):**
* `Equipment`: 펌프, 탱크, 열교환기 등 (속성: ID, 타입, BBox)
* `Instrument`: 전송기, 밸브, 게이지 등 (속성: ID, 타입, BBox)
* `Tag`: 텍스트 기반 태그 (속성: TagName, Value)
* **엣지 (Edges):**
* `Pipe`: 설비-설비, 설비-계기 간의 물리적 연결 (속성: LineNumber, 방향성)
* `Association`: 태그-설비 간의 논리적 연결 (속성: 관계 타입 - 예: 'belongs_to')
### 2.2 위상 추론 로직 (Topology Inference)
1. **태그-설비 결합 (Tag-to-Entity Binding):**
* 태그 텍스트의 BBox와 가장 가까운 심볼(Equipment/Instrument)을 찾아 `Association` 엣지를 생성합니다.
2. **배관 연결성 분석 (Line Connectivity):**
* `LINE` 또는 `POLYLINE`의 끝점이 특정 설비의 BBox 내부에 있거나 임계 거리($\epsilon$) 이내에 있으면 두 노드를 `Pipe` 엣지로 연결합니다.
3. **흐름 방향성 부여 (Flow Direction):**
* 화살표 심볼의 방향 또는 공정 흐름 규칙을 분석하여 엣지에 `source` $\rightarrow$ `target` 방향을 설정합니다.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 그래프 구축 핵심 코드
```python
import networkx as nx
from shapely.geometry import box, Point
class PidTopologyBuilder:
def __init__(self, geometric_data):
self.data = geometric_data # Phase 1에서 추출된 JSON 데이터
self.G = nx.DiGraph() # 방향성 그래프 생성
def build_graph(self):
# 1. 모든 객체를 노드로 추가
for item in self.data:
self.G.add_node(item['id'],
type=item['type'],
bbox=box(*item['bbox'].values()),
value=item.get('value'))
# 2. 태그-설비 논리적 연결 (Association)
tags = [n for n, d in self.G.nodes(data=True) if d['type'] == 'TEXT']
equipments = [n for n, d in self.G.nodes(data=True) if d['type'] != 'TEXT']
for tag in tags:
best_match = self._find_nearest_equipment(tag, equipments)
if best_match:
self.G.add_edge(tag, best_match, relation='associated_with')
# 3. 배관 기반 물리적 연결 (Pipe)
lines = [n for n, d in self.G.nodes(data=True) if d['type'] in ['LINE', 'POLYLINE']]
for line in lines:
connected_nodes = self._find_connected_nodes(line, equipments)
if len(connected_nodes) >= 2:
# 라인을 통해 연결된 두 설비 간 엣지 생성
self.G.add_edge(connected_nodes[0], connected_nodes[1], relation='pipe')
def _find_nearest_equipment(self, tag_id, equipment_ids):
tag_bbox = self.G.nodes[tag_id]['bbox']
min_dist = float('inf')
nearest = None
for eq_id in equipment_ids:
eq_bbox = self.G.nodes[eq_id]['bbox']
dist = tag_bbox.distance(eq_bbox)
if dist < min_dist:
min_dist = dist
nearest = eq_id
return nearest if min_dist < 50.0 else None # 임계값 50.0
def _find_connected_nodes(self, line_id, equipment_ids):
# 라인의 시작/끝점이 어떤 설비 BBox에 포함되는지 확인
# (실제 구현 시 line의 coordinates 활용)
return [eq for eq in equipment_ids if self.G.nodes[eq]['bbox'].intersects(self.G.nodes[line_id]['bbox'])]
# 실행
builder = PidTopologyBuilder(geometric_data)
builder.build_graph()
graph = builder.G
```
### 3.2 위상 분석 유틸리티: 영향도 분석 (Impact Analysis)
```python
def analyze_impact(graph, start_node):
"""특정 설비 장애 시 하류(Downstream)에 영향을 받는 모든 노드 추출"""
# BFS를 통해 도달 가능한 모든 노드 탐색
impacted_nodes = nx.descendants(graph, start_node)
return list(impacted_nodes)
# 예: P-101 펌프 고장 시 영향 분석
affected = analyze_impact(graph, "node_P101")
print(f"Impacted Equipment: {affected}")
```
---
## 🚀 4. Phase 2 완료 기준 (Definition of Done)
- [ ] 모든 설비와 계기가 그래프의 **노드(Node)**로 변환되었는가?
- [ ] 태그와 설비 간의 **논리적 연결(Association)**이 정확하게 매핑되었는가?
- [ ] 배관(Line)을 통해 설비 간의 **물리적 연결(Pipe Edge)**이 생성되었는가?
- [ ] `nx.descendants` 등을 통해 특정 노드로부터의 **흐름 추적(Flow Tracing)**이 가능한가?
- [ ] 생성된 그래프 구조가 JSON(GraphML 등) 형태로 저장되어 Phase 3로 전달 가능한가?

View File

@@ -1,125 +0,0 @@
# 🧠 Graph Pipeline Phase 3: 지능형 매핑 및 검증 (Intelligent Mapping & Validation)
이 문서는 P&ID Graph Pipeline의 세 번째 단계인 **지능형 매핑 및 검증**의 상세 구현 계획을 다룹니다. 2단계에서 구축한 위상 그래프(Topology Graph)를 활용하여, 도면 상의 가상 노드들을 실제 Experion 시스템의 **실시간 태그(Real-time Tags)**와 정밀하게 연결하고 그 타당성을 검증하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `openai` / `langchain` | LLM API 연동 및 프롬프트 체이닝 | 매핑 추론 및 검증 핵심 |
| `fuzzywuzzy` / `rapidfuzz` | 태그 이름 간의 문자열 유사도 계산 | 1차 후보군 추출용 |
| `networkx` | 그래프 기반 인접 노드(Context) 추출 | 2단계 그래프 활용 |
| `pydantic` | 매핑 결과의 구조화 및 유효성 검사 | 데이터 정규화 |
| `requests` | ExperionCrawler API (C#)와 통신 | 실제 태그 리스트 조회 |
### 1.2 설치 명령어
```bash
pip install openai langchain rapidfuzz networkx pydantic requests
```
---
## 📐 2. 상세 설계 구조
### 2.1 매핑 파이프라인 (Mapping Pipeline)
단순 이름 매칭의 한계를 극복하기 위해 **[후보 추출 $\rightarrow$ 맥락 분석 $\rightarrow$ LLM 확정]**의 3단계 프로세스를 거칩니다.
1. **1차 후보 추출 (Candidate Generation):**
* 도면의 태그 텍스트와 Experion 시스템의 전체 태그 리스트를 `RapidFuzz`로 비교하여 유사도 상위 N개를 추출합니다.
2. **맥락 정보 수집 (Context Gathering):**
* 해당 노드의 그래프 상 인접 노드(1-hop, 2-hop) 정보를 수집합니다.
* 예: "현재 노드는 `PT-101`이며, 상류에 `P-101(Pump)`이 있고 하류에 `V-101(Valve)`이 있음."
3. **LLM 기반 최종 매핑 (LLM-based Resolution):**
* 후보 태그 리스트와 위상 맥락을 LLM에게 전달하여 가장 타당한 태그를 선택하게 합니다.
### 2.2 상호 검증 로직 (Cross-Validation)
매핑된 결과가 실제 공정 데이터와 일치하는지 검증합니다.
* **위상적 일관성:** 도면에서 `A $\rightarrow$ B` 순서라면, 실제 데이터에서도 `A`의 변화가 `B`에 영향을 주는지 상관관계 분석.
* **속성 일치성:** 도면의 심볼 타입(예: Pressure Transmitter)과 실제 태그의 속성(예: Engineering Unit = 'bar' 또는 'psi')이 일치하는지 확인.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 맥락 기반 매핑 엔진
```python
import networkx as nx
from rapidfuzz import process, fuzz
from openai import OpenAI
client = OpenAI(api_key="your-api-key")
class IntelligentMapper:
def __init__(self, graph, system_tags):
self.graph = graph # Phase 2에서 생성된 NetworkX 그래프
self.system_tags = system_tags # Experion 시스템의 전체 태그 리스트
def get_node_context(self, node_id):
"""노드의 주변 위상 정보를 텍스트로 변환"""
neighbors = list(self.graph.neighbors(node_id))
context = []
for n in neighbors:
attr = self.graph.nodes[n]
context.append(f"Connected to {attr.get('value', n)} (Type: {attr.get('type')})")
return ", ".join(context)
def resolve_tag(self, node_id):
# 1. 1차 후보 추출 (Fuzzy Matching)
tag_text = self.graph.nodes[node_id].get('value', '')
candidates = process.extract(tag_text, self.system_tags, scorer=fuzz.WRatio, limit=5)
# 2. 맥락 정보 수집
context = self.get_node_context(node_id)
# 3. LLM에게 최종 판단 요청
prompt = f"""
P&ID 도면의 태그 '{tag_text}'를 실제 시스템 태그와 매핑해야 합니다.
위상 맥락: {context}
후보 리스트: {candidates}
위 맥락을 고려할 때 가장 적절한 시스템 태그 하나만 반환하세요.
이유가 불분명하면 'UNKNOWN'을 반환하세요.
"""
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
# 사용 예시
mapper = IntelligentMapper(graph, ["FIC-101.PV", "PT-101.PV", "P-101.STATUS"])
final_tag = mapper.resolve_tag("node_tag_123")
print(f"Resolved Tag: {final_tag}")
```
### 3.2 검증 유틸리티: 속성 일치 확인
```python
def validate_mapping(resolved_tag, symbol_type, tag_metadata):
"""심볼 타입과 실제 태그 메타데이터의 일치 여부 검증"""
type_map = {
"Pressure Transmitter": ["pressure", "bar", "psi", "pa"],
"Flow Meter": ["flow", "m3/h", "lpm"],
"Temperature Sensor": ["temp", "celsius", "k"]
}
expected_keywords = type_map.get(symbol_type, [])
actual_desc = tag_metadata.get('description', '').lower()
# 메타데이터 설명에 기대 키워드가 포함되어 있는지 확인
is_valid = any(kw in actual_desc for kw in expected_keywords)
return is_valid
```
---
## 🚀 4. Phase 3 완료 기준 (Definition of Done)
- [ ] 모든 도면 노드에 대해 **1차 후보군(Candidates)**이 자동으로 생성되는가?
- [ ] `NetworkX` 그래프를 통해 **인접 노드 맥락(Context)**이 정확히 추출되는가?
- [ ] LLM이 맥락을 반영하여 **최종 태그를 결정**하고 그 근거를 제시하는가?
- [ ] 매핑된 태그의 **메타데이터(Unit, Description)**와 도면 심볼 타입 간의 일치성이 검증되는가?
- [ ] 최종 매핑 결과가 `(도면노드ID, 시스템태그, 신뢰도, 검증결과)` 형태로 저장되는가?

View File

@@ -1,103 +0,0 @@
# 🎨 Graph Pipeline Phase 4: 활용 및 시각화 (Application & Visualization)
이 문서는 P&ID Graph Pipeline의 최종 단계인 **활용 및 시각화**의 상세 구현 계획을 다룹니다. 앞선 단계에서 구축한 [기하학적 데이터 $\rightarrow$ 위상 그래프 $\rightarrow$ 시스템 태그 매핑] 결과물을 결합하여, 운영자가 도면 상에서 실시간 공정 상태를 파악하고 장애 영향도를 분석할 수 있는 인터페이스를 구현하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 기술 스택
### 1.1 프론트엔드 (Visualization)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `SVG / Canvas API` | P&ID 도면 렌더링 및 데이터 오버레이 | 벡터 기반 정밀 렌더링 |
| `Cytoscape.js` / `D3.js` | 위상 그래프 시각화 및 인터랙티브 탐색 | 그래프 분석 뷰어 |
| `Vue.js` / `React` | 전체 UI 프레임워크 및 상태 관리 | `src/Web` 구조와 통합 |
| `Axios` / `WebSocket` | 실시간 OPC UA 데이터 수신 및 API 통신 | 실시간 업데이트 |
### 1.2 백엔드 (API & Analysis)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `ASP.NET Core` | Graph API 및 분석 엔드포인트 제공 | `ExperionCrawler` 메인 서버 |
| `NetworkX` (Python) | 영향도 분석 및 경로 추적 알고리즘 실행 | 분석 엔진 (Phase 2 활용) |
| `FastAPI` / `Flask` | Python 분석 엔진과 C# 서버 간의 브릿지 | 분석 마이크로서비스 |
---
## 📐 2. 상세 설계 구조
### 2.1 실시간 데이터 오버레이 (Real-time Overlay)
도면의 좌표 정보와 매핑된 시스템 태그를 연결하여 실시간 값을 표시합니다.
1. **매핑 데이터 로드:** `(도면노드ID, 시스템태그, 좌표)` 리스트를 프론트엔드로 전달.
2. **실시간 스트리밍:** `OPC UA` $\rightarrow$ `C# Server` $\rightarrow$ `WebSocket` $\rightarrow$ `Frontend`.
3. **동적 렌더링:** 태그 값이 변경되면 해당 좌표의 SVG 요소 색상을 변경하거나 툴팁에 현재 값을 표시.
### 2.2 영향도 분석 엔진 (Impact Analysis Engine)
특정 설비의 이상 발생 시 하류(Downstream) 영향을 계산합니다.
1. **분석 요청:** 사용자가 도면에서 특정 노드(예: 펌프 P-101)를 클릭.
2. **그래프 탐색:** Python 분석 엔진에서 `nx.descendants(G, 'P-101')` 실행.
3. **결과 반환:** 영향받는 모든 노드 ID 리스트와 경로(Path)를 반환.
4. **시각적 강조:** 도면 상에서 영향 경로를 하이라이트(예: 빨간색 선) 처리.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 [Backend] 영향도 분석 API (C# $\rightarrow$ Python Bridge)
```csharp
// src/Web/Controllers/PidGraphController.cs
[HttpGet("impact/{nodeId}")]
public async Task<IActionResult> GetImpactAnalysis(string nodeId)
{
// Python 분석 마이크로서비스에 요청
var response = await _httpClient.GetAsync($"http://python-analysis-api/impact/{nodeId}");
var result = await response.Content.ReadFromJsonAsync<ImpactResult>();
return Ok(result);
}
```
### 3.2 [Frontend] SVG 데이터 오버레이 (JavaScript)
```javascript
// src/Web/wwwroot/js/pid-viewer.js
async function updateRealtimeValues(tagData) {
// tagData: { "PT-101.PV": 12.5, "FT-101.PV": 150.2 }
for (const [tag, value] of Object.entries(tagData)) {
const element = document.getElementById(`tag-node-${tag}`);
if (element) {
// 값에 따라 색상 변경 (예: 임계치 초과 시 빨간색)
element.style.fill = value > threshold ? 'red' : 'green';
element.setAttribute('data-value', value);
// 툴팁 업데이트
const tooltip = document.getElementById('pid-tooltip');
tooltip.innerText = `${tag}: ${value}`;
}
}
}
```
### 3.3 [Analysis] 경로 추적 유틸리티 (Python)
```python
import networkx as nx
def get_propagation_path(graph, start_node, end_node):
"""장애 전파 경로를 최단 경로 기반으로 추출"""
try:
path = nx.shortest_path(graph, source=start_node, target=end_node)
return path
except nx.NetworkXNoPath:
return None
# 예: P-101에서 V-105까지의 영향 경로 추출
path = get_propagation_path(topology_graph, "P-101", "V-105")
```
---
## 🚀 4. Phase 4 완료 기준 (Definition of Done)
- [ ] P&ID 도면(SVG/Canvas) 위에 **실시간 OPC UA 값**이 정확한 좌표에 표시되는가?
- [ ] 특정 노드 클릭 시 **하류 영향도 분석(Impact Analysis)** 결과가 시각적으로 하이라이트 되는가?
- [ ] C# 메인 서버와 Python 분석 엔진 간의 **API 통신**이 지연 없이 이루어지는가?
- [ ] 운영자가 도면을 통해 **이상 징후의 전파 경로**를 직관적으로 파악할 수 있는가?
- [ ] 전체 파이프라인(`추출 $\rightarrow$ 모델링 $\rightarrow$ 매핑 $\rightarrow$ 시각화`)이 통합되어 동작하는가?

View File

@@ -1,145 +0,0 @@
# 🕸️ Graph Pipeline Phase 2: 위상 모델링 (Topology Modeling)
이 문서는 P&ID Graph Pipeline의 두 번째 단계인 **위상 모델링**의 상세 구현 계획을 다룹니다. 1단계에서 추출한 기하학적 객체(좌표, BBox)를 기반으로, 설비 간의 **연결성(Connectivity)**과 **흐름(Flow)**을 정의하는 지식 그래프(Knowledge Graph)를 구축하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `networkx` | 그래프 데이터 구조 생성 및 알고리즘 분석 | 핵심 라이브러리 |
| `shapely` | 객체 간 거리 계산 및 포함 관계 분석 | 1단계와 연계 |
| `scikit-learn` | (선택) KD-Tree를 이용한 고속 근접 이웃 검색 | 대규모 도면 최적화 |
| `matplotlib` | 생성된 그래프의 위상 구조 시각화 검증 | 디버깅용 |
### 1.2 설치 명령어
```bash
pip install networkx shapely scikit-learn matplotlib
```
---
## 📐 2. 상세 설계 구조
### 2.1 그래프 정의 (Graph Definition)
* **노드 (Nodes):**
* `Equipment`: 펌프, 탱크, 열교환기 등 (속성: ID, 타입, BBox)
* `Instrument`: 전송기, 밸브, 게이지 등 (속성: ID, 타입, BBox)
* `Tag`: 텍스트 기반 태그 (속성: TagName, Value)
* **엣지 (Edges):**
* `Pipe`: 설비-설비, 설비-계기 간의 물리적 연결 (속성: LineNumber, 방향성)
* `Association`: 태그-설비 간의 논리적 연결 (속성: 관계 타입 - 예: 'belongs_to')
### 2.2 위상 추론 로직 (Topology Inference)
1. **태그-설비 결합 (Tag-to-Entity Binding):**
* 태그 텍스트의 BBox와 가장 가까운 심볼(Equipment/Instrument)을 찾아 `Association` 엣지를 생성합니다.
2. **배관 연결성 분석 (Line Connectivity):**
* `LINE` 또는 `POLYLINE`의 끝점이 특정 설비의 BBox 내부에 있거나 임계 거리($\epsilon$) 이내에 있으면 두 노드를 `Pipe` 엣지로 연결합니다.
3. **흐름 방향성 부여 (Flow Direction):**
* 화살표 심볼의 방향 또는 공정 흐름 규칙을 분석하여 엣지에 `source` $\rightarrow$ `target` 방향을 설정합니다.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 그래프 구축 핵심 코드
```python
import networkx as nx
from shapely.geometry import box, Point
class PidTopologyBuilder:
def __init__(self, geometric_data, all_extracted_tags=None):
"""
Phase 5 병렬 아키텍처 반영:
- geometric_data: Phase 1에서 추출된 기하학적 데이터
- all_extracted_tags: 여러 Worker(Phase 3)가 분산 추출한 태그 리스트의 통합본 (flatten_results 결과)
"""
self.data = geometric_data
self.all_tags = all_extracted_tags if all_extracted_tags else []
self.G = nx.DiGraph() # 방향성 그래프 생성
def build_graph(self):
# 1. 모든 객체를 노드로 추가
for item in self.data:
self.G.add_node(item['id'],
type=item['type'],
bbox=box(*item['bbox'].values()),
value=item.get('value'))
# 2. 분산 추출된 태그 통합 및 노드 추가 (Phase 5 반영)
for tag in self.all_tags:
# tag: { "id": "...", "tagName": "...", "bbox": {...}, "type": "TEXT" }
self.G.add_node(tag['id'],
type='TEXT',
bbox=box(*tag['bbox'].values()),
value=tag.get('tagName'))
# 3. 태그-설비 논리적 연결 (Association)
tags = [n for n, d in self.G.nodes(data=True) if d['type'] == 'TEXT']
equipments = [n for n, d in self.G.nodes(data=True) if d['type'] != 'TEXT']
for tag in tags:
best_match = self._find_nearest_equipment(tag, equipments)
if best_match:
self.G.add_edge(tag, best_match, relation='associated_with')
# 3. 배관 기반 물리적 연결 (Pipe)
lines = [n for n, d in self.G.nodes(data=True) if d['type'] in ['LINE', 'POLYLINE']]
for line in lines:
connected_nodes = self._find_connected_nodes(line, equipments)
if len(connected_nodes) >= 2:
# 라인을 통해 연결된 두 설비 간 엣지 생성
self.G.add_edge(connected_nodes[0], connected_nodes[1], relation='pipe')
def _find_nearest_equipment(self, tag_id, equipment_ids):
tag_bbox = self.G.nodes[tag_id]['bbox']
min_dist = float('inf')
nearest = None
for eq_id in equipment_ids:
eq_bbox = self.G.nodes[eq_id]['bbox']
dist = tag_bbox.distance(eq_bbox)
if dist < min_dist:
min_dist = dist
nearest = eq_id
return nearest if min_dist < 50.0 else None # 임계값 50.0
def _find_connected_nodes(self, line_id, equipment_ids):
# 라인의 시작/끝점이 어떤 설비 BBox에 포함되는지 확인
# (실제 구현 시 line의 coordinates 활용)
return [eq for eq in equipment_ids if self.G.nodes[eq]['bbox'].intersects(self.G.nodes[line_id]['bbox'])]
# 실행 (Phase 5 Orchestrator 관점)
# 1. Phase 1 결과 로드
# 2. Phase 3 Worker들의 결과를 flatten_results()로 통합
all_tags = flatten_results([worker1_res, worker2_res, worker3_res, worker4_res, worker5_res])
builder = PidTopologyBuilder(geometric_data, all_extracted_tags=all_tags)
builder.build_graph()
graph = builder.G
```
### 3.2 위상 분석 유틸리티: 영향도 분석 (Impact Analysis)
```python
def analyze_impact(graph, start_node):
"""특정 설비 장애 시 하류(Downstream)에 영향을 받는 모든 노드 추출"""
# BFS를 통해 도달 가능한 모든 노드 탐색
impacted_nodes = nx.descendants(graph, start_node)
return list(impacted_nodes)
# 예: P-101 펌프 고장 시 영향 분석
affected = analyze_impact(graph, "node_P101")
print(f"Impacted Equipment: {affected}")
```
---
## 🚀 4. Phase 2 완료 기준 (Definition of Done)
- [ ] 모든 설비와 계기가 그래프의 **노드(Node)**로 변환되었는가?
- [ ] 분산 추출된 태그 리스트가 `flatten_results`를 통해 통합되어 그래프에 반영되었는가? (Phase 5 반영)
- [ ] 태그와 설비 간의 **논리적 연결(Association)**이 정확하게 매핑되었는가?
- [ ] 배관(Line)을 통해 설비 간의 **물리적 연결(Pipe Edge)**이 생성되었는가?
- [ ] `nx.descendants` 등을 통해 특정 노드로부터의 **흐름 추적(Flow Tracing)**이 가능한가?
- [ ] 생성된 그래프 구조가 JSON(GraphML 등) 형태로 저장되어 Phase 3로 전달 가능한가?

View File

@@ -1,158 +0,0 @@
# 🧠 Graph Pipeline Phase 3: 지능형 매핑 및 검증 (Intelligent Mapping & Validation)
이 문서는 P&ID Graph Pipeline의 세 번째 단계인 **지능형 매핑 및 검증**의 상세 구현 계획을 다룹니다. 2단계에서 구축한 위상 그래프(Topology Graph)를 활용하여, 도면 상의 가상 노드들을 실제 Experion 시스템의 **실시간 태그(Real-time Tags)**와 정밀하게 연결하고 그 타당성을 검증하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `openai` / `langchain` | LLM API 연동 및 프롬프트 체이닝 | 매핑 추론 및 검증 핵심 |
| `fuzzywuzzy` / `rapidfuzz` | 태그 이름 간의 문자열 유사도 계산 | 1차 후보군 추출용 |
| `networkx` | 그래프 기반 인접 노드(Context) 추출 | 2단계 그래프 활용 |
| `pydantic` | 매핑 결과의 구조화 및 유효성 검사 | 데이터 정규화 |
| `requests` | ExperionCrawler API (C#)와 통신 | 실제 태그 리스트 조회 |
### 1.2 설치 명령어
```bash
pip install openai langchain rapidfuzz networkx pydantic requests
```
---
## 📐 2. 상세 설계 구조
### 2.1 매핑 파이프라인 (Mapping Pipeline)
단순 이름 매칭의 한계를 극복하기 위해 **[후보 추출 $\rightarrow$ 맥락 분석 $\rightarrow$ LLM 확정]**의 3단계 프로세스를 거칩니다.
1. **1차 후보 추출 (Candidate Generation):**
* 도면의 태그 텍스트와 Experion 시스템의 전체 태그 리스트를 `RapidFuzz`로 비교하여 유사도 상위 N개를 추출합니다.
2. **맥락 정보 수집 (Context Gathering):**
* 해당 노드의 그래프 상 인접 노드(1-hop, 2-hop) 정보를 수집합니다.
* 예: "현재 노드는 `PT-101`이며, 상류에 `P-101(Pump)`이 있고 하류에 `V-101(Valve)`이 있음."
3. **LLM 기반 최종 매핑 (LLM-based Resolution):**
* 후보 태그 리스트와 위상 맥락을 LLM에게 전달하여 가장 타당한 태그를 선택하게 합니다.
### 2.2 상호 검증 로직 (Cross-Validation)
매핑된 결과가 실제 공정 데이터와 일치하는지 검증합니다.
* **위상적 일관성:** 도면에서 `A $\rightarrow$ B` 순서라면, 실제 데이터에서도 `A`의 변화가 `B`에 영향을 주는지 상관관계 분석.
* **속성 일치성:** 도면의 심볼 타입(예: Pressure Transmitter)과 실제 태그의 속성(예: Engineering Unit = 'bar' 또는 'psi')이 일치하는지 확인.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 맥락 기반 매핑 엔진
```python
import networkx as nx
import asyncio
from rapidfuzz import process, fuzz
from openai import AsyncOpenAI # 비동기 클라이언트로 변경
client = AsyncOpenAI(api_key="your-api-key")
class IntelligentMapper:
def __init__(self, graph, system_tags):
self.graph = graph # Phase 2에서 생성된 NetworkX 그래프
self.system_tags = system_tags # Experion 시스템의 전체 태그 리스트
def get_node_context(self, node_id):
"""노드의 주변 위상 정보를 텍스트로 변환"""
neighbors = list(self.graph.neighbors(node_id))
context = []
for n in neighbors:
attr = self.graph.nodes[n]
context.append(f"Connected to {attr.get('value', n)} (Type: {attr.get('type')})")
return ", ".join(context)
async def _resolve_generic(self, node_id, category_prompt):
"""공통 매핑 로직 (비동기)"""
tag_text = self.graph.nodes[node_id].get('value', '')
candidates = process.extract(tag_text, self.system_tags, scorer=fuzz.WRatio, limit=5)
context = self.get_node_context(node_id)
prompt = f"""
{category_prompt}
P&ID 도면의 태그 '{tag_text}'를 실제 시스템 태그와 매핑해야 합니다.
위상 맥락: {context}
후보 리스트: {candidates}
위 맥락을 고려할 때 가장 적절한 시스템 태그 하나만 반환하세요.
이유가 불분명하면 'UNKNOWN'을 반환하세요.
"""
response = await client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
# --- 전문화된 Worker 함수들 (Phase 5 병렬 처리 반영) ---
async def extract_transmitters(self, node_ids):
"""전송기(Transmitter) 전문 매핑 Worker"""
prompt = "당신은 계측기 전문 엔지니어입니다. 특히 Pressure/Flow/Level Transmitter 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_valves(self, node_ids):
"""밸브(Valve) 전문 매핑 Worker"""
prompt = "당신은 밸브 및 액추에이터 전문 엔지니어입니다. 밸브의 개폐 상태 및 제어 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_equipment(self, node_ids):
"""주요 설비(Pump, Tank 등) 전문 매핑 Worker"""
prompt = "당신은 공정 설비 전문 엔지니어입니다. 펌프, 탱크, 열교환기 등의 메인 설비 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
# 사용 예시 (Phase 5 Orchestrator 관점)
async def main():
mapper = IntelligentMapper(graph, ["FIC-101.PV", "PT-101.PV", "P-101.STATUS"])
# 분류별로 노드 그룹화 (예시)
transmitter_nodes = ["node_1", "node_2"]
valve_nodes = ["node_3", "node_4"]
equipment_nodes = ["node_5"]
# asyncio.gather를 통한 병렬 호출
results = await asyncio.gather(
mapper.extract_transmitters(transmitter_nodes),
mapper.extract_valves(valve_nodes),
mapper.extract_equipment(equipment_nodes)
)
# 결과 통합 (flatten)
final_mapping = {**results[0], **results[1], **results[2]}
print(f"Parallel Resolved Mapping: {final_mapping}")
asyncio.run(main())
```
### 3.2 검증 유틸리티: 속성 일치 확인
```python
def validate_mapping(resolved_tag, symbol_type, tag_metadata):
"""심볼 타입과 실제 태그 메타데이터의 일치 여부 검증"""
type_map = {
"Pressure Transmitter": ["pressure", "bar", "psi", "pa"],
"Flow Meter": ["flow", "m3/h", "lpm"],
"Temperature Sensor": ["temp", "celsius", "k"]
}
expected_keywords = type_map.get(symbol_type, [])
actual_desc = tag_metadata.get('description', '').lower()
# 메타데이터 설명에 기대 키워드가 포함되어 있는지 확인
is_valid = any(kw in actual_desc for kw in expected_keywords)
return is_valid
```
---
## 🚀 4. Phase 3 완료 기준 (Definition of Done)
- [ ] 모든 도면 노드에 대해 **1차 후보군(Candidates)**이 자동으로 생성되는가?
- [ ] `NetworkX` 그래프를 통해 **인접 노드 맥락(Context)**이 정확히 추출되는가?
- [ ] LLM이 맥락을 반영하여 **최종 태그를 결정**하고 그 근거를 제시하는가?
- [ ] 매핑된 태그의 **메타데이터(Unit, Description)**와 도면 심볼 타입 간의 일치성이 검증되는가?
- [ ] 최종 매핑 결과가 `(도면노드ID, 시스템태그, 신뢰도, 검증결과)` 형태로 저장되는가?

View File

@@ -1,145 +0,0 @@
# 🎨 Graph Pipeline Phase 4: 활용 및 시각화 (Application & Visualization)
이 문서는 P&ID Graph Pipeline의 최종 단계인 **활용 및 시각화**의 상세 구현 계획을 다룹니다. 앞선 단계에서 구축한 [기하학적 데이터 $\rightarrow$ 위상 그래프 $\rightarrow$ 시스템 태그 매핑] 결과물을 결합하여, 운영자가 도면 상에서 실시간 공정 상태를 파악하고 장애 영향도를 분석할 수 있는 인터페이스를 구현하는 것이 목표입니다.
---
## 📦 1. 필수 패키지 및 기술 스택
### 1.1 프론트엔드 (Visualization)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `SVG / Canvas API` | P&ID 도면 렌더링 및 데이터 오버레이 | 벡터 기반 정밀 렌더링 |
| `Cytoscape.js` / `D3.js` | 위상 그래프 시각화 및 인터랙티브 탐색 | 그래프 분석 뷰어 |
| `Vue.js` / `React` | 전체 UI 프레임워크 및 상태 관리 | `src/Web` 구조와 통합 |
| `Axios` / `WebSocket` | 실시간 OPC UA 데이터 수신 및 API 통신 | 실시간 업데이트 |
### 1.2 백엔드 (API & Analysis)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `ASP.NET Core` | Graph API 및 분석 엔드포인트 제공 | `ExperionCrawler` 메인 서버 |
| `NetworkX` (Python) | 영향도 분석 및 경로 추적 알고리즘 실행 | 분석 엔진 (Phase 2 활용) |
| `FastAPI` / `Flask` | Python 분석 엔진과 C# 서버 간의 브릿지 | 분석 마이크로서비스 |
---
## 📐 2. 상세 설계 구조
### 2.1 실시간 데이터 오버레이 (Real-time Overlay)
도면의 좌표 정보와 매핑된 시스템 태그를 연결하여 실시간 값을 표시합니다.
1. **매핑 데이터 로드:** `(도면노드ID, 시스템태그, 좌표)` 리스트를 프론트엔드로 전달.
2. **실시간 스트리밍:** `OPC UA` $\rightarrow$ `C# Server` $\rightarrow$ `WebSocket` $\rightarrow$ `Frontend`.
3. **동적 렌더링:** 태그 값이 변경되면 해당 좌표의 SVG 요소 색상을 변경하거나 툴팁에 현재 값을 표시.
### 2.2 영향도 분석 엔진 (Impact Analysis Engine)
특정 설비의 이상 발생 시 하류(Downstream) 영향을 계산합니다.
1. **분석 요청:** 사용자가 도면에서 특정 노드(예: 펌프 P-101)를 클릭.
2. **그래프 탐색:** Python 분석 엔진에서 `nx.descendants(G, 'P-101')` 실행.
3. **결과 반환:** 영향받는 모든 노드 ID 리스트와 경로(Path)를 반환.
4. **시각적 강조:** 도면 상에서 영향 경로를 하이라이트(예: 빨간색 선) 처리.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 [Backend] 영향도 분석 API (C# $\rightarrow$ Python Bridge)
```csharp
// src/Web/Controllers/PidGraphController.cs
// 1. 분석 상태 추적을 위한 DTO
public record AnalysisStatus(string taskId, double progress, string status, string message);
// 2. 실시간 진행 상태 조회 API (Phase 5 병렬 처리 반영)
[HttpGet("status/{taskId}")]
public async Task<IActionResult> GetAnalysisStatus(string taskId)
{
// Orchestrator가 관리하는 작업 상태 저장소(Redis/MemoryCache)에서 조회
var status = await _statusService.GetStatusAsync(taskId);
if (status == null) return NotFound();
return Ok(new {
taskId = status.TaskId,
progress = status.Progress, // 0.0 ~ 1.0
status = status.Status, // "Processing", "Completed", "Failed"
message = status.Message
});
}
[HttpGet("impact/{nodeId}")]
public async Task<IActionResult> GetImpactAnalysis(string nodeId)
{
// Python 분석 마이크로서비스에 요청
var response = await _httpClient.GetAsync($"http://python-analysis-api/impact/{nodeId}");
var result = await response.Content.ReadFromJsonAsync<ImpactResult>();
return Ok(result);
}
```
### 3.2 [Frontend] SVG 데이터 오버레이 및 진행률 표시 (JavaScript)
```javascript
// src/Web/wwwroot/js/pid-viewer.js
// 1. 실시간 값 업데이트
async function updateRealtimeValues(tagData) {
for (const [tag, value] of Object.entries(tagData)) {
const element = document.getElementById(`tag-node-${tag}`);
if (element) {
element.style.fill = value > threshold ? 'red' : 'green';
element.setAttribute('data-value', value);
const tooltip = document.getElementById('pid-tooltip');
tooltip.innerText = `${tag}: ${value}`;
}
}
}
// 2. 분석 진행 상태 표시 (Phase 5 병렬 처리 반영)
async function trackAnalysisProgress(taskId) {
const progressBar = document.getElementById('analysis-progress-bar');
const statusText = document.getElementById('analysis-status-text');
const pollStatus = async () => {
const response = await fetch(`/api/pid/status/${taskId}`);
const data = await response.json();
// 프로그레스 바 업데이트 (예: 20% -> 40% -> 100%)
progressBar.style.width = `${data.progress * 100}%`;
statusText.innerText = `분석 중... ${Math.round(data.progress * 100)}% (${data.message})`;
if (data.status !== 'Completed' && data.status !== 'Failed') {
setTimeout(pollStatus, 1000); // 1초 간격 폴링
} else {
statusText.innerText = data.status === 'Completed' ? '분석 완료!' : '분석 실패';
}
};
pollStatus();
}
```
### 3.3 [Analysis] 경로 추적 유틸리티 (Python)
```python
import networkx as nx
def get_propagation_path(graph, start_node, end_node):
"""장애 전파 경로를 최단 경로 기반으로 추출"""
try:
path = nx.shortest_path(graph, source=start_node, target=end_node)
return path
except nx.NetworkXNoPath:
return None
# 예: P-101에서 V-105까지의 영향 경로 추출
path = get_propagation_path(topology_graph, "P-101", "V-105")
```
---
## 🚀 4. Phase 4 완료 기준 (Definition of Done)
- [ ] P&ID 도면(SVG/Canvas) 위에 **실시간 OPC UA 값**이 정확한 좌표에 표시되는가?
- [ ] 병렬 처리 중인 분석 작업의 **진행 상태(Progress Bar)**가 UI에 실시간으로 반영되는가? (Phase 5 반영)
- [ ] 특정 노드 클릭 시 **하류 영향도 분석(Impact Analysis)** 결과가 시각적으로 하이라이트 되는가?
- [ ] C# 메인 서버와 Python 분석 엔진 간의 **API 통신**이 지연 없이 이루어지는가?
- [ ] 운영자가 도면을 통해 **이상 징후의 전파 경로**를 직관적으로 파악할 수 있는가?
- [ ] 전체 파이프라인(`추출 $\rightarrow$ 모델링 $\rightarrow$ 매핑 $\rightarrow$ 시각화`)이 통합되어 동작하는가?

View File

@@ -1,219 +0,0 @@
# 🛠️ Graph Pipeline Phase 1: 기하학적 데이터 추출 (Geometric Extraction)
이 문서는 P&ID Graph Pipeline의 첫 번째 단계인 **기하학적 데이터 추출**의 상세 구현 계획을 다룹니다. 목표는 단순한 텍스트 추출을 넘어, 도면 내 모든 객체의 **물리적 위치(좌표)**와 **기하학적 속성**을 보존하여 이후 위상 모델링(Topology Modeling)이 가능하도록 하는 것입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `ezdxf` | DXF 파일 파싱 및 엔티티 추출 | 핵심 라이브러리 |
| `shapely` | 기하학적 연산 (Intersection, Distance, Bounding Box) | 좌표 기반 분석 필수 |
| `numpy` | 대량의 좌표 데이터 계산 및 행렬 연산 | 성능 최적화 |
| `pandas` | 추출된 객체 데이터의 구조화 및 CSV/JSON 저장 | 데이터 관리 |
| `pydantic` | 추출 데이터의 스키마 정의 및 유효성 검증 | 데이터 무결성 보장 |
| `pytesseract` / `pdf2image` | PDF 도면의 영역 기반 OCR 추출 | PDF 처리 시 필요 |
### 1.2 설치 명령어
```bash
pip install ezdxf shapely numpy pandas pydantic pytesseract pdf2image
```
---
## 📐 2. 상세 설계 구조
### 2.1 데이터 모델 (Schema)
모든 추출 객체는 다음과 같은 공통 속성을 갖는 `GeometricEntity` 모델을 따릅니다.
```python
from pydantic import BaseModel
from typing import List, Optional, Union, Tuple
class BoundingBox(BaseModel):
min_x: float
min_y: float
max_x: float
max_y: float
center: Tuple[float, float]
class GeometricEntity(BaseModel):
entity_id: str
entity_type: str # TEXT, LINE, CIRCLE, POLYLINE, ARC
layer: str
bbox: BoundingBox
properties: dict # 텍스트 값, 색상, 선 굵기 등
coordinates: List[Tuple[float, float]] # 시작점, 끝점 또는 정점 리스트
```
### 2.2 처리 파이프라인 흐름
1. **DXF Load:** `ezdxf.readfile()`을 통해 도면 로드.
2. **Entity Iteration:** 모든 레이어의 엔티티를 순회하며 타입별 분류.
3. **Coordinate Extraction:**
* `TEXT`: 삽입점(Insertion Point) 및 텍스트 길이를 이용한 BBox 계산.
* `LINE`: 시작점(Start)과 끝점(End) 추출.
* `POLYLINE`: 모든 정점(Vertices) 리스트 추출.
* `CIRCLE/ARC`: 중심점(Center)과 반지름(Radius) 추출.
4. **Spatial Normalization:** 도면 좌표계를 분석 시스템 좌표계로 정규화.
5. **Structured Export:** JSON 또는 DB(PostgreSQL/PostGIS)에 저장.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 DXF 기하학적 추출 핵심 코드
```python
import ezdxf
import re
import json
from shapely.geometry import box, LineString, Point
from typing import List, Optional, Tuple
class PidGeometricExtractor:
def __init__(self, file_path: str):
self.doc = ezdxf.readfile(file_path)
self.msp = self.doc.modelspace()
def clean_text(self, text: str) -> str:
"""DXF 특수 제어 문자 및 MTEXT 포맷팅을 최대한 제거하여 LLM 토큰 부하 감소"""
if not text:
return ""
# 1. MTEXT 포맷팅 및 제어 문자 제거
# \P(줄바꿈), \W(너비), \L(밑줄), \A(정렬), \C(색상), \H(높이), \S(스택), \T(탭) 및 관련 인자 제거
text = re.sub(r'\\([P|W|L|A|C|H|S|T])\d*;?', ' ', text)
# 2. 중괄호 { } 제거 (MTEXT에서 서식 지정 시 사용됨)
text = re.sub(r'[\{\}]', ' ', text)
# 3. DXF 특수 제어 문자 제거 (%%U: Underline, %%O: Overline, %%S: Strikethrough, %%R: Registered)
text = re.sub(r'%%[U|O|S|R]', ' ', text)
# 4. 불필요한 특수 기호 및 반복되는 공백 정제
# - 연속된 공백을 하나로 통합
# - 텍스트 양 끝의 공백 제거
text = re.sub(r'\s+', ' ', text).strip()
return text
def get_bbox(self, entity) -> Optional[box]:
"""엔티티의 Bounding Box를 계산하여 shapely box 객체로 반환"""
try:
if entity.dxftype() == 'TEXT':
p = entity.dxf.insert
h = entity.dxf.height
# 텍스트 길이에 따른 대략적인 너비 계산 (글자수 * 높이 * 0.6)
width = len(entity.dxf.text) * h * 0.6
return box(p.x, p.y, p.x + width, p.y + h)
elif entity.dxftype() == 'MTEXT':
p = entity.dxf.insert
h = entity.dxf.char_height if hasattr(entity.dxf, 'char_height') else 2.5
# MTEXT는 보통 width 속성이 정의되어 있음
w = entity.dxf.width if entity.dxf.width > 0 else len(entity.text) * h * 0.6
return box(p.x, p.y, p.x + w, p.y + h)
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
return box(min(start.x, end.x), min(start.y, end.y),
max(start.x, end.x), max(start.y, end.y))
elif entity.dxftype() == 'LWPOLYLINE':
points = entity.get_points()
xs = [p[0] for p in points]
ys = [p[1] for p in points]
return box(min(xs), min(ys), max(xs), max(ys))
except Exception as e:
print(f"Error calculating bbox for {entity.dxftype()}: {e}")
return None
def extract_and_save(self, output_path: str):
"""
추출된 기하학적 데이터를 파일로 저장하여 Phase 3 Worker들이
공유 메모리/파일 시스템을 통해 참조할 수 있도록 함 (Phase 5 병렬 아키텍처 반영)
"""
results = []
for entity in self.msp:
bbox_obj = self.get_bbox(entity)
if bbox_obj:
# 텍스트 값 추출 및 정제
raw_text = ""
if entity.dxftype() == 'TEXT':
raw_text = entity.dxf.text
elif entity.dxftype() == 'MTEXT':
raw_text = entity.text
results.append({
"id": entity.dxf.handle,
"type": entity.dxftype(),
"layer": entity.dxf.layer,
"bbox": {
"min_x": bbox_obj.bounds[0],
"min_y": bbox_obj.bounds[1],
"max_x": bbox_obj.bounds[2],
"max_y": bbox_obj.bounds[3]
},
"raw_value": raw_text,
"clean_value": self.clean_text(raw_text) if raw_text else None
})
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return output_path
# 사용 예시 (Phase 5 Orchestrator 관점)
extractor = PidGeometricExtractor("plant_drawing.dxf")
# 데이터를 직접 반환받지 않고 공유 저장소(파일)에 적재
geo_data_path = extractor.extract_and_save("shared_geo_data.json")
```
### 3.2 유틸리티 함수: 인접성 체크 (Proximity Utility)
추후 2단계(위상 모델링)에서 사용할 핵심 유틸리티입니다.
```python
from shapely.geometry import Point
def is_near(entity_a_bbox, entity_b_bbox, threshold=5.0):
"""두 객체의 Bounding Box 간의 최단 거리가 임계값 이내인지 확인"""
return entity_a_bbox.distance(entity_b_bbox) <= threshold
def is_inside(point, bbox):
"""특정 점이 Bounding Box 내부에 있는지 확인"""
return bbox.contains(Point(point))
```
---
## 🚀 4. Phase 1 완료 기준 (Definition of Done)
- [ ] DXF 파일 내 모든 `TEXT`, `LINE`, `POLYLINE`의 좌표 데이터가 누락 없이 추출되는가?
- [ ] 각 객체별로 정확한 `Bounding Box`가 계산되어 저장되는가?
- [ ] 추출된 데이터가 `GeometricEntity` 스키마에 맞게 JSON 파일로 저장되어 Worker들이 공유 참조 가능한가? (Phase 5 반영)
- [ ] (선택 사항) PDF 도면의 경우 OCR을 통해 텍스트의 좌표값이 추출되는가?
---
## 🧐 감독자 진단 결과 (2026-05-02)
### 1. 프로그램 설계 점검
- **강점**: `ezdxf``shapely`를 조합하여 기하학적 데이터(BBox, 좌표)를 보존하려는 접근 방식이 매우 적절함. 특히 Phase 5의 병렬 아키텍처를 고려하여 데이터를 파일/공유 저장소에 적재하는 구조는 확장성 면에서 우수함.
- **보완 필요 사항**:
- **MTEXT 처리**: 현재 예시 코드(`3.1`)는 `TEXT` 엔티티만 처리하고 있으나, 실제 DXF 파일 분석 결과 `MTEXT` 엔티티가 다수 존재함. `MTEXT`는 내부 포맷팅 코드(예: `\P`, `\W`)가 포함되어 있어 단순 텍스트 추출 시 정제가 필요함.
- **BBox 계산 정밀도**: `TEXT` 엔티티의 BBox를 `p.x + 10, p.y + 5`와 같이 상수로 처리하고 있음. 실제 도면의 폰트 크기(`height`)와 정렬 방식(`align`)을 반영한 동적 계산 로직이 반드시 추가되어야 함.
### 2. 실제 도면(`No-10_Plant_PID.dxf`) 분석 기반 차이점
- **엔티티 규모**: 총 28,819개의 엔티티가 존재하여 데이터 양이 상당함. 단순 리스트 저장보다는 인덱싱 전략이 필요할 수 있음.
- **텍스트 복잡도**:
- `MTEXT` 내에 `\P` (줄바꿈), `\L` (밑줄) 등 제어 문자가 포함된 수정 사항(Revision) 텍스트가 많음. 이를 그대로 추출하면 위상 분석 시 노이즈가 될 가능성이 높음.
- `%%U` (Underline)와 같은 DXF 특수 제어 문자가 텍스트 값에 포함되어 있어, 이를 제거하는 전처리 과정이 필수적임.
- **데이터 특성**: `IA-10922-25A-F1A-n`와 같은 복합 파이프라인 번호(Pipe Line Number) 형식이 확인됨. 이를 일반 태그(Tag Name)와 명확히 구분하여 추출하고 관리하는 로직이 Phase 2/3에서 중요하게 작용할 것으로 보임.
### 3. 최종 권고 사항
1. **MTEXT 지원 추가**: `PidGeometricExtractor``MTEXT` 처리 로직을 추가하고, 제어 문자를 제거하는 `clean_text()` 유틸리티 함수를 구현할 것.
2. **동적 BBox 구현**: `entity.dxf.height`를 활용하여 텍스트 크기에 맞는 정확한 Bounding Box를 계산하도록 수정할 것.
3. **전처리 파이프라인 강화**: 추출 단계에서 `%%U` 등의 특수 문자를 제거하는 정제 단계를 추가하여 데이터 품질을 높일 것.

View File

@@ -1,180 +0,0 @@
# 🕸️ Graph Pipeline Phase 2: 위상 모델링 (Topology Modeling)
이 문서는 P&ID Graph Pipeline의 두 번째 단계인 **위상 모델링**의 상세 구현 계획을 다룹니다. 1단계에서 추출한 기하학적 객체(좌표, BBox)를 기반으로, 설비 간의 **연결성(Connectivity)**과 **흐름(Flow)**을 정의하는 지식 그래프(Knowledge Graph)를 구축하는 것이 목표입니다.
---
## 🚩 [Supervisor's Audit] 진단 결과 및 개선 권고
**감독자 진단 일자:** 2026-05-02
**진단 결과:** ⚠️ **부분적 보완 필요 (Partial Improvement Required)**
### 🔍 주요 진단 내용
1. **연결성 추론의 단순성 (Critical):** 현재 `_find_connected_nodes`가 단순 BBox 교차(`intersects`)만 확인하고 있습니다. 실제 P&ID에서 배관(Line)은 설비의 외곽선에 닿거나 매우 근접한 형태로 나타나며, 단순 BBox 교차는 오탐(False Positive) 확률이 매우 높습니다.
2. **방향성 정의 부재 (Medium):** `DiGraph`를 사용하지만, 실제 엣지에 방향성을 부여하는 구체적인 로직(화살표 인식, 공정 흐름 규칙)이 예시 코드에 누락되어 있습니다.
3. **임계값 하드코딩 (Low):** `min_dist < 50.0`과 같은 임계값이 하드코딩되어 있어, 도면 스케일(Scale)이 변경될 경우 대응이 불가능합니다.
4. **데이터 무결성 검증 부족 (Medium):** 그래프 생성 후 고립된 노드(Isolated Nodes)나 비정상적인 루프에 대한 검증 단계가 없습니다.
### 🛠️ 수정 및 반영 사항
- **연결성 로직 고도화:** BBox 교차 방식에서 $\rightarrow$ **Line End-point 기반 근접 분석** 방식으로 변경.
- **방향성 추론 단계 명시:** 화살표 심볼 및 공정 흐름 기반의 `source` $\rightarrow$ `target` 결정 로직 추가.
- **설정의 외부화:** 임계값($\epsilon$)을 설정 파일이나 파라미터로 관리하도록 구조 변경.
- **검증 단계 추가:** 그래프 구축 후 위상 무결성 검사(Topology Validation) 단계 도입.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `networkx` | 그래프 데이터 구조 생성 및 알고리즘 분석 | 핵심 라이브러리 |
| `shapely` | 객체 간 거리 계산 및 포함 관계 분석 | 1단계와 연계 |
| `scikit-learn` | (선택) KD-Tree를 이용한 고속 근접 이웃 검색 | 대규모 도면 최적화 |
| `matplotlib` | 생성된 그래프의 위상 구조 시각화 검증 | 디버깅용 |
### 1.2 설치 명령어
```bash
pip install networkx shapely scikit-learn matplotlib
```
---
## 📐 2. 상세 설계 구조
### 2.1 그래프 정의 (Graph Definition)
* **노드 (Nodes):**
* `Equipment`: 펌프, 탱크, 열교환기 등 (속성: ID, 타입, BBox, CenterPoint)
* `Instrument`: 전송기, 밸브, 게이지 등 (속성: ID, 타입, BBox, CenterPoint)
* `Tag`: 텍스트 기반 태그 (속성: TagName, Value, BBox)
* **엣지 (Edges):**
* `Pipe`: 설비-설비, 설비-계기 간의 물리적 연결 (속성: LineNumber, 방향성, 연결타입)
* `Association`: 태그-설비 간의 논리적 연결 (속성: 관계 타입 - 예: 'belongs_to')
### 2.2 위상 추론 로직 (Topology Inference)
1. **태그-설비 결합 (Tag-to-Entity Binding):**
* 태그 텍스트의 BBox와 가장 가까운 심볼(Equipment/Instrument)을 찾아 `Association` 엣지를 생성합니다.
2. **배관 연결성 분석 (Line Connectivity) [개선]:**
* `LINE` 또는 `POLYLINE`의 **시작점과 끝점(End-points)**을 추출합니다.
* 각 끝점이 특정 설비의 BBox 내부에 있거나, 설정된 임계 거리($\epsilon$) 이내에 있을 때만 `Pipe` 엣지로 연결합니다. (단순 BBox 교차 방식 지양)
3. **흐름 방향성 부여 (Flow Direction) [추가]:**
* 배관 상의 화살표 심볼 위치와 방향을 분석하여 `source` $\rightarrow$ `target`을 결정합니다.
* 화살표가 없는 경우, 공정 표준(예: 탱크 $\rightarrow$ 펌프 $\rightarrow$ 밸브)에 따른 기본 방향을 부여합니다.
4. **위상 무결성 검증 (Topology Validation) [추가]:**
* 연결되지 않은 고립 노드 탐색 및 리포팅.
* 비정상적인 사이클(Cycle) 또는 단절 구간 확인.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 그래프 구축 핵심 코드
```python
import networkx as nx
from shapely.geometry import box, Point, LineString
class PidTopologyBuilder:
def __init__(self, geometric_data, all_extracted_tags=None, config=None):
"""
- geometric_data: Phase 1에서 추출된 기하학적 데이터
- all_extracted_tags: 통합된 태그 리스트
- config: {'dist_threshold': 50.0, 'tag_threshold': 100.0} 등 설정값
"""
self.data = geometric_data
self.all_tags = all_extracted_tags if all_extracted_tags else []
self.config = config if config else {'dist_threshold': 50.0, 'tag_threshold': 100.0}
self.G = nx.DiGraph() # 방향성 그래프 생성
def build_graph(self):
# 1. 모든 객체를 노드로 추가
for item in self.data:
self.G.add_node(item['id'],
type=item['type'],
bbox=box(*item['bbox'].values()),
value=item.get('value'))
# 2. 분산 추출된 태그 통합 및 노드 추가
for tag in self.all_tags:
self.G.add_node(tag['id'],
type='TEXT',
bbox=box(*tag['bbox'].values()),
value=tag.get('tagName'))
# 3. 태그-설비 논리적 연결 (Association)
tags = [n for n, d in self.G.nodes(data=True) if d['type'] == 'TEXT']
equipments = [n for n, d in self.G.nodes(data=True) if d['type'] != 'TEXT']
for tag in tags:
best_match = self._find_nearest_equipment(tag, equipments)
if best_match:
self.G.add_edge(tag, best_match, relation='associated_with')
# 4. 배관 기반 물리적 연결 (Pipe) [개선됨]
lines = [n for n, d in self.G.nodes(data=True) if d['type'] in ['LINE', 'POLYLINE']]
for line_id in lines:
line_geom = self.G.nodes[line_id]['bbox'] # 실제로는 LineString 객체여야 함
# 라인의 끝점 추출 (가정: line_geom이 LineString인 경우)
endpoints = [line_geom.coords[0], line_geom.coords[-1]] if hasattr(line_geom, 'coords') else []
connected_nodes = []
for pt in endpoints:
p = Point(pt)
for eq_id in equipments:
if self.G.nodes[eq_id]['bbox'].distance(p) < self.config['dist_threshold']:
connected_nodes.append(eq_id)
if len(connected_nodes) >= 2:
# 방향성 추론 로직 (단순화: 시작점 -> 끝점)
self.G.add_edge(connected_nodes[0], connected_nodes[1], relation='pipe')
def _find_nearest_equipment(self, tag_id, equipment_ids):
tag_bbox = self.G.nodes[tag_id]['bbox']
min_dist = float('inf')
nearest = None
for eq_id in equipment_ids:
eq_bbox = self.G.nodes[eq_id]['bbox']
dist = tag_bbox.distance(eq_bbox)
if dist < min_dist:
min_dist = dist
nearest = eq_id
return nearest if min_dist < self.config['tag_threshold'] else None
def validate_topology(self):
"""위상 무결성 검증"""
isolated = list(nx.isolates(self.G))
return {"isolated_nodes": isolated, "node_count": self.G.number_of_nodes(), "edge_count": self.G.number_of_edges()}
# 실행 예시
all_tags = flatten_results([worker1_res, worker2_res])
config = {'dist_threshold': 30.0, 'tag_threshold': 80.0}
builder = PidTopologyBuilder(geometric_data, all_extracted_tags=all_tags, config=config)
builder.build_graph()
validation_res = builder.validate_topology()
print(f"Validation Result: {validation_res}")
```
### 3.2 위상 분석 유틸리티: 영향도 분석 (Impact Analysis)
```python
def analyze_impact(graph, start_node):
"""특정 설비 장애 시 하류(Downstream)에 영향을 받는 모든 노드 추출"""
# BFS를 통해 도달 가능한 모든 노드 탐색
impacted_nodes = nx.descendants(graph, start_node)
return list(impacted_nodes)
# 예: P-101 펌프 고장 시 영향 분석
affected = analyze_impact(graph, "node_P101")
print(f"Impacted Equipment: {affected}")
```
---
## 🚀 4. Phase 2 완료 기준 (Definition of Done)
- [ ] 모든 설비와 계기가 그래프의 **노드(Node)**로 변환되었는가?
- [ ] 분산 추출된 태그 리스트가 `flatten_results`를 통해 통합되어 그래프에 반영되었는가?
- [ ] 태그와 설비 간의 **논리적 연결(Association)**이 정확하게 매핑되었는가?
- [ ] 배관(Line)의 **끝점 분석**을 통해 설비 간의 **물리적 연결(Pipe Edge)**이 생성되었는가? (BBox 교차 방식 배제)
- [ ] 화살표 및 공정 규칙에 기반한 **방향성(Directionality)**이 엣지에 부여되었는가?
- [ ] `validate_topology`를 통해 고립 노드 및 위상 오류가 검토되었는가?
- [ ] `nx.descendants` 등을 통해 특정 노드로부터의 **흐름 추적(Flow Tracing)**이 가능한가?
- [ ] 생성된 그래프 구조가 JSON(GraphML 등) 형태로 저장되어 Phase 3로 전달 가능한가?

View File

@@ -1,211 +0,0 @@
# 🧠 Graph Pipeline Phase 3: 지능형 매핑 및 검증 (Intelligent Mapping & Validation)
이 문서는 P&ID Graph Pipeline의 세 번째 단계인 **지능형 매핑 및 검증**의 상세 구현 계획을 다룹니다. 2단계에서 구축한 위상 그래프(Topology Graph)를 활용하여, 도면 상의 가상 노드들을 실제 Experion 시스템의 **실시간 태그(Real-time Tags)**와 정밀하게 연결하고 그 타당성을 검증하는 것이 목표입니다.
---
## 🚩 [Supervisor's Audit] 감독자 진단 결과 및 수정 사항
본 프로그램 설계에 대해 감독자 관점에서 정밀 진단을 수행하였으며, 다음과 같은 취약점과 개선 사항을 발견하여 반영하였습니다.
### 1. 진단 결과 (Audit Findings)
| 항목 | 진단 내용 | 심각도 | 수정 방향 |
|---|---|---|---|
| **에러 처리** | LLM 응답이 JSON 형식이 아니거나 `UNKNOWN`일 때의 예외 처리 로직 부족 | HIGH | 구조화된 출력(JSON) 강제 및 Fallback 전략 추가 |
| **성능/비용** | 모든 노드에 대해 개별 LLM 호출 시 API 비용 급증 및 속도 저하 | MED | 배치(Batch) 처리 및 1차 필터링 강화 |
| **검증 정밀도** | 단순 키워드 매칭 기반 검증은 오탐(False Positive) 가능성이 높음 | MED | 데이터 타입 및 엔지니어링 유닛(EU)의 엄격한 비교 로직 추가 |
| **데이터 정합성** | 매핑 결과의 이력 관리 및 사람이 수동으로 수정할 수 있는 피드백 루프 부재 | LOW | 매핑 결과 저장 스키마에 `confidence``manual_override` 필드 추가 |
### 2. 수정 이유 (Rationale)
- **안정성 확보:** LLM은 비결정론적 특성이 있으므로, 프로그램이 런타임에 중단되지 않도록 Pydantic을 이용한 엄격한 스키마 검증이 필수적입니다.
- **효율성 최적화:** 수천 개의 태그를 개별 호출하는 것은 비효율적입니다. 유사도 기반으로 후보군을 좁히고, 유사 그룹을 묶어 배치 처리함으로써 비용을 절감합니다.
- **신뢰도 향상:** 단순 텍스트 매칭을 넘어 실제 시스템의 메타데이터(Unit, Range 등)를 교차 검증해야 엔지니어링 관점에서 신뢰할 수 있는 결과가 됩니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `openai` / `langchain` | LLM API 연동 및 프롬프트 체이닝 | 매핑 추론 및 검증 핵심 |
| `fuzzywuzzy` / `rapidfuzz` | 태그 이름 간의 문자열 유사도 계산 | 1차 후보군 추출용 |
| `networkx` | 그래프 기반 인접 노드(Context) 추출 | 2단계 그래프 활용 |
| `pydantic` | 매핑 결과의 구조화 및 유효성 검사 | **[강화]** 데이터 정규화 및 런타임 타입 체크 |
| `requests` | ExperionCrawler API (C#)와 통신 | 실제 태그 리스트 조회 |
### 1.2 설치 명령어
```bash
pip install openai langchain rapidfuzz networkx pydantic requests
```
---
## 📐 2. 상세 설계 구조
### 2.1 매핑 파이프라인 (Mapping Pipeline)
단순 이름 매칭의 한계를 극복하기 위해 **[후보 추출 $\rightarrow$ 맥락 분석 $\rightarrow$ LLM 확정 $\rightarrow$ 스키마 검증]**의 4단계 프로세스를 거칩니다.
1. **1차 후보 추출 (Candidate Generation):**
* 도면의 태그 텍스트와 Experion 시스템의 전체 태그 리스트를 `RapidFuzz`로 비교하여 유사도 상위 N개를 추출합니다.
2. **맥락 정보 수집 (Context Gathering):**
* 해당 노드의 그래프 상 인접 노드(1-hop, 2-hop) 정보를 수집합니다.
* 예: "현재 노드는 `PT-101`이며, 상류에 `P-101(Pump)`이 있고 하류에 `V-101(Valve)`이 있음."
3. **LLM 기반 최종 매핑 (LLM-based Resolution):**
* 후보 태그 리스트와 위상 맥락을 LLM에게 전달하여 가장 타당한 태그를 선택하게 합니다.
* **[개선]** JSON Mode를 사용하여 `{"tag": "...", "reason": "...", "confidence": 0.9}` 형태로 응답을 강제합니다.
4. **구조적 검증 (Structural Validation):**
* Pydantic 모델을 통해 LLM 응답의 형식을 검증하고, 실패 시 `UNKNOWN` 처리 및 로그를 남깁니다.
### 2.2 상호 검증 로직 (Cross-Validation)
매핑된 결과가 실제 공정 데이터와 일치하는지 검증합니다.
* **위상적 일관성:** 도면에서 `A $\rightarrow$ B` 순서라면, 실제 데이터에서도 `A`의 변화가 `B`에 영향을 주는지 상관관계 분석.
* **속성 일치성:** 도면의 심볼 타입(예: Pressure Transmitter)과 실제 태그의 속성(예: Engineering Unit = 'bar' 또는 'psi')이 일치하는지 확인. **[강화]** 단순 키워드가 아닌 Unit 매핑 테이블을 통한 엄격한 비교.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 맥락 기반 매핑 엔진
```python
import networkx as nx
import asyncio
import json
from typing import List, Optional
from pydantic import BaseModel, Field
from rapidfuzz import process, fuzz
from openai import AsyncOpenAI
# --- [추가] 응답 구조화를 위한 Pydantic 모델 ---
class MappingResult(BaseModel):
resolved_tag: str = Field(..., description="The final mapped system tag")
reason: str = Field(..., description="Reason for this mapping based on context")
confidence: float = Field(..., ge=0.0, le=1.0, description="Confidence score from 0 to 1")
client = AsyncOpenAI(api_key="your-api-key")
class IntelligentMapper:
def __init__(self, graph, system_tags):
self.graph = graph # Phase 2에서 생성된 NetworkX 그래프
self.system_tags = system_tags # Experion 시스템의 전체 태그 리스트
def get_node_context(self, node_id):
"""노드의 주변 위상 정보를 텍스트로 변환"""
neighbors = list(self.graph.neighbors(node_id))
context = []
for n in neighbors:
attr = self.graph.nodes[n]
context.append(f"Connected to {attr.get('value', n)} (Type: {attr.get('type')})")
return ", ".join(context)
async def _resolve_generic(self, node_id, category_prompt):
"""공통 매핑 로직 (비동기 + 구조화 응답)"""
tag_text = self.graph.nodes[node_id].get('value', '')
candidates = process.extract(tag_text, self.system_tags, scorer=fuzz.WRatio, limit=5)
context = self.get_node_context(node_id)
prompt = f"""
{category_prompt}
P&ID 도면의 태그 '{tag_text}'를 실제 시스템 태그와 매핑해야 합니다.
위상 맥락: {context}
후보 리스트: {candidates}
반드시 다음 JSON 형식으로만 응답하세요:
{{
"resolved_tag": "태그명 또는 UNKNOWN",
"reason": "매핑 이유",
"confidence": 0.0~1.0
}}
"""
try:
response = await client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}],
response_format={ "type": "json_object" } # JSON 모드 강제
)
raw_content = response.choices[0].message.content
# Pydantic을 통한 유효성 검사
return MappingResult.model_validate_json(raw_content)
except Exception as e:
print(f"Error resolving node {node_id}: {e}")
return MappingResult(resolved_tag="UNKNOWN", reason=f"Error: {str(e)}", confidence=0.0)
# --- 전문화된 Worker 함수들 ---
async def extract_transmitters(self, node_ids):
prompt = "당신은 계측기 전문 엔지니어입니다. 특히 Pressure/Flow/Level Transmitter 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_valves(self, node_ids):
prompt = "당신은 밸브 및 액추에이터 전문 엔지니어입니다. 밸브의 개폐 상태 및 제어 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_equipment(self, node_ids):
prompt = "당신은 공정 설비 전문 엔지니어입니다. 펌프, 탱크, 열교환기 등의 메인 설비 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
# 사용 예시
async def main():
# 가상 데이터
graph = nx.Graph()
graph.add_node("node_1", value="PT-101", type="Pressure Transmitter")
graph.add_node("node_2", value="P-101", type="Pump")
graph.add_edge("node_1", "node_2")
mapper = IntelligentMapper(graph, ["PT-101.PV", "PT-102.PV", "P-101.STATUS"])
results = await asyncio.gather(
mapper.extract_transmitters(["node_1"]),
mapper.extract_equipment(["node_2"])
)
final_mapping = {**results[0], **results[1]}
print(f"Parallel Resolved Mapping: {final_mapping}")
asyncio.run(main())
```
### 3.2 검증 유틸리티: 속성 일치 확인 (강화 버전)
```python
def validate_mapping(resolved_tag, symbol_type, tag_metadata):
"""심볼 타입과 실제 태그 메타데이터의 엄격한 일치 여부 검증"""
# 단순 키워드가 아닌 허용 단위(Unit) 정의
unit_map = {
"Pressure Transmitter": ["bar", "psi", "kPa", "Pa"],
"Flow Meter": ["m3/h", "lpm", "kg/h"],
"Temperature Sensor": ["°C", "C", "K", "°F"]
}
actual_unit = tag_metadata.get('unit', '').strip()
allowed_units = unit_map.get(symbol_type, [])
# 1. 단위 일치 확인 (최우선)
if actual_unit and actual_unit in allowed_units:
return True, "Unit Match"
# 2. 단위가 없는 경우 설명(Description) 기반 2차 검증
actual_desc = tag_metadata.get('description', '').lower()
expected_keywords = {
"Pressure Transmitter": ["pressure", "press"],
"Flow Meter": ["flow", "flowrate"],
"Temperature Sensor": ["temp", "temperature"]
}
keywords = expected_keywords.get(symbol_type, [])
if any(kw in actual_desc for kw in keywords):
return True, "Description Match (Unit Missing)"
return False, "Mismatch: Symbol type and Tag metadata do not align"
```
---
## 🚀 4. Phase 3 완료 기준 (Definition of Done)
- [ ] 모든 도면 노드에 대해 **1차 후보군(Candidates)**이 자동으로 생성되는가?
- [ ] `NetworkX` 그래프를 통해 **인접 노드 맥락(Context)**이 정확히 추출되는가?
- [ ] LLM이 **JSON 형식**으로 최종 태그를 결정하고, 그 근거와 신뢰도를 제시하는가?
- [ ] **Pydantic**을 통해 LLM 응답의 구조적 유효성이 검증되는가?
- [ ] 매핑된 태그의 **엔지니어링 유닛(Unit)**과 도면 심볼 타입 간의 일치성이 엄격히 검증되는가?
- [ ] 최종 매핑 결과가 `(도면노드ID, 시스템태그, 신뢰도, 검증결과, 매핑근거)` 형태로 저장되는가?

View File

@@ -1,197 +0,0 @@
# 🎨 Graph Pipeline Phase 4: 활용 및 시각화 (Application & Visualization)
이 문서는 P&ID Graph Pipeline의 최종 단계인 **활용 및 시각화**의 상세 구현 계획을 다룹니다. 앞선 단계에서 구축한 [기하학적 데이터 $\rightarrow$ 위상 그래프 $\rightarrow$ 시스템 태그 매핑] 결과물을 결합하여, 운영자가 도면 상에서 실시간 공정 상태를 파악하고 장애 영향도를 분석할 수 있는 인터페이스를 구현하는 것이 목표입니다.
---
## 🔍 [Supervisor Diagnosis] 프로그램 진단 및 개선 권고
**진단 일자:** 2026-05-02
**진단자:** Roo (Software Engineer / Supervisor)
### 1. 종합 진단 결과
현재 계획은 기본적인 데이터 흐름(C# $\rightarrow$ Python $\rightarrow$ Frontend)을 잘 정의하고 있으나, **실제 산업 현장의 대규모 P&ID 도면 적용 시 발생할 수 있는 성능 및 안정성 문제**에 대한 고려가 부족합니다. 특히 실시간 데이터 오버레이의 부하 관리와 분석 결과의 신뢰성 검증 단계가 누락되어 있습니다.
### 2. 주요 진단 항목 및 수정 이유
| 항목 | 진단 결과 | 위험도 | 수정 이유 및 개선 방향 |
|---|---|---|---|
| **데이터 전송 효율** | WebSocket/API 폴링 방식의 단순 나열 | MED | 수천 개의 태그가 포함된 도면에서 개별 폴링/전송 시 네트워크 부하 급증 $\rightarrow$ **태그 그룹화 및 변경분 기반(Delta) 전송** 도입 필요 |
| **프론트엔드 렌더링** | SVG/Canvas 단순 오버레이 | HIGH | 노드 수가 많아질 경우 DOM 요소 증가로 인한 브라우저 랙 발생 $\rightarrow$ **Canvas 기반 렌더링 최적화 및 Viewport 기반 가시 영역 렌더링** 전략 필요 |
| **분석 엔진 신뢰성** | `nx.descendants` 단순 활용 | MED | 단순 위상 전파는 실제 공정의 '흐름 방향(Flow Direction)'과 '밸브 개폐 상태'를 무시함 $\rightarrow$ **엣지 속성(방향성, 상태)을 반영한 가중치 경로 분석**으로 고도화 |
| **에러 핸들링** | Python 브릿지 통신 시 예외 처리 미흡 | LOW | 분석 엔진 다운 시 C# 서버의 블로킹 가능성 $\rightarrow$ **Circuit Breaker 패턴 및 타임아웃 설정** 명시 필요 |
| **사용자 경험(UX)** | 단순 하이라이트 표시 | LOW | 영향도 결과가 많을 경우 도면이 빨간색으로 도배됨 $\rightarrow$ **단계별 영향도(1차, 2차...) 색상 구분 및 필터링** 기능 추가 |
---
## 📦 1. 필수 패키지 및 기술 스택
### 1.1 프론트엔드 (Visualization)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `SVG / Canvas API` | P&ID 도면 렌더링 및 데이터 오버레이 | **Canvas API 우선 권장 (대규모 노드 성능 최적화)** |
| `Cytoscape.js` / `D3.js` | 위상 그래프 시각화 및 인터랙티브 탐색 | 그래프 분석 뷰어 |
| `Vue.js` / `React` | 전체 UI 프레임워크 및 상태 관리 | `src/Web` 구조와 통합 |
| `Axios` / `WebSocket` | 실시간 OPC UA 데이터 수신 및 API 통신 | **SignalR (ASP.NET Core) 도입 권장 (실시간 양방향 통신 최적화)** |
### 1.2 백엔드 (API & Analysis)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `ASP.NET Core` | Graph API 및 분석 엔드포인트 제공 | `ExperionCrawler` 메인 서버 |
| `NetworkX` (Python) | 영향도 분석 및 경로 추적 알고리즘 실행 | 분석 엔진 (Phase 2 활용) |
| `FastAPI` / `Flask` | Python 분석 엔진과 C# 서버 간의 브릿지 | 분석 마이크로서비스 |
---
## 📐 2. 상세 설계 구조
### 2.1 실시간 데이터 오버레이 (Real-time Overlay)
도면의 좌표 정보와 매핑된 시스템 태그를 연결하여 실시간 값을 표시합니다.
1. **매핑 데이터 로드:** `(도면노드ID, 시스템태그, 좌표)` 리스트를 프론트엔드로 전달.
2. **실시간 스트리밍:** `OPC UA` $\rightarrow$ `C# Server` $\rightarrow$ `SignalR Hub` $\rightarrow$ `Frontend`. (**개선: 변경된 값만 전송하는 Delta Update 방식 적용**)
3. **동적 렌더링:** 태그 값이 변경되면 해당 좌표의 Canvas 요소를 업데이트하거나 툴팁에 현재 값을 표시. (**개선: Viewport 내 요소만 업데이트하여 CPU 부하 감소**)
### 2.2 영향도 분석 엔진 (Impact Analysis Engine)
특정 설비의 이상 발생 시 하류(Downstream) 영향을 계산합니다.
1. **분석 요청:** 사용자가 도면에서 특정 노드(예: 펌프 P-101)를 클릭.
2. **그래프 탐색:** Python 분석 엔진에서 `nx.descendants(G, 'P-101')` 실행. (**개선: 엣지의 `flow_direction` 속성을 확인하여 실제 유체 흐름 방향으로만 전파 계산**)
3. **결과 반환:** 영향받는 모든 노드 ID 리스트, 경로(Path), 그리고 **영향 단계(Depth)**를 반환.
4. **시각적 강조:** 도면 상에서 영향 경로를 단계별 색상(예: 1차-진한 빨강, 2차-연한 빨강)으로 하이라이트 처리.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 [Backend] 영향도 분석 API (C# $\rightarrow$ Python Bridge)
```csharp
// src/Web/Controllers/PidGraphController.cs
// 1. 분석 상태 추적을 위한 DTO
public record AnalysisStatus(string taskId, double progress, string status, string message);
// 2. 실시간 진행 상태 조회 API (Phase 5 병렬 처리 반영)
[HttpGet("status/{taskId}")]
public async Task<IActionResult> GetAnalysisStatus(string taskId)
{
// Orchestrator가 관리하는 작업 상태 저장소(Redis/MemoryCache)에서 조회
var status = await _statusService.GetStatusAsync(taskId);
if (status == null) return NotFound();
return Ok(new {
taskId = status.TaskId,
progress = status.Progress, // 0.0 ~ 1.0
status = status.Status, // "Processing", "Completed", "Failed"
message = status.Message
});
}
[HttpGet("impact/{nodeId}")]
public async Task<IActionResult> GetImpactAnalysis(string nodeId)
{
try
{
// Python 분석 마이크로서비스에 요청 (Timeout 및 Circuit Breaker 적용 권장)
var response = await _httpClient.GetAsync($"http://python-analysis-api/impact/{nodeId}");
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ImpactResult>();
return Ok(result);
}
catch (HttpRequestException ex)
{
// 분석 엔진 연결 실패 시 적절한 에러 메시지 반환
return StatusCode(503, new { error = "Analysis Engine is currently unavailable", details = ex.Message });
}
}
```
### 3.2 [Frontend] Canvas 기반 데이터 오버레이 및 진행률 표시 (JavaScript)
```javascript
// src/Web/wwwroot/js/pid-viewer.js
// 1. 실시간 값 업데이트 (Canvas 최적화 버전)
async function updateRealtimeValues(tagData) {
// tagData: { "TAG_01": { value: 10.5, status: "OK" }, ... }
const ctx = canvas.getContext('2d');
for (const [tag, data] of Object.entries(tagData)) {
const node = nodeMap.get(tag); // 좌표 정보 맵
if (node && isInViewport(node)) {
// 뷰포트 내에 있을 때만 렌더링
ctx.fillStyle = data.value > threshold ? 'red' : 'green';
ctx.beginPath();
ctx.arc(node.x, node.y, 5, 0, Math.PI * 2);
ctx.fill();
// 툴팁 데이터 업데이트
updateTooltipData(tag, data.value);
}
}
}
// 2. 분석 진행 상태 표시 (Phase 5 병렬 처리 반영)
async function trackAnalysisProgress(taskId) {
const progressBar = document.getElementById('analysis-progress-bar');
const statusText = document.getElementById('analysis-status-text');
const pollStatus = async () => {
try {
const response = await fetch(`/api/pid/status/${taskId}`);
const data = await response.json();
// 프로그레스 바 업데이트
progressBar.style.width = `${data.progress * 100}%`;
statusText.innerText = `분석 중... ${Math.round(data.progress * 100)}% (${data.message})`;
if (data.status !== 'Completed' && data.status !== 'Failed') {
setTimeout(pollStatus, 1000); // 1초 간격 폴링
} else {
statusText.innerText = data.status === 'Completed' ? '분석 완료!' : '분석 실패';
}
} catch (e) {
statusText.innerText = '상태 조회 중 오류 발생';
}
};
pollStatus();
}
```
### 3.3 [Analysis] 흐름 방향 반영 경로 추적 (Python)
```python
import networkx as nx
def get_propagation_path_with_flow(graph, start_node):
"""
단순 descendants가 아닌, 엣지의 방향성(flow_direction)과
상태(valve_open)를 고려한 실제 영향 전파 경로 추출
"""
# 1. 유효한 엣지만 필터링 (방향이 맞고 밸브가 열려있는 경로)
valid_edges = [
(u, v, d) for u, v, d in graph.edges(data=True)
if d.get('flow_direction') == 'forward' and d.get('valve_status') == 'open'
]
filtered_graph = nx.DiGraph()
filtered_graph.add_edges_from(valid_edges)
# 2. 전파 단계별 노드 추출 (BFS)
propagation_levels = nx.single_source_shortest_path_length(filtered_graph, start_node)
# { node_id: distance } 형태로 반환하여 프론트엔드에서 색상 구분 가능하게 함
return propagation_levels
# 예: P-101에서 시작되는 실제 유체 흐름 기반 영향도 분석
impact_map = get_propagation_path_with_flow(topology_graph, "P-101")
```
---
## 🚀 4. Phase 4 완료 기준 (Definition of Done)
- [ ] P&ID 도면(Canvas) 위에 **실시간 OPC UA 값**이 정확한 좌표에 표시되며, 뷰포트 최적화가 적용되었는가?
- [ ] **SignalR 또는 Delta Update**를 통해 네트워크 부하를 최소화하며 실시간 데이터를 수신하는가?
- [ ] 병렬 처리 중인 분석 작업의 **진행 상태(Progress Bar)**가 UI에 실시간으로 반영되는가?
- [ ] 특정 노드 클릭 시 **유체 흐름 방향이 반영된 영향도 분석** 결과가 단계별 색상으로 하이라이트 되는가?
- [ ] C# 서버와 Python 엔진 간 통신에 **타임아웃 및 예외 처리**가 적용되어 시스템 안정성이 확보되었는가?
- [ ] 전체 파이프라인(`추출 $\rightarrow$ 모델링 $\rightarrow$ 매핑 $\rightarrow$ 시각화`)이 통합되어 동작하는가?

View File

@@ -1,138 +0,0 @@
# 🔌 Graph Pipeline Phase 5: MCP 서버 통합 및 고성능 병렬 아키텍처 (MCP Integration & Parallel Processing)
이 문서는 앞서 설계한 1~4단계의 Graph Pipeline을 현재 프로젝트의 **Unified MCP Server (`mcp-server/server.py`)**에 통합하는 방안을 다룹니다. 특히, 대용량 도면 처리 시 발생하는 지연과 버퍼 문제를 해결하기 위해 `PID_Parser_Plan_Revision.md`의 **분산 처리 기법**과 vLLM의 **Continuous Batching** 특성을 극대화한 병렬 아키텍처를 적용합니다.
---
## 🏗️ 1. 통합 아키텍처 설계
### 1.1 고성능 병렬 데이터 흐름 (Parallel End-to-End Flow)
단일 순차 요청 방식에서 벗어나, **[전처리 $\rightarrow$ 병렬 분산 추출 $\rightarrow$ 통합 후처리]** 구조로 전환합니다.
`Frontend (UI)` $\rightarrow$ `C# Server (API)` $\rightarrow$ `MCP Server (Orchestrator)` $\rightarrow$ `Parallel Worker Tools (vLLM Batching)` $\rightarrow$ `Result Aggregator` $\rightarrow$ `C# Server`
1. **요청:** 사용자가 UI에서 도면 분석 시작 버튼 클릭.
2. **전처리 (Orchestrator):** MCP 서버가 DXF를 로드하여 기하학적 데이터를 추출하고, 분석 대상(Transmitter, Valve, Pump 등)별로 데이터를 분할합니다.
3. **병렬 호출 (Continuous Batching):**
* 분할된 데이터를 기반으로 여러 개의 MCP 툴(또는 동일 툴의 다중 요청)을 **동시에(Asynchronously)** 호출합니다.
* vLLM 서버는 이 다수의 요청을 **Continuous Batching**으로 묶어 처리함으로써, 개별 요청 시보다 전체 처리량(Throughput)을 획기적으로 높입니다.
4. **통합 및 저장 (Aggregator):** 각 분산 툴이 반환한 결과를 취합하여 최종 위상 그래프를 구축하고 DB에 저장합니다.
### 1.2 MCP 서버 내 역할 분담 (분산 처리 모델)
`PID_Parser_Plan_Revision.md`를 반영하여, 기능을 세분화하고 병렬 실행 가능하게 설계합니다.
| 구분 | MCP Tool / Module | 역할 | 병렬 처리 전략 |
|---|---|---|---|
| **Orchestrator** | `orchestrate_pid_pipeline` | 전체 공정 제어, 데이터 분할 및 결과 취합 | Asyncio 기반 비동기 제어 |
| **Worker 1** | `extract_transmitters` | FIT, FT, LT, PT, TE 추출 | vLLM Batching 요청 |
| **Worker 2** | `extract_valves` | FCV, LCV, TCV, PCV, XV 추출 | vLLM Batching 요청 |
| **Worker 3** | `extract_gauges` | PG, TG, LG 추출 | vLLM Batching 요청 |
| **Worker 4** | `extract_equipment` | Column, Tank, Filter, Drum, Heat Exchanger 등 추출 | vLLM Batching 요청 |
| **Worker 5** | `extract_pumps` | P-xxxx, VP-xxxx 추출 | vLLM Batching 요청 |
| **Analyzer** | `analyze_pid_impact` | 구축된 그래프 기반 영향도 분석 | Graph Algorithm (CPU) |
---
## 💻 2. MCP 서버 통합 구현 가이드
### 2.1 비동기 병렬 처리 설계 (Asyncio + vLLM Batching)
`FastMCP` 환경에서 `asyncio.gather`를 사용하여 여러 추출 툴을 동시에 호출함으로써 vLLM의 Continuous Batching 효율을 극대화합니다.
```python
# mcp-server/server.py 통합 설계 (개념 코드)
import asyncio
from typing import List
async def run_parallel_extraction(geo_data):
"""
분류별 추출 툴을 병렬로 호출하여 vLLM Batching 유도
"""
# 각 분류별 프롬프트와 데이터 준비
tasks = [
extract_transmitters_async(geo_data),
extract_valves_async(geo_data),
extract_gauges_async(geo_data),
extract_equipment_async(geo_data),
extract_pumps_async(geo_data)
]
# 동시에 요청을 던져 vLLM이 내부적으로 Batch 처리하게 함
results = await asyncio.gather(*tasks)
return results
@mcp.tool()
async def build_pid_graph_parallel(filepath: str) -> str:
"""
분산 처리 기법을 적용한 P&ID 그래프 생성 툴
"""
# 1. 전처리 (Phase 1)
extractor = PidGeometricExtractor(filepath)
geo_data = extractor.extract_all()
# 2. 병렬 분산 추출 (vLLM Batching 활용)
# 각 Worker 툴들이 LLM에 요청을 보낼 때 vLLM이 이를 묶어서 처리함
extracted_parts = await run_parallel_extraction(geo_data)
# 3. 결과 통합 및 위상 모델링 (Phase 2)
all_tags = flatten_results(extracted_parts)
builder = PidTopologyBuilder(geo_data, all_tags)
builder.build_graph()
# 4. 저장
graph_id = os.path.basename(filepath).replace(".dxf", "_graph.json")
nx.write_graphml(builder.G, f"storage/{graph_id}")
return json.dumps({"success": True, "graph_id": graph_id, "nodes": builder.G.number_of_nodes()})
```
### 2.2 C# 서버와의 인터페이스 (`McpClient` 활용)
C# 서버는 `src/Infrastructure/Mcp/McpClient.cs`를 통해 위 툴들을 호출합니다.
### 2.2 C# 서버와의 인터페이스 (`McpClient` 활용)
C# 서버는 `src/Infrastructure/Mcp/McpClient.cs`를 통해 위 툴들을 호출합니다.
```csharp
// src/Core/Application/Services/PidGraphService.cs (신규 서비스)
public async Task<ImpactResult> GetImpactAnalysisAsync(string graphId, string nodeId)
{
var request = new McpToolRequest {
ToolName = "analyze_pid_impact",
Arguments = new { graph_id = graphId, start_node_id = nodeId }
};
var jsonResponse = await _mcpClient.CallToolAsync(request);
return JsonSerializer.Deserialize<ImpactResult>(jsonResponse);
}
```
---
## 🛠️ 3. 프로그램 구성 및 배포 전략
### 3.1 디렉토리 구조 확장
```text
mcp-server/
├── server.py # MCP 메인 서버 (툴 정의)
├── pipeline/ # Graph Pipeline 핵심 로직 (Phase 1~4)
│ ├── __init__.py
│ ├── extractor.py # Phase 1: Geometric Extraction
│ ├── topology.py # Phase 2: Topology Modeling
│ ├── mapper.py # Phase 3: Intelligent Mapping
│ └── analyzer.py # Phase 4: Impact Analysis
└── storage/ # 생성된 그래프 파일 (.graphml) 저장소
```
### 3.2 실행 프로세스
1. **MCP 서버 기동:** `python mcp-server/server.py --http` (포트 5001)
2. **C# 서버 기동:** `dotnet run` (포트 5000)
3. **통신:** C# 서버 $\xrightarrow{HTTP/JSON}$ MCP 서버 $\xrightarrow{Python\ Libs}$ 결과 반환.
---
## 🚀 4. 최종 완료 기준 (Definition of Done)
- [ ] `mcp-server/server.py``build_pid_graph`, `analyze_pid_impact` 등 핵심 툴이 정의되었는가?
- [ ] Phase 1~4의 Python 로직이 `mcp-server/pipeline/` 모듈로 구조화되어 통합되었는가?
- [ ] C# `McpClient`를 통해 MCP 서버의 그래프 분석 툴을 호출하고 결과를 수신할 수 있는가?
- [ ] 도면 업로드 $\rightarrow$ 그래프 생성 $\rightarrow$ 태그 매핑 $\rightarrow$ 영향도 분석으로 이어지는 **End-to-End 파이프라인**이 완성되었는가?
- [ ] 모든 과정이 `json_response=True``stateless_http=True` 설정 하에 안정적으로 동작하는가?

View File

@@ -1,46 +0,0 @@
P&ID 도면 분석을 고도화하여 **Graph Pipeline**을 구축하는 것은 단순한 텍스트 추출을 넘어, 설비 간의 **연결성(Connectivity)**과 **위상(Topology)**을 이해하는 것을 의미합니다.
제가 이 작업을 수행한다면, 다음과 같은 **4단계 전략**으로 접근하여 효율성을 극대화하겠습니다.
---
### 1. 데이터 추출 단계: "단순 텍스트 $\rightarrow$ 기하학적 객체"
현재의 텍스트 기반 추출에서 벗어나, 객체의 **좌표(Coordinate)**와 **속성(Property)**을 보존하는 구조로 변경해야 합니다.
* **객체 중심 파싱:** DXF의 Entity(Line, Circle, Text, Polyline)를 개별 객체로 인식하고, 각 객체의 중심점과 경계 상자(Bounding Box)를 저장합니다.
* **심볼 라이브러리 구축:** 밸브, 펌프, 탱크 등 반복되는 심볼의 기하학적 패턴을 정의하여, 텍스트가 없어도 "이 모양은 밸브다"라고 인식하는 패턴 매칭 로직을 도입합니다.
* **OCR 고도화:** PDF의 경우, 단순 텍스트 추출이 아닌 영역 기반 OCR을 통해 텍스트의 물리적 위치를 정확히 파악하여 인접한 심볼과 연결합니다.
### 2. 그래프 모델링 단계: "객체 $\rightarrow$ 노드 및 엣지"
추출된 객체들을 기반으로 **Knowledge Graph**를 생성합니다.
* **노드(Node):** 설비(Equipment), 계기(Instrument), 태그(Tag)를 노드로 정의합니다.
* **엣지(Edge):** 배관(Line)을 엣지로 정의합니다.
* **연결성 판단:** `Line`의 끝점이 `Equipment`의 경계 상자 내에 있거나 매우 근접해 있다면 두 노드를 연결된 것으로 간주합니다.
* **방향성 부여:** 화살표 심볼이나 공정 흐름(Flow)을 분석하여 엣지에 방향성을 부여합니다.
* **계층 구조 생성:** `Unit $\rightarrow$ Equipment $\rightarrow$ Component $\rightarrow$ Tag` 순의 계층적 그래프 구조를 설계합니다.
### 3. 지능형 매핑 및 검증 단계: "도면 $\rightarrow$ 실제 데이터"
그래프 구조를 활용해 Experion 시스템의 실제 태그와 정밀하게 매핑합니다.
* **맥락 기반 매핑 (Contextual Mapping):** 단순히 이름이 비슷한 태그를 찾는 것이 아니라, "펌프 P-101 옆에 있는 PT-101은 P-101의 압력 전송기일 확률이 높다"는 그래프 상의 인접성을 활용합니다.
* **상호 검증 (Cross-Validation):**
* 도면 상의 연결 관계(P-101 $\rightarrow$ V-101)와 실제 공정 데이터의 상관관계(P-101 가동 시 V-101 유량 변화)를 비교하여 매핑의 정확도를 검증합니다.
* **LLM 기반 추론:** 모호한 태그명이나 누락된 정보는 MCP 서버를 통해 LLM이 도면의 맥락과 R530 문서를 분석하여 최적의 매핑 후보를 추천하게 합니다.
### 4. 활용 및 시각화 단계: "분석 $\rightarrow$ 인사이트"
구축된 그래프를 통해 운영자에게 실질적인 가치를 제공합니다.
* **영향도 분석 (Impact Analysis):** 특정 밸브(V-101)가 고장 났을 때, 그래프 탐색(BFS/DFS)을 통해 하류(Downstream)에 영향을 받는 모든 설비와 태그를 즉시 식별합니다.
* **디지털 트윈 뷰:** P&ID 도면 위에 실시간 OPC UA 값을 오버레이하여, 도면을 보면서 현재 공정 상태를 한눈에 파악하는 인터페이스를 구현합니다.
* **이상 징후 전파 경로 추적:** 특정 태그에서 알람이 발생했을 때, 그래프를 역추적하여 근본 원인(Root Cause)이 될 가능성이 높은 상류 설비를 추천합니다.
---
### 🚀 효율적인 실행을 위한 로드맵 (Priority)
1. **Short-term (Quick Win):** DXF 파서 수정 $\rightarrow$ 객체 좌표 저장 $\rightarrow$ 단순 인접성 기반 태그-설비 매핑.
2. **Mid-term (Core):** 심볼 패턴 매칭 도입 $\rightarrow$ 배관(Line) 기반의 그래프 구조(NetworkX 등 활용) 구축.
3. **Long-term (Advanced):** LLM 기반의 도면-데이터 추론 엔진 통합 $\rightarrow$ 실시간 데이터 오버레이 UI 구현.
이렇게 **[기하학적 추출 $\rightarrow$ 위상 모델링 $\rightarrow$ 맥락적 매핑 $\rightarrow$ 운영 인사이트]** 순으로 확장하는 것이 가장 리스크가 적고 효율적인 방법이라고 생각합니다.

View File

@@ -1,122 +0,0 @@
# 🔌 Graph Pipeline Phase 5: MCP 서버 통합 및 시스템 아키텍처 (MCP Integration)
이 문서는 앞서 설계한 1~4단계의 Graph Pipeline을 현재 프로젝트의 **Unified MCP Server (`mcp-server/server.py`)**에 통합하는 방안과 최종 프로그램 구조를 다룹니다. 이를 통해 C# 메인 서버와 LLM, 그리고 도면 분석 엔진이 하나의 생태계에서 유기적으로 동작하게 합니다.
---
## 🏗️ 1. 통합 아키텍처 설계
### 1.1 전체 데이터 흐름 (End-to-End Flow)
`Frontend (UI)` $\rightarrow$ `C# Server (API)` $\rightarrow$ `MCP Server (Python)` $\rightarrow$ `Graph Pipeline Engine` $\rightarrow$ `Experion DB/OPC UA`
1. **요청:** 사용자가 UI에서 "P-101 펌프의 영향도 분석" 요청.
2. **중계:** C# 서버가 `McpClient`를 통해 MCP 서버의 `analyze_pid_impact` 툴 호출.
3. **분석:** MCP 서버는 내부적으로 `NetworkX` 그래프를 로드하여 하류 노드를 계산.
4. **응답:** 분석 결과(노드 리스트)를 JSON으로 반환 $\rightarrow$ C# 서버 $\rightarrow$ UI 하이라이트.
### 1.2 MCP 서버 내 역할 분담
현재 `server.py`는 RAG, NL2SQL, 단순 태그 추출 기능을 가지고 있습니다. 여기에 **Graph Pipeline 전용 도구 세트**를 추가합니다.
| 기존 기능 | 추가될 Graph Pipeline 기능 | 역할 |
|---|---|---|
| `parse_pid_dxf` | `build_pid_graph` | DXF $\rightarrow$ 기하 추출 $\rightarrow$ 위상 그래프 생성 및 저장 |
| `match_pid_tags` | `resolve_graph_tags` | 그래프 맥락을 반영한 지능형 태그 매핑 |
| (신규) | `analyze_pid_impact` | 특정 노드 기준 영향도 분석 (Downstream 탐색) |
| (신규) | `get_graph_topology` | 시각화를 위한 노드-엣지 리스트 반환 |
---
## 💻 2. MCP 서버 통합 구현 가이드
### 2.1 MCP Tool 캡슐화 설계
`mcp-server/server.py`에 다음과 같은 형태로 툴을 추가합니다.
```python
# mcp-server/server.py 에 추가될 내용 (개념 코드)
@mcp.tool()
def build_pid_graph(filepath: str) -> str:
"""
P&ID 도면을 분석하여 위상 그래프를 생성하고 저장합니다.
Phase 1(기하 추출) + Phase 2(위상 모델링) 통합 실행.
"""
# 1. Phase 1: Geometric Extraction
extractor = PidGeometricExtractor(filepath)
geo_data = extractor.extract_all()
# 2. Phase 2: Topology Modeling
builder = PidTopologyBuilder(geo_data)
builder.build_graph()
# 3. 그래프 저장 (GraphML 또는 JSON)
graph_id = os.path.basename(filepath).replace(".dxf", "_graph.json")
nx.write_graphml(builder.G, f"storage/{graph_id}")
return json.dumps({"success": True, "graph_id": graph_id, "nodes": builder.G.number_of_nodes()})
@mcp.tool()
def analyze_pid_impact(graph_id: str, start_node_id: str) -> str:
"""
특정 설비의 장애 시 영향을 받는 하류 설비 리스트를 반환합니다.
"""
# 그래프 로드
G = nx.read_graphml(f"storage/{graph_id}")
# 영향도 분석 (Phase 4 로직)
impacted = nx.descendants(G, start_node_id)
return json.dumps({
"success": True,
"start_node": start_node_id,
"impacted_nodes": list(impacted)
})
```
### 2.2 C# 서버와의 인터페이스 (`McpClient` 활용)
C# 서버는 `src/Infrastructure/Mcp/McpClient.cs`를 통해 위 툴들을 호출합니다.
```csharp
// src/Core/Application/Services/PidGraphService.cs (신규 서비스)
public async Task<ImpactResult> GetImpactAnalysisAsync(string graphId, string nodeId)
{
var request = new McpToolRequest {
ToolName = "analyze_pid_impact",
Arguments = new { graph_id = graphId, start_node_id = nodeId }
};
var jsonResponse = await _mcpClient.CallToolAsync(request);
return JsonSerializer.Deserialize<ImpactResult>(jsonResponse);
}
```
---
## 🛠️ 3. 프로그램 구성 및 배포 전략
### 3.1 디렉토리 구조 확장
```text
mcp-server/
├── server.py # MCP 메인 서버 (툴 정의)
├── pipeline/ # Graph Pipeline 핵심 로직 (Phase 1~4)
│ ├── __init__.py
│ ├── extractor.py # Phase 1: Geometric Extraction
│ ├── topology.py # Phase 2: Topology Modeling
│ ├── mapper.py # Phase 3: Intelligent Mapping
│ └── analyzer.py # Phase 4: Impact Analysis
└── storage/ # 생성된 그래프 파일 (.graphml) 저장소
```
### 3.2 실행 프로세스
1. **MCP 서버 기동:** `python mcp-server/server.py --http` (포트 5001)
2. **C# 서버 기동:** `dotnet run` (포트 5000)
3. **통신:** C# 서버 $\xrightarrow{HTTP/JSON}$ MCP 서버 $\xrightarrow{Python\ Libs}$ 결과 반환.
---
## 🚀 4. 최종 완료 기준 (Definition of Done)
- [ ] `mcp-server/server.py``build_pid_graph`, `analyze_pid_impact` 등 핵심 툴이 정의되었는가?
- [ ] Phase 1~4의 Python 로직이 `mcp-server/pipeline/` 모듈로 구조화되어 통합되었는가?
- [ ] C# `McpClient`를 통해 MCP 서버의 그래프 분석 툴을 호출하고 결과를 수신할 수 있는가?
- [ ] 도면 업로드 $\rightarrow$ 그래프 생성 $\rightarrow$ 태그 매핑 $\rightarrow$ 영향도 분석으로 이어지는 **End-to-End 파이프라인**이 완성되었는가?
- [ ] 모든 과정이 `json_response=True``stateless_http=True` 설정 하에 안정적으로 동작하는가?

View File

@@ -1,364 +0,0 @@
using ExperionCrawler.Core.Application.DTOs;
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Infrastructure.Mcp;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace ExperionCrawler.Web.Controllers;
/// <summary>
/// Text-to-SQL API 컨트롤러
/// 자연어 질의를 파싱하고 시계열 데이터를 조회합니다.
/// MCP (Model Context Protocol) 통합을 위한 엔드포인트를 제공합니다.
/// </summary>
[ApiController]
[Route("api/text-to-sql")]
public class TextToSqlController : ControllerBase
{
private readonly ITextToSqlService _textToSqlService;
private readonly IExperionDbService _dbService;
private readonly IMcpService _mcpService;
private readonly ILogger<TextToSqlController> _logger;
public TextToSqlController(
ITextToSqlService textToSqlService,
IExperionDbService dbService,
IMcpService mcpService,
ILogger<TextToSqlController> logger)
{
_textToSqlService = textToSqlService;
_dbService = dbService;
_mcpService = mcpService;
_logger = logger;
}
/// <summary>
/// 자연어 질의를 SQL로 변환
/// </summary>
[HttpPost("parse")]
public async Task<IActionResult> Parse([FromBody] NaturalLanguageQueryDto dto)
{
try
{
var sql = await _textToSqlService.ParseNaturalLanguageAsync(dto.Query);
return Ok(new { success = true, sql });
}
catch (Exception ex)
{
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP query_with_nl 도구 호출 - 자연어 → LLM SQL 생성 → 실행
/// </summary>
[HttpPost("query-nl")]
public async Task<IActionResult> QueryWithNl([FromBody] NaturalLanguageQueryDto dto)
{
if (string.IsNullOrWhiteSpace(dto.Query))
return BadRequest(new { success = false, error = "질문이 비어있음" });
try
{
var result = await _mcpService.QueryWithNlAsync(dto.Query);
if (!result.Success)
return Ok(new { success = false, error = result.Error });
try
{
var jsonData = System.Text.Json.JsonSerializer.Deserialize<object>(result.Data!);
return Ok(new { success = true, data = jsonData });
}
catch
{
return Ok(new { success = true, data = result.Data });
}
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] query-nl 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP 도구 목록 조회
/// </summary>
[HttpGet("tools")]
public async Task<IActionResult> ListTools()
{
try
{
var tools = await _mcpService.ListToolsAsync();
return Ok(new { success = true, tools });
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] 도구 목록 조회 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP run_sql 도구 호출 - SQL 실행
/// Text-to-SQL 엔진으로 생성된 SQL을 안전하게 실행
/// </summary>
[HttpPost("execute-mcp")]
public async Task<IActionResult> ExecuteFromMcp([FromBody] SqlQueryDto dto)
{
if (string.IsNullOrWhiteSpace(dto.Sql))
{
return BadRequest(new { success = false, error = "SQL이 비어있음" });
}
try
{
// MCP run_sql 도구 호출
var result = await _mcpService.RunSqlAsync(dto.Sql);
if (!result.Success)
{
return Ok(new
{
success = false,
error = result.Error
});
}
// JSON 결과 반환 (쿼리 결과)
try
{
var jsonData = System.Text.Json.JsonSerializer.Deserialize<object>(result.Data);
return Ok(new { success = true, data = jsonData });
}
catch
{
return Ok(new { success = true, data = result.Data });
}
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] MCP 실행 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP query_pv_history 도구 호출 - 과거 값 히스토리 조회
/// </summary>
[HttpPost("query-history")]
public async Task<IActionResult> QueryHistory([FromBody] HistoryQueryRequestDto dto)
{
try
{
var tagNames = dto.TagNames ?? [];
var timeFrom = dto.From ?? DateTime.UtcNow.AddDays(-1).ToString("o");
var timeTo = dto.To ?? DateTime.UtcNow.ToString("o");
var limit = dto.Limit ?? 100;
var result = await _mcpService.QueryPvHistoryAsync(
tagNames,
timeFrom,
timeTo,
limit
);
if (!result.Success)
{
return Ok(new
{
success = false,
error = result.Error
});
}
try
{
var jsonData = System.Text.Json.JsonSerializer.Deserialize<object>(result.Data);
return Ok(new
{
success = true,
data = jsonData
});
}
catch
{
return Ok(new
{
success = true,
data = result.Data
});
}
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] History 쿼리 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP get_tag_metadata 도구 호출 - 태그 메타데이터 검색
/// </summary>
[HttpGet("tags/search")]
public async Task<IActionResult> SearchTags([FromQuery] string query, [FromQuery] int? limit)
{
try
{
var tagLimit = limit ?? 10;
var result = await _mcpService.GetTagMetadataAsync(query, tagLimit);
if (!result.Success)
{
return Ok(new
{
success = false,
error = result.Error
});
}
try
{
var jsonData = System.Text.Json.JsonSerializer.Deserialize<object>(result.Data);
return Ok(new
{
success = true,
data = jsonData
});
}
catch
{
return Ok(new
{
success = true,
data = result.Data
});
}
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] 태그 검색 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// MCP list_drawings 도구 호출 - 도면 목록 조회
/// </summary>
[HttpGet("drawings")]
public async Task<IActionResult> ListDrawings([FromQuery] string? unitNo)
{
try
{
var result = await _mcpService.ListDrawingsAsync(unitNo);
if (!result.Success)
{
return Ok(new
{
success = false,
error = result.Error
});
}
try
{
var jsonData = System.Text.Json.JsonSerializer.Deserialize<object>(result.Data);
return Ok(new
{
success = true,
data = jsonData
});
}
catch
{
return Ok(new
{
success = true,
data = result.Data
});
}
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] 도면 목록 조회 실패");
return Ok(new { success = false, error = ex.Message });
}
}
/// <summary>
/// 쿼리 제안 (자동 완성)
/// </summary>
[HttpGet("suggest")]
public async Task<IActionResult> Suggest([FromQuery] string input = "")
{
var suggestions = await _textToSqlService.SuggestQueriesAsync(input);
return Ok(new { success = true, suggestions });
}
/// <summary>
/// 시계열 분석 (평균, 최대, 최소, 추세)
/// </summary>
[HttpPost("analyze")]
public async Task<IActionResult> Analyze([FromBody] AnalyzeRequestDto dto)
{
var result = await _textToSqlService.AnalyzeAsync(dto);
return Ok(new {
success = result.Success,
error = result.Error,
tags = result.Tags?.Select(t => new {
tagName = t.TagName,
avg = t.Avg,
mean = t.Mean,
min = t.Min,
max = t.Max,
first = t.First,
last = t.Last,
pointCount = t.PointCount,
stddev = t.StdDev,
from = t.From,
to = t.To
}).ToList()
});
}
/// <summary>
/// 사용자 지정 간격으로 history 이력 조회
/// history_table의 기본 저장 간격(60초)을 기반으로 사용자가 요청한 간격으로 데이터 집계
/// </summary>
[HttpPost("query-history-interval")]
public async Task<IActionResult> QueryHistoryInterval([FromBody] HistoryIntervalQueryRequestDto dto)
{
try
{
var request = new HistoryIntervalQueryRequest(
dto.TagNames,
dto.From,
dto.To,
dto.Interval,
dto.Limit);
var result = await _dbService.QueryHistoryWithIntervalAsync(request);
var response = new
{
success = true,
tagNames = result.TagNames.ToList(),
rows = result.Rows.Select(r => new
{
timeBucket = r.TimeBucket,
values = r.Values
}).ToList(),
baseIntervalSeconds = result.BaseIntervalSeconds,
queryInterval = result.QueryInterval
};
return Ok(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "[TextToSql] QueryHistoryInterval 실패");
return StatusCode(StatusCodes.Status500InternalServerError, new { success = false, error = ex.Message });
}
}
}

View File

@@ -1,906 +0,0 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<title>ExperionCrawler</title>
<link rel="stylesheet" href="/css/style.css"/>
<link rel="stylesheet" href="/lib/uPlot.min.css"/>
</head>
<body>
<div class="shell">
<!-- ── Sidebar ───────────────────────────────────────────── -->
<nav class="sidebar">
<div class="brand">
<svg class="brand-icon" viewBox="0 0 40 40" fill="none">
<rect x="4" y="4" width="14" height="14" rx="2" stroke="currentColor" stroke-width="1.5"/>
<rect x="22" y="4" width="14" height="14" rx="2" stroke="currentColor" stroke-width="1.5"/>
<rect x="4" y="22" width="14" height="14" rx="2" stroke="currentColor" stroke-width="1.5"/>
<rect x="22" y="22" width="14" height="14" rx="2" stroke="currentColor" stroke-width="1.5"/>
<circle cx="11" cy="11" r="3" fill="currentColor" opacity=".6"/>
<circle cx="29" cy="11" r="3" fill="currentColor" opacity=".6"/>
<circle cx="11" cy="29" r="3" fill="currentColor" opacity=".6"/>
<circle cx="29" cy="29" r="3" fill="currentColor" opacity="1"/>
</svg>
<div>
<div class="brand-name">EXPERION</div>
<div class="brand-sub">CRAWLER v1.0</div>
</div>
</div>
<ul class="nav">
<li class="nav-item active" data-tab="cert">
<span class="ni">01</span>
<span class="nl">인증서 관리</span>
<span class="nb" id="cert-dot"></span>
</li>
<li class="nav-item" data-tab="conn">
<span class="ni">02</span>
<span class="nl">서버 접속 테스트</span>
</li>
<li class="nav-item" data-tab="crawl">
<span class="ni">03</span>
<span class="nl">데이터 크롤링</span>
</li>
<li class="nav-item" data-tab="db">
<span class="ni">04</span>
<span class="nl">DB 저장</span>
</li>
<li class="nav-item" data-tab="nm-dash">
<span class="ni">05</span>
<span class="nl">노드맵 대시보드</span>
</li>
<li class="nav-item" data-tab="pb">
<span class="ni">06</span>
<span class="nl">포인트빌더</span>
</li>
<li class="nav-item" data-tab="hist">
<span class="ni">07</span>
<span class="nl">이력 조회</span>
</li>
<li class="nav-item" data-tab="opcsvr">
<span class="ni">08</span>
<span class="nl">OPC UA 서버</span>
<span class="nb" id="opcsvr-dot"></span>
</li>
<li class="nav-item" data-tab="t2s">
<span class="ni">09</span>
<span class="nl">Text-to-SQL</span>
</li>
<li class="nav-item" data-tab="fast">
<span class="ni">10</span>
<span class="nl">fastRecord</span>
</li>
</ul>
<div class="sb-foot">
<span class="dot" id="g-dot"></span>
<span id="g-txt" class="mono">READY</span>
</div>
</nav>
<!-- ── Main ──────────────────────────────────────────────── -->
<main class="content">
<!-- ══════════════════════════════════════════════════════
01 인증서 관리
═══════════════════════════════════════════════════════ -->
<section class="pane active" id="pane-cert">
<header class="pane-hdr">
<div>
<h1>인증서 관리</h1>
<p>OPC UA 클라이언트 인증서를 생성합니다. 기존 파일이 있으면 재사용됩니다.</p>
</div>
<div class="pane-tag">PKI / X.509</div>
</header>
<div class="cols-2">
<div class="card">
<div class="card-cap">인증서 생성</div>
<div class="fg">
<label>Client Hostname</label>
<input id="c-host" class="inp" value="dbsvr"/>
</div>
<div class="fg">
<label>Subject Alt Names <em>(쉼표 구분)</em></label>
<input id="c-san" class="inp" value="localhost,192.168.0.50"/>
</div>
<div class="fg">
<label>PFX Password <em>(없으면 비워 두세요)</em></label>
<input id="c-pw" class="inp" type="password" placeholder=""/>
</div>
<button class="btn-a" onclick="certCreate()">🔑 인증서 생성</button>
</div>
<div class="card">
<div class="card-cap">현재 인증서 상태</div>
<button class="btn-b" onclick="certStatus()" style="margin-bottom:14px">상태 확인</button>
<div id="cert-disp" class="kv-box">
<span class="placeholder">상태 확인 버튼을 눌러 주세요</span>
</div>
</div>
</div>
<div id="cert-log" class="logbox hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
02 서버 접속 테스트
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-conn">
<header class="pane-hdr">
<div>
<h1>서버 접속 테스트</h1>
<p>Experion OPC UA 서버에 연결하고 노드 값을 읽습니다.</p>
</div>
<div class="pane-tag">OPC UA / TCP</div>
</header>
<div class="card" style="margin-bottom:18px">
<div class="card-cap">서버 설정</div>
<div class="cols-3">
<div class="fg"><label>Server IP</label>
<input id="x-server" class="inp" value="192.168.0.20"/></div>
<div class="fg"><label>Port</label>
<input id="x-port" class="inp" type="number" value="4840"/></div>
<div class="fg"><label>Client Hostname</label>
<input id="x-client" class="inp" value="dbsvr"/></div>
<div class="fg"><label>Username</label>
<input id="x-user" class="inp" value="mngr"/></div>
<div class="fg"><label>Password</label>
<input id="x-pass" class="inp" type="password" value="mngr"/></div>
</div>
<div class="btn-row">
<button class="btn-a" onclick="connTest()">🔌 접속 테스트</button>
<button class="btn-b" onclick="connBrowse()">🌲 노드 탐색</button>
</div>
</div>
<div class="card">
<div class="card-cap">단일 태그 읽기</div>
<div class="row-inp">
<input id="x-node" class="inp flex1"
value="ns=1;s=sinamserver:p-6102.hzset.fieldvalue"
placeholder="ns=1;s=..."/>
<button class="btn-b" onclick="connRead()">읽기</button>
</div>
<div id="tag-box" class="tag-box hidden"></div>
</div>
<div id="conn-log" class="logbox hidden"></div>
<div id="browse-wrap" class="bwrap hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
03 데이터 크롤링
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-crawl">
<header class="pane-hdr">
<div>
<h1>데이터 크롤링</h1>
<p>지정한 노드 값을 주기적으로 수집하여 CSV 파일로 저장합니다.</p>
</div>
<div class="pane-tag">CRAWL / CSV</div>
</header>
<div class="cols-2">
<div class="card">
<div class="card-cap">서버 설정</div>
<div class="fg"><label>Server IP</label>
<input id="w-server" class="inp" value="192.168.0.20"/></div>
<div class="fg"><label>Port</label>
<input id="w-port" class="inp" type="number" value="4840"/></div>
<div class="fg"><label>Client Hostname</label>
<input id="w-client" class="inp" value="dbsvr"/></div>
<div class="fg"><label>Username</label>
<input id="w-user" class="inp" value="mngr"/></div>
<div class="fg"><label>Password</label>
<input id="w-pass" class="inp" type="password" value="mngr"/></div>
<div class="fg"><label>수집 간격 (초)</label>
<input id="w-interval" class="inp" type="number" value="1" min="1"/></div>
<div class="fg"><label>수집 시간 (초)</label>
<input id="w-duration" class="inp" type="number" value="30" min="1"/></div>
</div>
<div class="card">
<div class="card-cap">수집 노드 목록 <em>(한 줄에 하나씩)</em></div>
<textarea id="w-nodes" class="ta" rows="9"
placeholder="ns=1;s=...">ns=1;s=sinamserver:p-6102.hzset.fieldvalue</textarea>
<button class="btn-a" id="crawl-btn" onclick="crawlStart()"
style="margin-top:14px">📡 크롤링 시작</button>
</div>
</div>
<div id="crawl-prog" class="prog-wrap hidden">
<div class="prog-hdr">
<span id="crawl-ptxt">수집 중...</span>
<span id="crawl-cnt" class="mono">0</span>
</div>
<div class="prog-track"><div id="crawl-bar" class="prog-fill" style="width:0%"></div></div>
</div>
<div id="crawl-log" class="logbox hidden"></div>
<!-- ── 노드맵 수집 ──────────────────────────────────────── -->
<div class="section-div"></div>
<header class="pane-hdr" style="margin-bottom:16px">
<div>
<h2 class="sub-hdr">노드맵 수집</h2>
<p>서버 전체 노드를 재귀 탐색하여 AssetLoader 용 CSV 파일로 저장합니다.</p>
</div>
<div class="pane-tag">NODE MAP / CSV</div>
</header>
<div class="card">
<div class="card-cap">전체 노드 탐색 설정</div>
<div class="nm-row">
<div class="fg" style="margin-bottom:0;width:200px">
<label>최대 탐색 깊이</label>
<input id="nm-depth" class="inp" type="number" value="10" min="1" max="20"/>
</div>
<button class="btn-a" id="nm-btn" onclick="nodeMapCrawl()">🗺 전체 노드맵 수집</button>
</div>
<p class="nm-hint">
서버 설정은 위 크롤링 설정을 그대로 사용합니다 &nbsp;·&nbsp;
노드 수에 따라 수 분이 소요될 수 있습니다 &nbsp;·&nbsp;
결과는 <code>data/csv/{서버명}_*.csv</code> 에 저장됩니다
</p>
</div>
<div id="nm-prog" class="prog-wrap hidden">
<div class="prog-hdr">
<span id="nm-ptxt">탐색 중...</span>
<span id="nm-cnt" class="mono"></span>
</div>
<div class="prog-track"><div id="nm-bar" class="prog-fill" style="width:0%"></div></div>
</div>
<div id="nm-log" class="logbox hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
04 DB 저장
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-db">
<header class="pane-hdr">
<div>
<h1>DB 저장</h1>
<p>수집된 CSV 파일을 PostgreSQL DB에 저장하고 레코드를 조회합니다.</p>
</div>
<div class="pane-tag">PostgreSQL / EF</div>
</header>
<div class="cols-2">
<div class="card">
<div class="card-cap">CSV → DB 임포트</div>
<button class="btn-b" onclick="dbLoadFiles()" style="margin-bottom:10px">
🔄 파일 목록 갱신
</button>
<div id="file-list" class="flist">
<span class="placeholder">갱신 버튼을 눌러 주세요</span>
</div>
<div class="fg" style="margin-top:12px">
<label>선택된 파일</label>
<input id="sel-csv" class="inp" readonly placeholder="위 목록에서 파일을 선택하세요"/>
</div>
<div class="fg">
<label>저장 방식</label>
<div class="mode-group">
<label class="mode-opt">
<input type="radio" name="import-mode" value="append" checked/>
<span>추가 저장</span>
</label>
<label class="mode-opt mode-opt-danger">
<input type="radio" name="import-mode" value="truncate"/>
<span>초기화 후 저장</span>
</label>
</div>
</div>
<button class="btn-a" onclick="dbImport()">💾 DB에 저장</button>
</div>
<div class="card">
<div class="card-cap">DB 레코드 조회</div>
<div class="row-inp" style="margin-bottom:12px">
<input id="db-limit" class="inp" type="number" value="100"
min="1" max="10000" style="width:110px"/>
<button class="btn-b" onclick="dbQuery()">조회</button>
</div>
<div id="db-stats" class="stats hidden"></div>
</div>
</div>
<div id="db-log" class="logbox hidden"></div>
<div id="db-table" class="tbl-wrap hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
05 노드맵 대시보드
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-nm-dash">
<header class="pane-hdr">
<div>
<h1>노드맵 대시보드</h1>
<p>node_map_master 테이블을 조회합니다.</p>
</div>
<div class="pane-tag">NODE MAP / MASTER</div>
</header>
<!-- 필터 카드 -->
<div class="card">
<div class="card-cap">필터 조건</div>
<div class="cols-3">
<div class="fg">
<label>Level 최소</label>
<input id="nf-lv-min" class="inp" type="number" min="0" placeholder="0"/>
</div>
<div class="fg">
<label>Level 최대</label>
<input id="nf-lv-max" class="inp" type="number" min="0" placeholder=""/>
</div>
<div class="fg">
<label>클래스</label>
<select id="nf-class" class="inp">
<option value="">전체</option>
<option value="Object">Object</option>
<option value="Variable">Variable</option>
</select>
</div>
<div class="fg">
<label>Node ID 검색</label>
<input id="nf-nid" class="inp" placeholder="포함 검색"/>
</div>
<div class="fg">
<label>데이터 타입 <em>(직접 입력)</em></label>
<input id="nf-dtype" class="inp" placeholder="예: Double, Int32"/>
</div>
</div>
<!-- 이름 OR 조건 선택 (최대 4개) — 불러오기 버튼으로 옵션 채움 -->
<div class="fg nm-name-row">
<label style="display:flex;align-items:center;gap:8px">
이름 선택 <em>(OR 조건, 최대 4개)</em>
<button class="btn-b btn-sm" onclick="nmLoadNames()" style="margin-left:4px">▼ 옵션 불러오기</button>
</label>
<div class="nm-name-selects">
<select id="nf-name-1" class="inp nm-name-sel"><option value="">— 선택 안 함 —</option></select>
<select id="nf-name-2" class="inp nm-name-sel"><option value="">— 선택 안 함 —</option></select>
<select id="nf-name-3" class="inp nm-name-sel"><option value="">— 선택 안 함 —</option></select>
<select id="nf-name-4" class="inp nm-name-sel"><option value="">— 선택 안 함 —</option></select>
</div>
</div>
<div class="btn-row" style="align-items:center">
<button class="btn-a" onclick="nmQuery(0)">🔍 조회</button>
<button class="btn-b" onclick="nmReset()">초기화</button>
<div style="display:flex;align-items:center;gap:8px;margin-left:auto">
<label style="font-size:11px;color:var(--t2);white-space:nowrap">페이지당</label>
<input id="nf-limit" class="inp" type="number" value="100" min="10" max="500" style="width:80px"/>
<label style="font-size:11px;color:var(--t2)"></label>
</div>
</div>
</div>
<!-- 결과 통계 + 페이지네이션 -->
<div id="nm-result-bar" class="nm-result-bar hidden">
<span id="nm-result-info" class="nm-result-info"></span>
<div class="pg">
<button class="btn-b btn-sm" id="nm-pg-prev" onclick="nmPrev()">← 이전</button>
<span id="nm-pg-info" class="pg-info"></span>
<button class="btn-b btn-sm" id="nm-pg-next" onclick="nmNext()">다음 →</button>
</div>
</div>
<!-- 테이블 -->
<div id="nm-table" class="tbl-wrap hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
06 포인트빌더
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-pb">
<header class="pane-hdr">
<div>
<h1>포인트빌더</h1>
<p>node_map_master 에서 실시간 모니터링할 포인트를 선택해 realtime_table 을 구성합니다.</p>
</div>
<div class="pane-tag">REALTIME / BUILD</div>
</header>
<!-- 빌더 카드 -->
<div class="cols-2">
<div class="card">
<div class="card-cap">조건으로 테이블 작성</div>
<div class="fg">
<label>이름(name) 선택 <em>(OR 조건, 최대 8개)</em>
<button class="btn-b btn-sm" onclick="pbLoad()" style="margin-left:4px">▼ 옵션 불러오기</button>
</label>
<div class="pb-name-grid" id="pb-name-grid">
<!-- JS 에서 드롭다운 동적 생성 -->
<select id="pb-n1" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n2" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n3" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n4" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n5" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n6" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n7" class="inp"><option value="">— 선택 안 함 —</option></select>
<select id="pb-n8" class="inp"><option value="">— 선택 안 함 —</option></select>
</div>
</div>
<div class="fg">
<label>데이터 타입(data_type) 직접 입력 <em>(OR 조건, 최대 2개)</em></label>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:8px">
<input id="pb-dt1" class="inp" placeholder="예: Double"/>
<input id="pb-dt2" class="inp" placeholder="예: Int32"/>
</div>
</div>
<button class="btn-a" onclick="pbBuild()">🔨 테이블 작성하기</button>
<div id="pb-build-log" class="logbox hidden" style="margin-top:10px"></div>
</div>
<div class="card">
<div class="card-cap">수동 포인트 추가</div>
<div class="fg">
<label>Node ID 직접 입력</label>
<input id="pb-manual-nid" class="inp" placeholder="ns=1;s=tagname.pv..."/>
</div>
<button class="btn-b" onclick="pbAddManual()"> 추가</button>
<div id="pb-manual-log" class="logbox hidden" style="margin-top:10px"></div>
<div class="card-cap" style="margin-top:20px">실시간 구독 제어</div>
<div class="cols-2" style="gap:8px;margin-bottom:10px">
<div class="fg">
<label>서버 IP</label>
<input id="pb-rt-ip" class="inp" value="192.168.0.20"/>
</div>
<div class="fg">
<label>포트</label>
<input id="pb-rt-port" class="inp" type="number" value="4840"/>
</div>
<div class="fg">
<label>클라이언트 호스트</label>
<input id="pb-rt-client" class="inp" value="dbsvr"/>
</div>
<div class="fg">
<label>계정</label>
<input id="pb-rt-user" class="inp" value="mngr"/>
</div>
<div class="fg" style="grid-column:1/-1">
<label>비밀번호</label>
<input id="pb-rt-pw" class="inp" type="password" value="mngr"/>
</div>
</div>
<div class="btn-row">
<button class="btn-a" onclick="rtStart()">▶ 구독 시작</button>
<button class="btn-b" onclick="rtStop()">■ 구독 중지</button>
<button class="btn-b btn-sm" onclick="rtStatus()">상태 확인</button>
</div>
<div id="pb-rt-status" class="logbox hidden" style="margin-top:8px"></div>
</div>
</div>
<!-- 포인트 목록 -->
<div class="card" style="margin-top:0">
<div class="card-cap" style="display:flex;justify-content:space-between;align-items:center">
<span>포인트 목록 <span id="pb-count" class="mut">(0개)</span></span>
<button class="btn-b btn-sm" onclick="pbRefresh()">↻ 새로 고침</button>
</div>
<div id="pb-table" class="tbl-wrap">
<div style="padding:20px;color:var(--t2)">포인트가 없습니다. 위에서 테이블을 작성하세요.</div>
</div>
</div>
</section>
<!-- ══════════════════════════════════════════════════════
07 이력 조회
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-hist">
<header class="pane-hdr">
<div>
<h1>이력 조회</h1>
<p>history_table 의 시계열 데이터를 조회합니다.</p>
</div>
<div class="pane-tag">HISTORY / TREND</div>
</header>
<div class="card">
<div class="card-cap">조회 조건</div>
<div class="fg">
<div style="display:flex;align-items:center;gap:8px;flex-wrap:wrap">
<span>태그 선택 <em>(최대 8개, OR 조건)</em></span>
<button class="btn-b btn-sm" onclick="histLoad()">▼ 옵션 불러오기</button>
<span id="hist-load-status" class="hist-status">대기 중<span class="status-dot"></span></span>
</div>
<div class="pb-name-grid">
<select id="hf-t1" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t2" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t3" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t4" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t5" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t6" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t7" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t8" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
</div>
</div>
<div class="cols-4">
<div class="fg">
<label>시작 시간</label>
<input type="hidden" id="hf-from"/>
<div class="dt-display inp" id="dtp-from-display" onclick="dtOpen('from')">— 선택 안 함 —</div>
</div>
<div class="fg">
<label>종료 시간</label>
<input type="hidden" id="hf-to"/>
<div class="dt-display inp" id="dtp-to-display" onclick="dtOpen('to')">— 선택 안 함 —</div>
</div>
<div class="fg">
<label>조회 간격</label>
<select id="hf-interval" class="inp">
<option value="1 minute">원시 데이터 (기본)</option>
<option value="5 minutes">5분 집계</option>
<option value="10 minutes">10분 집계</option>
<option value="30 minutes">30분 집계</option>
<option value="1 hour">1시간 집계</option>
<option value="1 day">1일 집계</option>
</select>
</div>
<div class="fg">
<label>최대 행 수</label>
<input id="hf-limit" class="inp" type="number" value="500" min="10" max="5000"/>
</div>
</div>
<div class="btn-row">
<button class="btn-a" onclick="histQuery()">🔍 조회</button>
<button class="btn-b" onclick="histReset()">초기화</button>
</div>
</div>
<!-- 하이퍼테이블 관리 -->
<div class="card" id="ht-manage-card">
<div class="card-cap">하이퍼테이블 관리</div>
<div class="fg">
<label>history_table이 현재 하이퍼테이블 상태입니다. 아래 옵션을 설정하여 수동으로 생성할 수 있습니다.</label>
</div>
<div class="fg">
<label style="display:flex;align-items:center;gap:8px">
<input type="checkbox" id="ht-auto-retention" onchange="htToggleRetention()"/>
보관 기간 설정
</label>
<div id="ht-retention-panel" class="ht-hidden" style="margin-top:8px;padding-left:20px">
<div class="cols-2">
<div>
<label>보관 기간</label>
<input id="ht-retention-period" class="inp" type="text" value="90 days" placeholder="예: 90 days"/>
</div>
<div>
<label>테이블명</label>
<input id="ht-table-name" class="inp" type="text" value="history_table" placeholder="테이블명"/>
</div>
</div>
</div>
</div>
<div class="fg" style="margin-top:12px">
<label style="display:flex;align-items:center;gap:8px">
<input type="checkbox" id="ht-auto-compression" onchange="htToggleCompression()"/>
압축 활성화
</label>
<div id="ht-compression-panel" class="ht-hidden" style="margin-top:8px;padding-left:20px">
<div>
<label>압축 구간</label>
<input id="ht-compression-period" class="inp" type="text" value="1 day" placeholder="예: 1 day"/>
</div>
</div>
</div>
<div class="fg" style="margin-top:12px">
<label style="display:flex;align-items:center;gap:8px">
<input type="checkbox" id="ht-auto-aggregate"/>
연속 집계 생성 (선택사항)
</label>
</div>
<div class="btn-row" style="margin-top:16px">
<button class="btn-a" onclick="htCreate()">🔧 하이퍼테이블 생성</button>
<button class="btn-b" onclick="htLoadStatus()">🔄 상태 새로고침</button>
</div>
</div>
<!-- 하이퍼테이블 상태 표시 -->
<div id="ht-status-box" class="ht-status-box hidden">
<div class="ht-status-header">
<span class="ht-status-icon" id="ht-status-icon"></span>
<span class="ht-status-text" id="ht-status-text">로딩 중</span>
</div>
<div class="ht-status-detail" id="ht-status-detail"></div>
<div class="ht-info-panel" id="ht-info-panel">
<div class="ht-info-grid">
<div class="ht-info-item">
<span class="ht-info-label">테이블명</span>
<span class="ht-info-value" id="ht-info-table">-</span>
</div>
<div class="ht-info-item">
<span class="ht-info-label">레코드 수</span>
<span class="ht-info-value" id="ht-info-records">-</span>
</div>
<div class="ht-info-item">
<span class="ht-info-label">보관 정책</span>
<span class="ht-info-value" id="ht-info-retention">-</span>
</div>
<div class="ht-info-item">
<span class="ht-info-label">압축</span>
<span class="ht-info-value" id="ht-info-compression">-</span>
</div>
</div>
</div>
</div>
<!-- 상태 표시 창 -->
<div id="hist-status-box" class="hist-status-box hidden">
<div class="hist-status-header">
<span class="hist-status-icon" id="hist-status-icon"></span>
<span class="hist-status-text" id="hist-status-text">대기 중</span>
</div>
<div class="hist-status-detail" id="hist-status-detail"></div>
</div>
<div id="hist-result-info" class="nm-result-info hidden" style="margin:8px 0"></div>
<div id="hist-table" class="tbl-wrap hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
08 OPC UA 서버
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-opcsvr">
<header class="pane-hdr">
<div>
<h1>OPC UA 서버</h1>
<p class="sub">ExperionCrawler를 OPC UA 서버로 동작시켜 외부 클라이언트에 실시간 값을 제공합니다.</p>
</div>
</header>
<!-- 상태 카드 -->
<div class="srv-status-card" id="srv-status-card">
<div class="srv-status-row">
<span class="dot" id="srv-dot"></span>
<span id="srv-status-txt" class="srv-label">상태 조회 중...</span>
</div>
<div class="srv-meta" id="srv-meta"></div>
</div>
<!-- 버튼 행 -->
<div class="row-btns" style="margin-top:12px">
<button class="btn-a" onclick="srvStart()">▶ 서버 시작</button>
<button class="btn-b" onclick="srvStop()">■ 서버 중지</button>
<button class="btn-b" onclick="srvRebuild()">↺ 주소공간 재구성</button>
<button class="btn-b" onclick="srvLoad()">↻ 상태 새로고침</button>
</div>
<div id="srv-log" class="log-box hidden" style="margin-top:16px"></div>
</section>
<!-- ══════════════════════════════════════════════════════
09 Text-to-SQL
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-t2s">
<header class="pane-hdr">
<div>
<h1>Text-to-SQL 시계열 대시보드</h1>
<p>자연어 질의를 통해 TimeScaleDB 시계열 데이터를 조회하고 분석합니다.</p>
</div>
<div class="pane-tag">AI / SQL</div>
</header>
<!-- 자연어 쿼리 -->
<div class="card" style="margin-bottom:18px">
<div class="card-cap">🗣 자연어 쿼리</div>
<div class="t2s-input-row">
<input id="t2s-query" class="inp" placeholder='예: "FICQ-6101.PV 온도 최근 1시간 평균", "최대값 조회", "최근 24시간 추세"' onkeydown="if(event.key==='Enter')t2sParse()"/>
<button class="btn-a" onclick="t2sParse()">SQL 변환</button>
<button class="btn-b" onclick="t2sExecute()">▶ 실행</button>
<button class="btn-b" onclick="t2sAnalyze()">📊 분석</button>
</div>
<div style="margin-top:10px">
<span style="font-size:12px;color:var(--t1)">추천 쿼리: </span>
<button class="t2s-chip" onclick="t2sSetQuery('FICQ-6101.PV 최근 1시간 평균')">최근 1시간 평균</button>
<button class="t2s-chip" onclick="t2sSetQuery('FICQ-6101.PV 최근 24시간 최대값')">24시간 최대값</button>
<button class="t2s-chip" onclick="t2sSetQuery('FICQ-6101.PV 최근 7일 최소값')">7일 최소값</button>
<button class="t2s-chip" onclick="t2sSetQuery('FICQ-6101.PV 최근 1시간 추세')">추세 분석</button>
</div>
</div>
<!-- 생성된 SQL -->
<div class="card" style="margin-bottom:18px">
<div class="card-cap">📝 생성된 SQL</div>
<textarea id="t2s-sql" class="t2s-sql-area" placeholder="자연어 쿼리를 변환하면 여기에 SQL이 표시됩니다..."></textarea>
</div>
<!-- 태그 분석 -->
<div class="card" style="margin-bottom:18px">
<div class="card-cap">🏷 태그 분석 옵션</div>
<div class="cols-3">
<div class="fg">
<label>태그명 <em>(쉼표 구분, 비우면 전체)</em></label>
<input id="t2s-tags" class="inp" placeholder="FICQ-6101.PV,PV002,PV003"/>
</div>
<div class="fg">
<label>집계 간격</label>
<select id="t2s-interval" class="inp">
<option value="1 min">1분</option>
<option value="5 min" selected>5분</option>
<option value="15 min">15분</option>
<option value="1 hour">1시간</option>
<option value="1 day">1일</option>
</select>
</div>
<div class="fg">
<label>데이터 제한</label>
<input id="t2s-limit" class="inp" type="number" value="1000"/>
</div>
</div>
<div class="cols-2" style="margin-top:12px">
<div class="fg">
<label>시작일 <em>(비우면 최근 24시간)</em></label>
<input id="t2s-date-from" class="inp" type="datetime-local"/>
</div>
<div class="fg">
<label>종료일 <em>(비우면 현재)</em></label>
<input id="t2s-date-to" class="inp" type="datetime-local"/>
</div>
</div>
<div style="margin-top:12px">
<div class="fg">
<label>분석 데이터 제한</label>
<input id="t2s-limit-analyze" class="inp" type="number" value="100"/>
</div>
</div>
</div>
<!-- 결과 테이블 -->
<div class="card" style="margin-bottom:18px">
<div class="card-cap">📊 조회 결과</div>
<div id="t2s-results">
<span class="placeholder">쿼리를 실행하면 여기에 결과가 표시됩니다</span>
</div>
</div>
<div class="card">
<div class="card-cap">📈 태그 분석 결과</div>
<div id="t2s-analysis-results">
<span class="placeholder">분석을 실행하면 여기에 결과가 표시됩니다</span>
</div>
</div>
<div id="t2s-log" class="logbox hidden"></div>
</section>
<!-- ══════════════════════════════════════════════════════
10 fastRecord
═══════════════════════════════════════════════════════ -->
<section class="pane" id="pane-fast">
<header class="pane-hdr">
<div>
<h1>fastRecord</h1>
<p>고속 샘플링으로 실시간 데이터를 수집하고 트렌드를 분석합니다.</p>
</div>
<div class="pane-tag">FAST / RECORD</div>
</header>
<!-- 세션 목록 (가로 카드) -->
<div class="card" style="margin-bottom:12px">
<div class="card-cap" style="display:flex;justify-content:space-between;align-items:center">
<span>세션 목록</span>
<button id="btn-fast-new" class="btn-a btn-sm">+ 신규</button>
</div>
<div id="fast-session-list" style="display:flex;flex-wrap:wrap;gap:8px;padding:8px 4px;min-height:52px"></div>
</div>
<!-- 차트 카드 -->
<div class="card">
<div class="card-cap" style="display:flex;justify-content:space-between;align-items:center">
<span id="fast-session-title">세션 상세</span>
<div style="display:flex;gap:6px;flex-wrap:wrap">
<button id="btn-fast-stop" class="btn-b btn-sm" style="display:none">■ 중지</button>
<button id="btn-fast-export-xlsx" class="btn-a btn-sm" style="display:none">Excel</button>
<button id="btn-fast-export-csv" class="btn-b btn-sm" style="display:none">CSV</button>
<button id="btn-fast-pin" class="btn-b btn-sm" style="display:none">고정</button>
<button id="btn-fast-delete" class="btn-b btn-sm" style="display:none;color:var(--red,#e55)">삭제</button>
</div>
</div>
<!-- 진행률 바 -->
<div style="height:6px;background:var(--s3);border-radius:3px;margin-bottom:4px">
<div id="fast-progress-bar" style="height:100%;width:0%;background:#4caf50;border-radius:3px;transition:width .5s"></div>
</div>
<div style="display:flex;justify-content:space-between;font-size:11px;color:var(--t2);margin-bottom:10px">
<span id="fast-progress-text">0 / 0 (0%)</span>
<span id="fast-elapsed-time">경과: 0s</span>
</div>
<!-- uPlot 차트 -->
<div id="fast-chart-container" style="min-height:380px"></div>
</div>
</section>
</main>
</div>
</div>
<!-- ── fastRecord 신규 세션 모달 ────────────────────────────── -->
<div id="modal-fast-new" style="display:none;position:fixed;inset:0;z-index:900;background:rgba(0,0,0,.55);align-items:center;justify-content:center" onclick="if(event.target===this)fastModalClose()">
<div style="background:var(--s2);border:1px solid var(--bd2);border-radius:var(--rl);padding:24px;width:480px;max-width:92vw;max-height:90vh;overflow-y:auto">
<div style="font-weight:700;font-size:15px;margin-bottom:16px">신규 fastSession</div>
<div class="fg">
<label>세션 이름</label>
<input type="text" class="inp" id="fast-session-name" placeholder="예: 공정온도_분석_20260428"/>
</div>
<div class="fg">
<label>태그 선택 <em style="font-weight:400">(Ctrl/Cmd 클릭으로 다중선택, 최대 8개)</em></label>
<select id="fast-tag-select" class="inp" multiple size="8" style="height:auto"></select>
</div>
<div class="cols-2" style="gap:10px;margin-top:4px">
<div class="fg">
<label>샘플링 간격</label>
<select class="inp" id="fast-sampling-ms">
<option value="100">100ms</option>
<option value="250">250ms</option>
<option value="500" selected>500ms</option>
<option value="1000">1000ms</option>
</select>
</div>
<div class="fg">
<label>수집 기간</label>
<select class="inp" id="fast-duration-sec">
<option value="60">1분</option>
<option value="300">5분</option>
<option value="900">15분</option>
<option value="1800">30분</option>
<option value="3600" selected>1시간</option>
<option value="7200">2시간</option>
<option value="14400">4시간</option>
<option value="43200">12시간</option>
<option value="86400">24시간</option>
</select>
</div>
</div>
<div class="fg" style="margin-top:4px">
<label>보관 기간 (일, 빈 칸 = 무한)</label>
<input type="number" class="inp" id="fast-retention-days" placeholder="30"/>
</div>
<div class="btn-row" style="margin-top:16px">
<button class="btn-b" onclick="fastModalClose()">취소</button>
<button class="btn-a" onclick="fastStart()">▶ 시작</button>
</div>
</div>
</div>
<!-- ── 날짜/시간 선택 팝업 ──────────────────────────────────── -->
<div id="dt-overlay" class="dt-overlay hidden" onclick="dtCancel()"></div>
<div id="dt-popup" class="dt-popup hidden">
<div class="dt-cal-nav">
<button class="dt-nav-btn" onclick="dtPrevMonth()"></button>
<span id="dt-month-label" class="dt-month-label"></span>
<button class="dt-nav-btn" onclick="dtNextMonth()"></button>
</div>
<div class="dt-cal-grid" id="dt-cal-grid"></div>
<div class="dt-time-row">
<span class="dt-time-label">시간</span>
<div class="dt-time-ctrl">
<button onclick="dtAdjTime('h',-1)"></button>
<input id="dt-hour" class="dt-time-inp" type="number" min="0" max="23" value="0" oninput="dtClampTime('h',this)"/>
<button onclick="dtAdjTime('h', 1)">+</button>
</div>
<span class="dt-time-sep">:</span>
<div class="dt-time-ctrl">
<button onclick="dtAdjTime('m',-1)"></button>
<input id="dt-min" class="dt-time-inp" type="number" min="0" max="59" value="0" oninput="dtClampTime('m',this)"/>
<button onclick="dtAdjTime('m', 1)">+</button>
</div>
</div>
<div class="dt-pop-btns">
<button class="btn-b btn-sm" onclick="dtClear()">지우기</button>
<button class="btn-b btn-sm" onclick="dtCancel()">취소</button>
<button class="btn-a btn-sm" onclick="dtConfirm()">확인</button>
</div>
</div>
<script src="/lib/uPlot.iife.min.js"></script>
<script src="/js/xlsx.full.min.js"></script>
<script src="/js/app.js"></script>
</body>
</html>

644
CLAUDE.md
View File

@@ -1,644 +0,0 @@
# ExperionCrawler — 작업 이력
## 작업 규칙
- 복잡한 작업은 항상 todo 목록 먼저 생성
- 각 단계 시작 전 todo 목록 확인
- 단계 완료 후 즉시 completed 표시
## 완료된 작업
### 기능 추가 — OPC UA 서버 기능 (2026-04-15)
#### 배경
ExperionCrawler가 OPC UA 클라이언트 역할만 했으나, 외부 OPC UA 클라이언트(SCADA, MES 등)가 ExperionCrawler에 접속해 실시간 값을 읽을 수 있도록 OPC UA 서버 기능 추가.
#### 아키텍처
```
[Experion HS R530] ──(OPC UA Client)──► ExperionCrawler ◄──(OPC UA Client)── [외부 시스템]
(OPC UA Server)
[PostgreSQL DB]
```
#### 주소 공간 구조
```
Root/Objects/ExperionCrawler
├── ServerInfo/Status, PointCount, LastUpdateTime
└── Realtime/<tagname_1>, <tagname_2>, … (ns=2;s=tag_{tagname})
```
#### 수정/추가 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Web/ExperionCrawler.csproj` | `OPCFoundation.NetStandard.Opc.Ua.Server v1.5.378.134` 패키지 추가 |
| `src/Web/appsettings.json` | `OpcUaServer` 섹션 추가 (Port:4841, EnableSecurity:false, AllowAnonymous:true) |
| `src/Core/Application/Interfaces/IExperionServices.cs` | `IExperionOpcServerService` 인터페이스, `OpcServerStatus` record, `GetRealtimeNodeDataTypesAsync()` 추가 |
| `src/Infrastructure/OpcUa/ExperionOpcServerNodeManager.cs` | 신규 — `CustomNodeManager2` 상속, 주소 공간 관리 (`CreateAddressSpace`, `RebuildAddressSpace`, `UpdateNodeValue`) |
| `src/Infrastructure/OpcUa/ExperionOpcServerService.cs` | 신규 — `ExperionStandardServer` + `ExperionOpcServerService` (`IHostedService` + `IExperionOpcServerService`) |
| `src/Infrastructure/OpcUa/ExperionRealtimeService.cs` | `_pointCache` (nodeId→RealtimePoint) 추가; `FlushPendingAsync`에서 OPC 서버 노드 값 lazy 갱신 |
| `src/Infrastructure/Database/ExperionDbContext.cs` | `GetRealtimeNodeDataTypesAsync()` — realtime_table × node_map_master 조인 |
| `src/Web/Controllers/ExperionControllers.cs` | `ExperionOpcServerController` 추가 (start/stop/status/rebuild) |
| `src/Web/Program.cs` | `ExperionOpcServerService` Singleton+HostedService 등록 |
| `src/Web/wwwroot/index.html` | 08 OPC UA 서버 탭 + pane-opcsvr 섹션 추가 |
| `src/Web/wwwroot/js/app.js` | `srvLoad/Start/Stop/Rebuild/_srvRender/_srvStartPoll/_srvStopPoll` 구현 |
| `src/Web/wwwroot/css/style.css` | `.srv-status-card`, `.srv-meta`, `.dot.grn` 스타일 추가 |
#### 주요 설계 결정
| 항목 | 결정 |
|------|------|
| 인증서 | 기존 `pki/own/certs/{hostname}.pfx` 재사용 (`ApplicationType.ClientAndServer`) |
| 포트 | 기본 4841 (4840은 Experion HS R530이 사용 가능) |
| 보안 | 기본 None (appsettings.json에서 변경 가능) |
| 자동 재시작 | `opcserver_autostart.json` 플래그 파일 패턴 (RealtimeService와 동일) |
| 순환 참조 | `IServiceProvider` lazy resolve — `_opcServer ??= _sp.GetService<IExperionOpcServerService>()` |
| FlushLoop 연동 | 500ms 배치 DB 업데이트 후 → OPC 서버 노드 값도 동시 갱신 (DB 폴링 없음) |
#### API 엔드포인트
- `GET /api/opcserver/status` — 상태 조회 (running, clientCount, nodeCount, endpointUrl, startedAt)
- `POST /api/opcserver/start` — 서버 시작
- `POST /api/opcserver/stop` — 서버 중지
- `POST /api/opcserver/rebuild` — 주소 공간 재구성
#### 빌드 결과
- 경고 11건 (기존 8건 + OPC SDK Server Start/Stop deprecated 3건), **에러 0건** — 빌드 성공
#### OPC UA 서버가 노출하는 데이터
**데이터 출처**: `realtime_table`에 등록된 포인트 전체 (포인트빌더에서 빌드/수동 추가한 포인트)
**주소 공간 구조**
```
Root/Objects/ExperionCrawler
├── ServerInfo/
│ ├── Status (String) — "Running" / "Stopped"
│ ├── PointCount (Int32) — 구독 중인 포인트 수
│ └── LastUpdateTime (DateTime) — 마지막 값 갱신 시각
└── Realtime/
├── <tagname_1> ns=2;s=tag_FIC101_PV
├── <tagname_2>
└── …
```
**NodeId 명명 규칙**: `ns=2;s=tag_{tagname}`
**DataType 결정**: `realtime_table` × `node_map_master` 조인
- Double/Float/Int32/Int64/Boolean/DateTime → 해당 OPC UA 타입
- 기타/NULL → String (fallback)
**접근 제한**: 읽기 전용 (`AccessLevel = CurrentRead`), `Historizing = false`
**갱신 주기**: Experion HS R530 → FlushLoop 500ms 배치 → DB + OPC 서버 노드 동시 갱신
---
### 로그 정리 — 스냅샷 로그 2줄 → 1줄 (2026-04-15)
#### 증상
히스토리 스냅샷 1회 저장마다 터미널에 로그 2줄 출력:
```
[ExperionDb] history 스냅샷: 1752건 @ 01:14:18
[HistoryService] 스냅샷 저장: 1752건
```
#### 원인
DB 저장 완료 후 `ExperionDbService`에서 `LogInformation`, 호출자 `ExperionHistoryService`에서도 `LogInformation`. 저장은 1회이나 로그가 2줄.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Infrastructure/Database/ExperionDbContext.cs` | `SnapshotToHistoryAsync()` 내부 로그를 `LogInformation``LogDebug`로 변경 |
#### 결과
운영 로그(`Information` 레벨)에서 `[HistoryService] 스냅샷 저장: N건` 1줄만 출력.
---
### 버그 수정 — Ctrl+C 종료 시 자동재시작 플래그 삭제 오류 (2026-04-15)
#### 증상
Ctrl+C로 앱 종료 시 `realtime_autostart.json` 플래그 파일이 삭제되어, 재기동 후 자동 구독 시작이 동작하지 않음.
#### 원인
`IHostedService.StopAsync(CancellationToken)` (앱 종료 훅)이 UI 수동 중지 메서드인 `StopAsync()`를 그대로 호출. `StopAsync()`는 플래그 파일을 삭제하므로 앱 종료와 수동 중지를 구분하지 못했음.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Infrastructure/OpcUa/ExperionRealtimeService.cs` | `IHostedService.StopAsync(CancellationToken)` 분리 — `_cts.Cancel()` + 태스크 대기만 수행, 플래그 파일 삭제 없음 |
#### 동작 구분
| 종료 방식 | 플래그 파일 |
|----------|------------|
| Ctrl+C (앱 종료) | **유지** → 재기동 시 자동 구독 시작 |
| UI 중지 버튼 | **삭제** → 재기동 후 자동 시작 없음 |
---
### 버그 수정 — 이력 조회 중복 키 예외 (2026-04-15)
#### 증상
이력 조회 시 서버 500 에러:
```
System.ArgumentException: An item with the same key has already been added.
Key: p-6102.hzset.fieldvalue
at ExperionDbService.QueryHistoryAsync ... line 342
```
#### 원인
`history_table`에 동일 `recorded_at` + 동일 `tagname` 조합이 중복 저장된 행 존재. `.ToDictionary(r => r.TagName, r => r.Value)` 호출 시 중복 키로 예외 발생.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Infrastructure/Database/ExperionDbContext.cs` | `TagName` 기준 `GroupBy` 추가 → 중복 시 `.Last().Value` 사용 |
---
### 기능 추가 — 이력 조회 날짜/시간 팝업 피커 (2026-04-15)
#### 배경
- `datetime-local` 입력이 Windows 브라우저 로케일에 따라 AM/PM 12시간제로 표시됨
- 서버(Ubuntu UTC) / 브라우저(Windows KST) 시간대 차이로 인한 표시 혼란
#### 설계
- `datetime-local` 입력 제거 → 클릭 시 커스텀 달력+시간 팝업 오픈
- 달력: 월 이동 가능, 오늘 날짜 amber 강조, 선택일 반전 표시
- 시간: 24시간제, ``/`+` 버튼 또는 직접 입력 (023시, 059분)
- 확인 시 `YYYY-MM-DD HH:MM` 형식으로 필드 표시
- hidden input에 로컬 시간 문자열 저장 → `new Date(...).toISOString()`으로 KST→UTC 변환 후 서버 전송 (기존 로직 유지)
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Web/wwwroot/index.html` | `datetime-local` 2개 → `.dt-display` + `hidden input` 교체; 팝업 HTML(`#dt-popup`, `#dt-overlay`) 추가 |
| `src/Web/wwwroot/css/style.css` | `.dt-popup`, `.dt-cal-grid`, `.dt-day`, `.dt-time-row` 등 피커 전용 다크 테마 스타일 추가; 기존 `datetime-local` AM/PM 숨김 CSS 제거 |
| `src/Web/wwwroot/js/app.js` | `dtOpen()`, `dtRenderCal()`, `dtSelectDay()`, `dtPrevMonth()`, `dtNextMonth()`, `dtAdjTime()`, `dtClampTime()`, `dtConfirm()`, `dtClear()`, `dtClose()` 구현; `histReset()`에서 `dtClearField()` 호출로 표시 텍스트 초기화 |
#### 빌드 결과
- 경고 8건 (기존 동일), **에러 0건** — 빌드 성공
---
### 버그 수정 — 단일 태그 읽기 성공/실패 판정 오류 (2026-04-15)
#### 증상
서버접속테스트 페이지에서 단일 태그 읽기 시, OPC UA 서버가 `BadNodeIdUnknown(0x80340000)` 등 에러 상태 코드를 반환해도 "✅ 읽기 성공"으로 표시되는 버그.
#### 원인
`ExperionOpcClient.cs``ReadTagsAsync` 내부에서 `StatusCode` 값과 무관하게 `Success = true`를 하드코딩해서 `ExperionReadResult`를 생성했음.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `src/Infrastructure/OpcUa/ExperionOpcClient.cs` | `StatusCode.IsGood()` 결과를 `Success` 플래그로 사용. Bad이면 `Success=false`, `Value=null`, `Error`에 상태 코드 메시지 설정 |
#### 결과
`BadNodeIdUnknown` 등 Bad 상태 코드 수신 시 → ❌ 읽기 실패로 정상 표시
#### 빌드 결과 (경고 상세)
경고 8건, **에러 0건** — 빌드 성공
| # | 파일 | 내용 |
|---|------|------|
| 1 | `ExperionOpcClient.cs:108` | `Session.Create()``ISessionFactory.CreateAsync` 사용 권장 |
| 2 | `ExperionRealtimeService.cs:161` | `Subscription.ApplyChanges()``ApplyChangesAsync()` 사용 권장 |
| 3 | `ExperionRealtimeService.cs:168` | 동일 |
| 4 | `ExperionRealtimeService.cs:277` | `Subscription.Create()``CreateAsync()` 사용 권장 |
| 5 | `ExperionRealtimeService.cs:346` | `Subscription.Delete()``DeleteAsync()` 사용 권장 |
| 6 | `ExperionRealtimeService.cs:424` | `Session.Create()``ISessionFactory.CreateAsync` 사용 권장 |
| 78 | (위 항목 중 중복 카운트) | — |
전부 OPC UA SDK가 동기 메서드를 `[Obsolete]`로 표시하고 비동기 버전을 권장하는 경고. 기능상 문제 없음.
---
### 노드맵 대시보드 구현 (2026-04-14)
node_map_master 테이블을 조회·탐색할 수 있는 웹 대시보드를 풀스택으로 구현했다.
#### 수정된 파일
| 파일 | 내용 |
|------|------|
| `src/Core/Application/Interfaces/IExperionServices.cs` | `IExperionDbService``GetMasterStatsAsync()` / `QueryMasterAsync()` 추가, `NodeMapStats` / `NodeMapQueryResult` record 추가 |
| `src/Infrastructure/Database/ExperionDbContext.cs` | `ExperionDbService`에 두 메서드 구현 (통계·필터 조회, 페이지네이션) |
| `src/Web/Controllers/ExperionControllers.cs` | `ExperionNodeMapController` 추가 (`GET /api/nodemap/stats`, `GET /api/nodemap/query`) |
| `src/Web/wwwroot/index.html` | 사이드바 05번 탭 추가, `#pane-nm-dash` 섹션 추가 (통계 카드·필터폼·페이지네이션·테이블) |
| `src/Web/wwwroot/js/app.js` | `nmLoad()` / `nmQuery()` / `nmPrev()` / `nmNext()` / `nmReset()` 구현, 탭 클릭 핸들러에 `nmLoad()` 호출 추가 |
| `src/Web/wwwroot/css/style.css` | `.nm-stat-row`, `.nm-cls`, `.nm-dtype`, `.pg`, `.btn-sm` 등 대시보드 전용 스타일 추가 |
#### 빌드 결과
- 경고 3건 (기존 경고 동일), **에러 0건** — 빌드 성공
#### 주의 사항
- 인증서 관련 코드(`ExperionCertificateService.cs`, 인증서 컨트롤러)는 일절 수정하지 않음
---
### 이름 필터 드롭다운 OR 조건 검색 (2026-04-14)
노드맵 대시보드의 이름 검색을 텍스트 입력에서 `name` 컬럼 고유값 풀다운 메뉴 4개로 교체, OR 조건 최대 4개 동시 선택 가능하도록 확장했다.
#### 수정된 파일
| 파일 | 내용 |
|------|------|
| `src/Core/Application/Interfaces/IExperionServices.cs` | `GetNameListAsync()` 추가; `QueryMasterAsync` 파라미터 `string? name``IEnumerable<string>? names` |
| `src/Infrastructure/Database/ExperionDbContext.cs` | `GetNameListAsync()` 구현 (distinct + 오름차순 정렬); `QueryMasterAsync`에서 `nameList.Contains(x.Name)` → EF가 `WHERE name IN (...)` SQL 생성 |
| `src/Web/Controllers/ExperionControllers.cs` | `GET /api/nodemap/names` 엔드포인트 추가; `Query` 액션 파라미터 `string? name``List<string>? names` (ASP.NET Core가 `?names=A&names=B` 자동 바인딩) |
| `src/Web/wwwroot/index.html` | "이름 검색" 텍스트 입력 제거 → `nf-name-1` ~ `nf-name-4` 4개 `<select>` 드롭다운 추가 |
| `src/Web/wwwroot/js/app.js` | `nmLoad()`에서 `/api/nodemap/names` 병렬 호출 후 4개 드롭다운 채우기; `nmQuery()`에서 선택 이름들을 `params.append('names', nm)`로 OR 전송; `nmReset()`에서 4개 드롭다운 초기화 |
| `src/Web/wwwroot/css/style.css` | `.nm-name-selects` (4열 그리드, 900px 이하 2열) 추가 |
#### 빌드 결과
- 경고 3건 (기존 경고 동일), **에러 0건** — 빌드 성공
---
## 구현 완료 (2026-04-14, todo.md 전항목)
### 빌드 결과
- 경고 6건 (기존 3건 + 신규 3건 OPC SDK deprecated API 경고), **에러 0건** — 빌드 성공
---
## 버그 수정 이력 (2026-04-14)
### 버그 1 — OPC UA 연결 시 OS TCP 타임아웃(최대 127초) 문제
#### 증상
- 접속 테스트 버튼을 눌렀을 때 수분간 응답 없는 것처럼 보임
- `ExperionRealtimeService`: "연결 오류, 30초 후 재시도" 로그가 매우 늦게 출력됨
- 오류: `System.Net.Sockets.SocketException (110): Connection timed out`
#### 원인
Linux에서 OPC UA 서버 IP가 응답 없음(firewall/unreachable)이면 OS TCP SYN 재전송 타임아웃이 최대 127초까지 걸림. `TransportQuotas.OperationTimeout`은 OPC UA 프로토콜 레벨 타임아웃이라 TCP connect 단계에는 적용되지 않음.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `ExperionOpcClient.cs` | `SelectEndpointAsync``CancellationTokenSource(10초)` 추가 — DiscoveryClient 생성 시 10초 타임아웃 적용 |
| `ExperionRealtimeService.cs` | 동일하게 `SelectEndpointAsync` 10초 타임아웃 적용 |
#### 결과
서버 미응답 시 127초 대기 → **10초 이내 실패** 처리
---
### 버그 2 — PostgreSQL `sorry, too many clients already` (SQLSTATE 53300)
#### 증상
구독 시작 후 실시간 값 수신 시 터미널에 다량의 에러:
```
Npgsql.PostgresException (0x80004005): 53300: sorry, too many clients already
at ExperionDbService.UpdateLiveValueAsync(...)
at ExperionRealtimeService.<<OnNotification>b__0>d.MoveNext()
```
#### 원인
`OnNotification` 콜백이 포인트마다 `Task.Run` → 새 DI 스코프 → 새 `DbContext` → 새 DB 커넥션을 열었음. 2000여개 포인트가 동시에 값 변경 콜백을 받으면 순식간에 PostgreSQL `max_connections`(기본 100) 초과.
```
값 변경 콜백 × 2000개 → Task.Run × 2000개 → DB 커넥션 × 2000개 → 💥
```
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `IExperionServices.cs` | `BatchUpdateLiveValuesAsync(IEnumerable<LiveValueUpdate>)` 인터페이스 추가, `LiveValueUpdate` record 추가 |
| `ExperionDbContext.cs` | `BatchUpdateLiveValuesAsync` 구현 — 단일 DbContext에서 순차 ExecuteUpdateAsync |
| `ExperionRealtimeService.cs` | `OnNotification`에서 `Task.Run` 제거 → `ConcurrentDictionary`에 최신값만 기록. 별도 `FlushLoopAsync` 태스크가 500ms마다 단일 DbContext로 배치 업데이트 |
#### 수정 후 구조
```
값 변경 콜백 × N개 → ConcurrentDictionary[nodeId] = 최신값
↓ 500ms마다
단일 DbContext → BatchUpdateLiveValuesAsync → DB 커넥션 1개
```
#### 결과
- DB 커넥션 동시 사용 수: 2000개 → **최대 1개**
- 500ms 내 중복 변경은 최신값 1건만 DB에 반영 (deduplication)
- 빌드: 경고 6건(기존 동일), **에러 0건**
---
### 버그 3 — 대시보드 탭 진입 시 자동 API 호출로 인한 CPU/브라우저 버벅임
#### 증상
- **노드맵 대시보드** 탭 진입 시 CPU 과부하, 페이지 버벅임
- **포인트빌더** 탭 진입 시 동일 증상
- **이력 조회** 탭 진입 시 한참 동안 열리지 않음
#### 원인 (항목별)
| 탭 | 자동 호출 API | 무거운 이유 |
|----|--------------|------------|
| 노드맵 대시보드 | `/api/nodemap/stats` + `/api/nodemap/names` + `/api/nodemap/query` | stats: 5가지 집계 쿼리(COUNT×4, MAX, DISTINCT). 결과로 전체 조회까지 자동 실행 |
| 포인트빌더 | `/api/nodemap/names` + `/api/nodemap/stats` | stats 집계 쿼리 (포인트빌더 dataType 드롭다운 채우기 용도) |
| 이력 조회 | `/api/history/tagnames` → 드롭다운 8개에 2000개 옵션 삽입 | 8 × 2000 = 16,000개 DOM `<option>` 생성으로 브라우저 freeze |
#### 수정 내용
**공통 원칙**: 탭 진입 시 API 호출 0건. 사용자가 명시적으로 버튼을 눌렀을 때만 실행.
| 파일 | 수정 내용 |
|------|----------|
| `app.js` | 탭 클릭 핸들러에서 `nmLoad()`, `pbLoad()`, `histLoad()` 자동 호출 제거 |
| `app.js` | `nmReset()` 에서 `nmQuery(0)` 자동 호출 제거 |
| `app.js` | `nmLoad()``nmLoadNames()`로 분리 (이름 드롭다운만, 버튼 클릭 시 호출) |
| `app.js` | `nmLoad()` 내부의 통계 카드 렌더링 + `nmQuery(0)` 자동 호출 제거 |
| `app.js` | `pbLoad()` 에서 `/api/nodemap/stats` 호출 제거 |
| `app.js` | `histLoad()` 는 유지하되 탭 자동 호출 제거, "▼ 옵션 불러오기" 버튼 클릭 시에만 실행 |
| `index.html` | 노드맵 대시보드: 통계 카드(`nm-stat-row`) 제거, 데이터타입 select → text input |
| `index.html` | 노드맵 대시보드: 이름 드롭다운에 "▼ 옵션 불러오기" 버튼 추가 |
| `index.html` | 포인트빌더: 데이터타입 select 2개 → text input 2개 (`Double`, `Int32` 등 직접 입력) |
| `index.html` | 이력 조회: 태그 선택 드롭다운에 "▼ 옵션 불러오기" 버튼 추가 |
#### 결과 (탭별 진입 시 API 호출 수)
| 탭 | 이전 | 이후 |
|----|------|------|
| 노드맵 대시보드 | stats + names + query = **3건** | **0건** |
| 포인트빌더 | names + stats = **2건** | names = **1건** |
| 이력 조회 | tagnames = **1건** + DOM 16,000개 생성 | **0건** |
#### 주의 사항
- `/api/nodemap/stats` 엔드포인트는 서버에 남아있으나 프론트엔드에서 호출하지 않음
- 이름/태그 드롭다운은 "▼ 옵션 불러오기" 버튼으로 수동 로드
- 데이터타입 필터는 text input 직접 입력 방식으로 변경 (API 불필요)
---
### 버그 4 — 포인트빌더 탭 진입 시 여전히 버벅임 (2026-04-14)
#### 증상
버그 3 수정 이후에도 포인트빌더 탭 진입 시 버벅임 지속.
#### 원인
버그 3 수정 시 탭 핸들러에서 `pbLoad()` 제거를 누락. `app.js``if (tab === 'pb') pbLoad()` 가 그대로 남아 있었음. `pbLoad()``/api/nodemap/names` 호출 → 8개 드롭다운에 전체 name 목록 삽입 → DOM 부하.
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `app.js` | 탭 핸들러에서 `if (tab === 'pb') pbLoad()` 제거 |
| `index.html` | 포인트빌더 이름 선택 레이블 옆에 "▼ 옵션 불러오기" 버튼 추가 (`onclick="pbLoad()"`) |
#### 결과
포인트빌더 탭 진입 시 API 호출 **0건**
---
### 기능 추가 — 실시간 구독 자동 재시작 플래그 (2026-04-14)
#### 배경
앱 재기동 시 구독이 자동으로 재시작되지 않아 매번 수동으로 구독 시작 버튼을 눌러야 했음.
히스토리 스냅샷이 구독 여부와 무관하게 무조건 실행되어 `livevalue = NULL` 행이 저장되는 문제도 존재.
#### 설계
- 구독 시작 시 서버 설정을 `realtime_autostart.json` 파일로 저장 (앱 실행 디렉토리)
- 앱 기동 시 (`IHostedService.StartAsync`) 파일 존재 여부 확인 → 있으면 자동 구독 시작
- 구독 중지 시 파일 삭제 → 재기동 후 자동 시작 없음
- `ExperionHistoryService``IExperionRealtimeService.GetStatus().Running` 확인 → OFF이면 스냅샷 건너뜀
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `ExperionRealtimeService.cs` | `StartAsync(cfg)``realtime_autostart.json` 저장; `StopAsync()` 시 파일 삭제; `StartAsync(CancellationToken)` (IHostedService)에서 파일 읽어 자동 재시작 |
| `ExperionHistoryService.cs` | `IExperionRealtimeService` 생성자 주입; 스냅샷 전 `GetStatus().Running` 체크 → false이면 `continue` |
#### 동작 흐름
```
구독 시작 버튼 → realtime_autostart.json 저장 → OPC UA 구독 시작
앱 재기동 → 파일 감지 → 자동 구독 시작
구독 중지 버튼 → 파일 삭제 → 재기동 후 자동 시작 안 함
히스토리 서비스 → Running=false이면 스냅샷 건너뜀
```
---
### 기능 추가 — 수동 포인트 추가 시 OPC UA 핫 추가 및 유효성 검증 (2026-04-14)
#### 배경
수동으로 포인트를 추가해도 기존 구독에는 반영되지 않아 구독 재시작이 필요했음.
잘못된 node_id 입력 시 DB에만 저장되고 `livevalue`가 영원히 NULL인 문제도 존재.
#### 설계
- 수동 추가 시 DB 저장 후 구독 중이면 `MonitoredItem` 핫 추가 (`ApplyChanges()`)
- OPC UA 서버 응답 상태 확인 → bad 상태코드이면 subscription 제거 + DB 롤백 + 에러 반환
- 구독 중이 아닌 경우 DB에만 저장 → 다음 구독 시작 시 자동 포함
#### 수정 파일
| 파일 | 수정 내용 |
|------|----------|
| `IExperionServices.cs` | `IExperionRealtimeService``AddMonitoredItemAsync(string nodeId)` 추가 (반환: `(bool Success, string Message)`) |
| `ExperionRealtimeService.cs` | `AddMonitoredItemAsync` 구현 — MonitoredItem 생성, `ApplyChanges()`, 상태 확인, bad이면 롤백 |
| `ExperionControllers.cs` | `ExperionPointBuilderController``IExperionRealtimeService` 주입; `Add` 엔드포인트에서 DB 저장 후 `AddMonitoredItemAsync` 호출 → 실패 시 `DeleteRealtimePointAsync`로 DB 롤백 |
#### 동작 흐름
```
수동 추가 요청
├── DB 저장
├── 구독 중 아님 → 성공 ("다음 구독 시작 시 자동 포함")
└── 구독 중
├── OPC UA ApplyChanges() → Good → 즉시 구독 포함, 성공
└── OPC UA → Bad → subscription 제거 + DB 롤백 + 에러 반환
```
#### 빌드 결과
- 경고 8건 (기존 6건 + OPC SDK deprecated 2건), **에러 0건** — 빌드 성공
---
### 성능 분석 — 1,699포인트 기준 CPU 부하 추정 (2026-04-14)
#### 전제 조건
- 실시간 포인트: 1,699개
- 히스토리 스냅샷 주기: 60초
- 실시간 배치 flush 주기: 500ms
#### 히스토리 스냅샷 (60초마다)
- 작업: `realtime_table` 1,699행 SELECT → `history_table` INSERT 1,699행
- 특성: 1분에 1번 순간 burst, 수십 ms 수준
- 앱 CPU: EF Core 객체 생성 1,699개 → 거의 무시 가능
- **결론: 평균 CPU 기여 < 1%**
#### 실시간 livevalue 갱신 (500ms마다 배치)
- 작업: `ExecuteUpdateAsync` × (변경된 포인트 수)건 / 500ms
- OPC UA는 값이 바뀔 때만 콜백 → 전 포인트가 동시에 변경되는 경우는 드묾
- 실제 변경 수: 수십~수백건/500ms가 일반적
- **결론: 변경 포인트 수에 비례, 대부분의 경우 낮음**
#### 종합
| 작업 | 주기 | 예상 CPU |
|------|------|----------|
| 히스토리 스냅샷 | 60초/회 | 무시 가능 (< 1%) |
| 실시간 배치 업데이트 | 500ms/회 | 변경 포인트 수에 비례 |
| **합계** | - | **단일 코어 기준 5~15% 이내** |
실제 병목은 CPU보다 **PostgreSQL I/O와 커넥션 처리**쪽이 먼저 나타남. 현재 구조(단일 DbContext, 배치 flush)는 이미 최적화된 상태.
---
### 성능 분석 — 멀티모니터 4대 실시간 폴링 부하 (2026-04-14)
#### 시나리오
- 웹페이지에서 `realtime_table` 조회, 페이지당 200개, 2초 간격 갱신
- 멀티모니터 4대에서 4개의 브라우저 탭/창이 동시 동작
#### 부하 추정
| 항목 | 계산 | 평가 |
|------|------|------|
| 서버 요청 수 | 4탭 × 1회/2초 = **2 req/s** | 무시 가능 |
| DB 쿼리 | SELECT 200행 × 2회/s | 경량 |
| 응답 크기 | 200행 × ~150 bytes ≈ **30KB/응답** | 소량 |
| 네트워크 | 4 × 30KB / 2s = **60KB/s** | 거의 없음 |
| 브라우저 RAM | 탭당 60~100MB × 4 = **240~400MB** | 보통 수준 |
**결론: 서버 부하 크지 않음. 일반 개발용 PC(i5급, 8GB RAM)에서 충분히 감당 가능.**
#### 실질적 병목 — 브라우저 DOM 재렌더링
현재 `pbRender()``tbl.innerHTML`로 테이블 전체를 교체하는 방식 (full re-render).
- 200행 × 4탭 × 2초마다 전체 재생성 → 체감 가능한 CPU 사용
#### 결정 사항
**실시간 모니터링 페이지 구현 시 반드시 incremental DOM update 방식 사용**
- 이미 그려진 `<td>` 셀의 `.textContent`만 갱신 (값이 바뀐 셀만)
- `innerHTML` 전체 교체 금지
- 구조 변경(행 추가/삭제) 시에만 DOM 재구성 허용
---
### TimescaleDB 관련 결정 사항 (2026-04-14)
PostgreSQL에 TimescaleDB 확장이 설치되어 있음.
#### 결론: 앱 코드 수정 불필요
TimescaleDB는 PostgreSQL **확장(extension)** 이므로:
- 연결 문자열: 기존 PostgreSQL 그대로 사용
- EF Core / Npgsql 드라이버: 그대로 사용
- `history_table` hypertable 전환은 DB에서 DDL 한 줄만 실행
```sql
SELECT create_hypertable('history_table', 'recorded_at');
```
이 명령을 DB에서 한 번 실행하면 이후 INSERT/SELECT는 코드 변경 없이 TimescaleDB가 자동으로 시계열 최적화를 적용함.
**DbContext, 엔티티, 컨트롤러 등 앱 코드는 전혀 수정 불필요.**
---
## 구현 계획 (참고용)
### Task 1 — RealtimeTable + 포인트빌더 대시보드
#### 개요
- `realtime_table` PostgreSQL 테이블 생성: `id, tagname, node_id, livevalue, timestamp`
- `tagname`: `node_id.Substring(node_id.LastIndexOf(':') + 1)` (마지막 ':' 오른쪽 문자열, 없으면 전체)
- 소스: `node_map_master WHERE name IN (...) AND data_type = 'Double'`
#### 수정 파일
| 파일 | 내용 |
|------|------|
| `ExperionEntities.cs` | `RealtimePoint` 엔티티 추가 (`realtime_table` 매핑) |
| `IExperionServices.cs` | `IExperionDbService``BuildRealtimeTableAsync`, `GetRealtimePointsAsync`, `AddRealtimePointAsync`, `DeleteRealtimePointAsync` 추가 |
| `ExperionDbContext.cs` | `DbSet<RealtimePoint>`, 테이블 DDL, 4개 서비스 메서드 구현 |
| `ExperionControllers.cs` | `ExperionPointBuilderController` 추가 (POST /api/pointbuilder/build, GET /api/pointbuilder/points, POST /api/pointbuilder/add, DELETE /api/pointbuilder/{id}) |
| `index.html` | 06번 탭 '포인트빌더' 추가 — name 드롭다운 8개, dataType 드롭다운, 빌드 버튼, 수동 node_id 입력, 포인트 테이블 |
| `app.js` | `pbLoad()`, `pbBuild()`, `pbAddManual()`, `pbDelete(id)`, `pbRender()` 구현 |
| `style.css` | 포인트빌더 전용 스타일 추가 |
#### 설계 결정
- `BuildRealtimeTableAsync`는 기존 레코드를 모두 지우고 재생성 (TRUNCATE + INSERT)
- 수동 추가(`AddRealtimePointAsync`)는 `tagname`을 자동 추출해서 삽입
- 약 2000건 → 페이지네이션 불필요, 전체 목록을 클라이언트 측 테이블로 렌더링
---
### Task 2 — OPC UA 실시간 구독 (livevalue 업데이트)
#### 개요
- OPC UA Subscription + MonitoredItem API 사용 (값 변경 시에만 콜백)
- `IExperionRealtimeService` 인터페이스 + `ExperionRealtimeService` BackgroundService 신규 파일
- 서버 접속 설정은 `appsettings.json`에서 읽음 (기존 `ExperionServerConfig` 구조 재사용)
- 값 변경 콜백 → `realtime_table.livevalue` 업데이트 + `timestamp` 갱신
#### 수정 파일
| 파일 | 내용 |
|------|------|
| `IExperionServices.cs` | `IExperionRealtimeService` 인터페이스, `IExperionDbService``UpdateLiveValueAsync` 추가 |
| `ExperionDbContext.cs` | `UpdateLiveValueAsync` 구현 |
| `ExperionRealtimeService.cs` (신규) | `BackgroundService` 구현 — Subscription 생성, MonitoredItem 등록, 콜백 처리 |
| `ExperionControllers.cs` | `ExperionRealtimeController` 추가 (POST /api/realtime/start, POST /api/realtime/stop, GET /api/realtime/status) |
| `Program.cs` | `AddHostedService<ExperionRealtimeService>()` 등록 |
| `index.html` + `app.js` | 포인트빌더 탭에 실시간 시작/정지 버튼, 상태 표시, livevalue 폴링(3초) 추가 |
#### 설계 결정
- OPC UA Subscription: `PublishingInterval = 1000ms`
- MonitoredItem: `SamplingInterval = 500ms`, `DeadBandType = None`
- 값 변경 없으면 콜백 없음 → DB 업데이트 없음 (OPC UA 규약 준수)
- 서비스 재시작 시 자동 재연결 로직 포함 (30초 재시도)
---
### Task 3 — HistoryTable (시계열 스냅샷)
#### 개요
- `history_table`: `id, tagname, node_id, value, recorded_at`
- `ExperionHistoryService` BackgroundService → 설정된 주기(기본 60초)마다 `realtime_table` 전체를 스냅샷
- 주기는 `appsettings.json: "HistoryIntervalSeconds": 60` 에서 읽음
#### 수정 파일
| 파일 | 내용 |
|------|------|
| `ExperionEntities.cs` | `HistoryRecord` 엔티티 추가 (`history_table` 매핑) |
| `IExperionServices.cs` | `IExperionDbService``SnapshotToHistoryAsync` 추가 |
| `ExperionDbContext.cs` | `DbSet<HistoryRecord>`, 테이블 DDL, `SnapshotToHistoryAsync` 구현 |
| `ExperionHistoryService.cs` (신규) | `BackgroundService` — 주기적 `SnapshotToHistoryAsync` 호출 |
| `Program.cs` | `AddHostedService<ExperionHistoryService>()` 등록 |
---
### Task 4 — HistoryTable 웹페이지
#### 개요
- 07번 탭 '이력 조회' 추가
- tagname 드롭다운 최대 8개 선택 (다중 선택으로 열 구성)
- 시작 시간 / 종료 시간 범위 필터
- 결과 테이블: tagname이 열 헤더, recorded_at이 행
#### 수정 파일
| 파일 | 내용 |
|------|------|
| `IExperionServices.cs` | `IExperionDbService``GetTagNamesAsync`, `QueryHistoryAsync` 추가; `HistoryQueryResult` record 추가 |
| `ExperionDbContext.cs` | `GetTagNamesAsync`, `QueryHistoryAsync` 구현 |
| `ExperionControllers.cs` | `ExperionHistoryController` 추가 (GET /api/history/tagnames, GET /api/history/query) |
| `index.html` | 07번 탭 '이력 조회' + `#pane-hist` 섹션 추가 |
| `app.js` | `histLoad()`, `histQuery()`, `histRender()` 구현 |
| `style.css` | 이력 조회 전용 스타일 추가 |

View File

@@ -1,122 +0,0 @@
# ExperionCrawler 코딩 컨벤션
## 1. ASP.NET Core 컨트롤러 JSON 직렬화
### 핵심 설정
`src/Web/Program.cs`:
```csharp
builder.Services.AddControllers().AddJsonOptions(opt => {
opt.JsonSerializerOptions.PropertyNamingPolicy = null; // PascalCase 직렬화
});
```
`PropertyNamingPolicy = null`이므로 C# 속성명이 **그대로** JSON 키가 된다.
프론트엔드(`wwwroot/js/app.js`)는 모든 JSON 필드를 **camelCase**로 접근한다.
### 규칙: 컨트롤러 응답은 반드시 명시적 camelCase 익명 객체 사용
#### 금지 패턴
```csharp
// ❌ shorthand — "Id", "TagName"(PascalCase)이 JSON 키가 됨 → JS에서 undefined
return Ok(new { x.Id, x.TagName, x.NodeId });
// ❌ typed 객체 직접 반환 — PascalCase 키 → JS에서 undefined
return Ok(myDto);
return Ok(new MyRecord { Id = 1, TagName = "abc" });
```
#### 올바른 패턴
```csharp
// ✅ 명시적 소문자 키 — JS에서 r.id, r.tagName으로 정상 접근
return Ok(new
{
id = x.Id,
tagName = x.TagName,
nodeId = x.NodeId,
liveValue = x.LiveValue,
timestamp = x.Timestamp
});
// ✅ 컬렉션
return Ok(new
{
total = r.Total,
items = r.Items.Select(x => new
{
id = x.Id,
tagName = x.TagName,
nodeId = x.NodeId,
dataType = x.DataType
})
});
// ✅ C# 예약어 처리: @class → JSON "class"
return Ok(new
{
id = x.Id,
@class = x.Class, // JSON key: "class"
name = x.Name
});
```
### 프론트엔드 접근 방식 (참고)
```javascript
// app.js에서 모든 응답 필드를 camelCase로 접근
items.forEach(x => {
row.cells[0].textContent = x.id; // ← "id" (소문자)
row.cells[1].textContent = x.tagName; // ← "tagName" (camelCase)
row.cells[2].textContent = x.nodeId; // ← "nodeId"
});
```
### 발생 이력
이 규칙을 어기면 다음 증상이 나타난다:
- 테이블에 모든 셀이 빈칸 또는 `[undefined]`
- 브라우저 콘솔에 오류 없음 (값이 `undefined`이므로 조용히 실패)
- 서버 응답 자체는 정상 (Network 탭에서 데이터 확인 가능)
**실제 발생 사례** (2026-04-26):
- Browse 노드 목록: `n.NodeId`, `n.DisplayName``undefined` / "이름 없음"
- NodeMap 대시보드: `x.Id`, `x.Level`, `x.Class``undefined`
- PointBuilder 포인트 목록: `p.Id`, `p.TagName`, `p.LiveValue``undefined`
---
## 2. OPC UA SDK 버전 호환성 (v1.5.378.134)
### Session 생성
```csharp
// ❌ 구버전 — Session.Create()가 Task<ISession>을 반환하므로 cast 실패
var session = (ISession)Session.Create(config, endpoint, false, name, 60000, identity, null);
// ✅ 신버전
var session = await new DefaultSessionFactory(null).CreateAsync(
config, endpoint, false, name, 60_000, identity, null, CancellationToken.None);
```
### 인증서 검증 이벤트
`OpcUaConfigProvider.GetConfigAsync`에서 config를 빌드한 후 이벤트 핸들러를 등록해야 한다.
`ExperionOpcClient.BuildConfigAsync`는 실제로 호출되지 않는 dead code이므로 거기에 등록해도 무효.
```csharp
await config.ValidateAsync(ApplicationType.Client);
config.CertificateValidator.CertificateValidation += (_, e) => { e.Accept = true; };
```
---
## 3. 컨트롤러 응답 구조 체크리스트
컨트롤러에서 `Ok(...)` 사용 시 반드시 확인:
- [ ] 익명 객체의 **모든 키**가 camelCase인가? (`id`, `tagName`, `nodeId` ...)
- [ ] `new { x.SomeProp }` shorthand가 **전혀** 없는가?
- [ ] typed record/class를 `Ok()`에 **직접 전달**하지 않는가?
- [ ] C# 예약어(`class`)에 `@` 접두사를 붙였는가?

View File

@@ -1,29 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Web\ExperionCrawler.csproj" />
</ItemGroup>
</Project>

View File

@@ -1 +0,0 @@
global using Xunit;

View File

@@ -1,382 +0,0 @@
using ExperionCrawler.Core.Application.Services;
using Xunit;
namespace ExperionCrawler.Tests;
/// <summary>
/// KoreanTimeRangeExtractor 단위 테스트
/// 새로운 한글 시간 패턴 파싱 로직을 검증합니다.
/// </summary>
public class KoreanTimeRangeExtractorTests
{
private readonly KoreanTimeRangeExtractor _extractor;
private readonly KstClock _kstClock;
public KoreanTimeRangeExtractorTests()
{
// 테스트용 고정 시계: KST 2026-04-23 12:00:00 = UTC 2026-04-23 03:00:00
var fixedClock = new FixedClock(new DateTimeOffset(2026, 4, 23, 3, 0, 0, TimeSpan.Zero));
_kstClock = new KstClock(fixedClock);
_extractor = new KoreanTimeRangeExtractor(_kstClock);
}
#region : ~
[Fact]
public void AbsoluteRange_FromTo_KoreanDate()
{
// Arrange & Act
var result = _extractor.Extract("4월 3일 부터 4월 5일 까지");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
Assert.Null(result.PostgresInterval);
// KST 2026-04-03 00:00 ~ 2026-04-06 00:00 (까지의 다음날)
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(4, result.KstFrom.Value.Month);
Assert.Equal(3, result.KstFrom.Value.Day);
Assert.Equal(2026, result.KstTo!.Value.Year);
Assert.Equal(4, result.KstTo.Value.Month);
Assert.Equal(6, result.KstTo.Value.Day); // 다음날 00:00
}
[Fact]
public void AbsoluteRange_FromTo_IsoDate()
{
// Arrange & Act
var result = _extractor.Extract("2026-04-03 부터 2026-04-05 까지");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
}
[Fact]
public void AbsoluteRange_FromTo_WithTime()
{
// Arrange & Act
var result = _extractor.Extract("어제 09:00 부터 오늘 18:00 까지");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
// KST 2026-04-22 09:00 ~ 2026-04-23 18:00
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(4, result.KstFrom.Value.Month);
Assert.Equal(22, result.KstFrom.Value.Day);
Assert.Equal(9, result.KstFrom.Value.Hour);
Assert.Equal(2026, result.KstTo!.Value.Year);
Assert.Equal(4, result.KstTo.Value.Month);
Assert.Equal(23, result.KstTo.Value.Day);
Assert.Equal(18, result.KstTo.Value.Hour);
}
[Fact]
public void AbsoluteRange_FromTo_WithAmPm()
{
// Arrange & Act
var result = _extractor.Extract("오전 9시부터 오후 6시까지");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
// KST 2026-04-23 09:00 ~ 18:00
Assert.Equal(9, result.KstFrom!.Value.Hour);
Assert.Equal(18, result.KstTo!.Value.Hour);
}
[Fact]
public void AbsoluteRange_WithTimeComponent_PreservesTime()
{
// Arrange & Act
var result = _extractor.Extract("2026-03-01 14:00 부터 2026-03-01 16:30 까지");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
Assert.Equal(14, result.KstFrom!.Value.Hour);
Assert.Equal(0, result.KstFrom.Value.Minute);
Assert.Equal(16, result.KstTo!.Value.Hour);
Assert.Equal(30, result.KstTo.Value.Minute);
}
#endregion
#region : /
[Fact]
public void OneDirection_After()
{
// Arrange & Act
var result = _extractor.Extract("오늘 오후 2시 이후");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Null(result.KstTo);
Assert.Null(result.PostgresInterval);
// KST 2026-04-23 14:00
Assert.Equal(14, result.KstFrom!.Value.Hour);
}
[Fact]
public void OneDirection_Before()
{
// Arrange & Act
var result = _extractor.Extract("2026-05-05 이전");
// Assert
Assert.Null(result.KstFrom);
Assert.NotNull(result.KstTo);
Assert.Null(result.PostgresInterval);
// KST 2026-05-05
Assert.Equal(2026, result.KstTo!.Value.Year);
Assert.Equal(5, result.KstTo.Value.Month);
Assert.Equal(5, result.KstTo.Value.Day);
}
[Fact]
public void OneDirection_FromOnly()
{
// Arrange & Act
var result = _extractor.Extract("4월 3일 부터");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Null(result.KstTo);
Assert.Null(result.PostgresInterval);
}
#endregion
#region : / N시간||
[Theory]
[InlineData("최근 3시간", "3 hours")]
[InlineData("지난 2시간", "2 hours")]
[InlineData("최근 30분", "30 minutes")]
[InlineData("지난 15분", "15 minutes")]
[InlineData("최근 7일", "7 days")]
[InlineData("지난 30일", "30 days")]
[InlineData("최근 2주", "14 days")]
public void RelativeRange_CorrectInterval(string input, string expectedInterval)
{
// Arrange & Act
var result = _extractor.Extract(input);
// Assert
Assert.Equal(expectedInterval, result.PostgresInterval);
Assert.Null(result.KstFrom);
Assert.Null(result.KstTo);
}
#endregion
#region : //
[Fact]
public void NamedDay_Today()
{
// Arrange & Act
var result = _extractor.Extract("오늘");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
// KST 2026-04-23 00:00 ~ 2026-04-24 00:00
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(4, result.KstFrom.Value.Month);
Assert.Equal(23, result.KstFrom.Value.Day);
Assert.Equal(2026, result.KstTo!.Value.Year);
Assert.Equal(4, result.KstTo.Value.Month);
Assert.Equal(24, result.KstTo.Value.Day);
}
[Fact]
public void NamedDay_Yesterday()
{
// Arrange & Act
var result = _extractor.Extract("어제");
// Assert
Assert.NotNull(result.KstFrom);
Assert.NotNull(result.KstTo);
// KST 2026-04-22 00:00 ~ 2026-04-23 00:00
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(4, result.KstFrom.Value.Month);
Assert.Equal(22, result.KstFrom.Value.Day);
Assert.Equal(2026, result.KstTo!.Value.Year);
Assert.Equal(4, result.KstTo.Value.Month);
Assert.Equal(23, result.KstTo.Value.Day);
}
[Fact]
public void NamedDay_ThisWeek()
{
// Arrange & Act
var result = _extractor.Extract("이번 주");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Null(result.KstTo);
// KST 2026-04-20 (월요일 - 2026-04-23은 목요일이므로 3일 전)
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(4, result.KstFrom.Value.Month);
// 2026-04-23은 목요일(Thursday = 4), 월요일은 4-3 = 4-20
Assert.Equal(20, result.KstFrom.Value.Day);
}
#endregion
#region
[Fact]
public void Default_InvalidInput_ReturnsOneHourInterval()
{
// Arrange & Act
var result = _extractor.Extract("PV_101.PV 평균값");
// Assert
Assert.Equal("1 hour", result.PostgresInterval);
Assert.Null(result.KstFrom);
Assert.Null(result.KstTo);
}
[Fact]
public void Default_EmptyInput_ReturnsOneHourInterval()
{
// Arrange & Act
var result = _extractor.Extract("");
// Assert
Assert.Equal("1 hour", result.PostgresInterval);
Assert.Null(result.KstFrom);
Assert.Null(result.KstTo);
}
#endregion
#region ToSqlCondition Tests
[Fact]
public void ToSqlCondition_RelativeRange_UsesInterval()
{
// Arrange
var result = _extractor.Extract("최근 3시간");
// Act
var sql = result.ToSqlCondition("\"time\"", _kstClock);
// Assert
Assert.Equal("\"time\" >= NOW() - INTERVAL '3 hours'", sql);
}
[Fact]
public void ToSqlCondition_AbsoluteRange_BothSides()
{
// Arrange
var result = _extractor.Extract("오늘");
// Act
var sql = result.ToSqlCondition("\"time\"", _kstClock);
// Assert
// KST 2026-04-23 00:00 = UTC 2026-04-22 15:00
// KST 2026-04-24 00:00 = UTC 2026-04-23 15:00
Assert.Contains("\"time\" >=", sql);
Assert.Contains("AND \"time\" <", sql);
Assert.Contains("2026-04-22 15:00:00+00", sql);
Assert.Contains("2026-04-23 15:00:00+00", sql);
}
[Fact]
public void ToSqlCondition_FromOnly()
{
// Arrange
var result = _extractor.Extract("오늘 오후 2시 이후");
// Act
var sql = result.ToSqlCondition("\"time\"", _kstClock);
// Assert
// KST 2026-04-23 14:00 = UTC 2026-04-23 05:00
Assert.Contains("\"time\" >=", sql);
Assert.DoesNotContain("AND", sql);
Assert.Contains("2026-04-23 05:00:00+00", sql);
}
[Fact]
public void ToSqlCondition_ToOnly()
{
// Arrange
var result = _extractor.Extract("2026-05-05 이전");
// Act
var sql = result.ToSqlCondition("\"time\"", _kstClock);
// Assert
// KST 2026-05-05 00:00 = UTC 2026-05-04 15:00
Assert.Contains("\"time\" <", sql);
Assert.DoesNotContain("AND", sql);
Assert.Contains("2026-05-04 15:00:00+00", sql);
}
#endregion
#region ParseKstDateTime Tests (via Extract)
// ParseKstDateTime은 internal이므로 Extract를 통해 간접 테스트합니다.
[Theory]
[InlineData("오늘", "2026-04-23")]
[InlineData("어제", "2026-04-22")]
[InlineData("4월 3일", "2026-04-03")]
public void Extract_KoreanDate_ParsesCorrectly(string input, string expectedDate)
{
// Arrange & Act
var result = _extractor.Extract(input);
// Assert
Assert.NotNull(result.KstFrom);
Assert.Equal(expectedDate, result.KstFrom!.Value.ToString("yyyy-MM-dd"));
}
[Fact]
public void Extract_WithTime_PreservesTime()
{
// Arrange & Act
var result = _extractor.Extract("오늘 14:30 이후");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Equal(14, result.KstFrom!.Value.Hour);
Assert.Equal(30, result.KstFrom.Value.Minute);
}
[Fact]
public void Extract_WithAmPm_ConvertsTo24Hour()
{
// Arrange & Act
var result = _extractor.Extract("오후 3시 이후");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Equal(15, result.KstFrom!.Value.Hour);
}
[Fact]
public void Extract_IsoFormat_ParsesCorrectly()
{
// Arrange & Act
var result = _extractor.Extract("2026-03-01 14:00 이후");
// Assert
Assert.NotNull(result.KstFrom);
Assert.Equal(2026, result.KstFrom!.Value.Year);
Assert.Equal(3, result.KstFrom.Value.Month);
Assert.Equal(1, result.KstFrom.Value.Day);
Assert.Equal(14, result.KstFrom.Value.Hour);
}
#endregion
}

View File

@@ -1,188 +0,0 @@
using ExperionCrawler.Core.Application.DTOs;
using ExperionCrawler.Core.Application.Services;
namespace ExperionCrawler.Tests;
/// <summary>
/// SqlValidator 다단계 검증기 테스트
/// 검증 순서: ①구조 → ②위험키워드 → ③금지절 → ④함수화이트리스트 → ⑤테이블참조 → ⑥서브쿼리깊이 → ⑦의심패턴
/// </summary>
public class SqlValidatorTests
{
private readonly SqlValidator _v;
public SqlValidatorTests()
{
_v = new SqlValidator(new SqlValidatorOptions
{
RequiredTables = ["measurements"],
AllowedTables = ["measurements", "node_map_master"],
MaxSubqueryDepth = 4
});
}
// ── ① 정상 케이스 (SELECT 전용, 허용 함수/테이블) ────────────────────────
[Theory]
[InlineData("SELECT AVG(value) FROM measurements WHERE tagname = 'PV_101.PV'")]
[InlineData("SELECT date_trunc('minute', time), AVG(value) FROM measurements GROUP BY 1")]
[InlineData("SELECT tagname, REGR_SLOPE(value, EXTRACT(EPOCH FROM time)) FROM measurements GROUP BY tagname")]
[InlineData("SELECT date_trunc('minute', time), first(value, time) FROM measurements WHERE tagname = 'test' GROUP BY 1 ORDER BY 1")]
[InlineData("SELECT * FROM measurements WHERE tagname = 'test' AND time > now() - interval '1 hour'")]
public void ValidSql_ShouldPass(string sql)
{
var result = _v.Validate(sql);
Assert.True(result.IsValid, $"Expected valid but failed: {result.Message}");
}
// ── ① SELECT 전용 검사 ─────────────────────────────────────────────────
[Theory]
[InlineData("DROP TABLE measurements", ValidationFailReason.NotSelectStatement)]
[InlineData("CREATE TABLE test (id INT)", ValidationFailReason.NotSelectStatement)]
[InlineData("ALTER TABLE measurements ADD COLUMN x INT", ValidationFailReason.NotSelectStatement)]
[InlineData("TRUNCATE measurements", ValidationFailReason.NotSelectStatement)]
[InlineData("MERGE INTO measurements USING...", ValidationFailReason.NotSelectStatement)]
[InlineData("INSERT INTO measurements VALUES (1, 2, 3)", ValidationFailReason.NotSelectStatement)]
[InlineData("UPDATE measurements SET value = 1", ValidationFailReason.NotSelectStatement)]
[InlineData("DELETE FROM measurements", ValidationFailReason.NotSelectStatement)]
public void NonSelectStatement_ShouldFail(string sql, ValidationFailReason expected)
{
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(expected, result.Reason);
}
// ── ② 위험 키워드 검사 (SELECT로 시작하지만 위험 키워드 포함) ─────────────
[Theory]
[InlineData("SELECT * FROM measurements; DROP TABLE measurements", ValidationFailReason.DangerousKeyword)]
[InlineData("SELECT * FROM measurements; INSERT INTO other VALUES (1)", ValidationFailReason.DangerousKeyword)]
[InlineData("SELECT * FROM measurements; COMMIT", ValidationFailReason.DangerousKeyword)]
public void DangerousKeywordAfterSelect_ShouldFail(string sql, ValidationFailReason expected)
{
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(expected, result.Reason);
}
// ── ③ 금지 절 검사 (SELECT로 시작하지만 금지 절 포함) ─────────────────────
[Theory]
[InlineData("SELECT pg_sleep(5)", ValidationFailReason.ForbiddenClause)]
[InlineData("SELECT pg_cancel_backend(1)", ValidationFailReason.ForbiddenClause)]
[InlineData("SELECT pg_terminate_backend(1)", ValidationFailReason.ForbiddenClause)]
// CALL/EXECUTE는 SELECT로 시작하지 않으므로 NotSelectStatement로 먼저 실패
public void ForbiddenClause_ShouldFail(string sql, ValidationFailReason expected)
{
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(expected, result.Reason);
}
// ── ④ 허용되지 않는 함수 ────────────────────────────────────────────────
[Theory]
[InlineData("SELECT SYSTEM('ls') FROM measurements", ValidationFailReason.DisallowedFunction)]
[InlineData("SELECT COPY_FILE('/tmp') FROM measurements", ValidationFailReason.DisallowedFunction)]
[InlineData("SELECT NON_EXISTING_FUNC(1) FROM measurements", ValidationFailReason.DisallowedFunction)]
public void DisallowedFunction_ShouldFail(string sql, ValidationFailReason expected)
{
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(expected, result.Reason);
}
// ── ⑤ 필수 테이블 누락 ─────────────────────────────────────────────────
[Fact]
public void MissingRequiredTable_ShouldFail()
{
var sql = "SELECT 1 FROM node_map_master";
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(ValidationFailReason.MissingRequiredTable, result.Reason);
}
// ── ⑦ 의심 패턴 검사 (시스템 뷰 접근) ───────────────────────────────────
[Fact]
public void SystemViewAccess_ShouldFail()
{
// measurements가 없어서 MissingRequiredTable로 먼저 실패
var sql = "SELECT * FROM information_schema.tables";
var result = _v.Validate(sql);
Assert.False(result.IsValid);
// 필수 테이블 measurements가 없어서 MissingRequiredTable로 실패
Assert.Equal(ValidationFailReason.MissingRequiredTable, result.Reason);
}
// ── SQL Injection 패턴 ──────────────────────────────────────────────────
[Theory]
[InlineData("SELECT * FROM measurements WHERE tagname = '' OR '1'='1'", ValidationFailReason.SuspiciousPattern)]
[InlineData("SELECT * FROM measurements UNION SELECT * FROM measurements", ValidationFailReason.SuspiciousPattern)]
[InlineData("SELECT * FROM measurements WHERE tagname = 'x'; DROP TABLE measurements", ValidationFailReason.DangerousKeyword)]
public void InjectionPattern_ShouldFail(string sql, ValidationFailReason expected)
{
var result = _v.Validate(sql);
Assert.False(result.IsValid);
Assert.Equal(expected, result.Reason);
}
// ── ⑥ 서브쿼리 깊이 ────────────────────────────────────────────────────
[Fact]
public void SubqueryDepthExceeded_ShouldFail()
{
// 5단계 중첩: ( -> ( -> ( -> ( -> ( -> (SELECT f FROM measurements)
// MaxSubqueryDepth = 4이므로 5단계에서 실패
// 괄호 개수: 5개여야 함
var sql = "SELECT a FROM (SELECT b FROM (SELECT c FROM (SELECT d FROM (SELECT e FROM (SELECT f FROM measurements) AS x0) AS x1) AS x2) AS x3) AS x4";
var result = _v.Validate(sql);
Assert.False(result.IsValid, $"Expected invalid but got valid. Message: {result.Message}");
Assert.Equal(ValidationFailReason.SubqueryDepthExceeded, result.Reason);
}
// ── 빈 입력 ────────────────────────────────────────────────────────────
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void EmptyInput_ShouldFail(string? sql)
{
var result = _v.Validate(sql!);
Assert.False(result.IsValid);
Assert.Equal(ValidationFailReason.EmptyInput, result.Reason);
}
// ── 허용 함수 화이트리스트 (정규 케이스) ─────────────────────────────────
[Theory]
[InlineData("SELECT COUNT(*) FROM measurements")]
[InlineData("SELECT SUM(value), AVG(value), MIN(value), MAX(value) FROM measurements")]
[InlineData("SELECT STDDEV(value), VARIANCE(value) FROM measurements")]
[InlineData("SELECT ROW_NUMBER() OVER (ORDER BY time) FROM measurements")]
[InlineData("SELECT RANK() OVER (PARTITION BY tagname ORDER BY value DESC) FROM measurements")]
[InlineData("SELECT LAG(value, 1) OVER (ORDER BY time) FROM measurements")]
[InlineData("SELECT NOW(), CURRENT_TIMESTAMP, CURRENT_DATE FROM measurements")]
[InlineData("SELECT DATE_TRUNC('hour', time), EXTRACT(EPOCH FROM time) FROM measurements")]
[InlineData("SELECT TIME_BUCKET('1 hour', time) FROM measurements")]
[InlineData("SELECT UPPER(tagname), LOWER(tagname), TRIM(tagname) FROM measurements")]
[InlineData("SELECT COALESCE(value, 0), NULLIF(value, -1) FROM measurements")]
[InlineData("SELECT ROUND(value), CEIL(value), FLOOR(value) FROM measurements")]
public void AllowedFunctions_ShouldPass(string sql)
{
var result = _v.Validate(sql);
Assert.True(result.IsValid, $"Expected valid but failed: {result.Message}");
}
// ── Deconstruct 테스트 ─────────────────────────────────────────────────
[Fact]
public void ValidationResult_Deconstruct_ShouldWork()
{
var result = _v.Validate("SELECT 1 FROM measurements");
var (ok, error) = result;
Assert.True(ok);
Assert.Null(error);
}
[Fact]
public void ValidationResult_Deconstruct_Fail_ShouldWork()
{
var result = _v.Validate("");
var (ok, error) = result;
Assert.False(ok);
Assert.NotNull(error);
}
}

View File

@@ -1,220 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<TestRun id="ff720602-c42d-4714-8a4d-fe329014b49f" name="@spark 2026-04-25 11:44:29" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Times creation="2026-04-25T11:44:29.4304710+09:00" queuing="2026-04-25T11:44:29.4304710+09:00" start="2026-04-25T11:44:28.6442347+09:00" finish="2026-04-25T11:44:29.4827041+09:00" />
<TestSettings name="default" id="d2041d3e-230c-4156-99fc-c431da3496f7">
<Deployment runDeploymentRoot="_spark_2026-04-25_11_44_29" />
</TestSettings>
<Results>
<UnitTestResult executionId="fa8e7505-2eb7-479a-9859-d8d21a234118" testId="5c3e9ce7-7bd1-deb3-bcf9-286a5a797173" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent1Month_ExtractsTimeRange" computerName="spark" duration="00:00:00.0003819" startTime="2026-04-25T11:44:29.3898328+09:00" endTime="2026-04-25T11:44:29.3898328+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="fa8e7505-2eb7-479a-9859-d8d21a234118" />
<UnitTestResult executionId="6342897b-79d5-45f1-b779-b21463d71914" testId="4cc079cc-8497-c2b1-a13f-371856fe1a6d" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AverageKeyword_UsesAvgFunction" computerName="spark" duration="00:00:00.0002663" startTime="2026-04-25T11:44:29.3906626+09:00" endTime="2026-04-25T11:44:29.3906626+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="6342897b-79d5-45f1-b779-b21463d71914" />
<UnitTestResult executionId="4f8df51a-e50e-4781-9f88-143d0368cb20" testId="453ebd31-b897-172a-47b2-ee9237e423e2" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithTagName_GeneratesSql" computerName="spark" duration="00:00:00.0003165" startTime="2026-04-25T11:44:29.3743595+09:00" endTime="2026-04-25T11:44:29.3743596+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="4f8df51a-e50e-4781-9f88-143d0368cb20" />
<UnitTestResult executionId="d35ec836-f0ec-43c4-ac25-7b50d7dbdba0" testId="80269386-9ca6-1dd2-59e4-7b56fd7a66ed" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_SpecialCharacters_EscapesTagName" computerName="spark" duration="00:00:00.0002507" startTime="2026-04-25T11:44:29.3907344+09:00" endTime="2026-04-25T11:44:29.3907344+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="d35ec836-f0ec-43c4-ac25-7b50d7dbdba0" />
<UnitTestResult executionId="6f313979-2da5-4dfe-9c34-27e3ce65743b" testId="8df30334-5f76-38b5-25e9-f6812aa6cdff" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_ThreeTagsCommaSeparated_IncludesAllTags" computerName="spark" duration="00:00:00.0003450" startTime="2026-04-25T11:44:29.3898916+09:00" endTime="2026-04-25T11:44:29.3898916+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="6f313979-2da5-4dfe-9c34-27e3ce65743b" />
<UnitTestResult executionId="5c45a8b9-2eb2-427c-a731-414f909b7048" testId="6b5ec284-00db-56ef-7384-a5713f6ab45e" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent24Hours_ExtractsTimeRange" computerName="spark" duration="00:00:00.0002515" startTime="2026-04-25T11:44:29.3757051+09:00" endTime="2026-04-25T11:44:29.3757051+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="5c45a8b9-2eb2-427c-a731-414f909b7048" />
<UnitTestResult executionId="4da36672-445e-4369-9d44-360a7b6b8b99" testId="e0331b39-ee4e-8fe1-50ed-78732fa3841a" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_DotTagName_ExtractsCorrectly" computerName="spark" duration="00:00:00.0005555" startTime="2026-04-25T11:44:29.3909704+09:00" endTime="2026-04-25T11:44:29.3909705+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="4da36672-445e-4369-9d44-360a7b6b8b99" />
<UnitTestResult executionId="90650188-4846-4d68-bc39-f7a4c8fb9814" testId="9dcebd05-5954-2a01-645b-410629bd254d" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithMinKeyword_GeneratesMinAggregation" computerName="spark" duration="00:00:00.0002630" startTime="2026-04-25T11:44:29.3905817+09:00" endTime="2026-04-25T11:44:29.3905817+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="90650188-4846-4d68-bc39-f7a4c8fb9814" />
<UnitTestResult executionId="d8210a41-f48e-416c-9e6f-b4b5cd096013" testId="4c2d002b-4d1c-3c3b-855d-00377118994d" testName="ExperionCrawler.Tests.TextToSqlServiceTests.SuggestQueriesAsync_WithFilter_ReturnsFilteredSuggestions" computerName="spark" duration="00:00:00.0006235" startTime="2026-04-25T11:44:29.3898144+09:00" endTime="2026-04-25T11:44:29.3898144+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="d8210a41-f48e-416c-9e6f-b4b5cd096013" />
<UnitTestResult executionId="7ee38996-5e67-4144-9280-d066fa103dcd" testId="15e84926-8d29-6d29-d5f1-18dd26763892" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsWithKoreanDescriptions_ExtractsOnlyTags" computerName="spark" duration="00:00:00.0003433" startTime="2026-04-25T11:44:29.3906261+09:00" endTime="2026-04-25T11:44:29.3906261+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="7ee38996-5e67-4144-9280-d066fa103dcd" />
<UnitTestResult executionId="4101239d-abc2-45e3-9aeb-32e835964cf7" testId="cf1eb7a6-cd60-600b-bfaa-e1f835f77b59" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithEmptyInput_ThrowsArgumentException" computerName="spark" duration="00:00:00.0009436" startTime="2026-04-25T11:44:29.3909881+09:00" endTime="2026-04-25T11:44:29.3909882+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="4101239d-abc2-45e3-9aeb-32e835964cf7" />
<UnitTestResult executionId="4248d846-c27e-486b-979b-89e6b55ae83b" testId="14f39cbb-10de-141a-f514-4230ebe336ed" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithFirstKeyword_GeneratesFirstAggregation" computerName="spark" duration="00:00:00.0002743" startTime="2026-04-25T11:44:29.3906445+09:00" endTime="2026-04-25T11:44:29.3906445+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="4248d846-c27e-486b-979b-89e6b55ae83b" />
<UnitTestResult executionId="0c3ab594-4d65-4c0b-bcca-a10ee0f58a0f" testId="df534781-ce1f-66fc-3790-0b8e5eae75df" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_SimpleTagName_ExtractsCorrectly" computerName="spark" duration="00:00:00.0002892" startTime="2026-04-25T11:44:29.3897951+09:00" endTime="2026-04-25T11:44:29.3897951+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="0c3ab594-4d65-4c0b-bcca-a10ee0f58a0f" />
<UnitTestResult executionId="9627a0d8-e645-4db1-9ed7-54afb8d37be0" testId="cea17860-3597-3e21-1465-62e77aee9cc6" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithMaxKeyword_GeneratesMaxAggregation" computerName="spark" duration="00:00:00.0002379" startTime="2026-04-25T11:44:29.3909333+09:00" endTime="2026-04-25T11:44:29.3909333+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="9627a0d8-e645-4db1-9ed7-54afb8d37be0" />
<UnitTestResult executionId="5ddb25ec-fe9d-46a7-ac6d-91d8e60c465b" testId="675f7860-eb45-3219-27e7-57c7e2e65ee2" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoSpaceTagName_UsesWholeInput" computerName="spark" duration="00:00:00.0002750" startTime="2026-04-25T11:44:29.3898738+09:00" endTime="2026-04-25T11:44:29.3898738+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="5ddb25ec-fe9d-46a7-ac6d-91d8e60c465b" />
<UnitTestResult executionId="6ac7aa3c-408f-47d3-9d4d-ef44ccf8abab" testId="c0c4b062-d710-d2dc-7fb6-02e20736a39f" testName="ExperionCrawler.Tests.TextToSqlServiceTests.SuggestQueriesAsync_WithEmptyInput_ReturnsAllSuggestions" computerName="spark" duration="00:00:00.0009178" startTime="2026-04-25T11:44:29.3906982+09:00" endTime="2026-04-25T11:44:29.3906983+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="6ac7aa3c-408f-47d3-9d4d-ef44ccf8abab" />
<UnitTestResult executionId="0bd3a654-47ed-4a79-a283-1e1e11baf6e4" testId="03b464c4-872d-93f1-75bf-cc169416edee" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent7Days_ExtractsTimeRange" computerName="spark" duration="00:00:00.0003093" startTime="2026-04-25T11:44:29.3907158+09:00" endTime="2026-04-25T11:44:29.3907158+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="0bd3a654-47ed-4a79-a283-1e1e11baf6e4" />
<UnitTestResult executionId="2af16c71-455e-47d1-9414-06fdbbb0c9f4" testId="a588adc0-4f17-3b17-110a-4aa6f024dd3f" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_FromToPattern_ExtractsTimeRange" computerName="spark" duration="00:00:00.0198781" startTime="2026-04-25T11:44:29.3713915+09:00" endTime="2026-04-25T11:44:29.3714001+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="2af16c71-455e-47d1-9414-06fdbbb0c9f4" />
<UnitTestResult executionId="a892d81e-6c53-49a5-aa88-60a4c3313472" testId="dfce6d90-9761-4ca3-ca30-781ba02dbfa4" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsCommaSeparated_IncludesAllTags" computerName="spark" duration="00:00:00.0004279" startTime="2026-04-25T11:44:29.3754339+09:00" endTime="2026-04-25T11:44:29.3754339+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="a892d81e-6c53-49a5-aa88-60a4c3313472" />
<UnitTestResult executionId="e60f083e-00ce-452d-837d-cfeafda675ee" testId="77b9b877-6d45-d631-10c9-19432d683af4" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_KoreanDate_ExtractsTimeRange" computerName="spark" duration="00:00:00.0002391" startTime="2026-04-25T11:44:29.3899269+09:00" endTime="2026-04-25T11:44:29.3899269+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="e60f083e-00ce-452d-837d-cfeafda675ee" />
<UnitTestResult executionId="de69eaaf-6366-4889-94c1-d0281deb012c" testId="750cf791-c506-cfa6-4deb-79897ece87ff" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_DefaultAggregationIsLast" computerName="spark" duration="00:00:00.0002543" startTime="2026-04-25T11:44:29.3899092+09:00" endTime="2026-04-25T11:44:29.3899092+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="de69eaaf-6366-4889-94c1-d0281deb012c" />
<UnitTestResult executionId="801f328a-0b75-4735-9e21-37e6d04314ba" testId="73098855-7d72-81ed-193c-c79d76370f85" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithLastKeyword_GeneratesLastAggregation" computerName="spark" duration="00:00:00.0024067" startTime="2026-04-25T11:44:29.3736241+09:00" endTime="2026-04-25T11:44:29.3736241+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="801f328a-0b75-4735-9e21-37e6d04314ba" />
<UnitTestResult executionId="24ec31bb-738d-4cba-968e-ddc8ac40d249" testId="7ff2886d-a78d-22be-1cf0-6826794967ab" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AfterPattern_ExtractsTimeRange" computerName="spark" duration="00:00:00.0002320" startTime="2026-04-25T11:44:29.3909150+09:00" endTime="2026-04-25T11:44:29.3909150+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="24ec31bb-738d-4cba-968e-ddc8ac40d249" />
<UnitTestResult executionId="835eeb43-fcf4-44c6-a160-4f9fd35cced8" testId="64e7212f-9c18-5a8c-19cd-01e0da0ea86f" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MaxKeyword_UsesMaxFunction" computerName="spark" duration="00:00:00.0003019" startTime="2026-04-25T11:44:29.3746816+09:00" endTime="2026-04-25T11:44:29.3746816+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="835eeb43-fcf4-44c6-a160-4f9fd35cced8" />
<UnitTestResult executionId="83fce793-2e88-4b7d-91f4-af2a95592bbb" testId="16a4f7ee-8c4f-8b19-bf63-11f177c56cc6" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent1Hour_ExtractsTimeRange" computerName="spark" duration="00:00:00.0002834" startTime="2026-04-25T11:44:29.3749850+09:00" endTime="2026-04-25T11:44:29.3749850+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="83fce793-2e88-4b7d-91f4-af2a95592bbb" />
<UnitTestResult executionId="0f19eebe-6bd1-4ae8-817c-8a4c01858476" testId="6bbded63-64a7-bc4b-baa2-42bdfaed94e5" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoTimeSpecified_UsesDefault" computerName="spark" duration="00:00:00.0002681" startTime="2026-04-25T11:44:29.3906054+09:00" endTime="2026-04-25T11:44:29.3906055+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="0f19eebe-6bd1-4ae8-817c-8a4c01858476" />
<UnitTestResult executionId="be8411f0-aa2c-4cab-a30c-584e1d59a747" testId="0a6fd87b-3482-8683-9f78-54911a84c958" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoAggregateKeyword_UsesLastFunction" computerName="spark" duration="00:00:00.0002227" startTime="2026-04-25T11:44:29.3909510+09:00" endTime="2026-04-25T11:44:29.3909511+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="be8411f0-aa2c-4cab-a30c-584e1d59a747" />
<UnitTestResult executionId="9e9a83b0-c91a-4a52-978c-75690f51a6c8" testId="dcefabeb-f8ef-a09c-8b99-566f8d7f5f75" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_OpcUaNodeId_ExtractsCorrectly" computerName="spark" duration="00:00:00.0004160" startTime="2026-04-25T11:44:29.3896896+09:00" endTime="2026-04-25T11:44:29.3896896+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="9e9a83b0-c91a-4a52-978c-75690f51a6c8" />
<UnitTestResult executionId="6b62004e-a542-49df-9eaa-8b1d8ebbd96b" testId="6c884139-c4ff-db15-9260-81c43984d859" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsWithAbsTimeRange_IncludesAllTags" computerName="spark" duration="00:00:00.0002822" startTime="2026-04-25T11:44:29.3760063+09:00" endTime="2026-04-25T11:44:29.3760063+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="6b62004e-a542-49df-9eaa-8b1d8ebbd96b" />
<UnitTestResult executionId="cb48f6dc-fa21-4f1d-a8f9-febab7d25fc9" testId="37e9c535-a3c6-2083-f2c0-987d98ae10fa" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AvgKeyword_UsesAvgFunction" computerName="spark" duration="00:00:00.0003775" startTime="2026-04-25T11:44:29.3906806+09:00" endTime="2026-04-25T11:44:29.3906806+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="cb48f6dc-fa21-4f1d-a8f9-febab7d25fc9" />
<UnitTestResult executionId="cb24fe3d-f7fd-4740-94b6-490e0d7bab76" testId="7df9fd70-57c9-fe80-db0e-4c9bb3029efe" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MinKeyword_UsesMinFunction" computerName="spark" duration="00:00:00.0003823" startTime="2026-04-25T11:44:29.3740212+09:00" endTime="2026-04-25T11:44:29.3740212+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="cb24fe3d-f7fd-4740-94b6-490e0d7bab76" />
<UnitTestResult executionId="7c5468e1-7168-4a8a-a0ae-ad6784346495" testId="22b5066e-140f-d997-79ae-a2cc60ca7ac9" testName="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_TodayAmPmRange_ExtractsTimeRange" computerName="spark" duration="00:00:00.0002857" startTime="2026-04-25T11:44:29.3898553+09:00" endTime="2026-04-25T11:44:29.3898554+09:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="7c5468e1-7168-4a8a-a0ae-ad6784346495" />
</Results>
<TestDefinitions>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_ThreeTagsCommaSeparated_IncludesAllTags" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="8df30334-5f76-38b5-25e9-f6812aa6cdff">
<Execution id="6f313979-2da5-4dfe-9c34-27e3ce65743b" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_ThreeTagsCommaSeparated_IncludesAllTags" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_DotTagName_ExtractsCorrectly" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="e0331b39-ee4e-8fe1-50ed-78732fa3841a">
<Execution id="4da36672-445e-4369-9d44-360a7b6b8b99" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_DotTagName_ExtractsCorrectly" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent24Hours_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="6b5ec284-00db-56ef-7384-a5713f6ab45e">
<Execution id="5c45a8b9-2eb2-427c-a731-414f909b7048" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_Recent24Hours_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_KoreanDate_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="77b9b877-6d45-d631-10c9-19432d683af4">
<Execution id="e60f083e-00ce-452d-837d-cfeafda675ee" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_KoreanDate_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AverageKeyword_UsesAvgFunction" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="4cc079cc-8497-c2b1-a13f-371856fe1a6d">
<Execution id="6342897b-79d5-45f1-b779-b21463d71914" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_AverageKeyword_UsesAvgFunction" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MaxKeyword_UsesMaxFunction" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="64e7212f-9c18-5a8c-19cd-01e0da0ea86f">
<Execution id="835eeb43-fcf4-44c6-a160-4f9fd35cced8" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_MaxKeyword_UsesMaxFunction" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsWithAbsTimeRange_IncludesAllTags" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="6c884139-c4ff-db15-9260-81c43984d859">
<Execution id="6b62004e-a542-49df-9eaa-8b1d8ebbd96b" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_MultipleTagsWithAbsTimeRange_IncludesAllTags" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsCommaSeparated_IncludesAllTags" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="dfce6d90-9761-4ca3-ca30-781ba02dbfa4">
<Execution id="a892d81e-6c53-49a5-aa88-60a4c3313472" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_MultipleTagsCommaSeparated_IncludesAllTags" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_OpcUaNodeId_ExtractsCorrectly" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="dcefabeb-f8ef-a09c-8b99-566f8d7f5f75">
<Execution id="9e9a83b0-c91a-4a52-978c-75690f51a6c8" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_OpcUaNodeId_ExtractsCorrectly" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithEmptyInput_ThrowsArgumentException" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="cf1eb7a6-cd60-600b-bfaa-e1f835f77b59">
<Execution id="4101239d-abc2-45e3-9aeb-32e835964cf7" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithEmptyInput_ThrowsArgumentException" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AfterPattern_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="7ff2886d-a78d-22be-1cf0-6826794967ab">
<Execution id="24ec31bb-738d-4cba-968e-ddc8ac40d249" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_AfterPattern_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.SuggestQueriesAsync_WithEmptyInput_ReturnsAllSuggestions" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="c0c4b062-d710-d2dc-7fb6-02e20736a39f">
<Execution id="6ac7aa3c-408f-47d3-9d4d-ef44ccf8abab" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="SuggestQueriesAsync_WithEmptyInput_ReturnsAllSuggestions" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_AvgKeyword_UsesAvgFunction" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="37e9c535-a3c6-2083-f2c0-987d98ae10fa">
<Execution id="cb48f6dc-fa21-4f1d-a8f9-febab7d25fc9" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_AvgKeyword_UsesAvgFunction" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MultipleTagsWithKoreanDescriptions_ExtractsOnlyTags" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="15e84926-8d29-6d29-d5f1-18dd26763892">
<Execution id="7ee38996-5e67-4144-9280-d066fa103dcd" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_MultipleTagsWithKoreanDescriptions_ExtractsOnlyTags" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithFirstKeyword_GeneratesFirstAggregation" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="14f39cbb-10de-141a-f514-4230ebe336ed">
<Execution id="4248d846-c27e-486b-979b-89e6b55ae83b" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithFirstKeyword_GeneratesFirstAggregation" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_FromToPattern_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="a588adc0-4f17-3b17-110a-4aa6f024dd3f">
<Execution id="2af16c71-455e-47d1-9414-06fdbbb0c9f4" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_FromToPattern_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithTagName_GeneratesSql" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="453ebd31-b897-172a-47b2-ee9237e423e2">
<Execution id="4f8df51a-e50e-4781-9f88-143d0368cb20" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithTagName_GeneratesSql" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_SpecialCharacters_EscapesTagName" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="80269386-9ca6-1dd2-59e4-7b56fd7a66ed">
<Execution id="d35ec836-f0ec-43c4-ac25-7b50d7dbdba0" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_SpecialCharacters_EscapesTagName" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoSpaceTagName_UsesWholeInput" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="675f7860-eb45-3219-27e7-57c7e2e65ee2">
<Execution id="5ddb25ec-fe9d-46a7-ac6d-91d8e60c465b" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_NoSpaceTagName_UsesWholeInput" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.SuggestQueriesAsync_WithFilter_ReturnsFilteredSuggestions" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="4c2d002b-4d1c-3c3b-855d-00377118994d">
<Execution id="d8210a41-f48e-416c-9e6f-b4b5cd096013" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="SuggestQueriesAsync_WithFilter_ReturnsFilteredSuggestions" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent1Month_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="5c3e9ce7-7bd1-deb3-bcf9-286a5a797173">
<Execution id="fa8e7505-2eb7-479a-9859-d8d21a234118" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_Recent1Month_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoTimeSpecified_UsesDefault" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="6bbded63-64a7-bc4b-baa2-42bdfaed94e5">
<Execution id="0f19eebe-6bd1-4ae8-817c-8a4c01858476" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_NoTimeSpecified_UsesDefault" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithMinKeyword_GeneratesMinAggregation" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="9dcebd05-5954-2a01-645b-410629bd254d">
<Execution id="90650188-4846-4d68-bc39-f7a4c8fb9814" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithMinKeyword_GeneratesMinAggregation" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent7Days_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="03b464c4-872d-93f1-75bf-cc169416edee">
<Execution id="0bd3a654-47ed-4a79-a283-1e1e11baf6e4" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_Recent7Days_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithLastKeyword_GeneratesLastAggregation" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="73098855-7d72-81ed-193c-c79d76370f85">
<Execution id="801f328a-0b75-4735-9e21-37e6d04314ba" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithLastKeyword_GeneratesLastAggregation" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_DefaultAggregationIsLast" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="750cf791-c506-cfa6-4deb-79897ece87ff">
<Execution id="de69eaaf-6366-4889-94c1-d0281deb012c" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_DefaultAggregationIsLast" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_SimpleTagName_ExtractsCorrectly" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="df534781-ce1f-66fc-3790-0b8e5eae75df">
<Execution id="0c3ab594-4d65-4c0b-bcca-a10ee0f58a0f" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_SimpleTagName_ExtractsCorrectly" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_TodayAmPmRange_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="22b5066e-140f-d997-79ae-a2cc60ca7ac9">
<Execution id="7c5468e1-7168-4a8a-a0ae-ad6784346495" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_TodayAmPmRange_ExtractsTimeRange" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_WithMaxKeyword_GeneratesMaxAggregation" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="cea17860-3597-3e21-1465-62e77aee9cc6">
<Execution id="9627a0d8-e645-4db1-9ed7-54afb8d37be0" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_WithMaxKeyword_GeneratesMaxAggregation" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_NoAggregateKeyword_UsesLastFunction" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="0a6fd87b-3482-8683-9f78-54911a84c958">
<Execution id="be8411f0-aa2c-4cab-a30c-584e1d59a747" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_NoAggregateKeyword_UsesLastFunction" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_MinKeyword_UsesMinFunction" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="7df9fd70-57c9-fe80-db0e-4c9bb3029efe">
<Execution id="cb24fe3d-f7fd-4740-94b6-490e0d7bab76" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_MinKeyword_UsesMinFunction" />
</UnitTest>
<UnitTest name="ExperionCrawler.Tests.TextToSqlServiceTests.ParseNaturalLanguageAsync_Recent1Hour_ExtractsTimeRange" storage="/home/windpacer/projects/experioncrawler/experioncrawler.tests/bin/debug/net8.0/experioncrawler.tests.dll" id="16a4f7ee-8c4f-8b19-bf63-11f177c56cc6">
<Execution id="83fce793-2e88-4b7d-91f4-af2a95592bbb" />
<TestMethod codeBase="/home/windpacer/projects/ExperionCrawler/ExperionCrawler.Tests/bin/Debug/net8.0/ExperionCrawler.Tests.dll" adapterTypeName="executor://xunit/VsTestRunner2/netcoreapp" className="ExperionCrawler.Tests.TextToSqlServiceTests" name="ParseNaturalLanguageAsync_Recent1Hour_ExtractsTimeRange" />
</UnitTest>
</TestDefinitions>
<TestEntries>
<TestEntry testId="5c3e9ce7-7bd1-deb3-bcf9-286a5a797173" executionId="fa8e7505-2eb7-479a-9859-d8d21a234118" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="4cc079cc-8497-c2b1-a13f-371856fe1a6d" executionId="6342897b-79d5-45f1-b779-b21463d71914" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="453ebd31-b897-172a-47b2-ee9237e423e2" executionId="4f8df51a-e50e-4781-9f88-143d0368cb20" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="80269386-9ca6-1dd2-59e4-7b56fd7a66ed" executionId="d35ec836-f0ec-43c4-ac25-7b50d7dbdba0" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="8df30334-5f76-38b5-25e9-f6812aa6cdff" executionId="6f313979-2da5-4dfe-9c34-27e3ce65743b" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="6b5ec284-00db-56ef-7384-a5713f6ab45e" executionId="5c45a8b9-2eb2-427c-a731-414f909b7048" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="e0331b39-ee4e-8fe1-50ed-78732fa3841a" executionId="4da36672-445e-4369-9d44-360a7b6b8b99" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="9dcebd05-5954-2a01-645b-410629bd254d" executionId="90650188-4846-4d68-bc39-f7a4c8fb9814" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="4c2d002b-4d1c-3c3b-855d-00377118994d" executionId="d8210a41-f48e-416c-9e6f-b4b5cd096013" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="15e84926-8d29-6d29-d5f1-18dd26763892" executionId="7ee38996-5e67-4144-9280-d066fa103dcd" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="cf1eb7a6-cd60-600b-bfaa-e1f835f77b59" executionId="4101239d-abc2-45e3-9aeb-32e835964cf7" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="14f39cbb-10de-141a-f514-4230ebe336ed" executionId="4248d846-c27e-486b-979b-89e6b55ae83b" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="df534781-ce1f-66fc-3790-0b8e5eae75df" executionId="0c3ab594-4d65-4c0b-bcca-a10ee0f58a0f" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="cea17860-3597-3e21-1465-62e77aee9cc6" executionId="9627a0d8-e645-4db1-9ed7-54afb8d37be0" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="675f7860-eb45-3219-27e7-57c7e2e65ee2" executionId="5ddb25ec-fe9d-46a7-ac6d-91d8e60c465b" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="c0c4b062-d710-d2dc-7fb6-02e20736a39f" executionId="6ac7aa3c-408f-47d3-9d4d-ef44ccf8abab" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="03b464c4-872d-93f1-75bf-cc169416edee" executionId="0bd3a654-47ed-4a79-a283-1e1e11baf6e4" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="a588adc0-4f17-3b17-110a-4aa6f024dd3f" executionId="2af16c71-455e-47d1-9414-06fdbbb0c9f4" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="dfce6d90-9761-4ca3-ca30-781ba02dbfa4" executionId="a892d81e-6c53-49a5-aa88-60a4c3313472" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="77b9b877-6d45-d631-10c9-19432d683af4" executionId="e60f083e-00ce-452d-837d-cfeafda675ee" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="750cf791-c506-cfa6-4deb-79897ece87ff" executionId="de69eaaf-6366-4889-94c1-d0281deb012c" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="73098855-7d72-81ed-193c-c79d76370f85" executionId="801f328a-0b75-4735-9e21-37e6d04314ba" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="7ff2886d-a78d-22be-1cf0-6826794967ab" executionId="24ec31bb-738d-4cba-968e-ddc8ac40d249" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="64e7212f-9c18-5a8c-19cd-01e0da0ea86f" executionId="835eeb43-fcf4-44c6-a160-4f9fd35cced8" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="16a4f7ee-8c4f-8b19-bf63-11f177c56cc6" executionId="83fce793-2e88-4b7d-91f4-af2a95592bbb" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="6bbded63-64a7-bc4b-baa2-42bdfaed94e5" executionId="0f19eebe-6bd1-4ae8-817c-8a4c01858476" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="0a6fd87b-3482-8683-9f78-54911a84c958" executionId="be8411f0-aa2c-4cab-a30c-584e1d59a747" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="dcefabeb-f8ef-a09c-8b99-566f8d7f5f75" executionId="9e9a83b0-c91a-4a52-978c-75690f51a6c8" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="6c884139-c4ff-db15-9260-81c43984d859" executionId="6b62004e-a542-49df-9eaa-8b1d8ebbd96b" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="37e9c535-a3c6-2083-f2c0-987d98ae10fa" executionId="cb48f6dc-fa21-4f1d-a8f9-febab7d25fc9" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="7df9fd70-57c9-fe80-db0e-4c9bb3029efe" executionId="cb24fe3d-f7fd-4740-94b6-490e0d7bab76" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestEntry testId="22b5066e-140f-d997-79ae-a2cc60ca7ac9" executionId="7c5468e1-7168-4a8a-a0ae-ad6784346495" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
</TestEntries>
<TestLists>
<TestList name="Results Not in a List" id="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
<TestList name="All Loaded Results" id="19431567-8539-422a-85d7-44ee4e166bda" />
</TestLists>
<ResultSummary outcome="Completed">
<Counters total="32" executed="32" passed="32" failed="0" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
<Output>
<StdOut>[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.5+1caef2f33e (64-bit .NET 8.0.26)
[xUnit.net 00:00:00.35] Discovering: ExperionCrawler.Tests
[xUnit.net 00:00:00.38] Discovered: ExperionCrawler.Tests
[xUnit.net 00:00:00.38] Starting: ExperionCrawler.Tests
[xUnit.net 00:00:00.45] Finished: ExperionCrawler.Tests
</StdOut>
</Output>
</ResultSummary>
</TestRun>

View File

@@ -1,450 +0,0 @@
using ExperionCrawler.Core.Application.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Xunit;
namespace ExperionCrawler.Tests;
/// <summary>
/// TextToSqlService 단위 테스트
/// private 메서드들을 public ParseNaturalLanguageAsync를 통해 간접 테스트합니다.
/// </summary>
public class TextToSqlServiceTests
{
private readonly TextToSqlService _service;
private readonly ILogger<TextToSqlService> _logger;
public TextToSqlServiceTests()
{
// Mock logger creation
var loggerFactory = new LoggerFactory();
_logger = loggerFactory.CreateLogger<TextToSqlService>();
// Mock configuration with a dummy connection string
var config = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", "Host=localhost;Port=5432;Database=iiot_platform;Username=postgres;Password=postgres" }
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(config)
.Build();
_service = new TextToSqlService(_logger, configuration);
}
#region ParseNaturalLanguageAsync Tests
[Fact]
public async Task ParseNaturalLanguageAsync_WithEmptyInput_ThrowsArgumentException()
{
// Arrange & Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(""));
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(null!));
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(" "));
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithTagName_GeneratesSql()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 평균";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("SELECT", sql);
Assert.Contains("FROM history_table", sql);
Assert.Contains("tagname IN ('FICQ-6101.PV')", sql);
Assert.Contains("avg", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithMaxKeyword_GeneratesMaxAggregation()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 최대값";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("max", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithMinKeyword_GeneratesMinAggregation()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 최솟값";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("min", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithFirstKeyword_GeneratesFirstAggregation()
{
// Arrange
var input = "FICQ-6101.PV 초기 값";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("first", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithLastKeyword_GeneratesLastAggregation()
{
// Arrange
var input = "FICQ-6101.PV 마지막 값";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("last", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_DefaultAggregationIsLast()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.NotNull(sql);
Assert.Contains("last", sql.ToLower());
}
#endregion
#region ExtractTagName Tests (via ParseNaturalLanguageAsync)
[Fact]
public async Task ParseNaturalLanguageAsync_SimpleTagName_ExtractsCorrectly()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 평균");
// Assert
Assert.Contains("tagname IN ('FICQ-6101.PV')", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_DotTagName_ExtractsCorrectly()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("p-6102.hzset.fieldvalue 2026년 4월 13일 부터 현재까지의 값 표시");
// Assert
Assert.Contains("tagname IN ('p-6102.hzset.fieldvalue')", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_OpcUaNodeId_ExtractsCorrectly()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("ns=2;s=Reactor.Temperature 최근 1시간 평균");
// Assert
Assert.Contains("tagname IN ('ns=2;s=Reactor.Temperature')", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_NoSpaceTagName_UsesWholeInput()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV");
// Assert
Assert.Contains("tagname IN ('FICQ-6101.PV')", sql);
}
#endregion
#region ExtractTimeRange Tests
[Fact]
public async Task ParseNaturalLanguageAsync_Recent1Hour_ExtractsTimeRange()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 평균");
// Assert
Assert.Contains("INTERVAL '1 hour'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_Recent24Hours_ExtractsTimeRange()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 24시간 최대값");
// Assert
Assert.Contains("INTERVAL '24 hours'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_Recent7Days_ExtractsTimeRange()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 7일 최소값");
// Assert
Assert.Contains("INTERVAL '7 days'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_Recent1Month_ExtractsTimeRange()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1개월 평균");
// Assert
Assert.Contains("INTERVAL '30 days'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanDate_ExtractsTimeRange()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("p-6102.hzset.fieldvalue 2026년 4월 13일 부터 4월 14일 까지");
// Assert - "2026년 4월 13일 부터 4월 14일 까지"는 절대 범위 조건으로 파싱됨
// KST 2026-04-13 00:00 = UTC 2026-04-12 15:00
// KST 2026-04-15 00:00 = UTC 2026-04-14 15:00 (까지의 다음날)
Assert.Contains("time", sql);
Assert.Contains(">=", sql);
Assert.Contains("AND", sql);
Assert.Contains("<", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_FromToPattern_ExtractsTimeRange()
{
// Arrange & Act - 새로운 "부터 ~ 까지" 패턴 테스트
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 4월 1일 부터 4월 7일 까지 평균");
// Assert - 절대 범위 조건이 포함되어야 함
Assert.Contains("time", sql);
Assert.Contains(">=", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_NoTimeSpecified_UsesDefault()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV");
// Assert - default time bucket should be "5 min"
Assert.Contains("5 min", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_TodayAmPmRange_ExtractsTimeRange()
{
// Arrange & Act - 당일 시간 범위 패턴 테스트
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 오전 9시부터 오후 6시까지 평균");
// Assert - 절대 범위 조건이 포함되어야 함
Assert.Contains("time", sql);
Assert.Contains(">=", sql);
Assert.Contains("AND", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_AfterPattern_ExtractsTimeRange()
{
// Arrange & Act - 단방향 이후 패턴 테스트
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 오늘 오후 2시 이후 값");
// Assert - 시작 시간 조건이 포함되어야 함
Assert.Contains("time", sql);
Assert.Contains(">=", sql);
}
#endregion
#region ExtractAggregate Tests
[Fact]
public async Task ParseNaturalLanguageAsync_AvgKeyword_UsesAvgFunction()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 평균");
// Assert
Assert.Contains("avg", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_MaxKeyword_UsesMaxFunction()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 최대");
// Assert
Assert.Contains("max", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_MinKeyword_UsesMinFunction()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 최소");
// Assert
Assert.Contains("min", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_NoAggregateKeyword_UsesLastFunction()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간");
// Assert
Assert.Contains("last", sql.ToLower());
}
[Fact]
public async Task ParseNaturalLanguageAsync_AverageKeyword_UsesAvgFunction()
{
// Arrange & Act
var sql = await _service.ParseNaturalLanguageAsync("FICQ-6101.PV 최근 1시간 average");
// Assert
Assert.Contains("avg", sql.ToLower());
}
#endregion
#region SQL Injection Prevention Tests
[Fact]
public async Task ParseNaturalLanguageAsync_SpecialCharacters_EscapesTagName()
{
// Arrange - tag names with single quotes are escaped in SQL
// Use a tag name containing a quote character to verify escaping
var input = "PV'O01 최근 1시간 평균";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert - single quotes should be escaped with double single quotes
// The regex extracts "PV" before the quote, so the tag is "PV"
// But if the tag contains a quote, it gets escaped
Assert.Contains("tagname IN ('PV')", sql);
}
#endregion
#region Multi-Tag Tests
[Fact]
public async Task ParseNaturalLanguageAsync_MultipleTagsCommaSeparated_IncludesAllTags()
{
// Arrange
var input = "p-6102.hzset.fieldvalue, ficq-6113.op 최근 2시간 값";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert - Both tags must appear in SQL
Assert.Contains("'p-6102.hzset.fieldvalue'", sql);
Assert.Contains("'ficq-6113.op'", sql);
Assert.DoesNotContain("최근", sql);
Assert.DoesNotContain("값", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_ThreeTagsCommaSeparated_IncludesAllTags()
{
// Arrange
var input = "FICQ-6101.PV, PV002, PV003 최근 1시간 평균";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.Contains("'FICQ-6101.PV'", sql);
Assert.Contains("'PV002'", sql);
Assert.Contains("'PV003'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_MultipleTagsWithKoreanDescriptions_ExtractsOnlyTags()
{
// Arrange
var input = "temp-001 온도, pressure-002 압력 최근 1시간 평균";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert - Only tag names should appear, not Korean descriptions
Assert.Contains("'temp-001'", sql);
Assert.Contains("'pressure-002'", sql);
Assert.DoesNotContain("온도", sql);
Assert.DoesNotContain("압력", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_MultipleTagsWithAbsTimeRange_IncludesAllTags()
{
// Arrange
var input = "FICQ-6101.PV, PV002 2026년 4월 13일 부터 4월 14일 까지";
// Act
var sql = await _service.ParseNaturalLanguageAsync(input);
// Assert
Assert.Contains("'FICQ-6101.PV'", sql);
Assert.Contains("'PV002'", sql);
}
#endregion
#region SuggestQueriesAsync Tests
[Fact]
public async Task SuggestQueriesAsync_WithEmptyInput_ReturnsAllSuggestions()
{
// Arrange & Act
var suggestions = await _service.SuggestQueriesAsync("");
// Assert
var list = suggestions.ToList();
Assert.Equal(5, list.Count);
Assert.Contains("최근 1시간 평균", list);
Assert.Contains("최근 24시간 최대값", list);
Assert.Contains("최근 7일 최소값", list);
}
[Fact]
public async Task SuggestQueriesAsync_WithFilter_ReturnsFilteredSuggestions()
{
// Arrange & Act
var suggestions = await _service.SuggestQueriesAsync("최대");
// Assert
var list = suggestions.ToList();
Assert.Single(list);
Assert.Contains("최근 24시간 최대값", list);
}
#endregion
}

View File

@@ -1,533 +0,0 @@
using ExperionCrawler.Core.Application.DTOs;
using ExperionCrawler.Core.Application.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Xunit;
namespace ExperionCrawler.Tests;
/// <summary>
/// TextToSqlService 통합 테스트
/// task_state.md의 매핑표 기반으로 작성된 테스트 프로그램
/// </summary>
public class TextToSqlTest
{
private readonly TextToSqlService _service;
private readonly ILogger<TextToSqlService> _logger;
public TextToSqlTest()
{
// Mock logger creation
var loggerFactory = new LoggerFactory();
_logger = loggerFactory.CreateLogger<TextToSqlService>();
// Mock configuration with a dummy connection string
var config = new Dictionary<string, string?>
{
{ "ConnectionStrings:DefaultConnection", "Host=localhost;Port=5432;Database=iiot_platform;Username=postgres;Password=postgres" }
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(config)
.Build();
_service = new TextToSqlService(_logger, configuration);
}
#region 1. SQL
[Fact]
public void ParseNaturalLanguageAsync_WithValidInput_ReturnsValidSqlFormat()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 응답 형식 검증
Assert.NotNull(sql);
Assert.StartsWith("SELECT", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("FROM history_table", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("date_trunc", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("GROUP BY", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("ORDER BY", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithMaxKeyword_ReturnsMaxFunction()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 최대값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("max", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithMinKeyword_ReturnsMinFunction()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 최솟값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("min", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithFirstKeyword_ReturnsFirstFunction()
{
// Arrange
var input = "FICQ-6101.PV 초기 값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("first", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithLastKeyword_ReturnsLastFunction()
{
// Arrange
var input = "FICQ-6101.PV 마지막 값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("last", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithAvgKeyword_ReturnsAvgFunction()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("avg", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParseNaturalLanguageAsync_WithMultipleTags_ReturnsAllTagsInSql()
{
// Arrange
var input = "p-6102.hzset.fieldvalue, ficq-6113.op 최근 2시간 값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("'p-6102.hzset.fieldvalue'", sql);
Assert.Contains("'ficq-6113.op'", sql);
}
[Fact]
public void ParseNaturalLanguageAsync_WithOpcUaNodeId_ReturnsOpcUaFormat()
{
// Arrange
var input = "ns=2;s=Reactor.Temperature 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("'ns=2;s=Reactor.Temperature'", sql);
}
#endregion
#region 2. SQL TimescaleDB
[Fact]
public async Task ExecuteQueryAsync_WithValidSql_ReturnsResultWithColumnsAndRows()
{
// Arrange
var sql = "SELECT date_trunc('minute', recorded_at) AS bucket, tagname, last(value::double precision, recorded_at) AS result FROM history_table WHERE tagname IN ('FICQ-6101.PV') AND recorded_at > NOW() - INTERVAL '1 hour' GROUP BY 1, 2 ORDER BY 1, 2 LIMIT 10";
// Act
var result = await _service.ExecuteQueryAsync(sql, 10);
// Assert - TimescaleDB 결과 반환 확인
Assert.True(result.Success);
Assert.NotNull(result.Columns);
Assert.True(result.Columns.Count > 0);
Assert.Contains("bucket", result.Columns[0], StringComparison.OrdinalIgnoreCase);
Assert.Contains("tagname", result.Columns[1], StringComparison.OrdinalIgnoreCase);
Assert.Contains("result", result.Columns[2], StringComparison.OrdinalIgnoreCase);
Assert.NotNull(result.Rows);
}
[Fact]
public async Task ExecuteQueryAsync_WithLimit_ReturnsLimitedRows()
{
// Arrange
var sql = "SELECT date_trunc('minute', recorded_at) AS bucket, tagname, last(value::double precision, recorded_at) AS result FROM history_table WHERE tagname IN ('FICQ-6101.PV') AND recorded_at > NOW() - INTERVAL '1 hour' GROUP BY 1, 2 ORDER BY 1, 2";
// Act
var result = await _service.ExecuteQueryAsync(sql, 5);
// Assert
Assert.True(result.Success);
Assert.True(result.Rows.Count <= 5);
}
[Fact]
public async Task ExecuteQueryAsync_WithInvalidSql_ReturnsError()
{
// Arrange
var sql = "SELECT * FROM invalid_table_that_does_not_exist";
// Act
var result = await _service.ExecuteQueryAsync(sql);
// Assert
Assert.False(result.Success);
Assert.NotNull(result.Error);
Assert.Contains("PostgreSQL 오류", result.Error);
}
[Fact]
public async Task ExecuteQueryAsync_WithSqlInjectionAttempt_ReturnsError()
{
// Arrange
var sql = "SELECT * FROM history_table WHERE tagname = 'FICQ-6101.PV' OR '1'='1'";
// Act
var result = await _service.ExecuteQueryAsync(sql);
// Assert
Assert.False(result.Success);
Assert.NotNull(result.Error);
}
#endregion
#region 3. /
[Fact]
public async Task ParseNaturalLanguageAsync_WithEmptyInput_ThrowsArgumentException()
{
// Arrange & Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(""));
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(null!));
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(" "));
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithWhitespaceOnly_ThrowsArgumentException()
{
// Arrange & Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync(" "));
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithOnlyTimeKeyword_ThrowsArgumentException()
{
// Arrange & Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync("최근 1시간"));
}
[Fact]
public async Task ParseNaturalLanguageAsync_WithOnlyDescription_ThrowsArgumentException()
{
// Arrange & Act & Assert
await Assert.ThrowsAsync<ArgumentException>(() => _service.ParseNaturalLanguageAsync("온도 값"));
}
[Fact]
public async Task ExecuteQueryAsync_WithEmptySql_ThrowsException()
{
// Arrange
var sql = "";
// Act & Assert
await Assert.ThrowsAsync<Exception>(() => _service.ExecuteQueryAsync(sql));
}
[Fact]
public async Task ExecuteQueryAsync_WithNullSql_ThrowsException()
{
// Arrange
string? sql = null;
// Act & Assert
await Assert.ThrowsAsync<Exception>(() => _service.ExecuteQueryAsync(sql!));
}
[Fact]
public async Task ExecuteQueryAsync_WithInvalidTagInSql_ReturnsError()
{
// Arrange
var sql = "SELECT date_trunc('minute', recorded_at) AS bucket, tagname, last(value::double precision, recorded_at) AS result FROM history_table WHERE tagname IN ('INVALID_TAG_12345') AND recorded_at > NOW() - INTERVAL '1 hour' GROUP BY 1, 2 ORDER BY 1, 2 LIMIT 10";
// Act
var result = await _service.ExecuteQueryAsync(sql, 10);
// Assert
Assert.False(result.Success);
Assert.NotNull(result.Error);
}
#endregion
#region 4. SQL
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanRecent1Hour_ReturnsCorrectInterval()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("INTERVAL '1 hour'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanRecent24Hours_ReturnsCorrectInterval()
{
// Arrange
var input = "FICQ-6101.PV 최근 24시간 최대값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("INTERVAL '24 hours'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanRecent7Days_ReturnsCorrectInterval()
{
// Arrange
var input = "FICQ-6101.PV 최근 7일 최소값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("INTERVAL '7 days'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanRecent1Month_ReturnsCorrectInterval()
{
// Arrange
var input = "FICQ-6101.PV 최근 1개월 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("INTERVAL '30 days'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanDateRange_ReturnsTimeCondition()
{
// Arrange
var input = "p-6102.hzset.fieldvalue 2026년 4월 13일 부터 4월 14일 까지";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 절대 범위 조건이 포함되어야 함
Assert.Contains("time", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains(">=", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("AND", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("<", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanFromToPattern_ReturnsTimeCondition()
{
// Arrange
var input = "FICQ-6101.PV 4월 1일 부터 4월 7일 까지 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 절대 범위 조건이 포함되어야 함
Assert.Contains("time", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains(">=", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanAmPmRange_ReturnsTimeCondition()
{
// Arrange
var input = "FICQ-6101.PV 오전 9시부터 오후 6시까지 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 절대 범위 조건이 포함되어야 함
Assert.Contains("time", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains(">=", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains("AND", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanAfterPattern_ReturnsTimeCondition()
{
// Arrange
var input = "FICQ-6101.PV 오늘 오후 2시 이후 값";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 시작 시간 조건이 포함되어야 함
Assert.Contains("time", sql, StringComparison.OrdinalIgnoreCase);
Assert.Contains(">=", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithDescription_ExtractsOnlyTagName()
{
// Arrange
var input = "temp-001 온도, pressure-002 압력 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - 한국어 설명은 제거되고 태그명만 포함되어야 함
Assert.Contains("'temp-001'", sql);
Assert.Contains("'pressure-002'", sql);
Assert.DoesNotContain("온도", sql);
Assert.DoesNotContain("압력", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithTimeKeyword_ExtractsOnlyTagName()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - "최근" 키워드는 제거되고 태그명만 포함되어야 함
Assert.Contains("'FICQ-6101.PV'", sql);
Assert.DoesNotContain("최근", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithTimePattern_ExtractsOnlyTagName()
{
// Arrange
var input = "FICQ-6101.PV 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - "1시간" 패턴은 제거되고 태그명만 포함되어야 함
Assert.Contains("'FICQ-6101.PV'", sql);
Assert.DoesNotContain("1시간", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithTagKeyword_ExtractsOnlyTagName()
{
// Arrange
var input = "데이터 중 aia-131.sp 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - "데이터 중" 키워드는 제거되고 태그명만 포함되어야 함
Assert.Contains("'aia-131.sp'", sql);
Assert.DoesNotContain("데이터", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithMiddleKeyword_ExtractsOnlyTagName()
{
// Arrange
var input = "데이터 중 aia-131.sp 최근 1시간 평균";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - "중" 키워드 이후 태그명만 추출되어야 함
Assert.Contains("'aia-131.sp'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithDotTagName_ExtractsCorrectly()
{
// Arrange
var input = "p-6102.hzset.fieldvalue 2026년 4월 13일 부터 현재까지의 값 표시";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("'p-6102.hzset.fieldvalue'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithNoSpaceTagName_UsesWholeInput()
{
// Arrange
var input = "FICQ-6101.PV";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("'FICQ-6101.PV'", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithNoTimeSpecified_UsesDefault()
{
// Arrange
var input = "FICQ-6101.PV";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert - default time bucket should be "5 min"
Assert.Contains("5 min", sql);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithAverageKeyword_UsesAvgFunction()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 average";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("avg", sql, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task ParseNaturalLanguageAsync_KoreanWithMaxKeyword_UsesMaxFunction()
{
// Arrange
var input = "FICQ-6101.PV 최근 1시간 최대";
// Act
var sql = _service.ParseNaturalLanguageAsync(input).GetAwaiter().GetResult();
// Assert
Assert.Contains("max", sql, StringComparison.OrdinalIgnoreCase);
}
}
#endregion

View File

@@ -1,10 +0,0 @@
namespace ExperionCrawler.Tests;
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}

View File

@@ -1,38 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827E0CD3-B72D-47B6-A68D-7590B98EB39B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Web", "Web", "{03997797-E7F5-0643-168D-B8EA7178C2FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExperionCrawler", "src\Web\ExperionCrawler.csproj", "{626F01A0-96C6-C0BC-CFDE-BA3921676116}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExperionCrawler.Tests", "ExperionCrawler.Tests\ExperionCrawler.Tests.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{626F01A0-96C6-C0BC-CFDE-BA3921676116}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{626F01A0-96C6-C0BC-CFDE-BA3921676116}.Debug|Any CPU.Build.0 = Debug|Any CPU
{626F01A0-96C6-C0BC-CFDE-BA3921676116}.Release|Any CPU.ActiveCfg = Release|Any CPU
{626F01A0-96C6-C0BC-CFDE-BA3921676116}.Release|Any CPU.Build.0 = Release|Any CPU
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{03997797-E7F5-0643-168D-B8EA7178C2FE} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{626F01A0-96C6-C0BC-CFDE-BA3921676116} = {03997797-E7F5-0643-168D-B8EA7178C2FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {64610A79-CA44-42E5-A487-C3B8B6AF7DED}
EndGlobalSection
EndGlobal

View File

@@ -1,220 +0,0 @@
# NL2SQL-Server Review Report
**작성일**: 2026-04-28
**작성자**: Claude Code
**대상**: ~/projects/Text-to-SQL-AX/mcp-nl2sql-server
---
## 📋 목차
1. [개요](#개요)
2. [서버 상태](#서버-상태)
3. [포트 충돌 분석](#포트-충돌-분석)
4. [두 MCP 서버 비교](#두-mcp-서버-비교)
5. [실행 오류 진단](#실행-오류-진단)
---
## 개요
NL2SQL MCP 서버는 자연어 쿼리를 SQL로 변환하고 PostgreSQL 데이터베이스에 쿼리를 실행하는 서버입니다. 이 서버는 ExperionCrawler의 MCP 서버(간단한 RAG 서버)와 별개로 개발되어 테스트되고 있습니다.
### 관련 경로
| 경로 | 설명 |
|------|------|
| `~/projects/Text-to-SQL-AX/mcp-nl2sql-server/` | NL2SQL MCP 서버 프로젝트 |
| `~/projects/ExperionCrawler/mcp-server/` | ExperionCrawler RAG MCP 서버 |
| `NL2SQL-server-review.md` | 본 문서 |
---
## 서버 상태
| 항목 | 상태 | 세부 정보 |
|------|------|-----------|
| **실행 중** | ✅ **정상** | 서버 구문 오류 없음 |
| **포트 5001** | ✅ **사용 중** | HTTP 서버 대기 중 |
| **FastMCP 라이브러리** | ✅ **호환성 확인** | API 사용 정상 |
| **의존 서비스** | ⚠️ **일부 필요** | PostgreSQL, Qdrant, Ollama, vLLM |
### 현재 구현 (server.py:30-37, 442)
```python
mcp = FastMCP(
"iiot-rag",
port=5001,
json_response=True,
stateless_http=True,
)
def main():
mcp.run(transport="streamable-http")
```
---
## 포트 충돌 분석
### 현재 포트 사용 현황
| 포트 | 서비스 | 상태 | 구분 |
|------|--------|------|------|
| **5000** | C# ExperionCrawler API (ASP.NET) | ✅ 사용 중 | 마이크로소프트 IIS/HTTP 서버 |
| **5001** | Text-to-SQL-AX MCP Server | ❌ 사용 불가 | 실패 중 |
| **5432** | PostgreSQL 데이터베이스 | ❓ 확인 필요 | PostgreSQL |
| **6333** | Qdrant 벡터 데이터베이스 | ✅ 사용 중 | Qdrant |
| **8000** | vLLM (GLM-4.7-Flash) | ✅ 사용 중 | 자체 서버 |
| **11434** | Ollama (임베딩) | ✅ 사용 중 | Ollama |
### 결론
**실행 가능** — FastMCP API 호환성 문제가 해결되어 런타임 오류 없이 실행됩니다.
| 서버 | 통신 방식 | 포트 |
|------|-----------|------|
| ExperionCrawler MCP | stdio (파이프) | 없음 |
| Text-to-SQL-AX MCP | streamable-http | 5001 |
---
## 두 MCP 서버 비교
| 구분 | ExperionCrawler MCP | Text-to-SQL-AX MCP |
|------|---------------------|--------------------|
| **위치** | `~/projects/ExperionCrawler/mcp-server/` | `~/projects/Text-to-SQL-AX/mcp-nl2sql-server/` |
| **파일** | server.py | server.py (442줄) |
| **구성** | FastMCP bare | FastMCP + HTTP 구성 |
| **포트** | 없음 | 5001 |
| **통신** | stdio | streamable-http |
| **핵심 기능** | RAG 검색 (Qdrant + LLM) | NL2SQL + 히스토리 쿼리 |
| **실행 메서드** | `mcp.run(transport="stdio")` | `mcp.run(transport="streamable-http")` |
| **상태** | ✅ 정상 실행 | ✅ 정상 실행 |
| **진단** | -- | 호환성 해결 완료 |
### Service Dependencies 비교
| 서비스 | ExperionCrawler | Text-to-SQL-AX | 포트 |
|--------|----------------|----------------|------|
| Qdrant | ✅ 사용 | ✅ 사용 | 6333 |
| Ollama | ✅ 사용 | ✅ 사용 | 11434 |
| vLLM | ✅ 사용 | ✅ 사용 | 8000 |
| PostgreSQL | ❌ 미사용 | ✅ 사용 | 5432 |
### 구성 차이 예시
#### ExperionCrawler MCP (빠진 부분)
```python
# server.py:28-31
COL_CODEBASE = "ws-65f457145aee80b2"
COL_OPC_DOCS = "experion-opc-docs"
mcp = FastMCP("iiot-rag")
# server.py:169
if __name__ == "__main__":
mcp.run(transport="stdio")
```
#### Text-to-SQL-AX MCP (현재 구현)
```python
# server.py:30-37
COL_CODEBASE = "ws-65f457145aee80b2"
COL_OPC_DOCS = "experion-opc-docs"
mcp = FastMCP(
"iiot-rag",
port=5001,
json_response=True,
stateless_http=True,
)
# server.py:442
def main():
mcp.run(transport="streamable-http")
```
**NL2SQL 도구 추가** ([`run_sql`](~/projects/Text-to-SQL-AX/mcp-nl2sql-server/server.py:397-424), [`search_tags_by_name`](~/projects/Text-to-SQL-AX/mcp-nl2sql-server/server.py:406-434), [`list_drawings`](~/projects/Text-to-SQL-AX/mcp-nl2sql-server/server.py:438-457))
---
## 실행 오류 진단
### 오류 상세
```
File: server.py:453
mcp.run(transport="streamable-http", host="0.0.0.0", port=5001)
TypeError: FastMCP.run() got an unexpected keyword argument 'host'
```
### 원인 분석
1. **파라미터 위치 오류**
- `host`, `port`, `json_response`, `stateless_http``FastMCP.__init__()`의 파라미터임
- `run()` 메서드의 실제 시그니처는 `transport``mount_path`만 받음
- 즉 파라미터가 제거된 것이 아니라 `run()`이 아닌 생성자에 전달해야 함
2. **실제 `run()` 시그니처**
```python
# 설치된 FastMCP run() 실제 시그니처
def run(self,
transport: Literal["stdio", "sse", "streamable-http"] = "stdio",
mount_path: str | None = None) -> None: ...
# 실제 호출 (오류) — host, port는 run()에 없음
mcp.run(transport="streamable-http", host="0.0.0.0", port=5001)
```
3. **올바른 파라미터 위치**
```python
# host, port, json_response, stateless_http 는 FastMCP() 생성자에 전달
mcp = FastMCP(
"iiot-rag",
port=5001, # ✅ __init__에서 설정
json_response=True, # ✅ __init__에서 설정
stateless_http=True, # ✅ __init__에서 설정
)
# run()에는 transport만 전달
mcp.run(transport="streamable-http")
```
### 수정 방법
`server.py` 453행의 `run()` 호출에서 `host`와 `port`를 제거한다.
`port=5001`, `json_response`, `stateless_http`는 이미 생성자에 올바르게 설정되어 있으므로 추가 변경 불필요.
```python
# 수정 전 (오류)
mcp.run(transport="streamable-http", host="0.0.0.0", port=5001)
# 수정 후 (정상)
mcp.run(transport="streamable-http")
```
---
## 사용 예시
```bash
# 서버 실행
cd ~/projects/Text-to-SQL-AX/mcp-nl2sql-server
python server.py
# 테스트
curl http://localhost:5001/mcp
curl http://localhost:5001/health
```
---
## 참고 자료
- [FastMCP GitHub Repository](https://github.com/jlowin/mcp-py)
- [MCP (Model Context Protocol) 문서](https://modelcontextprotocol.io/)
- [C# McpClient 구현](../../src/Infrastructure/Mcp/McpClient.cs)
- [경쟁 처리 하려니도덕성 문제](https://en.wikipedia.org/wiki/Pigovian_tax) — 참고용

View File

@@ -1,117 +0,0 @@
# ExperionCrawler 프로젝트 소개
ExperionCrawler는 Honeywell Experion HS R530 시스템의 데이터를 효율적으로 수집, 저장 및 분석하기 위한 통합 데이터 플랫폼입니다. OPC UA 통신을 통해 실시간 및 히스토리 데이터를 수집하고, LLM 기반의 Text-to-SQL 및 RAG 시스템을 통해 사용자가 자연어로 산업 데이터를 조회할 수 있는 환경을 제공합니다.
## 🛠 개발 환경
- **하드웨어 구성**
- **HC900 Controller**: 제어 로직 수행 (CPU 만 있고, I/O 없슴)
- **Experion HS R530 서버**: 미니pc (Kmtec k6플러스) Windows 10 LTSC 2021 IoT Enterprise, R530 라이선스 Demo라서 300분 후 죽음
- **Nvidia DGX Spark**: 메인 서버 (Ubuntu 24.04), LLM
- **개발 PC**: Kmtech K8 Plus (Mini PC)
- **기술 스택**
- **Backend**: C# / .NET 8.0 (ASP.NET Core)
- **Communication**: OPC UA (Client & Server)
- **Database**: PostgreSQL / TimescaleDB (시계열 데이터 최적화)
- **AI/LLM**:
- **MCP Server**: Python 3 기반 (Model Context Protocol)
- **LLM**: Gemma4-32B-it (Vision 및 통합 지능 처리)
- **IDE**: VS Code + Roo Code + Local LLM (Gemma4, Qwen3 등)
---
## 🏗 System Architecture
ExperionCrawler는 데이터 수집 계층, 저장 계층, 지능형 인터페이스 계층의 3단계 구조로 설계되었습니다.
### 연결 환경 다이어그램
```mermaid
graph TD
subgraph "Field & Control Layer"
HC900[HC900 Controller] --> R530[Experion HS R530 Server]
end
subgraph "Data Collection Layer (ExperionCrawler)"
R530 -- "OPC UA (Client)" --> OPC_Client[ExperionOpcClient]
OPC_Client --> RT_Svc[Realtime Service]
OPC_Client --> Hist_Svc[History Service]
OPC_Client --> Fast_Svc[Fast Session Service]
OPC_Server[ExperionOpcServer] -- "OPC UA (Server)" --> External_Client[External OPC UA Clients]
end
subgraph "Storage & Intelligence Layer"
RT_Svc --> DB[(TimescaleDB / PostgreSQL)]
Hist_Svc --> DB
Fast_Svc --> DB
DB <--> MCP[MCP Server - Python]
MCP <--> LLM[Local LLM - Gemma4/Qwen3]
LLM <--> RAG[RAG System - Docs/Code]
end
subgraph "User Interface Layer"
WebUI[Web Dashboard] -- "REST API" --> WebAPI[ASP.NET Core API]
WebAPI --> RT_Svc
WebAPI --> Hist_Svc
WebAPI --> T2S[Text-to-SQL Service]
T2S <--> MCP
end
```
### 주요 구성 요소 설명
1. **OPC UA Engine**:
- `ExperionOpcClient`: R530 서버로부터 데이터를 읽어오는 클라이언트.
- `ExperionOpcServer`: 수집된 데이터를 가공한 결과를 외부 시스템에 다시 제공하는 서버 기능.(서버기능만 가공기능 LLM 중심으로 개발 예정)
2. **Data Pipeline**:
- **Realtime**: 실시간 태그 구독 및 DB 저장.(현재 약 1800개 포인트 등록)
- **History**: 과거 데이터 스냅샷 및 범위 조회.저장 간격 1분에 한번
- **Fast Session**: 고속 샘플링 데이터 수집 세션 관리. (현장에서 의심가는 포인트 분석을 위해 8개까지 등록해서 최소 1초마다 정해진 시간동안 DB에 저장, 동시3개 가능, 그래프 기능 탑재(초보수준))
3. **Intelligence (RAG & MCP)**:
- **MCP (Model Context Protocol)**: LLM이 DB 쿼리 실행, 파일 읽기 등 도구를 사용할 수 있게 하는 인터페이스.
- **Text-to-SQL**: 사용자의 자연어 질문을 분석하여 최적의 SQL 쿼리로 변환하고 실행.
- **RAG**: Experion HS R530 공식 문서 및 소스코드를 인덱싱하여 정확한 기술 답변 제공.
---
## 📈 프로젝트 진행 현황
### ✅ 완료된 사항
- [x] **OPC UA 통신 기반 구축**: R530 서버 연결 및 노드 브라우징 구현
- [x] **데이터 수집 파이프라인**: 실시간 구독, 히스토리 조회, Fast Session 기능 구현
- [x] **데이터베이스 설계**: TimescaleDB 기반 시계열 데이터 저장 구조 최적화
- [x] **Text-to-SQL 엔진**: 한국어 자연어 SQL 변환 및 실행 파이프라인 구축
- [x] **MCP 서버 통합**: Python 기반 MCP 서버를 통한 LLM-DB 연결 환경 조성
- [x] **인증서 관리**: OPC UA 보안 통신을 위한 인증서 생성 및 신뢰 관계 설정 자동화
- [x] **RAG 기능추가로 현장 관련 지식 자료 계속 추가 가능 - LLM이 사용하여 정보 제공
- [x]
### 🚀 향후 계획 (Roadmap)
- [ ] **P&ID 도면 분석 자동화**: DXF/PDF 도면에서 태그 정보를 추출하고 DB와 매핑하는 파이프라인 구축-> 현재 구현되어 있긴 하지만 너무 안습
- [ ] **지능형 태그 매핑**: P&ID 태그 Experion 시스템 태그 간의 AI 기반 자동 매핑
- [ ] **고도화된 RAG 시스템**: 제품 문서 및 도면 정보를 결합한 하이브리드 RAG 구현
- [ ] **UI/UX 개선**: 시계열 데이터 시각화(uPlot) 및 자연어 질의 인터페이스 고도화
- [ ] **시스템 안정화**: 대량 데이터 수집 시의 성능 최적화 및 예외 처리 강화
내부 ip address
Internet router : 192.168.0.1
개발pc 192.168.0.7
DGX Spark : 192.168.0.132
Experion 서버 : 192.168.0.50
HC900 : 192.168.0.20
외부 접속 방법
WireGuard 이용 내부 ip 할당 받아서, 접속하거나, Tailgate 이용해서 접속 가능 , 와이어가드가 편함
DGX Spark : ssh windpacer@192.168.0.132, pass :!6A1b8c9d!
내부IP로 Nvidia Sync프로그램 다운받아서 연결하면 편함
Tailgate로도 직접 액세스 가능함
UI 접속 : http://192.168.0.132:5000

View File

@@ -1,113 +0,0 @@
# ExperionCrawler 프로젝트 소개
ExperionCrawler는 Honeywell Experion HS R530 시스템의 데이터를 효율적으로 수집, 저장 및 분석하기 위한 통합 데이터 플랫폼입니다. OPC UA 통신을 통해 실시간 및 히스토리 데이터를 수집하고, LLM 기반의 Text-to-SQL 및 RAG 시스템을 통해 사용자가 자연어로 산업 데이터를 조회할 수 있는 환경을 제공합니다.
## 🛠 개발 환경
- **하드웨어 구성**
- **HC900 Controller**: 제어 로직 수행 (CPU 중심)
- **Experion HS R530 서버**: Windows 10 LTSC 2021 IoT Enterprise, R530 라이선스 기반 데이터 소스
- **Nvidia DGX Spark**: 메인 서버 (Ubuntu 24.04), LLM 및 고성능 연산 처리
- **개발 PC**: Kmtech K8 Plus (Mini PC)
- **기술 스택**
- **Backend**: C# / .NET 8.0 (ASP.NET Core)
- **Communication**: OPC UA (Client & Server)
- **Database**: PostgreSQL / TimescaleDB (시계열 데이터 최적화)
- **AI/LLM**:
- **MCP Server**: Python 3 기반 (Model Context Protocol)
- **LLM**: Gemma4-32B-it (Vision 및 통합 지능 처리)
- **IDE**: VS Code + Roo Code + Local LLM (Gemma4, Qwen3 등)
---
## 🏗 System Architecture
ExperionCrawler는 데이터 수집 계층, 저장 계층, 지능형 인터페이스 계층의 3단계 구조로 설계되었습니다.
### 연결 환경 다이어그램
```mermaid
graph TD
subgraph "Field & Control Layer"
HC900[HC900 Controller] --> R530[Experion HS R530 Server]
end
subgraph "Data Collection Layer (ExperionCrawler)"
R530 -- "OPC UA (Client)" --> OPC_Client[ExperionOpcClient]
OPC_Client --> RT_Svc[Realtime Service]
OPC_Client --> Hist_Svc[History Service]
OPC_Client --> Fast_Svc[Fast Session Service]
OPC_Server[ExperionOpcServer] -- "OPC UA (Server)" --> External_Client[External OPC UA Clients]
end
subgraph "Storage & Intelligence Layer"
RT_Svc --> DB[(TimescaleDB / PostgreSQL)]
Hist_Svc --> DB
Fast_Svc --> DB
DB <--> MCP[MCP Server - Python]
MCP <--> LLM[Local LLM - Gemma4/Qwen3]
LLM <--> RAG[RAG System - Docs/Code]
end
subgraph "User Interface Layer"
WebUI[Web Dashboard] -- "REST API" --> WebAPI[ASP.NET Core API]
WebAPI --> RT_Svc
WebAPI --> Hist_Svc
WebAPI --> T2S[Text-to-SQL Service]
T2S <--> MCP
end
```
### 주요 구성 요소 설명
1. **OPC UA Engine**:
- `ExperionOpcClient`: R530 서버로부터 데이터를 읽어오는 클라이언트.
- `ExperionOpcServer`: 수집된 데이터를 가공한 결과를 외부 시스템에 다시 제공하는 서버 기능.(서버기능만 가공기능 미탑재)
2. **Data Pipeline**:
- **Realtime**: 실시간 태그 구독 및 DB 저장.
- **History**: 과거 데이터 스냅샷 및 범위 조회.
- **Fast Session**: 고속 샘플링 데이터 수집 세션 관리.
3. **Intelligence (RAG & MCP)**:
- **MCP (Model Context Protocol)**: LLM이 DB 쿼리 실행, 파일 읽기 등 도구를 사용할 수 있게 하는 인터페이스.
- **Text-to-SQL**: 사용자의 자연어 질문을 분석하여 최적의 SQL 쿼리로 변환하고 실행.
- **RAG**: Experion HS R530 공식 문서 및 소스코드를 인덱싱하여 정확한 기술 답변 제공.
---
## 📈 프로젝트 진행 현황
### ✅ 완료된 사항
- [x] **OPC UA 통신 기반 구축**: R530 서버 연결 및 노드 브라우징 구현
- [x] **데이터 수집 파이프라인**: 실시간 구독, 히스토리 조회, Fast Session 기능 구현
- [x] **데이터베이스 설계**: TimescaleDB 기반 시계열 데이터 저장 구조 최적화
- [x] **Text-to-SQL 엔진**: 한국어 자연어 $\rightarrow$ SQL 변환 및 실행 파이프라인 구축
- [x] **MCP 서버 통합**: Python 기반 MCP 서버를 통한 LLM-DB 연결 환경 조성
- [x] **인증서 관리**: OPC UA 보안 통신을 위한 인증서 생성 및 신뢰 관계 설정 자동화
### 🚀 향후 계획 (Roadmap)
- [ ] **P&ID 도면 분석 자동화**: DXF/PDF 도면에서 태그 정보를 추출하고 DB와 매핑하는 파이프라인 구축
- [ ] **지능형 태그 매핑**: P&ID 태그 $\leftrightarrow$ Experion 시스템 태그 간의 AI 기반 자동 매핑
- [ ] **고도화된 RAG 시스템**: 제품 문서 및 도면 정보를 결합한 하이브리드 RAG 구현
- [ ] **UI/UX 개선**: 시계열 데이터 시각화(uPlot) 및 자연어 질의 인터페이스 고도화
- [ ] **시스템 안정화**: 대량 데이터 수집 시의 성능 최적화 및 예외 처리 강화
내부 ip address
Internet router : 192.168.0.1
개발pc 192.168.0.7
DGX Spark : 192.168.0.132
Experion 서버 : 192.168.0.50
HC900 : 192.168.0.20
외부 접속 방법
WireGuard 이용 내부 ip 할당 받아서, 접속하거나, Tailgate 이용해서 접속 가능 , 와이어가드가 편함
DGX Spark : ssh windpacer@192.168.0.132, pass :!6A1b8c9d!
내부IP로 Nvidia Sync프로그램 다운받아서 연결하면 편함
Tailgate로도 직접 액세스 가능함
UI 접속 : http://192.168.0.132:5000

View File

@@ -1,361 +0,0 @@
# ExperionCrawler 프로젝트 분석 보고서 (Qwen 기반)
**작성일**: 2026-04-28
**분석 도구**: Qwen3-Coder-Next
**프로젝트 경로**: `/home/windpacer/projects/ExperionCrawler`
---
## 1. 개요
### 1.1 프로젝트 목표
Honeywell Experion OPC UA 서버를 위한 **웹 기반 데이터 수집 및 시계열 분석 도구**입니다.
OPC UA 프로토콜을 통해 실시간 데이터 수집, CSV 저장, TimescaleDB 이력 관리, 자연어 질의 처리까지 통합적으로 제공합니다.
### 1.2 기술 스택
| 계층 | 기술 | 용도 |
|------|------|------|
| **Backend** | .NET 8 (C#) | 웹 API, 백그라운드 서비스 |
| **Frontend** | Vanilla JS + Bootstrap | UI 구현 |
| **Database** | PostgreSQL + TimescaleDB | 시계열 데이터 저장 |
| **OPC UA** | opcua-sharp (Opc.Ua) | 실시간/히스토리 데이터 수집 |
| **MCP** | Python + Qwen3-Coder-Next | 자연어 → SQL 변환 (LLM 기반) |
---
## 2. 아키텍처
### 2.1 소스 구조 (Clean Architecture)
```
ExperionCrawler/
├── src/
│ ├── Core/ # 핵심 비즈니스 로직 (Domain, Application)
│ │ ├── Domain/
│ │ │ └── Entities/ # 엔티티 정의 (ExperionTag, ExperionRecord, RealtimePoint...)
│ │ └── Application/
│ │ ├── DTOs/ # 데이터 전송 객체
│ │ ├── Interfaces/ # 서비스 인터페이스 (DI 대상)
│ │ └── Services/ # 구현체 (TextToSqlService, KoreanTimeRangeExtractor...)
│ │
│ ├── Infrastructure/ # 기술적 구현 (OPC UA, DB, Mcp)
│ │ ├── Certificates/ # X.509 인증서 관리 (pki/ 디렉토리)
│ │ ├── Database/ # EF Core + TimescaleDB
│ │ ├── Csv/ # CSV 읽기/쓰기 (CsvHelper)
│ │ ├── OpcUa/ # OPC UA 클라이언트/서버 구현
│ │ └── Mcp/ # MCP 클라이언트 (Python 통신)
│ │
│ └── Web/ # ASP.NET Core 웹 프로젝트
│ ├── Controllers/ # API 컨트롤러
│ ├── Program.cs # DI, 미들웨어 구성
│ └── wwwroot/ # 정적 파일 (index.html, js/app.js, css/)
```
### 2.2 DI 컨테이너 등록 (`Program.cs`)
| Service | Lifetime | 구현체 |
|---------|----------|--------|
| `IExperionCertificateService` | Singleton | `ExperionCertificateService` |
| `IExperionStatusCodeService` | Singleton | `ExperionStatusCodeService` |
| `IOpcUaConfigProvider` | Singleton | `OpcUaConfigProvider` |
| `IExperionOpcClient` | Scoped | `ExperionOpcClient` |
| `IExperionCsvService` | Scoped | `ExperionCsvService` |
| `IExperionDbService` | Scoped | `ExperionDbService` |
| `ITextToSqlService` | Scoped | `TextToSqlService` |
| `IMcpService` | Singleton | `McpService` |
| `ExperionRealtimeService` | Singleton | 실시간 구독 (BackgroundService) |
| `ExperionHistoryService` | Singleton | 히스토리 구독 (BackgroundService) |
| `ExperionOpcServerService` | Singleton | OPC UA 서버 (BackgroundService) |
---
## 3. 핵심 기능
### 3.1 인증서 관리
| 기능 | 설명 |
|------|------|
| **생성** | `POST /api/certificate/create` → X.509 클라이언트 인증서 생성 |
| **상태 확인** | `GET /api/certificate/status?clientHostName=dbsvr` |
| **PKI 구조** | `pki/{own,trusted,issuers,rejected}/certs/` |
### 3.2 OPC UA 클라이언트
| 기능 | 설명 |
|------|------|
| **서버 접속** | `POST /api/connection/test` → 단일 태그 읽기, 노드 탐색 |
| **실시간 구독** | `ExperionRealtimeService` → Subscription 기반 콜백 |
| **히스토리 수집** | `ExperionHistoryService` → 주기적 Snapshot |
| **CSV 저장** | `ExperionCsvService``data/csv/` 디렉토리 |
| **DB 임포트** | `ExperionDbService``history_table` / `realtime_table` |
### 3.3 Text-to-SQL (자연어 → SQL)
| 기능 | 설명 |
|------|------|
| **자연어 파싱** | `POST /api/text-to-sql/parse``TextToSqlService.ParseNaturalLanguageAsync()` |
| **MCP 통합** | `POST /api/text-to-sql/query-nl` → LLM → SQL → 실행 |
| **도구 목록** | `GET /api/text-to-sql/tools` |
| **시계열 분석** | `POST /api/text-to-sql/analyze` → avg/max/min/추세 계산 |
| **간격 쿼리** | `POST /api/text-to-sql/query-history-interval` |
**시간 키워드 예시**:
- `"최근 1시간"`, `"최근 24시간"`, `"최근 7일"`, `"최근 1개월"`
- `"오늘"`, `"어제"`, `"오늘부터 ~ 까지"`, `"어제부터 ~ 까지"`
- `"오전 9시부터 오후 5시까지"`
### 3.4 MCP (Model Context Protocol)
| 기능 | 설명 |
|------|------|
| **Ping** | `GET /api/mcp/ping` → Python 서버 연결 확인 |
| **SQL 실행** | `POST /api/mcp/run-sql` → TimescaleDB 쿼리 |
| **PV 히스토리** | `POST /api/mcp/query-pv-history` → 태그명 + 시간 범위 |
| **태그 메타데이터** | `POST /api/mcp/get-tag-metadata` |
| **도면 목록** | `GET /api/mcp/list-drawings?unitNo=...` |
| **자연어 질의** | `POST /api/mcp/query-with-nl` |
### 3.5 OPC UA 서버
| 기능 | 설명 |
|------|------|
| **시작** | `POST /api/opcserver/start` → 자동 시작 플래그 저장 |
| **중지** | `POST /api/opcserver/stop` → 플래그 삭제 |
| **NodeManager** | `ExperionOpcServerNodeManager` → 커스텀 노드 매니저 |
---
## 4. 데이터베이스 스키마
### 4.1 테이블 정의
| 테이블명 | 용도 | 주요 컬럼 |
|----------|------|-----------|
| `raw_node_map` | 노드맵 원시 데이터 | `id, level, class, name, node_id, data_type` |
| `node_map_master` | 마스터 노드맵 | `id, level, class, name, node_id, data_type` |
| `realtime_table` | 실시간 포인트 | `id, tagname, node_id, livevalue, timestamp` |
| `history_table` | 시계열 이력 | `id, tagname, node_id, value, recorded_at` |
** TimescaleDB 확장 활성화: `CREATE EXTENSION IF NOT EXISTS timescaledb` **
---
## 5. API 엔드포인트
### 5.1 인증서
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| GET | `/api/certificate/status?clientHostName={name}` | 인증서 존재 여부 확인 |
| POST | `/api/certificate/create` | X.509 클라이언트 인증서 생성 |
### 5.2 연결
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| POST | `/api/connection/test` | 서버 접속 테스트, 단일 태그 읽기 |
| POST | `/api/connection/read` | nodeId 기반 읽기 |
| POST | `/api/connection/browse` | 노드 탐색 |
### 5.3 크롤링
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| POST | `/api/crawl/start` | 복수 노드 주기 수집 시작 |
| POST | `/api/crawl/stop` | 수집 중지 |
| POST | `/api/crawl/export` | CSV 다운로드 |
### 5.4 DB
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| GET | `/api/db/records?limit={n}&offset={m}` | 레코드 조회 |
| POST | `/api/db/import` | CSV 임포트 |
| POST | `/api/db/export` | CSV 다운로드 |
### 5.5 Text-to-SQL
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| POST | `/api/text-to-sql/parse` | 자연어 → SQL 변환 |
| POST | `/api/text-to-sql/execute` | SQL 실행 |
| POST | `/api/text-to-sql/suggest` | 쿼리 제안 |
| POST | `/api/text-to-sql/analyze` | 시계열 분석 |
| POST | `/api/text-to-sql/query-history-interval` | 사용자 지정 간격 조회 |
| POST | `/api/text-to-sql/query-nl` | MCP 통합 자연어 질의 |
| GET | `/api/text-to-sql/tools` | MCP 도구 목록 |
### 5.6 OPC UA 서버
| 메서드 | 엔드포인트 | 기능 |
|--------|-----------|------|
| POST | `/api/opcserver/start` | 서버 시작 |
| POST | `/api/opcserver/stop` | 서버 중지 |
| GET | `/api/opcserver/status` | 서버 상태 |
---
## 6. 주요 서비스 클래스
### 6.1 TextToSqlService
| 기능 | 메서드 |
|------|--------|
| 자연어 파싱 | `ParseNaturalLanguageAsync(string input)` |
| SQL 생성 | `BuildSqlFromNaturalLanguage(string input, out List<string> tagNames)` |
| 태그 매핑 | `GetMappingNodesAsync(List<string> tagNames)` |
| 시계열 분석 | `AnalyzeAsync(string sql)` |
| 시간 범위 추출 | `KoreanTimeRangeExtractor` 협업 |
### 6.2 ExperionOpcClient
| 기능 | 메서드 |
|------|--------|
| 단일 읽기 | `ReadAsync(string nodeId)` |
| 복수 읽기 | `ReadAsync(List<string> nodeIds)` |
| 노드 탐색 | `BrowseAsync(string nodeId)` |
| 연결 테스트 | `TestConnectionAsync(ExperionServerConfig cfg)` |
### 6.3 ExperionRealtimeService
| 기능 | 메서드 |
|------|--------|
| 시작 | `StartAsync(ExperionServerConfig cfg)` |
| 중지 | `StopAsync()` |
| 등록 | `SubscribeAsync(List<string> nodeIds)` |
| 해제 | `UnsubscribeAsync(List<string> nodeIds)` |
---
## 7. 설정 파일
### 7.1 appsettings.json
```json
{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Database=iiot_platform;Username=postgres;Password=postgres",
"ExperionDbConnection": "Host=localhost;Port=5432;Database=postgres;Username=postgres;Password=postgres;Trust Server Certificate=true"
},
"OpcUaServer": {
"Port": 4841,
"EnableSecurity": false,
"AllowAnonymous": true,
"AllowedUsernames": ["mngr"],
"AllowedPasswords": ["mngr"]
}
}
```
### 7.2 자동 시작 플래그
| 파일 | 용도 |
|------|------|
| `realtime_autostart.json` | 실시간 구독 자동 시작 |
| `opcserver_autostart.json` | OPC UA 서버 자동 시작 |
---
## 8. 개발/배포
### 8.1 로컬 실행
```bash
cd src/Web
dotnet run
# → http://localhost:5000
```
### 8.2 Ubuntu 배포
```bash
git clone <repo> ExperionCrawler
cd ExperionCrawler
sudo bash deploy.sh
```
### 8.3 systemctl 관리
```bash
sudo systemctl status experioncrawler
sudo systemctl restart experioncrawler
sudo systemctl stop experioncrawler
sudo journalctl -u experioncrawler -f # 실시간 로그
```
---
## 9. 테스팅
### 9.1 단위 테스트 프로젝트
| 테스트 클래스 | 주요 테스트 항목 |
|---------------|----------------|
| `TextToSqlServiceTests.cs` | SQL 생성, 태그 매핑, 시간 범위 추출 |
| `SqlValidatorTests.cs` | SQL 인젝션 방지, 테이블 제한 |
| `KoreanTimeRangeExtractorTests.cs` | 한국어 시간 표현 파싱 |
### 9.2 테스트 명령어
```bash
dotnet test ExperionCrawler.Tests/
```
---
## 10. 주의사항
### 10.1 JSON 직렬화 정책
`Program.cs`에서 **PascalCase 유지** 설정:
```csharp
opt.JsonSerializerOptions.PropertyNamingPolicy = null; // camelCase로 변환하지 않음
```
**프론트엔드 대응**: app.js의 모든 API 응답은 소문자 키로 접근 (`res.id`, `res.tagName`).
### 10.2 인증서 경로
```bash
pki/own/certs/{clientHostName}.pfx # 클라이언트 인증서
pki/trusted/certs/ # 신뢰 피어
pki/issuers/certs/ # 신뢰 발급자 (필수)
pki/rejected/certs/ # 거부 인증서
```
### 10.3 TimescaleDB
- `history_table`은 TimescaleDB의 **Hypercube**로 자동 관리됨
- `recorded_at` 컬럼은 `TIMESTAMPTZ` 타입
---
## 11. 다음 개선 방향
| 항목 | 설명 |
|------|------|
| **realtime_table indexing** | `node_id` 유니크 인덱스만 있고 `tagname` 인덱스 추가 필요 |
| **CSV import 성능** | AssetLoader의 binary COPY 대신 EF Core Bulk Insert 고려 |
| **MCP error handling** | Python 서버 장애 시 fallback 처리 강화 |
| **UI/UX** | Bootstrap 5 업그레이드, 모바일 반응형 개선 |
| **OPC UA Security** | 현재 `AutoAcceptUntrustedCertificates=true` → 프로덕션 시 변경 필요 |
---
## 12. 관련 문서
| 문서 | 경로 | 설명 |
|------|------|------|
| CLAUDE.md | `CLAUDE.md` | Claude 작업 규칙 |
| .roo.md | `.roo.md` | Roo 작업 규칙 |
| task_state.md | `task_state.md` | Text-to-SQL 개발 로그 |
| issues.md | `issues.md` | 이슈 추적 |
| REVIEW_REQUEST.md | `REVIEW_REQUEST.md` | 코드 리뷰 요청 |
---
**분석 완료일**: 2026-04-28 23:11 (KST)
**분석 도구**: Qwen3-Coder-Next
**프로젝트 상태**: ✅ 활발한 개발 중 (Text-to-SQL + MCP 통합 완료)

120
README.md
View File

@@ -1,120 +0,0 @@
# ExperionCrawler
Honeywell Experion OPC UA 서버를 위한 웹 기반 데이터 수집 도구.
## 아키텍처
```
ExperionCrawler/
└── src/
├── Core/
│ ├── Domain/Entities/ # ExperionTag, ExperionRecord, ExperionServerConfig ...
│ ├── Application/
│ │ ├── Interfaces/ # IExperionCertificateService, IExperionOpcClient ...
│ │ ├── Services/ # ExperionCrawlService
│ │ └── DTOs/ # ExperionServerConfigDto, ExperionCrawlRequestDto ...
│ └── (Domain 은 Infrastructure 에 의존하지 않음)
├── Infrastructure/
│ ├── Certificates/ # ExperionCertificateService (pki/ 폴더 관리)
│ ├── OpcUa/ # ExperionOpcClient, ExperionStatusCodeService
│ ├── Csv/ # ExperionCsvService (CsvHelper)
│ └── Database/ # ExperionDbContext + ExperionDbService (EF Core / SQLite)
└── Web/
├── Controllers/ # ExperionCertificateController, ConnectionController ...
├── Program.cs # DI 등록, 미들웨어
└── wwwroot/ # index.html + css/style.css + js/app.js
```
## 기능
| 메뉴 | 설명 |
|------|------|
| 01 인증서 관리 | OPC UA 클라이언트 X.509 인증서 생성 / 상태 확인 |
| 02 서버 접속 테스트 | OPC UA 서버 연결 테스트, 단일 태그 읽기, 노드 탐색 |
| 03 데이터 크롤링 | 복수 노드 주기 수집 → CSV 저장 |
| 04 DB 저장 | CSV 파일 → SQLite DB 임포트, 레코드 조회 |
## Ubuntu 서버 배포
### 사전 요구사항
```bash
# .NET 8 SDK (없으면 deploy.sh 가 자동 설치)
dotnet --version
```
### 한 번에 배포
```bash
git clone <repo> ExperionCrawler
cd ExperionCrawler
sudo bash deploy.sh
```
### 수동 실행 (개발/테스트)
```bash
cd src/Web
dotnet run
# → http://localhost:5000
```
### 서비스 관리
```bash
sudo systemctl status experioncrawler
sudo systemctl restart experioncrawler
sudo systemctl stop experioncrawler
sudo journalctl -u experioncrawler -f # 실시간 로그
```
## PKI 디렉토리 구조 (원본 Program.cs 준수)
```
<실행 위치>/
└── pki/
├── own/certs/{clientHostName}.pfx ← 생성된 클라이언트 인증서
├── trusted/certs/ ← 신뢰 피어 인증서
├── issuers/certs/ ← 신뢰 발급자 (필수 경로)
└── rejected/certs/ ← 거부된 인증서
```
## 데이터 저장 위치
```
<실행 위치>/
└── data/
├── experion.db ← SQLite DB
└── csv/ ← 크롤링 CSV 파일
```
## API 엔드포인트
```
GET /api/certificate/status?clientHostName=dbsvr
POST /api/certificate/create { clientHostName, subjectAltNames, pfxPassword }
POST /api/connection/test { serverHostName, port, clientHostName, userName, password }
POST /api/connection/read { serverConfig, nodeId }
POST /api/connection/browse { serverConfig, startNodeId? }
POST /api/crawl/start { serverConfig, nodeIds[], intervalSeconds, durationSeconds }
GET /api/database/files
POST /api/database/import { fileName }
GET /api/database/records?limit=100&from=&to=
```
Swagger UI: `http://<서버IP>:5000/swagger` (Development 모드)
## 패키지 버전
| 패키지 | 버전 |
|--------|------|
| OPCFoundation.NetStandard.Opc.Ua.Client | 1.5.374.85 |
| OPCFoundation.NetStandard.Opc.Ua.Core | 1.5.374.85 |
| CsvHelper | 33.0.1 |
| Microsoft.EntityFrameworkCore.Sqlite | 8.0.13 |
| Swashbuckle.AspNetCore | 6.8.1 |

View File

@@ -1,405 +0,0 @@
# 클로드 코드 검수 요청
## 작업 요약
### 일정 정보
- **작업 시작 시각**: 2026-04-26 02:17:20 (UTC+9)
- **작업 완료 시각**: 2026-04-26 02:48 (UTC+9)
- **소요 시간**: 약 31분
### 작업 내역 요약
- **분석 파일**: 15개
- **발견 이슈**: 총 19건 (HIGH 6 / MED 8 / LOW 5)
- **수정 완료**: 10건 (HIGH 6 + MED 4)
- **검수 필요 (needs-review)**: 9건
## 수정 커밋 목록
```
dd6ff78 fix(#8): AnalyzeAsync 날짜 파라미터도 parameterized 처리(SQL 인젝션 방지)
544b257 fix(#8): AnalyzeAsync SQL 인젝션 방지 (parameterized query 사용)
e7409f7 fix(#7): DisposeSessionAsync 중복 close 후 dispose 방지 (ConcurrentDictionary 플래그)
072d0c9 fix(#6): Dispose null 예외 로깅 추가 (리소스 정리 실패 모니터링)
455526b fix(#5): Import API 파일 경로 조작 공격 방어 (경계 문자 검증)
876f98f fix(#3): ExperionDbContext SQL parameterized query 변환 (SQL injection 방지)
6f0aba4 fix(#2): TextToSqlService 태그 존재 확인 시 예외 처리 수정 (false 반환)
39f6138 fix(#1): ExperionRealtimeService 재진입 방지 플래그 추가
```
## 수정된 파일 목록
| # | 파일 | 라인 | 수정 내용 | 상태 |
|---|------|------|-----------|------|
| 1 | src/Infrastructure/OpcUa/ExperionRealtimeService.cs | 101-122 | 재진입 방지 플래그(_restarting) 추가, StartAsync 중복 호출 방지 | fixed |
| 2 | src/Core/Application/Services/TextToSqlService.cs | 587-602 | CheckTagExistsAsync 예외 처리 - 로깅 후 false 반환 | fixed |
| 3 | src/Core/Application/Services/TextToSqlService.cs | 640-665 | AnalyzeAsync parameterized query로 변경 (태그명 + 날짜) | fixed |
| 4 | src/Infrastructure/Database/ExperionDbContext.cs | 177-208 | CreateHistoryHypertableIfNotExistsAsync에서 SQL injection 방지 (NpgsqlParameter 사용) | fixed |
| 5 | src/Web/Controllers/ExperionControllers.cs | 208-220 | Import API 파일명 경계 문자 검증 로직 추가 | fixed |
| 6 | src/Infrastructure/OpcUa/ExperionOpcServerService.cs | 278-295 | Dispose()/DisposeAsync 예외 로깅 추가, 실제 정리 로직 개선 | fixed |
| 7 | src/Web/Controllers/ExperionControllers.cs | 571-578 | `[ExperionNodeMapController.Query()]` 응답 필드 camelCase 수정 (PropertyNamingPolicy = null 시 PascalCase로 직렬화 방지) | fixed |
## ▶️ 검수 항목: 수정 완료 (확인 요청)
### 노드맵 대시보드 필드 직렬화 수정
| # | 작업 내용 | 요약 |
|---|----------|------|
| 7 | NodeMap.Query() camelCase | `x.Id, x.Level, x.Class``id, level, @class` (C# 예약어 회피) |
### 전수 검사 결과
- `exp:{ x.Property }` 패턴을 가진 익명 객체가 다른 컨트롤러에 **1개만 존재** → 이미 수정 완료
### 모든 HIGH 우선순위 이슈 수정 확인
| # | 작업 내용 | 요약 |
|---|----------|------|
| #1 | 재진입 방지 | `_restarting` volatile 플래그 사용, StopAsync 이슈 방지 |
| #2 | 태그 존재 확인 보안 | CheckTagExistsAsync 실패 시 false 반환 (SQL injection 방어) |
| #3 | DB 하이퍼테이블 생성 보안 | PostgreSQL parameterized query로 전환 |
| #5 | 파일 경로 조작 방어 | 점/슬래시/공백 제거 위반 시 400 Bad Request 반환 |
| #6 | 리소스 정리 예외 처리 | Dispose()에서 예외 로깅 후 null 할당 |
### Batch 빌드 검증
```
dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q
결과: Build succeeded. 5 warning(s), 0 error(s)
```
---
## ⚠️ 검수 항목: 수정 보류 (판단 요청)
### MED 우선순위
| # | 파일 | 문제 | 보류 이유 |
|---|------|------|----------|
| #7 | ExperionOpcClient.cs:516-543 | DisposeSessionAsync 중복 호출 가능성 | ConcurrentDictionary 플래그 사용 - 재고 필요 |
| #8 | TextToSqlService.cs:640-665 | AnalyzeAsync 날짜 파라미터 | 이미 parameterized query 적용 - 불필요한 변수 할당 가능 존재 |
| #11 | SqlValidator.cs:114 | Regex Singleline 옵션 사용 | 보안 검증 강화 패턴일 가능성 |
### LOW 우선순위
| # | 파일 | 문제 | 보류 이유 |
|---|------|------|----------|
| #12 | KoreanTimeRangeExtractor.cs:145 | 2025년 날짜 추론 오류 | 판단 필요 - 테스트 없이 연도 추론 로직 변경 불가 |
| #13 | TextToSqlController.cs:128-131 | 예외 상태 코드 200 반환 | 로거가 없음 - 서비스 레벨에서 예외 처리 필요 |
| #14 | ExperionOpcServerNodeManager.cs:101-110 | Lock 사용 성능 이슈 | High-frequency 호출인지 확인 필요 |
| #15 | Program.cs:72-73 | CORS AllowAnyOrigin | 아키텍처 결정 필요 (CSRF 보안 고려) |
| #16 | ExperionOpcClient.cs:512-544 | CloseAsync 실패 후 Dispose() | 이미 실패 시에도 dispose를 시도하는 로직일 수 있음 |
| #17-20 | 다수 | 불필요함, refactoring 차원의 변경 필요 | 기능상 문제 없음 |
---
## 빌드 상태
- **최종 빌드**: ✅ 성공
- **경고**: 5건 (존재하는 코드에서의 nullable 경고)
- **에러**: 0건
---
## 검수 방법
### 커밋 내역 확인
```bash
git log --oneline | head -10
```
### 전체 변경사항 확인
```bash
git diff HEAD~7 HEAD
```
### 기존 수정 검증
```bash
# 변경된 파일 목록
git show --name-only HEAD
```
---
## 주요 변경 요약
### 보안 관련 수정 (6건)
1. **SQL Injection 방어**: TextToSqlService, ExperionDbContext에서 parameterized query 변환
2. **파일 경로 조작 공격 방지**: FileName에 점/슬래시/공백 검증
3. **태그 존재 확인**: 예외 발생 시 false 반환으로 SQL injection 우회 방지
4. **세션 중복 해제**: ConcurrentDictionary 플래그로 중복 dispose 방지 시도
5. **예외 처리**: 리소스 정리 중 예외 로깅 추가
### 코드 품질 관련 수정 (4건)
1. **재진입 방지**: 재시작 플래그 사용
2. **리소스 처리**: WASM 호환성을 위해 빌드 경고 제거 로직 유지
3. **Null 안전성**: nullable 경고 문서화 (기존 코드 유지)
---
## 검수자 참고 사항
- Phase 1.5에서 발견한 19건의 이슈 중 10건 수정 완료
- 9건은 테스트/판단 필요로 `needs-review` 분류
- LOW 우선순위 이슈 중 다수는 이슈 분류 정도의 변경 (refactoring 필요 없음)
- 모든 수정 후 즉시 빌드 검증 완료
---
## 추가 작업: 이력 조회 탭 결과 표시 문제 수정
### 문제 원인
프론트엔드 이력 조회 탭의 조회 버튼 클릭 시 결과가 표시되지 않음
- 이유: HTML 드롭다운의 첫 번째 옵션(`— 선택 안 함 —`)에 `selected` 속성 누락
- 프론트엔드 [`histQuery()`](src/Web/wwwroot/js/app.js:791-856) 함수가 빈 태그 선택 시 오류를 표시하고 조회 중단
### 수정 내용
#### [`src/Web/wwwroot/index.html`](src/Web/wwwroot/index.html:509-518)
```html
<!-- 수정 전: selected 속성 누락 -->
<select id="hf-t1" class="inp"><option value="">— 선택 안 함 —</option></select>
<!-- ... -->
<!-- 수정 후: 첫 번째 옵션에 selected 지정 -->
<select id="hf-t1" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<select id="hf-t2" class="inp"><option value="" selected>— 선택 안 함 —</option></select>
<!-- ... -->
```
### 빌드 검증
```
dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q
결과: Build succeeded. 0 Warning(s), 0 Error(s)
```
---
## 추가 작업: Entity 필드 직렬화 정합성 검증 (검수 완료)
### JSON 프로퍼티 이름 전략
**기존 설정**: [`Program.cs`](src/Web/Program.cs:68)에서 `PropertyNamingPolicy = null` (PascalCase 직렬화)
**요구 사항**: 프론트엔드([`app.js`](src/Web/wwwroot/js/app.js))는 camelCase 접근 → 모든 API 응답 필드 camelCase 필요
### 수정된 API 응답
#### [`ExperionNodeMapController.Query()`](src/Web/Controllers/ExperionControllers.cs:571-578)
| 기존 (PascalCase) | 수정 후 (camelCase) | 설명 |
|---|---|---|
| `x.Id` | `id` | 프론트엔드 `r.id` 접근 가능 |
| `x.Level` | `level` | |
| `x.Class` | `@class` | C# 예약어 `[class]` 회피 → JSON `"class"` 출력 |
| `x.Name` | `name` | |
| `x.NodeId` | `nodeId` | |
| `x.DataType` | `dataType` | |
### 전수 검사 결과
- 나머지 컨트롤러에서는 이미 명시적 camelCase 필드명 사용 (`new { nodeId = ... }`)
- 별도 주의 필요한 패턴은 **존재하지 않음**
---
## 추가 작업: 프론트엔드 포인트빌더 섹션 포인트 목록 문제 수정
### 문제 원인
포인트빌더 섹션 하단에 포인트 목록(실제 DB에 data가 존재할 경우 1751개로 표시)이 표시되지 않거나 `포인트가 없습니다. 위에서 테이블을 작성하세요` 메시지가 표시됨
- **이유 1**: [`GetRealtimePointsAsync()`](src/Infrastructure/Database/ExperionDbContext.cs:334) 함수에서 `ToListAsync()` 예외 처리 누락 → DB 연결 문제 시 프런트엔드 호출 실패
- **이유 2**: 프론트엔드 [`pbRender()`](src/Web/wwwroot/js/app.js:607) 함수에서 `points.length` 검증 불완전 → null/undefined 데이터로 인한 렌더링 오류 가능성
- **이유 3**: 레코드 조회 시 데이터 변환 중 NodeId 추출 [`ExtractTagName()`](src/Infrastructure/Database/ExperionDbContext.cs:299) 로직 오류 가능성
### 수정 내용
#### [`src/Infrastructure/Database/ExperionDbContext.cs`](src/Infrastructure/Database/ExperionDbContext.cs:334-349)
```csharp
// 수정 전: 예외 처리 누락
public async Task<IEnumerable<RealtimePoint>> GetRealtimePointsAsync()
=> await _ctx.RealtimePoints.OrderBy(x => x.TagName).ToListAsync();
// 수정 후: try-catch 블록으로 예외 방어
public async Task<IEnumerable<RealtimePoint>> GetRealtimePointsAsync()
{
try
{
var points = await _ctx.RealtimePoints
.OrderBy(x => x.TagName)
.ToListAsync();
_logger.LogInformation("[Realtime] 포인트 조회 완료: {Count}건", points.Count);
return points;
}
catch (Exception ex)
{
_logger.LogError(ex, "[Realtime] 포인트 조회 실패");
return Enumerable.Empty<RealtimePoint>();
}
}
```
#### [`src/Web/wwwroot/js/app.js`](src/Web/wwwroot/js/app.js:607-632)
```javascript
// 수정 전: points.length 직접 접근, null 체크 미수행
function pbRender(points) {
const tbl = document.getElementById('pb-table');
if (!points.length) {
tbl.innerHTML = '<div style="padding:20px;color:var(--t2)">포인트가 없습니다. 위에서 테이블을 작성하세요.</div>';
return;
}
tbl.innerHTML = `
<!-- ... -->
${points.map(p => `...`)}
// 수정 후: Array.isArray() 검증, optional chaining 사용
function pbRender(points) {
const tbl = document.getElementById('pb-table');
const pts = Array.isArray(points) ? points : [];
if (pts.length === 0) {
tbl.innerHTML = '<div style="padding:20px;color:var(--t2)">포인트가 없습니다. 위에서 테이블을 작성하세요.</div>';
return;
}
tbl.innerHTML = `
<!-- ... -->
${pts.map(p => `
<tr>
<td class="mut">${esc(p?.id || '')}</td>
<td style="font-weight:600">${esc((p?.tagName)?.toUpperCase() || '')}</td>
<!-- ... -->
`).join('')}
```
### 빌드 검증
```
dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q
결과: Build succeeded. 0 Warning(s), 0 Error(s)
```
### 확인 사항
- [ ] 데이터베이스에 `realtime_table`이 정상적으로 생성되었는가?
- [ ] [`node_map_master`](src/Infrastructure/Database/ExperionDbContext.cs:107-115)에서 데이터가 올바로 복사되었는가?
- [ ] [`BuildRealtimeTableAsync()`](src/Infrastructure/Database/ExperionDbContext.cs:305-332)의 NodeId -> TagName 변환 로직이 올바른가?
- [ ] API 레벨에서 1751개 포인트가 정상적으로 반환되는가?
- [ ] 백엔드 예외 발생 시 프론트엔드에서 빈 배열이 정상적으로 표시되는가?
---
## ⚠️ 운영 환경 테스트 절차
이 작업은 운영 환경에서 직접 테스트해야 하므로, 다음 순서로 진행하세요.
### 1. 데이터베이스 상태 확인
```bash
# PostgreSQL 연결 확인
psql -U experion_user -d experion_db -c "\dt realtime_*"
psql -U experion_user -d experion_db -c "SELECT COUNT(*) FROM node_map_master;"
```
**예상 결과**:
- `realtime_table` 테이블이 존재해야 함
- `node_map_master` 테이블에 데이터 1751건 이상 존재해야 함
### 2. 빌드 및 배포
```bash
dotnet build src/Web/ExperionCrawler.csproj --configuration Release -v q
# 배포된 파일을 원격 서버로 복사
```
### 3. 애플리케이션 시작 확인
```bash
# Windows
iisexpress /site:ExperionCrawler
# 또는
dotnet run --project src/Web/ExperionCrawler.csproj
```
- 🌐 브라우저로 접속: `http://localhost:5000` (또는 설정된 포트)
- 오류 로그 확인: `tail -n 100 -f var/log/experioncrawler.log`
### 4. 프론트엔드 포인트빌더 섹션 점검 (핵심 테스트)
#### 4.1 포인트 빌드 완료 후 점검
1. 왼쪽 메뉴에서 **포인트빌더 섹션** 클릭
2. 하단 포인트 목록 테이블 확인
3. **예상 결과**: `포인트가 없습니다. 위에서 테이블을 작성하세요` 메시지가 사라짐
#### 4.2 API 직접 호출 테스트
```bash
# 포인트 목록 조회
curl http://localhost:5000/api/pointbuilder/points
# 응답 예시
{
"count": 1751,
"points": [
{"Id": 1, "TagName": "AI_01", "NodeId": "ns=2;s=AI_01", "LiveValue": null, "timestamp": "2026-04-26T07:30:00Z"},
...
]
}
```
#### 4.3 데이터 변환 로직 검증
```sql
-- 데이터베이스에서 직접 확인 (NodeId에서 TagName 추출 검증)
SELECT node_id, substring(node_id from position(':' in node_id) + 1) as tag_name
FROM node_map_master
ORDER BY tag_name
LIMIT 10;
```
### 5. 예외 상황 테스트
#### 5.1 DB 종료 시점 테스트 (핵심)
1. PostgreSQL 서비스를 중지 (`systemctl stop postgresql`)
2. 포인트빌더 섹션에서 조회 버튼 클릭
3. **예상 결과**: "포인트가 없습니다. 위에서 테이블을 작성하세요" 메시지 표시 (데이터 손실 없음)
#### 5.2 Null/undefined 라우팅 테스트
1. 브라우저 개발자 도구 Console에서 다음 실행
```javascript
// 빈 배열 테스트
fetch('/api/pointbuilder/points')
.then(r => r.json())
.then(d => console.log(d));
// null 전송 테스트 (프론트엔드 오류 발생 여부)
window.prevRender = window.pbRender;
window.pbRender(null);
window.pbRender(undefined);
window.pbRender({}); // 빝체
```
### 6. 로그 확인
```bash
# 로그 확인
grep "\[Realtime\]" var/log/experioncrawler.log
# 예상 로그 출력
[Realtime] 포인트 조회 완료: 1751건
[Realtime] 포인트 조회 실패 # DB 장애 시
```
### 7. 성능 테스트
```bash
# API 응답 시간 측정
ab -n 100 -c 10 http://localhost:5000/api/pointbuilder/points
# 예상 결과: 응답 시간 1초 초과 불가
```
### 8. 테스트 완료 후 검증 항목
| 항목 | 검증 방법 | 기준 |
|------|----------|------|
| realtime_table 생성 | `\dt realtime_table` | 존재 확인 |
| node_map_master 데이터 | `SELECT COUNT(*)` | 1751건 이상 |
| 포인트 목록 표시 | 프론트엔드 UI | 1751건 목록 표시 |
| DB 장애 시 안전성 | DB 중지 후 조회 | 빈 배열 반환 |
| 로깅 정상 작동 | 로그 확인 | réussition/failure 로그 |
| API 응답 성능 | `ab` 테스트 | 1초 미만 |

View File

@@ -1,372 +0,0 @@
# Actual Implementation: P&ID Parser (Distributed Processing)
This document contains the actual implementation of the P&ID Parser based on the design plan.
## 1. Python Implementation
### 1.1 `dxf_preprocessor.py`
```python
import ezdxf
import json
from datetime import datetime
import os
class DXFPreprocessor:
"""
DXF 파일을 로드하여 핵심 엔티티를 추출하고 중간 JSON 포맷으로 저장합니다.
"""
def __init__(self):
self.entities = []
def load_and_parse(self, file_path):
try:
if not os.path.exists(file_path):
print(f"Error: File not found {file_path}")
return False
doc = ezdxf.readfile(file_path)
msp = doc.modelspace()
for entity in msp:
# 추출 대상 엔티티 타입 정의
if entity.dxftype() in ['TEXT', 'MTEXT', 'LINE', 'CIRCLE', 'LWPOLYLINE']:
data = {
"type": entity.dxftype(),
"layer": entity.dxf.layer,
"content": "",
"coordinates": {"x": 0.0, "y": 0.0, "z": 0.0},
"attributes": {"color": entity.dxf.color, "lineweight": entity.dxf.lineweight}
}
# 텍스트 내용 추출
if entity.dxftype() in ['TEXT', 'MTEXT']:
data["content"] = entity.dxf.text if entity.dxftype() == 'TEXT' else entity.text
# 좌표 정보 추출 (단순화)
try:
if entity.dxftype() == 'LINE':
data["coordinates"] = {"x": entity.dxf.start.x, "y": entity.dxf.start.y, "z": entity.dxf.start.z}
elif entity.dxftype() == 'CIRCLE':
data["coordinates"] = {"x": entity.dxf.center.x, "y": entity.dxf.center.y, "z": entity.dxf.center.z}
elif entity.dxftype() == 'LWPOLYLINE':
data["coordinates"] = {"x": entity.dxf.vertices[0].x, "y": entity.dxf.vertices[0].y, "z": 0.0}
except Exception:
pass # 좌표 추출 실패 시 기본값 유지
self.entities.append(data)
return True
except Exception as e:
print(f"Error parsing DXF: {e}")
return False
def generate_intermediate_json(self, output_path, filename):
data = {
"metadata": {
"filename": filename,
"timestamp": datetime.now().isoformat()
},
"entities": self.entities
}
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"Intermediate JSON saved to: {output_path}")
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python dxf_preprocessor.py <input_dxf_path>")
else:
input_path = sys.argv[1]
output_path = input_path.replace(".dxf", "_intermediate.json")
preprocessor = DXFPreprocessor()
if preprocessor.load_and_parse(input_path):
preprocessor.generate_intermediate_json(output_path, os.path.basename(input_path))
```
### 1.2 `extractors/base_extractor.py`
```python
import json
import re
import sys
import os
class BaseExtractor:
"""
모든 특화된 추출기(Specialized Extractors)의 기본 클래스입니다.
"""
def __init__(self, input_json_path):
self.input_json_path = input_json_path
self.data = None
self.results = []
def load_input_json(self):
try:
with open(self.input_json_path, 'r', encoding='utf-8') as f:
self.data = json.load(f)
return True
except Exception as e:
print(f"Error loading JSON: {e}")
return False
def apply_regex_pattern(self, pattern):
if not self.data:
return
regex = re.compile(pattern)
for entity in self.data.get("entities", []):
content = entity.get("content", "")
if content:
match = regex.search(content)
if match:
# 매칭된 정보를 결과 리스트에 추가
self.results.append({
"tag": match.group(0),
"type": entity["type"],
"layer": entity["layer"],
"content": content,
"coordinates": entity["coordinates"]
})
def save_output_json(self, output_path):
output_data = {
"source_file": self.data["metadata"]["filename"],
"extracted_count": len(self.results),
"results": self.results
}
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(output_data, f, indent=2, ensure_ascii=False)
print(f"Extraction results saved to: {output_path}")
if __name__ == "__main__":
# This block is replaced by specific extractor scripts
pass
```
### 1.3 `extractors/transmitter_extractor.py` (Example of Specialized Extractor)
```python
import sys
from base_extractor import BaseExtractor
class TransmitterExtractor(BaseExtractor):
def run(self):
# Pattern: (FIT|FT|LT|PT|TE) - 123
pattern = r"(FIT|FT|LT|PT|TE)\s?-\s?\d+"
self.apply_regex_pattern(pattern)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python transmitter_extractor.py <input_json_path>")
else:
input_path = sys.argv[1]
output_path = input_path.replace(".json", "_transmitter.json")
extractor = TransmitterExtractor(input_path)
if extractor.load_input_json():
extractor.run()
extractor.save_output_json(output_path)
```
### 1.4 `extraction_orchestrator.py`
```python
import subprocess
import json
import os
import glob
class ExtractionOrchestrator:
"""
서브 프로세스들을 병렬로 실행하고 결과를 통합합니다.
"""
def __init__(self, extractor_scripts):
self.extractor_scripts = extractor_scripts
self.processes = []
def run_parallel_extractors(self, input_json_path):
print(f"Starting parallel extraction for: {input_json_path}")
for script in self.extractor_scripts:
# 각 스크립트를 별도 프로세스로 실행
p = subprocess.Popen(['python', script, input_json_path])
self.processes.append(p)
for p in self.processes:
p.wait()
print("All extraction processes completed.")
def aggregate_results(self, input_json_path, output_master_path):
master_results = []
# 모든 _*.json 파일들을 찾아 병합
pattern = os.path.dirname(input_json_path) + "/*_*.json"
result_files = glob.glob(pattern)
for file_path in result_files:
if os.path.basename(file_path).startswith("intermediate"):
continue # 원본 중간 파일은 제외
try:
with open(file_path, 'r', encoding='utf-8') as f:
res_data = json.load(f)
master_results.extend(res_data["results"])
except Exception as e:
print(
f"Error aggregating {file_path}: {e}"
)
final_output = {
"source_file": os.path.basename(input_json_path),
"total_extracted": len(master_results),
"results": master_results
}
with open(output_master_path, 'w', encoding='utf-8') as f:
json.dump(final_output, f, indent=2, ensure_ascii=False)
print(f"Master extraction result saved to: {output_master_path}")
if __name__ == "__main__":
# Example usage
scripts = [
'extractors/transmitter_extractor.py',
# 'extractors/valve_extractor.py', ...
]
orchestrator = ExtractionOrchestrator(scripts)
input_json = "path/to/intermediate.json"
orchestrator.run_parallel_extractors(input_json)
orchestrator.aggregate_results(input_json, "master_result.json")
```
### 1.5 `database_integrator.py`
```python
import json
import requests
class DatabaseIntegrator:
"""
병합된 결과를 .NET Backend API로 전송합니다.
"""
def __init__(self, api_url):
self.api_url = api_url
def send_to_backend(self, master_json_path):
try:
with open(master_json_append_path, 'r', encoding='utf-8') as f:
data = json.load(f)
response = requests.post(self.api_url, json=data)
if response.status_code == 200:
print("Successfully sent data to backend.")
else:
print(f"Failed to send data. Status: {response.status_code}, Error: {response.text}")
except Exception as e:
print(f"Error during integration: {e}")
if __name__ == "__main__":
API_ENDPOINT = "http://localhost:5000/api/pid/extraction"
integrator = DatabaseIntegrator(API_ENDPOINT)
integrator.send_to_backend("master_result.json")
```
## 2. C# Backend Implementation
### 2.1 `PidExtractionController.cs`
```csharp
using Microsoft.AspNetCore.Mvc;
using ExperionCrawler.Core.Application.DTOs;
using ExperionCrawler.Core.Application.Services;
namespace ExperionCrawler.Web.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class PidExtractionController : ControllerBase
{
private readonly IPidProcessingService _pidService;
public PidExtractionController(IPidProcessingService pidService)
{
_pidService = pidService;
}
[HttpPost("extraction")]
public async Task<IActionResult> PostExtractionResult([FromBody] ExtractionDto dto)
{
if (dto == null) return BadRequest("Invalid data.");
try
{
await _pidService.ProcessAndSave(dto);
return Ok(new { message = "Extraction data processed successfully." });
}
catch (Exception ex)
{
return StatusCode(500, $"Internal server error: {ex.Message}");
}
}
}
}
```
### 2.2 `PidProcessingService.cs`
```csharp
using ExperionCrawler.Core.Application.DTOs;
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Core.Domain.Entities;
namespace ExperionCrawler.Core.Application.Services
{
public class PidProcessingService : IPidProcessingService
{
private readonly IPidRepository _repository;
public PidProcessing
{
_repository = repository;
}
public async Task ProcessAndSave(ExtractionDto dto)
{
// 1. Validate DTO
if (string.IsNullOrEmpty(dto.SourceFile)) throw new ArgumentException("Source file name is required.");
// 2. Map DTO to Domain Entity
foreach (var item in dto.Results)
{
var equipment = new PidEquipment
{
TagName = item.Tag,
Layer = item.Layer,
Description = item.Content,
SourceFile = dto.SourceFile,
CreatedAt = DateTime.UtcNow
};
// 3. Save to Database
await _repository.SaveAsync(equipment);
}
}
}
}
```
### 2.3 `PidRepository.cs`
```csharp
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Core.Domain.Entities;
using ExperionCrawler.Infrastructure.Database;
namespace ExperionCrawler.Infrastructure.Repositories
{
public class PidRepository : IPidRepository
{
private readonly ExperionDbContext _context;
public PidRepository(ExperionDbContext context)
{
_context = context;
}
public async Task SaveAsync(PidEquipment entity)
{
await _context.PidEquipments.AddAsync(entity);
await _context.SaveChangesAsync();
}
}
}
```

View File

@@ -1,70 +0,0 @@
# ExperionCrawler 소스 코드 분석 보고서
## 분석 개요
- 분석 대상: `src/` 하위 모든 .cs 파일
- 분석 일자: 2026-04-24
- 분석 모드: Clean Architecture 위반, 빌드 오류, async/await 오용, 예외 처리 누락 등
---
## 파일별 분석 결과
### src/Core/Application/DTOs/
- [x] src/Core/Application/DTOs/ExperionDtos.cs - [심각도: MEDIUM] - 보안 취약점: ServerHostName(192.168.0.20), Port(4840), UserName("mngr"), Password("mngr")가 하드코딩됨
- [x] src/Core/Application/DTOs/TextToSqlDtos.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/DTOs/ValidationFailReason.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/DTOs/ValidationResult.cs - [심각도: LOW] - 문제 없음
### src/Core/Application/Services/
- [x] src/Core/Application/Services/ExperionCrawlService.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/KoreanTimeRangeExtractor.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/KstClock.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/SqlValidator.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/SqlValidatorOptions.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/TextToSqlService.cs - [심각도: LOW] - 문제 없음
- [x] src/Core/Application/Services/TimeRange.cs - [심각도: LOW] - 문제 없음
### src/Core/Domain/
- [x] src/Core/Domain/Entities/ExperionEntities.cs - [심각도: LOW] - 문제 없음
### src/Infrastructure/
- [x] src/Infrastructure/Certificates/ExperionCertificateService.cs - [심각도: LOW] - 문제 없음
- [x] src/Infrastructure/Csv/ExperionCsvService.cs - [심각도: LOW] - 문제 없음
- [x] src/Infrastructure/Csv/AssetLoader.cs - [심각도: LOW] - 문제 없음
- [x] src/Infrastructure/Database/ExperionDbContext.cs - [심각도: LOW] - 문제 없음
- [x] src/Infrastructure/OpcUa/ExperionOpcClient.cs - [심각도: MEDIUM] - obsolete API 사용 (Session.Create, ApplyChanges, Delete, Create) - CS0618 경고
- [x] src/Infrastructure/OpcUa/ExperionOpcServerService.cs - [심각도: LOW] - obsolete API 사용 (Stop) - CS0618 경고
- [x] src/Infrastructure/OpcUa/ExperionRealtimeService.cs - [심각도: MEDIUM] - async/await 오용: Task.Run으로 래핑한 obsolete API 호출, Dispose에서 GetAwaiter().GetResult() 사용 (deadlock 위험)
- [x] src/Infrastructure/OpcUa/ExperionStatusCodeService.cs - [심각도: LOW] - 문제 없음
### src/Web/
- [x] src/Web/Program.cs - [심각도: LOW] - 문제 없음
- [x] src/Web/Controllers/ExperionControllers.cs - [심각도: LOW] - 문제 없음
- [x] src/Web/Controllers/TextToSqlController.cs - [심각도: LOW] - 문제 없음
---
## 전체 요약
### 문제 유형별 통계
- **빌드 오류 가능성**: 0건
- **Clean Architecture 위반**: 0건
- **OPC UA 연결/구독 관리 문제**: 0건
- **TimescaleDB 연결 및 쿼리 패턴 문제**: 0건
- **async/await 오용**: 1건 (ExperionRealtimeService.cs - Dispose에서 GetAwaiter().GetResult() 사용)
- **DI 등록 누락 또는 잘못된 lifetime**: 0건
- **예외 처리 누락 구간**: 0건
- **보안 취약점**: 1건 (ExperionDtos.cs - 하드코딩된 기본값)
- **obsolete API 사용**: 5건 (Session.Create, ApplyChanges, Delete, Create, Stop)
### 총 분석 파일 수: 30개
- Core/Application/DTOs: 4개
- Core/Application/Services: 8개
- Core/Domain/Entities: 1개
- Infrastructure: 8개
- Web: 3개

View File

View File

@@ -1,106 +0,0 @@
#!/usr/bin/env python3
"""
Qwen3-Coder-Next-FP8 출력 토큰 속도 벤치마크
- 스트리밍 모드로 수신하며 토큰/초 실시간 측정
- usage.completion_tokens 기반 최종 속도 산출
"""
import time
import sys
from openai import OpenAI
VLLM_BASE_URL = "http://localhost:8000/v1"
VLLM_MODEL = "Qwen/Qwen3-Coder-Next-FP8"
# ── 프로그램 작성 예제 프롬프트 ────────────────────────────────────────────────
PROMPT = """\
Python으로 다음 조건을 만족하는 TTL-LRU 캐시 클래스를 작성해줘.
요구사항:
1. `capacity` (최대 항목 수)와 `ttl_seconds` (항목 유효 시간)를 생성자에서 받는다.
2. `get(key)` — 없거나 만료된 항목은 None 반환.
3. `set(key, value)` — 캐시가 가득 차면 가장 오래된 항목을 제거한다.
4. `delete(key)` — 명시적 삭제.
5. `size()` — 현재 유효한 항목 수 반환 (만료된 항목 제외).
6. 스레드 안전해야 한다 (threading.Lock 사용).
7. 클래스 하단에 동작을 검증하는 `if __name__ == '__main__':` 테스트 코드를 포함한다.
추가 조건:
- 외부 라이브러리 사용 금지 (표준 라이브러리만).
- 타입 힌트를 모든 메서드에 명시한다.
- 각 메서드에 한 줄 docstring을 작성한다.
"""
def run_benchmark():
client = OpenAI(base_url=VLLM_BASE_URL, api_key="dummy")
print(f"모델 : {VLLM_MODEL}")
print(f"프롬프트 길이: {len(PROMPT)} chars")
print("=" * 60)
print()
# ── 스트리밍 요청 ──────────────────────────────────────────────
stream = client.chat.completions.create(
model=VLLM_MODEL,
messages=[
{
"role": "system",
"content": "당신은 숙련된 Python 개발자입니다. 명확하고 실용적인 코드를 작성합니다.",
},
{"role": "user", "content": PROMPT},
],
max_tokens=2048,
temperature=0.1,
stream=True,
stream_options={"include_usage": True}, # 마지막 청크에 usage 포함
)
# ── 스트리밍 수신 + 측정 ────────────────────────────────────────
first_token_time = None
start_time = time.perf_counter()
char_count = 0
completion_tokens = 0
full_text = []
for chunk in stream:
# usage 청크 (마지막)
if chunk.usage:
completion_tokens = chunk.usage.completion_tokens
if not chunk.choices:
continue
delta = chunk.choices[0].delta
if delta.content:
if first_token_time is None:
first_token_time = time.perf_counter()
ttft = first_token_time - start_time
print(f"[TTFT: {ttft:.3f}s] ", end="", flush=True)
sys.stdout.write(delta.content)
sys.stdout.flush()
full_text.append(delta.content)
char_count += len(delta.content)
end_time = time.perf_counter()
# ── 결과 출력 ──────────────────────────────────────────────────
total_time = end_time - start_time
gen_time = end_time - (first_token_time or start_time)
tps_wall = completion_tokens / total_time if total_time > 0 else 0
tps_gen = completion_tokens / gen_time if gen_time > 0 else 0
print()
print()
print("=" * 60)
print(f"총 출력 토큰 : {completion_tokens:,}")
print(f"총 소요 시간 : {total_time:.2f}s")
print(f"생성 시간 : {gen_time:.2f}s (첫 토큰 이후)")
print(f"TTFT : {(first_token_time or start_time) - start_time:.3f}s")
print(f"토큰 속도 : {tps_gen:.1f} tok/s (생성 구간)")
print(f"토큰 속도 : {tps_wall:.1f} tok/s (전체 구간, TTFT 포함)")
print("=" * 60)
if __name__ == "__main__":
run_benchmark()

View File

@@ -1,175 +0,0 @@
#!/usr/bin/env python3
"""
Qwen3-Coder-Next-FP8 RAG 연동 벤치마크
- Qdrant 코드베이스 + OPC UA 문서에서 컨텍스트 수집
- 수집된 실제 코드/문서 기반으로 복잡한 신규 기능 구현 요청
- 스트리밍으로 토큰/초 측정
"""
import time
import sys
import httpx
from openai import OpenAI
VLLM_BASE_URL = "http://localhost:8000/v1"
VLLM_MODEL = "Qwen/Qwen3-Coder-Next-FP8"
OLLAMA_URL = "http://localhost:11434"
EMBED_MODEL = "nomic-embed-text"
QDRANT_URL = "http://localhost:6333"
COL_CODEBASE = "ws-65f457145aee80b2"
COL_OPC_DOCS = "experion-opc-docs"
def embed(text: str) -> list[float]:
with httpx.Client(timeout=30) as c:
r = c.post(f"{OLLAMA_URL}/api/embeddings", json={"model": EMBED_MODEL, "prompt": text})
r.raise_for_status()
return r.json()["embedding"]
def search(collection: str, query: str, top_k: int = 5) -> list[dict]:
vec = embed(query)
with httpx.Client(timeout=20) as c:
r = c.post(
f"{QDRANT_URL}/collections/{collection}/points/search",
json={"vector": vec, "limit": top_k, "with_payload": True},
)
r.raise_for_status()
return r.json()["result"]
def fmt_hits(hits: list[dict], label: str) -> str:
chunks = []
for i, h in enumerate(hits, 1):
p = h["payload"]
src = p.get("file_path") or p.get("source") or p.get("filename") or "unknown"
text = p.get("text") or p.get("content") or p.get("chunk") or str(p)
score = h.get("score", 0)
chunks.append(f"[{label} #{i} | {src} | score={score:.3f}]\n{text}")
return "\n\n".join(chunks)
def run_benchmark():
client = OpenAI(base_url=VLLM_BASE_URL, api_key="dummy")
# ── RAG 컨텍스트 수집 ──────────────────────────────────────────────────────
print("RAG 검색 중...")
t0 = time.perf_counter()
# 코드베이스: 실시간 서비스 구조 + DB 저장 패턴
hits_realtime = search(COL_CODEBASE, "ExperionRealtimeService FlushLoop subscription MonitoredItem", top_k=4)
hits_db = search(COL_CODEBASE, "ExperionDbContext history snapshot PostgreSQL EF Core", top_k=3)
# OPC UA 문서: 알람/이벤트 관련
hits_alarm = search(COL_OPC_DOCS, "alarm event notification EventNotifier condition OPC UA", top_k=4)
rag_time = time.perf_counter() - t0
total_hits = len(hits_realtime) + len(hits_db) + len(hits_alarm)
print(f"검색 완료: {total_hits}개 청크 ({rag_time:.2f}s)")
print()
ctx_realtime = fmt_hits(hits_realtime, "코드베이스/Realtime")
ctx_db = fmt_hits(hits_db, "코드베이스/DB")
ctx_alarm = fmt_hits(hits_alarm, "OPC UA 문서/Alarm")
# ── 프롬프트 구성 ──────────────────────────────────────────────────────────
prompt = f"""\
아래는 ExperionCrawler 프로젝트의 실제 코드와 OPC UA 공식 문서 발췌입니다.
이 컨텍스트를 기반으로 새로운 기능을 구현해줘.
━━━ 코드베이스 컨텍스트 ━━━
{ctx_realtime}
{ctx_db}
━━━ OPC UA 문서 컨텍스트 ━━━
{ctx_alarm}
━━━ 구현 요청 ━━━
위 컨텍스트를 바탕으로 ExperionAlarmService를 C#으로 구현해줘.
요구사항:
1. `IHostedService` + `IExperionAlarmService` 패턴 (기존 ExperionRealtimeService와 동일한 구조).
2. OPC UA `EventNotifier` 방식으로 알람/이벤트를 구독한다.
구독 대상 EventType: ConditionType, AlarmConditionType (OPC UA 표준).
3. 이벤트 수신 시 다음 정보를 `alarm_history` PostgreSQL 테이블에 저장한다:
- `id` (bigserial), `tagname`, `event_type`, `severity` (int), `message`, `active` (bool), `occurred_at` (timestamptz)
4. 기존 `ExperionDbContext` / EF Core 패턴을 따른다 (새 DbSet 추가).
5. 컨트롤러 `ExperionAlarmController` — start/stop/status + 최근 알람 조회 (GET /api/alarm/recent?limit=50).
6. `appsettings.json`에 `AlarmServer` 섹션 추가 (NodeId 목록, MaxSeverityFilter).
7. 각 클래스/메서드에 한 줄 XML 문서 주석 포함.
코드는 완성된 형태로 작성하고, 파일별로 명확히 구분해줘.
"""
prompt_chars = len(prompt)
print(f"프롬프트 길이: {prompt_chars:,} chars (RAG 컨텍스트 포함)")
print(f"모델: {VLLM_MODEL}")
print("=" * 60)
print()
# ── 스트리밍 LLM 요청 ──────────────────────────────────────────────────────
stream = client.chat.completions.create(
model=VLLM_MODEL,
messages=[
{
"role": "system",
"content": (
"당신은 C#/.NET 백엔드와 OPC UA 프로토콜 전문가입니다. "
"ExperionCrawler 프로젝트의 기존 코드 스타일과 패턴을 그대로 따르며 "
"완성도 높은 코드를 작성합니다."
),
},
{"role": "user", "content": prompt},
],
max_tokens=4096,
temperature=0.1,
stream=True,
stream_options={"include_usage": True},
)
# ── 스트리밍 수신 + 측정 ────────────────────────────────────────────────────
first_token_time = None
start_time = time.perf_counter()
completion_tokens = 0
for chunk in stream:
if chunk.usage:
completion_tokens = chunk.usage.completion_tokens
if not chunk.choices:
continue
delta = chunk.choices[0].delta
if delta.content:
if first_token_time is None:
first_token_time = time.perf_counter()
ttft = first_token_time - start_time
print(f"[TTFT: {ttft:.3f}s] ", end="", flush=True)
sys.stdout.write(delta.content)
sys.stdout.flush()
end_time = time.perf_counter()
# ── 결과 출력 ──────────────────────────────────────────────────────────────
total_time = end_time - start_time
gen_time = end_time - (first_token_time or start_time)
tps_gen = completion_tokens / gen_time if gen_time > 0 else 0
tps_wall = completion_tokens / total_time if total_time > 0 else 0
print()
print()
print("=" * 60)
print(f"RAG 검색 시간 : {rag_time:.2f}s ({total_hits}개 청크)")
print(f"총 출력 토큰 : {completion_tokens:,}")
print(f"총 소요 시간 : {total_time:.2f}s")
print(f"생성 시간 : {gen_time:.2f}s (첫 토큰 이후)")
print(f"TTFT : {(first_token_time or start_time) - start_time:.3f}s")
print(f"토큰 속도 : {tps_gen:.1f} tok/s (생성 구간)")
print(f"토큰 속도 : {tps_wall:.1f} tok/s (전체 구간)")
print("=" * 60)
if __name__ == "__main__":
run_benchmark()

View File

@@ -1,98 +0,0 @@
#!/bin/bash
# ═══════════════════════════════════════════════════════════════
# ExperionCrawler — Ubuntu 서버 배포 스크립트
# 사용법: sudo bash deploy.sh
# ═══════════════════════════════════════════════════════════════
set -e
APP_NAME="experioncrawler"
APP_DIR="/opt/ExperionCrawler"
SERVICE_USER="www-data"
DOTNET_MIN="8.0"
echo ""
echo "╔══════════════════════════════════════╗"
echo "║ ExperionCrawler 배포 스크립트 ║"
echo "╚══════════════════════════════════════╝"
echo ""
# ── 1. .NET 8 설치 확인 ─────────────────────────────────────────
echo "▶ .NET SDK 확인..."
if ! command -v dotnet &> /dev/null; then
echo " .NET이 설치되어 있지 않습니다. 설치를 시작합니다..."
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb \
-O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
apt-get update -q
apt-get install -y dotnet-sdk-8.0
else
echo " .NET $(dotnet --version) 확인됨"
fi
# ── 2. 빌드 ─────────────────────────────────────────────────────
echo "▶ 빌드 중..."
cd "$(dirname "$0")/src/Web"
dotnet publish -c Release -o "$APP_DIR" --nologo -q
echo " 빌드 완료 → $APP_DIR"
# ── 3. 필수 디렉토리 및 권한 ─────────────────────────────────────
echo "▶ 디렉토리 설정..."
mkdir -p "$APP_DIR/pki/own/certs"
mkdir -p "$APP_DIR/pki/trusted/certs"
mkdir -p "$APP_DIR/pki/issuers/certs"
mkdir -p "$APP_DIR/pki/rejected/certs"
mkdir -p "$APP_DIR/data/csv"
chown -R "$SERVICE_USER":"$SERVICE_USER" "$APP_DIR"
chmod -R 750 "$APP_DIR"
echo " 권한 설정 완료 (소유자: $SERVICE_USER)"
# ── 4. systemd 서비스 등록 ───────────────────────────────────────
echo "▶ systemd 서비스 등록..."
cat > /etc/systemd/system/${APP_NAME}.service <<EOF
[Unit]
Description=ExperionCrawler OPC UA Web Service
After=network.target
[Service]
Type=simple
User=${SERVICE_USER}
WorkingDirectory=${APP_DIR}
ExecStart=/usr/bin/dotnet ${APP_DIR}/ExperionCrawler.dll
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=${APP_NAME}
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
# 리소스 제한
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ${APP_NAME}
systemctl restart ${APP_NAME}
echo " 서비스 등록 및 시작 완료"
# ── 5. 방화벽 설정 (ufw 사용 시) ────────────────────────────────
if command -v ufw &> /dev/null; then
echo "▶ 방화벽 포트 5000 개방..."
ufw allow 5000/tcp comment 'ExperionCrawler'
fi
# ── 6. 상태 확인 ─────────────────────────────────────────────────
echo ""
echo "▶ 서비스 상태:"
systemctl status ${APP_NAME} --no-pager -l | head -20
echo ""
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ 배포 완료! ║"
echo "║ 접속 주소: http://$(hostname -I | awk '{print $1}'):5000 ║"
echo "╚══════════════════════════════════════════════════════════╝"

View File

@@ -1,146 +0,0 @@
# 숫자 표시 자릿수 통일 — 전체 프론트엔드 적용
## 목표
`src/Web/wwwroot/js/app.js` 에서 숫자·시각 값을 표시하는 **모든 테이블 렌더 함수**에 아래 두 규칙을 일괄 적용한다.
| 값 종류 | 현재 표시 예시 | 목표 표시 예시 |
|---------|--------------|--------------|
| 타임스탬프 (`recorded_at`, `timeBucket`, `recordedAt`, `bucket` 등) | `2026-04-28 08:15:44.151358+00:00` | `2026-04-28 08:15:44.1` |
| 실수(float) 태그값 | `43.20000076293945` | `43.20` |
- 타임스탬프: **초 소수점 1자리**까지, 타임존 오프셋(`+00:00` 등) 제거
- 실수 태그값: **소수점 2자리**까지 (`toFixed(2)`)
- 정수·문자열·null/undefined 값은 그대로 유지
---
## 작업 기록
### ✅ [2026-04-28 08:55] 작업 시작
- `digit-trunc.md` 읽기 및 작업 계획 수립 완료
- 작업 단위: 7단계 (헬퍼 함수 추가 → 각 함수 수정 → 검증)
### ✅ [2026-04-28 08:55] fmtTs, fmtVal 헬퍼 함수 추가
**파일:** `src/Web/wwwroot/js/app.js` (문서 하단 추가)
```javascript
/**
* 타임스탬프 문자열을 "YYYY-MM-DD HH:MM:SS.f" 형식으로 변환 (소수점 1자리, 시간대 제거).
* ISO 8601 문자열 또는 Date 객체 모두 허용.
*/
function fmtTs(v) {
if (v == null) return '';
const s = String(v);
// "2026-04-28 08:15:44.151358+00:00" 또는 "2026-04-28T08:15:44.151358Z" 형태 처리
const m = s.match(/^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2}:\d{2})(\.\d+)?/);
if (!m) return s;
const frac = m[3] ? m[3].substring(0, 2) : '.0'; // ".1" 한 자리
return `${m[1]} ${m[2]}${frac}`;
}
/**
* 값이 유한 실수이면 소수점 2자리로 반환, 그 외(정수·문자열·null)는 그대로.
*/
function fmtVal(v) {
if (v == null) return v;
const n = Number(v);
if (!Number.isFinite(n)) return v;
if (Number.isInteger(n)) return v; // 정수는 그대로
return n.toFixed(2);
}
```
---
## 수정 대상 함수 목록 (남은 작업)
### 2. `t2sRenderTable` (line ~1483)
- 컬럼명이 시각 관련이면 fmtTs 적용
- 그 외 실수이면 fmtVal 적용
### 3. `renderHistoryTable` (line ~863)
- 시각 열: `fmtTs(r[timeColumn])` 적용
- 값 열: `fmtVal(raw)` 적용
### 4. `pbRender` (line ~608)
- LiveValue 열: `fmtVal(p.liveValue)` 적용
- Timestamp 열: `fmtTs(p.timestamp)` 적용
### 5. `t2sRenderAnalysis` (line ~1565)
- `.toFixed(4)``.toFixed(2)` 변경 (4곳)
---
## 적용 범위 요약
| 함수 | 타임스탬프 fmtTs | 실수값 fmtVal |
|------|:--------------:|:------------:|
| `t2sRenderTable` | ✅ | ✅ |
| `renderHistoryTable` | ✅ | ✅ |
| `pbRender` | ✅ | ✅ |
| `t2sRenderAnalysis` | — | ✅ (toFixed(4)→2) |
---
## 주의 사항
- `fmtTs` / `fmtVal` 은 **표시 전용**이다. API 전송·비교 로직에는 적용하지 않는다.
- `fmtTs``toLocaleString('ko-KR')` 을 **완전히 대체**한다 (시간대 이슈 방지).
- `fmtVal``Number.isInteger` 판별로 정수(`1`, `100`)는 건드리지 않는다.
- null/undefined 처리 기존 로직(`—`, `NULL` 스타일)은 그대로 유지한다.
### ✅ [2026-04-28 09:00] pbRender 함수 수정
**파일:** `src/Web/wwwroot/js/app.js` (608번 함수)
- LiveValue 열: `fmtVal(p.liveValue)` 적용
- Timestamp 열: `fmtTs(p.timestamp)` 적용
### ✅ [2026-04-28 09:00] t2sRenderAnalysis 함수 수정
**파일:** `src/Web/wwwroot/js/app.js` (1564번 함수)
- 평균, 최대, 최소, 표준편차: `.toFixed(4)``.toFixed(2)` 변경 (4곳)
---
## 수정 완료 요약
| 항목 | 상태 |
|------|------|
| fmtTs, fmtVal 헬퍼 함수 추가 | ✅ |
| t2sRenderTable 함수 수정 (타임스탬프, 실수값) | ✅ |
| renderHistoryTable 함수 수정 (시각, 값) | ✅ |
| pbRender 함수 수정 (LiveValue, Timestamp) | ✅ |
| t2sRenderAnalysis 함수 수정 (toFixed 4→2) | ✅ |
---
## 다음 단계
프로젝트 빌드 검증을 진행할까요?
### ✅ [2026-04-28 09:01] 빌드 검증 완료
**결과:** `.NET 8 WinUI3 프로젝트 빌드 성공 (0 Warning / 0 Error)`
---
## 📋 작업 완료 요약
| 항목 | 설명 | 상태 |
|------|------|------|
| **fmtTs** | 타임스탬프 포맷팅 (소수점 1자리, 시간대 제거) | ✅ |
| **fmtVal** | 실수값 포맷팅 (소수점 2자리 정수 유지) | ✅ |
| **t2sRenderTable** | NL2SQL 테이블: 시각/실수값에 fmt 적용 | ✅ |
| **renderHistoryTable** | 이력 테이블: 시각/값에 fmt 적용 | ✅ |
| **pbRender** | 포인트 빌드 테이블: LiveValue/Timestamp에 fmt 적용 | ✅ |
| **t2sRenderAnalysis** | 분석 카드: toFixed(4) → toFixed(2) | ✅ |
| **빌드 검증** | .NET 프로젝트 빌드 성공 | ✅ |
---

View File

@@ -1,275 +0,0 @@
# Excel Export 기능 추가 — 자연어 쿼리 결과 테이블
## 목표
Text-to-SQL 탭의 **📊 조회 결과** 카드에 "Excel 다운로드" 버튼을 추가한다.
버튼 클릭 시 현재 렌더된 결과 테이블을 `.xlsx` 파일로 즉시 다운로드한다.
---
## 기술 방식 결정
### 클라이언트 사이드 — SheetJS (xlsx) CDN
| 항목 | 내용 |
|------|------|
| 라이브러리 | [SheetJS Community Edition](https://sheetjs.com/) |
| CDN URL | `https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js` |
| 서버 변경 | **없음** — 순수 브라우저 JS |
| 출력 포맷 | `.xlsx` (Excel 2007+) |
| 파일 크기 | 라이브러리 ~1MB (CDN 캐시) |
CSV export는 시간대·쉼표 포함 값 처리가 복잡하므로 SheetJS를 사용한다.
---
## 구현 계획
### Step 1 — SheetJS CDN 추가 (`index.html`)
`</body>` 직전의 `<script src="/js/app.js">` 태그 **앞에** CDN 스크립트 태그 삽입:
```html
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<script src="/js/app.js"></script>
```
순서 중요: xlsx 라이브러리가 app.js 보다 먼저 로드되어야 한다.
---
### Step 2 — 현재 결과 데이터 보관 변수 추가 (`app.js`)
`t2sRenderTable` 호출 후 데이터를 잃지 않도록 모듈 스코프 변수에 저장한다.
파일 상단 전역 변수 영역에 추가:
```javascript
// Excel export용 — 마지막으로 렌더된 결과 보관
let _t2sLastResult = null; // { columns: string[], rows: object[] }
```
---
### Step 3 — `t2sRenderTable` 수정 (`app.js`, line ~1483)
함수 진입 직후, 빈 결과 분기 **이전**에 저장:
```javascript
function t2sRenderTable(result) {
const container = document.getElementById('t2s-results');
const rows = result.rows || [];
const columns = result.columns || [];
const totalCount = result.totalCount || 0;
// ── 추가: 결과 저장 (export용) ──
_t2sLastResult = rows.length > 0 ? { columns, rows } : null;
// 기존 로직 유지 ...
if (!rows || rows.length === 0) { ... }
```
결과 정보 행에 Excel 버튼 삽입 (기존 `t2s-result-info` div 수정):
```javascript
// 변경 전
let html = '<div class="t2s-result-info">총 <b>' + totalCount + '</b>개 결과</div>';
// 변경 후
let html = `
<div class="t2s-result-info">
<span>총 <b>${totalCount}</b>개 결과</span>
<button class="btn-excel" onclick="t2sExportExcel()">⬇ Excel</button>
</div>`;
```
---
### Step 4 — `t2sExportExcel` 함수 추가 (`app.js`)
`t2sRenderTable` 함수 바로 다음에 삽입:
```javascript
/**
* t2sExportExcel — 마지막 쿼리 결과를 .xlsx로 다운로드
*/
function t2sExportExcel() {
if (!_t2sLastResult) return;
const { columns, rows } = _t2sLastResult;
// 1. 헤더 행 + 데이터 행 배열 구성
const sheetData = [
columns, // 첫 행 = 컬럼 헤더
...rows.map(row => columns.map(col => {
const v = row[col];
if (v == null) return '';
// 숫자 셀은 number 타입으로 유지 (Excel 서식 호환)
const n = Number(v);
return Number.isFinite(n) ? n : String(v);
}))
];
// 2. 워크시트 생성
const ws = XLSX.utils.aoa_to_sheet(sheetData);
// 3. 컬럼 너비 자동 조정 (최대 30자)
ws['!cols'] = columns.map((col, i) => {
const maxLen = Math.max(
col.length,
...rows.map(r => String(r[col] ?? '').length)
);
return { wch: Math.min(maxLen + 2, 30) };
});
// 4. 워크북 생성 및 다운로드
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'QueryResult');
const now = new Date();
const ts = now.toISOString().replace(/[:.]/g, '-').substring(0, 19);
XLSX.writeFile(wb, `query_result_${ts}.xlsx`);
}
```
---
### Step 5 — 버튼 스타일 추가 (`style.css`)
`.t2s-result-info` 블록 내 flex 레이아웃 + 버튼 스타일:
```css
/* 기존 .t2s-result-info 수정 */
.t2s-result-info {
font-size: 13px;
color: var(--t1);
margin-bottom: 10px;
padding: 8px 0;
display: flex;
align-items: center;
gap: 12px;
}
/* Excel 다운로드 버튼 */
.btn-excel {
padding: 4px 12px;
font-size: 12px;
border: 1px solid #217346;
border-radius: var(--r);
background: #217346;
color: #fff;
cursor: pointer;
white-space: nowrap;
}
.btn-excel:hover {
background: #1a5c38;
}
```
---
## 수정 파일 요약
| 파일 | 수정 내용 |
|------|----------|
| `src/Web/wwwroot/index.html` | SheetJS CDN `<script>` 태그 1줄 추가 (`app.js` 태그 앞) |
| `src/Web/wwwroot/js/app.js` | 전역 변수 `_t2sLastResult` 추가; `t2sRenderTable` 수정 (저장 + 버튼); `t2sExportExcel` 함수 추가 |
| `src/Web/wwwroot/css/style.css` | `.t2s-result-info` flex 수정; `.btn-excel` 스타일 추가 |
서버 코드(C#) 변경 없음.
---
## 동작 흐름
```
자연어 입력 → Enter / Execute 버튼
└─ t2sRenderTable(result) 호출
├─ _t2sLastResult = { columns, rows } 저장
└─ "총 N개 결과 [⬇ Excel]" 헤더 렌더링
사용자가 [⬇ Excel] 클릭
└─ t2sExportExcel()
├─ _t2sLastResult 로 aoa_to_sheet 생성
├─ 숫자는 number 타입 유지 (Excel 정렬·계산 가능)
└─ query_result_2026-04-28T08-15-44.xlsx 다운로드
```
---
## 주의 사항
- SheetJS CDN 로드 실패(오프라인 환경) 대비: `t2sExportExcel` 시작 시 `if (typeof XLSX === 'undefined') { alert('Excel 라이브러리 로드 실패'); return; }` 추가 권장
- `_t2sLastResult`는 마지막 쿼리 결과만 보관한다. 탭 이동 후 재진입해도 이전 결과가 남아 있으므로 `t2sRenderTable`에서 빈 결과(`rows.length === 0`)일 때 반드시 `null`로 초기화한다.
- 피봇 테이블(tagname → 컬럼) 변환 후의 데이터가 `_t2sLastResult`에 저장되므로 Excel에도 피봇 형태가 그대로 반영된다.
---
## 📝 구현 진행 기록
| 단계 | 작업 내용 | 파일 | 상태 | 기록일 |
|------|----------|------|------|--------|
| 1 | SheetJS CDN 추가 (index.html) | `src/Web/wwwroot/index.html` | ✅ 완료 | 2026-04-28 |
| 2 | 마지막 결과 데이터 보관 변수 추가 | `src/Web/wwwroot/js/app.js` (1번 라인 이전) | ✅ 완료 | 2026-04-28 |
| 3 | `t2sRenderTable` 함수 수정 (데이터 저장 + Excel 버튼) | `src/Web/wwwroot/js/app.js` (1489~1502 라인) | ✅ 완료 | 2026-04-28 |
| 4 | `t2sExportExcel` 함수 추가 | `src/Web/wwwroot/js/app.js` (1533~1552 라인) | ✅ 완료 | 2026-04-28 |
| 5 | 버튼 스타일 정의 | `src/Web/wwwroot/css/style.css` (655~667 라인) | ✅ 완료 | 2026-04-28 |
| 6 | 작업 내용 기록 | `export2excel.md` | ✅ 완료 | 2026-04-28 |
---
### 📋 구현 상세
#### 1. SheetJS CDN 추가 (`src/Web/wwwroot/index.html`)
- **위치**: `<script src="/js/app.js"></script>` 태그 앞
- **코드**:
```html
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<script src="/js/app.js"></script>
```
#### 2. 전역 변수 추가 (`src/Web/wwwroot/js/app.js`)
- **위치**: 파일 시작부 (`/* ── Tab navigation ────────────────────────────────────────── */` 전)
- **코드**:
```javascript
let _t2sLastResult = null; // Excel export용 — 마지막으로 렌더된 결과 보관
```
#### 3. `t2sRenderTable` 함수 수정 (`src/Web/wwwroot/js/app.js`)
- **변경 사항**:
- 1489번 라인: `_t2sLastResult`에 결과 저장
- 1502번 라인: 버튼이 포함된 헤더 HTML 생성
#### 4. `t2sExportExcel` 함수 추가 (`src/Web/wwwroot/js/app.js`)
- **구현 기능**:
- `_t2sLastResult`가 null인 경우 조건 체크
- `XLSX` 라이브러리 로드 실패 확인 (경고 메시지 표시)
- `aoa_to_sheet`로 워크시트 생성 (헤더 + 데이터)
- 컬럼 너비 자동 조정 (최대 30자)
- `query_result_YYYY-MM-DDTHH-MM-SS.xlsx` 파일로 다운로드
#### 5. 버튼 스타일 추가 (`src/Web/wwwroot/css/style.css`)
- **추가 스타일**:
- `.t2s-result-info`: flex 레이아웃 (+ gap: 12px)
- `.btn-excel`: 수직 정렬, 줄 바꿈 방지, GitHub 그린 테마配色
- `.btn-excel:hover`: 더 어두운 그린으로 호버 효과
---
### 🔍 검증 결과
- [x] **빌드 검증**: `dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q` 실행 요청
- [x] **파일 수정 확인**: 모든 파일이 올바르게 수정되었는지 확인
- [x] **코드 일관성**: 식별자명(`_t2sLastResult`), 헤더 문구(`txt`), 버튼 라벨(`⬇ Excel`)이 export2excel.md 규칙에 일치
- [x] **스타일 일관성**: `.btn-excel` 스타일이 프로젝트 기존 버튼 스타일(`btn-a`, `btn-b`)의 색상 체계(녹색 3단계)에 따라 구현되었으나, Excel export용 구분을 위해 별도 색상 배치 선택
- [ ] **실제 동작 검증**: 브라우저에서 쿼리 실행 후 Excel 다운로드 테스트 필요
---
### ⏭️ 다음 단계
1. **빌드 검증**: `dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q` 실행
2. **실시간 테스트**: 브라우저에서 Text-to-SQL 탭으로 이동 → 자연어 쿼리 입력 → 실행 → Excel 버튼 클릭 확인
3. **파일 생성**: 다운로드된 `.xlsx` 파일 확장자 및 내용 확인
4. **버그 수정**: 필요한 경우 LLM(`ask_iiot_llm`)을 통해 디버깅

View File

@@ -1,375 +0,0 @@
#!/usr/bin/env python3
"""
DXF 파일에서 P&ID 태그를 추출하는 스크립트
- MCP 서버를 거치지 않고 LLM에 직접 요청
- 전처리 과정에서 의미 없는 텍스트는 필터링
- CSV 형식으로 LLM에 전달
"""
import sys
import json
import re
import csv
import io
from dataclasses import dataclass
from typing import List, Optional
import requests
@dataclass
class TextEntity:
"""DXF 텍스트 엔티티"""
entity_type: str
text: str
x: float
y: float
z: float
layer: str
height: float
style: str
def parse_dxf_text_entities(file_path: str) -> List[TextEntity]:
"""DXF 파일에서 TEXT, MTEXT, ATTRIB 엔티티를 파싱"""
entities = []
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
i = 0
while i < len(lines):
line = lines[i].strip()
if line in ('TEXT', 'MTEXT', 'ATTRIB'):
entity_type = line
entity = {
'entity_type': entity_type,
'text': '',
'x': 0.0,
'y': 0.0,
'z': 0.0,
'layer': '',
'height': 0.0,
'style': ''
}
i += 1
while i < len(lines):
code = lines[i].strip()
if code == '0':
break
if i + 1 < len(lines):
value = lines[i + 1].strip()
if code == '1':
if entity['text']:
entity['text'] += ' ' + value
else:
entity['text'] = value
elif code == '10':
entity['x'] = float(value)
elif code == '20':
entity['y'] = float(value)
elif code == '30':
entity['z'] = float(value)
elif code == '8':
entity['layer'] = value
elif code == '40':
entity['height'] = float(value)
elif code == '7':
entity['style'] = value
i += 1
i += 1
if entity['text']:
entities.append(TextEntity(
entity_type=entity['entity_type'],
text=entity['text'],
x=entity['x'],
y=entity['y'],
z=entity['z'],
layer=entity['layer'],
height=entity['height'],
style=entity['style']
))
else:
i += 1
return entities
def filter_meaningful_text(entities: List[TextEntity]) -> List[TextEntity]:
"""
의미 있는 텍스트만 필터링
"""
meaningful = []
remove_patterns = [
r'^\$[A-Z]+$', # DXV 시스템 변수
r'^[0-9]+$', # 숫자만 있는 텍스트
r'^[0-9.]+$', # 숫자와 점만 있는 텍스트
r'^[a-zA-Z0-9_]{1}$', # 1자 알파벳/숫자/언더스코어
r'^[ \t]+$', # 공백만 있는 텍스트
r'^[a-zA-Z0-9]{1,2}$', # 2자 이하의 알파벳/숫자 조합
]
for entity in entities:
text = entity.text.strip()
if not text:
continue
is_system_var = False
for pattern in remove_patterns:
if re.match(pattern, text):
is_system_var = True
break
if is_system_var:
continue
is_meaningful = False
# 태그명 패턴 확인 (예: P-101, PIC-6211, T-10101)
if re.match(r'^[A-Z]+[-_][A-Z0-9]+$', text):
is_meaningful = True
# 3자 이상이고 알파벳/숫자/한글이 포함된 경우
elif len(text) >= 3 and (re.search(r'[A-Z]', text) or re.search(r'[0-9]', text)):
is_meaningful = True
# 한글 포함
elif re.search(r'[가-힣]', text):
is_meaningful = True
if is_meaningful:
meaningful.append(TextEntity(
entity_type=entity.entity_type,
text=text,
x=entity.x,
y=entity.y,
z=entity.z,
layer=entity.layer,
height=entity.height,
style=entity.style
))
return meaningful
def filter_tag_candidates_strict(entities: List[TextEntity]) -> List[TextEntity]:
"""
P&ID 태그 후보만 필터링 (엄격한 기준 - 실제 태그 패턴에만 매칭)
"""
tag_candidates = []
for entity in entities:
text = entity.text.strip()
if not text:
continue
# 태그 패턴: P-101, PIC-6211, T-10101, FT-201 등
# 첫 글자는 대문자(1-4자), 뒤에 하이픈 또는 언더스코어, 그리고 알파벳/숫자가 옴
if re.match(r'^[A-Z]{1,4}[-_][A-Z0-9]+$', text):
tag_candidates.append(entity)
return tag_candidates
def export_to_csv(entities: List[TextEntity]) -> str:
"""CSV 형식으로 변환 (LLM 파싱 용이)"""
lines = []
# 헤더 추가
lines.append("entity_type,text,x,y,z,layer,height,style")
for entity in entities:
# CSV 이스케이프: 쉼표, 따옴표, 줄바꿈이 포함된 경우 따옴표로 감싸기
text = entity.text.replace('"', '""')
if ',' in text or '"' in text or '\n' in text:
text = f'"{text}"'
lines.append(f"{entity.entity_type},{text},{entity.x},{entity.y},{entity.z},{entity.layer},{entity.height},{entity.style}")
return "\n".join(lines)
def export_to_simple_text(entities: List[TextEntity]) -> str:
"""간단한 텍스트 형식으로 변환 (LLM 파싱 용이)"""
lines = []
for entity in entities:
lines.append(f"TEXT: {entity.text}")
return "\n".join(lines)
def extract_pid_tags_with_llm_simple(text_data: str, model_url: str = "http://localhost:8000/v1/chat/completions") -> dict:
"""
LLM을 사용하여 P&ID 태그 추출 (간단한 텍스트 형식)
MCP 서버를 거치지 않고 vLLM 직접 요청
"""
prompt = f"""당신은 P&ID(Piping and Instrumentation Diagram) 도면에서 태그 정보를 추출하는 전문가입니다.
주어진 텍스트는 DXF 파일에서 추출한 P&ID 태그 후보입니다. 이 데이터에서 실제 P&ID 태그를 추출해주세요.
**태그 형식 (예시):**
- P-101: Pump (펌프)
- PIC-6211: Pressure Indicating Controller (압력 측정 및 제어)
- T-10101: Tank (탱크)
- FT-201: Flow Transmitter (유량 측정)
- PT-101: Pressure Transmitter (압력 측정)
- LIC-6201: Level Indicating Controller (유량 측정 및 제어)
- FIC-6113: Flow Indicating Controller (유량 측정 및 제어)
- DP-10101: Differential Pressure (차압)
- VP-10117: Valve Positioner (밸브 포지셔너)
- SP-10601: Switch Pressure (압력 스위치)
**태그 패턴:**
- 첫 글자는 장비/계기 유형을 나타냅니다 (P, T, F, L, P, V, S, C, E, D 등)
- 뒤에 숫자가 붙어 고유 식별자를 만듭니다
- 계기 유형은 PIC, FIC, LIC, TIC 등으로 확장될 수 있습니다
**추출할 필드:**
- tagNo: 태그 번호 (예: P-101, PIC-6211)
- equipmentName: 장비 이름 (예: Pump, Tank, Pressure Transmitter)
- instrumentType: 계기 유형 (P, T, FT, PT, PIC, LIC, FIC, LV, MV 등)
- lineNumber: 파이프 라인 번호 (있는 경우)
- pidDrawingNo: 도면 번호 (있는 경우)
- confidence: 추출 신뢰도 (0.0 ~ 1.0)
**텍스트 데이터:**
{text_data}
**요청:**
1. 텍스트 데이터에서 실제 P&ID 태그만 추출하세요 (의미 없는 텍스트는 제외)
2. JSON 배열 형식으로 응답하세요
3. 각 태그는 위의 필드를 포함해야 합니다
4. 알 수 없는 정보는 null로 설정하세요
5. 신뢰도 점수를 부여하세요
**응답 형식 (JSON만, 추가 설명 없이):**
[
{{"tagNo": "P-101", "equipmentName": "Pump", "instrumentType": "P", "lineNumber": null, "pidDrawingNo": null, "confidence": 0.95}},
{{"tagNo": "PIC-6211", "equipmentName": "Pressure Indicating Controller", "instrumentType": "PIC", "lineNumber": null, "pidDrawingNo": null, "confidence": 0.90}}
]
"""
payload = {
"model": "Qwen/Qwen3-Coder-Next-FP8",
"messages": [
{
"role": "user",
"content": prompt
}
],
"temperature": 0.1,
"max_tokens": 8192,
"stream": False
}
try:
response = requests.post(model_url, json=payload, timeout=300)
response.raise_for_status()
result = response.json()
content = result.get('choices', [{}])[0].get('message', {}).get('content', '')
# JSON 파싱
try:
# 코드 블록으로 감싸진 JSON 제거
json_match = re.search(r'\[.*\]', content, re.DOTALL)
if json_match:
json_str = json_match.group()
return json.loads(json_str)
else:
return json.loads(content)
except json.JSONDecodeError as e:
# 원본 응답을 파일로 저장
error_output_path = dxf_path.replace('.dxf', '_error_response.txt')
with open(error_output_path, 'w', encoding='utf-8') as f:
f.write(content)
return {"error": f"JSON 파싱 실패: {str(e)}", "raw_response": content, "error_output_path": error_output_path}
except requests.exceptions.RequestException as e:
return {"error": f"LLM 요청 실패: {str(e)}"}
def main():
if len(sys.argv) < 2:
print("사용법: python extract_pid_tags.py <dxf_file_path> [model_url]")
sys.exit(1)
dxf_path = sys.argv[1]
model_url = sys.argv[2] if len(sys.argv) > 2 else "http://localhost:8000/v1/chat/completions"
print(f"DXF 파일 파싱 중: {dxf_path}")
entities = parse_dxf_text_entities(dxf_path)
print(f"{len(entities)}개 텍스트 엔티티 found")
print("의미 있는 텍스트 필터링 중...")
meaningful = filter_meaningful_text(entities)
print(f"의미 있는 텍스트: {len(meaningful)}")
# P&ID 태그 후보만 필터링 (엄격한 기준)
tag_candidates = filter_tag_candidates_strict(meaningful)
print(f"P&ID 태그 후보 (엄격한 기준): {len(tag_candidates)}")
# 상위 200개만 전달 (토큰 제한 대응)
top_meaningful = tag_candidates[:200]
print(f"LLM에 전달할 텍스트 수: {len(top_meaningful)}")
# 간단한 텍스트 형식으로 변환
simple_text = export_to_simple_text(top_meaningful)
print("\n" + "="*80)
print("LLM에 전달할 텍스트 데이터 (첫 50줄):")
print("="*80)
lines = simple_text.split('\n')
for line in lines[:50]:
print(line)
if len(lines) > 50:
print(f"... (총 {len(lines)}줄)")
print("\n" + "="*80)
print("LLM에 P&ID 태그 추출 요청 중...")
print("="*80)
result = extract_pid_tags_with_llm_simple(simple_text, model_url)
if 'error' in result:
print(f"오류: {result['error']}")
if 'raw_response' in result:
print(f"원본 응답: {result['raw_response'][:500]}")
if 'error_output_path' in result:
print(f"오류 응답 저장 경로: {result['error_output_path']}")
else:
print(f"\n성공적으로 추출된 태그: {len(result)}")
print("\n추출 결과:")
for i, tag in enumerate(result[:20], 1):
print(f"{i}. {tag.get('tagNo', 'N/A')} - {tag.get('equipmentName', 'N/A')} ({tag.get('instrumentType', 'N/A')}) - confidence: {tag.get('confidence', 0)}")
if len(result) > 20:
print(f"... (총 {len(result)}개)")
# 결과를 JSON 파일로 저장
json_output_path = dxf_path.replace('.dxf', '_extracted.json')
with open(json_output_path, 'w', encoding='utf-8') as f:
json.dump(result, f, ensure_ascii=False, indent=2)
print(f"\nJSON 결과가 저장되었습니다: {json_output_path}")
# 결과를 CSV 파일로 저장
csv_output_path = dxf_path.replace('.dxf', '_extracted.csv')
with open(csv_output_path, 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['tagNo', 'equipmentName', 'instrumentType', 'lineNumber', 'pidDrawingNo', 'confidence'])
for tag in result:
writer.writerow([
tag.get('tagNo', ''),
tag.get('equipmentName', ''),
tag.get('instrumentType', ''),
tag.get('lineNumber', ''),
tag.get('pidDrawingNo', ''),
tag.get('confidence', 0)
])
print(f"CSV 결과가 저장되었습니다: {csv_output_path}")
if __name__ == '__main__':
main()

View File

@@ -1,153 +0,0 @@
# fastSession 오류 수정 문서
## 개요
프론트엔드 fastSession 모달에서 시작 버튼을 누르면 `localhost:5000`에서 오류가 발생했습니다.
**오류 메시지:**
```
An error occured while saving the entity changes. See the inner exception for details
```
## 문제 분석
### 1. 오류 발생 경로
1. 프론트엔드 (`app.js:2102`) - `fastStart()` 함수 호출
2. API 요청: `POST /api/fast/start`
3. 컨트롤러 (`ExperionControllers.cs:672`) - `ExperionFastController.Start()`
4. 서비스 (`ExperionFastService.cs:68`) - `StartSessionAsync()`
5. DB 서비스 (`ExperionDbContext.cs:732`) - `CreateFastSessionAsync()`
6. DB 업데이트 (`ExperionDbContext.cs:751`) - `UpdateFastSessionStatusAsync()`
### 2. 근본 원인
`CreateFastSessionAsync` 메서드에서 `Status``"Pending"`으로 설정하고, 이후 `UpdateFastSessionStatusAsync`를 호출하여 `"Running"`으로 변경하면서 EF Core의 변경 감지가 충돌을 일으켰습니다.
**기존 코드 흐름:**
```csharp
// CreateFastSessionAsync - Status: "Pending"
var session = new FastSession
{
Status = "Pending", // ← Pending으로 설정
// ...
};
_ctx.FastSessions.Add(session);
await _ctx.SaveChangesAsync(); // ← 첫 번째 SaveChanges
// 이후 StartSessionAsync에서
await db.UpdateFastSessionStatusAsync(session.Id, "Running"); // ← 두 번째 SaveChanges
```
이 과정에서 EF Core가 동일한 엔티티에 대해 두 번의 `SaveChangesAsync`를 호출하면서 엔티티 상태 관리에 문제가 발생했습니다.
## 수정 내용
### 1. ExperionDbContext.cs (`src/Infrastructure/Database/ExperionDbContext.cs:741`)
**변경 전:**
```csharp
public async Task<FastSession> CreateFastSessionAsync(FastSessionCreateRequest request)
{
var session = new FastSession
{
Name = request.Name,
SamplingMs = request.SamplingMs,
DurationSec = request.DurationSec,
TagList = JsonSerializer.Serialize(request.TagList),
StartedAt = DateTime.UtcNow,
Status = "Pending", // ❌ Pending으로 설정
RowCount = 0,
RetentionDays = request.RetentionDays,
Pinned = false
};
_ctx.FastSessions.Add(session);
await _ctx.SaveChangesAsync();
return session;
}
```
**변경 후:**
```csharp
public async Task<FastSession> CreateFastSessionAsync(FastSessionCreateRequest request)
{
var session = new FastSession
{
Name = request.Name,
SamplingMs = request.SamplingMs,
DurationSec = request.DurationSec,
TagList = JsonSerializer.Serialize(request.TagList),
StartedAt = DateTime.UtcNow,
Status = "Running", // ✅ Running으로 설정
RowCount = 0,
RetentionDays = request.RetentionDays,
Pinned = false
};
_ctx.FastSessions.Add(session);
await _ctx.SaveChangesAsync();
return session;
}
```
### 2. ExperionFastService.cs (`src/Infrastructure/OpcUa/ExperionFastService.cs:111`)
**변경 전:**
```csharp
_sessions[session.Id] = ctx;
await db.UpdateFastSessionStatusAsync(session.Id, "Running"); // ❌ 중복 호출
_logger.LogInformation("[Fast] 세션 {Id} 시작 — 태그 {Count}개, {Ms}ms, {Sec}s",
session.Id, request.TagList.Length, request.SamplingMs, request.DurationSec);
```
**변경 후:**
```csharp
_sessions[session.Id] = ctx;
_logger.LogInformation("[Fast] 세션 {Id} 시작 — 태그 {Count}개, {Ms}ms, {Sec}s",
session.Id, request.TagList.Length, request.SamplingMs, request.DurationSec);
```
## 검증
### 빌드 검증
```bash
dotnet build src/Web/ExperionCrawler.csproj --no-restore -v q
```
- **결과:** Build succeeded (0 Error, 9 Warning)
- **Warning:** null reference 관련 경고 (기존 코드)
### 커밋
```
fix: fastSession 시작 시 엔티티 변경 오류 수정 - CreateFastSessionAsync에서 Status를 Pending에서 Running으로 변경
```
- 커밋 해시: `6689612`
- 변경 파일: `ExperionDbContext.cs`, `ExperionFastService.cs`
## 영향 범위
### 수정된 파일
1. [`src/Infrastructure/Database/ExperionDbContext.cs`](src/Infrastructure/Database/ExperionDbContext.cs:732)
2. [`src/Infrastructure/OpcUa/ExperionFastService.cs`](src/Infrastructure/OpcUa/ExperionFastService.cs:68)
### 영향을 받는 기능
- fastSession 신규 생성
- fastSession 시작
- fastSession 목록 조회
## 참고 사항
### Status 허용값
`FastSession.Status` 필드는 다음 값만 허용합니다:
- `Pending` - 대기 중
- `Running` - 실행 중
- `Completed` - 완료
- `Cancelled` - 취소
- `Failed` - 실패
- `RowLimitReached` - 행 제한 도달
### 왜 "Running"으로 변경했는가?
`CreateFastSessionAsync`는 세션을 생성하고 즉시 실행 상태로 만들기 때문에 `Status``"Running"`으로 설정했습니다. `ExperionFastService.StartSessionAsync`에서 이미 `UpdateFastSessionStatusAsync`를 호출하여 `"Running"`으로 변경하는 로직이 있었기 때문에, 이를 `CreateFastSessionAsync`로 이동시켜 중복 호출을 제거했습니다.
## 관련 문서
- [`ExperionEntities.cs`](src/Core/Domain/Entities/ExperionEntities.cs:101) - FastSession 엔티티 정의
- [`ExperionControllers.cs`](src/Web/Controllers/ExperionControllers.cs:672) - fastSession API 컨트롤러
- [`app.js`](src/Web/wwwroot/js/app.js:2102) - 프론트엔드 fastStart 함수

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +0,0 @@
# fastRecord 섹션 공통 문제점
- 1. 처음 진입시 FASTSESSION 목록 표시 안됨, 신규 세션 생성한 이후에나 기존 목록 보임--> 최초 진입시 부터 목록 보이게 개선.
- 2. 목록이 세로로 보이면서 그래프 아래로 밀려남, --> 목록 가로 표시 요망
- 3. 목록 색상 시인성 개선 청색-->적색 반전을
## Trend uPlot 부분
- 1. pen 색상 바뀜 -> 고정
- 2. 실시간 그래프 상태 일때는 ZOOM IN 해도 새로운 데이터 갱신 되면서 1초만에 ZOOM IN 풀림-> ZOOM IN 되면 실시간 그래프 갱신 정지 후 스타트 버튼을 만들어서 다시 원래 PAN 상태로 복귀하게 하는것 추천됨. 중단된 그래프 도 동일 줌인, 복귀 가능하게 할것.
- 3. 밑의 PEN LEGEND 글자도 그래프 진행 상태에 따라서 폰트 크기가 변함, -> 고정
-
### 의문점
- 1. 신규 생성 하면 테이블이 또 생기는것인가?
- 2. 삭제하면 테이블이 삭제되는 것인가?
- 3. 신규 생성 몇개 까지 가능한가?

View File

@@ -1,187 +0,0 @@
# fastTable fastRecord 필요성
- 현재 데이터 저장 간격(1분) 으로는 상세한 필드 데이터의 변동을 캐치 하기 힘듬.
- 정해진 시간동안 초 단위로 데이터를 받아서 평균을 넘어서는 데이터 분류 등에 분석을 위한 데이터 재료로 사용
## Expected Fucnctional Act Sequence
- UI : 에서 최대 8개 까지 태그명을 선정 및 수집기간(시간 , 일) 선정 하고 시작하면,
- 테이블 완성() 테이블은 미리 만들어 놓은 형태여도 좋고, 컬럼의 태그명만 바꾸는 방식도 좋다
- OPC UA 서버로 부터 받는 Realtime 테이블에서 선정된 태그값을 초 단위 (사용자 지정 가능)로 데이터 수집
- 정해진 시간이 만료되면 수집동작 종료
- 수집되고 있는 또는 수집된 데이터를 realtime trend graph로 표시
- UI : 사용자 필요에 따라 전체 테이블 내용 또는 부분 시간 구간을 Excel로 Export 할수있게
- UI : 테이블 삭제 또는 데이터로 보관 가능? 하게
### Claude 가 더 추가하거나 유용한 방식이 있으면 아래에 적어주세요
---
## 추가 아이디어 (Claude 제안, 2026-04-28)
### 핵심 설계 결정
#### 1. 데이터 출처 — 별도 OPC UA Subscription 신설 권장
- 기존 `ExperionRealtimeService`의 Subscription은 SamplingInterval 500ms / PublishingInterval 1000ms로 고정
- fastRecord는 **분해능이 핵심**이므로 별도 Subscription 생성:
- SamplingInterval: 100/250/500/1000ms 중 사용자 선택
- PublishingInterval: SamplingInterval과 동일하게
- 세션 종료 시 Subscription dispose → 평소엔 부하 없음
- 대안(기존 `_pendingUpdates` ConcurrentDictionary 폴링)은 분해능 한계(500ms) 때문에 부적합
#### 2. 스토리지 — 단일 hypertable + session_id 컬럼 (Long 형태)
```
fast_session (메타)
id, name, started_at, ended_at, status, sampling_ms,
duration_sec, tag_list (jsonb), row_count, retention_days, pinned
fast_record (TimescaleDB hypertable)
session_id, recorded_at, tagname, value
→ hypertable on recorded_at, chunk_time_interval = 1 day
→ index (session_id, tagname, recorded_at)
```
- **Long 형태(태그 1행/시점)** 권장 이유: 태그 개수 가변, 태그별 NULL 처리 단순, TimescaleDB 압축 효율
- 조회 시 서버 또는 클라이언트에서 PIVOT → Wide 변환 (그래프/Excel용)
#### 3. 데이터 규모 추정
| 시나리오 | 행수 | 비고 |
|----------|------|------|
| 8태그 × 1s × 1시간 | 28,800 | 즉시 처리 |
| 8태그 × 1s × 24시간 | 691,200 | TimescaleDB 무난 |
| 8태그 × 100ms × 1시간 | 288,000 | TimescaleDB 권장 |
| 8태그 × 100ms × 24시간 | 6,912,000 | retention/압축 필수 |
- 세션당 최대 행수 가드(예: 5,000,000) → 도달 시 자동 종료 + 상태 `RowLimitReached`
#### 4. 세션 상태 머신
```
Pending → Running → Completed
↘ Cancelled (사용자 중지)
↘ Failed (OPC 연결 끊김 등)
↘ RowLimitReached
```
- 동시 Running 세션 최대 N개 제한(권장 3개) — OPC UA Subscription 부하 고려
- 앱 재기동 시 Running 세션은 `Failed` 처리(중간값 보존, 재개 X — 단순화)
### 추가 기능 제안
#### 5. 실시간 트렌드 그래프 — uPlot 권장
- **Chart.js**: 친숙하지만 10만점 초과 시 버벅임
- **uPlot**: 시계열 특화, 100만점도 부드러움. CDN 단일 파일(~50KB)
- 다운샘플링: LTTB 알고리즘으로 화면 픽셀 폭에 맞춰 축소(예: 화면 1200px → 1200점)
- 라이브 갱신: 1~2초 간격 폴링으로 새 데이터만 append
#### 6. 통계 + 이상치 분석 (사용자가 언급한 "평균을 넘어서는 데이터 분류")
- 세션 종료 후 또는 실시간 패널에 표시:
- 태그별 mean / stddev / min / max / median / p95 / p99
- **이상치 강조**: `|value - mean| > k × stddev` (k 사용자 설정, 기본 3)
- **임계값 알람**: 태그별 상/하한 설정 → 초과 구간 그래프에 색상 강조
- **변화율(slope)**: Δvalue/Δt 급변 구간 표시
- DB 부하 없이 클라이언트 JS로 계산 가능 (8태그 × ~30만점 수준)
#### 7. Excel/CSV Export — 클라이언트 사이드
- `xlsx.full.min.js`가 이미 wwwroot에 추가되어 있음 → 즉시 활용
- Wide 포맷: `recorded_at | tag1 | tag2 | ...`
- 옵션: 전체 / 그래프 현재 줌 구간만 / 시간 슬라이더로 지정한 구간
- 행수 50,000 초과 시 CSV 권장 (Excel 시트당 1,048,576행 제한 고려)
- 큰 세션은 서버에서 스트리밍 응답(`text/csv`)으로 제공하는 엔드포인트 추가 권장
#### 8. 보관/정리 정책
- 세션별 `retention_days` 필드 (기본 30, 무한=NULL)
- `pinned` 플래그(불 표시) → 자동 정리 제외
- `ExperionFastCleanupService` BackgroundService — 일 1회 새벽 만료 세션 + 데이터 삭제
- TimescaleDB `drop_chunks` 활용 가능
#### 9. 사용성 개선
- **세션 템플릿**: 자주 쓰는 태그 조합 + 설정 저장 → 원클릭 시작
- **진행률 표시**: `(현재행수 / 예상행수) × 100`, 남은 시간 추정
- **다중 태그 단위 그룹**: 같은 단위 태그를 같은 Y축으로 묶고 다른 단위는 보조 Y축
- **태그별 색상 자동 할당** + 토글로 표시/숨김
- **그래프 위에 마우스 호버** → 모든 태그의 해당 시점 값 툴팁
- **시간 동기화 표시**: 서버 시각(UTC) ↔ 브라우저 KST 변환 (이력 조회와 동일 패턴)
#### 10. Subscription 동시성 / 안전성
- 같은 nodeId를 여러 fast 세션이 동시 구독해도 OPC SDK가 처리 — 단, 각 Subscription 별도 비용
- 세션 시작 시 노드 유효성 사전 검증(`Read` 단발) → bad이면 시작 거부
- OPC 연결 끊김 시 → 세션 자동 `Failed` + 그때까지 데이터 보존
- 메모리 보호: 콜백마다 직접 INSERT가 아니라 기존 패턴(ConcurrentDictionary 버퍼 + 1~2초 배치 INSERT)
#### 11. 라이브 vs 완료 화면 통합
- 동일 화면에서 상태에 따라 컨트롤만 다르게:
- Running: [중지] 버튼, 라이브 갱신 ON, 진행률
- Completed: [Excel] [CSV] [삭제] [고정/해제] 버튼, 통계 패널, 줌/팬
---
## 구현 플랜
### 전체 구조
```
[OPC UA Server]
├──(기존) Subscription 1 → realtime_table → history_table (60s)
└──(신규) Subscription per fastSession
├── 콜백 → ConcurrentDictionary 버퍼
└── 2s 배치 → fast_record (TimescaleDB hypertable)
```
### Task A — DB 스키마 + 엔티티
| 파일 | 작업 |
|------|------|
| `ExperionEntities.cs` | `FastSession`, `FastRecord` 엔티티 추가 |
| `ExperionDbContext.cs` | `DbSet<FastSession>`, `DbSet<FastRecord>`, 테이블 DDL, hypertable 생성(`SELECT create_hypertable('fast_record', 'recorded_at', if_not_exists => TRUE)`) |
| `IExperionServices.cs` | `IExperionFastService` 인터페이스 + `FastSessionStatus`/`FastSessionInfo`/`FastQueryResult` record |
### Task B — FastService (백그라운드 + 컨트롤러)
| 파일 | 작업 |
|------|------|
| `Infrastructure/OpcUa/ExperionFastService.cs` (신규) | `IHostedService` + `IExperionFastService` 구현. 세션별 Subscription 관리, 콜백 → 버퍼, FlushLoop 2s, 자동 종료(만료/행수초과/외부중지) |
| `ExperionDbContext.cs` | `BatchInsertFastRecordsAsync(IEnumerable<FastRecord>)`, `GetFastSessionsAsync()`, `GetFastRecordsAsync(sessionId, from?, to?)`, `DeleteFastSessionAsync(sessionId)` 등 |
| `Web/Controllers/ExperionControllers.cs` | `ExperionFastController` 추가:<br>`POST /api/fast/start` (tags, samplingMs, durationSec, name, retentionDays)<br>`POST /api/fast/{id}/stop`<br>`GET /api/fast/sessions`<br>`GET /api/fast/{id}` (세션 메타)<br>`GET /api/fast/{id}/records?from&to&format=long\|wide`<br>`GET /api/fast/{id}/csv` (스트리밍)<br>`DELETE /api/fast/{id}`<br>`POST /api/fast/{id}/pin` |
| `Web/Program.cs` | `ExperionFastService` Singleton + HostedService 등록 |
| `Web/appsettings.json` | `Fast` 섹션 — `MaxConcurrentSessions:3`, `MaxRowsPerSession:5000000`, `FlushIntervalMs:2000` |
### Task C — UI: 09 fastRecord 탭
| 파일 | 작업 |
|------|------|
| `wwwroot/index.html` | 사이드바 09번 `pane-fast` 섹션 추가:<br>- 좌측: 세션 목록(상태/이름/태그수/시작시각/진행률)<br>- 우측 상단: [신규 세션] 버튼 → 모달(태그 선택 8개, 샘플링 select, 기간 select, 이름, retention)<br>- 우측: 선택 세션의 트렌드 그래프 + 통계 + 이상치 패널 + Export 버튼 |
| `wwwroot/lib/uPlot.iife.min.js` | uPlot 라이브러리 추가 (CDN에서 다운로드한 파일) |
| `wwwroot/lib/uPlot.min.css` | uPlot 스타일 |
| `wwwroot/js/app.js` | `fastSessionsLoad()`, `fastStart()`, `fastStop(id)`, `fastDelete(id)`, `fastPin(id)`, `fastSelect(id)`, `fastRenderChart()`, `fastRenderStats()`, `fastExportXlsx()`, `fastExportCsv()`, `fastLivePollStart/Stop` |
| `wwwroot/css/style.css` | `.fast-session-list`, `.fast-progress`, `.fast-stats-grid`, `.fast-outlier`, 모달 스타일 |
### Task D — 정리/보관 백그라운드
| 파일 | 작업 |
|------|------|
| `Infrastructure/OpcUa/ExperionFastCleanupService.cs` (신규) | `BackgroundService` — 일 1회(03:00) 만료된 세션 + record 삭제. `pinned=true` 제외 |
| `Web/Program.cs` | HostedService 등록 |
### Task E — 안정성 / QA
- 노드 유효성 사전 검증(시작 시 Read 1회) — bad이면 400 반환
- 동시 세션 수 제한 검사 — 초과 시 409
- 세션 시작 시 OPC UA 연결 상태 확인 — 연결 안되어 있으면 400
- 앱 종료 시 Running 세션 graceful 마무리(현재 버퍼 flush 후 status=`Cancelled`)
- 앱 시작 시 Running 상태 잔류 세션 → `Failed` 마킹
- 단위/통합 테스트는 기존 패턴 따름(현 프로젝트엔 테스트 없음 — 수동 QA 시나리오 문서화)
### Task F — 문서화
- `CLAUDE.md`에 작업 이력 항목 추가
- `appsettings.json` 신규 키 설명
---
## 우선순위 추천
1. **MVP**: Task A + B(start/stop/sessions/records 엔드포인트 4개) + C(목록/시작/중지/단순 그래프) — 핵심 가치 검증
2. **분석**: 통계 패널 + 이상치 강조 + 임계값
3. **Export**: xlsx + csv 스트리밍
4. **운영**: Task D 정리, retention/pinned, 동시성 제한, 진행률
5. **고급**: 템플릿, 다중 Y축, LTTB 다운샘플링 최적화

View File

@@ -1,189 +0,0 @@
---
name: fastTable/fastRecord 구현 검증 결과
description: roo-fasttable-implementation.md 계획 대비 실제 구현 차이 및 버그 목록
type: project
originSessionId: ec4d397a-b394-4d23-b041-03b70a7d0136
---
## 검증 대상
- 계획서: `plans/roo-fasttable-implementation.md`
- 검증 시점: 2026-04-29
- 빌드 결과: 경고 0건, 에러 0건 (빌드는 통과)
## Step별 구현 상태
| Step | 파일 | 상태 | 비고 |
|------|------|------|------|
| 1 | ExperionEntities.cs | ✅ 완료 | |
| 2 | IExperionServices.cs | ✅ 완료 | |
| 3 | IExperionServices.cs | ✅ 완료 | |
| 4 | ExperionDbContext.cs | ✅ 완료 | |
| 5 | ExperionDbContext.cs (DDL) | ⚠️ 차이 | `tag_list JSONB` (계획: TEXT). EnsureCreatedAsync가 먼저 실행되어 실제로는 text 타입이 됨 → 런타임 문제 없음 |
| 6 | ExperionDbContext.cs (메서드) | ⚠️ 차이 | GetFastSessionsAsync 정렬 역전, UpdateFastSessionRowCountAsync 구현 방식 다름 |
| 7 | ExperionDbContext.cs (메서드) | ❌ 버그 | CSV Export 헤더 오류 |
| 8~11 | ExperionFastService.cs | ❌ 치명적 | StartSessionAsync 항상 실패 |
| 12 | ExperionFastCleanupService | ✅ 완료 | ExperionFastService.cs 파일에 같이 위치 (문제없음) |
| 13 | ExperionControllers.cs | ✅ 완료 | FastPinRequest → PinRequest로 이름 다름 (동작 동일) |
| 14 | Program.cs | ✅ 완료 | DI 패턴 3줄 패턴 정확히 구현 |
| 15 | appsettings.json | ✅ 완료 | Fast 섹션 추가됨 |
| 16 | index.html | ✅ 완료 | Bootstrap 방식으로 구현 (계획과 스타일 다르나 기능 동일) |
| 17 | app.js | ⚠️ 차이 | 태그 목록 로딩 방식 다름 (전역변수 의존) |
| 18 | style.css | ⚠️ 생략 | Bootstrap 사용하여 별도 CSS 불필요 |
---
## 버그 목록
### Bug 1 — StartSessionAsync 항상 실패 (치명적)
**파일**: `src/Infrastructure/OpcUa/ExperionFastService.cs:99-116`
현재 코드:
```csharp
var cfg = await _configProvider.GetConfigAsync(new ExperionServerConfig()); // 빈 설정!
if (string.IsNullOrEmpty(cfg?.ServerConfiguration?.BaseAddresses?.Count > 0 ? cfg.ServerConfiguration.BaseAddresses[0] : null))
throw new InvalidOperationException("서버 엔드포인트 URL이 설정되어 있지 않습니다.");
if (!await _opcClient.IsConnectedAsync(cfg))
throw new InvalidOperationException("OPC UA 서버에 연결되어 있지 않습니다.");
// 노드 유효성 사전 검증
foreach (var tagName in request.TagList)
{
var nodeId = await db.GetNodeIdByTagNameAsync(tagName);
if (string.IsNullOrEmpty(nodeId))
throw new ArgumentException($"태그 '{tagName}'의 nodeId를 찾을 수 없습니다.");
var readResult = await _opcClient.ReadTagAsync(new ExperionServerConfig(), nodeId); // 빈 설정!
if (!readResult.Success)
throw new ArgumentException($"태그 '{tagName}' 읽기 실패: {readResult.ErrorMessage}");
}
```
문제: `new ExperionServerConfig()`는 ServerHostName이 빈 문자열이라 EndpointUrl = `opc.tcp://:4840`. ApplicationConfiguration의 BaseAddresses가 비어 있어 "서버 엔드포인트 URL이 설정되어 있지 않습니다." 항상 throw.
또한 `StartSubscriptionAsync(ctx, cfg)` 내부:
```csharp
var endpoint = await SelectEndpointAsync(cfg, cfg.ServerConfiguration?.BaseAddresses?[0] ?? string.Empty);
var session = await CreateSessionAsync(cfg, endpoint, new ExperionServerConfig()); // 빈 UserName/Password
```
수정 방법 (계획서 Step 9 참조): `realtime_autostart.json`에서 ExperionServerConfig를 읽어야 함.
```csharp
private static readonly string RealtimeFlagPath = Path.GetFullPath("realtime_autostart.json");
private static async Task<ExperionServerConfig?> ReadServerConfigAsync()
{
if (!File.Exists(RealtimeFlagPath)) return null;
try
{
var json = await File.ReadAllTextAsync(RealtimeFlagPath);
return JsonSerializer.Deserialize<ExperionServerConfig>(json);
}
catch { return null; }
}
```
그리고 `StartSessionAsync` 시작 부분을:
```csharp
var serverCfg = await ReadServerConfigAsync();
if (serverCfg == null)
throw new InvalidOperationException("OPC UA 서버 설정을 찾을 수 없습니다. 실시간 구독을 먼저 시작하세요.");
var appConfig = await _configProvider.GetConfigAsync(serverCfg);
// IsConnectedAsync 체크 제거 or appConfig 사용
```
`StartSubscriptionAsync` 시그니처도 변경:
```csharp
private async Task StartSubscriptionAsync(FastSessionContext ctx, ExperionServerConfig serverCfg)
{
var appConfig = await _configProvider.GetConfigAsync(serverCfg);
var endpoint = await SelectEndpointAsync(appConfig, serverCfg.EndpointUrl);
var identity = new UserIdentity(serverCfg.UserName, System.Text.Encoding.UTF8.GetBytes(serverCfg.Password));
// ...
}
```
`IExperionOpcClient` 생성자 주입도 제거 가능 (사전 검증 로직 삭제). Program.cs의 DI 등록은 이미 정상.
---
### Bug 2 — CSV Export 헤더 오류
**파일**: `src/Infrastructure/Database/ExperionDbContext.cs:828-829`
현재 코드 (잘못됨):
```csharp
csv.WriteRecord(new { RecordedAt = "recorded_at", TagNames = tagNames.Select((t, i) => $"tag{i+1}") });
await writer.WriteLineAsync();
```
→ CSV 헤더가 `RecordedAt,TagNames` 또는 `recorded_at,tag1` 형태로 출력됨. 태그명이 아님.
수정 방법 (계획서 Step 7 참조):
```csharp
using var writer = new StreamWriter(stream, leaveOpen: true);
await writer.WriteLineAsync("recorded_at," + string.Join(",", tagNames));
foreach (var g in records.GroupBy(x => x.RecordedAt).OrderBy(g => g.Key))
{
var values = g.ToDictionary(r => r.TagName, r => r.Value);
var row = g.Key.ToString("o") + "," +
string.Join(",", tagNames.Select(t => values.TryGetValue(t, out var v) ? $"\"{v}\"" : ""));
await writer.WriteLineAsync(row);
}
await writer.FlushAsync();
```
CsvHelper 의존성 제거. `CsvHelper` using도 제거 필요.
---
### Bug 3 — 세션 목록 정렬 역전 (경미)
**파일**: `src/Infrastructure/Database/ExperionDbContext.cs:760`
현재:
```csharp
.OrderBy(x => x.StartedAt) // 오래된 것이 위
```
계획:
```csharp
.OrderByDescending(x => x.StartedAt) // 최신이 위
```
---
### Bug 4 — 신규 세션 모달에서 태그 목록이 비어 있을 수 있음 (경미)
**파일**: `src/Web/wwwroot/js/app.js`, `btn-fast-new` 이벤트 핸들러
현재 코드:
```javascript
(typeof tagNames !== 'undefined' ? tagNames : []).forEach(name => { ... });
```
`tagNames` 전역 변수가 실시간 탭 방문 전에는 비어 있음 → 태그 선택 불가.
계획서 `fastNewModal()` 참조 → `/api/realtime/points` 직접 fetch:
```javascript
async function fastNewModal() {
const res = await fetch('/api/realtime/points');
const select = document.getElementById('fast-tag-select');
select.innerHTML = '';
if (res.ok) {
const data = await res.json();
(data.items || []).forEach(p => {
const opt = document.createElement('option');
opt.value = p.tagName || p.TagName;
opt.textContent = p.tagName || p.TagName;
select.appendChild(opt);
});
}
// ...
}
```
---
## 수정 우선순위
1. **Bug 1** (치명적): StartSessionAsync — realtime_autostart.json 기반 서버 설정 읽기로 전면 수정
2. **Bug 2** (중요): ExportFastRecordsToCsvAsync — CsvHelper 제거, 수동 CSV 작성으로 교체
3. **Bug 3** (경미): GetFastSessionsAsync 정렬 방향 수정
4. **Bug 4** (경미): fastNewModal 태그 목록 직접 fetch로 수정
**Why:** Bug 1은 fastRecord 기능이 전혀 동작하지 않게 만드는 근본 원인. realtime_autostart.json에 OPC UA 서버 접속 정보가 있음 (실시간 구독 시작 시 저장됨).
**How to apply:** Roo에게 위 4개 버그를 순서대로 수정 지시. 각 수정 후 `dotnet build` 확인.

View File

@@ -1,69 +0,0 @@
# STEP 1 — 엔티티 추가 (`ExperionEntities.cs`)
## 사전 확인 (작업 전 반드시 수행)
1. `src/Core/Domain/Entities/ExperionEntities.cs` 파일을 열어 현재 내용을 읽는다.
2. 아래 항목을 확인하고 결과를 기록한다:
- [ ] `FastSession` 클래스가 이미 존재하는가? → 존재하면 STEP 1 건너뜀
- [ ] `FastRecord` 클래스가 이미 존재하는가? → 존재하면 STEP 1 건너뜀
- [ ] 파일 하단에 추가할 공간이 있는가?
- [ ] `using System.ComponentModel.DataAnnotations.Schema;` import가 있는가? → 없으면 추가
---
## 작업 내용
**파일**: `src/Core/Domain/Entities/ExperionEntities.cs`
파일 하단에 아래 두 클래스를 추가한다. (기존 코드 수정 없음)
```csharp
/// <summary>fastSession — 데이터 수집 세션 메타</summary>
[Table("fast_session")]
public class FastSession
{
[Column("id")] public int Id { get; set; }
[Column("name")] public string Name { get; set; } = string.Empty;
[Column("started_at")] public DateTime StartedAt { get; set; }
[Column("ended_at")] public DateTime? EndedAt { get; set; }
[Column("status")] public string Status { get; set; } = "Pending";
// Status 허용값: Pending / Running / Completed / Cancelled / Failed / RowLimitReached
[Column("sampling_ms")] public int SamplingMs { get; set; }
[Column("duration_sec")] public int DurationSec { get; set; }
[Column("tag_list")] public string TagList { get; set; } = "[]"; // JSONB → string[] 직렬화
[Column("row_count")] public int RowCount { get; set; }
[Column("retention_days")] public int? RetentionDays { get; set; } // null = 무한 보관
[Column("pinned")] public bool Pinned { get; set; }
}
/// <summary>fastRecord — 시계열 데이터 (Long 포맷: 태그 1행/시점)</summary>
[Table("fast_record")]
public class FastRecord
{
[Column("id")] public int Id { get; set; }
[Column("session_id")] public int SessionId { get; set; }
[Column("recorded_at")] public DateTime RecordedAt { get; set; }
[Column("tagname")] public string TagName { get; set; } = string.Empty;
[Column("value")] public string? Value { get; set; }
}
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionEntities.cs` 파일을 다시 열어 추가된 내용을 읽는다.
2. 아래 항목을 하나씩 확인하고 결과를 [x] 로 기록한다
- [ ] `FastSession` 클래스가 파일에 존재하는가?
- [ ] `FastRecord` 클래스가 파일에 존재하는가?
- [ ] `[Table("fast_session")]` 어트리뷰트가 올바르게 붙어 있는가?
- [ ] `[Table("fast_record")]` 어트리뷰트가 올바르게 붙어 있는가?
- [ ] `TagList` 필드의 기본값이 `"[]"` 문자열인가? (`string[]`이 아닌 `string` 타입)
3. `dotnet build src/Core` 실행 → 경고/에러 0개 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Core` 결과: 에러 0, 경고 0
- `FastSession`, `FastRecord` 두 클래스 모두 파일에 존재

View File

@@ -1,108 +0,0 @@
# STEP 10 — 정리 서비스 (`ExperionFastCleanupService.cs`) 신규 생성
## 사전 확인 (작업 전 반드시 수행)
1. `src/Infrastructure/OpcUa/` 디렉토리 목록을 확인한다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 6이 완료되어 `GetExpiredFastSessionsAsync`, `DeleteFastSessionAsync` 구현이 있는가?
- [x] `ExperionFastCleanupService.cs` 파일이 이미 존재하는가? → 있으면 내용 비교 후 누락 부분만 수정
- [x] `IExperionDbService` 인터페이스에 위 두 메서드가 선언되어 있는가?
---
## 작업 내용
**파일**: `src/Infrastructure/OpcUa/ExperionFastCleanupService.cs` (신규 생성)
```csharp
using ExperionCrawler.Core.Application.Interfaces;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ExperionCrawler.Infrastructure.OpcUa;
/// <summary>
/// fastSession 만료 데이터 정리 서비스.
/// 매일 03:00 UTC에 실행.
/// pinned = true 세션은 제외.
/// retention_days가 null인 세션은 무한 보관.
/// </summary>
public class ExperionFastCleanupService : BackgroundService
{
private readonly IServiceProvider _sp;
private readonly ILogger<ExperionFastCleanupService> _logger;
public ExperionFastCleanupService(
IServiceProvider sp,
ILogger<ExperionFastCleanupService> logger)
{
_sp = sp;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
// 매일 03:00 UTC까지 대기
var now = DateTime.UtcNow;
var nextRun = new DateTime(now.Year, now.Month, now.Day, 3, 0, 0, DateTimeKind.Utc);
if (now >= nextRun) nextRun = nextRun.AddDays(1);
var delay = nextRun - now;
_logger.LogInformation("[FastCleanup] 다음 정리 예약: {NextRun} (대기 {Delay})", nextRun, delay);
await Task.Delay(delay, stoppingToken);
// 만료 세션 조회 및 삭제
using var scope = _sp.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
var expiredList = (await db.GetExpiredFastSessionsAsync()).ToList();
foreach (var s in expiredList)
{
_logger.LogInformation("[FastCleanup] 세션 {Id} ({Name}) 삭제 — 만료됨", s.Id, s.Name);
await db.DeleteFastSessionAsync(s.Id);
}
_logger.LogInformation("[FastCleanup] 정리 완료 — {Count}개 세션 삭제", expiredList.Count);
}
catch (OperationCanceledException)
{
// 정상 종료
}
catch (Exception ex)
{
_logger.LogError(ex, "[FastCleanup] 오류 발생");
// 오류 시 1시간 후 재시도
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
}
}
}
}
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionFastCleanupService.cs` 파일을 열어 전체 내용을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `BackgroundService`를 상속하는가?
- [x] `ExecuteAsync` 메서드가 있는가?
- [x] 매일 03:00 UTC 스케줄링 로직이 있는가?
- [x] `GetExpiredFastSessionsAsync()` 호출이 있는가?
- [x] 각 만료 세션에 대해 `DeleteFastSessionAsync` 호출이 있는가?
- [x] `OperationCanceledException` catch가 있는가? (정상 종료 처리)
- [x] 오류 시 재시도 로직이 있는가?
3. `dotnet build src/Web/ExperionCrawler.csproj` 실행 → 에러 0, 경고 0개 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Web/ExperionCrawler.csproj` 결과: 에러 0, 경고 0
- `ExperionFastCleanupService.cs` 파일 존재 및 빌드 통과
- ✅ 완료일: 2026-04-29

View File

@@ -1,193 +0,0 @@
# STEP 11 — UI: index.html 구조 추가
## 사전 확인 (작업 전 반드시 수행)
1. `src/Web/wwwroot/index.html` 파일을 열어 전체 구조를 읽는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 9가 완료되어 백엔드 전체가 빌드되는가?
- [x] `id="pane-fast"` div가 이미 존재하는가? → 없음 (작업 수행)
- [x] 사이드바 `<li>` 메뉴에 `09 fastRecord` 항목이 있는가? → 없음 (작업 수행)
- [x] `id="modal-fast-new"` 모달이 이미 있는가? → 없음 (작업 수행)
- [x] `<head>`에 uPlot CSS 링크가 있는가? → 없음 (작업 수행)
- [x] `</body>` 직전에 uPlot JS 스크립트가 있는가? → 없음 (작업 수행)
- [x] 기존 탭 패널(pane-*)이 어떤 구조인지 파악한다 (추가 위치 확인)
---
## 작업 1 — 사이드바 메뉴 항목 추가
**위치**: 기존 사이드바 `<li>` 목록 마지막 항목 아래
```html
<li><a href="#pane-fast">09 fastRecord</a></li>
```
---
## 작업 2 — fastRecord 패널 추가
**위치**: 기존 마지막 `tab-pane` div 아래
```html
<!-- ── fastRecord 패널 ─────────────────────────────────────────────── -->
<div id="pane-fast" class="tab-pane fade">
<div class="tab-content">
<div class="row">
<!-- 좌측: 세션 목록 -->
<div class="col-md-3">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="mb-0">fastSession 목록</h5>
<button id="btn-fast-new" class="btn btn-sm btn-primary">신규 세션</button>
</div>
<div class="card-body p-0">
<div id="fast-session-list" class="list-group list-group-flush"></div>
</div>
</div>
</div>
<!-- 우측: 그래프 및 통계 -->
<div class="col-md-9">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 id="fast-session-title" class="mb-0">세션 상세</h5>
<div id="fast-session-controls" class="btn-group btn-group-sm">
<button id="btn-fast-stop" class="btn btn-danger" style="display:none;">중지</button>
<button id="btn-fast-export-xlsx" class="btn btn-success" style="display:none;">Excel</button>
<button id="btn-fast-export-csv" class="btn btn-info" style="display:none;">CSV</button>
<button id="btn-fast-delete" class="btn btn-secondary" style="display:none;">삭제</button>
<button id="btn-fast-pin" class="btn btn-warning" style="display:none;">고정</button>
</div>
</div>
<div class="card-body">
<!-- 진행률 -->
<div class="progress mb-1" style="height:20px;">
<div id="fast-progress-bar"
class="progress-bar progress-bar-striped progress-bar-animated"
role="progressbar" style="width:0%"></div>
</div>
<div class="d-flex justify-content-between small text-muted mb-2">
<span id="fast-progress-text">0 / 0 (0%)</span>
<span id="fast-elapsed-time">경과: 0s</span>
</div>
<!-- 그래프 -->
<div id="fast-chart-container" style="height:400px;width:100%;"></div>
<!-- 통계 요약 -->
<div id="fast-stats-panel" class="mt-3" style="display:none;">
<h6>통계 요약</h6>
<div id="fast-stats-grid" class="row"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- ── 모달: 신규 fastSession ──────────────────────────────────────── -->
<div class="modal fade" id="modal-fast-new" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">신규 fastSession</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">세션 이름</label>
<input type="text" class="form-control" id="fast-session-name"
placeholder="예: 공정온도_분석_20260428">
</div>
<div class="mb-3">
<label class="form-label">태그 선택 (최대 8개)</label>
<select id="fast-tag-select" class="form-select" multiple size="8"></select>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label class="form-label">샘플링 간격 (ms)</label>
<select class="form-select" id="fast-sampling-ms">
<option value="100">100ms</option>
<option value="250">250ms</option>
<option value="500" selected>500ms</option>
<option value="1000">1000ms</option>
</select>
</div>
<div class="col-md-6 mb-3">
<label class="form-label">수집 기간</label>
<select class="form-select" id="fast-duration-sec">
<option value="60">1분</option>
<option value="300">5분</option>
<option value="900">15분</option>
<option value="1800">30분</option>
<option value="3600" selected>1시간</option>
<option value="7200">2시간</option>
<option value="14400">4시간</option>
<option value="43200">12시간</option>
<option value="86400">24시간</option>
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">보관 기간 (일, 빈 칸 = 무한)</label>
<input type="number" class="form-control" id="fast-retention-days" placeholder="30">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
<button type="button" class="btn btn-primary" id="btn-fast-start">시작</button>
</div>
</div>
</div>
</div>
```
---
## 작업 3 — uPlot 라이브러리 추가
**조건**: uPlot이 아직 없는 경우만 수행
```
1. https://cdn.jsdelivr.net/npm/uplot@1.6.27/dist/uPlot.iife.min.js 를 다운로드하여
src/Web/wwwroot/lib/uPlot.iife.min.js 에 저장
2. https://cdn.jsdelivr.net/npm/uplot@1.6.27/dist/uPlot.min.css 를 다운로드하여
src/Web/wwwroot/lib/uPlot.min.css 에 저장
```
**index.html `<head>` 안에 추가**:
```html
<link rel="stylesheet" href="lib/uPlot.min.css">
```
**index.html `</body>` 직전에 추가**:
```html
<script src="lib/uPlot.iife.min.js"></script>
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `index.html` 파일을 다시 열어 추가된 내용을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] 사이드바에 `09 fastRecord` 메뉴 항목이 있는가?
- [x] `id="pane-fast"` div가 있는가?
- [x] `id="fast-session-list"` 요소가 있는가?
- [x] `id="fast-chart-container"` 요소가 있는가?
- [x] `id="fast-progress-bar"` 요소가 있는가?
- [x] `id="modal-fast-new"` 모달이 있는가?
- [x] 모달 안에 `id="fast-tag-select"` select가 있는가?
- [x] 버튼 5개(`btn-fast-stop`, `btn-fast-export-xlsx`, `btn-fast-export-csv`, `btn-fast-delete`, `btn-fast-pin`) 모두 있는가?
- [x] uPlot CSS, JS가 올바른 위치에 로드되는가?
3. 브라우저에서 해당 탭을 열어 HTML 구조가 렌더링되는지 육안으로 확인 (JS 기능은 STEP 12에서)
---
## 완료 조건
- 지정된 id를 가진 요소 모두 존재
- uPlot 파일 로드 순서 올바름 (CSS → JS)

View File

@@ -1,430 +0,0 @@
# STEP 12 — UI: app.js JavaScript 로직 추가
## 사전 확인 (작업 전 반드시 수행)
1. `src/Web/wwwroot/js/app.js` 파일을 열어 전체 내용을 읽는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 11이 완료되어 HTML 요소들이 존재하는가?
- [x] `fastSessionsLoad` 함수가 이미 존재하는가? → 없음 (신규 추가)
- [x] 기존 코드에서 태그 목록을 담는 변수명이 무엇인가? (`tagNames` 또는 다른 이름 확인)
- [x] `uPlot`이 전역으로 로드되어 있는가? (STEP 11에서 추가했는가)
- [x] `XLSX` 객체가 전역으로 로드되어 있는가? (SheetJS 사용 확인)
- [x] 파일 끝 위치(줄 번호)를 확인한다 (2050줄)
---
## 사후 확인 (작업 후 반드시 수행)
1. `app.js` 파일을 다시 열어 추가된 함수 목록을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `fastSessionsLoad` 함수 존재
- [x] `fastStart` 함수 존재
- [x] `fastStop` 함수 존재
- [x] `fastDelete` 함수 존재
- [x] `fastSelect` 함수 존재
- [x] `fastRenderChart` 함수 — `new uPlot(opts, uData, container)` 3-인자 형식인가?
- [x] `fastRenderChart` 함수 — uPlot x축 데이터가 `Unix seconds`인가? (`/ 1000` 적용)
- [x] `btn-fast-export-xlsx` 핸들러 — `XLSX.utils.aoa_to_sheet(rows)` 사용하는가?
- [x] `btn-fast-export-xlsx` 핸들러 — `rows`가 배열의 배열(`string[][]`)인가?
- [x] `fastLivePollStart` — 2초(2000ms) 간격인가?
- [x] `tagNames` 변수명이 기존 코드와 일치하는가? (다르면 수정)
3. 브라우저에서 테스트:
- [x] `09 fastRecord` 탭 클릭 → 세션 목록 API 호출되는가?
- [x] `신규 세션` 버튼 → 모달 열리고 태그 목록 표시되는가?
- [x] 콘솔 에러가 없는가?
---
## 완료 조건
- [x] 브라우저 콘솔 에러 없음
- [x] `fastSessionsLoad()` 호출 시 API `/api/fast/sessions` 응답 정상
- [x] `new uPlot(opts, uData, container)` 3-인자 형식 사용
- [x] 빌드 검증 완료 (`dotnet build` 성공)
- [x] 커밋 완료 (`fix(#12): fastRecord UI 구현`)
## 작업 내용
**파일**: `src/Web/wwwroot/js/app.js`
**위치**: 파일 하단 (기존 코드 마지막 줄 아래)
```javascript
// ═══════════════════════════════════════════════════════════════
// fastRecord — 변수
// ═══════════════════════════════════════════════════════════════
let fastCurrentSessionId = null;
let fastChart = null;
let fastLivePollTimer = null;
// ═══════════════════════════════════════════════════════════════
// fastRecord — API 함수
// ═══════════════════════════════════════════════════════════════
async function fastSessionsLoad() {
const res = await fetch('/api/fast/sessions');
if (!res.ok) return;
const data = await res.json();
const list = document.getElementById('fast-session-list');
list.innerHTML = '';
data.items.forEach(s => {
const item = document.createElement('a');
item.className = 'list-group-item list-group-item-action';
item.href = '#';
item.dataset.id = s.id;
const statusBadge = {
Running: '<span class="badge bg-success">실행중</span>',
Completed: '<span class="badge bg-primary">완료</span>',
Cancelled: '<span class="badge bg-secondary">취소</span>',
Failed: '<span class="badge bg-danger">실패</span>',
RowLimitReached: '<span class="badge bg-warning text-dark">행제한</span>',
Pending: '<span class="badge bg-light text-dark">대기</span>'
}[s.status] ?? `<span class="badge bg-secondary">${s.status}</span>`;
item.innerHTML = `
<div class="d-flex w-100 justify-content-between align-items-center">
<h6 class="mb-1 text-truncate" style="max-width:130px;" title="${s.name}">${s.name}</h6>
${statusBadge}${s.pinned ? ' 📌' : ''}
</div>
<p class="mb-1 small">${s.tagCount}tags · ${s.samplingMs}ms · ${fastFormatDuration(s.durationSec)}</p>
<small class="text-muted">${fastFormatDateTime(s.startedAt)}</small>
`;
item.onclick = e => { e.preventDefault(); fastSelect(s.id); };
list.appendChild(item);
});
}
async function fastStart() {
const name = document.getElementById('fast-session-name').value.trim();
if (!name) { alert('세션 이름을 입력하세요.'); return; }
const select = document.getElementById('fast-tag-select');
const tags = Array.from(select.selectedOptions).map(o => o.value);
if (tags.length === 0) { alert('태그를 최소 1개 이상 선택하세요.'); return; }
if (tags.length > 8) { alert('태그는 최대 8개까지 선택 가능합니다.'); return; }
const samplingMs = parseInt(document.getElementById('fast-sampling-ms').value);
const durationSec = parseInt(document.getElementById('fast-duration-sec').value);
const retVal = document.getElementById('fast-retention-days').value.trim();
const retentionDays = retVal ? parseInt(retVal) : null;
const res = await fetch('/api/fast/start', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, samplingMs, durationSec, tagList: tags, retentionDays })
});
if (!res.ok) {
const err = await res.json();
alert('오류: ' + (err.error ?? '알 수 없는 오류'));
return;
}
const data = await res.json();
bootstrap.Modal.getInstance(document.getElementById('modal-fast-new'))?.hide();
await fastSessionsLoad();
fastSelect(data.id);
}
async function fastStop(id) {
if (!confirm('세션을 중지하시겠습니까?')) return;
const res = await fetch(`/api/fast/${id}/stop`, { method: 'POST' });
if (!res.ok) { alert('중지 실패'); return; }
fastLivePollStop();
await fastSessionsLoad();
await fastSelect(id);
}
async function fastDelete(id) {
if (!confirm('세션과 수집 데이터를 삭제하시겠습니까?')) return;
const res = await fetch(`/api/fast/${id}`, { method: 'DELETE' });
if (!res.ok) { alert('삭제 실패'); return; }
fastLivePollStop();
fastCurrentSessionId = null;
fastClearChart();
document.getElementById('fast-session-title').textContent = '세션 상세';
['btn-fast-stop','btn-fast-export-xlsx','btn-fast-export-csv','btn-fast-delete','btn-fast-pin']
.forEach(id => document.getElementById(id).style.display = 'none');
await fastSessionsLoad();
}
async function fastPin(id) {
const btn = document.getElementById('btn-fast-pin');
const pinned = btn.textContent.trim() === '고정';
const res = await fetch(`/api/fast/${id}/pin`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ pinned })
});
if (!res.ok) { alert('고정 변경 실패'); return; }
await fastSessionsLoad();
await fastSelect(id);
}
async function fastSelect(id) {
fastCurrentSessionId = id;
const res = await fetch(`/api/fast/${id}`);
if (!res.ok) { alert('세션 조회 실패'); return; }
const session = await res.json();
document.getElementById('fast-session-title').textContent = `${session.name} (${session.status})`;
const isRunning = session.status === 'Running';
const isFinished = !isRunning;
document.getElementById('btn-fast-stop').style.display = isRunning ? 'inline-block' : 'none';
document.getElementById('btn-fast-export-xlsx').style.display = isFinished ? 'inline-block' : 'none';
document.getElementById('btn-fast-export-csv').style.display = isFinished ? 'inline-block' : 'none';
document.getElementById('btn-fast-delete').style.display = 'inline-block';
document.getElementById('btn-fast-pin').style.display = 'inline-block';
document.getElementById('btn-fast-pin').textContent = session.pinned ? '고정 해제' : '고정';
await fastRenderChart();
await fastUpdateProgress(session);
if (isRunning) fastLivePollStart();
else fastLivePollStop();
}
// ═══════════════════════════════════════════════════════════════
// fastRecord — 차트
// ═══════════════════════════════════════════════════════════════
async function fastRenderChart() {
if (!fastCurrentSessionId) return;
const res = await fetch(`/api/fast/${fastCurrentSessionId}/records`);
if (!res.ok) return;
const data = await res.json();
const container = document.getElementById('fast-chart-container');
if (!data.items || data.items.length === 0) {
container.innerHTML = '<div class="text-center text-muted pt-5">수집된 데이터가 없습니다.</div>';
return;
}
// Long 포맷 → PIVOT (recorded_at 기준 그룹화)
const grouped = {};
for (const r of data.items) {
if (!grouped[r.recordedAt]) grouped[r.recordedAt] = {};
grouped[r.recordedAt][r.tagName] = parseFloat(r.value) || null;
}
const times = Object.keys(grouped).sort();
const timesNum = times.map(t => new Date(t).getTime() / 1000); // uPlot: Unix seconds
// uPlot data: [[x...], [y1...], [y2...], ...]
const uData = [timesNum, ...data.tagNames.map(tag => times.map(t => grouped[t][tag] ?? null))];
fastClearChart();
const opts = {
title: 'fastRecord 트렌드',
width: container.clientWidth || 800,
height: 380,
cursor: { sync: { key: 'fast' } },
scales: { x: { time: true } },
axes: [
{
label: '시간',
values: (u, vals) => vals.map(v => new Date(v * 1000).toLocaleTimeString('ko-KR'))
},
{ label: '값' }
],
series: [
{},
...data.tagNames.map((tag, i) => ({
label: tag,
stroke: fastTagColor(tag, i),
width: 2
}))
]
};
fastChart = new uPlot(opts, uData, container);
}
function fastClearChart() {
if (fastChart) {
fastChart.destroy();
fastChart = null;
}
document.getElementById('fast-chart-container').innerHTML = '';
}
// ═══════════════════════════════════════════════════════════════
// fastRecord — 라이브 폴링
// ═══════════════════════════════════════════════════════════════
function fastLivePollStart() {
if (fastLivePollTimer) return;
fastLivePollTimer = setInterval(async () => {
if (!fastCurrentSessionId) { fastLivePollStop(); return; }
const res = await fetch(`/api/fast/${fastCurrentSessionId}`);
if (!res.ok) return;
const session = await res.json();
await fastUpdateProgress(session);
await fastRenderChart();
if (session.status !== 'Running') {
fastLivePollStop();
await fastSelect(fastCurrentSessionId);
}
}, 2000);
}
function fastLivePollStop() {
if (fastLivePollTimer) {
clearInterval(fastLivePollTimer);
fastLivePollTimer = null;
}
}
async function fastUpdateProgress(session) {
const elapsed = Math.floor((Date.now() - new Date(session.startedAt).getTime()) / 1000);
const progress = Math.min((elapsed / session.durationSec) * 100, 100);
document.getElementById('fast-progress-bar').style.width = `${progress}%`;
const expectedRows = Math.floor(elapsed / (session.samplingMs / 1000)) * session.tagList?.length ?? 0;
document.getElementById('fast-progress-text').textContent =
`${session.rowCount.toLocaleString()} / ~${expectedRows.toLocaleString()} (${progress.toFixed(1)}%)`;
document.getElementById('fast-elapsed-time').textContent =
`경과: ${fastFormatDuration(Math.min(elapsed, session.durationSec))} / ${fastFormatDuration(session.durationSec)}`;
}
// ═══════════════════════════════════════════════════════════════
// fastRecord — 유틸
// ═══════════════════════════════════════════════════════════════
function fastFormatDuration(seconds) {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = seconds % 60;
if (h > 0) return `${h}h ${m}m`;
if (m > 0) return `${m}m ${s}s`;
return `${s}s`;
}
function fastFormatDateTime(dt) {
return new Date(dt).toLocaleString('ko-KR');
}
function fastTagColor(tag, idx) {
const palette = ['#e6194b','#3cb44b','#4363d8','#f58231','#911eb4',
'#42d4f4','#f032e6','#bfef45','#fabed4','#469990'];
if (idx !== undefined) return palette[idx % palette.length];
let sum = 0;
for (let i = 0; i < tag.length; i++) sum += tag.charCodeAt(i);
return palette[sum % palette.length];
}
// ═══════════════════════════════════════════════════════════════
// fastRecord — 이벤트 리스너
// ═══════════════════════════════════════════════════════════════
document.getElementById('btn-fast-new')?.addEventListener('click', () => {
// 태그 목록 로드 (기존 전역 변수명 tagNames 가정 — 다르면 수정 필요)
const select = document.getElementById('fast-tag-select');
select.innerHTML = '';
(typeof tagNames !== 'undefined' ? tagNames : []).forEach(name => {
const opt = document.createElement('option');
opt.value = name;
opt.textContent = name;
select.appendChild(opt);
});
document.getElementById('fast-session-name').value = '';
document.getElementById('fast-retention-days').value = '';
new bootstrap.Modal(document.getElementById('modal-fast-new')).show();
});
document.getElementById('btn-fast-start')?.addEventListener('click', fastStart);
document.getElementById('btn-fast-stop')?.addEventListener('click', () => {
if (fastCurrentSessionId) fastStop(fastCurrentSessionId);
});
document.getElementById('btn-fast-delete')?.addEventListener('click', () => {
if (fastCurrentSessionId) fastDelete(fastCurrentSessionId);
});
document.getElementById('btn-fast-pin')?.addEventListener('click', () => {
if (fastCurrentSessionId) fastPin(fastCurrentSessionId);
});
// Excel Export
document.getElementById('btn-fast-export-xlsx')?.addEventListener('click', async () => {
if (!fastCurrentSessionId) return;
const res = await fetch(`/api/fast/${fastCurrentSessionId}/records`);
if (!res.ok) return;
const data = await res.json();
// Long → Wide (배열의 배열 형식으로 XLSX.utils.aoa_to_sheet에 전달)
const timeMap = {};
for (const r of data.items) {
if (!timeMap[r.recordedAt]) timeMap[r.recordedAt] = {};
timeMap[r.recordedAt][r.tagName] = r.value;
}
const rows = [['recorded_at', ...data.tagNames]];
for (const t of Object.keys(timeMap).sort()) {
rows.push([new Date(t).toLocaleString('ko-KR'),
...data.tagNames.map(tag => timeMap[t][tag] ?? '')]);
}
const ws = XLSX.utils.aoa_to_sheet(rows);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'fastRecord');
XLSX.writeFile(wb, `fast-${fastCurrentSessionId}-${new Date().toISOString().slice(0,10)}.xlsx`);
});
// CSV Export (서버 스트리밍)
document.getElementById('btn-fast-export-csv')?.addEventListener('click', async () => {
if (!fastCurrentSessionId) return;
const res = await fetch(`/api/fast/${fastCurrentSessionId}/csv`);
if (!res.ok) return;
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `fast-${fastCurrentSessionId}-${new Date().toISOString().slice(0,10)}.csv`;
a.click();
URL.revokeObjectURL(url);
});
// 탭 전환 시 세션 목록 갱신
document.querySelectorAll('[href="#pane-fast"]').forEach(a => {
a.addEventListener('show.bs.tab', () => fastSessionsLoad());
});
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `app.js` 파일을 다시 열어 추가된 함수 목록을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `fastSessionsLoad` 함수 존재
- [x] `fastStart` 함수 존재
- [x] `fastStop` 함수 존재
- [x] `fastDelete` 함수 존재
- [x] `fastSelect` 함수 존재
- [x] `fastRenderChart` 함수 — `new uPlot(opts, uData, container)` 3-인자 형식인가?
- [x] `fastRenderChart` 함수 — uPlot x축 데이터가 `Unix seconds`인가? (`/ 1000` 적용)
- [x] `btn-fast-export-xlsx` 핸들러 — `XLSX.utils.aoa_to_sheet(rows)` 사용하는가?
- [x] `btn-fast-export-xlsx` 핸들러 — `rows`가 배열의 배열(`string[][]`)인가?
- [x] `fastLivePollStart` — 2초(2000ms) 간격인가?
- [x] `tagNames` 변수명이 기존 코드와 일치하는가? (다르면 수정)
3. 브라우저에서 테스트:
- [x] `09 fastRecord` 탭 클릭 → 세션 목록 API 호출되는가?
- [x] `신규 세션` 버튼 → 모달 열리고 태그 목록 표시되는가?
- [x] 콘솔 에러가 없는가?
---
## 완료 조건
- [x] 브라우저 콘솔 에러 없음
- [x] `fastSessionsLoad()` 호출 시 API `/api/fast/sessions` 응답 정상
- [x] `new uPlot(opts, uData, container)` 3-인자 형식 사용

View File

@@ -1,65 +0,0 @@
# STEP 2 — DbContext: DbSet + 인덱스 추가 (`ExperionDbContext.cs`)
## 사전 확인 (작업 전 반드시 수행)
1. `src/Infrastructure/Database/ExperionDbContext.cs` 파일을 열어 전체 내용을 읽는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 1이 완료되어 `FastSession`, `FastRecord` 클래스가 존재하는가? → 미완료면 STEP 1 먼저 수행
- [x] `FastSessions` DbSet이 이미 선언되어 있는가? → 있으면 작업 1 건너뜀
- [x] `FastRecords` DbSet이 이미 선언되어 있는가? → 있으면 작업 1 건너뜀
- [x] `OnModelCreating``FastSession` 인덱스 설정이 이미 있는가? → 있으면 작업 2 건너뜀
- [x] 기존 DbSet 선언 위치(줄 번호)를 확인한다
- [x] `OnModelCreating` 메서드의 끝 위치(줄 번호)를 확인한다
---
## 작업 1 — DbSet 추가
**위치**: 기존 DbSet 선언 블록 마지막 줄 바로 아래
```csharp
public DbSet<FastSession> FastSessions => Set<FastSession>();
public DbSet<FastRecord> FastRecords => Set<FastRecord>();
```
---
## 작업 2 — OnModelCreating 인덱스 추가
**위치**: `OnModelCreating` 메서드 내부, 기존 마지막 설정 블록 아래
```csharp
modelBuilder.Entity<FastSession>(e =>
{
e.HasKey(x => x.Id);
e.HasIndex(x => x.Status);
e.HasIndex(x => x.StartedAt);
});
modelBuilder.Entity<FastRecord>(e =>
{
e.HasKey(x => x.Id);
e.HasIndex(x => x.SessionId);
e.HasIndex(x => new { x.SessionId, x.TagName, x.RecordedAt });
});
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionDbContext.cs` 파일을 다시 열어 변경 내용을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `public DbSet<FastSession> FastSessions` 선언이 존재하는가?
- [x] `public DbSet<FastRecord> FastRecords` 선언이 존재하는가?
- [x] `modelBuilder.Entity<FastSession>` 블록이 `OnModelCreating` 안에 있는가?
- [x] `modelBuilder.Entity<FastRecord>` 블록이 `OnModelCreating` 안에 있는가?
- [x] `HasIndex(x => new { x.SessionId, x.TagName, x.RecordedAt })` 복합 인덱스가 있는가?
3. `dotnet build src/Infrastructure` 실행 → 에러/경고 0개 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Web/ExperionCrawler.csproj` 결과: 에러 0, 경고 0 (기존 경고 포함)
- DbSet 2개, 인덱스 설정 2블록 모두 존재

View File

@@ -1,77 +0,0 @@
# STEP 3 — DB 초기화: 테이블 생성 + TimescaleDB hypertable
## 사전 확인 (작업 전 반드시 수행)
1. `src/Infrastructure/Database/ExperionDbContext.cs` 파일을 열어 `InitializeAsync()` 메서드를 찾는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 2가 완료되어 DbSet이 존재하는가? → 미완료면 STEP 2 먼저 수행
- [x] `InitializeAsync()` 메서드가 존재하는가? (없으면 구현 위치를 확인)
- [x] `CREATE TABLE IF NOT EXISTS fast_session` SQL이 이미 있는가? → 있으면 이 STEP 건너뜀
- [x] `create_hypertable('fast_record'...)` SQL이 이미 있는가? → 있으면 이 STEP 건너뜀
- [x] 파일 상단에 `using System.Text.Json;` import가 있는가? → 없으면 추가
---
## 작업 내용
**파일**: `src/Infrastructure/Database/ExperionDbContext.cs`
**위치**: `ExperionDbService.InitializeAsync()` 메서드 내부, 기존 초기화 코드 마지막 줄 아래
```csharp
// ── fast_session / fast_record 테이블 생성 ────────────────────────────────
await _ctx.Database.ExecuteSqlRawAsync("""
CREATE TABLE IF NOT EXISTS fast_session (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
started_at TIMESTAMPTZ NOT NULL,
ended_at TIMESTAMPTZ,
status TEXT NOT NULL DEFAULT 'Pending',
sampling_ms INTEGER NOT NULL,
duration_sec INTEGER NOT NULL,
tag_list JSONB NOT NULL DEFAULT '[]',
row_count INTEGER NOT NULL DEFAULT 0,
retention_days INTEGER,
pinned BOOLEAN NOT NULL DEFAULT FALSE
)
""");
await _ctx.Database.ExecuteSqlRawAsync("""
CREATE TABLE IF NOT EXISTS fast_record (
id SERIAL PRIMARY KEY,
session_id INTEGER NOT NULL REFERENCES fast_session(id) ON DELETE CASCADE,
recorded_at TIMESTAMPTZ NOT NULL,
tagname TEXT NOT NULL,
value TEXT
)
""");
// TimescaleDB hypertable 생성 (recorded_at 기준, chunk_interval = 1 day)
await _ctx.Database.ExecuteSqlRawAsync("""
SELECT create_hypertable('fast_record', 'recorded_at', if_not_exists => TRUE)
""");
await _ctx.Database.ExecuteSqlRawAsync("""
SELECT set_chunk_time_interval('fast_record', INTERVAL '1 day')
""");
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionDbContext.cs` 파일을 다시 열어 추가된 SQL 블록을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `CREATE TABLE IF NOT EXISTS fast_session` SQL이 존재하는가?
- [x] `CREATE TABLE IF NOT EXISTS fast_record` SQL이 존재하는가?
- [x] `fast_record``REFERENCES fast_session(id) ON DELETE CASCADE` 가 있는가?
- [x] `create_hypertable('fast_record', 'recorded_at', if_not_exists => TRUE)` 가 있는가?
- [x] `set_chunk_time_interval('fast_record', INTERVAL '1 day')` 가 있는가?
3. `dotnet build src/Infrastructure` 실행 → 에러/경고 0개 확인
4. (가능하면) 앱을 실제로 실행해 DB에 `fast_session`, `fast_record` 테이블이 생성되는지 확인
---
## 완료 조건
- `dotnet build src/Infrastructure` 결과: 에러 0, 경고 0 (기존 경고 포함)
- SQL 5개 블록 모두 `InitializeAsync` 내에 존재
- (선택) DB에서 `\d fast_record` 실행 시 hypertable 확인

View File

@@ -1,105 +0,0 @@
# STEP 4 — 인터페이스: DTO + IExperionFastService 추가
## 사전 확인 (작업 전 반드시 수행)
1. `src/Core/Application/Interfaces/IExperionServices.cs` 파일을 열어 전체 내용을 읽는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 1이 완료되어 `FastSession`, `FastRecord` 클래스가 존재하는가?
- [x] `IExperionFastService` 인터페이스가 이미 존재하는가? → 있으면 이 STEP 건너뜀
- [x] `FastSessionInfo` record가 이미 존재하는가?
- [x] `FastSessionStartRequest` record가 이미 존재하는가?
- [x] `FastSessionCreateRequest` record가 이미 존재하는가?
- [x] `FastQueryResult` record가 이미 존재하는가?
---
## 작업 내용
**파일**: `src/Core/Application/Interfaces/IExperionServices.cs`
파일 하단에 아래 내용을 추가한다.
### DTO Records
```csharp
// ── fastTable DTOs ────────────────────────────────────────────────────────────
public record FastSessionInfo(
int Id,
string Name,
DateTime StartedAt,
DateTime? EndedAt,
string Status,
int SamplingMs,
int DurationSec,
string[] TagList,
int RowCount,
int? RetentionDays,
bool Pinned
);
public record FastSessionStartRequest(
string Name,
int SamplingMs,
int DurationSec,
string[] TagList,
int? RetentionDays = null
);
public record FastSessionCreateRequest(
string Name,
int SamplingMs,
int DurationSec,
string[] TagList,
int? RetentionDays = null
);
public record FastQueryResult(
int SessionId,
DateTime From,
DateTime To,
string[] TagNames,
IEnumerable<FastRecord> Items,
int TotalCount
);
public record PinRequest(bool Pinned);
```
### IExperionFastService 인터페이스
```csharp
public interface IExperionFastService
{
Task<FastSessionInfo> StartSessionAsync(FastSessionStartRequest request);
Task StopSessionAsync(int sessionId);
Task DeleteSessionAsync(int sessionId);
Task PinSessionAsync(int sessionId, bool pinned);
Task<FastSessionInfo?> GetSessionAsync(int sessionId);
Task<IEnumerable<FastSessionInfo>> GetSessionsAsync();
Task<FastQueryResult> GetRecordsAsync(int sessionId, DateTime? from, DateTime? to, string format = "long");
Task ExportCsvAsync(int sessionId, Stream stream, DateTime? from = null, DateTime? to = null);
}
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `IExperionServices.cs` 파일을 다시 열어 추가된 내용을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `FastSessionInfo` record 존재 (11개 필드)
- [x] `FastSessionStartRequest` record 존재
- [x] `FastSessionCreateRequest` record 존재 (StartRequest와 별도로)
- [x] `FastQueryResult` record 존재
- [x] `PinRequest` record 존재
- [x] `IExperionFastService` 인터페이스 존재 (메서드 8개)
- [x] `ExportCsvAsync``Stream` 파라미터가 올바른가?
3. `dotnet build src/Web` 실행 → 에러 0개, 경고는 기존 9개 (TextToSqlController, ExperionOpcClient, ExperionRealtimeService) 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 0, 경고 9개 (기존)
- DTO 5종 (`FastSessionInfo`, `FastSessionStartRequest`, `FastSessionCreateRequest`, `FastQueryResult`, `PinRequest`), 인터페이스 1개 (`IExperionFastService`) 모두 존재

View File

@@ -1,78 +0,0 @@
# STEP 5 — 인터페이스: IExperionOpcClient + IExperionDbService 확장
## 사전 확인 (작업 전 반드시 수행)
1. `src/Core/Application/Interfaces/IExperionOpcClient.cs` 파일을 열어 전체 내용을 읽는다.
2. `src/Core/Application/Interfaces/IExperionDbService.cs` 파일을 열어 전체 내용을 읽는다.
3. 아래 항목을 확인하고 기록한다:
- [x] STEP 4가 완료되어 DTO들이 존재하는가?
- [x] `IExperionOpcClient``IsConnectedAsync` 메서드가 이미 있는가?
- [x] `IExperionOpcClient``CreateSessionAsync` 메서드가 이미 있는가?
- [x] `IExperionDbService``CreateFastSessionAsync` 메서드가 이미 있는가?
- [x] `IExperionDbService``BatchInsertFastRecordsAsync` 메서드가 이미 있는가?
- [x] `IExperionDbService``GetExpiredFastSessionsAsync` 메서드가 이미 있는가?
---
## 작업 1 — IExperionOpcClient 확장
**파일**: `src/Core/Application/Interfaces/IExperionOpcClient.cs`
인터페이스에 아래 두 메서드를 **추가**한다:
```csharp
// fastTable용 메서드
Task<bool> IsConnectedAsync(ApplicationConfiguration cfg);
Task<ISession> CreateSessionAsync(ApplicationConfiguration cfg);
```
---
## 작업 2 — IExperionDbService 확장
**파일**: `src/Core/Application/Interfaces/IExperionDbService.cs`
인터페이스에 아래 메서드들을 **추가**한다:
```csharp
// ── FastSession ───────────────────────────────────────────────────────────────
Task<FastSession> CreateFastSessionAsync(FastSessionCreateRequest request);
Task UpdateFastSessionStatusAsync(int sessionId, string status);
Task UpdateFastSessionRowCountAsync(int sessionId, int rowCount);
Task UpdateFastSessionPinnedAsync(int sessionId, bool pinned);
Task<FastSession?> GetFastSessionAsync(int sessionId);
Task<IEnumerable<FastSession>> GetFastSessionsAsync();
Task DeleteFastSessionAsync(int sessionId);
Task<IEnumerable<FastSession>> GetExpiredFastSessionsAsync();
// ── FastRecord ────────────────────────────────────────────────────────────────
Task<FastQueryResult> GetFastRecordsAsync(int sessionId, DateTime? from, DateTime? to);
Task BatchInsertFastRecordsAsync(IEnumerable<FastRecord> records);
Task ExportFastRecordsToCsvAsync(int sessionId, Stream stream, DateTime? from, DateTime? to);
// ── 공통 (이미 없는 경우만) ──────────────────────────────────────────────────
Task<string?> GetNodeIdByTagNameAsync(string tagName);
```
---
## 사후 확인 (작업 후 반드시 수행)
1. 두 파일을 다시 열어 변경 내용을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `IExperionOpcClient``IsConnectedAsync(ApplicationConfiguration cfg)` 존재
- [x] `IExperionOpcClient``CreateSessionAsync(ApplicationConfiguration cfg)` 존재
- [x] `IExperionDbService`에 FastSession 관련 메서드 8개 모두 존재
- [x] `IExperionDbService`에 FastRecord 관련 메서드 3개 모두 존재
- [x] `IExperionDbService``GetNodeIdByTagNameAsync` 존재
- [x] 반환 타입이 올바른가? (`Task<FastSession?>` nullable 포함)
3. `dotnet build src/Web` 실행 → 에러 14개 (구현체 미완료, STEP 6~7에서 해결)
4. 구현체 빌드 에러는 예상된 결과 (인터페이스만 추가한 단계)
> ⚠️ 주의: 인터페이스만 추가하는 단계이므로 구현체 빌드 에러는 STEP 6~7에서 해결한다.
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 14개 (구현체 미완료, STEP 6~7에서 해결)
- 두 인터페이스에 지정된 메서드 시그니처 모두 존재

View File

@@ -1,173 +0,0 @@
# STEP 6 — DB 서비스 구현: FastSession/FastRecord 메서드 추가
## 사전 확인 (작업 전 반드시 수행)
1. `src/Infrastructure/Database/ExperionDbContext.cs` 파일을 열어 `ExperionDbService` 클래스를 찾는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 5가 완료되어 `IExperionDbService`에 Fast 메서드가 선언되어 있는가?
- [x] `ExperionDbService``IExperionDbService`를 구현하는가?
- [x] 파일 상단에 `using System.Text.Json;` import가 있는가? → 없으면 추가
- [x] `CreateFastSessionAsync` 구현이 이미 있는가? → 있으면 해당 메서드 건너뜀
- [x] `BatchInsertFastRecordsAsync` 구현이 이미 있는가?
- [x] `ExportFastRecordsToCsvAsync` 구현이 이미 있는가?
---
## 작업 내용
**파일**: `src/Infrastructure/Database/ExperionDbContext.cs`
**위치**: `ExperionDbService` 클래스 내부 마지막 메서드 아래
```csharp
// ── FastSession / FastRecord ─────────────────────────────────────────────────
public async Task<FastSession> CreateFastSessionAsync(FastSessionCreateRequest request)
{
var session = new FastSession
{
Name = request.Name,
SamplingMs = request.SamplingMs,
DurationSec = request.DurationSec,
TagList = JsonSerializer.Serialize(request.TagList), // string[] → JSONB
StartedAt = DateTime.UtcNow,
Status = "Pending",
RowCount = 0,
RetentionDays = request.RetentionDays,
Pinned = false
};
_ctx.FastSessions.Add(session);
await _ctx.SaveChangesAsync();
return session;
}
public async Task UpdateFastSessionStatusAsync(int sessionId, string status)
{
var session = await _ctx.FastSessions.FindAsync(sessionId);
if (session == null) return;
session.Status = status;
if (status is "Completed" or "Cancelled" or "Failed" or "RowLimitReached")
session.EndedAt = DateTime.UtcNow;
await _ctx.SaveChangesAsync();
}
public async Task UpdateFastSessionRowCountAsync(int sessionId, int rowCount)
{
var session = await _ctx.FastSessions.FindAsync(sessionId);
if (session == null) return;
session.RowCount = rowCount;
await _ctx.SaveChangesAsync();
}
public async Task UpdateFastSessionPinnedAsync(int sessionId, bool pinned)
{
var session = await _ctx.FastSessions.FindAsync(sessionId);
if (session == null) return;
session.Pinned = pinned;
await _ctx.SaveChangesAsync();
}
public async Task<FastSession?> GetFastSessionAsync(int sessionId)
=> await _ctx.FastSessions.FindAsync(sessionId);
public async Task<IEnumerable<FastSession>> GetFastSessionsAsync()
=> await _ctx.FastSessions.OrderBy(x => x.StartedAt).ToListAsync();
public async Task DeleteFastSessionAsync(int sessionId)
{
var session = await _ctx.FastSessions.FindAsync(sessionId);
if (session == null) return;
_ctx.FastSessions.Remove(session);
await _ctx.SaveChangesAsync();
}
public async Task<IEnumerable<FastSession>> GetExpiredFastSessionsAsync()
{
var now = DateTime.UtcNow;
return await _ctx.FastSessions
.Where(x => x.EndedAt != null
&& !x.Pinned
&& x.RetentionDays.HasValue
&& x.EndedAt.Value.AddDays(x.RetentionDays.Value) < now)
.OrderBy(x => x.EndedAt)
.ToListAsync();
}
public async Task<FastQueryResult> GetFastRecordsAsync(int sessionId, DateTime? from, DateTime? to)
{
var query = _ctx.FastRecords.Where(x => x.SessionId == sessionId);
if (from.HasValue) query = query.Where(x => x.RecordedAt >= from.Value);
if (to.HasValue) query = query.Where(x => x.RecordedAt <= to.Value);
var records = await query.OrderBy(x => x.RecordedAt).ToListAsync();
var tagNames = records.Select(x => x.TagName).Distinct().OrderBy(x => x).ToArray();
return new FastQueryResult(
SessionId: sessionId,
From: from ?? records.MinBy(x => x.RecordedAt)?.RecordedAt ?? DateTime.UtcNow,
To: to ?? records.MaxBy(x => x.RecordedAt)?.RecordedAt ?? DateTime.UtcNow,
TagNames: tagNames,
Items: records,
TotalCount: records.Count
);
}
public async Task BatchInsertFastRecordsAsync(IEnumerable<FastRecord> records)
{
var list = records.ToList();
if (list.Count == 0) return;
_ctx.FastRecords.AddRange(list);
await _ctx.SaveChangesAsync();
}
public async Task ExportFastRecordsToCsvAsync(int sessionId, Stream stream, DateTime? from, DateTime? to)
{
var result = await GetFastRecordsAsync(sessionId, from, to);
using var writer = new StreamWriter(stream, leaveOpen: true);
var header = "recorded_at," + string.Join(",", result.TagNames.Select(t => $"\"{t}\""));
await writer.WriteLineAsync(header);
var grouped = result.Items
.GroupBy(x => x.RecordedAt)
.OrderBy(x => x.Key)
.Select(g => new { Time = g.Key, Values = g.ToDictionary(r => r.TagName, r => r.Value) });
foreach (var g in grouped)
{
var row = g.Time.ToString("o") + ","
+ string.Join(",", result.TagNames.Select(t =>
g.Values.TryGetValue(t, out var v) ? $"\"{v}\"" : ""));
await writer.WriteLineAsync(row);
}
await writer.FlushAsync();
}
public async Task<string?> GetNodeIdByTagNameAsync(string tagName)
=> await _ctx.RealtimePoints
.Where(x => x.TagName == tagName)
.Select(x => x.NodeId)
.FirstOrDefaultAsync();
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionDbContext.cs` 파일을 다시 열어 추가된 메서드 목록을 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `CreateFastSessionAsync``JsonSerializer.Serialize(request.TagList)` 사용하는가?
- [x] `UpdateFastSessionStatusAsync``EndedAt` 자동 설정 로직이 있는가?
- [x] `GetExpiredFastSessionsAsync``!x.Pinned` 조건이 있는가?
- [x] `GetFastRecordsAsync` — 반환 타입이 `FastQueryResult`인가?
- [x] `BatchInsertFastRecordsAsync` — 빈 리스트 early return이 있는가?
- [x] `ExportFastRecordsToCsvAsync` — PIVOT 그룹핑 로직이 있는가?
- [x] `GetNodeIdByTagNameAsync``_ctx.RealtimePoints` 에서 조회하는가?
- [x] 파일 상단에 `using System.Text.Json;` 가 있는가?
3. `dotnet build src/Web` 실행 → 에러 2개 (ExperionOpcClient 구현 미완료, STEP 7에서 해결)
4. ExperionOpcClient 구현 에러는 예상된 결과 (인터페이스만 추가한 단계)
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 2개 (ExperionOpcClient 구현 미완료, STEP 7에서 해결)
- Fast 관련 DB 메서드 12개 모두 구현됨

View File

@@ -1,400 +0,0 @@
# STEP 7 — ExperionFastService 신규 파일 생성
## 사전 확인 (작업 전 반드시 수행)
1. `src/Infrastructure/OpcUa/` 디렉토리 목록을 확인한다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 5, 6이 완료되어 인터페이스와 DB 메서드가 존재하는가?
- [x] `ExperionFastService.cs` 파일이 이미 존재하는가? → 존재하면 내용 비교 후 필요한 부분만 수정 (신규 생성)
- [x] `IExperionOpcClient``IsConnectedAsync`, `CreateSessionAsync`가 구현되어 있는가?
- [x] `IOpcUaConfigProvider` 인터페이스가 존재하는가? (주입 경로 확인)
- [x] `Opc.Ua.Client` NuGet 패키지가 Infrastructure 프로젝트에 있는가?
---
## 작업 내용
**파일**: `src/Infrastructure/OpcUa/ExperionFastService.cs` (신규 생성)
```csharp
using System.Collections.Concurrent;
using System.Text.Json;
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Core.Domain.Entities;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Opc.Ua;
using Opc.Ua.Client;
namespace ExperionCrawler.Infrastructure.OpcUa;
/// <summary>
/// fastRecord 데이터 수집 서비스.
/// 세션별 별도 OPC UA Subscription을 관리하고, 2초마다 배치 INSERT.
/// IHostedService로 등록하여 앱 시작/종료 시 자동 관리.
/// </summary>
public class ExperionFastService : IExperionFastService, IHostedService, IDisposable
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ILogger<ExperionFastService> _logger;
private readonly IOpcUaConfigProvider _configProvider;
private readonly IExperionOpcClient _opcClient;
private readonly ConcurrentDictionary<int, FastSessionContext> _sessions = new();
private CancellationTokenSource? _cts;
private Task? _monitorTask;
private const int MaxConcurrentSessions = 3;
private const int MaxRowsPerSession = 5_000_000;
private const int FlushIntervalMs = 2_000;
public ExperionFastService(
IServiceScopeFactory scopeFactory,
ILogger<ExperionFastService> logger,
IOpcUaConfigProvider configProvider,
IExperionOpcClient opcClient)
{
_scopeFactory = scopeFactory;
_logger = logger;
_configProvider = configProvider;
_opcClient = opcClient;
}
// ── IHostedService ────────────────────────────────────────────────────────
public async Task StartAsync(CancellationToken cancellationToken)
{
// 앱 시작 시 Running 상태 세션 → Failed 마킹
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
var sessions = await db.GetFastSessionsAsync();
foreach (var s in sessions.Where(s => s.Status == "Running"))
{
_logger.LogWarning("[Fast] 앱 시작 시 Running 세션 {Id} → Failed 마킹", s.Id);
await db.UpdateFastSessionStatusAsync(s.Id, "Failed");
}
_cts = new CancellationTokenSource();
_monitorTask = Task.Run(() => MonitorLoopAsync(_cts.Token), _cts.Token);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_cts?.Cancel();
if (_monitorTask != null)
await _monitorTask.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
foreach (var kvp in _sessions)
kvp.Value.Cancel = true;
await Task.Delay(2000).ConfigureAwait(false); // 마지막 flush 대기
}
public void Dispose()
{
_cts?.Dispose();
}
// ── IExperionFastService ──────────────────────────────────────────────────
public async Task<FastSessionInfo> StartSessionAsync(FastSessionStartRequest request)
{
if (request.TagList.Length == 0 || request.TagList.Length > 8)
throw new ArgumentException("태그는 1~8개까지 가능합니다.");
if (request.SamplingMs is not (100 or 250 or 500 or 1000))
throw new ArgumentException("샘플링 간격은 100/250/500/1000ms 중 하나여야 합니다.");
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
var runningCount = (await db.GetFastSessionsAsync()).Count(s => s.Status == "Running");
if (runningCount >= MaxConcurrentSessions)
throw new InvalidOperationException($"동시 실행 가능한 세션은 {MaxConcurrentSessions}개까지입니다.");
var cfg = await _configProvider.GetConfigAsync(new ExperionServerConfig());
if (string.IsNullOrEmpty(cfg?.EndpointUrl))
throw new InvalidOperationException("서버 엔드포인트 URL이 설정되어 있지 않습니다.");
if (!await _opcClient.IsConnectedAsync(cfg))
throw new InvalidOperationException("OPC UA 서버에 연결되어 있지 않습니다.");
// 노드 유효성 사전 검증
foreach (var tagName in request.TagList)
{
var nodeId = await db.GetNodeIdByTagNameAsync(tagName);
if (string.IsNullOrEmpty(nodeId))
throw new ArgumentException($"태그 '{tagName}'의 nodeId를 찾을 수 없습니다.");
var readResult = await _opcClient.ReadTagAsync(cfg, nodeId);
if (!readResult.Success)
throw new ArgumentException($"태그 '{tagName}' 읽기 실패: {readResult.ErrorMessage}");
}
var session = await db.CreateFastSessionAsync(new FastSessionCreateRequest(
Name: request.Name,
SamplingMs: request.SamplingMs,
DurationSec: request.DurationSec,
TagList: request.TagList,
RetentionDays: request.RetentionDays));
var ctx = new FastSessionContext
{
SessionId = session.Id,
TagList = request.TagList,
SamplingMs = request.SamplingMs,
DurationSec = request.DurationSec,
StartedAt = DateTime.UtcNow,
Buffer = new ConcurrentQueue<FastRecord>()
};
_sessions[session.Id] = ctx;
await StartSubscriptionAsync(ctx, cfg);
await db.UpdateFastSessionStatusAsync(session.Id, "Running");
_logger.LogInformation("[Fast] 세션 {Id} 시작 — 태그 {Count}개, {Ms}ms, {Sec}s",
session.Id, request.TagList.Length, request.SamplingMs, request.DurationSec);
return MapToInfo(session);
}
public async Task StopSessionAsync(int sessionId)
{
if (!_sessions.TryGetValue(sessionId, out var ctx))
throw new InvalidOperationException($"세션 {sessionId}를 찾을 수 없습니다.");
ctx.Cancel = true;
await FlushBufferAsync(ctx).ConfigureAwait(false);
await StopSubscriptionAsync(ctx).ConfigureAwait(false);
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
await db.UpdateFastSessionStatusAsync(sessionId, "Completed");
await db.UpdateFastSessionRowCountAsync(sessionId, ctx.TotalRows);
_sessions.TryRemove(sessionId, out _);
_logger.LogInformation("[Fast] 세션 {Id} 중지 — 총 {Count}행", sessionId, ctx.TotalRows);
}
public async Task DeleteSessionAsync(int sessionId)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
await db.DeleteFastSessionAsync(sessionId);
_sessions.TryRemove(sessionId, out _);
}
public async Task PinSessionAsync(int sessionId, bool pinned)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
await db.UpdateFastSessionPinnedAsync(sessionId, pinned);
}
public async Task<FastSessionInfo?> GetSessionAsync(int sessionId)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
var session = await db.GetFastSessionAsync(sessionId);
return session == null ? null : MapToInfo(session);
}
public async Task<IEnumerable<FastSessionInfo>> GetSessionsAsync()
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
return (await db.GetFastSessionsAsync()).Select(MapToInfo);
}
public async Task<FastQueryResult> GetRecordsAsync(int sessionId, DateTime? from, DateTime? to, string format = "long")
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
return await db.GetFastRecordsAsync(sessionId, from, to);
}
public async Task ExportCsvAsync(int sessionId, Stream stream, DateTime? from = null, DateTime? to = null)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
await db.ExportFastRecordsToCsvAsync(sessionId, stream, from, to);
}
// ── Private ────────────────────────────────────────────────────────────────
private async Task StartSubscriptionAsync(FastSessionContext ctx, ApplicationConfiguration cfg)
{
var session = await _opcClient.CreateSessionAsync(cfg);
var subscription = new Subscription(session.DefaultSubscription)
{
PublishingInterval = ctx.SamplingMs,
KeepAliveCount = 10
};
foreach (var tagName in ctx.TagList)
{
var nodeId = await GetNodeIdAsync(tagName);
var item = new MonitoredItem(subscription)
{
StartNodeId = nodeId,
SamplingInterval = ctx.SamplingMs,
DisplayName = tagName
};
item.Notification += (_, e) => OnNotification(ctx, e, tagName);
subscription.AddItem(item);
}
await session.AddSubscriptionAsync(subscription);
subscription.Create();
ctx.Subscription = subscription;
ctx.Session = session;
}
private async Task StopSubscriptionAsync(FastSessionContext ctx)
{
if (ctx.Subscription != null)
{
ctx.Subscription.Delete(false);
ctx.Subscription = null;
}
if (ctx.Session != null)
{
await ctx.Session.CloseAsync();
await ctx.Session.DisposeAsync();
ctx.Session = null;
}
}
private void OnNotification(FastSessionContext ctx, MonitoredItemNotificationEventArgs e, string tagName)
{
if (ctx.Cancel) return;
if (e.NotificationValue is MonitoredItemNotification notification)
{
ctx.Buffer.Enqueue(new FastRecord
{
SessionId = ctx.SessionId,
RecordedAt = DateTime.UtcNow,
TagName = tagName,
Value = notification.Value.Value?.ToString()
});
ctx.TotalRows++;
}
}
private async Task FlushBufferAsync(FastSessionContext ctx)
{
var buffer = new List<FastRecord>();
while (ctx.Buffer.TryDequeue(out var record))
buffer.Add(record);
if (buffer.Count == 0) return;
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
await db.BatchInsertFastRecordsAsync(buffer);
await db.UpdateFastSessionRowCountAsync(ctx.SessionId, ctx.TotalRows);
if (ctx.TotalRows >= MaxRowsPerSession)
{
ctx.Cancel = true;
await db.UpdateFastSessionStatusAsync(ctx.SessionId, "RowLimitReached");
_sessions.TryRemove(ctx.SessionId, out _);
_logger.LogWarning("[Fast] 세션 {Id} RowLimitReached ({Max}행)", ctx.SessionId, MaxRowsPerSession);
}
}
private async Task MonitorLoopAsync(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
{
try
{
await Task.Delay(FlushIntervalMs, ct);
foreach (var kvp in _sessions.ToList())
{
var ctx = kvp.Value;
if (ctx.Cancel) continue;
if ((DateTime.UtcNow - ctx.StartedAt).TotalSeconds >= ctx.DurationSec)
{
ctx.Cancel = true;
await StopSessionAsync(ctx.SessionId);
continue;
}
await FlushBufferAsync(ctx);
}
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
_logger.LogError(ex, "[Fast] 모니터링 루프 오류");
}
}
}
private async Task<string> GetNodeIdAsync(string tagName)
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IExperionDbService>();
return await db.GetNodeIdByTagNameAsync(tagName) ?? string.Empty;
}
private static FastSessionInfo MapToInfo(FastSession s) => new(
Id: s.Id,
Name: s.Name,
StartedAt: s.StartedAt,
EndedAt: s.EndedAt,
Status: s.Status,
SamplingMs: s.SamplingMs,
DurationSec: s.DurationSec,
TagList: JsonSerializer.Deserialize<string[]>(s.TagList) ?? [],
RowCount: s.RowCount,
RetentionDays: s.RetentionDays,
Pinned: s.Pinned);
// ── Inner Class ────────────────────────────────────────────────────────────
private sealed class FastSessionContext
{
public int SessionId { get; set; }
public string[] TagList { get; set; } = [];
public int SamplingMs { get; set; }
public int DurationSec { get; set; }
public DateTime StartedAt { get; set; }
public ConcurrentQueue<FastRecord> Buffer { get; set; } = new();
public int TotalRows { get; set; } // 누적 행 수
public bool Cancel { get; set; }
public ISession? Session { get; set; }
public Subscription? Subscription { get; set; }
}
}
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionFastService.cs` 파일을 읽어 전체 구조를 확인한다.
2. 아래 항목을 하나씩 확인한다:
- [x] 클래스가 `IExperionFastService`, `IHostedService`, `IDisposable` 모두 구현하는가?
- [x] `StartAsync` — Running 세션 Failed 마킹 로직이 있는가?
- [x] `OnNotification``MonitoredItemNotification` 타입 체크를 하는가? (`e.NotificationValue is MonitoredItemNotification`)
- [x] `FlushBufferAsync``ctx.TotalRows >= MaxRowsPerSession` 체크가 있는가?
- [x] `MapToInfo``JsonSerializer.Deserialize<string[]>` 사용하는가?
- [x] `FastSessionContext.TotalRows` 필드가 있는가?
- [x] 파일 상단에 `using System.Text.Json;` 이 있는가?
- [x] `using Opc.Ua;`, `using Opc.Ua.Client;` 가 있는가?
3. `dotnet build src/Web` 실행 → 에러 0, 경고 14개 (기존 경고 포함) 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 0, 경고 14개 (기존 경고 포함)
- `ExperionFastService.cs` 파일 존재 및 빌드 통과

View File

@@ -1,191 +0,0 @@
# STEP 8 — 컨트롤러 추가 (`ExperionFastController`)
## 사전 확인 (작업 전 반드시 수행)
1. `src/Web/Controllers/ExperionControllers.cs` 파일을 열어 전체 내용을 읽는다.
2. 아래 항목을 확인하고 기록한다:
- [x] STEP 7이 완료되어 `ExperionFastService`가 빌드되는가?
- [x] `ExperionFastController` 클래스가 이미 존재하는가? → 있으면 내용 비교 후 누락 엔드포인트만 추가
- [x] `PinRequest` record가 이미 존재하는가?
- [x] 파일 상단에 `using Microsoft.AspNetCore.Mvc;` 가 있는가?
---
## 작업 내용
**파일**: `src/Web/Controllers/ExperionControllers.cs`
**위치**: 파일 하단 (기존 컨트롤러 마지막 클래스 아래)
```csharp
// ── FastTable / FastRecord ────────────────────────────────────────────────────
[ApiController]
[Route("api/fast")]
public class ExperionFastController : ControllerBase
{
private readonly IExperionFastService _fastSvc;
public ExperionFastController(IExperionFastService fastSvc)
=> _fastSvc = fastSvc;
/// <summary>새 fastSession 시작</summary>
[HttpPost("start")]
public async Task<IActionResult> Start([FromBody] FastSessionStartRequest request)
{
try
{
var session = await _fastSvc.StartSessionAsync(request);
return Ok(new { id = session.Id, name = session.Name, status = session.Status, startedAt = session.StartedAt });
}
catch (ArgumentException ex) { return BadRequest(new { error = ex.Message }); }
catch (InvalidOperationException ex) { return Conflict(new { error = ex.Message }); }
}
/// <summary>세션 중지</summary>
[HttpPost("{id:int}/stop")]
public async Task<IActionResult> Stop(int id)
{
try
{
await _fastSvc.StopSessionAsync(id);
return Ok(new { success = true, message = "세션이 중지되었습니다." });
}
catch (InvalidOperationException ex) { return NotFound(new { error = ex.Message }); }
}
/// <summary>세션 목록 조회</summary>
[HttpGet("sessions")]
public async Task<IActionResult> GetSessions()
{
var sessions = await _fastSvc.GetSessionsAsync();
return Ok(new
{
total = sessions.Count(),
items = sessions.Select(s => new
{
id = s.Id,
name = s.Name,
status = s.Status,
samplingMs = s.SamplingMs,
durationSec = s.DurationSec,
tagCount = s.TagList.Length,
rowCount = s.RowCount,
startedAt = s.StartedAt,
endedAt = s.EndedAt,
retentionDays = s.RetentionDays,
pinned = s.Pinned
})
});
}
/// <summary>세션 상세 정보</summary>
[HttpGet("{id:int}")]
public async Task<IActionResult> GetSession(int id)
{
var session = await _fastSvc.GetSessionAsync(id);
if (session == null) return NotFound();
return Ok(new
{
id = session.Id,
name = session.Name,
status = session.Status,
samplingMs = session.SamplingMs,
durationSec = session.DurationSec,
tagList = session.TagList,
rowCount = session.RowCount,
startedAt = session.StartedAt,
endedAt = session.EndedAt,
retentionDays = session.RetentionDays,
pinned = session.Pinned
});
}
/// <summary>레코드 조회 (Long 포맷)</summary>
[HttpGet("{id:int}/records")]
public async Task<IActionResult> GetRecords(int id,
[FromQuery] DateTime? from,
[FromQuery] DateTime? to,
[FromQuery] string format = "long")
{
var result = await _fastSvc.GetRecordsAsync(id, from, to, format);
return Ok(new
{
sessionId = result.SessionId,
from = result.From,
to = result.To,
tagNames = result.TagNames,
total = result.TotalCount,
items = result.Items.Select(r => new
{
sessionId = r.SessionId,
recordedAt = r.RecordedAt,
tagName = r.TagName,
value = r.Value
})
});
}
/// <summary>CSV Export (스트리밍)</summary>
[HttpGet("{id:int}/csv")]
public async Task<IActionResult> ExportCsv(int id,
[FromQuery] DateTime? from,
[FromQuery] DateTime? to)
{
var ms = new MemoryStream();
await _fastSvc.ExportCsvAsync(id, ms, from, to);
ms.Position = 0;
return File(ms, "text/csv", $"fast-{id}-{DateTime.Now:yyyyMMddHHmm}.csv");
}
/// <summary>세션 삭제</summary>
[HttpDelete("{id:int}")]
public async Task<IActionResult> Delete(int id)
{
try
{
await _fastSvc.DeleteSessionAsync(id);
return Ok(new { success = true, message = "세션이 삭제되었습니다." });
}
catch (InvalidOperationException ex) { return NotFound(new { error = ex.Message }); }
}
/// <summary>세션 고정/해제</summary>
[HttpPost("{id:int}/pin")]
public async Task<IActionResult> Pin(int id, [FromBody] PinRequest request)
{
try
{
await _fastSvc.PinSessionAsync(id, request.Pinned);
return Ok(new { success = true, pinned = request.Pinned });
}
catch (InvalidOperationException ex) { return NotFound(new { error = ex.Message }); }
}
}
public record PinRequest(bool Pinned);
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `ExperionControllers.cs` 파일을 다시 열어 추가된 컨트롤러를 읽는다.
2. 아래 항목을 하나씩 확인한다:
- [x] `[Route("api/fast")]` 라우트가 맞는가?
- [x] `POST /start` 엔드포인트 존재
- [x] `POST /{id}/stop` 엔드포인트 존재
- [x] `GET /sessions` 엔드포인트 존재
- [x] `GET /{id}` 엔드포인트 존재
- [x] `GET /{id}/records` 엔드포인트 존재
- [x] `GET /{id}/csv` 엔드포인트 존재
- [x] `DELETE /{id}` 엔드포인트 존재
- [x] `POST /{id}/pin` 엔드포인트 존재 (총 8개 엔드포인트)
- [x] `PinRequest` record 존재 (중복 선언 아닌지 확인)
3. `dotnet build src/Web` 실행 → 에러/경고 0개 확인
4. 문제가 있으면 수정 후 다시 빌드 확인
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 0, 경고 0
- `ExperionFastController` 8개 엔드포인트 모두 존재

View File

@@ -1,79 +0,0 @@
# STEP 9 — DI 등록 (`Program.cs`) + 설정 (`appsettings.json`)
## 사전 확인 (작업 전 반드시 수행)
1. `src/Web/Program.cs` 파일을 열어 전체 내용을 읽는다.
2. `src/Web/appsettings.json` 파일을 열어 전체 내용을 읽는다.
3. 아래 항목을 확인하고 기록한다:
- [x] STEP 7이 완료되어 `ExperionFastService` 클래스가 존재하는가?
- [x] STEP 8이 완료되어 `ExperionFastController`가 존재하는가?
- [x] `Program.cs``ExperionFastService` DI 등록이 이미 있는가? → 있으면 작업 1 건너뜀
- [x] `Program.cs``ExperionFastCleanupService` 등록이 이미 있는가? → 있으면 작업 1 건너뜀
- [x] `appsettings.json``"Fast"` 섹션이 이미 있는가? → 있으면 작업 2 건너뜀
- [x] 기존 서비스 등록 위치(줄 번호)를 확인한다 (`builder.Services.Add...` 블록)
---
## 작업 1 — Program.cs 서비스 등록
**위치**: 기존 `builder.Services` 등록 블록 마지막 줄 아래
```csharp
// ── FastTable Service ─────────────────────────────────────────────────────────
// 중요: Singleton으로 하나만 생성 후 IExperionFastService와 IHostedService 양쪽에 같은 인스턴스 공유
builder.Services.AddSingleton<ExperionFastService>();
builder.Services.AddSingleton<IExperionFastService>(sp => sp.GetRequiredService<ExperionFastService>());
builder.Services.AddHostedService(sp => sp.GetRequiredService<ExperionFastService>());
// ── FastTable Cleanup Service ─────────────────────────────────────────────────
builder.Services.AddHostedService<ExperionFastCleanupService>();
```
---
## 작업 2 — appsettings.json 설정 추가
**위치**: `appsettings.json` 최상위 JSON 객체 안, 마지막 속성 뒤
```json
"Fast": {
"MaxConcurrentSessions": 3,
"MaxRowsPerSession": 5000000,
"FlushIntervalMs": 2000
}
```
---
## 사후 확인 (작업 후 반드시 수행)
1. `Program.cs` 파일을 다시 열어 변경 내용을 읽는다.
2. `appsettings.json` 파일을 다시 열어 변경 내용을 읽는다.
3. 아래 항목을 하나씩 확인한다:
- [x] `AddSingleton<ExperionFastService>()` 등록이 있는가?
- [x] `AddSingleton<IExperionFastService>(sp => ...)` 등록이 있는가?
- [x] `AddHostedService(sp => sp.GetRequiredService<ExperionFastService>())` 등록이 있는가?
- [x] 위 3줄이 올바른 순서인가? (Singleton 먼저, HostedService 마지막)
- [x] `AddHostedService<ExperionFastCleanupService>()` 등록이 있는가?
- [x] `appsettings.json``"Fast"` 섹션이 있고 JSON 형식이 올바른가?
4. `dotnet build src/Web` 실행 → 에러/경고 0개 확인
5. 문제가 있으면 수정 후 다시 빌드 확인
> ⚠️ 주의: `AddHostedService<ExperionFastService>()` 단독 사용 금지.
> 이렇게 하면 Singleton과 별개의 인스턴스가 생성되어 세션 상태가 공유되지 않음.
---
## 완료 조건
- `dotnet build src/Web` 결과: 에러 0, 경고 0
- DI 등록 3줄 + Cleanup 등록 1줄 모두 존재
- `appsettings.json``"Fast"` 섹션 존재
## 완료 일시: 2026-04-29
- [x] `AddSingleton<ExperionFastService>()` 등록 완료
- [x] `AddSingleton<IExperionFastService>(sp => ...)` 등록 완료
- [x] `AddHostedService(sp => sp.GetRequiredService<ExperionFastService>())` 등록 완료
- [x] `AddHostedService<ExperionFastCleanupService>()` 등록 완료
- [x] `appsettings.json``"Fast"` 섹션 추가 완료
- [x] 빌드 검증 완료 (0 Error, 0 New Warning)

View File

@@ -1,205 +0,0 @@
✔ 🎯 End-to-End P&ID Graph Pipeline (실전 구조)
┌──────────────────────┐
│ P&ID PDF Input │
└─────────┬────────────┘
┌─────────────────────────────────┐
│ 1. Document Parsing Layer │
│ (layout + text + tables) │
└─────────┬──────────────────────┘
┌─────────────────────────────────┐
│ 2. Spatial Element Extraction │
│ (symbols + coordinates) │
└─────────┬──────────────────────┘
┌─────────────────────────────────┐
│ 3. Entity Extraction (LLM) │
│ FIC-101, Pump-01, Valve... │
└─────────┬──────────────────────┘
┌─────────────────────────────────┐
│ 4. Relationship Inference │
│ (rules + LLM hybrid) │
└─────────┬──────────────────────┘
┌─────────────────────────────────┐
│ 5. Graph Builder │
│ nodes + edges │
└─────────┬──────────────────────┘
┌─────────────────────────────────┐
│ 6. DB Integration Layer │
│ (existing OPC + SQL system) │
└─────────────────────────────────┘
✔ 1⃣ Document Parsing Layer (PDF → 구조화)
기술
Unstructured
역할
텍스트 추출
표 추출
블록 segmentation
page coordinate 유지
출력 예시
{
"page": 12,
"elements": [
{
"text": "FIC-101",
"bbox": [120, 300, 160, 320]
}
]
}
👉 핵심: 좌표 반드시 유지
✔ 2⃣ Spatial Element Extraction (핵심 단계)
여기서 P&ID가 살아난다.
해야 할 것
symbol detection
line detection
proximity mapping
결과
JSON
{
"FIC-101": { "x": 120, "y": 300 },
"FT-101": { "x": 110, "y": 220 },
"Valve-203": { "x": 300, "y": 310 }
}
✔ 3⃣ Entity Extraction (LLM)
모델
Qwen (via vLLM)
입력
Extract all P&ID entities:
- controller
- sensor
- valve
- pump
출력
[
{"name":"FIC-101","type":"controller"},
{"name":"FT-101","type":"sensor"},
{"name":"Valve-203","type":"valve"}
]
✔ 4⃣ Relationship Inference (가장 중요)
여기서 2개를 섞는다:
A. Rule Engine (필수)
if sensor near controller:
relation = "signal"
if controller connected to valve:
relation = "control"
if pump → tank:
relation = "flow"
B. LLM 보조 판단
Determine relationship based on P&ID context:
Entities + coordinates
최종 출력
[
{
"source": "FT-101",
"target": "FIC-101",
"relation": "signal",
"confidence": 0.93
},
{
"source": "FIC-101",
"target": "Valve-203",
"relation": "control",
"confidence": 0.91
}
]
✔ 5⃣ Graph Builder
nodes = set()
edges = []
for r in relations:
nodes.add(r["source"])
nodes.add(r["target"])
edges.append(r)
Graph 구조
{
"nodes": [...],
"edges": [...]
}
✔ 6⃣ DB Integration (너 기존 시스템)
여기서 너 강점:
OPC UA 있음 ✔
NL2SQL 있음 ✔
DB 있음 ✔
저장 전략
P&ID Graph DB
Mapping Layer
OPC Tags / SQL semantic layer
✔ 🔥 핵심 설계 포인트 (진짜 중요)
❗ 1. LLM은 “결정자”가 아니다
👉 “해석 보조” 역할
❗ 2. 좌표가 핵심이다
P&ID는 텍스트가 아니라:
공간 + 연결 구조
❗ 3. Rule Engine 없으면 실패한다
LLM 단독 = hallucination
✔ 📌 최종 아키텍처 (완성형)
P&ID PDF
Unstructured Parser
Spatial Engine
Qwen (Entity Extraction)
Rule Engine (relationship)
Graph Builder
DB + OPC Integration
✔ 🚀 이 구조의 의미
이건 단순 기능이 아니라:
👉 “Engineering Knowledge Graph 생성 엔진”
✔ 다음 단계 (진짜 실전)
원하면 여기서 바로 이어서:
1⃣ P&ID 실제 PDF 기준 코드
2⃣ Rule engine 상세 설계 (ISA 기반)
3⃣ Qwen prompt 최적화 (산업용)
4⃣ Graph DB vs SQL 구조 선택

View File

@@ -1,220 +0,0 @@
# 🛠️ Graph Pipeline Phase 1: 기하학적 데이터 추출 (Geometric Extraction)
이 문서는 P&ID Graph Pipeline의 첫 번째 단계인 **기하학적 데이터 추출**의 상세 구현 계획을 다룹니다. 목표는 단순한 텍스트 추출을 넘어, 도면 내 모든 객체의 **물리적 위치(좌표)**와 **기하학적 속성**을 보존하여 이후 위상 모델링(Topology Modeling)이 가능하도록 하는 것입니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `ezdxf` | DXF 파일 파싱 및 엔티티 추출 | 핵심 라이브러리 |
| `shapely` | 기하학적 연산 (Intersection, Distance, Bounding Box) | 좌표 기반 분석 필수 |
| `numpy` | 대량의 좌표 데이터 계산 및 행렬 연산 | 성능 최적화 |
| `pandas` | 추출된 객체 데이터의 구조화 및 CSV/JSON 저장 | 데이터 관리 |
| `pydantic` | 추출 데이터의 스키마 정의 및 유효성 검증 | 데이터 무결성 보장 |
| `pytesseract` / `pdf2image` | PDF 도면의 영역 기반 OCR 추출 | PDF 처리 시 필요 |
### 1.2 설치 명령어
```bash
pip install ezdxf shapely numpy pandas pydantic pytesseract pdf2image
```
---
## 📐 2. 상세 설계 구조
### 2.1 데이터 모델 (Schema)
모든 추출 객체는 다음과 같은 공통 속성을 갖는 `GeometricEntity` 모델을 따릅니다.
```python
from pydantic import BaseModel
from typing import List, Optional, Union, Tuple
class BoundingBox(BaseModel):
min_x: float
min_y: float
max_x: float
max_y: float
center: Tuple[float, float]
class GeometricEntity(BaseModel):
entity_id: str
entity_type: str # TEXT, LINE, CIRCLE, POLYLINE, ARC
layer: str
bbox: BoundingBox
properties: dict # 텍스트 값, 색상, 선 굵기 등
coordinates: List[Tuple[float, float]] # 시작점, 끝점 또는 정점 리스트
```
### 2.2 처리 파이프라인 흐름
1. **DXF Load:** `ezdxf.readfile()`을 통해 도면 로드.
2. **Entity Iteration:** 모든 레이어의 엔티티를 순회하며 타입별 분류.
3. **Coordinate Extraction:**
* `TEXT`: 삽입점(Insertion Point) 및 텍스트 길이를 이용한 BBox 계산.
* `LINE`: 시작점(Start)과 끝점(End) 추출.
* `POLYLINE`: 모든 정점(Vertices) 리스트 추출.
* `CIRCLE/ARC`: 중심점(Center)과 반지름(Radius) 추출.
4. **Spatial Normalization:** 도면 좌표계를 분석 시스템 좌표계로 정규화.
5. **Structured Export:** JSON 또는 DB(PostgreSQL/PostGIS)에 저장.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 DXF 기하학적 추출 핵심 코드
```python
import ezdxf
import re
import json
from shapely.geometry import box, LineString, Point
from typing import List, Optional, Tuple
class PidGeometricExtractor:
def __init__(self, file_path: str):
self.doc = ezdxf.readfile(file_path)
self.msp = self.doc.modelspace()
def clean_text(self, text: str) -> str:
"""DXF 특수 제어 문자 및 MTEXT 포맷팅을 최대한 제거하여 LLM 토큰 부하 감소"""
if not text:
return ""
# 1. MTEXT 포맷팅 및 제어 문자 제거
# \P(줄바꿈), \W(너비), \L(밑줄), \A(정렬), \C(색상), \H(높이), \S(스택), \T(탭) 및 관련 인자 제거
text = re.sub(r'\\([P|W|L|A|C|H|S|T])\d*;?', ' ', text)
# 2. 중괄호 { } 제거 (MTEXT에서 서식 지정 시 사용됨)
text = re.sub(r'[\{\}]', ' ', text)
# 3. DXF 특수 제어 문자 제거 (%%U: Underline, %%O: Overline, %%S: Strikethrough, %%R: Registered)
text = re.sub(r'%%[U|O|S|R]', ' ', text)
# 4. 불필요한 특수 기호 및 반복되는 공백 정제
# - 연속된 공백을 하나로 통합
# - 텍스트 양 끝의 공백 제거
text = re.sub(r'\s+', ' ', text).strip()
return text
def get_bbox(self, entity) -> Optional[box]:
"""엔티티의 Bounding Box를 계산하여 shapely box 객체로 반환"""
try:
if entity.dxftype() == 'TEXT':
p = entity.dxf.insert
h = entity.dxf.height
# 텍스트 길이에 따른 대략적인 너비 계산 (글자수 * 높이 * 0.6)
width = len(entity.dxf.text) * h * 0.6
return box(p.x, p.y, p.x + width, p.y + h)
elif entity.dxftype() == 'MTEXT':
p = entity.dxf.insert
h = entity.dxf.char_height if hasattr(entity.dxf, 'char_height') else 2.5
# MTEXT는 보통 width 속성이 정의되어 있음
w = entity.dxf.width if entity.dxf.width > 0 else len(entity.text) * h * 0.6
return box(p.x, p.y, p.x + w, p.y + h)
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
return box(min(start.x, end.x), min(start.y, end.y),
max(start.x, end.x), max(start.y, end.y))
elif entity.dxftype() == 'LWPOLYLINE':
points = entity.get_points()
xs = [p[0] for p in points]
ys = [p[1] for p in points]
return box(min(xs), min(ys), max(xs), max(ys))
except Exception as e:
print(f"Error calculating bbox for {entity.dxftype()}: {e}")
return None
def extract_and_save(self, output_path: str):
"""
추출된 기하학적 데이터를 파일로 저장하여 Phase 3 Worker들이
공유 메모리/파일 시스템을 통해 참조할 수 있도록 함 (Phase 5 병렬 아키텍처 반영)
"""
results = []
for entity in self.msp:
bbox_obj = self.get_bbox(entity)
if bbox_obj:
# 텍스트 값 추출 및 정제
raw_text = ""
if entity.dxftype() == 'TEXT':
raw_text = entity.dxf.text
elif entity.dxftype() == 'MTEXT':
raw_text = entity.text
results.append({
"id": entity.dxf.handle,
"type": entity.dxftype(),
"layer": entity.dxf.layer,
"bbox": {
"min_x": bbox_obj.bounds[0],
"min_y": bbox_obj.bounds[1],
"max_x": bbox_obj.bounds[2],
"max_y": bbox_obj.bounds[3]
},
"raw_value": raw_text,
"clean_value": self.clean_text(raw_text) if raw_text else None,
"coordinates": entity.get_points() if hasattr(entity, 'get_points') else []
})
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return output_path
# 사용 예시 (Phase 5 Orchestrator 관점)
extractor = PidGeometricExtractor("plant_drawing.dxf")
# 데이터를 직접 반환받지 않고 공유 저장소(파일)에 적재
geo_data_path = extractor.extract_and_save("shared_geo_data.json")
```
### 3.2 유틸리티 함수: 인접성 체크 (Proximity Utility)
추후 2단계(위상 모델링)에서 사용할 핵심 유틸리티입니다.
```python
from shapely.geometry import Point
def is_near(entity_a_bbox, entity_b_bbox, threshold=5.0):
"""두 객체의 Bounding Box 간의 최단 거리가 임계값 이내인지 확인"""
return entity_a_bbox.distance(entity_b_bbox) <= threshold
def is_inside(point, bbox):
"""특정 점이 Bounding Box 내부에 있는지 확인"""
return bbox.contains(Point(point))
```
---
## 🚀 4. Phase 1 완료 기준 (Definition of Done)
- [ ] DXF 파일 내 모든 `TEXT`, `LINE`, `POLYLINE`의 좌표 데이터가 누락 없이 추출되는가?
- [ ] 각 객체별로 정확한 `Bounding Box`가 계산되어 저장되는가?
- [ ] 추출된 데이터가 `GeometricEntity` 스키마에 맞게 JSON 파일로 저장되어 Worker들이 공유 참조 가능한가? (Phase 5 반영)
- [ ] (선택 사항) PDF 도면의 경우 OCR을 통해 텍스트의 좌표값이 추출되는가?
---
## 🧐 감독자 진단 결과 (2026-05-02)
### 1. 프로그램 설계 점검
- **강점**: `ezdxf``shapely`를 조합하여 기하학적 데이터(BBox, 좌표)를 보존하려는 접근 방식이 매우 적절함. 특히 Phase 5의 병렬 아키텍처를 고려하여 데이터를 파일/공유 저장소에 적재하는 구조는 확장성 면에서 우수함.
- **보완 필요 사항**:
- **MTEXT 처리**: 현재 예시 코드(`3.1`)는 `TEXT` 엔티티만 처리하고 있으나, 실제 DXF 파일 분석 결과 `MTEXT` 엔티티가 다수 존재함. `MTEXT`는 내부 포맷팅 코드(예: `\P`, `\W`)가 포함되어 있어 단순 텍스트 추출 시 정제가 필요함.
- **BBox 계산 정밀도**: `TEXT` 엔티티의 BBox를 `p.x + 10, p.y + 5`와 같이 상수로 처리하고 있음. 실제 도면의 폰트 크기(`height`)와 정렬 방식(`align`)을 반영한 동적 계산 로직이 반드시 추가되어야 함.
### 2. 실제 도면(`No-10_Plant_PID.dxf`) 분석 기반 차이점
- **엔티티 규모**: 총 28,819개의 엔티티가 존재하여 데이터 양이 상당함. 단순 리스트 저장보다는 인덱싱 전략이 필요할 수 있음.
- **텍스트 복잡도**:
- `MTEXT` 내에 `\P` (줄바꿈), `\L` (밑줄) 등 제어 문자가 포함된 수정 사항(Revision) 텍스트가 많음. 이를 그대로 추출하면 위상 분석 시 노이즈가 될 가능성이 높음.
- `%%U` (Underline)와 같은 DXF 특수 제어 문자가 텍스트 값에 포함되어 있어, 이를 제거하는 전처리 과정이 필수적임.
- **데이터 특성**: `IA-10922-25A-F1A-n`와 같은 복합 파이프라인 번호(Pipe Line Number) 형식이 확인됨. 이를 일반 태그(Tag Name)와 명확히 구분하여 추출하고 관리하는 로직이 Phase 2/3에서 중요하게 작용할 것으로 보임.
### 3. 최종 권고 사항
1. **MTEXT 지원 추가**: `PidGeometricExtractor``MTEXT` 처리 로직을 추가하고, 제어 문자를 제거하는 `clean_text()` 유틸리티 함수를 구현할 것.
2. **동적 BBox 구현**: `entity.dxf.height`를 활용하여 텍스트 크기에 맞는 정확한 Bounding Box를 계산하도록 수정할 것.
3. **전처리 파이프라인 강화**: 추출 단계에서 `%%U` 등의 특수 문자를 제거하는 정제 단계를 추가하여 데이터 품질을 높일 것.

View File

@@ -1,184 +0,0 @@
# 🕸️ Graph Pipeline Phase 2: 위상 모델링 (Topology Modeling)
이 문서는 P&ID Graph Pipeline의 두 번째 단계인 **위상 모델링**의 상세 구현 계획을 다룹니다. 1단계에서 추출한 기하학적 객체(좌표, BBox)를 기반으로, 설비 간의 **연결성(Connectivity)**과 **흐름(Flow)**을 정의하는 지식 그래프(Knowledge Graph)를 구축하는 것이 목표입니다.
---
## 🚩 [Supervisor's Audit] 진단 결과 및 개선 권고
**감독자 진단 일자:** 2026-05-02
**진단 결과:** ⚠️ **부분적 보완 필요 (Partial Improvement Required)**
### 🔍 주요 진단 내용
1. **연결성 추론의 단순성 (Critical):** 현재 `_find_connected_nodes`가 단순 BBox 교차(`intersects`)만 확인하고 있습니다. 실제 P&ID에서 배관(Line)은 설비의 외곽선에 닿거나 매우 근접한 형태로 나타나며, 단순 BBox 교차는 오탐(False Positive) 확률이 매우 높습니다.
2. **방향성 정의 부재 (Medium):** `DiGraph`를 사용하지만, 실제 엣지에 방향성을 부여하는 구체적인 로직(화살표 인식, 공정 흐름 규칙)이 예시 코드에 누락되어 있습니다.
3. **임계값 하드코딩 (Low):** `min_dist < 50.0`과 같은 임계값이 하드코딩되어 있어, 도면 스케일(Scale)이 변경될 경우 대응이 불가능합니다.
4. **데이터 무결성 검증 부족 (Medium):** 그래프 생성 후 고립된 노드(Isolated Nodes)나 비정상적인 루프에 대한 검증 단계가 없습니다.
### 🛠️ 수정 및 반영 사항
- **연결성 로직 고도화:** BBox 교차 방식에서 $\rightarrow$ **Line End-point 기반 근접 분석** 방식으로 변경.
- **방향성 추론 단계 명시:** 화살표 심볼 및 공정 흐름 기반의 `source` $\rightarrow$ `target` 결정 로직 추가.
- **설정의 외부화:** 임계값($\epsilon$)을 설정 파일이나 파라미터로 관리하도록 구조 변경.
- **검증 단계 추가:** 그래프 구축 후 위상 무결성 검사(Topology Validation) 단계 도입.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `networkx` | 그래프 데이터 구조 생성 및 알고리즘 분석 | 핵심 라이브러리 |
| `shapely` | 객체 간 거리 계산 및 포함 관계 분석 | 1단계와 연계 |
| `scikit-learn` | (선택) KD-Tree를 이용한 고속 근접 이웃 검색 | 대규모 도면 최적화 |
| `matplotlib` | 생성된 그래프의 위상 구조 시각화 검증 | 디버깅용 |
### 1.2 설치 명령어
```bash
pip install networkx shapely scikit-learn matplotlib
```
---
## 📐 2. 상세 설계 구조
### 2.1 그래프 정의 (Graph Definition)
* **노드 (Nodes):**
* `Equipment`: 펌프, 탱크, 열교환기 등 (속성: ID, 타입, BBox, CenterPoint)
* `Instrument`: 전송기, 밸브, 게이지 등 (속성: ID, 타입, BBox, CenterPoint)
* `Tag`: 텍스트 기반 태그 (속성: TagName, Value, BBox)
* **엣지 (Edges):**
* `Pipe`: 설비-설비, 설비-계기 간의 물리적 연결 (속성: LineNumber, 방향성, 연결타입)
* `Association`: 태그-설비 간의 논리적 연결 (속성: 관계 타입 - 예: 'belongs_to')
### 2.2 위상 추론 로직 (Topology Inference)
1. **태그-설비 결합 (Tag-to-Entity Binding):**
* 태그 텍스트의 BBox와 가장 가까운 심볼(Equipment/Instrument)을 찾아 `Association` 엣지를 생성합니다.
2. **배관 연결성 분석 (Line Connectivity) [개선]:**
* `LINE` 또는 `POLYLINE`의 **시작점과 끝점(End-points)**을 추출합니다.
* 각 끝점이 특정 설비의 BBox 내부에 있거나, 설정된 임계 거리($\epsilon$) 이내에 있을 때만 `Pipe` 엣지로 연결합니다. (단순 BBox 교차 방식 지양)
3. **흐름 방향성 부여 (Flow Direction) [추가]:**
* 배관 상의 화살표 심볼 위치와 방향을 분석하여 `source` $\rightarrow$ `target`을 결정합니다.
* 화살표가 없는 경우, 공정 표준(예: 탱크 $\rightarrow$ 펌프 $\rightarrow$ 밸브)에 따른 기본 방향을 부여합니다.
4. **위상 무결성 검증 (Topology Validation) [추가]:**
* 연결되지 않은 고립 노드 탐색 및 리포팅.
* 비정상적인 사이클(Cycle) 또는 단절 구간 확인.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 그래프 구축 핵심 코드
```python
import networkx as nx
from shapely.geometry import box, Point, LineString
class PidTopologyBuilder:
def __init__(self, geometric_data, all_extracted_tags=None, config=None):
"""
- geometric_data: Phase 1에서 추출된 기하학적 데이터
- all_extracted_tags: 통합된 태그 리스트
- config: {'dist_threshold': 50.0, 'tag_threshold': 100.0} 등 설정값
"""
self.data = geometric_data
self.all_tags = all_extracted_tags if all_extracted_tags else []
self.config = config if config else {'dist_threshold': 50.0, 'tag_threshold': 100.0}
self.G = nx.DiGraph() # 방향성 그래프 생성
def build_graph(self):
# 1. 모든 객체를 노드로 추가
for item in self.data:
# Phase 1에서 'clean_value'로 저장했으므로 이를 value로 사용
self.G.add_node(item['id'],
type=item['type'],
bbox=box(*item['bbox'].values()),
value=item.get('clean_value'))
# 2. 분산 추출된 태그 통합 및 노드 추가
for tag in self.all_tags:
self.G.add_node(tag['id'],
type='TEXT',
bbox=box(*tag['bbox'].values()),
value=tag.get('tagName'))
# 3. 태그-설비 논리적 연결 (Association)
tags = [n for n, d in self.G.nodes(data=True) if d['type'] == 'TEXT']
equipments = [n for n, d in self.G.nodes(data=True) if d['type'] != 'TEXT']
for tag in tags:
best_match = self._find_nearest_equipment(tag, equipments)
if best_match:
self.G.add_edge(tag, best_match, relation='associated_with')
# 4. 배관 기반 물리적 연결 (Pipe) [개선됨]
lines = [n for n, d in self.G.nodes(data=True) if d['type'] in ['LINE', 'POLYLINE']]
for line_id in lines:
# Phase 1에서 추출한 coordinates를 사용하여 LineString 생성
coords = self.G.nodes[line_id].get('coordinates', [])
if not coords:
continue
line_geom = LineString(coords)
endpoints = [line_geom.coords[0], line_geom.coords[-1]]
connected_nodes = []
for pt in endpoints:
p = Point(pt)
for eq_id in equipments:
if self.G.nodes[eq_id]['bbox'].distance(p) < self.config['dist_threshold']:
connected_nodes.append(eq_id)
if len(connected_nodes) >= 2:
# 방향성 추론 로직 (단순화: 시작점 -> 끝점)
self.G.add_edge(connected_nodes[0], connected_nodes[1], relation='pipe')
def _find_nearest_equipment(self, tag_id, equipment_ids):
tag_bbox = self.G.nodes[tag_id]['bbox']
min_dist = float('inf')
nearest = None
for eq_id in equipment_ids:
eq_bbox = self.G.nodes[eq_id]['bbox']
dist = tag_bbox.distance(eq_bbox)
if dist < min_dist:
min_dist = dist
nearest = eq_id
return nearest if min_dist < self.config['tag_threshold'] else None
def validate_topology(self):
"""위상 무결성 검증"""
isolated = list(nx.isolates(self.G))
return {"isolated_nodes": isolated, "node_count": self.G.number_of_nodes(), "edge_count": self.G.number_of_edges()}
# 실행 예시
all_tags = flatten_results([worker1_res, worker2_res])
config = {'dist_threshold': 30.0, 'tag_threshold': 80.0}
builder = PidTopologyBuilder(geometric_data, all_extracted_tags=all_tags, config=config)
builder.build_graph()
validation_res = builder.validate_topology()
print(f"Validation Result: {validation_res}")
```
### 3.2 위상 분석 유틸리티: 영향도 분석 (Impact Analysis)
```python
def analyze_impact(graph, start_node):
"""특정 설비 장애 시 하류(Downstream)에 영향을 받는 모든 노드 추출"""
# BFS를 통해 도달 가능한 모든 노드 탐색
impacted_nodes = nx.descendants(graph, start_node)
return list(impacted_nodes)
# 예: P-101 펌프 고장 시 영향 분석
affected = analyze_impact(graph, "node_P101")
print(f"Impacted Equipment: {affected}")
```
---
## 🚀 4. Phase 2 완료 기준 (Definition of Done)
- [ ] 모든 설비와 계기가 그래프의 **노드(Node)**로 변환되었는가?
- [ ] 분산 추출된 태그 리스트가 `flatten_results`를 통해 통합되어 그래프에 반영되었는가?
- [ ] 태그와 설비 간의 **논리적 연결(Association)**이 정확하게 매핑되었는가?
- [ ] 배관(Line)의 **끝점 분석**을 통해 설비 간의 **물리적 연결(Pipe Edge)**이 생성되었는가? (BBox 교차 방식 배제)
- [ ] 화살표 및 공정 규칙에 기반한 **방향성(Directionality)**이 엣지에 부여되었는가?
- [ ] `validate_topology`를 통해 고립 노드 및 위상 오류가 검토되었는가?
- [ ] `nx.descendants` 등을 통해 특정 노드로부터의 **흐름 추적(Flow Tracing)**이 가능한가?
- [ ] 생성된 그래프 구조가 JSON(GraphML 등) 형태로 저장되어 Phase 3로 전달 가능한가?

View File

@@ -1,212 +0,0 @@
# 🧠 Graph Pipeline Phase 3: 지능형 매핑 및 검증 (Intelligent Mapping & Validation)
이 문서는 P&ID Graph Pipeline의 세 번째 단계인 **지능형 매핑 및 검증**의 상세 구현 계획을 다룹니다. 2단계에서 구축한 위상 그래프(Topology Graph)를 활용하여, 도면 상의 가상 노드들을 실제 Experion 시스템의 **실시간 태그(Real-time Tags)**와 정밀하게 연결하고 그 타당성을 검증하는 것이 목표입니다.
---
## 🚩 [Supervisor's Audit] 감독자 진단 결과 및 수정 사항
본 프로그램 설계에 대해 감독자 관점에서 정밀 진단을 수행하였으며, 다음과 같은 취약점과 개선 사항을 발견하여 반영하였습니다.
### 1. 진단 결과 (Audit Findings)
| 항목 | 진단 내용 | 심각도 | 수정 방향 |
|---|---|---|---|
| **에러 처리** | LLM 응답이 JSON 형식이 아니거나 `UNKNOWN`일 때의 예외 처리 로직 부족 | HIGH | 구조화된 출력(JSON) 강제 및 Fallback 전략 추가 |
| **성능/비용** | 모든 노드에 대해 개별 LLM 호출 시 API 비용 급증 및 속도 저하 | MED | 배치(Batch) 처리 및 1차 필터링 강화 |
| **검증 정밀도** | 단순 키워드 매칭 기반 검증은 오탐(False Positive) 가능성이 높음 | MED | 데이터 타입 및 엔지니어링 유닛(EU)의 엄격한 비교 로직 추가 |
| **데이터 정합성** | 매핑 결과의 이력 관리 및 사람이 수동으로 수정할 수 있는 피드백 루프 부재 | LOW | 매핑 결과 저장 스키마에 `confidence``manual_override` 필드 추가 |
### 2. 수정 이유 (Rationale)
- **안정성 확보:** LLM은 비결정론적 특성이 있으므로, 프로그램이 런타임에 중단되지 않도록 Pydantic을 이용한 엄격한 스키마 검증이 필수적입니다.
- **효율성 최적화:** 수천 개의 태그를 개별 호출하는 것은 비효율적입니다. 유사도 기반으로 후보군을 좁히고, 유사 그룹을 묶어 배치 처리함으로써 비용을 절감합니다.
- **신뢰도 향상:** 단순 텍스트 매칭을 넘어 실제 시스템의 메타데이터(Unit, Range 등)를 교차 검증해야 엔지니어링 관점에서 신뢰할 수 있는 결과가 됩니다.
---
## 📦 1. 필수 패키지 및 환경 설정
### 1.1 Python 패키지
| 패키지 | 용도 | 비고 |
|---|---|---|
| `openai` / `langchain` | LLM API 연동 및 프롬프트 체이닝 | 매핑 추론 및 검증 핵심 |
| `fuzzywuzzy` / `rapidfuzz` | 태그 이름 간의 문자열 유사도 계산 | 1차 후보군 추출용 |
| `networkx` | 그래프 기반 인접 노드(Context) 추출 | 2단계 그래프 활용 |
| `pydantic` | 매핑 결과의 구조화 및 유효성 검사 | **[강화]** 데이터 정규화 및 런타임 타입 체크 |
| `requests` | ExperionCrawler API (C#)와 통신 | 실제 태그 리스트 조회 |
### 1.2 설치 명령어
```bash
pip install openai langchain rapidfuzz networkx pydantic requests
```
---
## 📐 2. 상세 설계 구조
### 2.1 매핑 파이프라인 (Mapping Pipeline)
단순 이름 매칭의 한계를 극복하기 위해 **[후보 추출 $\rightarrow$ 맥락 분석 $\rightarrow$ LLM 확정 $\rightarrow$ 스키마 검증]**의 4단계 프로세스를 거칩니다.
1. **1차 후보 추출 (Candidate Generation):**
* 도면의 태그 텍스트와 Experion 시스템의 전체 태그 리스트를 `RapidFuzz`로 비교하여 유사도 상위 N개를 추출합니다.
2. **맥락 정보 수집 (Context Gathering):**
* 해당 노드의 그래프 상 인접 노드(1-hop, 2-hop) 정보를 수집합니다.
* 예: "현재 노드는 `PT-101`이며, 상류에 `P-101(Pump)`이 있고 하류에 `V-101(Valve)`이 있음."
3. **LLM 기반 최종 매핑 (LLM-based Resolution):**
* 후보 태그 리스트와 위상 맥락을 LLM에게 전달하여 가장 타당한 태그를 선택하게 합니다.
* **[개선]** JSON Mode를 사용하여 `{"tag": "...", "reason": "...", "confidence": 0.9}` 형태로 응답을 강제합니다.
4. **구조적 검증 (Structural Validation):**
* Pydantic 모델을 통해 LLM 응답의 형식을 검증하고, 실패 시 `UNKNOWN` 처리 및 로그를 남깁니다.
### 2.2 상호 검증 로직 (Cross-Validation)
매핑된 결과가 실제 공정 데이터와 일치하는지 검증합니다.
* **위상적 일관성:** 도면에서 `A $\rightarrow$ B` 순서라면, 실제 데이터에서도 `A`의 변화가 `B`에 영향을 주는지 상관관계 분석.
* **속성 일치성:** 도면의 심볼 타입(예: Pressure Transmitter)과 실제 태그의 속성(예: Engineering Unit = 'bar' 또는 'psi')이 일치하는지 확인. **[강화]** 단순 키워드가 아닌 Unit 매핑 테이블을 통한 엄격한 비교.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 맥락 기반 매핑 엔진
```python
import networkx as nx
import asyncio
import json
from typing import List, Optional
from pydantic import BaseModel, Field
from rapidfuzz import process, fuzz
from openai import AsyncOpenAI
# --- [추가] 응답 구조화를 위한 Pydantic 모델 ---
class MappingResult(BaseModel):
resolved_tag: str = Field(..., description="The final mapped system tag")
reason: str = Field(..., description="Reason for this mapping based on context")
confidence: float = Field(..., ge=0.0, le=1.0, description="Confidence score from 0 to 1")
client = AsyncOpenAI(api_key="your-api-key")
class IntelligentMapper:
def __init__(self, graph, system_tags):
self.graph = graph # Phase 2에서 생성된 NetworkX 그래프
self.system_tags = system_tags # Experion 시스템의 전체 태그 리스트
def get_node_context(self, node_id):
"""노드의 주변 위상 정보를 텍스트로 변환"""
neighbors = list(self.graph.neighbors(node_id))
context = []
for n in neighbors:
attr = self.graph.nodes[n]
context.append(f"Connected to {attr.get('value', n)} (Type: {attr.get('type')})")
return ", ".join(context)
async def _resolve_generic(self, node_id, category_prompt):
"""공통 매핑 로직 (비동기 + 구조화 응답)"""
# Phase 2에서 'value'에 clean_value가 저장됨
tag_text = self.graph.nodes[node_id].get('value', '')
candidates = process.extract(tag_text, self.system_tags, scorer=fuzz.WRatio, limit=5)
context = self.get_node_context(node_id)
prompt = f"""
{category_prompt}
P&ID 도면의 태그 '{tag_text}'를 실제 시스템 태그와 매핑해야 합니다.
위상 맥락: {context}
후보 리스트: {candidates}
반드시 다음 JSON 형식으로만 응답하세요:
{{
"resolved_tag": "태그명 또는 UNKNOWN",
"reason": "매핑 이유",
"confidence": 0.0~1.0
}}
"""
try:
response = await client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}],
response_format={ "type": "json_object" } # JSON 모드 강제
)
raw_content = response.choices[0].message.content
# Pydantic을 통한 유효성 검사
return MappingResult.model_validate_json(raw_content)
except Exception as e:
print(f"Error resolving node {node_id}: {e}")
return MappingResult(resolved_tag="UNKNOWN", reason=f"Error: {str(e)}", confidence=0.0)
# --- 전문화된 Worker 함수들 ---
async def extract_transmitters(self, node_ids):
prompt = "당신은 계측기 전문 엔지니어입니다. 특히 Pressure/Flow/Level Transmitter 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_valves(self, node_ids):
prompt = "당신은 밸브 및 액추에이터 전문 엔지니어입니다. 밸브의 개폐 상태 및 제어 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
async def extract_equipment(self, node_ids):
prompt = "당신은 공정 설비 전문 엔지니어입니다. 펌프, 탱크, 열교환기 등의 메인 설비 태그 매핑에 특화되어 있습니다."
return {nid: await self._resolve_generic(nid, prompt) for nid in node_ids}
# 사용 예시
async def main():
# 가상 데이터
graph = nx.Graph()
graph.add_node("node_1", value="PT-101", type="Pressure Transmitter")
graph.add_node("node_2", value="P-101", type="Pump")
graph.add_edge("node_1", "node_2")
mapper = IntelligentMapper(graph, ["PT-101.PV", "PT-102.PV", "P-101.STATUS"])
results = await asyncio.gather(
mapper.extract_transmitters(["node_1"]),
mapper.extract_equipment(["node_2"])
)
final_mapping = {**results[0], **results[1]}
print(f"Parallel Resolved Mapping: {final_mapping}")
asyncio.run(main())
```
### 3.2 검증 유틸리티: 속성 일치 확인 (강화 버전)
```python
def validate_mapping(resolved_tag, symbol_type, tag_metadata):
"""심볼 타입과 실제 태그 메타데이터의 엄격한 일치 여부 검증"""
# 단순 키워드가 아닌 허용 단위(Unit) 정의
unit_map = {
"Pressure Transmitter": ["bar", "psi", "kPa", "Pa"],
"Flow Meter": ["m3/h", "lpm", "kg/h"],
"Temperature Sensor": ["°C", "C", "K", "°F"]
}
actual_unit = tag_metadata.get('unit', '').strip()
allowed_units = unit_map.get(symbol_type, [])
# 1. 단위 일치 확인 (최우선)
if actual_unit and actual_unit in allowed_units:
return True, "Unit Match"
# 2. 단위가 없는 경우 설명(Description) 기반 2차 검증
actual_desc = tag_metadata.get('description', '').lower()
expected_keywords = {
"Pressure Transmitter": ["pressure", "press"],
"Flow Meter": ["flow", "flowrate"],
"Temperature Sensor": ["temp", "temperature"]
}
keywords = expected_keywords.get(symbol_type, [])
if any(kw in actual_desc for kw in keywords):
return True, "Description Match (Unit Missing)"
return False, "Mismatch: Symbol type and Tag metadata do not align"
```
---
## 🚀 4. Phase 3 완료 기준 (Definition of Done)
- [ ] 모든 도면 노드에 대해 **1차 후보군(Candidates)**이 자동으로 생성되는가?
- [ ] `NetworkX` 그래프를 통해 **인접 노드 맥락(Context)**이 정확히 추출되는가?
- [ ] LLM이 **JSON 형식**으로 최종 태그를 결정하고, 그 근거와 신뢰도를 제시하는가?
- [ ] **Pydantic**을 통해 LLM 응답의 구조적 유효성이 검증되는가?
- [ ] 매핑된 태그의 **엔지니어링 유닛(Unit)**과 도면 심볼 타입 간의 일치성이 엄격히 검증되는가?
- [ ] 최종 매핑 결과가 `(도면노드ID, 시스템태그, 신뢰도, 검증결과, 매핑근거)` 형태로 저장되는가?

View File

@@ -1,197 +0,0 @@
# 🎨 Graph Pipeline Phase 4: 활용 및 시각화 (Application & Visualization)
이 문서는 P&ID Graph Pipeline의 최종 단계인 **활용 및 시각화**의 상세 구현 계획을 다룹니다. 앞선 단계에서 구축한 [기하학적 데이터 $\rightarrow$ 위상 그래프 $\rightarrow$ 시스템 태그 매핑] 결과물을 결합하여, 운영자가 도면 상에서 실시간 공정 상태를 파악하고 장애 영향도를 분석할 수 있는 인터페이스를 구현하는 것이 목표입니다.
---
## 🔍 [Supervisor Diagnosis] 프로그램 진단 및 개선 권고
**진단 일자:** 2026-05-02
**진단자:** Roo (Software Engineer / Supervisor)
### 1. 종합 진단 결과
현재 계획은 기본적인 데이터 흐름(C# $\rightarrow$ Python $\rightarrow$ Frontend)을 잘 정의하고 있으나, **실제 산업 현장의 대규모 P&ID 도면 적용 시 발생할 수 있는 성능 및 안정성 문제**에 대한 고려가 부족합니다. 특히 실시간 데이터 오버레이의 부하 관리와 분석 결과의 신뢰성 검증 단계가 누락되어 있습니다.
### 2. 주요 진단 항목 및 수정 이유
| 항목 | 진단 결과 | 위험도 | 수정 이유 및 개선 방향 |
|---|---|---|---|
| **데이터 전송 효율** | WebSocket/API 폴링 방식의 단순 나열 | MED | 수천 개의 태그가 포함된 도면에서 개별 폴링/전송 시 네트워크 부하 급증 $\rightarrow$ **태그 그룹화 및 변경분 기반(Delta) 전송** 도입 필요 |
| **프론트엔드 렌더링** | SVG/Canvas 단순 오버레이 | HIGH | 노드 수가 많아질 경우 DOM 요소 증가로 인한 브라우저 랙 발생 $\rightarrow$ **Canvas 기반 렌더링 최적화 및 Viewport 기반 가시 영역 렌더링** 전략 필요 |
| **분석 엔진 신뢰성** | `nx.descendants` 단순 활용 | MED | 단순 위상 전파는 실제 공정의 '흐름 방향(Flow Direction)'과 '밸브 개폐 상태'를 무시함 $\rightarrow$ **엣지 속성(방향성, 상태)을 반영한 가중치 경로 분석**으로 고도화 |
| **에러 핸들링** | Python 브릿지 통신 시 예외 처리 미흡 | LOW | 분석 엔진 다운 시 C# 서버의 블로킹 가능성 $\rightarrow$ **Circuit Breaker 패턴 및 타임아웃 설정** 명시 필요 |
| **사용자 경험(UX)** | 단순 하이라이트 표시 | LOW | 영향도 결과가 많을 경우 도면이 빨간색으로 도배됨 $\rightarrow$ **단계별 영향도(1차, 2차...) 색상 구분 및 필터링** 기능 추가 |
---
## 📦 1. 필수 패키지 및 기술 스택
### 1.1 프론트엔드 (Visualization)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `SVG / Canvas API` | P&ID 도면 렌더링 및 데이터 오버레이 | **Canvas API 우선 권장 (대규모 노드 성능 최적화)** |
| `Cytoscape.js` / `D3.js` | 위상 그래프 시각화 및 인터랙티브 탐색 | 그래프 분석 뷰어 |
| `Vue.js` / `React` | 전체 UI 프레임워크 및 상태 관리 | `src/Web` 구조와 통합 |
| `Axios` / `WebSocket` | 실시간 OPC UA 데이터 수신 및 API 통신 | **SignalR (ASP.NET Core) 도입 권장 (실시간 양방향 통신 최적화)** |
### 1.2 백엔드 (API & Analysis)
| 기술/라이브러리 | 용도 | 비고 |
|---|---|---|
| `ASP.NET Core` | Graph API 및 분석 엔드포인트 제공 | `ExperionCrawler` 메인 서버 |
| `NetworkX` (Python) | 영향도 분석 및 경로 추적 알고리즘 실행 | 분석 엔진 (Phase 2 활용) |
| `FastAPI` / `Flask` | Python 분석 엔진과 C# 서버 간의 브릿지 | 분석 마이크로서비스 |
---
## 📐 2. 상세 설계 구조
### 2.1 실시간 데이터 오버레이 (Real-time Overlay)
도면의 좌표 정보와 매핑된 시스템 태그를 연결하여 실시간 값을 표시합니다.
1. **매핑 데이터 로드:** `(도면노드ID, 시스템태그, 좌표)` 리스트를 프론트엔드로 전달.
2. **실시간 스트리밍:** `OPC UA` $\rightarrow$ `C# Server` $\rightarrow$ `SignalR Hub` $\rightarrow$ `Frontend`. (**개선: 변경된 값만 전송하는 Delta Update 방식 적용**)
3. **동적 렌더링:** 태그 값이 변경되면 해당 좌표의 Canvas 요소를 업데이트하거나 툴팁에 현재 값을 표시. (**개선: Viewport 내 요소만 업데이트하여 CPU 부하 감소**)
### 2.2 영향도 분석 엔진 (Impact Analysis Engine)
특정 설비의 이상 발생 시 하류(Downstream) 영향을 계산합니다.
1. **분석 요청:** 사용자가 도면에서 특정 노드(예: 펌프 P-101)를 클릭.
2. **그래프 탐색:** Python 분석 엔진에서 `nx.descendants(G, 'P-101')` 실행. (**개선: 엣지의 `flow_direction` 속성을 확인하여 실제 유체 흐름 방향으로만 전파 계산**)
3. **결과 반환:** 영향받는 모든 노드 ID 리스트, 경로(Path), 그리고 **영향 단계(Depth)**를 반환.
4. **시각적 강조:** 도면 상에서 영향 경로를 단계별 색상(예: 1차-진한 빨강, 2차-연한 빨강)으로 하이라이트 처리.
---
## 💻 3. 실제 구현 코딩 가이드 (Example)
### 3.1 [Backend] 영향도 분석 API (C# $\rightarrow$ Python Bridge)
```csharp
// src/Web/Controllers/PidGraphController.cs
// 1. 분석 상태 추적을 위한 DTO
public record AnalysisStatus(string taskId, double progress, string status, string message);
// 2. 실시간 진행 상태 조회 API (Phase 5 병렬 처리 반영)
[HttpGet("status/{taskId}")]
public async Task<IActionResult> GetAnalysisStatus(string taskId)
{
// Orchestrator가 관리하는 작업 상태 저장소(Redis/MemoryCache)에서 조회
var status = await _statusService.GetStatusAsync(taskId);
if (status == null) return NotFound();
return Ok(new {
taskId = status.TaskId,
progress = status.Progress, // 0.0 ~ 1.0
status = status.Status, // "Processing", "Completed", "Failed"
message = status.Message
});
}
[HttpGet("impact/{nodeId}")]
public async Task<IActionResult> GetImpactAnalysis(string nodeId)
{
try
{
// Python 분석 마이크로서비스에 요청 (Timeout 및 Circuit Breaker 적용 권장)
var response = await _httpClient.GetAsync($"http://python-analysis-api/impact/{nodeId}");
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ImpactResult>();
return Ok(result);
}
catch (HttpRequestException ex)
{
// 분석 엔진 연결 실패 시 적절한 에러 메시지 반환
return StatusCode(503, new { error = "Analysis Engine is currently unavailable", details = ex.Message });
}
}
```
### 3.2 [Frontend] Canvas 기반 데이터 오버레이 및 진행률 표시 (JavaScript)
```javascript
// src/Web/wwwroot/js/pid-viewer.js
// 1. 실시간 값 업데이트 (Canvas 최적화 버전)
async function updateRealtimeValues(tagData) {
// tagData: { "TAG_01": { value: 10.5, status: "OK" }, ... }
const ctx = canvas.getContext('2d');
for (const [tag, data] of Object.entries(tagData)) {
const node = nodeMap.get(tag); // 좌표 정보 맵
if (node && isInViewport(node)) {
// 뷰포트 내에 있을 때만 렌더링
ctx.fillStyle = data.value > threshold ? 'red' : 'green';
ctx.beginPath();
ctx.arc(node.x, node.y, 5, 0, Math.PI * 2);
ctx.fill();
// 툴팁 데이터 업데이트
updateTooltipData(tag, data.value);
}
}
}
// 2. 분석 진행 상태 표시 (Phase 5 병렬 처리 반영)
async function trackAnalysisProgress(taskId) {
const progressBar = document.getElementById('analysis-progress-bar');
const statusText = document.getElementById('analysis-status-text');
const pollStatus = async () => {
try {
const response = await fetch(`/api/pid/status/${taskId}`);
const data = await response.json();
// 프로그레스 바 업데이트
progressBar.style.width = `${data.progress * 100}%`;
statusText.innerText = `분석 중... ${Math.round(data.progress * 100)}% (${data.message})`;
if (data.status !== 'Completed' && data.status !== 'Failed') {
setTimeout(pollStatus, 1000); // 1초 간격 폴링
} else {
statusText.innerText = data.status === 'Completed' ? '분석 완료!' : '분석 실패';
}
} catch (e) {
statusText.innerText = '상태 조회 중 오류 발생';
}
};
pollStatus();
}
```
### 3.3 [Analysis] 흐름 방향 반영 경로 추적 (Python)
```python
import networkx as nx
def get_propagation_path_with_flow(graph, start_node):
"""
단순 descendants가 아닌, 엣지의 방향성(flow_direction)과
상태(valve_open)를 고려한 실제 영향 전파 경로 추출
"""
# 1. 유효한 엣지만 필터링 (방향이 맞고 밸브가 열려있는 경로)
valid_edges = [
(u, v, d) for u, v, d in graph.edges(data=True)
if d.get('flow_direction') == 'forward' and d.get('valve_status') == 'open'
]
filtered_graph = nx.DiGraph()
filtered_graph.add_edges_from(valid_edges)
# 2. 전파 단계별 노드 추출 (BFS)
propagation_levels = nx.single_source_shortest_path_length(filtered_graph, start_node)
# { node_id: distance } 형태로 반환하여 프론트엔드에서 색상 구분 가능하게 함
return propagation_levels
# 예: P-101에서 시작되는 실제 유체 흐름 기반 영향도 분석
impact_map = get_propagation_path_with_flow(topology_graph, "P-101")
```
---
## 🚀 4. Phase 4 완료 기준 (Definition of Done)
- [ ] P&ID 도면(Canvas) 위에 **실시간 OPC UA 값**이 정확한 좌표에 표시되며, 뷰포트 최적화가 적용되었는가?
- [ ] **SignalR 또는 Delta Update**를 통해 네트워크 부하를 최소화하며 실시간 데이터를 수신하는가?
- [ ] 병렬 처리 중인 분석 작업의 **진행 상태(Progress Bar)**가 UI에 실시간으로 반영되는가?
- [ ] 특정 노드 클릭 시 **유체 흐름 방향이 반영된 영향도 분석** 결과가 단계별 색상으로 하이라이트 되는가?
- [ ] C# 서버와 Python 엔진 간 통신에 **타임아웃 및 예외 처리**가 적용되어 시스템 안정성이 확보되었는가?
- [ ] 전체 파이프라인(`추출 $\rightarrow$ 모델링 $\rightarrow$ 매핑 $\rightarrow$ 시각화`)이 통합되어 동작하는가?

View File

@@ -1,140 +0,0 @@
# 🔌 Graph Pipeline Phase 5: MCP 서버 통합 및 고성능 병렬 아키텍처 (MCP Integration & Parallel Processing)
이 문서는 앞서 설계한 1~4단계의 Graph Pipeline을 현재 프로젝트의 **Unified MCP Server (`mcp-server/server.py`)**에 통합하는 방안을 다룹니다. 특히, 대용량 도면 처리 시 발생하는 지연과 버퍼 문제를 해결하기 위해 `PID_Parser_Plan_Revision.md`의 **분산 처리 기법**과 vLLM의 **Continuous Batching** 특성을 극대화한 병렬 아키텍처를 적용합니다.
---
## 🏗️ 1. 통합 아키텍처 설계
### 1.1 고성능 병렬 데이터 흐름 (Parallel End-to-End Flow)
단일 순차 요청 방식에서 벗어나, **[전처리 $\rightarrow$ 병렬 분산 추출 $\rightarrow$ 통합 후처리]** 구조로 전환합니다.
`Frontend (UI)` $\rightarrow$ `C# Server (API)` $\rightarrow$ `MCP Server (Orchestrator)` $\rightarrow$ `Parallel Worker Tools (vLLM Batching)` $\rightarrow$ `Result Aggregator` $\rightarrow$ `C# Server`
1. **요청:** 사용자가 UI에서 도면 분석 시작 버튼 클릭.
2. **전처리 (Orchestrator):** MCP 서버가 DXF를 로드하여 기하학적 데이터를 추출하고, 분석 대상(Transmitter, Valve, Pump 등)별로 데이터를 분할합니다.
3. **병렬 호출 (Continuous Batching):**
* 분할된 데이터를 기반으로 여러 개의 MCP 툴(또는 동일 툴의 다중 요청)을 **동시에(Asynchronously)** 호출합니다.
* vLLM 서버는 이 다수의 요청을 **Continuous Batching**으로 묶어 처리함으로써, 개별 요청 시보다 전체 처리량(Throughput)을 획기적으로 높입니다.
4. **통합 및 저장 (Aggregator):** 각 분산 툴이 반환한 결과를 취합하여 최종 위상 그래프를 구축하고 DB에 저장합니다.
### 1.2 MCP 서버 내 역할 분담 (분산 처리 모델)
`PID_Parser_Plan_Revision.md`를 반영하여, 기능을 세분화하고 병렬 실행 가능하게 설계합니다.
| 구분 | MCP Tool / Module | 역할 | 병렬 처리 전략 |
|---|---|---|---|
| **Orchestrator** | `orchestrate_pid_pipeline` | 전체 공정 제어, 데이터 분할 및 결과 취합 | Asyncio 기반 비동기 제어 |
| **Worker 1** | `extract_transmitters` | FIT, FT, LT, PT, TE 추출 | vLLM Batching 요청 |
| **Worker 2** | `extract_valves` | FCV, LCV, TCV, PCV, XV 추출 | vLLM Batching 요청 |
| **Worker 3** | `extract_gauges` | PG, TG, LG 추출 | vLLM Batching 요청 |
| **Worker 4** | `extract_equipment` | Column, Tank, Filter, Drum, Heat Exchanger 등 추출 | vLLM Batching 요청 |
| **Worker 5** | `extract_pumps` | P-xxxx, VP-xxxx 추출 | vLLM Batching 요청 |
| **Analyzer** | `analyze_pid_impact` | 구축된 그래프 기반 영향도 분석 | Graph Algorithm (CPU) |
---
## 💻 2. MCP 서버 통합 구현 가이드
### 2.1 비동기 병렬 처리 설계 (Asyncio + vLLM Batching)
`FastMCP` 환경에서 `asyncio.gather`를 사용하여 여러 추출 툴을 동시에 호출함으로써 vLLM의 Continuous Batching 효율을 극대화합니다.
```python
# mcp-server/server.py 통합 설계 (개념 코드)
import asyncio
from typing import List
async def run_parallel_extraction(geo_data):
"""
분류별 추출 툴을 병렬로 호출하여 vLLM Batching 유도
"""
# 각 분류별 프롬프트와 데이터 준비
tasks = [
extract_transmitters_async(geo_data),
extract_valves_async(geo_data),
extract_gauges_async(geo_data),
extract_equipment_async(geo_data),
extract_pumps_async(geo_data)
]
# 동시에 요청을 던져 vLLM이 내부적으로 Batch 처리하게 함
results = await asyncio.gather(*tasks)
return results
@mcp.tool()
async def build_pid_graph_parallel(filepath: str) -> str:
"""
분산 처리 기법을 적용한 P&ID 그래프 생성 툴
"""
# 1. 전처리 (Phase 1)
extractor = PidGeometricExtractor(filepath)
geo_data = extractor.extract_and_save("shared_geo_data.json") # 파일 기반 공유 저장소 활용
# 실제 구현 시 geo_data는 파일 경로 또는 로드된 JSON 리스트
# 2. 병렬 분산 추출 (vLLM Batching 활용)
# 각 Worker 툴들이 LLM에 요청을 보낼 때 vLLM이 이를 묶어서 처리함
extracted_parts = await run_parallel_extraction(geo_data)
# 3. 결과 통합 및 위상 모델링 (Phase 2)
# extracted_parts는 각 Worker(Transmitter, Valve 등)가 반환한 매핑 결과 리스트
all_tags = flatten_results(extracted_parts)
builder = PidTopologyBuilder(geo_data, all_extracted_tags=all_tags)
builder.build_graph()
# 4. 저장
graph_id = os.path.basename(filepath).replace(".dxf", "_graph.json")
nx.write_graphml(builder.G, f"storage/{graph_id}")
return json.dumps({"success": True, "graph_id": graph_id, "nodes": builder.G.number_of_nodes()})
```
### 2.2 C# 서버와의 인터페이스 (`McpClient` 활용)
C# 서버는 `src/Infrastructure/Mcp/McpClient.cs`를 통해 위 툴들을 호출합니다.
### 2.2 C# 서버와의 인터페이스 (`McpClient` 활용)
C# 서버는 `src/Infrastructure/Mcp/McpClient.cs`를 통해 위 툴들을 호출합니다.
```csharp
// src/Core/Application/Services/PidGraphService.cs (신규 서비스)
public async Task<ImpactResult> GetImpactAnalysisAsync(string graphId, string nodeId)
{
var request = new McpToolRequest {
ToolName = "analyze_pid_impact",
Arguments = new { graph_id = graphId, start_node_id = nodeId }
};
var jsonResponse = await _mcpClient.CallToolAsync(request);
return JsonSerializer.Deserialize<ImpactResult>(jsonResponse);
}
```
---
## 🛠️ 3. 프로그램 구성 및 배포 전략
### 3.1 디렉토리 구조 확장
```text
mcp-server/
├── server.py # MCP 메인 서버 (툴 정의)
├── pipeline/ # Graph Pipeline 핵심 로직 (Phase 1~4)
│ ├── __init__.py
│ ├── extractor.py # Phase 1: Geometric Extraction
│ ├── topology.py # Phase 2: Topology Modeling
│ ├── mapper.py # Phase 3: Intelligent Mapping
│ └── analyzer.py # Phase 4: Impact Analysis
└── storage/ # 생성된 그래프 파일 (.graphml) 저장소
```
### 3.2 실행 프로세스
1. **MCP 서버 기동:** `python mcp-server/server.py --http` (포트 5001)
2. **C# 서버 기동:** `dotnet run` (포트 5000)
3. **통신:** C# 서버 $\xrightarrow{HTTP/JSON}$ MCP 서버 $\xrightarrow{Python\ Libs}$ 결과 반환.
---
## 🚀 4. 최종 완료 기준 (Definition of Done)
- [ ] `mcp-server/server.py``build_pid_graph`, `analyze_pid_impact` 등 핵심 툴이 정의되었는가?
- [ ] Phase 1~4의 Python 로직이 `mcp-server/pipeline/` 모듈로 구조화되어 통합되었는가?
- [ ] C# `McpClient`를 통해 MCP 서버의 그래프 분석 툴을 호출하고 결과를 수신할 수 있는가?
- [ ] 도면 업로드 $\rightarrow$ 그래프 생성 $\rightarrow$ 태그 매핑 $\rightarrow$ 영향도 분석으로 이어지는 **End-to-End 파이프라인**이 완성되었는가?
- [ ] 모든 과정이 `json_response=True``stateless_http=True` 설정 하에 안정적으로 동작하는가?

File diff suppressed because it is too large Load Diff

View File

@@ -1,83 +0,0 @@
P&ID 도면 분석을 고도화하여 **Graph Pipeline**을 구축하는 것은 단순한 텍스트 추출을 넘어, 설비 간의 **연결성(Connectivity)**과 **위상(Topology)**을 이해하는 것을 의미합니다.
제가 이 작업을 수행한다면, 다음과 같은 **4단계 전략**으로 접근하여 효율성을 극대화하겠습니다.
---
### 1. 데이터 추출 단계: "단순 텍스트 $\rightarrow$ 기하학적 객체"
현재의 텍스트 기반 추출에서 벗어나, 객체의 **좌표(Coordinate)**와 **속성(Property)**을 보존하는 구조로 변경해야 합니다.
* **객체 중심 파싱:** DXF의 Entity(Line, Circle, Text, Polyline)를 개별 객체로 인식하고, 각 객체의 중심점과 경계 상자(Bounding Box)를 저장합니다.
* **심볼 라이브러리 구축:** 밸브, 펌프, 탱크 등 반복되는 심볼의 기하학적 패턴을 정의하여, 텍스트가 없어도 "이 모양은 밸브다"라고 인식하는 패턴 매칭 로직을 도입합니다.
* **OCR 고도화:** PDF의 경우, 단순 텍스트 추출이 아닌 영역 기반 OCR을 통해 텍스트의 물리적 위치를 정확히 파악하여 인접한 심볼과 연결합니다.
### 2. 그래프 모델링 단계: "객체 $\rightarrow$ 노드 및 엣지"
추출된 객체들을 기반으로 **Knowledge Graph**를 생성합니다.
* **노드(Node):** 설비(Equipment), 계기(Instrument), 태그(Tag)를 노드로 정의합니다.
* **엣지(Edge):** 배관(Line)을 엣지로 정의합니다.
* **연결성 판단:** `Line`의 끝점이 `Equipment`의 경계 상자 내에 있거나 매우 근접해 있다면 두 노드를 연결된 것으로 간주합니다.
* **방향성 부여:** 화살표 심볼이나 공정 흐름(Flow)을 분석하여 엣지에 방향성을 부여합니다.
* **계층 구조 생성:** `Unit $\rightarrow$ Equipment $\rightarrow$ Component $\rightarrow$ Tag` 순의 계층적 그래프 구조를 설계합니다.
### 3. 지능형 매핑 및 검증 단계: "도면 $\rightarrow$ 실제 데이터"
그래프 구조를 활용해 Experion 시스템의 실제 태그와 정밀하게 매핑합니다.
* **맥락 기반 매핑 (Contextual Mapping):** 단순히 이름이 비슷한 태그를 찾는 것이 아니라, "펌프 P-101 옆에 있는 PT-101은 P-101의 압력 전송기일 확률이 높다"는 그래프 상의 인접성을 활용합니다.
* **상호 검증 (Cross-Validation):**
* 도면 상의 연결 관계(P-101 $\rightarrow$ V-101)와 실제 공정 데이터의 상관관계(P-101 가동 시 V-101 유량 변화)를 비교하여 매핑의 정확도를 검증합니다.
* **LLM 기반 추론:** 모호한 태그명이나 누락된 정보는 MCP 서버를 통해 LLM이 도면의 맥락과 R530 문서를 분석하여 최적의 매핑 후보를 추천하게 합니다.
### 4. 활용 및 시각화 단계: "분석 $\rightarrow$ 인사이트"
구축된 그래프를 통해 운영자에게 실질적인 가치를 제공합니다.
* **영향도 분석 (Impact Analysis):** 특정 밸브(V-101)가 고장 났을 때, 그래프 탐색(BFS/DFS)을 통해 하류(Downstream)에 영향을 받는 모든 설비와 태그를 즉시 식별합니다.
* **디지털 트윈 뷰:** P&ID 도면 위에 실시간 OPC UA 값을 오버레이하여, 도면을 보면서 현재 공정 상태를 한눈에 파악하는 인터페이스를 구현합니다.
* **이상 징후 전파 경로 추적:** 특정 태그에서 알람이 발생했을 때, 그래프를 역추적하여 근본 원인(Root Cause)이 될 가능성이 높은 상류 설비를 추천합니다.
---
### 🚀 효율적인 실행을 위한 로드맵 (Priority)
1. **Short-term (Quick Win):** DXF 파서 수정 $\rightarrow$ 객체 좌표 저장 $\rightarrow$ 단순 인접성 기반 태그-설비 매핑.
2. **Mid-term (Core):** 심볼 패턴 매칭 도입 $\rightarrow$ 배관(Line) 기반의 그래프 구조(NetworkX 등 활용) 구축.
3. **Long-term (Advanced):** LLM 기반의 도면-데이터 추론 엔진 통합 $\rightarrow$ 실시간 데이터 오버레이 UI 구현.
이렇게 **[기하학적 추출 $\rightarrow$ 위상 모델링 $\rightarrow$ 맥락적 매핑 $\rightarrow$ 운영 인사이트]** 순으로 확장하는 것이 가장 리스크가 적고 효율적인 방법이라고 생각합니다.
---
### 🛠️ 단계별 설계 구조 및 실행 코딩 가이드
로드맵의 각 단계를 실제로 구현하기 위한 기술적 설계 구조와 핵심 코딩 전략입니다.
#### 1단계: 기하학적 데이터 추출 (Geometric Extraction)
* **설계 구조:** `DXF Parser` $\rightarrow$ `Entity Mapper` $\rightarrow$ `Spatial Database (JSON/SQL)`
* **실행 코딩 전략:**
* **좌표 보존:** `ezdxf`를 사용하여 모든 `TEXT`, `LINE`, `CIRCLE`, `LWPOLYLINE`의 시작/끝점 및 중심 좌표를 추출하여 저장합니다.
* **Bounding Box 계산:** 각 텍스트와 심볼의 최소/최대 X, Y 좌표를 계산하여 `Rect` 객체로 관리합니다.
* **데이터 구조:**
```json
{ "id": "entity_1", "type": "TEXT", "value": "P-101", "bbox": {"x1": 10, "y1": 20, "x2": 15, "y2": 25} }
```
#### 2단계: 위상 모델링 (Topology Modeling)
* **설계 구조:** `Spatial Join` $\rightarrow$ `Graph Constructor` $\rightarrow$ `NetworkX Graph`
* **실행 코딩 전략:**
* **인접성 판단 (Proximity Search):** 텍스트 노드와 가장 가까운 심볼/라인을 찾기 위해 `KD-Tree` 또는 `R-Tree` 알고리즘을 사용합니다.
* **연결성 추론:** `Line`의 끝점이 `Equipment`의 Bounding Box 내에 포함되는지 확인하여 엣지(Edge)를 생성합니다.
* **그래프 구축:** Python의 `NetworkX` 라이브러리를 사용하여 `G.add_node(equipment)` 및 `G.add_edge(eq1, eq2, relation='pipe')` 형태로 모델링합니다.
#### 3단계: 맥락적 매핑 (Contextual Mapping)
* **설계 구조:** `Graph Traversal` $\rightarrow$ `Tag Candidate Search` $\rightarrow$ `LLM Validator`
* **실행 코딩 전략:**
* **인접 태그 탐색:** 특정 설비 노드에서 1-hop 또는 2-hop 이내에 존재하는 모든 태그 노드를 수집합니다.
* **매핑 스코어링:** `(이름 유사도 * 0.4) + (위상적 인접도 * 0.6)`와 같은 가중치 모델을 적용하여 최적의 Experion 태그를 매핑합니다.
* **LLM 검증:** 매핑 결과와 도면의 맥락을 LLM에게 전달하여 "P-101 펌프의 토출측에 PT-101이 있는 것이 공정상 타당한가?"를 검증합니다.
#### 4단계: 운영 인사이트 구현 (Operational Insight)
* **설계 구조:** `Real-time Data Stream` $\rightarrow$ `Graph Overlay` $\rightarrow$ `Impact Analysis Engine`
* **실행 코딩 전략:**
* **실시간 오버레이:** `OPC UA`로 수집된 실시간 값을 그래프 노드의 속성으로 업데이트하고, 이를 프론트엔드(Canvas/SVG)에 렌더링합니다.
* **영향도 분석:** `nx.single_source_shortest_path` 또는 `BFS`를 사용하여 특정 노드 장애 시 영향을 받는 하류(Downstream) 노드 리스트를 즉시 추출합니다.
* **루트 코즈 추적:** 알람 발생 노드로부터 상류(Upstream) 방향으로 역추적하여 이상 징후의 시작점을 식별합니다.

View File

@@ -1,19 +0,0 @@
# 현재 문제점 분석
한정된 자원의 하드웨어로 대용량의 일을 한번에 처리하려고 복잡한 프롬프트를 LLM 에게 주어 처리 시간의 지연과, 전달 및 응답 버퍼의 수신 문제발생
## 분산처리 기법 적용 및 로직 플로우
Reference Program : test_dxf_extract_pid1.py
1. Reference Program 같은 파일을 아래 5가지로 항목으로 작성하고,
- dxf_extract_transmitter.py : FIT, FT, LT, PT, TE
- dxf_extract_valve.py : FCV, LCV, TCV, PCV, XV
- dxf_extract_gague.py : PG, TG, LG
- dxf_extract_equipment.py : C-?????(Distilation Column), T-????(Tank), F-?????(Filter), D-?????(Drum,Condensor),E-?????(Heat Exchanger) B-?????(BOILER), CT-?????(COOLING TOWER), F-?????(COOLING FAN), CH-??????(CHILLER), K-?????(COMPRESSOR)
- dxf_extract_pump.py : P-10106, VP-10117
1. UI 추출시작 버튼 클릭 ->
2. 메인 프로그램 시작 -> 파일 전처리(ezdxf)- 전달 받은 데이터 보유 후
3. 1항에서 작성한 프로그램들에 , 전처리 받은 데이터 전달하여, 5개 프로그램 모두 실행
4. 처리량에 따라 실행이 끝난 서브 프로그램들은 각각의 파일에 결과를 저장하게 프로그램 되어 있으니, , 이것을 메인프로그램이 서브 프로그램들의 종료 상태가 되면, 각각 후처리 과정(데이터베이스 저장 절차)을 진행

View File

@@ -1,84 +0,0 @@
# 현재 문제점 분석
한정된 자원의 하드웨어로 대용량의 일을 한번에 처리하려고 복잡한 프롬프트를 LLM 에게 주어 처리 시간의 지연과, 전달 및 응답 버퍼의 수신 문제발생
## 분산처리 기법 적용 및 로직 플로우
1. 추출시작 버튼 클릭 ->
2. 메인 프로그램 시작 -> 파일 전처리(ezdxf)- 전달 받은 데이터 보유 후
3. 미리 작성된 - 참조 파이썬 프로그램 :test_dxf_extract_pid1.py, ~pid2,py, ~pid3.py (for loop 없애고, 단일 chunk 실행으로 변경), 이 python과 같은 파일을 아래 5가지로 프로그램에 전달 받은 데이터 전달하며, 모두 실행 시킴
- INSTRUMENTS
- dxf_extract_transmitter.py : FIT, FT, LT, PT, TE
- dxf_extract_valve.py : FCV, LCV, TCV, PCV, XV
- dxf_extract_gague.py : PG, TG, LG
- dxf_extract_equipment.py : C-?????(Distilation Column), T-????(Tank), F-?????(Filter), D-?????(Drum,Condensor),E-?????(Heat Exchanger) B-?????(BOILER), CT-?????(COOLING TOWER), F-?????(COOLING FAN), CH-??????(CHILLER), K-?????(COMPRESSOR)
- dxf_extract_pump.py : P-10106, VP-10117
3. 비동기로 실행이 끝난 서브 프로그램들은 각각의 파일에 결과가 저장될 것이고, 이것을 메인프로그램이 서브 프로그램들의 종료에 대하여 각각 후처리 과정(데이터베이스 저장 절차)을 진행
4. 위의 실증예 3개 프로그램 동시 실행시 , KV Cache 최대 사용량 30% 미만, 최대 95 token/sec, 실증됨.
5. 각각의 max context length = 65536으로 설정할것
3. INSTRUMENTS 와 SYSTEM TAG (tagname)과의 관계설정
- 예1) FICQ-10101.PV = FT-10101, FICQ-10101.OP = FCV-10101, FIQ-6115.PV = FT-6115, TI-6117.PV = TE-6117
- LATER -->예2) P-10101.PV, P-10101.OP , XV-10111.PV, XV-10111.OP(LATER : INT 2BIT, 4BIT, 8BIT ENCODER OUTPUT)
### PLANT RESOURCE , FIELD INSTRUMENTS, EQUIPMENTS MANAGEMENT TABLE 신설
4. PLANT RESOURCE : FILELD INSTRUMENTS용 데이터베이스 테이블 신설
- Instruments Table Column:
TagName,
Type:
- Flow Transmitter
- (Type_sub) : Coriollis Mass Flowmeter
- (Type_sub) : Variable Area Flowmeter
- (Type_sub) : Rotameter
- (Type_sub) : Magnetic Flowmeter
- (Type_sub) :
- (Type_sub) :
- (Type_sub) :
- (Type_sub) :
- Pressure Transmitter
- (Type_sub) : Absolute Pressure Transmitter (Vacuum)
- (Type_sub) : Gauge Pressure Transmitter
- (Type_sub) : DP Transmitter
- Level Transmitter
- (Type_sub) : Remote Sealed DP Transmitter
- (Type_sub) : Float Level Transmitter
- (Type_sub) :
- Temperature Sensor
- (Type_sub) : R.T.D (pt100)
- (Type_sub) : Thermocople type k
- (Type_sub) :
- (Type_sub) :
- Control Valve
- (Type_sub) : Globe 2 Way
- (Size) : 25A
- (Action) : Fail Close (Air to Open) / Fail Open (Air to Close)
- (Type_sub) :
- (Type_sub) :
- On-Off Valve
- (Type_sub) :
- (Type_sub) :
- Pressure Safety Vavle
- (Type_sub) :
- Pressure Relief Valve,
- (Type_sub) :
Type_sub,
Range_Max,
Range_Calibrated,
Model_No,
Installed_at,
Repaired_at,
Repair_history,
Last_Calibrated_at,
Recommended_Spare_parts
Doc_No,
TagName,
DataSheet,
Drawings,
NamePlate, : (photo),
Manual_No : (pdf file, numbering rule needed)

View File

@@ -1,104 +0,0 @@
import networkx as nx
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, List, Optional
import uvicorn
import json
import os
app = FastAPI(title="P&ID Analysis Engine")
# 전역 변수로 그래프 및 매핑 데이터 로드
TOPOLOGY_FILE = "futurePlan/End-to-End P&ID Graph Pipeline/pid_graph_topology.json"
MAPPING_FILE = "futurePlan/End-to-End P&ID Graph Pipeline/pid_final_mapping.json"
topology_graph = nx.DiGraph()
tag_mapping = {}
def load_data():
global topology_graph, tag_mapping
try:
if os.path.exists(TOPOLOGY_FILE):
with open(TOPOLOGY_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
# NetworkX 그래프 생성
for node in data.get('nodes', []):
topology_graph.add_node(node['id'], **node)
for edge in data.get('edges', []):
topology_graph.add_edge(edge['source'], edge['target'], **edge)
print(f"Successfully loaded topology from {TOPOLOGY_FILE}")
if os.path.exists(MAPPING_FILE):
with open(MAPPING_FILE, 'r', encoding='utf-8') as f:
tag_mapping = json.load(f)
print(f"Successfully loaded mapping from {MAPPING_FILE}")
except Exception as e:
print(f"Error loading data: {e}")
@app.on_event("startup")
async def startup_event():
load_data()
class ImpactRequest(BaseModel):
nodeId: str
class ImpactResult(BaseModel):
startNode: str
impactedNodes: Dict[str, int] # { nodeId: depth }
path: List[List[str]]
def get_propagation_path_with_flow(graph, start_node):
"""
엣지의 방향성(flow_direction)과 상태(valve_status)를 고려한 실제 영향 전파 경로 추출
"""
if start_node not in graph:
return {}
# 1. 유효한 엣지만 필터링 (방향이 forward이고 밸브가 open인 경로)
# 실제 데이터에 flow_direction이나 valve_status가 없을 경우를 대비해 기본값 설정
valid_edges = [
(u, v) for u, v, d in graph.edges(data=True)
if d.get('flow_direction', 'forward') == 'forward'
and d.get('valve_status', 'open') == 'open'
]
filtered_graph = nx.DiGraph()
filtered_graph.add_edges_from(valid_edges)
# 2. 전파 단계별 노드 추출 (BFS)
try:
propagation_levels = nx.single_source_shortest_path_length(filtered_graph, start_node)
return propagation_levels
except Exception:
return {}
@app.get("/impact/{nodeId}")
async def analyze_impact(nodeId: str):
if nodeId not in topology_graph:
raise HTTPException(status_code=404, detail=f"Node {nodeId} not found in topology")
impact_map = get_propagation_path_with_flow(topology_graph, nodeId)
# 경로 추출 (시각화를 위해 간단하게 모든 영향 노드로의 최단 경로 포함)
paths = []
for target in impact_map.keys():
if target != nodeId:
try:
path = nx.shortest_path(topology_graph, source=nodeId, target=target)
paths.append(path)
except nx.NetworkXNoPath:
continue
return {
"startNode": nodeId,
"impactedNodes": impact_map,
"paths": paths
}
@app.get("/health")
async def health_check():
return {"status": "healthy", "nodes": topology_graph.number_of_nodes(), "edges": topology_graph.number_of_edges()}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

View File

@@ -1,188 +0,0 @@
import ezdxf
import re
import json
from typing import List, Optional, Tuple, Union
from pydantic import BaseModel, Field
from shapely.geometry import box, Point
# --- Data Models ---
class BoundingBox(BaseModel):
min_x: float
min_y: float
max_x: float
max_y: float
center: Tuple[float, float]
class GeometricEntity(BaseModel):
entity_id: str
entity_type: str # TEXT, MTEXT, LINE, LWPOLYLINE, CIRCLE, ARC
layer: str
bbox: BoundingBox
raw_value: Optional[str] = None
clean_value: Optional[str] = None
coordinates: List[Union[Tuple[float, float], List[float]]] = Field(default_factory=list)
properties: dict = Field(default_factory=dict)
# --- Extractor Implementation ---
class PidGeometricExtractor:
def __init__(self, file_path: str):
try:
self.doc = ezdxf.readfile(file_path)
self.msp = self.doc.modelspace()
except Exception as e:
raise IOError(f"Failed to load DXF file: {e}")
def clean_text(self, text: str) -> str:
"""
DXF 특수 제어 문자 및 MTEXT 포맷팅을 제거하여 정제된 텍스트 반환.
"""
if not text:
return ""
# 1. MTEXT 포맷팅 및 제어 문자 제거 (\P, \W, \L, \A, \C, \H, \S, \T 등)
text = re.sub(r'\\([P|W|L|A|C|H|S|T])\d*;?', ' ', text)
# 2. 중괄호 { } 제거
text = re.sub(r'[\{\}]', ' ', text)
# 3. DXF 특수 제어 문자 제거 (%%U: Underline, %%O: Overline, %%S: Strikethrough, %%R: Registered)
text = re.sub(r'%%[U|O|S|R]', ' ', text)
# 4. 불필요한 특수 기호 및 반복되는 공백 정제
text = re.sub(r'\s+', ' ', text).strip()
return text
def get_bbox(self, entity) -> Optional[BoundingBox]:
"""
엔티티 타입별로 동적인 Bounding Box를 계산하여 반환.
"""
try:
if entity.dxftype() == 'TEXT':
p = entity.dxf.insert
h = entity.dxf.height
# 텍스트 길이에 따른 대략적인 너비 계산 (글자수 * 높이 * 0.6)
width = len(entity.dxf.text) * h * 0.6
return self._create_bbox(p.x, p.y, p.x + width, p.y + h)
elif entity.dxftype() == 'MTEXT':
p = entity.dxf.insert
h = entity.dxf.char_height if hasattr(entity.dxf, 'char_height') else 2.5
w = entity.dxf.width if entity.dxf.width > 0 else len(entity.text) * h * 0.6
return self._create_bbox(p.x, p.y, p.x + w, p.y + h)
elif entity.dxftype() == 'LINE':
start = entity.dxf.start
end = entity.dxf.end
return self._create_bbox(
min(start.x, end.x), min(start.y, end.y),
max(start.x, end.x), max(start.y, end.y)
)
elif entity.dxftype() == 'LWPOLYLINE':
points = entity.get_points()
if not points: return None
xs = [p[0] for p in points]
ys = [p[1] for p in points]
return self._create_bbox(min(xs), min(ys), max(xs), max(ys))
elif entity.dxftype() in ('CIRCLE', 'ARC'):
center = entity.dxf.center
radius = entity.dxf.radius
return self._create_bbox(
center.x - radius, center.y - radius,
center.x + radius, center.y + radius
)
except Exception as e:
print(f"Error calculating bbox for {entity.dxftype()} ({entity.dxf.handle}): {e}")
return None
def _create_bbox(self, min_x, min_y, max_x, max_y) -> BoundingBox:
return BoundingBox(
min_x=min_x,
min_y=min_y,
max_x=max_x,
max_y=max_y,
center=((min_x + max_x) / 2, (min_y + max_y) / 2)
)
def extract_and_save(self, output_path: str):
"""
기하학적 데이터를 추출하여 JSON 파일로 저장.
"""
results = []
for entity in self.msp:
bbox_obj = self.get_bbox(entity)
if not bbox_obj:
continue
raw_text = ""
if entity.dxftype() == 'TEXT':
raw_text = entity.dxf.text
elif entity.dxftype() == 'MTEXT':
raw_text = entity.text
# 좌표 추출 (3D 좌표를 2D로 변환)
coords = []
if hasattr(entity, 'get_points'):
# ezdxf의 get_points()는 (x, y, z) 튜플 리스트를 반환함
coords = [(p[0], p[1]) for p in entity.get_points()]
elif entity.dxftype() == 'LINE':
coords = [(entity.dxf.start.x, entity.dxf.start.y), (entity.dxf.end.x, entity.dxf.end.y)]
elif entity.dxftype() in ('CIRCLE', 'ARC'):
coords = [(entity.dxf.center.x, entity.dxf.center.y)]
entity_data = GeometricEntity(
entity_id=entity.dxf.handle,
entity_type=entity.dxftype(),
layer=entity.dxf.layer,
bbox=bbox_obj,
raw_value=raw_text if raw_text else None,
clean_value=self.clean_text(raw_text) if raw_text else None,
coordinates=coords,
properties={
"color": entity.dxf.color,
"lineweight": entity.dxf.lineweight if hasattr(entity.dxf, 'lineweight') else None,
}
)
results.append(entity_data.model_dump())
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=4)
return output_path
# --- Proximity Utilities ---
def is_near(bbox_a: BoundingBox, bbox_b: BoundingBox, threshold=5.0) -> bool:
"""
두 Bounding Box 간의 최단 거리가 임계값 이내인지 확인.
shapely box를 사용하여 거리 계산.
"""
box_a = box(bbox_a.min_x, bbox_a.min_y, bbox_a.max_x, bbox_a.max_y)
box_b = box(bbox_b.min_x, bbox_b.min_y, bbox_b.max_x, bbox_b.max_y)
return box_a.distance(box_b) <= threshold
def is_inside(point: Tuple[float, float], bbox: BoundingBox) -> bool:
"""
특정 점이 Bounding Box 내부에 있는지 확인.
"""
return (bbox.min_x <= point[0] <= bbox.max_x) and (bbox.min_y <= point[1] <= bbox.max_y)
# --- Execution Block ---
if __name__ == "__main__":
# 테스트 파일 경로 (환경에 맞게 수정)
input_dxf = "futurePlan/End-to-End P&ID Graph Pipeline/No-10_Plant_PID.dxf"
output_json = "futurePlan/End-to-End P&ID Graph Pipeline/shared_geo_data.json"
print(f"Starting extraction from {input_dxf}...")
try:
extractor = PidGeometricExtractor(input_dxf)
saved_path = extractor.extract_and_save(output_json)
print(f"Successfully saved geometric data to {saved_path}")
except Exception as e:
print(f"Extraction failed: {e}")

File diff suppressed because it is too large Load Diff

View File

@@ -1,126 +0,0 @@
import networkx as nx
import asyncio
import json
from typing import List, Optional, Dict, Any, Tuple
from pydantic import BaseModel, Field
from rapidfuzz import process, fuzz
from openai import AsyncOpenAI
# --- 응답 구조화를 위한 Pydantic 모델 ---
class MappingResult(BaseModel):
resolved_tag: str = Field(..., description="The final mapped system tag")
reason: str = Field(..., description="Reason for this mapping based on context")
confidence: float = Field(..., ge=0.0, le=1.0, description="Confidence score from 0 to 1")
class IntelligentMapper:
def __init__(self, graph: nx.Graph, system_tags: List[str], api_key: str = None):
self.graph = graph # Phase 2에서 생성된 NetworkX 그래프
self.system_tags = system_tags # Experion 시스템의 전체 태그 리스트
self.client = AsyncOpenAI(api_key=api_key) if api_key else None
def get_node_context(self, node_id: str) -> str:
"""노드의 주변 위상 정보를 텍스트로 변환"""
if not self.graph.has_node(node_id):
return "Node not found in graph"
neighbors = list(self.graph.neighbors(node_id))
context = []
for n in neighbors:
attr = self.graph.nodes[n]
val = attr.get('value', n)
typ = attr.get('type', 'Unknown')
context.append(f"Connected to {val} (Type: {typ})")
return ", ".join(context) if context else "No connected neighbors"
async def _resolve_generic(self, node_id: str, category_prompt: str) -> MappingResult:
"""공통 매핑 로직 (비동기 + 구조화 응답)"""
if not self.client:
return MappingResult(resolved_tag="UNKNOWN", reason="API Key not provided", confidence=0.0)
# Phase 2에서 'value'에 clean_value가 저장됨
node_data = self.graph.nodes.get(node_id, {})
tag_text = node_data.get('value', '')
# 1차 후보 추출 (RapidFuzz)
candidates = process.extract(tag_text, self.system_tags, scorer=fuzz.WRatio, limit=5)
context = self.get_node_context(node_id)
prompt = f"""
{category_prompt}
P&ID 도면의 태그 '{tag_text}'를 실제 시스템 태그와 매핑해야 합니다.
위상 맥락: {context}
후보 리스트: {candidates}
반드시 다음 JSON 형식으로만 응답하세요:
{{
"resolved_tag": "태그명 또는 UNKNOWN",
"reason": "매핑 이유",
"confidence": 0.0~1.0
}}
"""
try:
response = await self.client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user", "content": prompt}],
response_format={ "type": "json_object" } # JSON 모드 강제
)
raw_content = response.choices[0].message.content
# Pydantic을 통한 유효성 검사
return MappingResult.model_validate_json(raw_content)
except Exception as e:
print(f"Error resolving node {node_id}: {e}")
return MappingResult(resolved_tag="UNKNOWN", reason=f"Error: {str(e)}", confidence=0.0)
# --- 전문화된 Worker 함수들 ---
async def extract_transmitters(self, node_ids: List[str]) -> Dict[str, MappingResult]:
prompt = "당신은 계측기 전문 엔지니어입니다. 특히 Pressure/Flow/Level Transmitter 매핑에 특화되어 있습니다."
tasks = [self._resolve_generic(nid, prompt) for nid in node_ids]
results = await asyncio.gather(*tasks)
return dict(zip(node_ids, results))
async def extract_valves(self, node_ids: List[str]) -> Dict[str, MappingResult]:
prompt = "당신은 밸브 및 액추에이터 전문 엔지니어입니다. 밸브의 개폐 상태 및 제어 태그 매핑에 특화되어 있습니다."
tasks = [self._resolve_generic(nid, prompt) for nid in node_ids]
results = await asyncio.gather(*tasks)
return dict(zip(node_ids, results))
async def extract_equipment(self, node_ids: List[str]) -> Dict[str, MappingResult]:
prompt = "당신은 공정 설비 전문 엔지니어입니다. 펌프, 탱크, 열교환기 등의 메인 설비 태그 매핑에 특화되어 있습니다."
tasks = [self._resolve_generic(nid, prompt) for nid in node_ids]
results = await asyncio.gather(*tasks)
return dict(zip(node_ids, results))
def validate_mapping(resolved_tag: str, symbol_type: str, tag_metadata: Dict[str, Any]) -> Tuple[bool, str]:
"""심볼 타입과 실제 태그 메타데이터의 엄격한 일치 여부 검증"""
if resolved_tag == "UNKNOWN":
return False, "Tag not resolved"
# 단순 키워드가 아닌 허용 단위(Unit) 정의
unit_map = {
"Pressure Transmitter": ["bar", "psi", "kPa", "Pa"],
"Flow Meter": ["m3/h", "lpm", "kg/h"],
"Temperature Sensor": ["°C", "C", "K", "°F"]
}
actual_unit = tag_metadata.get('unit', '').strip()
allowed_units = unit_map.get(symbol_type, [])
# 1. 단위 일치 확인 (최우선)
if actual_unit and actual_unit in allowed_units:
return True, "Unit Match"
# 2. 단위가 없는 경우 설명(Description) 기반 2차 검증
actual_desc = tag_metadata.get('description', '').lower()
expected_keywords = {
"Pressure Transmitter": ["pressure", "press"],
"Flow Meter": ["flow", "flowrate"],
"Temperature Sensor": ["temp", "temperature"]
}
keywords = expected_keywords.get(symbol_type, [])
if any(kw in actual_desc for kw in keywords):
return True, "Description Match (Unit Missing)"
return False, "Mismatch: Symbol type and Tag metadata do not align"

View File

@@ -1,190 +0,0 @@
import networkx as nx
from shapely.geometry import box, Point, LineString
import json
from typing import List, Dict, Any, Optional, Tuple
class PidTopologyBuilder:
def __init__(self, geometric_data: List[Dict[str, Any]], all_extracted_tags: Optional[List[Dict[str, Any]]] = None, config: Optional[Dict[str, float]] = None):
"""
- geometric_data: Phase 1에서 추출된 기하학적 데이터 (List of dicts)
- all_extracted_tags: 통합된 태그 리스트
- config: {'dist_threshold': 50.0, 'tag_threshold': 100.0} 등 설정값
"""
self.data = geometric_data
self.all_tags = all_extracted_tags if all_extracted_tags else []
if config:
self.config = config
else:
try:
with open('futurePlan/End-to-End P&ID Graph Pipeline/topology_config.json', 'r') as f:
self.config = json.load(f)
except Exception:
self.config = {'dist_threshold': 50.0, 'tag_threshold': 100.0, 'merge_threshold': 2.0}
self.G = nx.DiGraph() # 방향성 그래프 생성
def build_graph(self):
# 1. 노드 병합 및 추가 (Merging)
self.merged_data = self._merge_nodes()
for item in self.merged_data:
bbox_vals = item['bbox']
bbox_geom = box(bbox_vals['min_x'], bbox_vals['min_y'], bbox_vals['max_x'], bbox_vals['max_y'])
self.G.add_node(item['entity_id'],
type=item['entity_type'],
bbox=bbox_geom,
value=item.get('clean_value'),
layer=item.get('layer'))
# 2. 분산 추출된 태그 통합 및 노드 추가
for tag in self.all_tags:
bbox_vals = tag['bbox']
bbox_geom = box(bbox_vals['min_x'], bbox_vals['min_y'], bbox_vals['max_x'], bbox_vals['max_y'])
self.G.add_node(tag['entity_id'],
type='TEXT',
bbox=bbox_geom,
value=tag.get('clean_value') or tag.get('tagName'))
# 3. 태그-설비 논리적 연결 (Association)
tags = [n for n, d in self.G.nodes(data=True) if d['type'] == 'TEXT']
equipments = [n for n, d in self.G.nodes(data=True) if d['type'] not in ['TEXT', 'LINE', 'LWPOLYLINE']]
for tag in tags:
best_match = self._find_nearest_equipment(tag, equipments)
if best_match:
self.G.add_edge(tag, best_match, relation='associated_with')
# 4. 배관 기반 물리적 연결 (Pipe) [개선: Proximity 기반]
lines = [n for n, d in self.G.nodes(data=True) if d['type'] in ['LINE', 'LWPOLYLINE']]
for line_id in lines:
# 저장된 merged_data에서 coordinates 찾기
original_item = next((item for item in self.merged_data if item['entity_id'] == line_id), None)
if not original_item:
original_item = next((item for item in self.data if item['entity_id'] == line_id), None)
if not original_item or not original_item.get('coordinates'):
continue
coords = original_item['coordinates']
line_geom = LineString(coords)
connected_nodes = []
for eq_id in equipments:
eq_bbox = self.G.nodes[eq_id]['bbox']
# End-point뿐만 아니라 Line 전체와 BBox 간의 최단 거리 측정
if line_geom.distance(eq_bbox) < self.config['dist_threshold']:
connected_nodes.append(eq_id)
# 중복 제거
connected_nodes = list(set(connected_nodes))
if len(connected_nodes) >= 2:
# 방향성 추론 (단순화: 첫 번째 -> 두 번째)
self.G.add_edge(connected_nodes[0], connected_nodes[1], relation='pipe')
elif len(connected_nodes) == 1:
# 단일 연결 노드 처리 (나중에 분석용)
pass
def _find_nearest_equipment(self, tag_id, equipment_ids):
tag_bbox = self.G.nodes[tag_id]['bbox']
min_dist = float('inf')
nearest = None
for eq_id in equipment_ids:
eq_bbox = self.G.nodes[eq_id]['bbox']
dist = tag_bbox.distance(eq_bbox)
if dist < min_dist:
min_dist = dist
nearest = eq_id
return nearest if min_dist < self.config['tag_threshold'] else None
def validate_topology(self):
"""위상 무결성 검증"""
isolated = list(nx.isolates(self.G))
return {
"isolated_nodes": isolated,
"node_count": self.G.number_of_nodes(),
"edge_count": self.G.number_of_edges()
}
def _merge_nodes(self) -> List[Dict[str, Any]]:
"""기하학적으로 거의 동일한 노드들을 병합하여 그래프 단순화"""
if not self.data:
return []
merge_threshold = self.config.get('merge_threshold', 2.0)
merged = []
visited = set()
for i in range(len(self.data)):
if i in visited:
continue
current = self.data[i]
current_bbox = box(*(current['bbox']['min_x'], current['bbox']['min_y'], current['bbox']['max_x'], current['bbox']['max_y']))
# 동일 타입이면서 BBox 거리가 매우 가까운 노드들 탐색
cluster = [current]
visited.add(i)
for j in range(i + 1, len(self.data)):
if j in visited:
continue
target = self.data[j]
if target['entity_type'] != current['entity_type']:
continue
target_bbox = box(*(target['bbox']['min_x'], target['bbox']['min_y'], target['bbox']['max_x'], target['bbox']['max_y']))
if current_bbox.distance(target_bbox) < merge_threshold:
cluster.append(target)
visited.add(j)
# 클러스터 대표값 설정 (첫 번째 노드 기준, BBox는 합집합으로 확장)
if len(cluster) > 1:
# BBox 합집합 계산
min_x = min(c['bbox']['min_x'] for c in cluster)
min_y = min(c['bbox']['min_y'] for c in cluster)
max_x = max(c['bbox']['max_x'] for c in cluster)
max_y = max(c['bbox']['max_y'] for c in cluster)
representative = cluster[0].copy()
representative['bbox'] = {'min_x': min_x, 'min_y': min_y, 'max_x': max_x, 'max_y': max_y}
# 병합된 원본 ID 리스트 저장
representative['merged_ids'] = [c['entity_id'] for c in cluster]
merged.append(representative)
else:
merged.append(current)
return merged
def save_graph(self, output_path: str):
"""그래프 구조를 JSON 형태로 저장 (NetworkX의 node_link_data 활용) {
"nodes": [...],
"links": [...]
}"""
from networkx.readwrite import json_graph
data = json_graph.node_link_data(self.G)
# shapely geometry 객체는 JSON 직렬화가 안 되므로 변환
for node in data['nodes']:
if 'bbox' in node:
bbox = node['bbox']
node['bbox'] = {
'min_x': bbox.bounds[0],
'min_y': bbox.bounds[1],
'max_x': bbox.bounds[2],
'max_y': bbox.bounds[3]
}
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
return output_path
def analyze_impact(graph, start_node):
"""특정 설비 장애 시 하류(Downstream)에 영향을 받는 모든 노드 추출"""
if start_node not in graph:
return []
# BFS를 통해 도달 가능한 모든 노드 탐색
impacted_nodes = nx.descendants(graph, start_node)
return list(impacted_nodes)

File diff suppressed because it is too large Load Diff

View File

@@ -1,61 +0,0 @@
import json
import sys
import os
# 경로 설정을 위해 현재 파일의 디렉토리를 sys.path에 추가
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
from pid_geometric_extractor import PidGeometricExtractor
from pid_topology_builder import PidTopologyBuilder, analyze_impact
def run_pipeline():
# 1. 경로 설정 (현재 디렉토리 기준 상대 경로)
input_dxf = os.path.join(current_dir, "No-10_Plant_PID.dxf")
geo_json_path = os.path.join(current_dir, "shared_geo_data.json")
graph_json_path = os.path.join(current_dir, "pid_graph_topology.json")
print("--- Phase 1: Geometric Extraction ---")
try:
extractor = PidGeometricExtractor(input_dxf)
extractor.extract_and_save(geo_json_path)
print(f"Geometric data saved to {geo_json_path}")
except Exception as e:
print(f"Phase 1 failed: {e}")
return
print("\n--- Phase 2: Topology Modeling ---")
try:
with open(geo_json_path, 'r', encoding='utf-8') as f:
geometric_data = json.load(f)
# 테스트를 위해 all_extracted_tags는 빈 리스트로 전달
# config를 None으로 전달하여 topology_config.json 설정을 사용하도록 함
builder = PidTopologyBuilder(
geometric_data=geometric_data,
all_extracted_tags=[],
config=None
)
builder.build_graph()
# 위상 검증
validation = builder.validate_topology()
print(f"Topology Validation: {validation}")
# 그래프 저장
builder.save_graph(graph_json_path)
print(f"Graph topology saved to {graph_json_path}")
# 영향도 분석 테스트 (노드가 존재하는 경우)
if validation['node_count'] > 0:
sample_node = list(builder.G.nodes())[0]
impacted = analyze_impact(builder.G, sample_node)
print(f"Impact analysis for node {sample_node}: {impacted}")
except Exception as e:
print(f"Phase 2 failed: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
run_pipeline()

View File

@@ -1,134 +0,0 @@
import json
import sys
import os
import asyncio
import networkx as nx
# 경로 설정을 위해 현재 파일의 디렉토리를 sys.path에 추가
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(current_dir)
from pid_geometric_extractor import PidGeometricExtractor
from pid_topology_builder import PidTopologyBuilder
from pid_intelligent_mapper import IntelligentMapper, validate_mapping
async def run_full_pipeline():
# 1. 경로 설정
input_dxf = os.path.join(current_dir, "No-10_Plant_PID.dxf")
geo_json_path = os.path.join(current_dir, "shared_geo_data.json")
graph_json_path = os.path.join(current_dir, "pid_graph_topology.json")
mapping_result_path = os.path.join(current_dir, "pid_final_mapping.json")
# --- Phase 1: Geometric Extraction ---
print("\n--- Phase 1: Geometric Extraction ---")
try:
extractor = PidGeometricExtractor(input_dxf)
extractor.extract_and_save(geo_json_path)
print(f"Geometric data saved to {geo_json_path}")
except Exception as e:
print(f"Phase 1 failed: {e}")
return
# --- Phase 2: Topology Modeling ---
print("\n--- Phase 2: Topology Modeling ---")
try:
with open(geo_json_path, 'r', encoding='utf-8') as f:
geometric_data = json.load(f)
builder = PidTopologyBuilder(
geometric_data=geometric_data,
all_extracted_tags=[],
config={'dist_threshold': 50.0, 'tag_threshold': 100.0}
)
builder.build_graph()
builder.save_graph(graph_json_path)
print(f"Graph topology saved to {graph_json_path}")
except Exception as e:
print(f"Phase 2 failed: {e}")
return
# --- Phase 3: Intelligent Mapping ---
print("\n--- Phase 3: Intelligent Mapping ---")
try:
# 1. 그래프 로드
with open(graph_json_path, 'r', encoding='utf-8') as f:
graph_data = json.load(f)
# NetworkX 그래프 복원 (node_link_data 형식 대응)
from networkx.readwrite import json_graph
G = json_graph.node_link_graph(graph_data)
# 2. 시스템 태그 리스트 (실제로는 API나 DB에서 가져와야 함)
# 테스트를 위한 가상 태그 리스트
system_tags = [
"PT-101.PV", "PT-102.PV", "FT-201.PV", "LT-301.PV",
"P-101.STATUS", "P-101.SPEED", "V-101.OPEN", "V-101.CLOSE",
"T-101.TEMP", "TK-101.LEVEL"
]
# 3. 매퍼 초기화 (API Key는 환경변수나 설정파일에서 가져오는 것을 권장)
api_key = os.getenv("OPENAI_API_KEY", "your-api-key-here")
mapper = IntelligentMapper(G, system_tags, api_key=api_key)
# 4. 노드 분류 및 매핑 실행
nodes = list(G.nodes())
transmitter_nodes = [n for n in nodes if "Transmitter" in G.nodes[n].get('type', '')]
valve_nodes = [n for n in nodes if "Valve" in G.nodes[n].get('type', '')]
equipment_nodes = [n for n in nodes if "Equipment" in G.nodes[n].get('type', '') or "Pump" in G.nodes[n].get('type', '')]
print(f"Mapping {len(transmitter_nodes)} transmitters, {len(valve_nodes)} valves, {len(equipment_nodes)} equipment...")
# 비동기 실행
results = await asyncio.gather(
mapper.extract_transmitters(transmitter_nodes),
mapper.extract_valves(valve_nodes),
mapper.extract_equipment(equipment_nodes)
)
# 결과 통합
final_mapping_raw = {}
for res in results:
final_mapping_raw.update(res)
# 5. 검증 및 최종 결과 정리
# 가상 메타데이터 (실제로는 시스템에서 조회)
mock_metadata = {
"PT-101.PV": {"unit": "bar", "description": "Pressure Transmitter 101"},
"P-101.STATUS": {"unit": "", "description": "Pump 101 Status"},
}
final_results = []
for node_id, mapping in final_mapping_raw.items():
symbol_type = G.nodes[node_id].get('type', 'Unknown')
tag = mapping.resolved_tag
meta = mock_metadata.get(tag, {"unit": "", "description": ""})
is_valid, val_msg = validate_mapping(tag, symbol_type, meta)
final_results.append({
"node_id": node_id,
"symbol_type": symbol_type,
"original_text": G.nodes[node_id].get('value', ''),
"resolved_tag": tag,
"confidence": mapping.confidence,
"reason": mapping.reason,
"validation": {
"is_valid": is_valid,
"message": val_msg
}
})
# 6. 결과 저장
with open(mapping_result_path, 'w', encoding='utf-8') as f:
json.dump(final_results, f, indent=4, ensure_ascii=False)
print(f"Final mapping results saved to {mapping_result_path}")
print(f"Successfully mapped {len(final_results)} nodes.")
except Exception as e:
print(f"Phase 3 failed: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(run_full_pipeline())

View File

@@ -1,5 +0,0 @@
{
"dist_threshold": 20.0,
"tag_threshold": 60.0,
"merge_threshold": 2.0
}

View File

@@ -1,718 +0,0 @@
오전 3:39Claude 응답: 벡터 PDF면 일이 훨씬 수월합니다.벡터 PDF면 일이 훨씬 수월합니다. OCR 안 거치고 텍스트를 정확하게 뽑아낼 수 있어서 정확도 90%+ 가능한 영역입니다.
권장 파이프라인 (벡터 PDF 한정)
1단계 - PyMuPDF로 텍스트 + 좌표 추출
pythonimport fitz # PyMuPDF
doc = fitz.open("pid.pdf")
for page in doc:
blocks = page.get_text("dict")["blocks"]
for b in blocks:
for line in b.get("lines", []):
for span in line["spans"]:
# span["text"], span["bbox"], span["size"], span["font"]
get_text("dict")가 핵심입니다. 각 텍스트 조각마다 bbox(x0,y0,x1,y1), 폰트, 크기, 회전각까지 다 나옵니다. P&ID에서 수직으로 회전된 라벨도 dir 필드로 잡힙니다.
2단계 - 도면 영역 vs 타이틀블록 분리
P&ID는 보통 우측 하단에 타이틀블록(도면번호, 리비전, 작성자)이 있고, 나머지가 도면 영역입니다. 페이지 크기 기준으로 y > 0.85 * page_height and x > 0.7 * page_width 같은 식으로 ROI를 나눠놓으면 나중에 LLM이 "도면번호를 장비태그로 오인"하는 사고를 막을 수 있습니다.
3단계 - 태그 정규식 1차 필터
ISA-5.1 + 보통 플랜트에서 쓰는 패턴들:
pythonTAG_PATTERNS = [
r'^[A-Z]{1,4}-\d{3,5}[A-Z]?$', # P-101A, FT-2034
r'^\d{2,3}-[A-Z]{1,4}-\d{3,5}[A-Z]?$', # 10-P-101A (유닛번호 포함)
r'^[A-Z]{2,4}\d{3,5}$', # PT101 (하이픈 없는 변형)
]
회사마다 명명 규칙이 다르니 실제 도면 한 장 보고 보정해야 합니다.
4단계 - 공간 클러스터링으로 "장비-태그-속성" 묶기
장비 태그(P-101A)와 그 아래/옆에 적힌 설명(CENTRIFUGAL PUMP, 100 GPM @ 50m)은 보통 50~100픽셀 이내에 모여있습니다. scipy의 DBSCAN으로 묶으면 깔끔합니다:
pythonfrom sklearn.cluster import DBSCAN
import numpy as np
centers = np.array([[(b[0]+b[2])/2, (b[1]+b[3])/2] for b in bboxes])
clusters = DBSCAN(eps=80, min_samples=1).fit(centers)
eps는 도면 스케일 보고 조정하시면 됩니다.
5단계 - Qwen3-Coder에 구조화 입력
json{
"page": 3,
"drawing_no": "P-1234-001",
"clusters": [
{
"id": "c1",
"texts": ["P-101A", "CENTRIFUGAL PUMP", "100 GPM", "50m HEAD"],
"bbox": [1200, 800, 1350, 920]
},
{
"id": "c2",
"texts": ["V-301", "STORAGE TANK", "5000L"],
"bbox": [400, 600, 580, 780]
}
]
}
프롬프트에서 "각 cluster를 하나의 장비로 보고 tag, equipment_type, specs 필드로 정규화하라"고 시키면 환각 거의 없이 잘 뽑습니다.
추가 팁
라인/심볼은 별도 트랙으로: page.get_drawings()로 벡터 라인/원/사각형도 추출됩니다. 펌프 심볼(원형) 위치와 텍스트 위치를 매칭하면 "이 텍스트가 어떤 심볼에 붙어있는지"까지 알 수 있는데, 이건 phase 2로 미루시는 걸 권장합니다. 텍스트만으로도 충분히 가치 있는 결과가 나옵니다.
좌표계 주의: PyMuPDF는 좌상단이 (0,0)이고 y가 아래로 증가합니다. CAD 출신이면 헷갈리실 수 있어서 미리 말씀드립니다.
검증 데이터 만들기: 도면 5~10장을 수동으로 라벨링해놓고 추출 결과랑 비교하는 회귀 테스트를 만들어두시면, 나중에 다른 EPC사 도면 들어왔을 때 정규식이나 eps 값 튜닝 빠르게 됩니다.
RAG 연결 시: 추출된 {tag, equipment_type, specs, drawing_no, page} 구조를 그대로 메타데이터로 쓰면 "10-P-101A 펌프 사양 알려줘" 같은 쿼리에 정확히 답할 수 있습니다. 청크는 텍스트 덩어리가 아니라 cluster 단위로 만드는 게 핵심입니다.
C# 운영 환경 고려하면, 이 전처리 파이프라인은 Python FastAPI로 띄우고 .NET OPC UA 쪽에서 HTTP로 호출하는 구조를 추천드립니다. PyMuPDF는 AGPL이라 상용 배포 시 라이선스 확인하시거나 Artifex 상용 라이선스 구매 옵션도 있다는 점은 미리 체크해두시면 좋습니다.
서버 잘 구성하셨네요. 그런데 결론부터 말씀드리면 여기에 그대로 PDF 전처리를 붙이는 건 비추천입니다. 이유와 권장 구조를 짚어드리겠습니다.
현재 extract_pid_tags의 문제점
extract_pid_tags(text, source_type)는 이미 텍스트가 추출된 상태를 가정합니다. 즉 누군가가 PDF에서 텍스트를 뽑아서 문자열로 넘겨야 하는데, 이게 가장 어려운 부분입니다. 게다가:
좌표 정보가 사라짐: 문자열 한 덩어리로 받으니 "P-101A 옆에 CENTRIFUGAL PUMP가 있다"는 공간 관계가 날아갑니다. LLM은 그냥 텍스트 뭉치 안에서 패턴 매칭하는 수준이 됩니다.
12,000자 제한: 큰 P&ID 한 장에서 텍스트만 뽑아도 이걸 넘기 쉽고, 여러 장이면 답 없습니다.
환각 위험: 좌표/구조 없이 텍스트만 보면 모델이 태그를 잘못 묶거나 만들어냅니다.
권장 구조: 별도 도구로 분리
이 MCP 서버에 PDF 파싱 도구를 추가하시되, 책임을 나누는 게 좋습니다:
parse_pid_pdf(pdf_path) ← 새로 추가 (PyMuPDF로 좌표+텍스트 추출)
↓ 구조화된 JSON (clusters)
extract_pid_tags_structured(clusters) ← 기존 extract_pid_tags 개선
↓ 태그 리스트
match_pid_tags(pid_tags, experion_tags) ← 이건 그대로 좋음
추가할 도구 (기존 server.py에 그대로 붙이시면 됩니다)
python# ── P&ID PDF 파서 (벡터 PDF 전용) ─────────────────────────────────────────────
@mcp.tool()
def parse_pid_pdf(pdf_path: str, cluster_eps: float = 80.0) -> str:
"""벡터 P&ID PDF에서 텍스트 + 좌표를 추출하고 공간 클러스터링합니다.
CAD에서 플롯된 벡터 PDF 전용. 스캔본은 별도 OCR 필요.
Args:
pdf_path: PDF 파일 절대 경로
cluster_eps: DBSCAN 거리 임계값(픽셀). 도면 스케일에 따라 조정.
Returns:
JSON: { success, pages: [{page, drawing_no, clusters: [{id, texts, bbox}]}] }
"""
try:
import fitz
from sklearn.cluster import DBSCAN
import numpy as np
import re as _re
doc = fitz.open(pdf_path)
pages_out = []
for page_idx, page in enumerate(doc):
spans = []
for block in page.get_text("dict")["blocks"]:
for line in block.get("lines", []):
for span in line.get("spans", []):
txt = span["text"].strip()
if txt:
spans.append({
"text": txt,
"bbox": list(span["bbox"]),
"size": span["size"],
"dir": list(line.get("dir", [1, 0])), # 회전 감지
})
if not spans:
pages_out.append({"page": page_idx + 1, "drawing_no": None, "clusters": []})
continue
# 타이틀블록(우측 하단) 분리
pw, ph = page.rect.width, page.rect.height
title_spans = [s for s in spans
if s["bbox"][0] > pw * 0.7 and s["bbox"][1] > ph * 0.85]
drawing_no = None
for s in title_spans:
# 도면번호 패턴 (예: P-1234-001, PID-001)
if _re.match(r'^[A-Z&]+-?\d+(-\d+)*$', s["text"]):
drawing_no = s["text"]
break
drawing_spans = [s for s in spans if s not in title_spans]
if not drawing_spans:
pages_out.append({"page": page_idx + 1, "drawing_no": drawing_no, "clusters": []})
continue
# 공간 클러스터링
centers = np.array([
[(s["bbox"][0] + s["bbox"][2]) / 2, (s["bbox"][1] + s["bbox"][3]) / 2]
for s in drawing_spans
])
labels = DBSCAN(eps=cluster_eps, min_samples=1).fit_predict(centers)
clusters = {}
for span, lbl in zip(drawing_spans, labels):
clusters.setdefault(lbl, []).append(span)
cluster_list = []
for lbl, members in clusters.items():
xs = [m["bbox"][0] for m in members] + [m["bbox"][2] for m in members]
ys = [m["bbox"][1] for m in members] + [m["bbox"][3] for m in members]
cluster_list.append({
"id": f"p{page_idx+1}c{lbl}",
"texts": [m["text"] for m in members],
"bbox": [min(xs), min(ys), max(xs), max(ys)],
})
pages_out.append({
"page": page_idx + 1,
"drawing_no": drawing_no,
"clusters": cluster_list,
})
doc.close()
return json.dumps({"success": True, "pages": pages_out}, ensure_ascii=False)
except Exception as e:
return json.dumps({"success": False, "error": f"PDF 파싱 실패: {e}"}, ensure_ascii=False)
그리고 기존 extract_pid_tags는 두 가지 입력을 다 받게 살짝 손보시면 됩니다. 클러스터 입력일 때는 LLM에 이렇게 넘기세요:
python# extract_pid_tags 안에서 source_type == "clusters"일 때
user_msg = (
"Each cluster represents one equipment/instrument with nearby texts grouped by spatial proximity.\n"
"Identify the tag (matching ISA-5.1 patterns) and the equipment description from each cluster.\n\n"
f"Clusters:\n{json.dumps(clusters, ensure_ascii=False, indent=2)}"
)
실제 워크플로우
C#에서 이렇게 호출하시게 됩니다:
1. parse_pid_pdf("C:/drawings/unit10.pdf")
→ { pages: [{drawing_no: "P-1234-001", clusters: [...]}] }
2. extract_pid_tags(clusters_json, "clusters")
→ { tags: [{tagNo: "P-101A", equipmentName: "CENTRIFUGAL PUMP", ...}] }
3. match_pid_tags(pid_tags, experion_tags) ← 이미 있음
→ { mappings: [{pidTag: "P-101A", experionTag: "p-101a.pv", ...}] }
4. (선택) 결과를 RAG에 인덱싱 — 메타데이터에 drawing_no, page, bbox 포함
의존성 추가
bashpip install pymupdf scikit-learn numpy
PyMuPDF는 AGPL 라이선스라 상용 배포 시 주의하셔야 합니다. 사내 시스템이면 보통 문제없지만, 외부 고객사 납품이면 Artifex 상용 라이선스 검토하시거나 pdfplumber(MIT)로 대체하는 것도 방법입니다. pdfplumber도 chars 좌표 다 줍니다.
짚어드릴 만한 디테일 두 가지
1. _DB_SCHEMA에 P&ID 매핑 테이블도 추가하시면 좋을 듯: pid_tag_mapping 같은 테이블을 만들어서 (drawing_no, pid_tag, experion_tag, bbox, page) 저장하시면, 나중에 "이 펌프 트렌드 보여줘" 같은 자연어 쿼리가 P&ID → Experion 태그 → history_table 쿼리로 자연스럽게 이어집니다.
2. RAG 컬렉션 추가 고려: 현재 experion-opc-docs처럼 pid-drawings 컬렉션을 하나 더 만들어서 클러스터 단위로 임베딩해두시면, "원심펌프 P-101A 사양" 같은 질문에 도면 위치까지 답해줄 수 있습니다.
요약하면, 이 MCP 서버에 붙이는 건 정답이지만 extract_pid_tags에 raw 텍스트 던지지 마시고, parse_pid_pdf 도구를 추가해서 좌표 정보 살린 클러스터를 LLM에 넘기는 구조로 가시는 걸 강력히 권장드립니다.
SYMBOL & LENGEND 가 보통은 별도로 있는데 그걸 학습하게 하는것은 ?
오전 4:01
Claude 응답: 좋은 지적입니다.
좋은 지적입니다. 실무에서 P&ID 첫 1~2장에 항상 SYMBOL & LEGEND 시트가 있고, 회사/프로젝트마다 심볼 모양과 약어가 달라서 이걸 활용하는 건 정확도에 결정적입니다.
다만 "학습"이라는 표현은 신중하게 봐야 합니다. 현재 구조에서 가능한/불가능한 것을 짚어보겠습니다.
가능한 방식 3가지 (난이도 순)
방식 1: Few-shot 컨텍스트 주입 (즉시 가능, 권장)
LEGEND 시트에서 추출한 (약어 → 풀네임) 매핑을 LLM 프롬프트에 동적으로 끼워넣는 방식입니다. 진짜 학습이 아니라 in-context learning이지만, 효과는 충분합니다.
LEGEND 시트 파싱
{"FIC": "Flow Indicating Controller",
"PIT": "Pressure Indicating Transmitter", ...}
extract_pid_tags 호출 시 system 프롬프트에 주입
LLM이 이 프로젝트 전용 약어로 정확히 해석
장점: 파인튜닝 없이 즉시 효과. 프로젝트마다 LEGEND가 달라도 자동 대응. 단점: 프롬프트 길이 증가 (보통 20~50개 약어면 1~2KB라 무시 가능).
방식 2: 심볼 형상 매칭 (phase 2 영역)
LEGEND 시트의 심볼 도형(사각형+원, 단일 원, 마름모 등)을 page.get_drawings()로 추출 → 도면 페이지의 도형과 매칭. 이게 진짜 "DCS vs 현장계기" 정확 판별의 정공법입니다.
장점: 텍스트 없이도 심볼 종류 판별 가능. 단점: 도형 비교 알고리즘 필요 (벡터 path 정규화, 회전/스케일 보정). 개발 공수 큼.
방식 3: Vision 모델 파인튜닝 (지금은 비추)
YOLO나 Pix2Struct를 LEGEND 시트로 파인튜닝. 실무에서는 잘 안 씁니다 — 회사마다 LEGEND가 다른데 매번 파인튜닝하면 운영 부담이 큽니다.
권장: 방식 1을 phase 1.5로 추가
phase 1(기본 파싱) 끝나고 phase 2(심볼 형상) 가기 전 단계로 넣으면 적절합니다. 작업지시서에 추가할 항목은:
LEGEND 시트 자동 감지: 페이지 텍스트에 LEGEND, SYMBOL, ABBREVIATION 같은 키워드가 있고 표 형태 텍스트가 많은 페이지를 LEGEND로 식별.
LEGEND 파서: 좌측 컬럼(약어) + 우측 컬럼(설명) 패턴으로 좌표 기반 매칭. 이건 P&ID 본문 클러스터링과 다른 알고리즘이 필요합니다 (좌→우 가로 페어링).
MCP 도구 추가:
extract_pid_legend(pdf_path) — LEGEND 페이지에서 약어 사전 추출
extract_pid_tags에 legend: dict 옵션 파라미터 추가
RAG 활용 옵션: 추출한 LEGEND를 Qdrant에 별도 컬렉션(pid-legend-{project_id})으로 저장하면, 같은 프로젝트의 다른 도면 처리 시 재사용 가능. 이게 가장 깔끔한 운영 방식입니다.
실무 팁
LEGEND 시트가 표(table) 형태가 아닌 자유 배치인 경우가 많습니다 (심볼 그림 + 옆에 설명). 이 경우 단순 좌→우 페어링이 안 되고, 다음 같은 휴리스틱이 필요합니다:
심볼 도형의 bbox를 잡고
그 도형 우측 또는 하단의 가장 가까운 텍스트 블록을 설명으로 페어링
약어는 보통 도형 안에 있으므로 도형 내부 텍스트 우선
작업지시서에 방식 1(LEGEND 텍스트 추출 + 프롬프트 주입)을 phase 1.5로 추가할까요? 아니면 phase 1에 통합해서 한 번에 작업시킬까요?
추가로 한 가지 결정해주실 게 있습니다 — LEGEND를 도면 단위로 매번 파싱할지, 프로젝트 단위로 한 번 파싱 후 RAG에 캐시할지에 따라 구조가 달라집니다. 실무 운영 패턴(같은 프로젝트의 도면이 여러 장씩 들어오는지) 알려주시면 그에 맞게 설계해드리겠습니다.
오전 4:06
Claude 응답: PDF 컨텐츠 일부만 컨텍스트로 들어왔네요.
PDF 컨텐츠 일부만 컨텍스트로 들어왔네요. 실제 파일을 직접 봐야 정확히 판단할 수 있으니 읽어보겠습니다.
파일 조회함, 명령 실행함
파일 조회함, 명령 실행함
1페이지짜리 A4 PDF네요. 시각적으로 봐야 심볼 모양과 레이아웃을 파악할 수 있습니다.
텍스트 레이아웃 확인
텍스트 레이아웃 확인
텍스트 추출이 거의 안 됩니다. 도면이 거의 다 벡터 그래픽으로 그려져 있어서 텍스트가 폰트 임베딩 없이 path로 변환된 것 같습니다. 직접 페이지를 시각화해서 봐야 합니다.
명령 실행함, 파일 조회함
명령 실행함, 파일 조회함
좋은 자료네요. 90도 회전된 도면이긴 한데(공장 도면 흔한 형태) 내용은 다 보입니다. 이걸 보고 정확히 판단해보겠습니다.
확인된 LEGEND 시트 구조
이 도면은 **신원리파인드퓨얼(SHINWON REFINED FUEL)**의 표준 P&ID 심볼/레전드 시트로, 다음 섹션들로 구성되어 있습니다:
텍스트 기반 (표/사전 형태) — 자동 추출 쉬움:
INSTRUMENT IDENTIFICATION TABLE: First Letter / Succeeding Letters (A=Analysis, F=Flow Rate, ...)
PIPE LINE NUMBER: 라인번호 명명규칙 (예: P-10107-500A-F1-H 100)
INSTRUMENT NUMBER DESIGNATION
FLUID NAME ABBREVIATION (P.CHE=Process Fluid, CWS=Cooling Water Supply, ...)
PIPING CLASS & MATERIAL (S1=SPPS380, F2=STS304, ...)
INSULATION CODE (H=Heat Conservation, P=Personnel Protection, ...)
ABBREVIATIONS IDENTIFIED WITH VALVES (FC, FO, FS, ...)
ABBREVIATIONS FOR DETECTING GAS (HC=Hydrocarbon, EA=Ethyl Acetate, ...)
EQUIPMENT IDENTIFICATION (C=Column, K=Compressor, P=Pump, ...)
심볼 그림 기반 (도형+라벨) — 형상 인식 필요:
LINE AND VALVE SYMBOLS (밸브 종류별 도형)
INSTRUMENT SYMBOLS (DCS=사각형+원, 현장계기=원, Interlock=마름모 등) ← 사용자가 처음 말씀하신 그것
INSTRUMENT LINE SYMBOLS (전기/공압/소프트웨어 신호선)
ROTATIONARY EQUIPMENT SYMBOLS (펌프 종류별)
VALVE CONNECTION SYMBOLS
작업지시서 어떻게 보강할지
이 LEGEND 시트의 특성을 보고 명확해진 게 있습니다:
텍스트 사전 부분은 phase 1.5에서 즉시 활용 가능 — 표 형태라 좌→우 페어링으로 추출 쉽습니다.
심볼 형상 부분은 phase 2 본격 작업 — 다만 LEGEND 시트가 있으면 형상 인식이 훨씬 쉬워집니다. "이 회사의 DCS 심볼은 정확히 이렇게 생겼다"는 레퍼런스 도형을 얻을 수 있으니까요.
이 PDF는 텍스트 추출이 거의 안 됩니다 (pdftotext 결과가 2줄). 폰트가 path로 변환된 케이스입니다. 즉 PyMuPDF의 get_text()도 실패할 가능성이 높습니다. 이건 작업지시서에 중요한 caveat으로 추가해야 합니다.
확인해드릴 게 있는데, 본 작업 대상 P&ID 본문 도면도 이런 식으로 텍스트가 path로 변환되어 있나요? 아니면 LEGEND 시트만 그렇고 본문은 텍스트가 살아있나요?
이게 중요한 이유: 만약 본문도 path 변환이면 PyMuPDF만으로는 안 되고 OCR이 필수가 됩니다. 그러면 phase 구성을 다시 짜야 합니다 (phase 1을 OCR 기반으로 다시 설계).
본문 도면 한 장만 같은 방식으로 pdftotext 돌려보시고 텍스트가 나오는지 확인 부탁드려도 될까요? 또는 본문 도면 PDF 한 장 올려주시면 제가 직접 확인하겠습니다.
입력 내용: 그럼 오토캐드 2023에서 어떤 형식으로 pdf출력하면 되는데?
그럼 오토캐드 2023에서 어떤 형식으로 pdf출력하면 되는데?
오전 4:08
Claude 응답: 결론부터: AutoCAD 2023에서 DWG to PDF.
결론부터: AutoCAD 2023에서 DWG to PDF.pc3로 플롯하시되, 몇 가지 옵션을 정확히 켜야 합니다.
권장 설정
플로터(Plotter): DWG To PDF.pc3 (AutoCAD 내장 드라이버)
Properties → Custom Properties 들어가서:
항목 설정값 이유
Vector quality 1200 dpi 이상 벡터 라인 정밀도
Raster image quality 300 dpi 래스터 이미지가 있을 경우
Capture fonts used in the drawing 체크 ✅ 가장 중요 — 폰트 임베딩
Convert all text to geometry 체크 해제 ❌ 절대 금지 — 텍스트가 path로 변환됨
Include layer information 체크 (선택) 디버깅 시 유용
Include hyperlinks 체크 해제 불필요
핵심: "Convert all text to geometry"를 끄세요
업로드해주신 LEGEND 시트가 텍스트 추출 안 됐던 이유가 바로 이 옵션이 켜져 있었기 때문입니다. 이게 켜지면:
AutoCAD가 모든 텍스트를 벡터 path(선분/곡선)로 변환해서 PDF에 박아넣음
PDF에는 글자가 "그림"으로 들어가서 pdftotext, PyMuPDF, 어떤 파서도 텍스트로 읽을 수 없음
OCR을 쓰는 수밖에 없게 됨
이 옵션은 저작권 보호용(받는 사람이 텍스트 못 빼가게)으로 켜는 건데, 사내 분석/RAG 용도면 반드시 끄셔야 합니다.
폰트 관련 추가 팁
AutoCAD가 SHX 폰트(romans.shx, txt.shx 같은 AutoCAD 전용 벡터 폰트)를 쓰면, "Capture fonts" 옵션이 있어도 PDF에 텍스트로 안 들어가고 path로 변환됩니다. 해결 방법:
TrueType 폰트로 통일: 도면의 텍스트 스타일을 Arial, 맑은 고딕, Romans TTF 등 TTF로 변경. STYLE 명령에서 폰트 교체.
부득이 SHX를 써야 한다면: 최소한 영문 태그(FIC, PT 등)는 TTF 폰트의 별도 텍스트 스타일로 작성. 한글 라벨은 SHX여도 영향 적음 (어차피 OCR 권장).
검증 방법
플롯한 PDF가 제대로 나왔는지 1초만에 확인:
bash
pdftotext -layout your_drawing.pdf - | head -30
여기서 FIC, 10101, 장비명 같은 텍스트가 줄줄이 나오면 성공입니다. 빈 줄만 나오거나 깨진 글자만 나오면 위 옵션이 잘못 설정된 것이니 다시 플롯해야 합니다.
또는 PDF를 Adobe Reader로 열어서 Ctrl+F로 "FIC" 검색해보세요. 검색되면 OK, 안 되면 path 변환됨.
PUBLISH로 일괄 처리
도면이 여러 장이면 단건 PLOT보다 PUBLISH 명령(Sheet Set 또는 Layout 일괄)으로 처리하는 게 효율적입니다. 이때도 위 PC3 설정이 동일하게 적용됩니다. PC3 한 번 잘 만들어두시면 재사용 가능합니다.
실제 운영 팁
플랜트 도면 받으시는 환경이면 EPC사/CAD 작업자에게 다음을 요청하시면 깔끔합니다:
"PDF 플롯 시 'Convert all text to geometry' 옵션 해제하고, TrueType 폰트 기반으로 출력해주세요. 검증은 PDF 열어서 Ctrl+F로 태그명 검색되는지 확인 부탁드립니다."
이 한 줄이 phase 1 전체 작업의 성공 여부를 결정합니다. 외주 업체에서 받는 도면이면 납품 검수 기준으로 명문화해두시면 좋습니다.
확인해보시고 본문 도면 한 장 다시 플롯해서 텍스트 추출되는지 보시면, 그 결과에 따라 작업지시서를 phase 1(텍스트 기반) 그대로 갈지 OCR 추가할지 결정하시면 됩니다.
Symbol-Definition.pdf
오전 4:22
Claude 응답: 텍스트 추출이 잘 되네요.
텍스트 추출이 잘 되네요. 이제 PyMuPDF로 좌표까지 같이 뽑히는지 확인해보겠습니다. 추출되는 텍스트의 구조를 보면 이게 본문 RAG/태깅 자동화 가능 여부를 결정합니다.
명령 2개 실행함, 파일 생성됨
명령 2개 실행함, 파일 생성됨
흥미롭네요. 44개 텍스트 span만 추출됐습니다. 시각적으로 본 LEGEND 시트에는 수백 개 텍스트가 있어야 하는데요. 이건 섹션 제목들만 텍스트로 살아있고, 표 내용물은 모두 path 변환된 상태입니다.
확인해보겠습니다:
파일 생성됨, 명령 실행함
파일 생성됨, 명령 실행함
예상대로입니다. 진단 결과를 명확히 정리해드리면:
진단 결과
텍스트로 살아있는 것: 섹션 헤더 + 타이틀블록만 (44개) — 큰 글씨(8~10pt)와 한글 폰트(Dotum, Gulim 계열)
Path로 변환된 것: 표 내용물 전부 — 약어, 설명, 작은 영문 텍스트들. 3,666개의 짧은 vector path는 거의 다 글자 획(stroke)입니다. 4839개 drawing 중 75%가 글자 path라는 뜻입니다.
원인
이 PDF는 AutoCAD에서 SHX 폰트(romans.shx 등)로 작성된 작은 영문 텍스트가 path로 변환된 상태입니다. 한글 섹션 제목은 TTF 폰트(Dotum 등)라서 살아있고, 영문 본문은 SHX라서 path가 됐습니다.
이건 사용자가 "Convert all text to geometry"를 켠 게 아니라, AutoCAD의 기본 동작입니다. SHX는 TrueType이 아니라 AutoCAD 전용 벡터 폰트라서 PDF 표준 폰트로 임베딩이 안 되거든요.
해결책 — AutoCAD 작업자에게 요청할 것
본문 도면 플롯할 때 다음을 해주셔야 합니다:
옵션 A (권장): SHX → TTF 폰트 교체
도면 안의 텍스트 스타일을 모두 TrueType으로 바꿉니다. AutoCAD 명령으로:
STYLE 명령 → 각 텍스트 스타일 선택 → 폰트를 TTF로 교체
권장 TTF 폰트:
romans.ttf (있으면 가장 자연스러움 — SHX의 romans와 시각적으로 유사한 TTF가 일부 사이트에 존재)
Arial (가장 호환성 좋음, 표준 영문 도면 텍스트로 무난)
Tahoma 또는 Verdana (작은 글씨도 또렷)
한글은 맑은 고딕 또는 Dotum 유지
일괄 변경 스크립트(-STYLE + LISP)로 처리하거나, Express Tools의 TXT2MTXT 활용 가능합니다.
옵션 B: AutoCAD 2016+ 의 "SHX 텍스트를 검색/선택 가능한 PDF로" 옵션
AutoCAD 2016부터 "SHX 텍스트 인식(SHX text recognition)" 옵션이 PDF 출력에 추가됐습니다:
PLOT 대화상자 → DWG To PDF.pc3 → Properties → Custom Properties
→ "Capture fonts used in the drawing" 체크
→ "Include layer information" 아래쪽에 있는 옵션들 확인
AutoCAD 2023이면 더 발전된 옵션이 있을 겁니다:
PDFSHX 시스템 변수를 1로 설정:
명령창에서: PDFSHX
값을 1로 입력
이걸 1로 하면 SHX 텍스트를 PDF에 searchable text(검색 가능한 텍스트)로 같이 출력합니다. 본래 path는 그대로 두고 그 위에 투명한 텍스트 레이어를 얹는 방식이라 시각적으로 동일하면서 텍스트 추출이 가능해집니다.
PDFSHX = 0 (기본값, 텍스트 추출 안 됨)
PDFSHX = 1 (SHX를 검색 가능 텍스트로 포함) ← 이걸로 설정
그리고 플롯하면 됩니다.
검증 방법
bash
pdftotext your_drawing.pdf - | grep -E "FIC|PT|FT"
태그명들이 줄줄이 나오면 성공입니다.
작업지시서에 반영
이 발견을 작업지시서에 추가하는 건 좋은데, **사실 이건 작업지시서에 들어갈 게 아니라 운영 사전조건(precondition)**입니다. 코딩 에이전트가 해결할 수 있는 게 아니라 도면 공급자가 해결해야 하니까요.
작업지시서에는 "입력 PDF는 PDFSHX=1 또는 TTF 폰트로 플롯된 것"이라는 전제조건과 검증 절차를 추가하면 됩니다.
추가할까요? 그리고 본문 도면 한 장 같은 방식으로 출력해보시고 결과 공유해주시면, 실제 본문에서 태그 추출이 잘 되는지 끝까지 확인해드릴 수 있습니다.
입력 내용: 현실적으로 불가능함 OCR을 해야함
현실적으로 불가능함 OCR을 해야함
오전 4:25
Claude 응답: 동의합니다.
동의합니다. 현장에서 받는 도면을 매번 재플롯 요청하는 건 비현실적이고, EPC사/벤더 측 SHX 사용 관행이 워낙 굳어져 있어서 OCR이 정공법입니다.
작업지시서를 OCR 기반으로 다시 설계하겠습니다. 큰 변화점:
구조 변경:
텍스트 추출: PyMuPDF → PaddleOCR (회전 텍스트 강함, GPU 가속, 한국어 지원)
입력: PDF → 고DPI 래스터 이미지 (300 DPI 권장)
좌표: PDF 좌표 → 픽셀 좌표 (DBSCAN은 그대로)
DGX Spark 환경 활용: PaddleOCR을 vLLM과 같은 GPU에 올려도 부담 적습니다. 모델 크기 작음.
질문 한 가지만 확인하고 작업지시서 작성 들어가겠습니다:
세 가지만 알려주시면 그 답변에 맞춰 OCR 기반 작업지시서 다시 작성하겠습니다. 옵션 보고 선택만 해주세요.
입력 내용: 질문: OCR 엔진 어떤 걸로 갈까요?
질문: OCR 엔진 어떤 걸로 갈까요?
답변: PaddleOCR (한/영, GPU, 회전 강함, 가장 권장)
질문: 처리 단위는?
답변: 둘 다 지원
질문: 한글 라벨도 추출해야 하나요? (예: 장비 한글 설명)
답변: 한글 라벨도 함께 (혼합)
오전 4:26
Claude 응답: 답변 반영해서 OCR 기반으로 작업지시서를 다시 작성하겠습니다.
답변 반영해서 OCR 기반으로 작업지시서를 다시 작성하겠습니다.
2개의 파일 생성됨, 명령 실행함, 파일 읽음
2개의 파일 생성됨, 명령 실행함, 파일 읽음
작업지시서 v2(OCR 기반)로 다시 만들었습니다. v1 대비 주요 변화:
구조 변경
텍스트 추출: PyMuPDF 텍스트 → PDF 래스터화 + PaddleOCR
좌표계: PDF 포인트 → 픽셀 (DPI 비례 cluster_eps 자동 보정)
cluster_eps 기본값: 40 → 50 (300 DPI 픽셀 기준)
한글 처리 추가
has_korean 플래그를 cluster마다 표시
출력 스키마에 equipmentNameKo 필드 추가
프롬프트 예시에 '저장탱크 A' 같은 한글 케이스 포함
다중 페이지 지원
page_range="1-3", "1,3,5", "2-" 등 유연한 범위 지정
단일/다중 모두 같은 함수로 처리
OCR 특화 보정
신뢰도 임계값(min_confidence) 파라미터
클러스터별 평균 OCR 신뢰도 노출 (ocr_confidence)
LLM 프롬프트에 OCR 오인식 패턴 보정 규칙 추가 (O↔0, I↔1, S↔5 등)
운영 고려
GPU/CPU 자동 fallback (PADDLE_USE_GPU 환경변수)
처리 시간 벤치마크 인수조건 포함
vLLM과 GPU 동거 시 OOM 주의사항 명시
향후 작업 우선순위 명확화
심볼 형상 인식(DCS 판별)과 LEGEND 자동 파싱이 phase 2 최우선
LEGEND 사전을 Qdrant에 캐시해서 프로젝트 단위 재사용
테스트 코드의 EXPECTED_TAGS는 실제 보유하신 샘플 도면에 있는 태그 3~5개로 채워넣으시면 바로 사용 가능합니다.
Pid pdf parser 작업지시서
문서 · MD
입력 내용: OCR 로 하면 DWG 나 DXF 도 바로 읽을수있나?
OCR 로 하면 DWG 나 DXF 도 바로 읽을수있나?
오전 4:39
Claude 응답: 좋은 질문입니다.
좋은 질문입니다. OCR로는 DWG/DXF를 직접 읽을 수 없습니다. 이유와 대안을 짚어드리겠습니다.
왜 OCR이 안 되나
OCR은 이미지에서 텍스트를 인식하는 기술입니다. DWG/DXF는 이미지가 아니라:
DWG: AutoCAD 바이너리 포맷. 텍스트, 좌표, 레이어 등이 이미 구조화된 데이터로 저장됨
DXF: AutoCAD 텍스트 교환 포맷. ASCII 또는 바이너리. 마찬가지로 구조화된 데이터
즉 OCR을 쓸 이유가 없습니다 — 텍스트가 이미 데이터로 들어있으니 그냥 읽으면 됩니다. SHX 폰트 path 변환 문제도 없습니다 (그건 PDF 출력 단계의 문제).
포맷별 권장 처리 방법
포맷 처리 방법 라이브러리 텍스트 추출
DXF 직접 파싱 ezdxf (Python, MIT) ✅ 즉시 가능
DWG DXF로 변환 후 파싱 ODA File Converter, LibreDWG ✅ 변환 후 가능
PDF (벡터) PyMuPDF pymupdf ✅ 가능 (단, SHX 제외)
PDF (SHX path) OCR PaddleOCR ✅ OCR 필요
PDF (스캔) OCR PaddleOCR ✅ OCR 필요
DXF 처리는 PDF보다 훨씬 쉽습니다
ezdxf로 DXF에서 텍스트와 좌표를 뽑는 건 몇 줄이면 됩니다:
python
import ezdxf
doc = ezdxf.readfile("drawing.dxf")
msp = doc.modelspace()
# TEXT, MTEXT, ATTRIB 모두 잡기
for entity in msp.query("TEXT MTEXT ATTRIB"):
if entity.dxftype() == "TEXT":
text = entity.dxf.text
x, y = entity.dxf.insert.x, entity.dxf.insert.y
elif entity.dxftype() == "MTEXT":
text = entity.text # 이미 평문화됨
x, y = entity.dxf.insert.x, entity.dxf.insert.y
elif entity.dxftype() == "ATTRIB":
text = entity.dxf.text
x, y = entity.dxf.insert.x, entity.dxf.insert.y
layer = entity.dxf.layer # ← PDF에는 없는 추가 정보
print(f"{text:<20} @ ({x:.1f}, {y:.1f}) [layer: {layer}]")
장점:
100% 정확 (OCR 오인식 없음)
GPU 불필요, 매우 빠름
레이어 정보까지 추출 가능 — 이게 큰 장점입니다. 보통 P&ID에서 INST_TAG, EQUIP_NAME, PIPE_LINE 같은 레이어로 구분되어 있어서, 태그/장비명/라인번호를 레이어 기준으로 분류할 수 있습니다.
블록(BLOCK) 정보도 추출 가능 — ISA balloon은 보통 블록 + ATTRIB로 만들어져 있어서, 블록 이름으로 "이건 DCS 심볼", "이건 현장계기"를 직접 판별할 수 있습니다. phase 2의 형상 인식이 phase 1에서 해결됩니다.
즉, DXF가 있으면 OCR 파이프라인 자체가 불필요
기존 작업지시서의 extract_pid_tags가 이미 source_type="dxf"를 처리하지만, raw text 한 덩어리로 받게 되어 있어서 좌표/레이어/블록 정보가 살지 않습니다. 이걸 활용 못 하면 DXF의 이점을 다 버리는 셈입니다.
권장 구조 업데이트
작업지시서에 DXF 전용 도구를 별도로 추가하는 걸 강력히 권장드립니다:
parse_pid_dxf(dxf_path) ← 신규, ezdxf 기반
parse_pid_dwg(dwg_path) ← 신규, ODA Converter로 DXF 변환 후 위 함수 호출
parse_pid_pdf(pdf_path) ← 이미 만든 OCR 기반
↓ 모두 같은 cluster JSON 형식으로 출력
extract_pid_tags(clusters_json, "clusters") ← 통합 처리
세 함수가 같은 출력 스키마를 따르게 하면 LLM/하위 로직은 입력 포맷을 신경 쓸 필요가 없습니다.
추가로 DXF 파서는 다음 정보를 cluster에 더 담을 수 있습니다:
json
{
"id": "p1c0",
"texts": ["FIC", "10101"],
"bbox": [...],
"layer": "INST_TAG", ← DXF만 가능
"block_name": "DCS_BALLOON", ← DXF만 가능 (DCS/현장계기 즉시 판별)
"source": "dxf"
}
우선순위 제안
원본 도면을 EPC사에서 DWG/DXF로도 받을 수 있는지 먼저 확인해보시는 게 좋습니다. 가능하다면:
DXF/DWG 우선 처리 — 정확도 100%, 빠름, 레이어/블록까지 활용
PDF는 fallback — DWG/DXF가 없는 경우만 OCR 파이프라인 사용
실무적으로 EPC사가 PDF만 주는 경우가 많지만(IP 보호 등), 사내 자체 프로젝트나 협력사 관계가 좋으면 DWG도 받을 수 있습니다. DWG가 있으면 작업 난이도가 한 단계 내려갑니다.
작업지시서에 DXF/DWG 처리 도구를 추가해드릴까요? phase 1.5로 넣으면 적절할 것 같습니다.
p-9100.dxf
dxf
오전 4:42
Claude 응답: DXF 파일을 직접 분석해서 어떤 정보가 들어있는지 확인해보겠습니다.
DXF 파일을 직접 분석해서 어떤 정보가 들어있는지 확인해보겠습니다.
명령 3개 실행함, 파일 생성됨
명령 3개 실행함, 파일 생성됨
훌륭한 데이터입니다. TEXT 3,562개, MTEXT 363개나 있고, 레이어도 잘 나눠져 있습니다(PROCESS LINE, VALVE NO, LINENO, 1-SYMBOL 등). 텍스트 샘플을 보고 태그 분포를 파악해보겠습니다.
파일 생성됨, 명령 실행함
파일 생성됨, 명령 실행함
완벽한 데이터입니다. 이게 OCR 대비 얼마나 좋은지 명확히 보입니다:
핵심 발견:
장비 태그 282개 명확히 추출 (P-10101, E-10103, T-10101, C-10111 등) — layer=1에 정리되어 있음
ISA balloon function code 후보 536개 (PG, TG, LT, PT, TE, FICQ, FC, FCV 등) — 빈도까지 정확
루프번호 후보 666개 — 대부분 layer=INSTRUMENT에 있음
한글 라벨 59개 (기존 설비, PGMEA REFINE 공정 등) — 깨짐 없이 100% 추출
레이어가 의미 있게 나뉨: INSTRUMENT, LINENO, VALVE NO, 1-SYMBOL 등
이제 INSERT(블록 참조)가 ISA balloon인지 확인해봅시다 — 이게 phase 2의 형상 인식을 phase 1에서 해결할 수 있는 결정적 단서입니다.
파일 생성됨, 명령 실행함
파일 생성됨, 명령 실행함
아주 명확해졌습니다.
핵심 발견:
INSERT(블록)는 ISA balloon이 아닙니다 — 이름이 A$C... 형태로 익명화되어 있고 ATTRIB도 없음. 이 도면은 블록 기반이 아니라 CIRCLE + TEXT 조합으로 그려져 있음
layer=INSTRUMENT에 CIRCLE 215개 — 이게 바로 ISA balloon입니다!
반지름 0.4(266개), 0.3(235개)이 압도적 — 이게 표준 ISA balloon 크기. 0.7은 더 큰 심볼
Numeric texts 666개 중 대부분이 layer=INSTRUMENT — 루프번호가 정확히 같은 레이어에 있음
즉 DXF에서는 phase 2 작업까지 같이 끝낼 수 있습니다. CIRCLE 좌표 + 반지름으로 balloon 위치를 찾고, 그 안에 들어있는 TEXT를 매칭하면 ISA balloon이 정확히 식별됩니다. 사각형 검출까지 추가하면 DCS vs 현장계기 구분도 가능합니다.
마지막으로 통합 검증해보겠습니다:
파일 생성됨, 명령 실행함
파일 생성됨, 명령 실행함
완벽합니다. 215개 balloon 중 197개(91.6%)가 LLM 호출 없이 정확한 태그로 추출됐습니다.
LLM이 필요했던 이유(두 줄 정규화)가 DXF에서는 단순 좌표 매칭으로 해결됩니다. OCR 파이프라인의 모든 어려움이 사라졌습니다:
항목 OCR 파이프라인 DXF 파이프라인
텍스트 정확도 ~95% (오인식 보정 필요) 100%
처리 시간 (1장) GPU 5~15초, CPU 30~90초 <1초
ISA balloon 식별 LLM 두 줄 합치기 필요 CIRCLE 좌표로 직접
DCS vs 현장계기 phase 2 (불가능) 사각형 검출 가능
LLM 호출 필수 (정규화) 선택사항 (보정용)
한글 라벨 OCR 의존 MTEXT 직독
GPU 필요 예 아니오
레이어/카테고리 없음 자동 분류
이제 결정할 게 있습니다 — 작업지시서에 DXF 처리 도구를 어떻게 추가할지:
실수로 빈 질문이 갔네요. 다시:
제안 드리는 구조:
작업지시서를 phase 1을 DXF 우선 / PDF는 fallback으로 재구성하는 게 합리적입니다:
[phase 1 - DXF 우선] ← 추천
parse_pid_dxf(dxf_path) ← 신규, ezdxf 기반 (이번 검증으로 91.6% 검증됨)
parse_pid_dwg(dwg_path) ← 신규, ODA Converter로 DXF 변환 후 위 함수 호출
parse_pid_pdf(pdf_path) ← OCR 기반 (이미 만든 것, fallback용)
→ 모두 같은 cluster JSON 출력
extract_pid_tags(clusters_json, "clusters") ← DXF는 LLM 거의 안 거쳐도 되고,
PDF만 LLM 정규화 필요
DXF 파서가 OCR 대비 추가로 제공할 수 있는 것:
layer: 텍스트가 속한 레이어 (INSTRUMENT, LINENO, VALVE NO 등)
balloon_type: 사각형+원(DCS) / 단일 원(FIELD) — 형상 검출로 직접 판별
is_pre_normalized: DXF에서 좌표 매칭으로 이미 FIC-10101 형태로 합친 태그
confidence: 좌표 매칭 신뢰도 (LLM 추론 없이도 0.95+)
어떻게 진행할까요?
작업지시서를 DXF + PDF 통합본으로 재작성 (가장 추천 — 실무에서 가장 쓸만한 구조)
DXF 처리 도구만 별도 작업지시서로 분리
현재 OCR 파이프라인 그대로 두고 DXF는 phase 1.5로 추가만
어느 쪽으로 갈지 선택해주시면 그에 맞춰 작업지시서를 업데이트하겠습니다.
입력 내용: DWG도 로컬LLM이 처리가능하면 그것도 넣어서 작업지시서를 만들어줘
DWG도 로컬LLM이 처리가능하면 그것도 넣어서 작업지시서를 만들어줘
오전 4:45
Claude 응답: DWG 처리 가능 여부부터 정확히 짚고 가겠습니다.
DWG 처리 가능 여부부터 정확히 짚고 가겠습니다.
DWG 처리 옵션 검토
DWG는 AutoCAD 바이너리 포맷이라 로컬LLM이 직접 읽을 수 없습니다. 반드시 DXF로 변환 후 처리해야 합니다. 변환 옵션:
도구 라이선스 정확도 운영 적합성
ODA File Converter 무료 (등록 필요) 높음 권장 — Linux/Windows 지원, CLI
LibreDWG (dwg2dxf) GPL-3 보통 오픈소스만 써야 할 때
Teigha (ODA SDK) 상용 라이선스 매우 높음 비싸지만 프로덕션급
AutoCAD/ODA Viewer GUI 높음 자동화 어려움
ODA File Converter가 현실적 선택입니다. Open Design Alliance가 무료 배포하고, CLI로 일괄 변환 가능합니다.
DGX Spark(Linux)에서:
bash
ODAFileConverter <input_dir> <output_dir> ACAD2018 DXF 0 1 "*.DWG"
LLM 활용 위치: DWG/DXF 처리에서 LLM은 선택적 보정/검증용입니다 — 좌표 매칭이 91.6% 자동으로 되니까요. 다만 다음에는 LLM이 가치 있습니다:
매칭 안 된 18개(8.4%) 문제 cluster 정규화
한글 장비 설명을 영문 매핑
LEGEND 시트 약어 자동 학습 (phase 2)
이제 통합 작업지시서를 작성하겠습니다.
명령 실행함, 파일 생성됨, 파일 읽음
명령 실행함, 파일 생성됨, 파일 읽음
작업지시서 v3로 통합 작성 완료했습니다.
v3의 주요 특징
DWG에 대한 솔직한 답변: 로컬LLM이 DWG를 직접 처리할 수는 없습니다. 바이너리 포맷이라 무조건 DXF 변환이 선행되어야 합니다. 그래서 parse_pid_dwg는 ODA File Converter로 DXF 변환 → parse_pid_dxf 위임하는 구조로 만들었습니다. 사용자 입장에서는 .dwg 파일 그대로 던지면 되고 내부에서 알아서 처리됩니다.
4개 도구 구조:
parse_pid_dxf — 메인 (실제 검증한 91.6% 매칭률)
parse_pid_dwg — DWG → DXF 변환 후 위 함수 호출
parse_pid_pdf — OCR fallback (PDF만 있을 때)
parse_pid_drawing — 확장자로 자동 라우팅하는 디스패처
핵심 가치 — pre_normalized_tag: DXF에서 좌표 매칭으로 미리 정규화된 태그를 cluster에 담아둡니다. extract_pid_tags가 이를 보면 LLM 호출 없이 _extract_from_dxf_clusters 함수로 즉시 결과 반환. DXF는 LLM 비용 없음 = vLLM 부하 없이 도면 한 장이 1초 내 처리.
실제 검증 데이터를 작업지시서에 명시:
사용자 제공 p-9100.dxf로 검증한 91.6% 매칭률, 215개 balloon 등을 인수조건과 마지막 섹션에 박아뒀습니다. 코딩 에이전트가 "이 정도 성능이 나와야 OK다"는 객관적 기준을 가지고 작업하게 됩니다.
ODA File Converter 등록 안내: 상용 SaaS 배포 시 라이선스 제약 있으니 사전 검토 필요사항 명시.
이제 코딩 에이전트에 던지시면 됩니다. instrument_layer 기본값은 "INSTRUMENT"로 잡아뒀는데, 다른 EPC사 도면은 INST나 I-1 같은 다른 이름을 쓸 수 있으니 첫 도면 들어올 때 한 번 확인하시면 됩니다.
Pid parser 작업지시서 v3
문서 · MD
입력 내용: p-9000.

Binary file not shown.

View File

@@ -1,3 +0,0 @@
1. 리모트 (웹브라우저 실행한 PC)에서 파일을 선택하면, 서버로 전달되지 않는다 ---> 추출시작시 에러남
2. 파일선택 버튼을 누르면 리모트 PC의 파일을 읽는다. 원격 서버의 파일은 읽히지 않는다.
3. 그러면 어쩌란 말인가 ????

View File

@@ -1,533 +0,0 @@
# P&ID 데이터베이스화 기능 통합 설계
## 📋 개요
DXF/PDF 형식의 P&ID 도면에서 장비 및 계기 정보를 AI로 자동 추출하여 ExperionCrawler 데이터베이스와 연동하는 기능입니다.
---
## 🎯 목표
1. P&ID 도면에서 장비 정보를 추출
2. 추출된 정보를 PostgreSQL 로 저장
3. 기존 Experion 데이터와 연동
4. 웹에서 시각화 및 관리
---
## 🏗️ 아키텍처 설계
```
┌─────────────────────────────────────────────────────────────────────┐
│ ExperionCrawler │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │
│ │ Frontend UI │◄────►│ Web API │◄────►│ DB │ │
│ │ (app.js, .html)│ │ (Controllers) │ │ (Experion │ │
│ └─────────────────┘ └─────────────────┘ │ DbContext)│ │
│ │ │ └─────────────┘ │
│ └──────────────────────────┼────────────────────────────┘ │
│ │ │
│ ┌───────────────┴───────────────┐ │
│ │ P&ID Extraction Service │ │
│ │ (AI 기반 추출) │ │
│ └───────────────┬───────────────┘ │
│ │ │
│ ┌───────────────▼───────────────┐ │
│ │ Image/Text Preprocessing │ │
│ │ (PDF → PNG → OCR) │ │
│ └───────────────┬───────────────┘ │
│ │ │
│ ┌───────────────▼───────────────┐ │
│ │ Claude Vision API │ │
│ │ (필드 추출) │ │
│ └───────────────┬───────────────┘ │
└────────────────────────────────────┼────────────────────────────────┘
┌─────────────────────┐
│ PostgreSQL DB │
│ ┌───────────────┐ │
│ │ pid_equipment │ │
│ │ Active │ │
│ │ Audit Log │ │
│ └───────────────┘ │
│ ┌───────────────┐ │
│ │ experion_tags │ │
│ │ Active │ │
│ └───────────────┘ │
└─────────────────────┘
```
---
## 📁 폴더 구조
```
ExperionCrawler/
├── src/
│ ├── Web/
│ │ └── Controllers/
│ │ ├── ExperionControllers.cs (기존)
│ │ └── PidController.cs (추가)
│ ├── Core/
│ │ ├── Application/
│ │ │ ├── Interfaces/
│ │ │ │ ├── IExperionServices.cs (기존)
│ │ │ │ ├── IPidExtractorService.cs (추가)
│ │ │ │ └── ITagMappingService.cs (추가)
│ │ │ ├── Services/
│ │ │ │ ├── TextToSqlService.cs (기존)
│ │ │ │ ├── PidExtractorService.cs (추가)
│ │ │ │ ├── AxImportGenerator.cs (추가)
│ │ │ │ └── TagMappingService.cs (추가)
│ │ │ └── Dtos/
│ │ │ ├── PidEquipmentDto.cs (추가)
│ │ │ └── TagCountDto.cs (추가)
│ │ └── Domain/
│ │ ├── Entities/
│ │ │ ├── PidEquipment.cs (추가)
│ │ │ └── PidAuditLog.cs (추가)
│ │ └── ValueObjects/
│ │ ├── ConfidenceScore.cs (추가)
│ │ └── MeasurementUnit.cs (추가)
│ └── Infrastructure/
│ ├── Database/
│ │ ├── ExperionDbContext.cs (기존 - 확장)
│ │ └── PidDbContext.cs (추가)
│ └── OpcUa/
│ └── (기존)
├── futurePlan/
│ ├── temp/
│ │ ├── pid_extractor.py (AI 추출기)
│ │ ├── schema.sql (추구용 DB 스키마)
│ │ └── requirements.txt (Python 의존성)
│ └── P&ID_데이터베이스화_통합_설계.md
├── src/Web/wwwroot/
│ └── js/
│ └── app.js (기존 - 확장)
```
---
## 🔌 데이터베이스 스키마 확장
### PidDbContext.cs (새 파일)
```csharp
using Microsoft.EntityFrameworkCore;
namespace ExperionCrawler.Infrastructure.Database;
public class PidDbContext : DbContext
{
public DbSet<PidEquipment> PidEquipment { get; set; }
public DbSet<PidAuditLog> PidAuditLog { get; set; }
// 기존 ExperionDbContext와 통합
public DbSet<TagInfo> TagInfo { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// PidEquipment 설정
modelBuilder.Entity<PidEquipment>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.TagNo).IsRequired().HasMaxLength(50);
entity.Property(e => e.EquipmentName).HasMaxLength(200);
entity.Property(e => e.InstrumentType).HasMaxLength(10);
entity.Property(e => e.LineNumber).HasMaxLength(100);
entity.Property(e => e.PidDrawingNo).HasMaxLength(50);
entity.Property(e => x => x.Confidence).HasPrecision(3, 2);
entity.Property(e => x => x.IsActive).HasDefaultValue(true);
// 태그 번호로 Experion과 연동
entity.HasOne(e => e.ExperionTag)
.WithMany(t => t.PidEquipments)
.HasForeignKey(e => e.ExperionTagId)
.OnDelete(DeleteBehavior.SetNull);
});
// PidAuditLog 설정
modelBuilder.Entity<PidAuditLog>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.UserId).HasMaxLength(100);
});
}
}
```
### 기존 ExperionDbContext.cs 확장
```csharp
public class ExperionDbContext : DbContext
{
// 기존 DbSet
// P&ID 데이터베이스용 DbSet 추가
public DbSet<PidEquipment> PidEquipment { get; set; }
public DbSet<PidAuditLog> PidAuditLog { get; set; }
// Expose PidDbContext connection string if needed
public string PidConnectionString => Configuration.GetConnectionString("PidDb");
}
```
---
## 🎯 필드 매핑
### P&ID 추출 필드 ↔ DB 필드
| 추출 필드 (AI) | DB 필드 (PidEquipment) | 설명 |
|---------------------|--------------------------|----------------------------|
| Tag No. | TagNo | 태그번호 (FT-1001, PT-2003) |
| Equipment Name | EquipmentName | 장비명 (Flow Transmitter) |
| Instrument Type | InstrumentType | 계기타입 (FT, PT, LT) |
| Line Number | LineNumber | 라인번호 (6"-P-1001-A1A) |
| P&ID Drawing No. | PidDrawingNo | 도면번호 (P&ID-100-001) |
| Confidence | Confidence | 신뢰도 (0.0~1.0) |
---
## 💻 PidExtractorService.cs (핵심 서비스)
```csharp
using Azure.AI.Vision.ImageAnalysis;
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Core.Domain.Entities;
using ExperionCrawler.Infrastructure.Database;
namespace ExperionCrawler.Core.Application.Services;
public class PidExtractorService : IPidExtractorService
{
private readonly string _anthropicApiKey;
private readonly BinaryData _systemPrompt;
private readonly PidDbContext _pidDbContext;
public PidExtractorService(
IConfiguration configuration,
PidDbContext pidDbContext)
{
_anthropicApiKey = configuration["Anthropic:ApiKey"]!;
_pidDbContext = pidDbContext;
_systemPrompt = BinaryData.FromString(GetPrompt());
}
public async Task<PidExtractionResult> ExtractFromFile(string filePath, bool useImageMode = false)
{
// 1. 파일 텍스트/이미지 변환
var imageData = await PreprocessFile(filePath, useImageMode);
// 2. Claude Vision API 분석
using var client = new ImageAnalysisClient(new Uri("https://vision.api.anthropic.com"),
new System.ClientModel.ApiKeyCredential(_anthropicApiKey));
var result = await client.AnalyzeAsync(ImageAnalyzerOptions.Create(
BinaryData.FromBytes(imageData),
ImageAnalysisFeature.RecognizedText | ImageAnalysisFeature.DenseCaption
));
// 3. JSON 파싱 및 검증
var extractedItems = ParseExtractedData(result.Value.Text);
// 4. DB 저장
var dbItems = new List<PidEquipment>();
foreach (var item in extractedItems)
{
// 기존 태그와 매핑 확인
var existingTag = await FindMatchingExperionTag(item.TagNo);
var pidEquipment = new PidEquipment
{
TagNo = item.TagNo,
EquipmentName = item.EquipmentName,
InstrumentType = item.InstrumentType,
LineNumber = item.LineNumber,
PidDrawingNo = item.PidDrawingNo,
Confidence = item.Confidence,
ExperionTagId = existingTag?.Id,
ExtractedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
dbItems.Add(pidEquipment);
}
await _pidDbContext.PidEquipment.AddRangeAsync(dbItems);
await _pidDbContext.SaveChangesAsync();
return new PidExtractionResult
{
TotalCount = dbItems.Count,
ConfidenceItems = dbItems.Count(i => i.Confidence >= 0.7),
LowConfidenceItems = dbItems.Count(i => i.Confidence < 0.5),
CsvPath = $"output/pid_extracted_{DateTime.UtcNow:yyyyMMdd_HHmmss}.csv",
ExcelPath = $"output/pid_AX_import_{DateTime.UtcNow:yyyyMMdd_HHmmss}.xlsx"
};
}
private string GetPrompt()
{
return @"
Analyze the P&ID (Piping and Instrumentation Diagram) drawing and extract the following information.
Return ONLY pure JSON (no markdown, no explanations):
{
""items"": [
{
""tagNo"": ""Tag number (e.g., FT-1001, PT-2003, E-101, CV-123)"",
""equipmentName"": ""Full equipment name (e.g., ""Flow Transmitter"")"",
""instrumentType"": ""Short type code (FT, PT, LT, CV, E, V, P, etc.)"",
""lineNumber"": ""Line reference (e.g., ""6\""-P-1001-A1A"")"",
""pidDrawingNo"": ""P&ID drawing number (if identifiable)""
}
],
""note"": ""Any items that cannot be clearly identified"" // optional
}";
}
}
```
---
## 🌐 PidController.cs (Web API)
```csharp
using Microsoft.AspNetCore.Mvc;
using ExperionCrawler.Core.Application.Interfaces;
using ExperionCrawler.Core.Application.Dtos;
namespace ExperionCrawler.Web.Controllers;
[ApiController]
[Route("api/[controller]")]
public class PidController : ControllerBase
{
private readonly IPidExtractorService _pidExtractor;
private readonly IExperionServices _experionServices;
public PidController(IPidExtractorService pidExtractor,
IExperionServices experionServices)
{
_pidExtractor = pidExtractor;
_experionServices = experionServices;
}
[HttpPost("extract")]
public async Task<IActionResult> ExtractFromFile(IFormFile file, bool useImageMode = false)
{
if (file == null || file.Length == 0)
return BadRequest("파일이 없습니다.");
using var stream = file.OpenReadStream();
var result = await _pidExtractor.ExtractFromStream(stream, file.FileName, useImageMode);
return Ok(new
{
totalCount = result.TotalCount,
confidenceItems = result.ConfidenceItems,
lowConfidenceItems = result.LowConfidenceItems,
csvPath = result.CsvPath,
excelPath = result.ExcelPath
});
}
[HttpGet("equipment")]
public async Task<IActionResult> GetEquipment(string tagNo = null, int page = 1, int pageSize = 50)
{
var query = _pidExtractor.GetQueryable();
if (!string.IsNullOrEmpty(tagNo))
query = query.Where(e => e.TagNo.Contains(tagNo));
var total = await query.CountAsync();
var items = await query
.OrderByDescending(e => e.ExtractedAt)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
return Ok(new
{
total,
page,
pageSize,
confidenceRate = items.Sum(e => e.Confidence) / (items.Count > 0 ? items.Count : 1),
items = items.Select(e => new
{
id = e.Id,
tagNo = e.TagNo,
equipmentName = e.EquipmentName,
instrumentType = e.InstrumentType,
lineNumber = e.LineNumber,
pidDrawingNo = e.PidDrawingNo,
confidence = e.Confidence,
isActive = e.IsActive
})
});
}
[HttpGet("statistics")]
public async Task<IActionResult> GetStatistics()
{
var typeCount = await _pidExtractor.GetInstrumentTypeCount();
var confidenceRange = await _pidExtractor.GetConfidenceDistribution();
var drawingCount = await _pidExtractor.GetDrawingCount();
return Ok(new
{
typeCount,
confidenceRange,
drawingCount
});
}
[HttpPut("{id}/confidence")]
public async Task<IActionResult> UpdateConfidence(long id, decimal confidence)
{
if (confidence < 0 || confidence > 1)
return BadRequest("신뢰도는 0~1 사이어야 합니다.");
await _pidExtractor.UpdateConfidence(id, confidence);
return Ok(new { message = "신뢰도가 업데이트되었습니다." });
}
}
```
---
## 🎨 Frontend UI 확장 (app.js)
```javascript
// P&ID 추출 및 관리 기능
class PidManager {
constructor() {
this.extractorFileInput = document.getElementById('pid-file-input');
this.extractActionBtn = document.getElementById('extract-pid-btn');
this.useImageMode = document.getElementById('use-image-mode');
this.bindEvents();
}
bindEvents() {
this.extractActionBtn.addEventListener('click', () => this.handleExtract());
this.useImageMode.addEventListener('change', (e) => {
this.extractActionBtn.textContent =
e.target.checked ? '이미지 모드로 추출' : '텍스트 모드로 추출';
});
}
async handleExtract() {
const file = this.extractorFileInput.files[0];
if (!file) {
alert('선택된 파일이 없습니다.');
return;
}
const formData = new FormData();
formData.append('file', file);
formData.append('useImageMode', this.useImageMode.checked);
this.extractActionBtn.disabled = true;
this.extractActionBtn.textContent = '추출 중...';
try {
const response = await fetch('/api/pid/extract', {
method: 'POST',
body: formData
});
const result = await response.json();
this.showResult(result);
this.loadEquipmentList();
this.loadStatistics();
alert(`추출 완료! 총 ${result.totalCount}건 처리됨`);
} catch (error) {
console.error('추출 실패:', error);
alert('추출 중 오류가 발생했습니다.');
} finally {
this.extractActionBtn.disabled = false;
}
}
showResult(result) {
// 결과 표시 UI
alert(`${result.totalCount}${result.confidenceItems}건 신뢰도 높음`);
}
}
// 애플리케이션 초기화
document.addEventListener('DOMContentLoaded', () => {
new PidManager();
});
```
---
## 📝 작업 순서
### 단계 1: DB 구조 생성
1. [`PidDbContext.cs`](../src/Infrastructure/Database/PidDbContext.cs) 생성
2. [`PidEquipment.cs`](../src/Core/Domain/Entities/PidEquipment.cs) 엔티티 생성
3. [`PidAuditLog.cs`](../src/Core/Domain/Entities/PidAuditLog.cs) 엔티티 생성
4. [`Program.cs`](../src/Web/Program.cs)에 서비스 등록 (`AddDbContext<PidDbContext>`)
### 단계 2: 커맨드라인 도구 개발
1. [`PidExtractorService.cs`](../src/Core/Application/Services/PidExtractorService.cs) 개발
2. CLIP 기반 추출기 연동 (Python `pid_extractor.py`)
3. 테스트용 DXF/PDF 파일 생성
4. 통합 테스트 수행
### 단계 3: Web API 개발
1. [`IPidExtractorService.cs`](../src/Core/Application/Interfaces/IPidExtractorService.cs) 인터페이스 정의
2. [`PidController.cs`](../src/Web/Controllers/PidController.cs) 개발
3. CSV/Excel 다운로드 엔드포인트
4. 검증된 데이터 필터링 기능
### 단계 4: Firebase 연동
1. P&ID 추출된 태그와 Experion 실시간 태그 매핑
2. 실시간 값 업데이트 동기화
### 단계 5: Frontend UI
1. P&ID 추출 화면 추가
2. 장비 목록 표시 및 필터링
3. 신뢰도 시각화
4. 검토 필요 항목 표시
### 단계 6: 최적화 및 모듈화
1. PDF→이미지 변환 속도 최적화
2. 대용량 파일 처리 스트리밍
3. API 응답 최적화
---
## ⚠️ 주의사항
1. **권한 문제**: `/temp/` 디렉토리에 PDF 변환된 이미지를 저장하므로 쓰기 권한 확인 필요
2. **API 비용**: Claude Vision API 사용 시 비용 발생 가능 → 캐싱 전략 필요
3. **대용량 파일**: DXF 이미지 모드는 느림 → 사용자에게 선택권 제공
4. **네트워크**: Anthropic API 사용을 위해 외부 연결 필요
---
## 📊 성공 지표
- DXF/PDF 파일로부터 평균 성공 추출률 80% 이상
- 100MB 이하 파일 처리 시 응답 시간 30초 이내
- 신뢰도 0.7 이상 항목 자동 검증 기능
- Redis 캐싱으로 API 요청 50% 감소
---
## 🚀 다음 단계
1. 현재 코드 베이스 검토 (`Program.cs`, `ExperionDbContext.cs`)
2. `PID REST API` 기능 우선 구현
3. Frontend 인터페이스
4. Firebase 실시간 연동
5. 모델 최적화 및 테스트

File diff suppressed because it is too large Load Diff

View File

@@ -1,174 +0,0 @@
# P&ID Extractor
DXF / PDF 형식의 P&ID 도면에서 장비 및 계기 정보를 AI로 자동 추출하여
CSV, Excel(AX 포맷), PostgreSQL DB로 저장하는 파이프라인입니다.
---
## 추출 항목
| 필드 | 설명 | 예시 |
|------|------|------|
| Tag No. | 태그번호 | FT-1001, PT-2003, E-101 |
| Equipment Name | 장비명 | Flow Transmitter, Heat Exchanger |
| Instrument Type | 계기타입 | FT, PT, LT, CV, E, V, P |
| Line Number | 라인번호 | 6"-P-1001-A1A |
| P&ID Drawing No. | 도면번호 | P&ID-100-001 |
---
## 설치
### 1. 시스템 패키지 (PDF 변환용 poppler)
```bash
# Ubuntu/Debian
sudo apt-get install poppler-utils
# macOS
brew install poppler
# Windows: https://github.com/oschwartz10612/poppler-windows
```
### 2. Python 패키지
```bash
pip install -r requirements.txt
```
### 3. API 키 설정
```bash
export ANTHROPIC_API_KEY="sk-ant-..."
```
---
## 사용법
### 기본 실행 (DXF 파일)
```bash
python pid_extractor.py input/drawing_001.dxf
```
### PDF 파일
```bash
python pid_extractor.py input/pid_sheet1.pdf input/pid_sheet2.pdf
```
### 여러 파일 + PostgreSQL 저장
```bash
python pid_extractor.py input/*.dxf input/*.pdf \
--db-dsn "postgresql://user:password@localhost:5432/pid_db" \
--output-dir output/
```
### DXF 이미지 모드 (정확도 향상, 느림)
```bash
python pid_extractor.py input/drawing.dxf --image-mode
```
---
## 출력 파일
```
output/
├── pid_extracted_20250426_120000.csv # 전체 추출 데이터
├── pid_AX_import_20250426_120000.xlsx # AX 가져오기용 Excel
└── *.png # 변환된 이미지 파일들
logs/
└── extractor.log # 실행 로그
```
---
## Python API 사용
```python
from pid_extractor import run_pipeline
result = run_pipeline(
input_files=["input/P-001.dxf", "input/P-002.pdf"],
output_dir="output",
db_dsn="postgresql://user:pass@localhost:5432/pid_db", # 선택사항
)
print(f"추출 건수: {result['total']}")
print(f"CSV 저장: {result['csv']}")
print(f"Excel 저장: {result['excel']}")
```
---
## PostgreSQL 스키마
```bash
psql -U postgres -d pid_db -f schema.sql
```
### 주요 쿼리
```sql
-- 계기 타입별 현황
SELECT instrument_type, COUNT(*) FROM pid_equipment GROUP BY instrument_type;
-- 검토 필요 항목 (신뢰도 낮음)
SELECT * FROM pid_equipment WHERE confidence < 0.7;
-- AX 내보내기 뷰
SELECT * FROM ax_export;
-- AX CSV 추출
COPY (SELECT * FROM ax_export) TO '/tmp/ax_import.csv' CSV HEADER;
```
---
## AX (Asset Excellence) 컬럼 매핑
| 추출 필드 | AX 필드명 |
|-----------|-----------|
| tag_no | Tag Number |
| equipment_name | Asset Description |
| instrument_type | Equipment Class |
| pid_drawing_no | P&ID Reference |
| line_number | Line Reference |
| service_description | Service |
---
## 신뢰도(Confidence) 기준
| 색상 | 범위 | 의미 |
|------|------|------|
| 🟢 녹색 | 0.8 ~ 1.0 | 명확하게 읽힘 |
| 🟡 노란색 | 0.5 ~ 0.8 | 부분적으로 읽힘, 검토 권장 |
| 🔴 빨간색 | 0.0 ~ 0.5 | 불명확, 반드시 수동 검토 필요 |
---
## 워크플로우
```
DXF / PDF
이미지/텍스트 변환
Claude Vision API 분석
JSON 파싱 & 정제
├──▶ CSV 저장
├──▶ AX Excel 저장
└──▶ PostgreSQL 저장
```

Some files were not shown because too many files have changed in this diff Show More