File size: 10,036 Bytes
1220b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb6f17c
 
 
 
 
 
 
 
 
 
 
 
 
1220b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb6f17c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220b7a
 
 
 
 
 
a160fef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1220b7a
a160fef
1220b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
955006e
 
 
 
 
 
 
 
 
 
 
 
1220b7a
955006e
 
 
 
 
 
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
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
import logging
from motor.motor_asyncio import AsyncIOMotorClient
from core.config import settings

logger = logging.getLogger("fitmate.db")

class FallbackCursor:
    """Async cursor simulating Motor / MongoDB cursor for in-memory datasets."""
    def __init__(self, data):
        self._data = list(data)

    def skip(self, n: int):
        self._data = self._data[n:]
        return self

    def limit(self, n: int):
        self._data = self._data[:n]
        return self

    def sort(self, key: str, direction: int = 1):
        self._data = sorted(
            self._data,
            key=lambda x: str(x.get(key, "")),
            reverse=(direction < 0)
        )
        return self

    async def to_list(self, length: int = None):
        if length is not None:
            return self._data[:length]
        return self._data

    def __aiter__(self):
        self._iter = iter(self._data)
        return self

    async def __anext__(self):
        try:
            return next(self._iter)
        except StopIteration:
            raise StopAsyncIteration


class FallbackCollection:
    """Async collection simulating Motor collection methods."""
    def __init__(self, name: str, data: list):
        self.name = name
        self.data = list(data)

    async def count_documents(self, filter_query: dict = None) -> int:
        if not filter_query:
            return len(self.data)
        count = 0
        for doc in self.data:
            match = True
            for k, v in filter_query.items():
                if doc.get(k) != v:
                    match = False
                    break
            if match:
                count += 1
        return count

    def find(self, filter_query: dict = None, projection: dict = None):
        filtered = []
        for doc in self.data:
            match = True
            if filter_query:
                for k, v in filter_query.items():
                    if doc.get(k) != v:
                        match = False
                        break
            if match:
                if projection:
                    has_inclusion = any(v == 1 or v is True for k, v in projection.items() if k != "_id")
                    if not has_inclusion:
                        proj_doc = dict(doc)
                        for pk, pv in projection.items():
                            if not pv and pk in proj_doc:
                                del proj_doc[pk]
                        filtered.append(proj_doc)
                    else:
                        proj_doc = {}
                        for pk, pv in projection.items():
                            if (pv == 1 or pv is True) and pk in doc:
                                proj_doc[pk] = doc[pk]
                        filtered.append(proj_doc)
                else:
                    filtered.append(dict(doc))
        return FallbackCursor(filtered)

    async def find_one(self, filter_query: dict = None) -> dict | None:
        for doc in self.data:
            match = True
            if filter_query:
                for k, v in filter_query.items():
                    if doc.get(k) != v:
                        match = False
                        break
            if match:
                return dict(doc)
        return None

    async def insert_one(self, document: dict):
        new_doc = dict(document)
        if "_id" not in new_doc:
            new_doc["_id"] = str(len(self.data) + 1)
        self.data.append(new_doc)
        class InsertResult:
            inserted_id = new_doc["_id"]
        return InsertResult()

    async def update_one(self, filter_query: dict, update: dict, upsert: bool = False):
        target = None
        for doc in self.data:
            match = True
            for k, v in filter_query.items():
                if doc.get(k) != v:
                    match = False
                    break
            if match:
                target = doc
                break

        if target:
            if "$set" in update:
                target.update(update["$set"])
            else:
                target.update(update)
            class UpdateResult:
                matched_count = 1
                modified_count = 1
            return UpdateResult()
        elif upsert:
            new_doc = dict(filter_query)
            if "$set" in update:
                new_doc.update(update["$set"])
            else:
                new_doc.update(update)
            await self.insert_one(new_doc)
            class UpsertResult:
                matched_count = 0
                modified_count = 1
            return UpsertResult()

        class NoOpResult:
            matched_count = 0
            modified_count = 0
        return NoOpResult()

    async def delete_one(self, filter_query: dict):
        for idx, doc in enumerate(self.data):
            match = True
            for k, v in filter_query.items():
                if doc.get(k) != v:
                    match = False
                    break
            if match:
                self.data.pop(idx)
                class DeleteResult:
                    deleted_count = 1
                return DeleteResult()
        class DeleteZero:
            deleted_count = 0
        return DeleteZero()


class FallbackDatabase:
    """In-memory database preloaded with 100 TCM ingredients."""
    def __init__(self):
        tcm_data = []
        try:
            import json
            from pathlib import Path
            json_path = Path(__file__).parent / "data" / "tcm_500_ingredients.json"
            if json_path.exists():
                with open(json_path, "r", encoding="utf-8") as f:
                    raw_items = json.load(f)
                for idx, it in enumerate(raw_items, 1):
                    doc = dict(it)
                    doc["_id"] = str(idx)
                    tcm_data.append(doc)
            else:
                from database.seed_100_tcm import POPULAR_TCM
                for idx, item in enumerate(POPULAR_TCM, 1):
                    mandarin, pinyin, indo, eng, is_toxic, tox_class, organ, desc, source = item
                    tcm_data.append({
                        "_id": str(idx),
                        "mandarin_name": mandarin,
                        "pinyin_name": pinyin,
                        "indonesian_name": indo,
                        "english_name": eng,
                        "latin_name": indo if " " in indo else None,
                        "is_toxic": is_toxic,
                        "toxicity_level": tox_class,
                        "toxicity_class": "toxic" if is_toxic else tox_class,
                        "target_organ": organ,
                        "description": desc,
                        "source_reference": source,
                        "validated_by": "fitmate_verified"
                    })
        except Exception as e:
            logger.warning(f"Could not load TCM ingredients into fallback: {e}")

        self._collections = {
            "tcm_ingredients": FallbackCollection("tcm_ingredients", tcm_data),
            "safety_rules": FallbackCollection("safety_rules", []),
            "scan_logs": FallbackCollection("scan_logs", []),
        }

    def __getitem__(self, name: str) -> FallbackCollection:
        if name not in self._collections:
            self._collections[name] = FallbackCollection(name, [])
        return self._collections[name]

    def __getattr__(self, name: str) -> FallbackCollection:
        return self[name]


class MongoDB:
    client: AsyncIOMotorClient = None
    is_fallback: bool = False
    fallback_db: FallbackDatabase = None

db = MongoDB()
db.fallback_db = FallbackDatabase()

async def connect_to_mongo():
    try:
        # Connect with a strict 3-second server selection timeout
        client = AsyncIOMotorClient(
            settings.MONGODB_URL,
            serverSelectionTimeoutMS=3000,
            connectTimeoutMS=3000
        )
        database = client[settings.MONGODB_DB_NAME]
        count = await database["tcm_ingredients"].count_documents({})
        if count == 0:
            print("[DB] Remote MongoDB collection 'tcm_ingredients' is empty. Seeding from POPULAR_TCM...")
            try:
                fallback_items = [dict(item) for item in db.fallback_db["tcm_ingredients"].data]
                if fallback_items:
                    await database["tcm_ingredients"].insert_many(fallback_items)
                    count = await database["tcm_ingredients"].count_documents({})
                    print(f"[DB] Successfully seeded remote MongoDB — {count} ingredients.")
            except Exception as seed_err:
                print(f"[DB] Seeding remote failed ({seed_err}). Falling back to in-memory store.")
                db.is_fallback = True

        db.client = client
        if count > 0 and not db.is_fallback:
            db.is_fallback = False
            print(f"[DB] Connected to MongoDB — {count} ingredients in tcm_ingredients")
        else:
            db.is_fallback = True
            print("[DB] Using in-memory dataset.")
    except Exception as e:
        db.client = None
        db.is_fallback = True
        total_fallback = await db.fallback_db["tcm_ingredients"].count_documents({})
        print(
            f"[DB] Remote MongoDB unreachable ({e.__class__.__name__}).\n"
            f"   Active: In-Memory Resilient Fallback Store ({total_fallback} validated TCM ingredients loaded)."
        )

async def close_mongo_connection():
    if db.client:
        db.client.close()

def get_db():
    if db.is_fallback or not db.client:
        return db.fallback_db
    try:
        return db.client[settings.MONGODB_DB_NAME]
    except Exception:
        return db.fallback_db