-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
53 lines (42 loc) · 1.44 KB
/
db.py
File metadata and controls
53 lines (42 loc) · 1.44 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
"""
SENTINEL — Database utilities
Contract ingestion, validation, and query helpers
"""
from pymongo import MongoClient
from typing import Optional
import os
def get_db():
"""Get MongoDB connection."""
client = MongoClient(os.environ["MONGODB_URI"])
return client["sentinel"]["contracts"]
def search_contracts(query: str, limit: int = 10) -> list[dict]:
"""Full-text search across contract records."""
db = get_db()
results = db.find(
{"$text": {"$search": query}},
{"score": {"$meta": "textScore"}}
).sort([("score", {"$meta": "textScore"})]).limit(limit)
return list(results)
def get_by_agency(agency: str) -> list[dict]:
"""Get all contracts for a specific agency."""
db = get_db()
return list(db.find({"agency": {"$regex": agency, "$options": "i"}}))
def get_by_vendor(vendor: str) -> list[dict]:
"""Get all contracts awarded to a specific vendor."""
db = get_db()
return list(db.find({"vendor": {"$regex": vendor, "$options": "i"}}))
def get_stats() -> dict:
"""Get summary statistics."""
db = get_db()
pipeline = [
{"$group": {
"_id": None,
"total_contracts": {"$sum": 1},
"total_value": {"$sum": "$value_usd"},
"agencies": {"$addToSet": "$agency"},
"vendors": {"$addToSet": "$vendor"},
}}
]
result = list(db.aggregate(pipeline))
return result[0] if result else {}