File size: 5,024 Bytes
1220b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

seed.py โ€” Seed MongoDB from validated pharmacist Excel



Reads validated/tcm_validated.xlsx and inserts records into MongoDB.

All records are validated via Pydantic schemas before insertion.

Records failing validation are logged to output/seed_errors.json โ€” NOT inserted.



Usage: python -m backend.database.seed

Requires: MONGODB_URL env var or default localhost connection

"""

import pandas as pd
import json
import os
from pathlib import Path
from datetime import datetime
from pydantic import ValidationError
from backend.database.mongodb import get_db, create_indexes
from backend.database.schemas import TCMIngredient

VALIDATED_EXCEL = Path(__file__).parent.parent.parent / "data" / "scraper" / "validated" / "tcm_validated.xlsx"
ERRORS_LOG = Path(__file__).parent.parent.parent / "data" / "scraper" / "output" / "seed_errors.json"

COLUMN_MAP = {
    "Mandarin Name (้”ๅฎš)": "mandarin_name",
    "Pinyin Name": "pinyin_name",
    "Latin/Scientific Name": "latin_name",
    "Indonesian Name โ† VERIFY": "indonesian_name",
    "Is Toxic? (TRUE/FALSE) โ† DECIDE": "is_toxic",
    "Target Organ โ† SELECT": "target_organ",
    "Toxicity Level โ† SELECT": "toxicity_level",
    "Warning Message (Indonesian) โ† WRITE": "medical_advice_preview",
    "Source (SymMap ID / BPOM ref)": "source_reference",
    "Validated? (TRUE when done) โ† CHECK": "validated",
}

def load_validated_excel() -> pd.DataFrame:
    if not VALIDATED_EXCEL.exists():
        raise FileNotFoundError(
            f"Validated Excel not found at {VALIDATED_EXCEL}\n"
            "Please run data/scraper/export_excel.py and have pharmacy team complete validation."
        )
    df = pd.read_excel(VALIDATED_EXCEL, engine="openpyxl")
    print(f"[load] Read {len(df)} rows from {VALIDATED_EXCEL.name}")
    return df

def seed_ingredients(df: pd.DataFrame) -> dict:
    """Seed tcm_ingredients collection from validated DataFrame."""
    db = get_db()
    collection = db["tcm_ingredients"]

    inserted = 0
    skipped_validation = 0
    skipped_unvalidated = 0
    errors = []

    for idx, row in df.iterrows():

        validated_flag = row.get("Validated? (TRUE when done) โ† CHECK", False)
        if str(validated_flag).upper() not in ("TRUE", "1", "YES"):
            skipped_unvalidated += 1
            continue

        record = {
            "mandarin_name": str(row.get("Mandarin Name (้”ๅฎš)", "")).strip(),
            "pinyin_name": str(row.get("Pinyin Name", "")).strip() or None,
            "latin_name": str(row.get("Latin/Scientific Name", "")).strip() or None,
            "indonesian_name": str(row.get("Indonesian Name โ† VERIFY", "")).strip(),
            "is_toxic": str(row.get("Is Toxic? (TRUE/FALSE) โ† DECIDE", "FALSE")).upper() == "TRUE",
            "target_organ": str(row.get("Target Organ โ† SELECT", "")).strip() or None,
            "toxicity_level": str(row.get("Toxicity Level โ† SELECT", "unknown")).strip().lower(),
            "source_reference": str(row.get("Source (SymMap ID / BPOM ref)", "")).strip(),
        }

        try:
            ingredient = TCMIngredient(**record)

            collection.update_one(
                {"mandarin_name": ingredient.mandarin_name},
                {"$set": ingredient.model_dump()},
                upsert=True,
            )
            inserted += 1
        except ValidationError as e:
            skipped_validation += 1
            errors.append({"row": idx + 2, "data": record, "error": str(e)})

    if errors:
        with open(ERRORS_LOG, "w", encoding="utf-8") as f:
            json.dump(errors, f, ensure_ascii=False, indent=2)
        print(f"[warn] {skipped_validation} records failed validation โ†’ {ERRORS_LOG.name}")

    return {
        "inserted": inserted,
        "skipped_unvalidated": skipped_unvalidated,
        "skipped_validation_errors": skipped_validation,
        "total": len(df),
    }

def main():
    print("=== FitMate MongoDB Seeder ===\n")
    print("[init] Creating indexes...")
    create_indexes()

    print("[load] Loading validated Excel...")
    df = load_validated_excel()

    print("[seed] Inserting ingredients...")
    result = seed_ingredients(df)

    print(f"\n=== Seed Complete ===")
    print(f"  Inserted/updated: {result['inserted']}")
    print(f"  Skipped (not validated): {result['skipped_unvalidated']}")
    print(f"  Skipped (validation errors): {result['skipped_validation_errors']}")
    print(f"  Total rows in Excel: {result['total']}")

    if result["inserted"] < 50:
        print(f"\n[warn] Only {result['inserted']} records seeded. Target is 50+.")
        print("  โ†’ Check that pharmacy team marked 'Validated? = TRUE' for reviewed rows")
        print("  โ†’ Check seed_errors.json for validation failures")
    else:
        print(f"\n[ok] Database ready with {result['inserted']} TCM ingredients")

if __name__ == "__main__":
    main()