diff --git a/backend-node/src/controllers/batchManagementController.ts b/backend-node/src/controllers/batchManagementController.ts index bdd9e869..0845b1cb 100644 --- a/backend-node/src/controllers/batchManagementController.ts +++ b/backend-node/src/controllers/batchManagementController.ts @@ -126,29 +126,41 @@ export class BatchManagementController { */ static async createBatchConfig(req: AuthenticatedRequest, res: Response) { try { - const { batchName, description, cronSchedule, mappings, isActive } = - req.body; + const { + batchName, description, cronSchedule, mappings, isActive, + executionType, nodeFlowId, nodeFlowContext, + } = req.body; + const companyCode = req.user?.companyCode; - if ( - !batchName || - !cronSchedule || - !mappings || - !Array.isArray(mappings) - ) { + if (!batchName || !cronSchedule) { return res.status(400).json({ success: false, - message: - "필수 필드가 누락되었습니다. (batchName, cronSchedule, mappings)", + message: "필수 필드가 누락되었습니다. (batchName, cronSchedule)", }); } - const batchConfig = await BatchService.createBatchConfig({ - batchName, - description, - cronSchedule, - mappings, - isActive: isActive !== undefined ? isActive : true, - } as CreateBatchConfigRequest); + // 노드 플로우 타입은 매핑 없이 생성 가능 + if (executionType !== "node_flow" && (!mappings || !Array.isArray(mappings))) { + return res.status(400).json({ + success: false, + message: "매핑 타입은 mappings 배열이 필요합니다.", + }); + } + + const batchConfig = await BatchService.createBatchConfig( + { + batchName, + description, + cronSchedule, + mappings: mappings || [], + isActive: isActive === false || isActive === "N" ? "N" : "Y", + companyCode: companyCode || "", + executionType: executionType || "mapping", + nodeFlowId: nodeFlowId || null, + nodeFlowContext: nodeFlowContext || null, + } as CreateBatchConfigRequest, + req.user?.userId + ); return res.status(201).json({ success: true, @@ -768,4 +780,287 @@ export class BatchManagementController { }); } } + + /** + * 노드 플로우 목록 조회 (배치 설정에서 플로우 선택용) + * GET /api/batch-management/node-flows + */ + static async getNodeFlows(req: AuthenticatedRequest, res: Response) { + try { + const companyCode = req.user?.companyCode; + + let flowQuery: string; + let flowParams: any[] = []; + + if (companyCode === "*") { + flowQuery = ` + SELECT flow_id, flow_name, flow_description AS description, company_code, + COALESCE(jsonb_array_length( + CASE WHEN flow_data IS NOT NULL AND flow_data::text != '' + THEN (flow_data::jsonb -> 'nodes') + ELSE '[]'::jsonb END + ), 0) AS node_count + FROM node_flows + ORDER BY flow_name + `; + } else { + flowQuery = ` + SELECT flow_id, flow_name, flow_description AS description, company_code, + COALESCE(jsonb_array_length( + CASE WHEN flow_data IS NOT NULL AND flow_data::text != '' + THEN (flow_data::jsonb -> 'nodes') + ELSE '[]'::jsonb END + ), 0) AS node_count + FROM node_flows + WHERE company_code = $1 + ORDER BY flow_name + `; + flowParams = [companyCode]; + } + + const result = await query(flowQuery, flowParams); + return res.json({ success: true, data: result }); + } catch (error) { + console.error("노드 플로우 목록 조회 오류:", error); + return res.status(500).json({ + success: false, + message: "노드 플로우 목록 조회 실패", + error: error instanceof Error ? error.message : "알 수 없는 오류", + }); + } + } + + /** + * 배치 대시보드 통계 조회 + * GET /api/batch-management/stats + * totalBatches, activeBatches, todayExecutions, todayFailures, prevDayExecutions, prevDayFailures + * 멀티테넌시: company_code 필터링 필수 + */ + static async getBatchStats(req: AuthenticatedRequest, res: Response) { + try { + const companyCode = req.user?.companyCode; + + // 전체/활성 배치 수 + let configQuery: string; + let configParams: any[] = []; + if (companyCode === "*") { + configQuery = ` + SELECT + COUNT(*)::int AS total, + COUNT(*) FILTER (WHERE is_active = 'Y')::int AS active + FROM batch_configs + `; + } else { + configQuery = ` + SELECT + COUNT(*)::int AS total, + COUNT(*) FILTER (WHERE is_active = 'Y')::int AS active + FROM batch_configs + WHERE company_code = $1 + `; + configParams = [companyCode]; + } + const configResult = await query<{ total: number; active: number }>( + configQuery, + configParams + ); + + // 오늘/어제 실행·실패 수 (KST 기준 날짜) + const logParams: any[] = []; + let logWhere = ""; + if (companyCode && companyCode !== "*") { + logWhere = " AND company_code = $1"; + logParams.push(companyCode); + } + const todayLogQuery = ` + SELECT + COUNT(*)::int AS today_executions, + COUNT(*) FILTER (WHERE execution_status = 'FAILED')::int AS today_failures + FROM batch_execution_logs + WHERE (start_time AT TIME ZONE 'Asia/Seoul')::date = (NOW() AT TIME ZONE 'Asia/Seoul')::date + ${logWhere} + `; + const prevDayLogQuery = ` + SELECT + COUNT(*)::int AS prev_executions, + COUNT(*) FILTER (WHERE execution_status = 'FAILED')::int AS prev_failures + FROM batch_execution_logs + WHERE (start_time AT TIME ZONE 'Asia/Seoul')::date = (NOW() AT TIME ZONE 'Asia/Seoul')::date - INTERVAL '1 day' + ${logWhere} + `; + const [todayResult, prevResult] = await Promise.all([ + query<{ today_executions: number; today_failures: number }>( + todayLogQuery, + logParams + ), + query<{ prev_executions: number; prev_failures: number }>( + prevDayLogQuery, + logParams + ), + ]); + + const config = configResult[0]; + const today = todayResult[0]; + const prev = prevResult[0]; + + return res.json({ + success: true, + data: { + totalBatches: config?.total ?? 0, + activeBatches: config?.active ?? 0, + todayExecutions: today?.today_executions ?? 0, + todayFailures: today?.today_failures ?? 0, + prevDayExecutions: prev?.prev_executions ?? 0, + prevDayFailures: prev?.prev_failures ?? 0, + }, + }); + } catch (error) { + console.error("배치 통계 조회 오류:", error); + return res.status(500).json({ + success: false, + message: "배치 통계 조회 실패", + error: error instanceof Error ? error.message : "알 수 없는 오류", + }); + } + } + + /** + * 배치별 최근 24시간 스파크라인 (1시간 단위 집계) + * GET /api/batch-management/batch-configs/:id/sparkline + * 멀티테넌시: company_code 필터링 필수 + */ + static async getBatchSparkline(req: AuthenticatedRequest, res: Response) { + try { + const { id } = req.params; + const companyCode = req.user?.companyCode; + const batchId = Number(id); + if (!id || isNaN(batchId)) { + return res.status(400).json({ + success: false, + message: "올바른 배치 ID를 제공해주세요.", + }); + } + + const params: any[] = [batchId]; + let companyFilter = ""; + if (companyCode && companyCode !== "*") { + companyFilter = " AND bel.company_code = $2"; + params.push(companyCode); + } + + // KST 기준 최근 24시간 1시간 단위 슬롯 + 집계 (generate_series로 24개 보장) + const sparklineQuery = ` + WITH kst_slots AS ( + SELECT to_char(s, 'YYYY-MM-DD"T"HH24:00:00') AS hour + FROM generate_series( + (NOW() AT TIME ZONE 'Asia/Seoul') - INTERVAL '23 hours', + (NOW() AT TIME ZONE 'Asia/Seoul'), + INTERVAL '1 hour' + ) AS s + ), + agg AS ( + SELECT + to_char(date_trunc('hour', (bel.start_time AT TIME ZONE 'Asia/Seoul')) AT TIME ZONE 'Asia/Seoul', 'YYYY-MM-DD"T"HH24:00:00') AS hour, + COUNT(*) FILTER (WHERE bel.execution_status = 'SUCCESS')::int AS success, + COUNT(*) FILTER (WHERE bel.execution_status = 'FAILED')::int AS failed + FROM batch_execution_logs bel + WHERE bel.batch_config_id = $1 + AND bel.start_time >= (NOW() AT TIME ZONE 'Asia/Seoul') - INTERVAL '24 hours' + ${companyFilter} + GROUP BY date_trunc('hour', (bel.start_time AT TIME ZONE 'Asia/Seoul')) + ) + SELECT + k.hour, + COALESCE(a.success, 0) AS success, + COALESCE(a.failed, 0) AS failed + FROM kst_slots k + LEFT JOIN agg a ON k.hour = a.hour + ORDER BY k.hour + `; + const data = await query<{ + hour: string; + success: number; + failed: number; + }>(sparklineQuery, params); + + return res.json({ success: true, data }); + } catch (error) { + console.error("스파크라인 조회 오류:", error); + return res.status(500).json({ + success: false, + message: "스파크라인 데이터 조회 실패", + error: error instanceof Error ? error.message : "알 수 없는 오류", + }); + } + } + + /** + * 배치별 최근 실행 로그 (최대 20건) + * GET /api/batch-management/batch-configs/:id/recent-logs + * 멀티테넌시: company_code 필터링 필수 + */ + static async getBatchRecentLogs(req: AuthenticatedRequest, res: Response) { + try { + const { id } = req.params; + const companyCode = req.user?.companyCode; + const batchId = Number(id); + const limit = Math.min(Number(req.query.limit) || 20, 20); + if (!id || isNaN(batchId)) { + return res.status(400).json({ + success: false, + message: "올바른 배치 ID를 제공해주세요.", + }); + } + + let logsQuery: string; + let logsParams: any[]; + if (companyCode === "*") { + logsQuery = ` + SELECT + id, + start_time AS started_at, + end_time AS finished_at, + execution_status AS status, + total_records, + success_records, + failed_records, + error_message, + duration_ms + FROM batch_execution_logs + WHERE batch_config_id = $1 + ORDER BY start_time DESC + LIMIT $2 + `; + logsParams = [batchId, limit]; + } else { + logsQuery = ` + SELECT + id, + start_time AS started_at, + end_time AS finished_at, + execution_status AS status, + total_records, + success_records, + failed_records, + error_message, + duration_ms + FROM batch_execution_logs + WHERE batch_config_id = $1 AND company_code = $2 + ORDER BY start_time DESC + LIMIT $3 + `; + logsParams = [batchId, companyCode, limit]; + } + + const result = await query(logsQuery, logsParams); + return res.json({ success: true, data: result }); + } catch (error) { + console.error("최근 실행 이력 조회 오류:", error); + return res.status(500).json({ + success: false, + message: "최근 실행 이력 조회 실패", + error: error instanceof Error ? error.message : "알 수 없는 오류", + }); + } + } } diff --git a/backend-node/src/routes/batchManagementRoutes.ts b/backend-node/src/routes/batchManagementRoutes.ts index 50ee1ea0..372113c0 100644 --- a/backend-node/src/routes/batchManagementRoutes.ts +++ b/backend-node/src/routes/batchManagementRoutes.ts @@ -7,6 +7,19 @@ import { authenticateToken } from "../middleware/authMiddleware"; const router = Router(); +/** + * GET /api/batch-management/stats + * 배치 대시보드 통계 (전체/활성 배치 수, 오늘·어제 실행/실패 수) + * 반드시 /batch-configs 보다 위에 등록 (/:id로 잡히지 않도록) + */ +router.get("/stats", authenticateToken, BatchManagementController.getBatchStats); + +/** + * GET /api/batch-management/node-flows + * 배치 설정에서 노드 플로우 선택용 목록 조회 + */ +router.get("/node-flows", authenticateToken, BatchManagementController.getNodeFlows); + /** * GET /api/batch-management/connections * 사용 가능한 커넥션 목록 조회 @@ -55,6 +68,18 @@ router.get("/batch-configs", authenticateToken, BatchManagementController.getBat */ router.get("/batch-configs/:id", authenticateToken, BatchManagementController.getBatchConfigById); +/** + * GET /api/batch-management/batch-configs/:id/sparkline + * 해당 배치 최근 24시간 1시간 단위 실행 집계 + */ +router.get("/batch-configs/:id/sparkline", authenticateToken, BatchManagementController.getBatchSparkline); + +/** + * GET /api/batch-management/batch-configs/:id/recent-logs + * 해당 배치 최근 실행 로그 (최대 20건) + */ +router.get("/batch-configs/:id/recent-logs", authenticateToken, BatchManagementController.getBatchRecentLogs); + /** * PUT /api/batch-management/batch-configs/:id * 배치 설정 업데이트 diff --git a/backend-node/src/routes/dataflow/node-flows.ts b/backend-node/src/routes/dataflow/node-flows.ts index 30fffd7b..4180f977 100644 --- a/backend-node/src/routes/dataflow/node-flows.ts +++ b/backend-node/src/routes/dataflow/node-flows.ts @@ -13,7 +13,54 @@ import { auditLogService, getClientIp } from "../../services/auditLogService"; const router = Router(); /** - * 플로우 목록 조회 + * flow_data에서 요약 정보 추출 + */ +function extractFlowSummary(flowData: any) { + try { + const parsed = typeof flowData === "string" ? JSON.parse(flowData) : flowData; + const nodes = parsed?.nodes || []; + const edges = parsed?.edges || []; + + const nodeTypes: Record = {}; + nodes.forEach((n: any) => { + const t = n.type || "unknown"; + nodeTypes[t] = (nodeTypes[t] || 0) + 1; + }); + + // 미니 토폴로지용 간소화된 좌표 (0~1 정규화) + let topology = null; + if (nodes.length > 0) { + const xs = nodes.map((n: any) => n.position?.x || 0); + const ys = nodes.map((n: any) => n.position?.y || 0); + const minX = Math.min(...xs), maxX = Math.max(...xs); + const minY = Math.min(...ys), maxY = Math.max(...ys); + const rangeX = maxX - minX || 1; + const rangeY = maxY - minY || 1; + + topology = { + nodes: nodes.map((n: any) => ({ + id: n.id, + type: n.type, + x: (((n.position?.x || 0) - minX) / rangeX), + y: (((n.position?.y || 0) - minY) / rangeY), + })), + edges: edges.map((e: any) => [e.source, e.target]), + }; + } + + return { + nodeCount: nodes.length, + edgeCount: edges.length, + nodeTypes, + topology, + }; + } catch { + return { nodeCount: 0, edgeCount: 0, nodeTypes: {}, topology: null }; + } +} + +/** + * 플로우 목록 조회 (summary 포함) */ router.get("/", async (req: AuthenticatedRequest, res: Response) => { try { @@ -24,6 +71,7 @@ router.get("/", async (req: AuthenticatedRequest, res: Response) => { flow_id as "flowId", flow_name as "flowName", flow_description as "flowDescription", + flow_data as "flowData", company_code as "companyCode", created_at as "createdAt", updated_at as "updatedAt" @@ -32,7 +80,6 @@ router.get("/", async (req: AuthenticatedRequest, res: Response) => { const params: any[] = []; - // 슈퍼 관리자가 아니면 회사별 필터링 if (userCompanyCode && userCompanyCode !== "*") { sqlQuery += ` WHERE company_code = $1`; params.push(userCompanyCode); @@ -42,9 +89,15 @@ router.get("/", async (req: AuthenticatedRequest, res: Response) => { const flows = await query(sqlQuery, params); + const flowsWithSummary = flows.map((flow: any) => { + const summary = extractFlowSummary(flow.flowData); + const { flowData, ...rest } = flow; + return { ...rest, summary }; + }); + return res.json({ success: true, - data: flows, + data: flowsWithSummary, }); } catch (error) { logger.error("플로우 목록 조회 실패:", error); diff --git a/backend-node/src/services/batchSchedulerService.ts b/backend-node/src/services/batchSchedulerService.ts index f6fe56a1..8feba9d9 100644 --- a/backend-node/src/services/batchSchedulerService.ts +++ b/backend-node/src/services/batchSchedulerService.ts @@ -122,20 +122,22 @@ export class BatchSchedulerService { } /** - * 배치 설정 실행 + * 배치 설정 실행 - execution_type에 따라 매핑 또는 노드 플로우 실행 */ static async executeBatchConfig(config: any) { const startTime = new Date(); let executionLog: any = null; try { - logger.info(`배치 실행 시작: ${config.batch_name} (ID: ${config.id})`); + logger.info(`배치 실행 시작: ${config.batch_name} (ID: ${config.id}, type: ${config.execution_type || "mapping"})`); - // 매핑 정보가 없으면 상세 조회로 다시 가져오기 - if (!config.batch_mappings || config.batch_mappings.length === 0) { - const fullConfig = await BatchService.getBatchConfigById(config.id); - if (fullConfig.success && fullConfig.data) { - config = fullConfig.data; + // 상세 조회 (매핑 또는 노드플로우 정보가 없을 수 있음) + if (!config.execution_type || config.execution_type === "mapping") { + if (!config.batch_mappings || config.batch_mappings.length === 0) { + const fullConfig = await BatchService.getBatchConfigById(config.id); + if (fullConfig.success && fullConfig.data) { + config = fullConfig.data; + } } } @@ -165,12 +167,17 @@ export class BatchSchedulerService { executionLog = executionLogResponse.data; - // 실제 배치 실행 로직 (수동 실행과 동일한 로직 사용) - const result = await this.executeBatchMappings(config); + let result: { totalRecords: number; successRecords: number; failedRecords: number }; + + if (config.execution_type === "node_flow") { + result = await this.executeNodeFlow(config); + } else { + result = await this.executeBatchMappings(config); + } // 실행 로그 업데이트 (성공) await BatchExecutionLogService.updateExecutionLog(executionLog.id, { - execution_status: "SUCCESS", + execution_status: result.failedRecords > 0 ? "PARTIAL" : "SUCCESS", end_time: new Date(), duration_ms: Date.now() - startTime.getTime(), total_records: result.totalRecords, @@ -182,12 +189,10 @@ export class BatchSchedulerService { `배치 실행 완료: ${config.batch_name} (처리된 레코드: ${result.totalRecords})` ); - // 성공 결과 반환 return result; } catch (error) { logger.error(`배치 실행 중 오류 발생: ${config.batch_name}`, error); - // 실행 로그 업데이트 (실패) if (executionLog) { await BatchExecutionLogService.updateExecutionLog(executionLog.id, { execution_status: "FAILED", @@ -198,7 +203,6 @@ export class BatchSchedulerService { }); } - // 실패 결과 반환 return { totalRecords: 0, successRecords: 0, @@ -207,6 +211,43 @@ export class BatchSchedulerService { } } + /** + * 노드 플로우 실행 - NodeFlowExecutionService에 위임 + */ + private static async executeNodeFlow(config: any) { + if (!config.node_flow_id) { + throw new Error("노드 플로우 ID가 설정되지 않았습니다."); + } + + const { NodeFlowExecutionService } = await import( + "./nodeFlowExecutionService" + ); + + const contextData: Record = { + companyCode: config.company_code, + batchConfigId: config.id, + batchName: config.batch_name, + executionSource: "batch_scheduler", + ...(config.node_flow_context || {}), + }; + + logger.info( + `노드 플로우 실행: flowId=${config.node_flow_id}, batch=${config.batch_name}` + ); + + const flowResult = await NodeFlowExecutionService.executeFlow( + config.node_flow_id, + contextData + ); + + // 노드 플로우 실행 결과를 배치 로그 형식으로 변환 + return { + totalRecords: flowResult.summary.total, + successRecords: flowResult.summary.success, + failedRecords: flowResult.summary.failed, + }; + } + /** * 배치 매핑 실행 (수동 실행과 동일한 로직) */ diff --git a/backend-node/src/services/batchService.ts b/backend-node/src/services/batchService.ts index 31ee2001..c8b6ecbe 100644 --- a/backend-node/src/services/batchService.ts +++ b/backend-node/src/services/batchService.ts @@ -72,9 +72,12 @@ export class BatchService { const total = parseInt(countResult[0].count); const totalPages = Math.ceil(total / limit); - // 목록 조회 + // 목록 조회 (최근 실행 정보 포함) const configs = await query( - `SELECT bc.* + `SELECT bc.*, + (SELECT bel.execution_status FROM batch_execution_logs bel WHERE bel.batch_config_id = bc.id ORDER BY bel.start_time DESC LIMIT 1) as last_status, + (SELECT bel.start_time FROM batch_execution_logs bel WHERE bel.batch_config_id = bc.id ORDER BY bel.start_time DESC LIMIT 1) as last_executed_at, + (SELECT bel.total_records FROM batch_execution_logs bel WHERE bel.batch_config_id = bc.id ORDER BY bel.start_time DESC LIMIT 1) as last_total_records FROM batch_configs bc ${whereClause} ORDER BY bc.created_date DESC @@ -82,9 +85,6 @@ export class BatchService { [...values, limit, offset] ); - // 매핑 정보 조회 (N+1 문제 해결을 위해 별도 쿼리 대신 여기서는 생략하고 상세 조회에서 처리) - // 하지만 목록에서도 간단한 정보는 필요할 수 있음 - return { success: true, data: configs as BatchConfig[], @@ -176,8 +176,8 @@ export class BatchService { // 배치 설정 생성 const batchConfigResult = await client.query( `INSERT INTO batch_configs - (batch_name, description, cron_schedule, is_active, company_code, save_mode, conflict_key, auth_service_name, data_array_path, created_by, created_date, updated_date) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, NOW(), NOW()) + (batch_name, description, cron_schedule, is_active, company_code, save_mode, conflict_key, auth_service_name, data_array_path, execution_type, node_flow_id, node_flow_context, created_by, created_date, updated_date) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, NOW(), NOW()) RETURNING *`, [ data.batchName, @@ -189,6 +189,9 @@ export class BatchService { data.conflictKey || null, data.authServiceName || null, data.dataArrayPath || null, + data.executionType || "mapping", + data.nodeFlowId || null, + data.nodeFlowContext ? JSON.stringify(data.nodeFlowContext) : null, userId, ] ); @@ -332,6 +335,22 @@ export class BatchService { updateFields.push(`data_array_path = $${paramIndex++}`); updateValues.push(data.dataArrayPath || null); } + if (data.executionType !== undefined) { + updateFields.push(`execution_type = $${paramIndex++}`); + updateValues.push(data.executionType); + } + if (data.nodeFlowId !== undefined) { + updateFields.push(`node_flow_id = $${paramIndex++}`); + updateValues.push(data.nodeFlowId || null); + } + if (data.nodeFlowContext !== undefined) { + updateFields.push(`node_flow_context = $${paramIndex++}`); + updateValues.push( + data.nodeFlowContext + ? JSON.stringify(data.nodeFlowContext) + : null + ); + } // 배치 설정 업데이트 const batchConfigResult = await client.query( diff --git a/backend-node/src/types/batchTypes.ts b/backend-node/src/types/batchTypes.ts index a6404036..9933194b 100644 --- a/backend-node/src/types/batchTypes.ts +++ b/backend-node/src/types/batchTypes.ts @@ -79,6 +79,9 @@ export interface BatchMapping { created_date?: Date; } +// 배치 실행 타입: 기존 매핑 방식 또는 노드 플로우 실행 +export type BatchExecutionType = "mapping" | "node_flow"; + // 배치 설정 타입 export interface BatchConfig { id?: number; @@ -87,15 +90,21 @@ export interface BatchConfig { cron_schedule: string; is_active: "Y" | "N"; company_code?: string; - save_mode?: "INSERT" | "UPSERT"; // 저장 모드 (기본: INSERT) - conflict_key?: string; // UPSERT 시 충돌 기준 컬럼명 - auth_service_name?: string; // REST API 인증에 사용할 토큰 서비스명 - data_array_path?: string; // REST API 응답에서 데이터 배열 경로 (예: response, data.items) + save_mode?: "INSERT" | "UPSERT"; + conflict_key?: string; + auth_service_name?: string; + data_array_path?: string; + execution_type?: BatchExecutionType; + node_flow_id?: number; + node_flow_context?: Record; created_by?: string; created_date?: Date; updated_by?: string; updated_date?: Date; batch_mappings?: BatchMapping[]; + last_status?: string; + last_executed_at?: string; + last_total_records?: number; } export interface BatchConnectionInfo { @@ -149,7 +158,10 @@ export interface CreateBatchConfigRequest { saveMode?: "INSERT" | "UPSERT"; conflictKey?: string; authServiceName?: string; - dataArrayPath?: string; // REST API 응답에서 데이터 배열 경로 + dataArrayPath?: string; + executionType?: BatchExecutionType; + nodeFlowId?: number; + nodeFlowContext?: Record; mappings: BatchMappingRequest[]; } @@ -161,7 +173,10 @@ export interface UpdateBatchConfigRequest { saveMode?: "INSERT" | "UPSERT"; conflictKey?: string; authServiceName?: string; - dataArrayPath?: string; // REST API 응답에서 데이터 배열 경로 + dataArrayPath?: string; + executionType?: BatchExecutionType; + nodeFlowId?: number; + nodeFlowContext?: Record; mappings?: BatchMappingRequest[]; } diff --git a/docs/kjs/배치_노드플로우_연동_계획서.md b/docs/kjs/배치_노드플로우_연동_계획서.md new file mode 100644 index 00000000..97630229 --- /dev/null +++ b/docs/kjs/배치_노드플로우_연동_계획서.md @@ -0,0 +1,909 @@ +# 배치 스케줄러 + 노드 플로우 연동 계획서 + +## 1. 배경 및 목적 + +### 현재 상태 + +현재 시스템에는 두 개의 독립적인 실행 엔진이 있다: + +| 시스템 | 역할 | 트리거 방식 | +|--------|------|-------------| +| **배치 스케줄러** | Cron 기반 자동 실행 (데이터 복사만 가능) | 시간 기반 (node-cron) | +| **노드 플로우 엔진** | 조건/변환/INSERT/UPDATE/DELETE 등 복합 로직 | 버튼 클릭 (수동) | + +### 문제 + +- 배치는 **INSERT/UPSERT만** 가능하고, 조건 기반 UPDATE/DELETE를 못 함 +- 노드 플로우는 강력하지만 **수동 실행만** 가능 (버튼 클릭 필수) +- "퇴사일이 지나면 자동으로 퇴사 처리" 같은 **시간 기반 비즈니스 로직**을 구현할 수 없음 + +### 목표 + +배치 스케줄러가 노드 플로우를 자동 실행할 수 있도록 연동하여, +시간 기반 비즈니스 로직 자동화를 지원한다. + +``` +[배치 스케줄러] ──Cron 트리거──> [노드 플로우 실행 엔진] + │ │ + │ ├── 테이블 소스 조회 + │ ├── 조건 분기 + │ ├── UPDATE / DELETE / INSERT + │ ├── 이메일 발송 + │ └── 로깅 + │ + └── 실행 로그 기록 (batch_execution_logs) +``` + +--- + +## 2. 사용 시나리오 + +### 시나리오 A: 자동 퇴사 처리 + +``` +매일 자정 실행: + 1. user_info에서 퇴사일 <= NOW() AND 상태 != '퇴사' 인 사람 조회 + 2. 해당 사용자의 상태를 '퇴사'로 UPDATE + 3. 관리자에게 이메일 알림 발송 +``` + +### 시나리오 B: 월말 재고 마감 + +``` +매월 1일 00:00 실행: + 1. 전월 재고 데이터를 재고마감 테이블로 INSERT + 2. 이월 수량 계산 후 UPDATE +``` + +### 시나리오 C: 미납 알림 + +``` +매일 09:00 실행: + 1. 납기일이 지난 미납 주문 조회 + 2. 담당자에게 이메일 발송 + 3. 알림 로그 INSERT +``` + +### 시나리오 D: 외부 API 연동 자동화 + +``` +매시간 실행: + 1. 외부 REST API에서 데이터 조회 + 2. 조건 필터링 (변경된 데이터만) + 3. 내부 테이블에 UPSERT +``` + +--- + +## 3. 구현 범위 + +### 3.1 DB 변경 (batch_configs 테이블 확장) + +```sql +-- batch_configs 테이블에 컬럼 추가 +ALTER TABLE batch_configs + ADD COLUMN execution_type VARCHAR(20) DEFAULT 'mapping', + ADD COLUMN node_flow_id INTEGER DEFAULT NULL, + ADD COLUMN node_flow_context JSONB DEFAULT NULL; + +-- execution_type: 'mapping' (기존 데이터 복사) | 'node_flow' (노드 플로우 실행) +-- node_flow_id: node_flows 테이블의 flow_id (FK) +-- node_flow_context: 플로우 실행 시 전달할 컨텍스트 데이터 (선택) + +COMMENT ON COLUMN batch_configs.execution_type IS '실행 타입: mapping(기존 데이터 복사), node_flow(노드 플로우 실행)'; +COMMENT ON COLUMN batch_configs.node_flow_id IS '연결된 노드 플로우 ID (execution_type이 node_flow일 때 사용)'; +COMMENT ON COLUMN batch_configs.node_flow_context IS '플로우 실행 시 전달할 컨텍스트 데이터 (JSON)'; +``` + +기존 데이터에 영향 없음 (`DEFAULT 'mapping'`으로 하위 호환성 보장) + +### 3.2 백엔드 변경 + +#### BatchSchedulerService 수정 (핵심) + +`executeBatchConfig()` 메서드에서 `execution_type` 분기: + +``` +executeBatchConfig(config) + ├── config.execution_type === 'mapping' + │ └── 기존 executeBatchMappings() (변경 없음) + │ + └── config.execution_type === 'node_flow' + └── NodeFlowExecutionService.executeFlow() + ├── 노드 플로우 조회 + ├── 위상 정렬 + ├── 레벨별 실행 + └── 결과 반환 +``` + +수정 파일: +- `backend-node/src/services/batchSchedulerService.ts` + - `executeBatchConfig()` 에 node_flow 분기 추가 + - 노드 플로우 실행 결과를 배치 로그 형식으로 변환 + +#### 배치 설정 API 수정 + +수정 파일: +- `backend-node/src/types/batchTypes.ts` + - `BatchConfig` 인터페이스에 `execution_type`, `node_flow_id`, `node_flow_context` 추가 + - `CreateBatchConfigRequest`, `UpdateBatchConfigRequest` 에도 추가 +- `backend-node/src/services/batchService.ts` + - `createBatchConfig()` - 새 필드 INSERT + - `updateBatchConfig()` - 새 필드 UPDATE +- `backend-node/src/controllers/batchManagementController.ts` + - 생성/수정 시 새 필드 처리 + +#### 노드 플로우 목록 API (배치용) + +추가 파일/수정: +- `backend-node/src/routes/batchManagementRoutes.ts` + - `GET /api/batch-management/node-flows` 추가 (배치 설정 UI에서 플로우 선택용) + +### 3.3 프론트엔드 변경 + +#### 배치 생성/편집 UI 수정 + +수정 파일: +- `frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx` +- `frontend/app/(main)/admin/automaticMng/batchmngList/edit/[id]/page.tsx` + +변경 내용: +- "실행 타입" 선택 추가 (기존 매핑 / 노드 플로우) +- 노드 플로우 선택 시: 플로우 드롭다운 표시 (기존 매핑 설정 숨김) +- 노드 플로우 선택 시: 컨텍스트 데이터 입력 (선택사항, JSON) + +``` +┌─────────────────────────────────────────┐ +│ 배치 설정 │ +├─────────────────────────────────────────┤ +│ 배치명: [자동 퇴사 처리 ] │ +│ 설명: [퇴사일 경과 사용자 자동 처리] │ +│ Cron: [0 0 * * * ] │ +│ │ +│ 실행 타입: ○ 데이터 매핑 ● 노드 플로우 │ +│ │ +│ ┌─ 노드 플로우 선택 ─────────────────┐ │ +│ │ [▾ 자동 퇴사 처리 플로우 ] │ │ +│ │ │ │ +│ │ 플로우 설명: user_info에서 퇴사일..│ │ +│ │ 노드 수: 4개 │ │ +│ └────────────────────────────────────┘ │ +│ │ +│ [취소] [저장] │ +└─────────────────────────────────────────┘ +``` + +#### 배치 목록 UI - Ops 대시보드 리디자인 + +현재 배치 목록은 단순 테이블인데, Vercel/Railway 스타일의 **운영 대시보드**로 전면 리디자인한다. +노드 플로우 연동과 함께 적용하면 새로운 실행 타입도 자연스럽게 표현 가능. + +디자인 컨셉: **"편집기"가 아닌 "운영 대시보드"** +- 데이터 타입 관리 = 컬럼 편집기 → 3패널(리스트/그리드/설정)이 적합 +- 배치 관리 = 운영 모니터링 → 테이블 + 인라인 상태 표시가 적합 +- 역할이 다르면 레이아웃도 달라야 함 + +--- + +##### 전체 레이아웃 + +``` +┌──────────────────────────────────────────────────────────────┐ +│ [헤더] 배치 관리 [새로고침] [새 배치] │ +│ └ 데이터 동기화 배치 작업을 모니터링하고 관리합니다 │ +├──────────────────────────────────────────────────────────────┤ +│ [통계 카드 4열 그리드] │ +│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ +│ │ 전체 배치 │ │ 활성 배치 │ │ 오늘 실행 │ │ 오늘 실패 │ │ +│ │ 8 │ │ 6 │ │ 142 │ │ 3 │ │ +│ │ +2 이번달│ │ 2 비활성 │ │+12% 전일 │ │+1 전일 │ │ +│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ +├──────────────────────────────────────────────────────────────┤ +│ [툴바] │ +│ 🔍 검색... [전체|활성|비활성] [전체|DB-DB|API-DB|플로우] 총 8건 │ +├──────────────────────────────────────────────────────────────┤ +│ [테이블 헤더] │ +│ ● 배치 타입 스케줄 최근24h 마지막실행 │ +├──────────────────────────────────────────────────────────────┤ +│ ● 품목 마스터 동기화 DB→DB */30**** ▌▌▌▐▌▌▌ 14:30 ▶✎🗑 │ +│ ┌────────────────────────────────────────────────────────┐ │ +│ │ [확장 상세 패널 - 클릭 시 토글] │ │ +│ │ 내러티브 + 파이프라인 + 매핑 + 설정 + 타임라인 │ │ +│ └────────────────────────────────────────────────────────┘ │ +│ ● 거래처 ERP 연동 API→DB 0*/2*** ▌▌▌▌▌▌▌ 14:00 ▶✎🗑 │ +│ ◉ 재고 현황 수집 API→DB 06,18** ▌▌▐▌▌▌░ 실행중 ▶✎🗑 │ +│ ○ BOM 백업 DB→DB 0 3**0 ░░░░░░░ 비활성 ▶✎🗑 │ +│ ... │ +└──────────────────────────────────────────────────────────────┘ +``` + +--- + +##### 1. 페이지 헤더 + +``` +구조: flex, align-items: flex-end, justify-content: space-between +하단 보더: 1px solid border +하단 마진: 24px + +좌측: + - 제목: "배치 관리" (text-xl font-extrabold tracking-tight) + - 부제: "데이터 동기화 배치 작업을 모니터링하고 관리합니다" (text-xs text-muted-foreground) + +우측 버튼 그룹 (gap-2): + - [새로고침] 버튼: variant="outline", RefreshCw 아이콘 + - [새 배치] 버튼: variant="default" (primary), Plus 아이콘 +``` + +--- + +##### 2. 통계 카드 영역 + +``` +레이아웃: grid grid-cols-4 gap-3 +각 카드: rounded-xl border bg-card p-4 + +카드 구조: + ┌──────────────────────────┐ + │ [라벨] [아이콘] │ ← stat-top: flex justify-between + │ │ + │ 숫자값 (28px 모노 볼드) │ ← stat-val: font-mono text-3xl font-extrabold + │ │ + │ [변화량 배지] 기간 텍스트 │ ← stat-footer: flex items-center gap-1.5 + └──────────────────────────┘ + +4개 카드 상세: +┌─────────────┬────────────┬───────────────────────────────┐ +│ 카드 │ 아이콘 색상 │ 값 색상 │ +├─────────────┼────────────┼───────────────────────────────┤ +│ 전체 배치 │ indigo bg │ foreground (기본) │ +│ 활성 배치 │ green bg │ green (--success) │ +│ 오늘 실행 │ cyan bg │ cyan (--info 계열) │ +│ 오늘 실패 │ red bg │ red (--destructive) │ +└─────────────┴────────────┴───────────────────────────────┘ + +변화량 배지: + - 증가: green 배경 + green 텍스트, "+N" 또는 "+N%" + - 감소/악화: red 배경 + red 텍스트 + - 크기: text-[10px] font-bold px-1.5 py-0.5 rounded + +아이콘 박스: 28x28px rounded-lg, 배경색 투명도 10% +아이콘: lucide-react (LayoutGrid, CheckCircle, Activity, XCircle) +``` + +**데이터 소스:** +``` +GET /api/batch-management/stats +→ { + totalBatches: number, // batch_configs COUNT(*) + activeBatches: number, // batch_configs WHERE is_active='Y' + todayExecutions: number, // batch_execution_logs WHERE DATE(start_time)=TODAY + todayFailures: number, // batch_execution_logs WHERE DATE(start_time)=TODAY AND status='FAILED' + // 선택사항: 전일 대비 변화량 + prevDayExecutions?: number, + prevDayFailures?: number + } +``` + +--- + +##### 3. 툴바 + +``` +레이아웃: flex items-center gap-2.5 + +요소 1 - 검색: + - 위치: 좌측, flex-1 max-w-[320px] + - 구조: relative div + input + Search 아이콘(absolute left) + - input: h-9, rounded-lg, border, bg-card, text-xs + - placeholder: "배치 이름으로 검색..." + - focus: ring-2 ring-primary + +요소 2 - 상태 필터 (pill-group): + - 컨테이너: flex gap-0.5, bg-card, border, rounded-lg, p-0.5 + - 각 pill: text-[11px] font-semibold px-3 py-1.5 rounded-md + - 활성 pill: bg-primary/10 text-primary + - 비활성 pill: text-muted-foreground, hover시 밝아짐 + - 항목: [전체] [활성] [비활성] + +요소 3 - 타입 필터 (pill-group): + - 동일 스타일 + - 항목: [전체] [DB-DB] [API-DB] [노드 플로우] ← 노드 플로우는 신규 + +요소 4 - 건수 표시: + - 위치: ml-auto (우측 정렬) + - 텍스트: "총 N건" (text-[11px] text-muted-foreground, N은 font-bold) +``` + +--- + +##### 4. 배치 테이블 + +``` +컨테이너: border rounded-xl overflow-hidden bg-card + +테이블 헤더: + - 배경: bg-muted/50 + - 높이: 40px + - 글자: text-[10px] font-bold text-muted-foreground uppercase tracking-wider + - 그리드 컬럼: 44px 1fr 100px 130px 160px 100px 120px + - 컬럼: [LED] [배치] [타입] [스케줄] [최근 24h] [마지막 실행] [액션] +``` + +--- + +##### 5. 배치 테이블 행 (핵심) + +``` +그리드: 44px 1fr 100px 130px 160px 100px 120px +높이: min-height 60px +하단 보더: 1px solid border +hover: bg-card/80 (약간 밝아짐) +선택됨: bg-primary/10 + 좌측 3px primary 박스 섀도우 (inset) +클릭 시: 상세 패널 토글 + +[셀 1] LED 상태 표시: + ┌──────────────────────────────────────┐ + │ 원형 8x8px, 센터 정렬 │ + │ │ + │ 활성(on): green + box-shadow glow │ + │ 실행중(run): amber + 1.5s blink 애니 │ + │ 비활성(off): muted-foreground (회색) │ + │ 에러(err): red + box-shadow glow │ + └──────────────────────────────────────┘ + +[셀 2] 배치 정보: + ┌──────────────────────────────────────┐ + │ 배치명: text-[13px] font-bold │ + │ 설명: text-[10px] text-muted-fg │ + │ overflow ellipsis (1줄) │ + │ │ + │ 비활성 배치: 배치명도 muted 색상 │ + └──────────────────────────────────────┘ + +[셀 3] 타입 배지: + ┌──────────────────────────────────────┐ + │ inline-flex, text-[10px] font-bold │ + │ px-2 py-0.5 rounded-[5px] │ + │ │ + │ DB → DB: cyan 배경/텍스트 │ + │ API → DB: violet 배경/텍스트 │ + │ 노드 플로우: indigo 배경/텍스트 (신규) │ + └──────────────────────────────────────┘ + +[셀 4] Cron 스케줄: + ┌──────────────────────────────────────┐ + │ Cron 표현식: font-mono text-[11px] │ + │ font-medium │ + │ 한글 설명: text-[9px] text-muted │ + │ "매 30분", "매일 01:00" │ + │ │ + │ 비활성: muted 색상 │ + └──────────────────────────────────────┘ + + Cron → 한글 변환 예시: + - */30 * * * * → "매 30분" + - 0 */2 * * * → "매 2시간" + - 0 6,18 * * * → "06:00, 18:00" + - 0 1 * * * → "매일 01:00" + - 0 3 * * 0 → "매주 일 03:00" + - 0 0 1 * * → "매월 1일 00:00" + +[셀 5] 스파크라인 (최근 24h): + ┌──────────────────────────────────────┐ + │ flex, items-end, gap-[1px], h-6 │ + │ │ + │ 24개 바 (시간당 1개): │ + │ - 성공(ok): green, opacity 60% │ + │ - 실패(fail): red, opacity 80% │ + │ - 미실행(none): muted, opacity 15% │ + │ │ + │ 각 바: flex-1, min-w-[3px] │ + │ rounded-t-[1px] │ + │ 높이: 실행시간 비례 또는 고정 │ + │ hover: opacity 100% │ + └──────────────────────────────────────┘ + + 데이터: 최근 24시간을 1시간 단위로 슬라이싱 + 각 슬롯별 가장 최근 실행의 status 사용 + 높이: 성공=80~95%, 실패=20~40%, 미실행=5% + +[셀 6] 마지막 실행: + ┌──────────────────────────────────────┐ + │ 시간: font-mono text-[10px] │ + │ "14:30:00" │ + │ 경과: text-[9px] muted │ + │ "12분 전" │ + │ │ + │ 실행 중: amber 색상 "실행 중..." │ + │ 비활성: muted "-" + "비활성" │ + └──────────────────────────────────────┘ + +[셀 7] 액션 버튼: + ┌──────────────────────────────────────┐ + │ flex gap-1, justify-end │ + │ │ + │ 3개 아이콘 버튼 (28x28 rounded-md): │ + │ │ + │ [▶] 수동 실행 │ + │ hover: green 테두리+배경+아이콘 │ + │ 아이콘: Play (lucide) │ + │ │ + │ [✎] 편집 │ + │ hover: 기본 밝아짐 │ + │ 아이콘: Pencil (lucide) │ + │ │ + │ [🗑] 삭제 │ + │ hover: red 테두리+배경+아이콘 │ + │ 아이콘: Trash2 (lucide) │ + └──────────────────────────────────────┘ +``` + +--- + +##### 6. 행 확장 상세 패널 (클릭 시 토글) + +행을 클릭하면 아래로 펼쳐지는 상세 패널. 매핑 타입과 노드 플로우 타입에 따라 내용이 달라진다. + +``` +컨테이너: + - border (상단 border 없음, 행과 이어짐) + - rounded-b-xl + - bg-muted/30 (행보다 약간 어두운 배경) + - padding: 20px 24px + +내부 구조: + ┌────────────────────────────────────────────────────────────┐ + │ [내러티브 박스] │ + │ "ERP_SOURCE DB의 item_master 테이블에서 현재 DB의 │ + │ item_info 테이블로 12개 컬럼을 매 30분마다 동기화하고 │ + │ 있어요. 오늘 48회 실행, 마지막 실행은 12분 전이에요." │ + ├────────────────────────────────────────────────────────────┤ + │ [파이프라인 플로우 다이어그램] │ + │ │ + │ ┌─────────────┐ 12 컬럼 UPSERT ┌─────────────┐ │ + │ │ 🗄 DB아이콘 │ ─────────────────→ │ 🗄 DB아이콘 │ │ + │ │ ERP_SOURCE │ WHERE USE_YN='Y' │ 현재 DB │ │ + │ │ item_master │ │ item_info │ │ + │ └─────────────┘ └─────────────┘ │ + ├──────────────────────┬─────────────────────────────────────┤ + │ [좌측: 매핑 + 설정] │ [우측: 실행 이력 타임라인] │ + │ │ │ + │ --- 컬럼 매핑 (12) --- │ --- 실행 이력 (최근 5건) --- │ + │ ITEM_CD → item_code PK│ ● 14:30:00 [성공] 1,842건 3.2s │ + │ ITEM_NM → item_name │ │ │ + │ ITEM_SPEC → spec... │ ● 14:00:00 [성공] 1,840건 3.1s │ + │ UNIT_CD → unit_code │ │ │ + │ STD_PRICE → std_price │ ✕ 13:30:00 [실패] Timeout │ + │ + 7개 더 보기 │ │ │ + │ │ ● 13:00:00 [성공] 1,838건 2.9s │ + │ --- 설정 --- │ │ │ + │ 배치 크기: 500 │ ● 12:30:00 [성공] 1,835건 3.5s │ + │ 타임아웃: 30s │ │ + │ 실패 시: 3회 재시도 │ │ + │ 매칭 키: item_code │ │ + │ 모드: [UPSERT] │ │ + └──────────────────────┴─────────────────────────────────────┘ +``` + +**6-1. 내러티브 박스 (Toss 스타일 자연어 설명)** + +``` +스타일: + - rounded-lg + - 배경: linear-gradient(135deg, primary/6%, info/4%) + - 보더: 1px solid primary/8% + - padding: 12px 14px + - margin-bottom: 16px + +텍스트: text-[11px] text-muted-foreground leading-relaxed +강조 텍스트: + - 굵은 텍스트(b): foreground font-semibold + - 하이라이트(hl): primary font-bold + +매핑 타입 예시: + "ERP_SOURCE 데이터베이스의 item_master 테이블에서 현재 DB의 + item_info 테이블로 12개 컬럼을 매 30분마다 동기화하고 있어요. + 오늘 48회 실행, 마지막 실행은 12분 전이에요." + +노드 플로우 타입 예시: + "자동 퇴사 처리 노드 플로우를 매일 00:00에 실행하고 있어요. + user_info 테이블에서 퇴사일이 지난 사용자를 조회하여 + 상태를 '퇴사'로 변경합니다. 4개 노드로 구성되어 있어요." +``` + +**6-2. 파이프라인 플로우 다이어그램** + +``` +컨테이너: + - flex items-center + - rounded-lg border bg-card p-4 + - margin-bottom: 16px + +구조: [소스 노드] ──[커넥터]──> [타겟 노드] + +소스 노드 (pipe-node src): + - 배경: cyan/6%, 보더: cyan/12% + - 아이콘: 32x32 rounded-lg, cyan/12% 배경 + - DB 타입: Database 아이콘 (lucide) + - API 타입: Cloud 아이콘 (lucide) + violet 색상 + - 이름: text-xs font-bold cyan 색상 + - 부제: font-mono text-[10px] muted (테이블명/URL) + +커넥터 (pipe-connector): + - flex-1, flex-col items-center + - 상단 라벨: text-[9px] font-bold muted ("12 컬럼 UPSERT") + - 라인: width 100%, h-[2px], gradient(cyan → green) + - 라인 끝: 삼각형 화살표 (CSS ::after) + - 하단 라벨: text-[9px] font-bold muted ("WHERE USE_YN='Y'") + +타겟 노드 (pipe-node tgt): + - 배경: green/6%, 보더: green/12% + - 아이콘: green/12% 배경 + - 이름: text-xs font-bold green 색상 + - 부제: 테이블명 + +노드 플로우 타입일 때: + - 소스/타겟 대신 노드 플로우 요약 카드로 대체 + - 아이콘: Workflow 아이콘 (lucide) + indigo 색상 + - 이름: 플로우명 + - 부제: "N개 노드 | 조건 분기 포함" + - 노드 목록: 간략 리스트 (Source → Condition → Update → Email) +``` + +**6-3. 하단 2열 그리드** + +``` +레이아웃: grid grid-cols-2 gap-5 + +[좌측 컬럼] 매핑 + 설정: + + 섹션 1 - 컬럼 매핑: + 헤더: flex items-center gap-1.5 + - Link 아이콘 (lucide, 13px, muted) + - "컬럼 매핑" (text-[11px] font-bold muted) + - 건수 배지 (ml-auto, text-[9px] font-bold, primary/10% bg, primary 색) + + 매핑 행 (map-row): + - flex items-center gap-1.5 + - rounded-md border bg-card px-2.5 py-1.5 + - margin-bottom: 2px + + 구조: [소스 컬럼] → [타겟 컬럼] [태그] + 소스: font-mono font-semibold text-[11px] cyan + 화살표: "→" muted + 타겟: font-mono font-semibold text-[11px] green + 태그: text-[8px] font-bold px-1.5 py-0.5 rounded-sm + PK = green 배경 + dark 텍스트 + + 5개까지 표시 후 "+ N개 더 보기" 접기/펼치기 + + 노드 플로우 타입일 때: + 매핑 대신 "노드 구성" 섹션으로 대체 + 각 행: [노드 아이콘] [노드 타입] [노드 설명] + 예: 🔍 테이블 소스 | user_info 조회 + 🔀 조건 분기 | 퇴사일 <= NOW() + ✏️ UPDATE | status → '퇴사' + 📧 이메일 | 관리자 알림 + + 섹션 2 - 설정 (cprop 리스트): + 헤더: Settings 아이콘 + "설정" + + 각 행 (cprop): + - flex justify-between py-1.5 + - 하단 보더: 1px solid white/3% + - 키: text-[11px] muted + - 값: text-[11px] font-semibold, mono체는 font-mono text-[10px] + - 특수 값: UPSERT 배지 (green/10% bg, green 색, text-[10px] font-bold) + + 매핑 타입 설정: + - 배치 크기: 500 + - 타임아웃: 30s + - 실패 시 재시도: 3회 (green) + - 매칭 키: item_code (mono) + - 모드: [UPSERT] (배지) + + 노드 플로우 타입 설정: + - 플로우 ID: 42 + - 노드 수: 4개 + - 실행 타임아웃: 60s + - 컨텍스트: { ... } (mono, 접기 가능) + + +[우측 컬럼] 실행 이력 타임라인: + + 헤더: Clock 아이콘 + "실행 이력" + "최근 5건" 배지 (green) + + 타임라인 (timeline): + flex-col, gap-0 + + 각 항목 (tl-item): + - flex items-start gap-3 + - padding: 10px 0 + - 하단 보더: 1px solid white/3% + + 좌측 - 점+선 (tl-dot-wrap): + - flex-col items-center, width 16px + - 점 (tl-dot): 8x8 rounded-full + 성공(ok): green + 실패(fail): red + 실행중(run): amber + blink 애니메이션 + - 선 (tl-line): width 1px, bg border, min-h 12px + 마지막 항목에는 선 없음 + + 우측 - 내용 (tl-body): + - 시간: font-mono text-[10px] font-semibold + - 상태 배지: text-[9px] font-bold px-1.5 py-0.5 rounded + 성공: green/10% bg + green 색 + 실패: red/10% bg + red 색 + - 메시지: text-[10px] muted, margin-top 2px + 성공: "1,842건 처리 / 3.2s 소요" + 실패: "Connection timeout: ERP_SOURCE 응답 없음" +``` + +--- + +##### 7. 반응형 대응 + +``` +1024px 이하 (태블릿): + - 통계 카드: grid-cols-2 + - 테이블 그리드: 36px 1fr 80px 110px 120px 80px (액션 숨김) + - 상세 패널 2열 그리드 → 1열 + +640px 이하 (모바일): + - 컨테이너 padding: 16px + - 통계 카드: grid-cols-2 gap-2 + - 테이블 헤더: 숨김 + - 테이블 행: grid-cols-1, 카드형태 (padding 16px, gap 8px) +``` + +--- + +##### 8. 필요한 백엔드 API + +``` +1. GET /api/batch-management/stats + → { + totalBatches: number, + activeBatches: number, + todayExecutions: number, + todayFailures: number, + prevDayExecutions?: number, + prevDayFailures?: number + } + 쿼리: batch_configs COUNT + batch_execution_logs 오늘/어제 집계 + +2. GET /api/batch-management/batch-configs/:id/sparkline + → [{ hour: 0~23, status: 'success'|'failed'|'none', count: number }] + 쿼리: batch_execution_logs WHERE batch_config_id=$1 + AND start_time >= NOW() - INTERVAL '24 hours' + GROUP BY EXTRACT(HOUR FROM start_time) + +3. GET /api/batch-management/batch-configs/:id/recent-logs?limit=5 + → [{ start_time, end_time, execution_status, total_records, + success_records, failed_records, error_message, duration_ms }] + 쿼리: batch_execution_logs WHERE batch_config_id=$1 + ORDER BY start_time DESC LIMIT $2 + +4. GET /api/batch-management/batch-configs (기존 수정) + → 각 배치에 sparkline 요약 + last_execution 포함하여 반환 + 목록 페이지에서 개별 sparkline API를 N번 호출하지 않도록 + 한번에 가져오기 (LEFT JOIN + 서브쿼리) +``` + +--- + +## 4. 변경 파일 목록 + +### DB + +| 파일 | 변경 | 설명 | +|------|------|------| +| `db/migrations/XXXX_batch_node_flow_integration.sql` | 신규 | ALTER TABLE batch_configs | + +### 백엔드 + +| 파일 | 변경 | 설명 | +|------|------|------| +| `backend-node/src/services/batchSchedulerService.ts` | 수정 | executeBatchConfig에 node_flow 분기 | +| `backend-node/src/types/batchTypes.ts` | 수정 | BatchConfig 타입에 새 필드 추가 | +| `backend-node/src/services/batchService.ts` | 수정 | create/update에 새 필드 처리 | +| `backend-node/src/controllers/batchManagementController.ts` | 수정 | 새 필드 API + stats/sparkline/recent-logs API | +| `backend-node/src/routes/batchManagementRoutes.ts` | 수정 | node-flows/stats/sparkline 엔드포인트 추가 | + +### 프론트엔드 + +| 파일 | 변경 | 설명 | +|------|------|------| +| `frontend/app/(main)/admin/automaticMng/batchmngList/page.tsx` | **리디자인** | Ops 대시보드 스타일로 전면 재작성 | +| `frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx` | 수정 | 실행 타입 선택 + 플로우 선택 | +| `frontend/app/(main)/admin/automaticMng/batchmngList/edit/[id]/page.tsx` | 수정 | 실행 타입 선택 + 플로우 선택 | + +--- + +## 5. 핵심 구현 상세 + +### 5.1 BatchSchedulerService 변경 (가장 중요) + +```typescript +// batchSchedulerService.ts - executeBatchConfig 메서드 수정 + +static async executeBatchConfig(config: any) { + const startTime = new Date(); + let executionLog: any = null; + + try { + // ... 실행 로그 생성 (기존 코드 유지) ... + + let result; + + // 실행 타입에 따라 분기 + if (config.execution_type === 'node_flow' && config.node_flow_id) { + // 노드 플로우 실행 + result = await this.executeNodeFlow(config); + } else { + // 기존 매핑 실행 (하위 호환) + result = await this.executeBatchMappings(config); + } + + // ... 실행 로그 업데이트 (기존 코드 유지) ... + return result; + } catch (error) { + // ... 에러 처리 (기존 코드 유지) ... + } +} + +/** + * 노드 플로우 실행 (신규) + */ +private static async executeNodeFlow(config: any) { + const { NodeFlowExecutionService } = await import('./nodeFlowExecutionService'); + + const context = { + sourceData: [], + dataSourceType: 'none', + nodeResults: new Map(), + executionOrder: [], + buttonContext: { + buttonId: `batch_${config.id}`, + companyCode: config.company_code, + userId: config.created_by || 'batch_system', + formData: config.node_flow_context || {}, + }, + }; + + const flowResult = await NodeFlowExecutionService.executeFlow( + config.node_flow_id, + context + ); + + // 노드 플로우 결과를 배치 로그 형식으로 변환 + return { + totalRecords: flowResult.totalNodes || 0, + successRecords: flowResult.successNodes || 0, + failedRecords: flowResult.failedNodes || 0, + }; +} +``` + +### 5.2 실행 결과 매핑 + +노드 플로우 결과 → 배치 로그 변환: + +| 노드 플로우 결과 | 배치 로그 필드 | 설명 | +|------------------|---------------|------| +| 전체 노드 수 | total_records | 실행 대상 노드 수 | +| 성공 노드 수 | success_records | 성공적으로 실행된 노드 | +| 실패 노드 수 | failed_records | 실패한 노드 | +| 에러 메시지 | error_message | 첫 번째 실패 노드의 에러 | + +### 5.3 보안 고려사항 + +- 배치에서 실행되는 노드 플로우도 **company_code** 필터링 적용 +- 배치 설정의 company_code와 노드 플로우의 company_code가 일치해야 함 +- 최고 관리자(`*`)는 모든 플로우 실행 가능 +- 실행 로그에 `batch_system`으로 사용자 기록 + +--- + +## 6. 구현 순서 + +### Phase 1: DB + 백엔드 코어 (1일) + +1. 마이그레이션 SQL 작성 및 실행 +2. `batchTypes.ts` 타입 수정 +3. `batchService.ts` create/update 수정 +4. `batchSchedulerService.ts` 핵심 분기 로직 추가 +5. `batchManagementRoutes.ts` 노드 플로우 목록 API 추가 +6. 수동 실행 테스트 (`POST /batch-configs/:id/execute`) + +### Phase 2: 백엔드 대시보드 API (0.5일) + +1. `GET /api/batch-management/stats` - 전체/활성/오늘실행/오늘실패 집계 API +2. `GET /api/batch-management/batch-configs/:id/sparkline` - 최근 24h 실행 결과 (시간대별 성공/실패/미실행) +3. `GET /api/batch-management/batch-configs/:id/recent-logs?limit=5` - 최근 N건 실행 이력 +4. 기존 목록 API에 sparkline 요약 데이터 포함 옵션 추가 + +### Phase 3: 프론트엔드 - 배치 목록 Ops 대시보드 (1.5일) + +상세 UI 명세는 위 "3.3 배치 목록 UI - Ops 대시보드 리디자인" 섹션 참조. + +1. **페이지 헤더**: 제목 + 부제 + 새로고침/새배치 버튼 (명세 항목 1) +2. **통계 카드 영역**: 4개 카드 + stats API 연동 (명세 항목 2) +3. **툴바**: 검색 + 상태/타입 필터 pill-group + 건수 표시 (명세 항목 3) +4. **배치 테이블**: 7열 그리드 헤더 + 행 (명세 항목 4~5) +5. **행 확장 상세 패널**: 내러티브 + 파이프라인 + 매핑/플로우 + 설정 + 타임라인 (명세 항목 6) +6. **반응형**: 1024px/640px 브레이크포인트 (명세 항목 7) +7. 배치 생성/편집 모달에 실행 타입 선택 + 노드 플로우 드롭다운 + +### Phase 4: 테스트 및 검증 (0.5일) + +1. 테스트용 노드 플로우 생성 (간단한 UPDATE) +2. 배치 설정에 연결 +3. 수동 실행 테스트 +4. Cron 스케줄 자동 실행 테스트 +5. 실행 로그 확인 +6. 대시보드 통계/스파크라인 정확성 확인 + +--- + +## 7. 리스크 및 대응 + +### 7.1 노드 플로우 실행 시간 초과 + +- **리스크**: 복잡한 플로우가 오래 걸려서 다음 스케줄과 겹칠 수 있음 +- **대응**: 실행 중인 배치는 중복 실행 방지 (락 메커니즘) - Phase 2 이후 고려 + +### 7.2 노드 플로우 삭제 시 배치 깨짐 + +- **리스크**: 연결된 노드 플로우가 삭제되면 배치 실행 실패 +- **대응**: + - 플로우 존재 여부 체크 후 실행 + - 실패 시 로그에 "플로우를 찾을 수 없습니다" 기록 + - (향후) 플로우 삭제 시 연결된 배치가 있으면 경고 + +### 7.3 멀티 인스턴스 환경 + +- **리스크**: 서버가 여러 대일 때 같은 배치가 중복 실행 +- **대응**: 현재 단일 인스턴스 운영이므로 당장은 문제 없음. 향후 Redis 기반 분산 락 고려 + +--- + +## 8. 기대 효과 + +1. **시간 기반 비즈니스 자동화**: 수동 작업 없이 조건 충족 시 자동 처리 +2. **기존 인프라 재활용**: 검증된 배치 스케줄러(1,200+건 성공) + 강력한 노드 플로우 엔진 +3. **최소 코드 변경**: DB 컬럼 3개 + 백엔드 분기 1개 + 프론트 UI 확장 +4. **운영 가시성 극대화**: Ops 대시보드로 배치 상태/건강도를 한눈에 파악 (스파크라인, LED, 타임라인) +5. **확장성**: 향후 이벤트 트리거(데이터 변경 감지) 등으로 확장 가능 + +--- + +## 9. 설계 의도 - 왜 기존 화면과 다른 레이아웃인가 + +| 비교 항목 | 데이터 타입 관리 (편집기) | 배치 관리 (대시보드) | +|-----------|------------------------|-------------------| +| 역할 | 컬럼 메타데이터 편집 | 운영 상태 모니터링 | +| 레이아웃 | 3패널 (리스트/그리드/설정) | 테이블 + 인라인 모니터링 | +| 주요 행위 | 필드 추가/삭제/수정 | 상태 확인, 수동 실행, 이력 조회 | +| 시각적 요소 | 폼, 드래그앤드롭 | LED, 스파크라인, 타임라인 | +| 참고 UI | IDE, Figma 속성 패널 | Vercel Functions, Railway | + +### 디자인 키포인트 6가지 + +1. **스파크라인 = 건강 상태 한눈에**: Vercel의 Function 목록처럼 각 배치 행에 최근 24h 실행 결과를 미니 바 차트로 표현. 숫자 읽을 필요 없이 패턴으로 건강 상태 파악. + +2. **Expandable Row 패턴**: 3패널 대신 클릭하면 행이 확장되어 상세 정보 표시. 파이프라인 플로우 + 매핑 + 타임라인이 한 번에. Railway/GitHub Actions의 Job 상세 패턴. + +3. **LED 상태 표시**: 카드의 Badge(활성/비활성) 대신 LED 점으로 상태 표현. 초록=활성, 주황깜빡임=실행중, 회색=비활성. 운영실 모니터 느낌. + +4. **파이프라인 플로우 다이어그램**: 소스 → 화살표 → 타겟을 수평 파이프라인으로 시각화. DB-DB는 DB 아이콘 둘, API-DB는 클라우드+DB. 데이터 흐름이 직관적. + +5. **내러티브 박스**: 설정값을 나열하는 대신 자연어로 요약. "A에서 B로 N개 컬럼을 매 30분마다 동기화하고 있어요" 식. Toss 스타일 UX Writing. + +6. **타임라인 실행 이력**: 테이블 로그 대신 세로 타임라인(점+선). 성공/실패가 시각적으로 즉시 구분. 문제 발생 시점 빠르게 특정 가능. + +### 디자인 원본 + +HTML 프리뷰 파일: `_local/batch-management-v3-preview.html` (브라우저에서 열어 시각적 확인 가능) diff --git a/frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx b/frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx index a4e1095c..e8b90461 100644 --- a/frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx +++ b/frontend/app/(main)/admin/automaticMng/batchmngList/create/page.tsx @@ -1,34 +1,101 @@ "use client"; -import React, { useState, useEffect } from "react"; -import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import React, { useState, useEffect, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; -import { ArrowLeft, Save, RefreshCw, ArrowRight, Trash2 } from "lucide-react"; +import { + ArrowLeft, Save, RefreshCw, Trash2, Search, + Database, Workflow, Clock, ChevronRight, +} from "lucide-react"; import { toast } from "sonner"; import { showErrorToast } from "@/lib/utils/toastUtils"; -import { useRouter } from "next/navigation"; +import { useTabStore } from "@/stores/tabStore"; import { BatchAPI, BatchMapping, ConnectionInfo, ColumnInfo, BatchMappingRequest, + type NodeFlowInfo, + type BatchExecutionType, } from "@/lib/api/batch"; +const SCHEDULE_PRESETS = [ + { label: "5분마다", cron: "*/5 * * * *", preview: "5분마다 실행돼요" }, + { label: "30분마다", cron: "*/30 * * * *", preview: "30분마다 실행돼요" }, + { label: "매시간", cron: "0 * * * *", preview: "매시간 정각에 실행돼요" }, + { label: "매일 오전 7시", cron: "0 7 * * *", preview: "매일 오전 7시에 실행돼요" }, + { label: "매일 오전 9시", cron: "0 9 * * *", preview: "매일 오전 9시에 실행돼요" }, + { label: "매일 자정", cron: "0 0 * * *", preview: "매일 밤 12시에 실행돼요" }, + { label: "매주 월요일", cron: "0 9 * * 1", preview: "매주 월요일 오전 9시에 실행돼요" }, + { label: "매월 1일", cron: "0 9 1 * *", preview: "매월 1일 오전 9시에 실행돼요" }, +]; + +function buildCustomCron(repeat: string, dow: string, hour: string, minute: string): string { + const h = hour; + const m = minute; + if (repeat === "daily") return `${m} ${h} * * *`; + if (repeat === "weekly") return `${m} ${h} * * ${dow}`; + if (repeat === "monthly") return `${m} ${h} 1 * *`; + return `${m} ${h} * * *`; +} + +function customCronPreview(repeat: string, dow: string, hour: string, minute: string): string { + const dowNames: Record = { "1": "월요일", "2": "화요일", "3": "수요일", "4": "목요일", "5": "금요일", "6": "토요일", "0": "일요일" }; + const h = Number(hour); + const ampm = h < 12 ? "오전" : "오후"; + const displayH = h === 0 ? 12 : h > 12 ? h - 12 : h; + const time = `${ampm} ${displayH}시${minute !== "0" ? ` ${minute}분` : ""}`; + if (repeat === "daily") return `매일 ${time}에 실행돼요`; + if (repeat === "weekly") return `매주 ${dowNames[dow] || dow} ${time}에 실행돼요`; + if (repeat === "monthly") return `매월 1일 ${time}에 실행돼요`; + return `매일 ${time}에 실행돼요`; +} + export default function BatchCreatePage() { - const router = useRouter(); - - // 기본 정보 + const { openTab } = useTabStore(); + + const [executionType, setExecutionType] = useState(() => { + if (typeof window !== "undefined") { + const stored = sessionStorage.getItem("batch_create_type"); + if (stored === "node_flow") { + sessionStorage.removeItem("batch_create_type"); + return "node_flow"; + } + sessionStorage.removeItem("batch_create_type"); + } + return "mapping"; + }); + const [nodeFlows, setNodeFlows] = useState([]); + const [selectedFlowId, setSelectedFlowId] = useState(null); + const [nodeFlowContext, setNodeFlowContext] = useState(""); + const [flowSearch, setFlowSearch] = useState(""); + const [batchName, setBatchName] = useState(""); - const [cronSchedule, setCronSchedule] = useState("0 12 * * *"); const [description, setDescription] = useState(""); - - // 커넥션 및 데이터 + + // 스케줄 관련 + const [scheduleMode, setScheduleMode] = useState<"preset" | "custom">("preset"); + const [selectedPresetIndex, setSelectedPresetIndex] = useState(3); // 매일 오전 7시 + const [customRepeat, setCustomRepeat] = useState("daily"); + const [customDow, setCustomDow] = useState("1"); + const [customHour, setCustomHour] = useState("9"); + const [customMinute, setCustomMinute] = useState("0"); + + const cronSchedule = useMemo(() => { + if (scheduleMode === "preset") return SCHEDULE_PRESETS[selectedPresetIndex].cron; + return buildCustomCron(customRepeat, customDow, customHour, customMinute); + }, [scheduleMode, selectedPresetIndex, customRepeat, customDow, customHour, customMinute]); + + const schedulePreview = useMemo(() => { + if (scheduleMode === "preset") return SCHEDULE_PRESETS[selectedPresetIndex].preview; + return customCronPreview(customRepeat, customDow, customHour, customMinute); + }, [scheduleMode, selectedPresetIndex, customRepeat, customDow, customHour, customMinute]); + const [connections, setConnections] = useState([]); const [fromConnection, setFromConnection] = useState(null); const [toConnection, setToConnection] = useState(null); @@ -38,19 +105,20 @@ export default function BatchCreatePage() { const [toTable, setToTable] = useState(""); const [fromColumns, setFromColumns] = useState([]); const [toColumns, setToColumns] = useState([]); - - // 매핑 상태 + const [selectedFromColumn, setSelectedFromColumn] = useState(null); const [mappings, setMappings] = useState([]); - - // 로딩 상태 + const [loading, setLoading] = useState(false); const [loadingConnections, setLoadingConnections] = useState(false); - // 커넥션 목록 로드 useEffect(() => { - loadConnections(); - }, []); + if (executionType === "node_flow") { + BatchAPI.getNodeFlows().then(setNodeFlows); + } + }, [executionType]); + + useEffect(() => { loadConnections(); }, []); const loadConnections = async () => { setLoadingConnections(true); @@ -59,487 +127,533 @@ export default function BatchCreatePage() { setConnections(Array.isArray(data) ? data : []); } catch (error) { console.error("커넥션 로드 실패:", error); - toast.error("커넥션 목록을 불러오는데 실패했습니다."); + toast.error("커넥션 목록을 불러올 수 없어요"); setConnections([]); } finally { setLoadingConnections(false); } }; - // FROM 커넥션 변경 const handleFromConnectionChange = async (connectionId: string) => { - if (connectionId === 'unknown') return; - - const connection = connections.find(conn => { - if (conn.type === 'internal') { - return connectionId === 'internal'; - } - return conn.id ? conn.id.toString() === connectionId : false; - }); - + if (connectionId === "unknown") return; + const connection = connections.find(conn => conn.type === "internal" ? connectionId === "internal" : conn.id?.toString() === connectionId); if (!connection) return; - setFromConnection(connection); - setFromTable(""); - setFromTables([]); - setFromColumns([]); - setSelectedFromColumn(null); - + setFromTable(""); setFromTables([]); setFromColumns([]); setSelectedFromColumn(null); try { const tables = await BatchAPI.getTablesFromConnection(connection); setFromTables(Array.isArray(tables) ? tables : []); - } catch (error) { - console.error("FROM 테이블 목록 로드 실패:", error); - toast.error("테이블 목록을 불러오는데 실패했습니다."); - } + } catch { toast.error("테이블 목록을 불러올 수 없어요"); } }; - // TO 커넥션 변경 const handleToConnectionChange = async (connectionId: string) => { - if (connectionId === 'unknown') return; - - const connection = connections.find(conn => { - if (conn.type === 'internal') { - return connectionId === 'internal'; - } - return conn.id ? conn.id.toString() === connectionId : false; - }); - + if (connectionId === "unknown") return; + const connection = connections.find(conn => conn.type === "internal" ? connectionId === "internal" : conn.id?.toString() === connectionId); if (!connection) return; - - setToConnection(connection); - setToTable(""); - setToTables([]); - setToColumns([]); - + setToConnection(connection); setToTable(""); setToTables([]); setToColumns([]); try { const tables = await BatchAPI.getTablesFromConnection(connection); setToTables(Array.isArray(tables) ? tables : []); - } catch (error) { - console.error("TO 테이블 목록 로드 실패:", error); - toast.error("테이블 목록을 불러오는데 실패했습니다."); - } + } catch { toast.error("테이블 목록을 불러올 수 없어요"); } }; - // FROM 테이블 변경 const handleFromTableChange = async (tableName: string) => { - setFromTable(tableName); - setFromColumns([]); - setSelectedFromColumn(null); - + setFromTable(tableName); setFromColumns([]); setSelectedFromColumn(null); if (!fromConnection || !tableName) return; - try { const columns = await BatchAPI.getTableColumns(fromConnection, tableName); setFromColumns(Array.isArray(columns) ? columns : []); } catch (error) { - console.error("FROM 컬럼 목록 로드 실패:", error); - showErrorToast("컬럼 목록을 불러오는 데 실패했습니다", error, { guidance: "테이블 정보를 확인해 주세요." }); + showErrorToast("컬럼 목록을 불러올 수 없어요", error, { guidance: "테이블 정보를 확인해 주세요." }); } }; - // TO 테이블 변경 const handleToTableChange = async (tableName: string) => { - setToTable(tableName); - setToColumns([]); - + setToTable(tableName); setToColumns([]); if (!toConnection || !tableName) return; - try { const columns = await BatchAPI.getTableColumns(toConnection, tableName); setToColumns(Array.isArray(columns) ? columns : []); - } catch (error) { - console.error("TO 컬럼 목록 로드 실패:", error); - toast.error("컬럼 목록을 불러오는데 실패했습니다."); - } + } catch { toast.error("컬럼 목록을 불러올 수 없어요"); } }; - // FROM 컬럼 선택 const handleFromColumnClick = (column: ColumnInfo) => { setSelectedFromColumn(column); - toast.info(`FROM 컬럼 선택됨: ${column.column_name}`); + toast.info(`FROM 컬럼 선택: ${column.column_name}`); }; - // TO 컬럼 선택 (매핑 생성) const handleToColumnClick = (toColumn: ColumnInfo) => { if (!selectedFromColumn || !fromConnection || !toConnection) { - toast.error("먼저 FROM 컬럼을 선택해주세요."); + toast.error("먼저 왼쪽(FROM)에서 컬럼을 선택해주세요"); return; } + const toKey = `${toConnection.type}:${toConnection.id || "internal"}:${toTable}:${toColumn.column_name}`; + const existingMapping = mappings.find(m => `${m.to_connection_type}:${m.to_connection_id || "internal"}:${m.to_table_name}:${m.to_column_name}` === toKey); + if (existingMapping) { toast.error("같은 대상 컬럼에 중복 매핑할 수 없어요"); return; } - // n:1 매핑 검사 - const toKey = `${toConnection.type}:${toConnection.id || 'internal'}:${toTable}:${toColumn.column_name}`; - const existingMapping = mappings.find(mapping => { - const existingToKey = `${mapping.to_connection_type}:${mapping.to_connection_id || 'internal'}:${mapping.to_table_name}:${mapping.to_column_name}`; - return existingToKey === toKey; - }); - - if (existingMapping) { - toast.error("동일한 TO 컬럼에 중복 매핑할 수 없습니다. (n:1 매핑 방지)"); - return; - } - - const newMapping: BatchMapping = { + setMappings([...mappings, { from_connection_type: fromConnection.type, - from_connection_id: fromConnection.id || null, + from_connection_id: fromConnection.id ?? undefined, from_table_name: fromTable, from_column_name: selectedFromColumn.column_name, - from_column_type: selectedFromColumn.data_type || '', + from_column_type: selectedFromColumn.data_type || "", to_connection_type: toConnection.type, - to_connection_id: toConnection.id || null, + to_connection_id: toConnection.id ?? undefined, to_table_name: toTable, to_column_name: toColumn.column_name, - to_column_type: toColumn.data_type || '', + to_column_type: toColumn.data_type || "", mapping_order: mappings.length + 1, - }; - - setMappings([...mappings, newMapping]); + }]); setSelectedFromColumn(null); - toast.success(`매핑 생성: ${selectedFromColumn.column_name} → ${toColumn.column_name}`); + toast.success(`매핑 완료: ${selectedFromColumn.column_name} → ${toColumn.column_name}`); }; - // 매핑 삭제 const removeMapping = (index: number) => { - const newMappings = mappings.filter((_, i) => i !== index); - const reorderedMappings = newMappings.map((mapping, i) => ({ - ...mapping, - mapping_order: i + 1 - })); - setMappings(reorderedMappings); - toast.success("매핑이 삭제되었습니다."); + setMappings(mappings.filter((_, i) => i !== index).map((m, i) => ({ ...m, mapping_order: i + 1 }))); + toast.success("매핑을 삭제했어요"); }; - // 배치 설정 저장 + const goBack = () => openTab({ type: "admin", title: "배치 관리", adminUrl: "/admin/automaticMng/batchmngList" }); + const saveBatchConfig = async () => { - if (!batchName.trim()) { - toast.error("배치명을 입력해주세요."); - return; - } - - if (!cronSchedule.trim()) { - toast.error("실행 스케줄을 입력해주세요."); - return; - } - - if (mappings.length === 0) { - toast.error("최소 하나 이상의 매핑을 추가해주세요."); + if (!batchName.trim()) { toast.error("배치 이름을 입력해주세요"); return; } + + if (executionType === "node_flow") { + if (!selectedFlowId) { toast.error("실행할 플로우를 선택해주세요"); return; } + let parsedContext: Record | undefined; + if (nodeFlowContext.trim()) { + try { parsedContext = JSON.parse(nodeFlowContext); } catch { toast.error("추가 데이터의 JSON 형식이 올바르지 않아요"); return; } + } + setLoading(true); + try { + await BatchAPI.createBatchConfig({ batchName, description: description || undefined, cronSchedule, mappings: [], isActive: true, executionType: "node_flow", nodeFlowId: selectedFlowId, nodeFlowContext: parsedContext }); + toast.success("배치를 저장했어요!"); + goBack(); + } catch (error) { showErrorToast("저장에 실패했어요", error, { guidance: "입력 내용을 확인하고 다시 시도해 주세요." }); } finally { setLoading(false); } return; } + if (mappings.length === 0) { toast.error("컬럼 매핑을 하나 이상 추가해주세요"); return; } setLoading(true); try { - const request = { - batchName: batchName, - description: description || undefined, - cronSchedule: cronSchedule, - mappings: mappings, - isActive: true - }; - - await BatchAPI.createBatchConfig(request); - toast.success("배치 설정이 성공적으로 저장되었습니다!"); - - // 목록 페이지로 이동 - router.push("/admin/batchmng"); - } catch (error) { - console.error("배치 설정 저장 실패:", error); - showErrorToast("배치 설정 저장에 실패했습니다", error, { guidance: "입력 데이터를 확인하고 다시 시도해 주세요." }); - } finally { - setLoading(false); - } + await BatchAPI.createBatchConfig({ batchName, description: description || undefined, cronSchedule, mappings, isActive: true }); + toast.success("배치를 저장했어요!"); + goBack(); + } catch (error) { showErrorToast("저장에 실패했어요", error, { guidance: "입력 내용을 확인하고 다시 시도해 주세요." }); } finally { setLoading(false); } }; + const selectedFlow = nodeFlows.find(f => f.flow_id === selectedFlowId); + return ( -
+
{/* 헤더 */} -
-
- +
+ +
-

배치관리 매핑 시스템

-

새로운 배치 매핑을 생성합니다.

+

새 배치 등록

+

데이터를 자동으로 처리하는 배치를 만들어 보세요

+ +
+
+ + {/* 실행 방식 선택 */} +
+

어떤 방식으로 실행할까요?

+
+ +
{/* 기본 정보 */} - - - 기본 정보 - - -
-
- - setBatchName(e.target.value)} - placeholder="배치명을 입력하세요" - /> -
-
- - setCronSchedule(e.target.value)} - placeholder="0 12 * * * (매일 12시)" - /> -
+
+

기본 정보

+
+
+ + setBatchName(e.target.value)} placeholder="예: 매출 데이터 동기화" className="h-10 text-sm" /> +

어떤 작업인지 한눈에 알 수 있게 적어주세요

-
- -