Repository navigation
Expand file tree
/
Copy pathmongodb.py
More file actions
136 lines (115 loc) · 4.78 KB
/
Copy pathmongodb.py
File metadata and controls
136 lines (115 loc) · 4.78 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
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
"""
MongoDB integration.
Owns the MongoDBService data-access layer (webhooks + nsys profiles) and
the connection lifecycle hooks (on_startup/on_shutdown) that app.py wires
into the Litestar app.
"""
import logging
from datetime import datetime
from typing import Any, Optional
from zoneinfo import ZoneInfo
from bson import ObjectId
from bson.codec_options import CodecOptions
from litestar import Litestar
from pymongo import AsyncMongoClient
from pymongo.asynchronous.database import AsyncDatabase
from config import config
class MongoDBService:
"""MongoDB service for webhook storage"""
def __init__(self, db: AsyncDatabase):
options = CodecOptions(tz_aware=True, tzinfo=ZoneInfo(config.app.tzinfo))
self.db = db
self.collection = db.get_collection("webhooks", options)
self.profiles_collection = db.get_collection("nsys_profiles", options)
async def store_webhook(
self, event_type: str, payload: dict[str, Any], headers: dict[str, str]
) -> str:
"""Store a webhook event in MongoDB"""
document = {
"event_type": event_type,
"payload": payload,
"headers": headers,
"received_at": datetime.now(ZoneInfo(config.app.tzinfo)),
}
result = await self.collection.insert_one(document)
return str(result.inserted_id)
async def get_webhooks(self, limit: int = 50, event_type: Optional[str] = None) -> list:
"""Retrieve webhook events from MongoDB"""
query = {}
if event_type:
query["event_type"] = event_type
cursor = self.collection.find(query).sort("received_at", -1).limit(limit)
webhooks = []
async for doc in cursor:
doc["_id"] = str(doc["_id"])
webhooks.append(doc)
return webhooks
async def get_webhook_by_id(self, webhook_id: str) -> Optional[dict]:
"""Retrieve a single webhook by ID"""
try:
doc = await self.collection.find_one({"_id": ObjectId(webhook_id)})
if doc:
doc["_id"] = str(doc["_id"])
return doc
except Exception:
return None
async def get_event_types(self) -> list:
"""Get all unique event types"""
return await self.collection.distinct("event_type")
async def get_stats(self) -> dict[str, Any]:
"""Get webhook statistics"""
total = await self.collection.count_documents({})
# Get event type counts
pipeline = [
{"$group": {"_id": "$event_type", "count": {"$sum": 1}}},
{"$sort": {"count": -1}},
]
event_counts = []
# From PyMongo documentation: pymongo.asynchronous.collection.AsyncCollection.aggregate
async with await self.collection.aggregate(pipeline) as cursor:
async for doc in cursor:
event_counts.append({"event_type": doc["_id"], "count": doc["count"]})
return {"total_webhooks": total, "event_counts": event_counts}
async def get_profiles(self, limit: int = 10) -> list:
"""Retrieve profiling data from MongoDB"""
cursor = self.profiles_collection.find({}).sort("timestamp", -1).limit(limit)
profiles = []
async for doc in cursor:
doc["_id"] = str(doc["_id"])
profiles.append(doc)
return profiles
async def get_profile_by_id(self, profile_id: str) -> Optional[dict]:
"""Retrieve a single profile by ID"""
try:
doc = await self.profiles_collection.find_one({"_id": ObjectId(profile_id)})
if doc:
doc["_id"] = str(doc["_id"])
return doc
except Exception:
return None
async def on_startup(app: Litestar) -> None:
"""Initialize MongoDB connection on startup"""
logging.info("Starting GitHub Webhook Receiver...")
logging.info(f"MongoDB: {config.mongodb.url}/{config.mongodb.db}")
logging.info(f"Mongo user: {config.mongodb.user}")
logging.info(
f"Webhook Secret: {'Configured' if config.webhook.secret else 'Not configured (signatures will not be verified)'}"
)
client = AsyncMongoClient(
config.mongodb.url,
username=config.mongodb.user,
password=config.mongodb.password,
serverSelectionTimeoutMS=15000,
)
try:
await client.admin.command("ping")
db = client[config.mongodb.db]
app.state.mongo_service = MongoDBService(db)
logging.info(f"Connected to MongoDB: {config.mongodb.url}/{config.mongodb.db}")
except Exception as e:
app.state.mongo_service = None
app.state.mongo_error = e
logging.error(f"Error pinging MongoDB server: {e}")
async def on_shutdown(app: Litestar) -> None:
"""Close MongoDB connection on shutdown"""
logging.info("Application shutting down")