-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
38 lines (32 loc) · 1.16 KB
/
storage.py
File metadata and controls
38 lines (32 loc) · 1.16 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
from pymongo import MongoClient
from datetime import datetime
import os
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = os.getenv("MONGO_DB")
COLLECTION_NAME = os.getenv("MONGO_COLLECTION")
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db[COLLECTION_NAME]
def store_email(email, ai_output, decision):
record = {
"sender": email.get("sender"),
"subject": email.get("subject"),
"body": email.get("body"),
"intent": ai_output.get("intent"),
"urgency": ai_output.get("urgency"),
"confidence": ai_output.get("confidence", None),
"decision": decision,
"timestamp": datetime.now().isoformat()
}
collection.insert_one(record)
print("Email stored in MongoDB:", record)
def get_all_emails(limit=10):
return list(collection.find().sort("timestamp", -1).limit(limit))
def get_by_decision(decision, limit=10):
return list(collection.find({"decision": decision}).sort("timestamp", -1).limit(limit))
def count_by_intent():
pipeline = [
{"$group": {"_id": "$intent", "count": {"$sum": 1}}},
{"$sort": {"count": -1}}
]
return list(collection.aggregate(pipeline))