-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_server.py
More file actions
498 lines (413 loc) · 15.9 KB
/
web_server.py
File metadata and controls
498 lines (413 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
"""
웹 서버 기반 데이터 전처리 애플리케이션
- Flask 기반 REST API
- 윈도우 서버에서 포트를 지정해 실행
- 기존 data_preprocessor.py 엔진 그대로 활용
- Excel/CSV 파일 업로드 및 다운로드 지원
"""
import os
import sys
import json
import uuid
import argparse
import tempfile
import traceback
from pathlib import Path
from datetime import datetime
from typing import Dict, Any, Optional
# Flask
from flask import (
Flask,
request,
jsonify,
send_file,
render_template_string,
session,
)
import pandas as pd
# 기존 모듈 임포트
from data_preprocessor import DataPreprocessor
from preset_manager import PresetManager
from version import __version__, APP_NAME, CHANGELOG, FEATURES, get_developer_info
# ──────────────────────────────────────────────
# Flask 앱 설정
# ──────────────────────────────────────────────
app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config["MAX_CONTENT_LENGTH"] = 200 * 1024 * 1024 # 200MB
# 세션별 전처리 인스턴스 관리
sessions: Dict[str, Dict[str, Any]] = {}
# 업로드/다운로드 임시 디렉토리
UPLOAD_DIR = Path(tempfile.gettempdir()) / "dp_web_uploads"
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
DOWNLOAD_DIR = Path(tempfile.gettempdir()) / "dp_web_downloads"
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
# 프리셋 매니저 (서버 공용)
preset_manager = PresetManager()
def get_session_data() -> Dict[str, Any]:
"""현재 세션의 전처리 인스턴스를 반환/생성"""
sid = session.get("sid")
if not sid or sid not in sessions:
sid = str(uuid.uuid4())
session["sid"] = sid
sessions[sid] = {
"preprocessor": DataPreprocessor(),
"current_file": None,
"original_filename": None,
"created": datetime.now().isoformat(),
}
return sessions[sid]
# ──────────────────────────────────────────────
# API 엔드포인트
# ──────────────────────────────────────────────
@app.route("/")
def index():
"""메인 페이지"""
html_path = Path(__file__).parent / "web_app_server.html"
with open(html_path, "r", encoding="utf-8") as f:
return f.read()
@app.route("/api/info")
def api_info():
"""앱 정보"""
dev = get_developer_info()
return jsonify(
{
"name": APP_NAME,
"version": __version__,
"features": FEATURES,
"developer": dev,
"changelog": CHANGELOG,
}
)
@app.route("/api/upload", methods=["POST"])
def api_upload():
"""파일 업로드 및 로드"""
if "file" not in request.files:
return jsonify({"success": False, "message": "파일이 없습니다."}), 400
file = request.files["file"]
if file.filename == "":
return jsonify({"success": False, "message": "파일이 선택되지 않았습니다."}), 400
# 확장자 검증
allowed = {".csv", ".xlsx", ".xls"}
ext = Path(file.filename).suffix.lower()
if ext not in allowed:
return (
jsonify(
{
"success": False,
"message": f"지원하지 않는 형식입니다: {ext} (CSV, XLSX, XLS만 지원)",
}
),
400,
)
# 임시 파일로 저장
save_name = f"{uuid.uuid4()}{ext}"
save_path = UPLOAD_DIR / save_name
file.save(str(save_path))
# 데이터 로드
sd = get_session_data()
pp: DataPreprocessor = sd["preprocessor"]
success, msg = pp.load_data(str(save_path))
if success:
sd["current_file"] = str(save_path)
sd["original_filename"] = file.filename
# 미리보기 데이터 (10행)
preview = pp.get_preview(10)
preview_data = []
for _, row in preview.iterrows():
row_dict = {}
for col in preview.columns:
val = row[col]
if pd.isna(val):
row_dict[col] = ""
elif isinstance(val, (pd.Timestamp, datetime)):
row_dict[col] = str(val)
else:
row_dict[col] = val
preview_data.append(row_dict)
return jsonify(
{
"success": True,
"message": msg,
"data": {
"rows": len(pp.original_df),
"cols": len(pp.columns),
"columns": pp.columns,
"numeric_columns": pp.numeric_columns,
"date_column": pp.date_column,
"preview": preview_data,
},
}
)
else:
return jsonify({"success": False, "message": msg}), 400
@app.route("/api/process", methods=["POST"])
def api_process():
"""전처리 실행"""
sd = get_session_data()
pp: DataPreprocessor = sd["preprocessor"]
if pp.original_df is None:
return jsonify({"success": False, "message": "먼저 파일을 업로드하세요."}), 400
params = request.get_json() or {}
logs = []
try:
pp.reset_processing_state()
logs.append(f"🔄 전처리 시작... (총 {len(pp.original_df):,}행)")
# 1. 필터링
filters = params.get("filters", [])
if filters:
success, msg = pp.apply_filters(filters)
logs.append(f"{'✅' if success else '❌'} {msg}")
if not success:
return jsonify({"success": False, "message": msg, "logs": logs})
else:
pp.processed_df = pp.original_df.copy()
pp.stats["filtered_rows"] = len(pp.original_df)
pp.stats["filter_removed"] = 0
logs.append("ℹ️ 필터 조건 없음 - 전체 데이터 사용")
# 2. 이상값 처리
outlier = params.get("outlier", {})
if outlier.get("apply", True):
method = outlier.get("method", "2.5sigma")
success, msg = pp.remove_outliers(method=method, action="drop")
logs.append(f"{'✅' if success else '❌'} {msg}")
# 3. 시간 정규화
time_opts = params.get("time", {})
interval = int(time_opts.get("interval", 2))
if time_opts.get("normalize", False):
success, msg = pp.normalize_timestamps(interval)
logs.append(f"{'✅' if success else '❌'} {msg}")
# 4. 시간 재정렬
if time_opts.get("realign", False):
start_time = time_opts.get("start_time", "")
if start_time:
success, msg = pp.realign_timestamps(start_time, interval)
logs.append(f"{'✅' if success else '❌'} {msg}")
# 결과 요약
logs.append("")
logs.append(pp.get_summary())
logs.append("✅ 전처리 완료! '결과 저장' 버튼을 눌러 저장하세요.")
# 미리보기
preview = pp.get_preview(10)
preview_data = []
for _, row in preview.iterrows():
row_dict = {}
for col in preview.columns:
val = row[col]
if pd.isna(val):
row_dict[col] = ""
elif isinstance(val, (pd.Timestamp, datetime)):
row_dict[col] = str(val)
else:
row_dict[col] = val
preview_data.append(row_dict)
return jsonify(
{
"success": True,
"message": "전처리 완료",
"logs": logs,
"stats": pp.stats,
"preview": preview_data,
"final_rows": len(pp.processed_df),
}
)
except Exception as e:
logs.append(f"❌ 오류: {str(e)}")
return (
jsonify(
{
"success": False,
"message": str(e),
"logs": logs,
"traceback": traceback.format_exc(),
}
),
500,
)
@app.route("/api/download", methods=["POST"])
def api_download():
"""전처리 결과 다운로드"""
sd = get_session_data()
pp: DataPreprocessor = sd["preprocessor"]
if pp.processed_df is None:
return jsonify({"success": False, "message": "저장할 데이터가 없습니다."}), 400
params = request.get_json() or {}
fmt = params.get("format", "csv") # csv or xlsx
original_name = sd.get("original_filename", "data")
base_name = Path(original_name).stem if original_name else "data"
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
if fmt == "xlsx":
out_name = f"{base_name}_processed_{timestamp}.xlsx"
else:
out_name = f"{base_name}_processed_{timestamp}.csv"
out_path = DOWNLOAD_DIR / out_name
try:
success, result = pp.save_data(str(out_path), sd.get("current_file"))
if success:
return jsonify(
{
"success": True,
"download_url": f"/api/download_file/{out_name}",
"filename": out_name,
}
)
else:
return jsonify({"success": False, "message": result}), 500
except Exception as e:
return jsonify({"success": False, "message": str(e)}), 500
@app.route("/api/download_file/<filename>")
def api_download_file(filename):
"""파일 다운로드 실행"""
file_path = DOWNLOAD_DIR / filename
if not file_path.exists():
return jsonify({"success": False, "message": "파일을 찾을 수 없습니다."}), 404
return send_file(str(file_path), as_attachment=True, download_name=filename)
@app.route("/api/validation", methods=["POST"])
def api_validation():
"""Validation 데이터 생성"""
sd = get_session_data()
pp: DataPreprocessor = sd["preprocessor"]
if pp.processed_df is None:
return (
jsonify({"success": False, "message": "먼저 전처리를 실행하세요."}),
400,
)
params = request.get_json() or {}
target_columns = params.get("target_columns", [])
validation_ratio = params.get("ratio", 0.20)
segment_ratios = params.get("segment_ratios", [25, 25, 25, 25])
sigma_start = params.get("sigma_start", 2.5)
sigma_end = params.get("sigma_end", 4.0)
interval = params.get("interval", None)
original_name = sd.get("original_filename", "data")
base_name = Path(original_name).stem if original_name else "data"
# DOWNLOAD_DIR에 저장
original_path = DOWNLOAD_DIR / f"{base_name}.tmp"
try:
success, msg, paths = pp.generate_validation_outputs(
target_columns=target_columns,
validation_ratio=validation_ratio / 100.0
if validation_ratio > 1
else validation_ratio,
segment_ratios=segment_ratios,
sigma_start=sigma_start,
sigma_end=sigma_end,
interval_minutes=interval,
original_path=str(original_path),
)
if success:
# 파일 경로를 다운로드 URL로 변환
download_files = {}
for key, path in paths.items():
fname = Path(path).name
# 파일을 DOWNLOAD_DIR로 이동
dest = DOWNLOAD_DIR / fname
if Path(path).exists() and str(Path(path).parent) != str(DOWNLOAD_DIR):
import shutil
shutil.move(str(path), str(dest))
download_files[key] = {
"filename": fname,
"url": f"/api/download_file/{fname}",
}
return jsonify(
{
"success": True,
"message": msg,
"files": download_files,
}
)
else:
return jsonify({"success": False, "message": msg}), 400
except Exception as e:
return (
jsonify(
{
"success": False,
"message": str(e),
"traceback": traceback.format_exc(),
}
),
500,
)
# ──────────────────────────────────────────────
# 프리셋 API
# ──────────────────────────────────────────────
@app.route("/api/presets", methods=["GET"])
def api_presets_list():
"""프리셋 목록"""
presets = preset_manager.list_presets()
return jsonify({"presets": presets})
@app.route("/api/presets", methods=["POST"])
def api_presets_save():
"""프리셋 저장"""
data = request.get_json() or {}
name = data.get("name", "").strip()
settings = data.get("settings", {})
description = data.get("description", "")
if not name:
return jsonify({"success": False, "message": "이름을 입력하세요."}), 400
success = preset_manager.save_preset(name, settings, description)
return jsonify({"success": success})
@app.route("/api/presets/<name>", methods=["GET"])
def api_presets_load(name):
"""프리셋 로드"""
preset = preset_manager.load_preset(name)
if preset:
return jsonify({"success": True, "preset": preset})
return jsonify({"success": False, "message": "프리셋을 찾을 수 없습니다."}), 404
@app.route("/api/presets/<name>", methods=["DELETE"])
def api_presets_delete(name):
"""프리셋 삭제"""
success = preset_manager.delete_preset(name)
return jsonify({"success": success})
# ──────────────────────────────────────────────
# 정리 (오래된 세션/파일)
# ──────────────────────────────────────────────
def cleanup_old_files(directory: Path, max_age_hours: int = 24):
"""오래된 임시 파일 정리"""
import time
now = time.time()
for f in directory.iterdir():
if f.is_file():
age_hours = (now - f.stat().st_mtime) / 3600
if age_hours > max_age_hours:
try:
f.unlink()
except OSError:
pass
# ──────────────────────────────────────────────
# 메인 실행
# ──────────────────────────────────────────────
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=f"{APP_NAME} - 웹 서버 (v{__version__})"
)
parser.add_argument(
"--port",
type=int,
default=8511,
help="서버 포트 (기본: 8511)",
)
parser.add_argument(
"--host",
type=str,
default="0.0.0.0",
help="바인드 주소 (기본: 0.0.0.0 = 모든 인터페이스)",
)
parser.add_argument(
"--debug",
action="store_true",
help="디버그 모드",
)
args = parser.parse_args()
# 시작 시 오래된 파일 정리
cleanup_old_files(UPLOAD_DIR)
cleanup_old_files(DOWNLOAD_DIR)
print(f"\n{'='*60}")
print(f" {APP_NAME} v{__version__} - 웹 서버")
print(f"{'='*60}")
print(f" 🌐 http://{args.host}:{args.port}")
print(f" 📁 업로드 디렉토리: {UPLOAD_DIR}")
print(f" 💾 다운로드 디렉토리: {DOWNLOAD_DIR}")
print(f"{'='*60}\n")
app.run(host=args.host, port=args.port, debug=args.debug)