Spaces:
Sleeping
Sleeping
Create data_fetcher.py
Browse files- data_fetcher.py +47 -0
data_fetcher.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🔍 data_fetcher.py
|
| 2 |
+
from firestore_client import get_firestore_client
|
| 3 |
+
|
| 4 |
+
db = get_firestore_client()
|
| 5 |
+
|
| 6 |
+
# Utility function to convert Gemini-style filters into Firestore queries
|
| 7 |
+
def apply_filters(query_ref, filters):
|
| 8 |
+
for f in filters:
|
| 9 |
+
field = f.get("field")
|
| 10 |
+
op = f.get("op", "==")
|
| 11 |
+
value = f.get("value")
|
| 12 |
+
if field and value is not None:
|
| 13 |
+
query_ref = query_ref.where(field, op, value)
|
| 14 |
+
return query_ref
|
| 15 |
+
|
| 16 |
+
def fetch_data_from_firestore(instruction):
|
| 17 |
+
"""
|
| 18 |
+
instruction: dict like
|
| 19 |
+
{
|
| 20 |
+
"collections": [
|
| 21 |
+
{
|
| 22 |
+
"name": "participants",
|
| 23 |
+
"filters": [
|
| 24 |
+
{"field": "companyCode", "op": "==", "value": "QTX"},
|
| 25 |
+
{"field": "status", "op": "==", "value": "active"}
|
| 26 |
+
],
|
| 27 |
+
"limit": 100
|
| 28 |
+
},
|
| 29 |
+
...
|
| 30 |
+
]
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
results = {}
|
| 34 |
+
for col in instruction.get("collections", []):
|
| 35 |
+
name = col.get("name")
|
| 36 |
+
filters = col.get("filters", [])
|
| 37 |
+
limit = col.get("limit", 50)
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
col_ref = db.collection(name)
|
| 41 |
+
col_ref = apply_filters(col_ref, filters)
|
| 42 |
+
docs = col_ref.limit(limit).stream()
|
| 43 |
+
results[name] = [doc.to_dict() for doc in docs]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
results[name] = {"error": str(e)}
|
| 46 |
+
|
| 47 |
+
return results
|