File size: 13,937 Bytes
cb442f8
 
 
cb7c9b7
1c094f3
cb442f8
1c094f3
cb7c9b7
 
 
 
 
1c094f3
 
 
37d20cf
cb442f8
 
 
 
 
37d20cf
cb442f8
 
 
cb7c9b7
 
 
 
cb442f8
 
 
 
 
 
 
 
 
 
 
 
 
 
37d20cf
cb442f8
37d20cf
cb442f8
 
1c094f3
 
cb442f8
 
 
 
 
37d20cf
57443be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d20cf
cb442f8
 
 
37d20cf
cb442f8
abbfca0
 
 
 
 
 
 
 
cb7c9b7
 
abbfca0
 
57443be
cb7c9b7
abbfca0
 
 
cb442f8
 
cb7c9b7
 
 
 
 
 
 
 
 
 
 
 
 
 
cb442f8
 
 
 
 
cb7c9b7
cb442f8
cb7c9b7
37d20cf
cb442f8
 
 
 
 
 
 
 
 
37d20cf
cb442f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d20cf
cb7c9b7
 
 
57443be
cb7c9b7
57443be
 
 
 
 
 
cb7c9b7
57443be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb7c9b7
 
 
 
57443be
 
cb7c9b7
 
 
57443be
 
 
 
 
 
 
 
 
 
 
 
 
cb7c9b7
57443be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb7c9b7
57443be
 
 
 
 
ead1bf0
 
 
 
 
 
 
 
 
 
 
 
 
 
57443be
 
 
 
8d5bdc7
 
 
 
 
 
 
 
 
 
 
 
 
 
57443be
 
 
8d5bdc7
 
 
5726b96
57443be
 
8d5bdc7
57443be
ead1bf0
8d5bdc7
ead1bf0
8d5bdc7
 
ead1bf0
8d5bdc7
57443be
 
8d5bdc7
57443be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb7c9b7
57443be
 
 
 
 
 
cb7c9b7
57443be
 
 
cb7c9b7
57443be
 
cb7c9b7
5726b96
ead1bf0
5726b96
 
 
 
 
ead1bf0
 
 
 
 
5726b96
 
ead1bf0
 
 
 
 
 
 
 
 
 
5726b96
 
 
ead1bf0
 
 
 
 
 
 
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
import os
import json
from typing import List, Optional
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, Field, ConfigDict
import oracledb
from dotenv import load_dotenv
import json
import requests
import pandas as pd
from datetime import datetime
from datasets import Dataset, DatasetDict, load_dataset

# ๋กœ์ปฌ ๊ฐœ๋ฐœ: .env ํŒŒ์ผ ๋กœ๋“œ (์žˆ์œผ๋ฉด)
load_dotenv()

# ----- ํ™˜๊ฒฝ ๋ณ€์ˆ˜ -----
DB_USER = os.environ["DB_USER"]
DB_PASSWORD = os.environ["DB_PASSWORD"]
WALLET_DIR = os.environ["WALLET_DIR"]
WALLET_PASSWORD = os.environ["WALLET_PASSWORD"]

# tnsnames.ora ์•ˆ์˜ alias ํ™•์ธ (๋ณดํ†ต *_high)
TNS_ALIAS = os.environ.get("DB_TNS_ALIAS", "musclecare_high")

# Hugging Face ์„ค์ •
HF_DATA_REPO_ID = os.getenv("HF_DATA_REPO_ID")
HF_DATA_TOKEN = os.getenv("HF_DATA_TOKEN")

# ----- ์—ฐ๊ฒฐ ํ’€: Thin + mTLS (์ง€๊ฐ‘) -----
# ์ ˆ๋Œ€ ํ˜ธ์ถœํ•˜์ง€ ๋งˆ์„ธ์š”: oracledb.init_oracle_client()  # (Thick๋กœ ๋น ์ ธ์„œ ์‹คํŒจ ๊ฐ€๋Šฅ)
pool: oracledb.ConnectionPool = oracledb.create_pool(
    user=DB_USER,
    password=DB_PASSWORD,
    dsn=TNS_ALIAS,                  # Wallet tnsnames.ora์˜ alias
    config_dir=WALLET_DIR,
    wallet_location=WALLET_DIR,
    wallet_password=WALLET_PASSWORD,
    min=1, max=4, increment=1,
    homogeneous=True,
    timeout=60,
    retry_count=6, retry_delay=2
)

app = FastAPI(title="MuscleCare Hybrid Server (mTLS)")

# ----- ๋ชจ๋ธ -----
class StatePayload(BaseModel):
    model_config = ConfigDict(protected_namespaces=())
    
    user_id: str
    rms_base: Optional[float] = None
    freq_base: Optional[float] = None
    user_emb: Optional[List[float]] = Field(default=None, description="length=12")
    model_version: Optional[str] = None

# ๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ์šฉ ์Šคํ‚ค๋งˆ
class BatchDataItem(BaseModel):
    user_id: str
    session_id: str
    measure_date: str
    rms: float
    freq: float
    fatigue: float
    mode: str
    window_count: int
    windows: List[dict] = Field(default_factory=list)
    measurement_count: int

class BatchUploadPayload(BaseModel):
    batch_data: List[BatchDataItem]
    batch_size: int
    batch_date: str


# ----- ์œ ํ‹ธ -----
def clob_json(obj) -> str:
    return json.dumps(obj, separators=(",", ":"), ensure_ascii=False)

# ----- ์—”๋“œํฌ์ธํŠธ -----
@app.get("/")
def root():
    """๋ฃจํŠธ ์—”๋“œํฌ์ธํŠธ - ์„œ๋ฒ„ ์ƒํƒœ ํ™•์ธ"""
    return {
        "status": "running",
        "message": "MuscleCare API Server",
        "version": "1.0.0",
        "endpoints": {
            "health": "/health (๋น ๋ฅธ ์ฒดํฌ)",
            "health_db": "/health/db (DB ์—ฐ๊ฒฐ ์ฒดํฌ)",
            "docs": "/docs",
            "upload_state": "/upload_state",
            "upload_batch_dataset": "/upload_batch_dataset (๋ฐฐ์น˜ ๋ฐ์ดํ„ฐ)",
            "user_dataset": "/user_dataset/{user_id}"
        }
    }

@app.get("/health")
def health():
    try:
        # ๊ฐ„๋‹จํ•œ health ์ฒดํฌ - DB ์—ฐ๊ฒฐ ์—†์ด ์„œ๋ฒ„ ์ƒํƒœ๋งŒ ํ™•์ธ
        return {
            "ok": True, 
            "server": "running",
            "timestamp": datetime.now().isoformat(),
            "status": "healthy"
        }
    except Exception as e:
        return {"ok": False, "error": str(e)}

@app.get("/health/db")
def health_db():
    """DB ์—ฐ๊ฒฐ์„ ํฌํ•จํ•œ ์ƒ์„ธ health ์ฒดํฌ"""
    try:
        with pool.acquire() as conn:
            with conn.cursor() as cur:
                cur.execute("SELECT 1 FROM DUAL")
                v = cur.fetchone()[0]
        return {"ok": True, "db": v, "server": "running"}
    except Exception as e:
        return {"ok": False, "db": "error", "error": str(e)}

@app.post("/upload_state")
def upload_state(p: StatePayload):
    # MERGE INTO MuscleCare.user_state
    try:
        emb_json = None
        if p.user_emb is not None:
            if len(p.user_emb) != 12:
                raise HTTPException(400, "user_emb must have length=12")
            emb_json = clob_json(p.user_emb)

        with pool.acquire() as conn:
            with conn.cursor() as cur:
                cur.execute("""
                    MERGE INTO MuscleCare.user_state t
                    USING (
                        SELECT :user_id AS user_id FROM dual
                    ) s
                    ON (t.user_id = s.user_id)
                    WHEN MATCHED THEN UPDATE SET
                        rms_base      = :rms_base,
                        freq_base     = :freq_base,
                        user_emb      = :user_emb,
                        model_version = :model_version,
                        last_sync     = CURRENT_TIMESTAMP
                    WHEN NOT MATCHED THEN INSERT
                        (user_id, rms_base, freq_base, user_emb, model_version, last_sync)
                    VALUES
                        (:user_id, :rms_base, :freq_base, :user_emb, :model_version, CURRENT_TIMESTAMP)
                """, dict(
                    user_id=p.user_id,
                    rms_base=p.rms_base,
                    freq_base=p.freq_base,
                    user_emb=emb_json,
                    model_version=p.model_version
                ))
                conn.commit()
        return {"ok": True}
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(500, f"upload_state failed: {e}")


@app.get("/user_dataset/{user_id}")
async def read_user_dataset(user_id: str):
    """Hugging Face Hub์—์„œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ์กฐํšŒ"""
    try:
        # Hugging Face ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ™•์ธ
        hf_repo_id = os.getenv("HF_DATA_REPO_ID")
        hf_token = os.getenv("HF_DATA_TOKEN")
        
        if not hf_repo_id or not hf_token:
            raise HTTPException(status_code=500, detail="Hugging Face ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค (HF_DATA_REPO_ID, HF_DATA_TOKEN)")
        
        # Hugging Face Hub์—์„œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ๋กœ๋“œ
        try:
            dataset = load_dataset(hf_repo_id, split=user_id, token=hf_token)
            data = dataset.to_pandas().to_dict(orient="records")
            
            # ์ตœ๊ทผ 5๊ฐœ ๋ ˆ์ฝ”๋“œ ๋ฐ˜ํ™˜
            recent_data = data[-5:] if len(data) > 5 else data
            
            return {
                "user_id": user_id, 
                "count": len(data), 
                "recent_data": recent_data,
                "filename": f"{user_id}.parquet",
                "source": "huggingface_hub",
                "repo_id": hf_repo_id
            }
            
        except Exception as e:
            # ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
            return {
                "user_id": user_id, 
                "count": 0, 
                "recent_data": [],
                "source": "huggingface_hub",
                "repo_id": hf_repo_id,
                "message": "No data found"
            }
        
    except HTTPException:
        raise
    except Exception as e:
        print(f"โŒ Hugging Face Hub ์กฐํšŒ ์‹คํŒจ: {e}")
        raise HTTPException(status_code=500, detail=f"Hugging Face Hub ์กฐํšŒ ์‹คํŒจ: {str(e)}")

@app.post("/upload_batch_dataset")
async def upload_batch_dataset(payload: BatchUploadPayload):
    """๋ฐฐ์น˜ ๋‹จ์œ„๋กœ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๋ฅผ Hugging Face Hub๋กœ ํ‘ธ์‹œ"""
    try:
        # Hugging Face ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ™•์ธ
        hf_repo_id = os.getenv("HF_DATA_REPO_ID")
        hf_token = os.getenv("HF_DATA_TOKEN")
        
        if not hf_repo_id or not hf_token:
            raise HTTPException(status_code=500, detail="Hugging Face ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค (HF_DATA_REPO_ID, HF_DATA_TOKEN)")

        # ์‚ฌ์šฉ์ž๋ณ„๋กœ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ™”
        user_data_groups = {}
        for item in payload.batch_data:
            user_id = item.user_id
            if user_id not in user_data_groups:
                user_data_groups[user_id] = []
            
            # ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜
            record = {
                "session_id": item.session_id,
                "measure_date": item.measure_date,
                "rms": item.rms,
                "freq": item.freq,
                "fatigue": item.fatigue,
                "mode": item.mode,
                "window_count": item.window_count,
                "windows": item.windows,
                "measurement_count": item.measurement_count,
                "batch_date": payload.batch_date,
                "batch_size": payload.batch_size,
                "timestamp": datetime.now().isoformat()
            }
            user_data_groups[user_id].append(record)

        results = {}
        
        # ํ˜„์žฌ repo์— ์žˆ๋Š” ๋ชจ๋“  split ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
        try:
            existing = load_dataset(hf_repo_id, token=hf_token)
            all_splits = list(existing.keys())
            print(f"๐Ÿ“‚ ๊ธฐ์กด splits: {all_splits}")
            
            # ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์™„์ „ํžˆ ์ƒˆ๋กœ ์ƒ์„ฑ (์Šคํ‚ค๋งˆ ํ†ต์ผ)
            new_existing = DatasetDict()
            for user_id in existing.keys():
                df = existing[user_id].to_pandas()
                # windows ํ•„๋“œ๋ฅผ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ•์ œ ๋ณ€ํ™˜
                df["windows"] = df["windows"].apply(
                    lambda w: [str(v) for v in w] if isinstance(w, list) and len(w) > 0 else []
                )
                # ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์Šคํ‚ค๋งˆ ํ†ต์ผ
                new_existing[user_id] = df_to_dataset(df)
                print(f"๐Ÿ”ง {user_id}: ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์žฌ์ƒ์„ฑ ์™„๋ฃŒ")
            existing = new_existing
                
        except Exception:
            existing = DatasetDict()
            print("๐Ÿ“‚ ๊ธฐ์กด repo ์—†์Œ โ†’ ์ƒˆ๋กœ ์ƒ์„ฑ")

        def normalize_windows(record):
            """windows ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋กœ ์ •๊ทœํ™”"""
            w = record.get("windows")
            if isinstance(w, list):
                if len(w) > 0 and isinstance(w[0], dict):
                    record["windows"] = [str(v) for d in w for v in d.values()]
                else:
                    record["windows"] = [str(x) for x in w]
            elif isinstance(w, dict):
                record["windows"] = [str(v) for v in w.values()]
            else:
                record["windows"] = []
            return record

        # ํ˜„์žฌ ์‚ฌ์šฉ์ž๋งŒ ์—…๋ฐ์ดํŠธ
        for user_id, records in user_data_groups.items():
            try:
                # ์ƒˆ ๋ฐ์ดํ„ฐ ์ •๊ทœํ™”
                normalized = [normalize_windows(r) for r in records]
                new_df = pd.DataFrame(normalized)
                new_dataset = df_to_dataset(new_df)

                if user_id in existing:
                    # ๊ธฐ์กด ๋ฐ์ดํ„ฐ ์ •๊ทœํ™” ๋ฐ ๋ณ‘ํ•ฉ
                    old_df = existing[user_id].to_pandas()
                    # ๊ธฐ์กด windows ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋กœ ์ •๊ทœํ™”
                    old_df["windows"] = old_df["windows"].apply(
                        lambda w: [str(v) for v in w] if isinstance(w, list) and len(w) > 0 else []
                    )
                    merged_df = pd.concat([old_df, new_df], ignore_index=True)
                    existing[user_id] = df_to_dataset(merged_df)
                    print(f"๐Ÿ“Š {user_id}: ๊ธฐ์กด ๋ฐ์ดํ„ฐ์™€ ๋ณ‘ํ•ฉ ({len(old_df)} + {len(new_df)} = {len(merged_df)}๊ฐœ ๋ ˆ์ฝ”๋“œ)")
                else:
                    existing[user_id] = new_dataset
                    print(f"๐Ÿ“Š {user_id}: ์‹ ๊ทœ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ({len(new_df)}๊ฐœ ๋ ˆ์ฝ”๋“œ)")

                results[user_id] = {
                    "status": "success",
                    "new_rows": len(records),
                    "filename": f"{user_id}.parquet"
                }
                
            except Exception as e:
                print(f"โŒ {user_id} ์ฒ˜๋ฆฌ ์‹คํŒจ: {e}")
                results[user_id] = {
                    "status": "failed",
                    "error": str(e)
                }

        # ๋ชจ๋“  split ํ†ต์งธ๋กœ ๋‹ค์‹œ push
        try:
            existing.push_to_hub(hf_repo_id, token=hf_token, private=True)
            print(f"โœ… ์ „์ฒด DatasetDict ํ‘ธ์‹œ ์™„๋ฃŒ: {len(existing)}๊ฐœ ์‚ฌ์šฉ์ž")
        except Exception as e:
            print(f"โŒ ์ „์ฒด ํ‘ธ์‹œ ์‹คํŒจ: {e}")
            raise HTTPException(status_code=500, detail=f"์ „์ฒด ํ‘ธ์‹œ ์‹คํŒจ: {str(e)}")

        return {
            "batch_date": payload.batch_date,
            "batch_size": payload.batch_size,
            "processed_users": len(user_data_groups),
            "results": results,
            "repo_id": hf_repo_id,
            "message": f"Batch upload completed for {len(user_data_groups)} users"
        }

    except HTTPException:
        raise
    except Exception as e:
        print(f"โŒ ๋ฐฐ์น˜ ํ‘ธ์‹œ ์‹คํŒจ: {e}")
        raise HTTPException(status_code=500, detail=f"๋ฐฐ์น˜ ํ‘ธ์‹œ ์‹คํŒจ: {str(e)}")

def normalize_windows(record):
    w = record.get("windows", [])
    result = []

    if isinstance(w, list):
        for item in w:
            if isinstance(item, dict):
                # ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
                for v in item.values():
                    if v is not None and str(v).strip():
                        result.append(str(v))
            elif item is not None and str(item).strip():
                result.append(str(item))
    elif isinstance(w, dict):
        # ๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
        for v in w.values():
            if v is not None and str(v).strip():
                result.append(str(v))
    else:
        # windows๊ฐ€ ์—†๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํƒ€์ž…์ธ ๊ฒฝ์šฐ ๋นˆ ๋ฆฌ์ŠคํŠธ
        result = []

    record["windows"] = result
    print(f"๐Ÿ” Windows ์ •๊ทœํ™”: {w} โ†’ {result}")
    return record

def df_to_dataset(df):
    """DataFrame์„ Dataset์œผ๋กœ ๋ณ€ํ™˜ (windows ํ•„๋“œ ์ •๊ทœํ™”)"""
    # windows ํ•„๋“œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์ •๊ทœํ™”
    if 'windows' in df.columns:
        df['windows'] = df['windows'].apply(
            lambda x: x if isinstance(x, list) else []
        )
    return Dataset.from_pandas(df)